1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "mozilla/DataMutex.h"
10 #include "nsHostResolver.h"
11 #include "nsIObserver.h"
13 #include "nsWeakReference.h"
14 #include "ODoHService.h"
15 #include "TRRServiceBase.h"
19 class nsINetworkLinkService
;
20 class nsIObserverService
;
25 class TRRServiceChild
;
26 class TRRServiceParent
;
28 class TRRService
: public TRRServiceBase
,
30 public nsITimerCallback
,
31 public nsSupportsWeakReference
,
32 public AHostResolver
{
34 NS_DECL_THREADSAFE_ISUPPORTS
36 NS_DECL_NSITIMERCALLBACK
41 bool Enabled(nsIRequest::TRRMode aMode
= nsIRequest::TRR_FIRST_MODE
);
42 bool IsConfirmed() { return mConfirmationState
== CONFIRM_OK
; }
44 bool DisableIPv6() { return mDisableIPv6
; }
45 nsresult
GetURI(nsACString
& result
);
46 nsresult
GetCredentials(nsCString
& result
);
47 uint32_t GetRequestTimeout();
49 LookupStatus
CompleteLookup(nsHostRecord
*, nsresult
, mozilla::net::AddrInfo
*,
50 bool pb
, const nsACString
& aOriginSuffix
,
51 nsHostRecord::TRRSkippedReason aReason
) override
;
52 LookupStatus
CompleteLookupByType(nsHostRecord
*, nsresult
,
53 mozilla::net::TypeRecordResultType
&,
54 uint32_t, bool pb
) override
;
55 void AddToBlocklist(const nsACString
& host
, const nsACString
& originSuffix
,
56 bool privateBrowsing
, bool aParentsToo
);
57 bool IsTemporarilyBlocked(const nsACString
& aHost
,
58 const nsACString
& aOriginSuffix
,
59 bool aPrivateBrowsing
, bool aParentsToo
);
60 bool IsExcludedFromTRR(const nsACString
& aHost
);
62 bool MaybeBootstrap(const nsACString
& possible
, nsACString
& result
);
63 enum TrrOkay
{ OKAY_NORMAL
= 0, OKAY_TIMEOUT
= 1, OKAY_BAD
= 2 };
64 void TRRIsOkay(enum TrrOkay aReason
);
65 bool ParentalControlEnabled() const { return mParentalControlEnabled
; }
67 nsresult
DispatchTRRRequest(TRR
* aTrrRequest
);
68 already_AddRefed
<nsIThread
> TRRThread();
71 bool IsUsingAutoDetectedURL() { return mURISetByDetection
; }
72 static const nsCString
& AutoDetectedKey();
75 virtual ~TRRService();
77 friend class TRRServiceChild
;
78 friend class TRRServiceParent
;
79 friend class ODoHService
;
80 static void AddObserver(nsIObserver
* aObserver
,
81 nsIObserverService
* aObserverService
= nullptr);
82 static bool CheckCaptivePortalIsPassed();
83 static bool GetParentalControlEnabledInternal();
84 static bool CheckPlatformDNSStatus(nsINetworkLinkService
* aLinkService
);
86 nsresult
ReadPrefs(const char* name
);
87 void GetPrefBranch(nsIPrefBranch
** result
);
89 void MaybeConfirm_locked();
90 friend class ::nsDNSService
;
91 void SetDetectedTrrURI(const nsACString
& aURI
);
93 bool IsDomainBlocked(const nsACString
& aHost
, const nsACString
& aOriginSuffix
,
94 bool aPrivateBrowsing
);
95 bool IsExcludedFromTRR_unlocked(const nsACString
& aHost
);
97 void RebuildSuffixList(nsTArray
<nsCString
>&& aSuffixList
);
99 nsresult
DispatchTRRRequestInternal(TRR
* aTrrRequest
, bool aWithLock
);
100 already_AddRefed
<nsIThread
> TRRThread_locked();
101 already_AddRefed
<nsIThread
> MainThreadOrTRRThread(bool aWithLock
= true);
103 // This method will process the URI and try to set mPrivateURI to that value.
104 // Will return true if performed the change (if the value was different)
105 // or false if mPrivateURI already had that value.
106 bool MaybeSetPrivateURI(const nsACString
& aURI
) override
;
107 void ClearEntireCache();
109 virtual void ReadEtcHostsFile() override
;
110 void AddEtcHosts(const nsTArray
<nsCString
>&);
113 Atomic
<uint32_t, Relaxed
> mBlocklistDurationSeconds
;
117 nsCString mPrivateCred
; // main thread only
118 nsCString mConfirmationNS
;
119 nsCString mBootstrapAddr
;
121 Atomic
<bool, Relaxed
>
122 mCaptiveIsPassed
; // set when captive portal check is passed
123 Atomic
<bool, Relaxed
> mDisableIPv6
; // don't even try
125 // TRR Blocklist storage
126 // mTRRBLStorage is only modified on the main thread, but we query whether it
127 // is initialized or not off the main thread as well. Therefore we need to
128 // lock while creating it and while accessing it off the main thread.
129 DataMutex
<nsDataHashtable
<nsCStringHashKey
, int32_t>> mTRRBLStorage
;
131 // A set of domains that we should not use TRR for.
132 nsTHashtable
<nsCStringHashKey
> mExcludedDomains
;
133 nsTHashtable
<nsCStringHashKey
> mDNSSuffixDomains
;
134 nsTHashtable
<nsCStringHashKey
> mEtcHostsDomains
;
136 enum ConfirmationState
{
142 Atomic
<ConfirmationState
, Relaxed
> mConfirmationState
;
143 RefPtr
<TRR
> mConfirmer
;
144 nsCOMPtr
<nsITimer
> mRetryConfirmTimer
;
145 uint32_t mRetryConfirmInterval
; // milliseconds until retry
146 Atomic
<uint32_t, Relaxed
> mTRRFailures
;
147 bool mParentalControlEnabled
;
148 RefPtr
<ODoHService
> mODoHService
;
151 extern TRRService
* gTRRService
;
154 } // namespace mozilla
156 #endif // TRRService_h_