michael@0: //* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef EffectiveTLDService_h michael@0: #define EffectiveTLDService_h michael@0: michael@0: #include "nsIEffectiveTLDService.h" michael@0: michael@0: #include "nsIMemoryReporter.h" michael@0: #include "nsTHashtable.h" michael@0: #include "nsString.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "mozilla/Attributes.h" michael@0: #include "mozilla/MemoryReporting.h" michael@0: michael@0: class nsIIDNService; michael@0: michael@0: #define ETLD_ENTRY_N_INDEX_BITS 30 michael@0: michael@0: // struct for static data generated from effective_tld_names.dat michael@0: struct ETLDEntry { michael@0: uint32_t strtab_index : ETLD_ENTRY_N_INDEX_BITS; michael@0: uint32_t exception : 1; michael@0: uint32_t wild : 1; michael@0: }; michael@0: michael@0: michael@0: // hash entry class michael@0: class nsDomainEntry : public PLDHashEntryHdr michael@0: { michael@0: friend class nsEffectiveTLDService; michael@0: public: michael@0: // Hash methods michael@0: typedef const char* KeyType; michael@0: typedef const char* KeyTypePointer; michael@0: michael@0: nsDomainEntry(KeyTypePointer aEntry) michael@0: { michael@0: } michael@0: michael@0: nsDomainEntry(const nsDomainEntry& toCopy) michael@0: { michael@0: // if we end up here, things will break. nsTHashtable shouldn't michael@0: // allow this, since we set ALLOW_MEMMOVE to true. michael@0: NS_NOTREACHED("nsDomainEntry copy constructor is forbidden!"); michael@0: } michael@0: michael@0: ~nsDomainEntry() michael@0: { michael@0: } michael@0: michael@0: KeyType GetKey() const michael@0: { michael@0: return GetEffectiveTLDName(mData->strtab_index); michael@0: } michael@0: michael@0: bool KeyEquals(KeyTypePointer aKey) const michael@0: { michael@0: return !strcmp(GetKey(), aKey); michael@0: } michael@0: michael@0: static KeyTypePointer KeyToPointer(KeyType aKey) michael@0: { michael@0: return aKey; michael@0: } michael@0: michael@0: static PLDHashNumber HashKey(KeyTypePointer aKey) michael@0: { michael@0: // PL_DHashStringKey doesn't use the table parameter, so we can safely michael@0: // pass nullptr michael@0: return PL_DHashStringKey(nullptr, aKey); michael@0: } michael@0: michael@0: enum { ALLOW_MEMMOVE = true }; michael@0: michael@0: void SetData(const ETLDEntry* entry) { mData = entry; } michael@0: michael@0: bool IsNormal() { return mData->wild || !mData->exception; } michael@0: bool IsException() { return mData->exception; } michael@0: bool IsWild() { return mData->wild; } michael@0: michael@0: static const char *GetEffectiveTLDName(size_t idx) michael@0: { michael@0: return strings.strtab + idx; michael@0: } michael@0: michael@0: private: michael@0: const ETLDEntry* mData; michael@0: #define ETLD_STR_NUM_1(line) str##line michael@0: #define ETLD_STR_NUM(line) ETLD_STR_NUM_1(line) michael@0: struct etld_string_list { michael@0: #define ETLD_ENTRY(name, ex, wild) char ETLD_STR_NUM(__LINE__)[sizeof(name)]; michael@0: #include "etld_data.inc" michael@0: #undef ETLD_ENTRY michael@0: }; michael@0: static const union etld_strings { michael@0: struct etld_string_list list; michael@0: char strtab[1]; michael@0: } strings; michael@0: static const ETLDEntry entries[]; michael@0: void FuncForStaticAsserts(void); michael@0: #undef ETLD_STR_NUM michael@0: #undef ETLD_STR_NUM1 michael@0: }; michael@0: michael@0: class nsEffectiveTLDService MOZ_FINAL michael@0: : public nsIEffectiveTLDService michael@0: , public nsIMemoryReporter michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIEFFECTIVETLDSERVICE michael@0: NS_DECL_NSIMEMORYREPORTER michael@0: michael@0: nsEffectiveTLDService(); michael@0: nsresult Init(); michael@0: michael@0: size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf); michael@0: michael@0: private: michael@0: nsresult GetBaseDomainInternal(nsCString &aHostname, int32_t aAdditionalParts, nsACString &aBaseDomain); michael@0: nsresult NormalizeHostname(nsCString &aHostname); michael@0: ~nsEffectiveTLDService(); michael@0: michael@0: nsTHashtable mHash; michael@0: nsCOMPtr mIDNService; michael@0: }; michael@0: michael@0: #endif // EffectiveTLDService_h