Backed out changeset 1893713e1d17 (bug 1930292) for causing bc failures @ browser_fin...
[gecko.git] / netwerk / dns / nsEffectiveTLDService.h
blob1433bf616fde8284b9a7cbd0103959bcb71cbd21
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 ~nsEffectiveTLDService();
49 // The DAFSA provides a compact encoding of the rather large eTLD list.
50 mozilla::Maybe<mozilla::Dafsa> mGraph MOZ_GUARDED_BY(mGraphLock);
52 // Memory map used for a new updated dafsa
53 mozilla::loader::AutoMemMap mDafsaMap MOZ_GUARDED_BY(mGraphLock);
55 // Lock for mGraph and mDafsaMap
56 mozilla::RWLock mGraphLock;
58 // Note that the cache entries here can record entries that were cached
59 // successfully or unsuccessfully. mResult must be checked before using an
60 // entry. If it's a success error code, the cache entry is valid and can be
61 // used.
62 struct TLDCacheEntry {
63 nsCString mHost;
64 nsCString mBaseDomain;
65 nsresult mResult;
68 // We use a small most recently used cache to compensate for DAFSA lookups
69 // being slightly slower than a binary search on a larger table of strings.
71 // We first check the cache for a matching result and avoid a DAFSA lookup
72 // if a match is found. Otherwise we lookup the domain in the DAFSA and then
73 // cache the result. During standard browsing the same domains are repeatedly
74 // fed into |GetBaseDomainInternal| so this ends up being an effective
75 // mitigation getting about a 99% hit rate with four tabs open.
76 struct TldCache
77 : public mozilla::MruCache<nsACString, TLDCacheEntry, TldCache> {
78 static mozilla::HashNumber Hash(const nsACString& aKey) {
79 return mozilla::HashString(aKey);
81 static bool Match(const nsACString& aKey, const TLDCacheEntry& aVal) {
82 return aKey == aVal.mHost;
86 // NOTE: Only used on the main thread, so not guarded by mGraphLock.
87 TldCache mMruTable;
90 #endif // EffectiveTLDService_h