toolkit/components/url-classifier/LookupCache.h

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial