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 #include "nsIEffectiveTLDService.h"
8 #include "nsTHashtable.h"
11 #include "mozilla/Attributes.h"
15 #define ETLD_ENTRY_N_INDEX_BITS 30
17 // struct for static data generated from effective_tld_names.dat
19 uint32_t strtab_index
: ETLD_ENTRY_N_INDEX_BITS
;
20 uint32_t exception
: 1;
26 class nsDomainEntry
: public PLDHashEntryHdr
28 friend class nsEffectiveTLDService
;
31 typedef const char* KeyType
;
32 typedef const char* KeyTypePointer
;
34 nsDomainEntry(KeyTypePointer aEntry
)
38 nsDomainEntry(const nsDomainEntry
& toCopy
)
40 // if we end up here, things will break. nsTHashtable shouldn't
41 // allow this, since we set ALLOW_MEMMOVE to true.
42 NS_NOTREACHED("nsDomainEntry copy constructor is forbidden!");
49 KeyType
GetKey() const
51 return GetEffectiveTLDName(mData
->strtab_index
);
54 bool KeyEquals(KeyTypePointer aKey
) const
56 return !strcmp(GetKey(), aKey
);
59 static KeyTypePointer
KeyToPointer(KeyType aKey
)
64 static PLDHashNumber
HashKey(KeyTypePointer aKey
)
66 // PL_DHashStringKey doesn't use the table parameter, so we can safely
68 return PL_DHashStringKey(nullptr, aKey
);
71 enum { ALLOW_MEMMOVE
= true };
73 void SetData(const ETLDEntry
* entry
) { mData
= entry
; }
75 bool IsNormal() { return mData
->wild
|| !mData
->exception
; }
76 bool IsException() { return mData
->exception
; }
77 bool IsWild() { return mData
->wild
; }
79 static const char *GetEffectiveTLDName(size_t idx
)
81 return strings
.strtab
+ idx
;
85 const ETLDEntry
* mData
;
86 #define ETLD_STR_NUM_1(line) str##line
87 #define ETLD_STR_NUM(line) ETLD_STR_NUM_1(line)
88 struct etld_string_list
{
89 #define ETLD_ENTRY(name, ex, wild) char ETLD_STR_NUM(__LINE__)[sizeof(name)];
90 #include "etld_data.inc"
93 static const union etld_strings
{
94 struct etld_string_list list
;
97 static const ETLDEntry entries
[];
98 void FuncForStaticAsserts(void);
103 class nsEffectiveTLDService MOZ_FINAL
: public nsIEffectiveTLDService
107 NS_DECL_NSIEFFECTIVETLDSERVICE
109 nsEffectiveTLDService() { }
113 nsresult
GetBaseDomainInternal(nsCString
&aHostname
, uint32_t aAdditionalParts
, nsACString
&aBaseDomain
);
114 nsresult
NormalizeHostname(nsCString
&aHostname
);
115 ~nsEffectiveTLDService() { }
117 nsTHashtable
<nsDomainEntry
> mHash
;
118 nsCOMPtr
<nsIIDNService
> mIDNService
;