toolkit/components/url-classifier/LookupCache.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 //* -*- Mode: C++; tab-width: 8; 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/. */
     6 #ifndef LookupCache_h__
     7 #define LookupCache_h__
     9 #include "Entries.h"
    10 #include "nsString.h"
    11 #include "nsTArray.h"
    12 #include "nsAutoPtr.h"
    13 #include "nsCOMPtr.h"
    14 #include "nsIFile.h"
    15 #include "nsIFileStreams.h"
    16 #include "nsUrlClassifierPrefixSet.h"
    17 #include "prlog.h"
    19 namespace mozilla {
    20 namespace safebrowsing {
    22 #define MAX_HOST_COMPONENTS 5
    23 #define MAX_PATH_COMPONENTS 4
    25 class LookupResult {
    26 public:
    27   LookupResult() : mComplete(false), mNoise(false), mFresh(false), mProtocolConfirmed(false) {}
    29   // The fragment that matched in the LookupCache
    30   union {
    31     Prefix prefix;
    32     Completion complete;
    33   } hash;
    35   const Prefix &PrefixHash() { return hash.prefix; }
    36   const Completion &CompleteHash() { return hash.complete; }
    38   bool Confirmed() const { return (mComplete && mFresh) || mProtocolConfirmed; }
    39   bool Complete() const { return mComplete; }
    41   // True if we have a complete match for this hash in the table.
    42   bool mComplete;
    44   // True if this is a noise entry, i.e. an extra entry
    45   // that is inserted to mask the true URL we are requesting
    46   bool mNoise;
    48   // True if we've updated this table recently-enough.
    49   bool mFresh;
    51   bool mProtocolConfirmed;
    53   nsCString mTableName;
    54 };
    56 typedef nsTArray<LookupResult> LookupResultArray;
    58 struct CacheResult {
    59   AddComplete entry;
    60   nsCString table;
    61 };
    62 typedef nsTArray<CacheResult> CacheResultArray;
    64 class LookupCache {
    65 public:
    66   // Check for a canonicalized IP address.
    67   static bool IsCanonicalizedIP(const nsACString& aHost);
    69   // take a lookup string (www.hostname.com/path/to/resource.html) and
    70   // expand it into the set of fragments that should be searched for in an
    71   // entry
    72   static nsresult GetLookupFragments(const nsACString& aSpec,
    73                                      nsTArray<nsCString>* aFragments);
    74   // Similar to GetKey(), but if the domain contains three or more components,
    75   // two keys will be returned:
    76   //  hostname.com/foo/bar -> [hostname.com]
    77   //  mail.hostname.com/foo/bar -> [hostname.com, mail.hostname.com]
    78   //  www.mail.hostname.com/foo/bar -> [hostname.com, mail.hostname.com]
    79   static nsresult GetHostKeys(const nsACString& aSpec,
    80                               nsTArray<nsCString>* aHostKeys);
    81   // Get the database key for a given URI.  This is the top three
    82   // domain components if they exist, otherwise the top two.
    83   //  hostname.com/foo/bar -> hostname.com
    84   //  mail.hostname.com/foo/bar -> mail.hostname.com
    85   //  www.mail.hostname.com/foo/bar -> mail.hostname.com
    86   static nsresult GetKey(const nsACString& aSpec, Completion* aHash,
    87                          nsCOMPtr<nsICryptoHash>& aCryptoHash);
    89   LookupCache(const nsACString& aTableName, nsIFile* aStoreFile);
    90   ~LookupCache();
    92   const nsCString &TableName() const { return mTableName; }
    94   nsresult Init();
    95   nsresult Open();
    96   // The directory handle where we operate will
    97   // be moved away when a backup is made.
    98   nsresult UpdateDirHandle(nsIFile* aStoreDirectory);
    99   // This will Clear() the passed arrays when done.
   100   nsresult Build(AddPrefixArray& aAddPrefixes,
   101                  AddCompleteArray& aAddCompletes);
   102   nsresult GetPrefixes(nsTArray<uint32_t>* aAddPrefixes);
   103   void ClearCompleteCache();
   105 #if DEBUG && defined(PR_LOGGING)
   106   void Dump();
   107 #endif
   108   nsresult WriteFile();
   109   nsresult Has(const Completion& aCompletion,
   110                bool* aHas, bool* aComplete);
   111   bool IsPrimed();
   113 private:
   114   void ClearAll();
   115   nsresult Reset();
   116   void UpdateHeader();
   117   nsresult ReadHeader(nsIInputStream* aInputStream);
   118   nsresult ReadCompletions(nsIInputStream* aInputStream);
   119   nsresult EnsureSizeConsistent();
   120   nsresult LoadPrefixSet();
   121   // Construct a Prefix Set with known prefixes.
   122   // This will Clear() aAddPrefixes when done.
   123   nsresult ConstructPrefixSet(AddPrefixArray& aAddPrefixes);
   125   struct Header {
   126     uint32_t magic;
   127     uint32_t version;
   128     uint32_t numCompletions;
   129   };
   130   Header mHeader;
   132   bool mPrimed;
   133   nsCString mTableName;
   134   nsCOMPtr<nsIFile> mStoreDirectory;
   135   CompletionArray mCompletions;
   136   // Set of prefixes known to be in the database
   137   nsRefPtr<nsUrlClassifierPrefixSet> mPrefixSet;
   138 };
   140 }
   141 }
   143 #endif

mercurial