Bug 1351472 - Skip AddNoise if the table is unknown or disallowed to getHash r=francois
authorThomas Nguyen <tnguyen@mozilla.com>
Fri, 28 Apr 2017 15:26:54 +0800
changeset 356740 6afd88f235febc9a3e988718bbd25c86a43577fa
parent 356739 47612d95b17794664b14584b295764bad724d270
child 356741 cfb9c5cf896590a5c7ac3c6cc2a1da081c071ed9
push id31770
push userkwierso@gmail.com
push dateFri, 05 May 2017 20:22:40 +0000
treeherdermozilla-central@e7bf9443be2c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfrancois
bugs1351472
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1351472 - Skip AddNoise if the table is unknown or disallowed to getHash r=francois 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;
     }
@@ -1714,17 +1718,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();
@@ -2257,29 +2261,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));
 }
 
@@ -2366,16 +2374,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