Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / dns / nsEffectiveTLDService.h
blobe6777b658a849c0b6f8d6fb04882eb6043d42eb4
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 "nsString.h"
24 class nsIIDNService;
26 class nsEffectiveTLDService final : public nsIEffectiveTLDService,
27 public nsIMemoryReporter {
28 public:
29 NS_DECL_THREADSAFE_ISUPPORTS
30 NS_DECL_NSIEFFECTIVETLDSERVICE
31 NS_DECL_NSIMEMORYREPORTER
33 nsEffectiveTLDService();
34 nsresult Init();
36 static nsEffectiveTLDService* GetInstance();
38 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf);
40 private:
41 nsresult GetBaseDomainInternal(nsCString& aHostname, int32_t aAdditionalParts,
42 bool aOnlyKnownPublicSuffix,
43 nsACString& aBaseDomain);
44 ~nsEffectiveTLDService();
46 // The DAFSA provides a compact encoding of the rather large eTLD list.
47 mozilla::Dafsa mGraph;
49 // Note that the cache entries here can record entries that were cached
50 // successfully or unsuccessfully. mResult must be checked before using an
51 // entry. If it's a success error code, the cache entry is valid and can be
52 // used.
53 struct TLDCacheEntry {
54 nsCString mHost;
55 nsCString mBaseDomain;
56 nsresult mResult;
59 // We use a small most recently used cache to compensate for DAFSA lookups
60 // being slightly slower than a binary search on a larger table of strings.
62 // We first check the cache for a matching result and avoid a DAFSA lookup
63 // if a match is found. Otherwise we lookup the domain in the DAFSA and then
64 // cache the result. During standard browsing the same domains are repeatedly
65 // fed into |GetBaseDomainInternal| so this ends up being an effective
66 // mitigation getting about a 99% hit rate with four tabs open.
67 struct TldCache
68 : public mozilla::MruCache<nsACString, TLDCacheEntry, TldCache> {
69 static mozilla::HashNumber Hash(const nsACString& aKey) {
70 return mozilla::HashString(aKey);
72 static bool Match(const nsACString& aKey, const TLDCacheEntry& aVal) {
73 return aKey == aVal.mHost;
77 // NOTE: Only used on the main thread, so not guarded by mGraphLock.
78 TldCache mMruTable;
81 #endif // EffectiveTLDService_h