|
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 "nsIMemoryReporter.h" |
|
12 #include "nsTHashtable.h" |
|
13 #include "nsString.h" |
|
14 #include "nsCOMPtr.h" |
|
15 #include "mozilla/Attributes.h" |
|
16 #include "mozilla/MemoryReporting.h" |
|
17 |
|
18 class nsIIDNService; |
|
19 |
|
20 #define ETLD_ENTRY_N_INDEX_BITS 30 |
|
21 |
|
22 // struct for static data generated from effective_tld_names.dat |
|
23 struct ETLDEntry { |
|
24 uint32_t strtab_index : ETLD_ENTRY_N_INDEX_BITS; |
|
25 uint32_t exception : 1; |
|
26 uint32_t wild : 1; |
|
27 }; |
|
28 |
|
29 |
|
30 // hash entry class |
|
31 class nsDomainEntry : public PLDHashEntryHdr |
|
32 { |
|
33 friend class nsEffectiveTLDService; |
|
34 public: |
|
35 // Hash methods |
|
36 typedef const char* KeyType; |
|
37 typedef const char* KeyTypePointer; |
|
38 |
|
39 nsDomainEntry(KeyTypePointer aEntry) |
|
40 { |
|
41 } |
|
42 |
|
43 nsDomainEntry(const nsDomainEntry& toCopy) |
|
44 { |
|
45 // if we end up here, things will break. nsTHashtable shouldn't |
|
46 // allow this, since we set ALLOW_MEMMOVE to true. |
|
47 NS_NOTREACHED("nsDomainEntry copy constructor is forbidden!"); |
|
48 } |
|
49 |
|
50 ~nsDomainEntry() |
|
51 { |
|
52 } |
|
53 |
|
54 KeyType GetKey() const |
|
55 { |
|
56 return GetEffectiveTLDName(mData->strtab_index); |
|
57 } |
|
58 |
|
59 bool KeyEquals(KeyTypePointer aKey) const |
|
60 { |
|
61 return !strcmp(GetKey(), aKey); |
|
62 } |
|
63 |
|
64 static KeyTypePointer KeyToPointer(KeyType aKey) |
|
65 { |
|
66 return aKey; |
|
67 } |
|
68 |
|
69 static PLDHashNumber HashKey(KeyTypePointer aKey) |
|
70 { |
|
71 // PL_DHashStringKey doesn't use the table parameter, so we can safely |
|
72 // pass nullptr |
|
73 return PL_DHashStringKey(nullptr, aKey); |
|
74 } |
|
75 |
|
76 enum { ALLOW_MEMMOVE = true }; |
|
77 |
|
78 void SetData(const ETLDEntry* entry) { mData = entry; } |
|
79 |
|
80 bool IsNormal() { return mData->wild || !mData->exception; } |
|
81 bool IsException() { return mData->exception; } |
|
82 bool IsWild() { return mData->wild; } |
|
83 |
|
84 static const char *GetEffectiveTLDName(size_t idx) |
|
85 { |
|
86 return strings.strtab + idx; |
|
87 } |
|
88 |
|
89 private: |
|
90 const ETLDEntry* mData; |
|
91 #define ETLD_STR_NUM_1(line) str##line |
|
92 #define ETLD_STR_NUM(line) ETLD_STR_NUM_1(line) |
|
93 struct etld_string_list { |
|
94 #define ETLD_ENTRY(name, ex, wild) char ETLD_STR_NUM(__LINE__)[sizeof(name)]; |
|
95 #include "etld_data.inc" |
|
96 #undef ETLD_ENTRY |
|
97 }; |
|
98 static const union etld_strings { |
|
99 struct etld_string_list list; |
|
100 char strtab[1]; |
|
101 } strings; |
|
102 static const ETLDEntry entries[]; |
|
103 void FuncForStaticAsserts(void); |
|
104 #undef ETLD_STR_NUM |
|
105 #undef ETLD_STR_NUM1 |
|
106 }; |
|
107 |
|
108 class nsEffectiveTLDService MOZ_FINAL |
|
109 : public nsIEffectiveTLDService |
|
110 , public nsIMemoryReporter |
|
111 { |
|
112 public: |
|
113 NS_DECL_ISUPPORTS |
|
114 NS_DECL_NSIEFFECTIVETLDSERVICE |
|
115 NS_DECL_NSIMEMORYREPORTER |
|
116 |
|
117 nsEffectiveTLDService(); |
|
118 nsresult Init(); |
|
119 |
|
120 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf); |
|
121 |
|
122 private: |
|
123 nsresult GetBaseDomainInternal(nsCString &aHostname, int32_t aAdditionalParts, nsACString &aBaseDomain); |
|
124 nsresult NormalizeHostname(nsCString &aHostname); |
|
125 ~nsEffectiveTLDService(); |
|
126 |
|
127 nsTHashtable<nsDomainEntry> mHash; |
|
128 nsCOMPtr<nsIIDNService> mIDNService; |
|
129 }; |
|
130 |
|
131 #endif // EffectiveTLDService_h |