michael@0: //* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef Classifier_h__ michael@0: #define Classifier_h__ michael@0: michael@0: #include "Entries.h" michael@0: #include "HashStore.h" michael@0: #include "ProtocolParser.h" michael@0: #include "LookupCache.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsString.h" michael@0: #include "nsIFile.h" michael@0: #include "nsICryptoHash.h" michael@0: #include "nsDataHashtable.h" michael@0: michael@0: namespace mozilla { michael@0: namespace safebrowsing { michael@0: michael@0: /** michael@0: * Maintains the stores and LookupCaches for the url classifier. michael@0: */ michael@0: class Classifier { michael@0: public: michael@0: Classifier(); michael@0: ~Classifier(); michael@0: michael@0: nsresult Open(nsIFile& aCacheDirectory); michael@0: void Close(); michael@0: void Reset(); michael@0: michael@0: /** michael@0: * Get the list of active tables and their chunks in a format michael@0: * suitable for an update request. michael@0: */ michael@0: void TableRequest(nsACString& aResult); michael@0: michael@0: /* michael@0: * Get all tables that we know about. michael@0: */ michael@0: nsresult ActiveTables(nsTArray& aTables); michael@0: michael@0: /** michael@0: * Check a URL against the specified tables. michael@0: */ michael@0: nsresult Check(const nsACString& aSpec, michael@0: const nsACString& tables, michael@0: LookupResultArray& aResults); michael@0: michael@0: /** michael@0: * Apply the table updates in the array. Takes ownership of michael@0: * the updates in the array and clears it. Wacky! michael@0: */ michael@0: nsresult ApplyUpdates(nsTArray* aUpdates); michael@0: /** michael@0: * Failed update. Spoil the entries so we don't block hosts michael@0: * unnecessarily michael@0: */ michael@0: nsresult MarkSpoiled(nsTArray& aTables); michael@0: nsresult CacheCompletions(const CacheResultArray& aResults); michael@0: uint32_t GetHashKey(void) { return mHashKey; } michael@0: void SetFreshTime(uint32_t aTime) { mFreshTime = aTime; } michael@0: /* michael@0: * Get a bunch of extra prefixes to query for completion michael@0: * and mask the real entry being requested michael@0: */ michael@0: nsresult ReadNoiseEntries(const Prefix& aPrefix, michael@0: const nsACString& aTableName, michael@0: uint32_t aCount, michael@0: PrefixArray* aNoiseEntries); michael@0: static void SplitTables(const nsACString& str, nsTArray& tables); michael@0: michael@0: private: michael@0: void DropStores(); michael@0: nsresult CreateStoreDirectory(); michael@0: nsresult SetupPathNames(); michael@0: nsresult RecoverBackups(); michael@0: nsresult CleanToDelete(); michael@0: nsresult BackupTables(); michael@0: nsresult RemoveBackupTables(); michael@0: nsresult RegenActiveTables(); michael@0: nsresult ScanStoreDir(nsTArray& aTables); michael@0: michael@0: nsresult ApplyTableUpdates(nsTArray* aUpdates, michael@0: const nsACString& aTable); michael@0: michael@0: LookupCache *GetLookupCache(const nsACString& aTable); michael@0: michael@0: // Root dir of the Local profile. michael@0: nsCOMPtr mCacheDirectory; michael@0: // Main directory where to store the databases. michael@0: nsCOMPtr mStoreDirectory; michael@0: // Used for atomically updating the other dirs. michael@0: nsCOMPtr mBackupDirectory; michael@0: nsCOMPtr mToDeleteDirectory; michael@0: nsCOMPtr mCryptoHash; michael@0: nsTArray mHashStores; michael@0: nsTArray mLookupCaches; michael@0: nsTArray mActiveTablesCache; michael@0: uint32_t mHashKey; michael@0: // Stores the last time a given table was updated (seconds). michael@0: nsDataHashtable mTableFreshness; michael@0: uint32_t mFreshTime; michael@0: }; michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif