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
nsACString& aBaseDomain);
46
nsresult NormalizeHostname(nsCString& aHostname);
47
~nsEffectiveTLDService();
48
49
nsCOMPtr<nsIIDNService> mIDNService;
50
51
// The DAFSA provides a compact encoding of the rather large eTLD list.
52
mozilla::Maybe<mozilla::Dafsa> mGraph;
53
54
// Memory map used for a new updated dafsa
55
mozilla::loader::AutoMemMap mDafsaMap;
56
57
// Lock for mGraph and mDafsaMap
58
mozilla::RWLock mGraphLock;
59
60
// Note that the cache entries here can record entries that were cached
61
// successfully or unsuccessfully. mResult must be checked before using an
62
// entry. If it's a success error code, the cache entry is valid and can be
63
// used.
64
struct TLDCacheEntry {
65
nsCString mHost;
66
nsCString mBaseDomain;
67
nsresult mResult;
68
};
69
70
// We use a small most recently used cache to compensate for DAFSA lookups
71
// being slightly slower than a binary search on a larger table of strings.
72
//
73
// We first check the cache for a matching result and avoid a DAFSA lookup
74
// if a match is found. Otherwise we lookup the domain in the DAFSA and then
75
// cache the result. During standard browsing the same domains are repeatedly
76
// fed into |GetBaseDomainInternal| so this ends up being an effective
77
// mitigation getting about a 99% hit rate with four tabs open.
78
struct TldCache
79
: public mozilla::MruCache<nsACString, TLDCacheEntry, TldCache> {
80
static mozilla::HashNumber Hash(const nsACString& aKey) {
81
return mozilla::HashString(aKey);
82
}
83
static bool Match(const nsACString& aKey, const TLDCacheEntry& aVal) {
84
return aKey == aVal.mHost;
85
}
86
};
87
88
TldCache mMruTable;
89
};
90
91
#endif // EffectiveTLDService_h