nsUrlClassifierDBService.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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/. */
  5. #ifndef nsUrlClassifierDBService_h_
  6. #define nsUrlClassifierDBService_h_
  7. #include <nsISupportsUtils.h>
  8. #include "nsID.h"
  9. #include "nsInterfaceHashtable.h"
  10. #include "nsIObserver.h"
  11. #include "nsUrlClassifierPrefixSet.h"
  12. #include "nsIUrlClassifierHashCompleter.h"
  13. #include "nsIUrlListManager.h"
  14. #include "nsIUrlClassifierDBService.h"
  15. #include "nsIURIClassifier.h"
  16. #include "nsToolkitCompsCID.h"
  17. #include "nsICryptoHMAC.h"
  18. #include "mozilla/Attributes.h"
  19. #include "mozilla/Mutex.h"
  20. #include "mozilla/TimeStamp.h"
  21. #include "Entries.h"
  22. #include "LookupCache.h"
  23. // GCC < 6.1 workaround, see bug 1329593
  24. #if defined(XP_WIN) && defined(__MINGW32__)
  25. #define GCC_MANGLING_WORKAROUND __stdcall
  26. #else
  27. #define GCC_MANGLING_WORKAROUND
  28. #endif
  29. // The hash length for a domain key.
  30. #define DOMAIN_LENGTH 4
  31. // The hash length of a partial hash entry.
  32. #define PARTIAL_LENGTH 4
  33. // The hash length of a complete hash entry.
  34. #define COMPLETE_LENGTH 32
  35. using namespace mozilla::safebrowsing;
  36. class nsUrlClassifierDBServiceWorker;
  37. class nsIThread;
  38. class nsIURI;
  39. class UrlClassifierDBServiceWorkerProxy;
  40. namespace mozilla {
  41. namespace safebrowsing {
  42. class Classifier;
  43. class ProtocolParser;
  44. class TableUpdate;
  45. nsresult
  46. TablesToResponse(const nsACString& tables);
  47. } // namespace safebrowsing
  48. } // namespace mozilla
  49. // This is a proxy class that just creates a background thread and delagates
  50. // calls to the background thread.
  51. class nsUrlClassifierDBService final : public nsIUrlClassifierDBService,
  52. public nsIURIClassifier,
  53. public nsIObserver
  54. {
  55. public:
  56. // This is thread safe. It throws an exception if the thread is busy.
  57. nsUrlClassifierDBService();
  58. nsresult Init();
  59. static nsUrlClassifierDBService* GetInstance(nsresult *result);
  60. NS_DECLARE_STATIC_IID_ACCESSOR(NS_URLCLASSIFIERDBSERVICE_CID)
  61. NS_DECL_THREADSAFE_ISUPPORTS
  62. NS_DECL_NSIURLCLASSIFIERDBSERVICE
  63. NS_DECL_NSIURICLASSIFIER
  64. NS_DECL_NSIOBSERVER
  65. bool GetCompleter(const nsACString& tableName,
  66. nsIUrlClassifierHashCompleter** completer);
  67. nsresult CacheCompletions(mozilla::safebrowsing::CacheResultArray *results);
  68. nsresult CacheMisses(mozilla::safebrowsing::PrefixArray *results);
  69. static nsIThread* BackgroundThread();
  70. static bool ShutdownHasStarted();
  71. private:
  72. // No subclassing
  73. ~nsUrlClassifierDBService();
  74. // Disallow copy constructor
  75. nsUrlClassifierDBService(nsUrlClassifierDBService&);
  76. nsresult LookupURI(nsIPrincipal* aPrincipal,
  77. const nsACString& tables,
  78. nsIUrlClassifierCallback* c,
  79. bool forceCheck, bool *didCheck);
  80. // Close db connection and join the background thread if it exists.
  81. nsresult Shutdown();
  82. // Check if the key is on a known-clean host.
  83. nsresult CheckClean(const nsACString &lookupKey,
  84. bool *clean);
  85. // Read everything into mGethashTables and mDisallowCompletionTables
  86. nsresult ReadTablesFromPrefs();
  87. // Build a comma-separated list of tables to check
  88. void BuildTables(bool trackingProtectionEnabled, nsCString& tables);
  89. RefPtr<nsUrlClassifierDBServiceWorker> mWorker;
  90. RefPtr<UrlClassifierDBServiceWorkerProxy> mWorkerProxy;
  91. nsInterfaceHashtable<nsCStringHashKey, nsIUrlClassifierHashCompleter> mCompleters;
  92. // TRUE if the nsURIClassifier implementation should check for malware
  93. // uris on document loads.
  94. bool mCheckMalware;
  95. // TRUE if the nsURIClassifier implementation should check for phishing
  96. // uris on document loads.
  97. bool mCheckPhishing;
  98. // TRUE if the nsURIClassifier implementation should check for tracking
  99. // uris on document loads.
  100. bool mCheckTracking;
  101. // TRUE if the nsURIClassifier implementation should check for blocked
  102. // uris on document loads.
  103. bool mCheckBlockedURIs;
  104. // TRUE if a BeginUpdate() has been called without an accompanying
  105. // CancelUpdate()/FinishUpdate(). This is used to prevent competing
  106. // updates, not to determine whether an update is still being
  107. // processed.
  108. bool mInUpdate;
  109. // The list of tables that can use the default hash completer object.
  110. nsTArray<nsCString> mGethashTables;
  111. // The list of tables that should never be hash completed.
  112. nsTArray<nsCString> mDisallowCompletionsTables;
  113. // Thread that we do the updates on.
  114. static nsIThread* gDbBackgroundThread;
  115. };
  116. class nsUrlClassifierDBServiceWorker final : public nsIUrlClassifierDBService
  117. {
  118. public:
  119. nsUrlClassifierDBServiceWorker();
  120. NS_DECL_THREADSAFE_ISUPPORTS
  121. NS_DECL_NSIURLCLASSIFIERDBSERVICE
  122. nsresult Init(uint32_t aGethashNoise, nsCOMPtr<nsIFile> aCacheDir);
  123. // Queue a lookup for the worker to perform, called in the main thread.
  124. // tables is a comma-separated list of tables to query
  125. nsresult QueueLookup(const nsACString& lookupKey,
  126. const nsACString& tables,
  127. nsIUrlClassifierLookupCallback* callback);
  128. // Handle any queued-up lookups. We call this function during long-running
  129. // update operations to prevent lookups from blocking for too long.
  130. nsresult HandlePendingLookups();
  131. // Perform a blocking classifier lookup for a given url. Can be called on
  132. // either the main thread or the worker thread.
  133. nsresult DoLocalLookup(const nsACString& spec,
  134. const nsACString& tables,
  135. LookupResultArray* results);
  136. // Open the DB connection
  137. nsresult GCC_MANGLING_WORKAROUND OpenDb();
  138. // Provide a way to forcibly close the db connection.
  139. nsresult GCC_MANGLING_WORKAROUND CloseDb();
  140. nsresult CacheCompletions(CacheResultArray * aEntries);
  141. nsresult CacheMisses(PrefixArray * aEntries);
  142. private:
  143. // No subclassing
  144. ~nsUrlClassifierDBServiceWorker();
  145. // Disallow copy constructor
  146. nsUrlClassifierDBServiceWorker(nsUrlClassifierDBServiceWorker&);
  147. // Applies the current transaction and resets the update/working times.
  148. nsresult ApplyUpdate();
  149. // Reset the in-progress update stream
  150. void ResetStream();
  151. // Reset the in-progress update
  152. void ResetUpdate();
  153. // Perform a classifier lookup for a given url.
  154. nsresult DoLookup(const nsACString& spec,
  155. const nsACString& tables,
  156. nsIUrlClassifierLookupCallback* c);
  157. nsresult AddNoise(const Prefix aPrefix,
  158. const nsCString tableName,
  159. uint32_t aCount,
  160. LookupResultArray& results);
  161. // Can only be used on the background thread
  162. nsCOMPtr<nsICryptoHash> mCryptoHash;
  163. nsAutoPtr<mozilla::safebrowsing::Classifier> mClassifier;
  164. // The class that actually parses the update chunks.
  165. nsAutoPtr<ProtocolParser> mProtocolParser;
  166. // Directory where to store the SB databases.
  167. nsCOMPtr<nsIFile> mCacheDir;
  168. // XXX: maybe an array of autoptrs. Or maybe a class specifically
  169. // storing a series of updates.
  170. nsTArray<mozilla::safebrowsing::TableUpdate*> mTableUpdates;
  171. uint32_t mUpdateWaitSec;
  172. // Entries that cannot be completed. We expect them to die at
  173. // the next update
  174. PrefixArray mMissCache;
  175. // Stores the last results that triggered a table update.
  176. CacheResultArray mLastResults;
  177. nsresult mUpdateStatus;
  178. nsTArray<nsCString> mUpdateTables;
  179. nsCOMPtr<nsIUrlClassifierUpdateObserver> mUpdateObserver;
  180. bool mInStream;
  181. // The number of noise entries to add to the set of lookup results.
  182. uint32_t mGethashNoise;
  183. // Pending lookups are stored in a queue for processing. The queue
  184. // is protected by mPendingLookupLock.
  185. mozilla::Mutex mPendingLookupLock;
  186. class PendingLookup {
  187. public:
  188. mozilla::TimeStamp mStartTime;
  189. nsCString mKey;
  190. nsCString mTables;
  191. nsCOMPtr<nsIUrlClassifierLookupCallback> mCallback;
  192. };
  193. // list of pending lookups
  194. nsTArray<PendingLookup> mPendingLookups;
  195. #ifdef MOZ_SAFEBROWSING_DUMP_FAILED_UPDATES
  196. // The raw update response for debugging.
  197. nsCString mRawTableUpdates;
  198. #endif
  199. };
  200. NS_DEFINE_STATIC_IID_ACCESSOR(nsUrlClassifierDBService, NS_URLCLASSIFIERDBSERVICE_CID)
  201. #endif // nsUrlClassifierDBService_h_