nsEffectiveTLDService.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef EffectiveTLDService_h
  6. #define EffectiveTLDService_h
  7. #include "nsIEffectiveTLDService.h"
  8. #include "nsIMemoryReporter.h"
  9. #include "nsString.h"
  10. #include "nsCOMPtr.h"
  11. #include "mozilla/Attributes.h"
  12. #include "mozilla/BinarySearch.h"
  13. #include "mozilla/MemoryReporting.h"
  14. class nsIIDNService;
  15. // struct for static data generated from effective_tld_names.dat
  16. struct ETLDEntry {
  17. friend class nsEffectiveTLDService;
  18. public:
  19. bool IsNormal() const { return wild || !exception; }
  20. bool IsException() const { return exception; }
  21. bool IsWild() const { return wild; }
  22. const char* GetEffectiveTLDName() const
  23. {
  24. return strings.strtab + strtab_index;
  25. }
  26. static const ETLDEntry* GetEntry(const char* aDomain);
  27. static const size_t ETLD_ENTRY_N_INDEX_BITS = 30;
  28. // These fields must be public to allow static construction.
  29. uint32_t strtab_index : ETLD_ENTRY_N_INDEX_BITS;
  30. uint32_t exception : 1;
  31. uint32_t wild : 1;
  32. private:
  33. struct Cmp {
  34. int operator()(const ETLDEntry aEntry) const
  35. {
  36. return strcmp(mName, aEntry.GetEffectiveTLDName());
  37. }
  38. explicit Cmp(const char* aName) : mName(aName) {}
  39. const char* mName;
  40. };
  41. #define ETLD_STR_NUM_1(line) str##line
  42. #define ETLD_STR_NUM(line) ETLD_STR_NUM_1(line)
  43. struct etld_string_list {
  44. #define ETLD_ENTRY(name, ex, wild) char ETLD_STR_NUM(__LINE__)[sizeof(name)];
  45. #include "etld_data.inc"
  46. #undef ETLD_ENTRY
  47. };
  48. // This static string table is all the eTLD domain names packed together.
  49. static const union etld_strings {
  50. struct etld_string_list list;
  51. char strtab[1];
  52. } strings;
  53. // This is the static entries table. Each entry has an index into the string
  54. // table. The entries are in sorted order so that binary search can be used.
  55. static const ETLDEntry entries[];
  56. void FuncForStaticAsserts(void);
  57. #undef ETLD_STR_NUM
  58. #undef ETLD_STR_NUM1
  59. };
  60. class nsEffectiveTLDService final
  61. : public nsIEffectiveTLDService
  62. , public nsIMemoryReporter
  63. {
  64. public:
  65. NS_DECL_ISUPPORTS
  66. NS_DECL_NSIEFFECTIVETLDSERVICE
  67. NS_DECL_NSIMEMORYREPORTER
  68. nsEffectiveTLDService();
  69. nsresult Init();
  70. size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf);
  71. private:
  72. nsresult GetBaseDomainInternal(nsCString &aHostname, int32_t aAdditionalParts, nsACString &aBaseDomain);
  73. nsresult NormalizeHostname(nsCString &aHostname);
  74. ~nsEffectiveTLDService();
  75. nsCOMPtr<nsIIDNService> mIDNService;
  76. };
  77. #endif // EffectiveTLDService_h