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 nsUrlClassifierDBService_h_ michael@0: #define nsUrlClassifierDBService_h_ michael@0: michael@0: #include michael@0: michael@0: #include "nsID.h" michael@0: #include "nsInterfaceHashtable.h" michael@0: #include "nsIObserver.h" michael@0: #include "nsUrlClassifierPrefixSet.h" michael@0: #include "nsIUrlClassifierHashCompleter.h" michael@0: #include "nsIUrlClassifierDBService.h" michael@0: #include "nsIURIClassifier.h" michael@0: #include "nsToolkitCompsCID.h" michael@0: #include "nsICryptoHash.h" michael@0: #include "nsICryptoHMAC.h" michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: #include "LookupCache.h" michael@0: michael@0: // The hash length for a domain key. michael@0: #define DOMAIN_LENGTH 4 michael@0: michael@0: // The hash length of a partial hash entry. michael@0: #define PARTIAL_LENGTH 4 michael@0: michael@0: // The hash length of a complete hash entry. michael@0: #define COMPLETE_LENGTH 32 michael@0: michael@0: class nsUrlClassifierDBServiceWorker; michael@0: class nsIThread; michael@0: class nsIURI; michael@0: michael@0: // This is a proxy class that just creates a background thread and delagates michael@0: // calls to the background thread. michael@0: class nsUrlClassifierDBService MOZ_FINAL : public nsIUrlClassifierDBService, michael@0: public nsIURIClassifier, michael@0: public nsIObserver michael@0: { michael@0: public: michael@0: // This is thread safe. It throws an exception if the thread is busy. michael@0: nsUrlClassifierDBService(); michael@0: michael@0: nsresult Init(); michael@0: michael@0: static nsUrlClassifierDBService* GetInstance(nsresult *result); michael@0: michael@0: NS_DECLARE_STATIC_IID_ACCESSOR(NS_URLCLASSIFIERDBSERVICE_CID) michael@0: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_NSIURLCLASSIFIERDBSERVICE michael@0: NS_DECL_NSIURICLASSIFIER michael@0: NS_DECL_NSIOBSERVER michael@0: michael@0: bool GetCompleter(const nsACString& tableName, michael@0: nsIUrlClassifierHashCompleter** completer); michael@0: nsresult CacheCompletions(mozilla::safebrowsing::CacheResultArray *results); michael@0: nsresult CacheMisses(mozilla::safebrowsing::PrefixArray *results); michael@0: michael@0: static nsIThread* BackgroundThread(); michael@0: michael@0: private: michael@0: // No subclassing michael@0: ~nsUrlClassifierDBService(); michael@0: michael@0: // Disallow copy constructor michael@0: nsUrlClassifierDBService(nsUrlClassifierDBService&); michael@0: michael@0: nsresult LookupURI(nsIPrincipal* aPrincipal, michael@0: const nsACString& tables, michael@0: nsIUrlClassifierCallback* c, michael@0: bool forceCheck, bool *didCheck); michael@0: michael@0: // Close db connection and join the background thread if it exists. michael@0: nsresult Shutdown(); michael@0: michael@0: // Check if the key is on a known-clean host. michael@0: nsresult CheckClean(const nsACString &lookupKey, michael@0: bool *clean); michael@0: michael@0: // Read everything into mGethashTables and mDisallowCompletionTables michael@0: nsresult ReadTablesFromPrefs(); michael@0: michael@0: nsRefPtr mWorker; michael@0: nsCOMPtr mWorkerProxy; michael@0: michael@0: nsInterfaceHashtable mCompleters; michael@0: michael@0: // TRUE if the nsURIClassifier implementation should check for malware michael@0: // uris on document loads. michael@0: bool mCheckMalware; michael@0: michael@0: // TRUE if the nsURIClassifier implementation should check for phishing michael@0: // uris on document loads. michael@0: bool mCheckPhishing; michael@0: michael@0: // TRUE if a BeginUpdate() has been called without an accompanying michael@0: // CancelUpdate()/FinishUpdate(). This is used to prevent competing michael@0: // updates, not to determine whether an update is still being michael@0: // processed. michael@0: bool mInUpdate; michael@0: michael@0: // The list of tables that can use the default hash completer object. michael@0: nsTArray mGethashTables; michael@0: michael@0: // The list of tables that should never be hash completed. michael@0: nsTArray mDisallowCompletionsTables; michael@0: michael@0: // Thread that we do the updates on. michael@0: static nsIThread* gDbBackgroundThread; michael@0: }; michael@0: michael@0: NS_DEFINE_STATIC_IID_ACCESSOR(nsUrlClassifierDBService, NS_URLCLASSIFIERDBSERVICE_CID) michael@0: michael@0: #endif // nsUrlClassifierDBService_h_