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/. */
7 #include "mozilla/net/TRRServiceChild.h"
8 #include "mozilla/Atomics.h"
9 #include "mozilla/ClearOnShutdown.h"
10 #include "mozilla/Services.h"
11 #include "mozilla/StaticPtr.h"
12 #include "nsHttpConnectionInfo.h"
13 #include "nsIDNService.h"
14 #include "nsIObserverService.h"
15 #include "nsServiceManagerUtils.h"
16 #include "TRRService.h"
21 static StaticRefPtr
<nsIDNSService
> sDNSService
;
22 static Atomic
<TRRServiceChild
*> sTRRServiceChild
;
24 NS_IMPL_ISUPPORTS(TRRServiceChild
, nsIObserver
, nsISupportsWeakReference
)
26 TRRServiceChild::TRRServiceChild() {
27 MOZ_ASSERT(NS_IsMainThread());
28 sTRRServiceChild
= this;
31 TRRServiceChild::~TRRServiceChild() {
32 MOZ_ASSERT(NS_IsMainThread());
33 sTRRServiceChild
= nullptr;
37 TRRServiceChild
* TRRServiceChild::GetSingleton() {
38 MOZ_ASSERT(NS_IsMainThread());
39 return sTRRServiceChild
;
42 void TRRServiceChild::Init(const bool& aCaptiveIsPassed
,
43 const bool& aParentalControlEnabled
,
44 nsTArray
<nsCString
>&& aDNSSuffixList
) {
45 nsCOMPtr
<nsIDNSService
> dns
=
46 do_GetService("@mozilla.org/network/dns-service;1");
48 ClearOnShutdown(&sDNSService
);
49 MOZ_ASSERT(sDNSService
);
51 TRRService
* trrService
= TRRService::Get();
52 MOZ_ASSERT(trrService
);
54 trrService
->mCaptiveIsPassed
= aCaptiveIsPassed
;
55 trrService
->mParentalControlEnabled
= aParentalControlEnabled
;
56 trrService
->RebuildSuffixList(std::move(aDNSSuffixList
));
58 nsCOMPtr
<nsIObserverService
> obs
= mozilla::services::GetObserverService();
59 obs
->AddObserver(this, "network:connectivity-service:dns-checks-complete",
61 obs
->AddObserver(this, "network:connectivity-service:ip-checks-complete",
66 TRRServiceChild::Observe(nsISupports
* aSubject
, const char* aTopic
,
67 const char16_t
* aData
) {
68 if (!strcmp(aTopic
, "network:connectivity-service:ip-checks-complete") ||
69 !strcmp(aTopic
, "network:connectivity-service:dns-checks-complete")) {
70 Unused
<< SendNotifyNetworkConnectivityServiceObservers(
71 nsPrintfCString("%s-from-socket-process", aTopic
));
77 mozilla::ipc::IPCResult
TRRServiceChild::RecvUpdatePlatformDNSInformation(
78 nsTArray
<nsCString
>&& aDNSSuffixList
) {
79 TRRService::Get()->RebuildSuffixList(std::move(aDNSSuffixList
));
83 mozilla::ipc::IPCResult
TRRServiceChild::RecvUpdateParentalControlEnabled(
84 const bool& aEnabled
) {
85 TRRService::Get()->mParentalControlEnabled
= aEnabled
;
89 mozilla::ipc::IPCResult
TRRServiceChild::RecvClearDNSCache(
90 const bool& aTrrToo
) {
91 Unused
<< sDNSService
->ClearCache(aTrrToo
);
95 mozilla::ipc::IPCResult
TRRServiceChild::RecvSetDetectedTrrURI(
96 const nsCString
& aURI
) {
97 TRRService::Get()->SetDetectedTrrURI(aURI
);
101 mozilla::ipc::IPCResult
TRRServiceChild::RecvSetDefaultTRRConnectionInfo(
102 Maybe
<HttpConnectionInfoCloneArgs
>&& aArgs
) {
104 TRRService::Get()->SetDefaultTRRConnectionInfo(nullptr);
108 RefPtr
<nsHttpConnectionInfo
> cinfo
=
109 nsHttpConnectionInfo::DeserializeHttpConnectionInfoCloneArgs(aArgs
.ref());
110 TRRService::Get()->SetDefaultTRRConnectionInfo(cinfo
);
114 mozilla::ipc::IPCResult
TRRServiceChild::RecvUpdateEtcHosts(
115 nsTArray
<nsCString
>&& aHosts
) {
116 TRRService::Get()->AddEtcHosts(aHosts
);
121 } // namespace mozilla