Source code

Revision control

Other Tools

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/. */
5
6
#ifndef EffectiveTLDService_h
7
#define EffectiveTLDService_h
8
9
#include "nsIEffectiveTLDService.h"
10
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"
18
19
#include "nsCOMPtr.h"
20
#include "nsHashKeys.h"
21
#include "nsIMemoryReporter.h"
22
#include "nsIObserver.h"
23
#include "nsString.h"
24
25
class nsIIDNService;
26
27
class nsEffectiveTLDService final : public nsIEffectiveTLDService,
28
public nsIObserver,
29
public nsIMemoryReporter {
30
public:
31
NS_DECL_ISUPPORTS
32
NS_DECL_NSIEFFECTIVETLDSERVICE
33
NS_DECL_NSIMEMORYREPORTER
34
NS_DECL_NSIOBSERVER
35
36
nsEffectiveTLDService();
37
nsresult Init();
38
39
static nsEffectiveTLDService* GetInstance();
40
41
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf);
42
43
private:
44
nsresult GetBaseDomainInternal(nsCString& aHostname, int32_t aAdditionalParts,
45
bool aOnlyKnownPublicSuffix,
46
nsACString& aBaseDomain);
47
nsresult NormalizeHostname(nsCString& aHostname);
48
~nsEffectiveTLDService();
49
50
nsCOMPtr<nsIIDNService> mIDNService;
51
52
// The DAFSA provides a compact encoding of the rather large eTLD list.
53
mozilla::Maybe<mozilla::Dafsa> mGraph;
54
55
// Memory map used for a new updated dafsa
56
mozilla::loader::AutoMemMap mDafsaMap;
57
58
// Lock for mGraph and mDafsaMap
59
mozilla::RWLock mGraphLock;
60
61
// Note that the cache entries here can record entries that were cached
62
// successfully or unsuccessfully. mResult must be checked before using an
63
// entry. If it's a success error code, the cache entry is valid and can be
64
// used.
65
struct TLDCacheEntry {
66
nsCString mHost;
67
nsCString mBaseDomain;
68
nsresult mResult;
69
};
70
71
// We use a small most recently used cache to compensate for DAFSA lookups
72
// being slightly slower than a binary search on a larger table of strings.
73
//
74
// We first check the cache for a matching result and avoid a DAFSA lookup
75
// if a match is found. Otherwise we lookup the domain in the DAFSA and then
76
// cache the result. During standard browsing the same domains are repeatedly
77
// fed into |GetBaseDomainInternal| so this ends up being an effective
78
// mitigation getting about a 99% hit rate with four tabs open.
79
struct TldCache
80
: public mozilla::MruCache<nsACString, TLDCacheEntry, TldCache> {
81
static mozilla::HashNumber Hash(const nsACString& aKey) {
82
return mozilla::HashString(aKey);
83
}
84
static bool Match(const nsACString& aKey, const TLDCacheEntry& aVal) {
85
return aKey == aVal.mHost;
86
}
87
};
88
89
TldCache mMruTable;
90
};
91
92
#endif // EffectiveTLDService_h