tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsEffectiveTLDService.h (2748B)


      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 "mozilla/AutoMemMap.h"
     12 #include "mozilla/Dafsa.h"
     13 #include "mozilla/MemoryReporting.h"
     14 #include "mozilla/MruCache.h"
     15 #include "mozilla/RWLock.h"
     16 
     17 #include "nsCOMPtr.h"
     18 #include "nsHashKeys.h"
     19 #include "nsIMemoryReporter.h"
     20 #include "nsString.h"
     21 
     22 class nsIIDNService;
     23 
     24 class nsEffectiveTLDService final : public nsIEffectiveTLDService,
     25                                    public nsIMemoryReporter {
     26 public:
     27  NS_DECL_THREADSAFE_ISUPPORTS
     28  NS_DECL_NSIEFFECTIVETLDSERVICE
     29  NS_DECL_NSIMEMORYREPORTER
     30 
     31  nsEffectiveTLDService();
     32  nsresult Init();
     33 
     34  static already_AddRefed<nsIEffectiveTLDService> GetXPCOMSingleton();
     35 
     36  size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf);
     37 
     38 private:
     39  nsresult GetBaseDomainInternal(nsCString& aHostname, int32_t aAdditionalParts,
     40                                 bool aOnlyKnownPublicSuffix,
     41                                 nsACString& aBaseDomain);
     42  ~nsEffectiveTLDService();
     43 
     44  // The DAFSA provides a compact encoding of the rather large eTLD list.
     45  mozilla::Dafsa mGraph;
     46 
     47  // Note that the cache entries here can record entries that were cached
     48  // successfully or unsuccessfully.  mResult must be checked before using an
     49  // entry.  If it's a success error code, the cache entry is valid and can be
     50  // used.
     51  struct TLDCacheEntry {
     52    nsCString mHost;
     53    nsCString mBaseDomain;
     54    nsresult mResult;
     55  };
     56 
     57  // We use a small most recently used cache to compensate for DAFSA lookups
     58  // being slightly slower than a binary search on a larger table of strings.
     59  //
     60  // We first check the cache for a matching result and avoid a DAFSA lookup
     61  // if a match is found. Otherwise we lookup the domain in the DAFSA and then
     62  // cache the result. During standard browsing the same domains are repeatedly
     63  // fed into |GetBaseDomainInternal| so this ends up being an effective
     64  // mitigation getting about a 99% hit rate with four tabs open.
     65  struct TldCache
     66      : public mozilla::MruCache<nsACString, TLDCacheEntry, TldCache> {
     67    static mozilla::HashNumber Hash(const nsACString& aKey) {
     68      return mozilla::HashString(aKey);
     69    }
     70    static bool Match(const nsACString& aKey, const TLDCacheEntry& aVal) {
     71      return aKey == aVal.mHost;
     72    }
     73  };
     74 
     75  // NOTE: Only used on the main thread, so not guarded by mGraphLock.
     76  TldCache mMruTable;
     77 };
     78 
     79 #endif  // EffectiveTLDService_h