1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/url-classifier/LookupCache.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,143 @@ 1.4 +//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef LookupCache_h__ 1.10 +#define LookupCache_h__ 1.11 + 1.12 +#include "Entries.h" 1.13 +#include "nsString.h" 1.14 +#include "nsTArray.h" 1.15 +#include "nsAutoPtr.h" 1.16 +#include "nsCOMPtr.h" 1.17 +#include "nsIFile.h" 1.18 +#include "nsIFileStreams.h" 1.19 +#include "nsUrlClassifierPrefixSet.h" 1.20 +#include "prlog.h" 1.21 + 1.22 +namespace mozilla { 1.23 +namespace safebrowsing { 1.24 + 1.25 +#define MAX_HOST_COMPONENTS 5 1.26 +#define MAX_PATH_COMPONENTS 4 1.27 + 1.28 +class LookupResult { 1.29 +public: 1.30 + LookupResult() : mComplete(false), mNoise(false), mFresh(false), mProtocolConfirmed(false) {} 1.31 + 1.32 + // The fragment that matched in the LookupCache 1.33 + union { 1.34 + Prefix prefix; 1.35 + Completion complete; 1.36 + } hash; 1.37 + 1.38 + const Prefix &PrefixHash() { return hash.prefix; } 1.39 + const Completion &CompleteHash() { return hash.complete; } 1.40 + 1.41 + bool Confirmed() const { return (mComplete && mFresh) || mProtocolConfirmed; } 1.42 + bool Complete() const { return mComplete; } 1.43 + 1.44 + // True if we have a complete match for this hash in the table. 1.45 + bool mComplete; 1.46 + 1.47 + // True if this is a noise entry, i.e. an extra entry 1.48 + // that is inserted to mask the true URL we are requesting 1.49 + bool mNoise; 1.50 + 1.51 + // True if we've updated this table recently-enough. 1.52 + bool mFresh; 1.53 + 1.54 + bool mProtocolConfirmed; 1.55 + 1.56 + nsCString mTableName; 1.57 +}; 1.58 + 1.59 +typedef nsTArray<LookupResult> LookupResultArray; 1.60 + 1.61 +struct CacheResult { 1.62 + AddComplete entry; 1.63 + nsCString table; 1.64 +}; 1.65 +typedef nsTArray<CacheResult> CacheResultArray; 1.66 + 1.67 +class LookupCache { 1.68 +public: 1.69 + // Check for a canonicalized IP address. 1.70 + static bool IsCanonicalizedIP(const nsACString& aHost); 1.71 + 1.72 + // take a lookup string (www.hostname.com/path/to/resource.html) and 1.73 + // expand it into the set of fragments that should be searched for in an 1.74 + // entry 1.75 + static nsresult GetLookupFragments(const nsACString& aSpec, 1.76 + nsTArray<nsCString>* aFragments); 1.77 + // Similar to GetKey(), but if the domain contains three or more components, 1.78 + // two keys will be returned: 1.79 + // hostname.com/foo/bar -> [hostname.com] 1.80 + // mail.hostname.com/foo/bar -> [hostname.com, mail.hostname.com] 1.81 + // www.mail.hostname.com/foo/bar -> [hostname.com, mail.hostname.com] 1.82 + static nsresult GetHostKeys(const nsACString& aSpec, 1.83 + nsTArray<nsCString>* aHostKeys); 1.84 + // Get the database key for a given URI. This is the top three 1.85 + // domain components if they exist, otherwise the top two. 1.86 + // hostname.com/foo/bar -> hostname.com 1.87 + // mail.hostname.com/foo/bar -> mail.hostname.com 1.88 + // www.mail.hostname.com/foo/bar -> mail.hostname.com 1.89 + static nsresult GetKey(const nsACString& aSpec, Completion* aHash, 1.90 + nsCOMPtr<nsICryptoHash>& aCryptoHash); 1.91 + 1.92 + LookupCache(const nsACString& aTableName, nsIFile* aStoreFile); 1.93 + ~LookupCache(); 1.94 + 1.95 + const nsCString &TableName() const { return mTableName; } 1.96 + 1.97 + nsresult Init(); 1.98 + nsresult Open(); 1.99 + // The directory handle where we operate will 1.100 + // be moved away when a backup is made. 1.101 + nsresult UpdateDirHandle(nsIFile* aStoreDirectory); 1.102 + // This will Clear() the passed arrays when done. 1.103 + nsresult Build(AddPrefixArray& aAddPrefixes, 1.104 + AddCompleteArray& aAddCompletes); 1.105 + nsresult GetPrefixes(nsTArray<uint32_t>* aAddPrefixes); 1.106 + void ClearCompleteCache(); 1.107 + 1.108 +#if DEBUG && defined(PR_LOGGING) 1.109 + void Dump(); 1.110 +#endif 1.111 + nsresult WriteFile(); 1.112 + nsresult Has(const Completion& aCompletion, 1.113 + bool* aHas, bool* aComplete); 1.114 + bool IsPrimed(); 1.115 + 1.116 +private: 1.117 + void ClearAll(); 1.118 + nsresult Reset(); 1.119 + void UpdateHeader(); 1.120 + nsresult ReadHeader(nsIInputStream* aInputStream); 1.121 + nsresult ReadCompletions(nsIInputStream* aInputStream); 1.122 + nsresult EnsureSizeConsistent(); 1.123 + nsresult LoadPrefixSet(); 1.124 + // Construct a Prefix Set with known prefixes. 1.125 + // This will Clear() aAddPrefixes when done. 1.126 + nsresult ConstructPrefixSet(AddPrefixArray& aAddPrefixes); 1.127 + 1.128 + struct Header { 1.129 + uint32_t magic; 1.130 + uint32_t version; 1.131 + uint32_t numCompletions; 1.132 + }; 1.133 + Header mHeader; 1.134 + 1.135 + bool mPrimed; 1.136 + nsCString mTableName; 1.137 + nsCOMPtr<nsIFile> mStoreDirectory; 1.138 + CompletionArray mCompletions; 1.139 + // Set of prefixes known to be in the database 1.140 + nsRefPtr<nsUrlClassifierPrefixSet> mPrefixSet; 1.141 +}; 1.142 + 1.143 +} 1.144 +} 1.145 + 1.146 +#endif