Bug 1351472 - Skip AddNoise if the table is unknown or disallowed to getHash draft
authorThomas Nguyen <tnguyen@mozilla.com>
Fri, 28 Apr 2017 15:26:54 +0800
changeset 573004 26a92b0a70a0206ae9b1ac0d56fa7febd3e0227a
parent 568509 0f5ba06c4c5959030a05cb852656d854065e2226
child 574077 ac9bf0563a40b5dd7e6ae49a704b1a67449142c5
push id57263
push userbmo:tnguyen@mozilla.com
push dateFri, 05 May 2017 05:25:20 +0000
bugs1351472
milestone55.0a1
Bug 1351472 - Skip AddNoise if the table is unknown or disallowed to getHash MozReview-Commit-ID: GMWs2UpiyiP
toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
toolkit/components/url-classifier/nsUrlClassifierDBService.h
--- a/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
+++ b/toolkit/components/url-classifier/nsUrlClassifierDBService.cpp
@@ -139,20 +139,22 @@ nsUrlClassifierDBServiceWorker::~nsUrlCl
 {
   NS_ASSERTION(!mClassifier,
                "Db connection not closed, leaking memory!  Call CloseDb "
                "to close the connection.");
 }
 
 nsresult
 nsUrlClassifierDBServiceWorker::Init(uint32_t aGethashNoise,
-                                     nsCOMPtr<nsIFile> aCacheDir)
+                                     nsCOMPtr<nsIFile> aCacheDir,
+                                     nsUrlClassifierDBService *aDBService)
 {
   mGethashNoise = aGethashNoise;
   mCacheDir = aCacheDir;
+  mDBService = aDBService;
 
   ResetUpdate();
 
   return NS_OK;
 }
 
 nsresult
 nsUrlClassifierDBServiceWorker::QueueLookup(const nsACString& spec,
@@ -271,17 +273,19 @@ nsUrlClassifierDBServiceWorker::DoLookup
     // mMissCache should only be used in V2.
     if (!lookupResult.mProtocolV2 ||
         !mMissCache.Contains(lookupResult.hash.fixedLengthPrefix)) {
       completes->AppendElement(lookupResult);
     }
   }
 
   for (uint32_t i = 0; i < completes->Length(); i++) {
-    if (!completes->ElementAt(i).Confirmed()) {
+    if (!completes->ElementAt(i).Confirmed() &&
+        mDBService->CanComplete(completes->ElementAt(i).mTableName)) {
+
       // We're going to be doing a gethash request, add some extra entries.
       // Note that we cannot pass the first two by reference, because we
       // add to completes, whicah can cause completes to reallocate and move.
       AddNoise(completes->ElementAt(i).hash.fixedLengthPrefix,
                completes->ElementAt(i).mTableName,
                mGethashNoise, *completes);
       break;
     }
@@ -1702,17 +1706,17 @@ nsUrlClassifierDBService::Init()
   rv = NS_NewNamedThread("URL Classifier", &gDbBackgroundThread);
   if (NS_FAILED(rv))
     return rv;
 
   mWorker = new nsUrlClassifierDBServiceWorker();
   if (!mWorker)
     return NS_ERROR_OUT_OF_MEMORY;
 
-  rv = mWorker->Init(sGethashNoise, cacheDir);
+  rv = mWorker->Init(sGethashNoise, cacheDir, this);
   if (NS_FAILED(rv)) {
     mWorker = nullptr;
     return rv;
   }
 
   // Proxy for calling the worker on the background thread
   mWorkerProxy = new UrlClassifierDBServiceWorkerProxy(mWorker);
   rv = mWorkerProxy->OpenDb();
@@ -2245,29 +2249,33 @@ nsresult
 nsUrlClassifierDBService::CacheMisses(PrefixArray *results)
 {
   NS_ENSURE_TRUE(gDbBackgroundThread, NS_ERROR_NOT_INITIALIZED);
 
   return mWorkerProxy->CacheMisses(results);
 }
 
 bool
+nsUrlClassifierDBService::CanComplete(const nsACString &aTableName)
+{
+  return mGethashTables.Contains(aTableName) &&
+    !mDisallowCompletionsTables.Contains(aTableName);
+}
+
+bool
 nsUrlClassifierDBService::GetCompleter(const nsACString &tableName,
                                        nsIUrlClassifierHashCompleter **completer)
 {
   // If we have specified a completer, go ahead and query it. This is only
   // used by tests.
   if (mCompleters.Get(tableName, completer)) {
     return true;
   }
 
-  // If we don't know about this table at all, or are disallowing completions
-  // for it, skip completion checks.
-  if (!mGethashTables.Contains(tableName) ||
-      mDisallowCompletionsTables.Contains(tableName)) {
+  if (!CanComplete(tableName)) {
     return false;
   }
 
   // Otherwise, call gethash to find the hash completions.
   return NS_SUCCEEDED(CallGetService(NS_URLCLASSIFIERHASHCOMPLETER_CONTRACTID,
                                      completer));
 }
 
@@ -2354,16 +2362,18 @@ nsUrlClassifierDBService::Shutdown()
   nsIThread *backgroundThread = nullptr;
   Swap(backgroundThread, gDbBackgroundThread);
 
   // 4. Wait until the worker thread is down.
   if (backgroundThread) {
     backgroundThread->Shutdown();
     NS_RELEASE(backgroundThread);
   }
+
+  mWorker = nullptr;
   return NS_OK;
 }
 
 nsIThread*
 nsUrlClassifierDBService::BackgroundThread()
 {
   return gDbBackgroundThread;
 }
--- a/toolkit/components/url-classifier/nsUrlClassifierDBService.h
+++ b/toolkit/components/url-classifier/nsUrlClassifierDBService.h
@@ -95,18 +95,19 @@ public:
 
   NS_DECLARE_STATIC_IID_ACCESSOR(NS_URLCLASSIFIERDBSERVICE_CID)
 
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_NSIURLCLASSIFIERDBSERVICE
   NS_DECL_NSIURICLASSIFIER
   NS_DECL_NSIOBSERVER
 
+  bool CanComplete(const nsACString &tableName);
   bool GetCompleter(const nsACString& tableName,
-                      nsIUrlClassifierHashCompleter** completer);
+                    nsIUrlClassifierHashCompleter** completer);
   nsresult CacheCompletions(mozilla::safebrowsing::CacheResultArray *results);
   nsresult CacheMisses(mozilla::safebrowsing::PrefixArray *results);
 
   static nsIThread* BackgroundThread();
 
   static bool ShutdownHasStarted();
 
 private:
@@ -185,17 +186,19 @@ private:
 class nsUrlClassifierDBServiceWorker final : public nsIUrlClassifierDBService
 {
 public:
   nsUrlClassifierDBServiceWorker();
 
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_NSIURLCLASSIFIERDBSERVICE
 
-  nsresult Init(uint32_t aGethashNoise, nsCOMPtr<nsIFile> aCacheDir);
+  nsresult Init(uint32_t aGethashNoise,
+                nsCOMPtr<nsIFile> aCacheDir,
+                nsUrlClassifierDBService* aDBService);
 
   // Queue a lookup for the worker to perform, called in the main thread.
   // tables is a comma-separated list of tables to query
   nsresult QueueLookup(const nsACString& lookupKey,
                        const nsACString& tables,
                        nsIUrlClassifierLookupCallback* callback);
 
   // Handle any queued-up lookups.  We call this function during long-running
@@ -263,16 +266,18 @@ private:
 
   nsAutoPtr<mozilla::safebrowsing::Classifier> mClassifier;
   // The class that actually parses the update chunks.
   nsAutoPtr<ProtocolParser> mProtocolParser;
 
   // Directory where to store the SB databases.
   nsCOMPtr<nsIFile> mCacheDir;
 
+  RefPtr<nsUrlClassifierDBService> mDBService;
+
   // XXX: maybe an array of autoptrs.  Or maybe a class specifically
   // storing a series of updates.
   nsTArray<mozilla::safebrowsing::TableUpdate*> mTableUpdates;
 
   uint32_t mUpdateWaitSec;
 
   // Entries that cannot be completed. We expect them to die at
   // the next update