Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/dns/nsEffectiveTLDService.h
Line
Count
Source (jump to first uncovered line)
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 "nsHashKeys.h"
12
#include "nsIMemoryReporter.h"
13
#include "nsString.h"
14
#include "nsCOMPtr.h"
15
#include "mozilla/Attributes.h"
16
#include "mozilla/Dafsa.h"
17
#include "mozilla/MemoryReporting.h"
18
#include "mozilla/MruCache.h"
19
20
class nsIIDNService;
21
22
class nsEffectiveTLDService final
23
  : public nsIEffectiveTLDService
24
  , public nsIMemoryReporter
25
{
26
public:
27
  NS_DECL_ISUPPORTS
28
  NS_DECL_NSIEFFECTIVETLDSERVICE
29
  NS_DECL_NSIMEMORYREPORTER
30
31
  nsEffectiveTLDService();
32
  nsresult Init();
33
34
  size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf);
35
36
private:
37
  nsresult GetBaseDomainInternal(nsCString &aHostname, int32_t aAdditionalParts, nsACString &aBaseDomain);
38
  nsresult NormalizeHostname(nsCString &aHostname);
39
  ~nsEffectiveTLDService();
40
41
  nsCOMPtr<nsIIDNService>     mIDNService;
42
43
  // The DAFSA provides a compact encoding of the rather large eTLD list.
44
  mozilla::Dafsa mGraph;
45
46
  struct TLDCacheEntry
47
  {
48
    nsCString mHost;
49
    nsCString mBaseDomain;
50
  };
51
52
  // We use a small most recently used cache to compensate for DAFSA lookups
53
  // being slightly slower than a binary search on a larger table of strings.
54
  //
55
  // We first check the cache for a matching result and avoid a DAFSA lookup
56
  // if a match is found. Otherwise we lookup the domain in the DAFSA and then
57
  // cache the result. During standard browsing the same domains are repeatedly
58
  // fed into |GetBaseDomainInternal| so this ends up being an effective
59
  // mitigation getting about a 99% hit rate with four tabs open.
60
  struct TldCache :
61
    public mozilla::MruCache<nsACString, TLDCacheEntry, TldCache>
62
  {
63
    static mozilla::HashNumber Hash(const nsACString& aKey)
64
0
    {
65
0
      return mozilla::HashString(aKey);
66
0
    }
67
    static bool Match(const nsACString& aKey, const TLDCacheEntry& aVal)
68
0
    {
69
0
      return aKey == aVal.mHost;
70
0
    }
71
  };
72
73
  TldCache mMruTable;
74
};
75
76
#endif // EffectiveTLDService_h