Bug 1861709 replace AudioCallbackDriver::ThreadRunning() assertions that mean to...
[gecko.git] / netwerk / dns / nsEffectiveTLDService.h
blobe68156582a706e5254b53e8cd5f24b71b455facc
1 //* -*- Mode: C++; tab-width: 2; 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/. */
6 #ifndef EffectiveTLDService_h
7 #define EffectiveTLDService_h
9 #include "nsIEffectiveTLDService.h"
11 #include "mozilla/AutoMemMap.h"
12 #include "mozilla/Attributes.h"
13 #include "mozilla/Dafsa.h"
14 #include "mozilla/Maybe.h"
15 #include "mozilla/MemoryReporting.h"
16 #include "mozilla/MruCache.h"
17 #include "mozilla/RWLock.h"
19 #include "nsCOMPtr.h"
20 #include "nsHashKeys.h"
21 #include "nsIMemoryReporter.h"
22 #include "nsIObserver.h"
23 #include "nsString.h"
25 class nsIIDNService;
27 class nsEffectiveTLDService final : public nsIEffectiveTLDService,
28 public nsIObserver,
29 public nsIMemoryReporter {
30 public:
31 NS_DECL_THREADSAFE_ISUPPORTS
32 NS_DECL_NSIEFFECTIVETLDSERVICE
33 NS_DECL_NSIMEMORYREPORTER
34 NS_DECL_NSIOBSERVER
36 nsEffectiveTLDService();
37 nsresult Init();
39 static nsEffectiveTLDService* GetInstance();
41 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf);
43 private:
44 nsresult GetBaseDomainInternal(nsCString& aHostname, int32_t aAdditionalParts,
45 bool aOnlyKnownPublicSuffix,
46 nsACString& aBaseDomain);
47 nsresult NormalizeHostname(nsCString& aHostname);
48 ~nsEffectiveTLDService();
50 // Only set in `Init()` which is called during service construction.
51 nsCOMPtr<nsIIDNService> mIDNService;
53 // The DAFSA provides a compact encoding of the rather large eTLD list.
54 mozilla::Maybe<mozilla::Dafsa> mGraph MOZ_GUARDED_BY(mGraphLock);
56 // Memory map used for a new updated dafsa
57 mozilla::loader::AutoMemMap mDafsaMap MOZ_GUARDED_BY(mGraphLock);
59 // Lock for mGraph and mDafsaMap
60 mozilla::RWLock mGraphLock;
62 // Note that the cache entries here can record entries that were cached
63 // successfully or unsuccessfully. mResult must be checked before using an
64 // entry. If it's a success error code, the cache entry is valid and can be
65 // used.
66 struct TLDCacheEntry {
67 nsCString mHost;
68 nsCString mBaseDomain;
69 nsresult mResult;
72 // We use a small most recently used cache to compensate for DAFSA lookups
73 // being slightly slower than a binary search on a larger table of strings.
75 // We first check the cache for a matching result and avoid a DAFSA lookup
76 // if a match is found. Otherwise we lookup the domain in the DAFSA and then
77 // cache the result. During standard browsing the same domains are repeatedly
78 // fed into |GetBaseDomainInternal| so this ends up being an effective
79 // mitigation getting about a 99% hit rate with four tabs open.
80 struct TldCache
81 : public mozilla::MruCache<nsACString, TLDCacheEntry, TldCache> {
82 static mozilla::HashNumber Hash(const nsACString& aKey) {
83 return mozilla::HashString(aKey);
85 static bool Match(const nsACString& aKey, const TLDCacheEntry& aVal) {
86 return aKey == aVal.mHost;
90 // NOTE: Only used on the main thread, so not guarded by mGraphLock.
91 TldCache mMruTable;
94 #endif // EffectiveTLDService_h