summaryrefslogtreecommitdiffstats
path: root/netwerk/dns/nsEffectiveTLDService.h
blob: 92e7abe059d67926c50e824a814c112e6a66891a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef EffectiveTLDService_h
#define EffectiveTLDService_h

#include "nsIEffectiveTLDService.h"

#include "nsIMemoryReporter.h"
#include "nsString.h"
#include "nsCOMPtr.h"
#include "mozilla/Attributes.h"
#include "mozilla/BinarySearch.h"
#include "mozilla/MemoryReporting.h"

class nsIIDNService;

// struct for static data generated from effective_tld_names.dat
struct ETLDEntry {
  friend class nsEffectiveTLDService;

public:
  bool IsNormal()    const { return wild || !exception; }
  bool IsException() const { return exception; }
  bool IsWild()      const { return wild; }

  const char* GetEffectiveTLDName() const
  {
    return strings.strtab + strtab_index;
  }

  static const ETLDEntry* GetEntry(const char* aDomain);

  static const size_t ETLD_ENTRY_N_INDEX_BITS = 30;

  // These fields must be public to allow static construction.
  uint32_t strtab_index : ETLD_ENTRY_N_INDEX_BITS;
  uint32_t exception : 1;
  uint32_t wild : 1;

private:
  struct Cmp {
    int operator()(const ETLDEntry aEntry) const
    {
      return strcmp(mName, aEntry.GetEffectiveTLDName());
    }
    explicit Cmp(const char* aName) : mName(aName) {}
    const char* mName;
  };

#define ETLD_STR_NUM_1(line) str##line
#define ETLD_STR_NUM(line) ETLD_STR_NUM_1(line)
  struct etld_string_list {
#define ETLD_ENTRY(name, ex, wild) char ETLD_STR_NUM(__LINE__)[sizeof(name)];
#include "etld_data.inc"
#undef ETLD_ENTRY
  };

  // This static string table is all the eTLD domain names packed together.
  static const union etld_strings {
    struct etld_string_list list;
    char strtab[1];
  } strings;

  // This is the static entries table. Each entry has an index into the string
  // table. The entries are in sorted order so that binary search can be used.
  static const ETLDEntry entries[];

  void FuncForStaticAsserts(void);
#undef ETLD_STR_NUM
#undef ETLD_STR_NUM1
};

class nsEffectiveTLDService final
  : public nsIEffectiveTLDService
  , public nsIMemoryReporter
{
public:
  NS_DECL_ISUPPORTS
  NS_DECL_NSIEFFECTIVETLDSERVICE
  NS_DECL_NSIMEMORYREPORTER

  nsEffectiveTLDService();
  nsresult Init();

  size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf);

private:
  nsresult GetBaseDomainInternal(nsCString &aHostname, int32_t aAdditionalParts, nsACString &aBaseDomain);
  nsresult NormalizeHostname(nsCString &aHostname);
  ~nsEffectiveTLDService();

  nsCOMPtr<nsIIDNService>     mIDNService;
};

#endif // EffectiveTLDService_h