tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

TRRServiceChild.cpp (3857B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set sw=2 ts=8 et tw=80 : */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "mozilla/net/TRRServiceChild.h"
      8 #include "mozilla/Atomics.h"
      9 #include "mozilla/Components.h"
     10 #include "mozilla/ClearOnShutdown.h"
     11 #include "mozilla/Services.h"
     12 #include "mozilla/StaticPtr.h"
     13 #include "nsHttpConnectionInfo.h"
     14 #include "nsIDNService.h"
     15 #include "nsIObserverService.h"
     16 #include "nsServiceManagerUtils.h"
     17 #include "TRRService.h"
     18 
     19 namespace mozilla {
     20 namespace net {
     21 
     22 static StaticRefPtr<nsIDNSService> sDNSService;
     23 static Atomic<TRRServiceChild*> sTRRServiceChild;
     24 
     25 NS_IMPL_ISUPPORTS(TRRServiceChild, nsIObserver, nsISupportsWeakReference)
     26 
     27 TRRServiceChild::TRRServiceChild() {
     28  MOZ_ASSERT(NS_IsMainThread());
     29  sTRRServiceChild = this;
     30 }
     31 
     32 TRRServiceChild::~TRRServiceChild() {
     33  MOZ_ASSERT(NS_IsMainThread());
     34  sTRRServiceChild = nullptr;
     35 }
     36 
     37 /* static */
     38 TRRServiceChild* TRRServiceChild::GetSingleton() {
     39  MOZ_ASSERT(NS_IsMainThread());
     40  return sTRRServiceChild;
     41 }
     42 
     43 void TRRServiceChild::Init(const bool& aCaptiveIsPassed,
     44                           const bool& aParentalControlEnabled,
     45                           nsTArray<nsCString>&& aDNSSuffixList) {
     46  nsCOMPtr<nsIDNSService> dns;
     47  dns = mozilla::components::DNS::Service();
     48  sDNSService = dns;
     49  ClearOnShutdown(&sDNSService);
     50  MOZ_ASSERT(sDNSService);
     51 
     52  TRRService* trrService = TRRService::Get();
     53  MOZ_ASSERT(trrService);
     54 
     55  trrService->mCaptiveIsPassed = aCaptiveIsPassed;
     56  trrService->mParentalControlEnabled = aParentalControlEnabled;
     57  trrService->RebuildSuffixList(std::move(aDNSSuffixList));
     58 
     59  nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
     60  obs->AddObserver(this, "network:connectivity-service:dns-checks-complete",
     61                   true);
     62  obs->AddObserver(this, "network:connectivity-service:ip-checks-complete",
     63                   true);
     64 }
     65 
     66 NS_IMETHODIMP
     67 TRRServiceChild::Observe(nsISupports* aSubject, const char* aTopic,
     68                         const char16_t* aData) {
     69  if (!strcmp(aTopic, "network:connectivity-service:ip-checks-complete") ||
     70      !strcmp(aTopic, "network:connectivity-service:dns-checks-complete")) {
     71    (void)SendNotifyNetworkConnectivityServiceObservers(
     72        nsPrintfCString("%s-from-socket-process", aTopic));
     73  }
     74 
     75  return NS_OK;
     76 }
     77 
     78 mozilla::ipc::IPCResult TRRServiceChild::RecvUpdatePlatformDNSInformation(
     79    nsTArray<nsCString>&& aDNSSuffixList) {
     80  TRRService::Get()->RebuildSuffixList(std::move(aDNSSuffixList));
     81  return IPC_OK();
     82 }
     83 
     84 mozilla::ipc::IPCResult TRRServiceChild::RecvUpdateParentalControlEnabled(
     85    const bool& aEnabled) {
     86  TRRService::Get()->mParentalControlEnabled = aEnabled;
     87  return IPC_OK();
     88 }
     89 
     90 mozilla::ipc::IPCResult TRRServiceChild::RecvClearDNSCache(
     91    const bool& aTrrToo) {
     92  (void)sDNSService->ClearCache(aTrrToo);
     93  return IPC_OK();
     94 }
     95 
     96 mozilla::ipc::IPCResult TRRServiceChild::RecvSetDetectedTrrURI(
     97    const nsCString& aURI) {
     98  TRRService::Get()->SetDetectedTrrURI(aURI);
     99  return IPC_OK();
    100 }
    101 
    102 mozilla::ipc::IPCResult TRRServiceChild::RecvSetDefaultTRRConnectionInfo(
    103    Maybe<HttpConnectionInfoCloneArgs>&& aArgs) {
    104  if (!aArgs) {
    105    TRRService::Get()->SetDefaultTRRConnectionInfo(nullptr);
    106    return IPC_OK();
    107  }
    108 
    109  RefPtr<nsHttpConnectionInfo> cinfo =
    110      nsHttpConnectionInfo::DeserializeHttpConnectionInfoCloneArgs(aArgs.ref());
    111  TRRService::Get()->SetDefaultTRRConnectionInfo(cinfo);
    112  return IPC_OK();
    113 }
    114 
    115 mozilla::ipc::IPCResult TRRServiceChild::RecvUpdateEtcHosts(
    116    nsTArray<nsCString>&& aHosts) {
    117  TRRService::Get()->AddEtcHosts(aHosts);
    118  return IPC_OK();
    119 }
    120 
    121 }  // namespace net
    122 }  // namespace mozilla