Bug 1362149 - Let URL Classifier use a default fallback file from the app directory, when a cached file does not exist. r=dimi draft
authorFelipe Gomes <felipc@gmail.com>
Thu, 18 May 2017 22:42:09 -0300
changeset 580846 8a980c64e1175318202bb919f4b26a5601661b2e
parent 580524 1ae7d2cdb61387354e97e2edea3b3972202ac15d
child 580848 87a4c8e4e4407aabe72597acce45c714f3c297ec
push id59677
push userfelipc@gmail.com
push dateFri, 19 May 2017 01:43:16 +0000
reviewersdimi
bugs1362149
milestone55.0a1
Bug 1362149 - Let URL Classifier use a default fallback file from the app directory, when a cached file does not exist. r=dimi This allows us to include these files in the build, only for the tables desired, to be used from a fresh startup until the list update can be downloaded. This file is read-only, and once the code receives a list update from the server and saves it locally to the cached location, this code path won't be used, and the code will proceed as usual with the newest content MozReview-Commit-ID: 9KK6yE8m2i1
toolkit/components/url-classifier/HashStore.cpp
toolkit/components/url-classifier/LookupCache.cpp
--- a/toolkit/components/url-classifier/HashStore.cpp
+++ b/toolkit/components/url-classifier/HashStore.cpp
@@ -37,16 +37,17 @@
 #include "nsCheckSummedOutputStream.h"
 #include "prio.h"
 #include "mozilla/Logging.h"
 #include "mozilla/IntegerPrintfMacros.h"
 #include "mozilla/SizePrintfMacros.h"
 #include "zlib.h"
 #include "Classifier.h"
 #include "nsUrlClassifierDBService.h"
+#include "nsDirectoryServiceDefs.h"
 #include "mozilla/Telemetry.h"
 
 // Main store for SafeBrowsing protocol data. We store
 // known add/sub chunks, prefixes and completions in memory
 // during an update, and serialize to disk.
 // We do not store the add prefixes, those are retrieved by
 // decompressing the PrefixSet cache whenever we need to apply
 // an update.
@@ -306,27 +307,49 @@ HashStore::CheckChecksum(uint32_t aFileS
 
 nsresult
 HashStore::Open()
 {
   nsCOMPtr<nsIFile> storeFile;
   nsresult rv = mStoreDirectory->Clone(getter_AddRefs(storeFile));
   NS_ENSURE_SUCCESS(rv, rv);
 
-  rv = storeFile->AppendNative(mTableName + NS_LITERAL_CSTRING(".sbstore"));
+  rv = storeFile->AppendNative(mTableName + NS_LITERAL_CSTRING(STORE_SUFFIX));
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIInputStream> origStream;
   rv = NS_NewLocalFileInputStream(getter_AddRefs(origStream), storeFile,
                                   PR_RDONLY | nsIFile::OS_READAHEAD);
 
   if (rv == NS_ERROR_FILE_NOT_FOUND) {
+    rv = NS_GetSpecialDirectory(NS_GRE_DIR,
+                                getter_AddRefs(storeFile));
+
+    rv = storeFile->AppendNative(NS_LITERAL_CSTRING("defaults"));
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    rv = storeFile->AppendNative(NS_LITERAL_CSTRING("safebrowsing"));
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    rv = storeFile->AppendNative(mTableName + NS_LITERAL_CSTRING(STORE_SUFFIX));
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    rv = NS_NewLocalFileInputStream(getter_AddRefs(origStream), storeFile,
+                                  PR_RDONLY | nsIFile::OS_READAHEAD);
+
+    if (rv != NS_ERROR_FILE_NOT_FOUND) {
+      LOG("Stored file was not found but a default file was found and will be used");
+    }
+  }
+
+  if (rv == NS_ERROR_FILE_NOT_FOUND) {
     UpdateHeader();
     return NS_OK;
   }
+
   NS_ENSURE_SUCCESS(rv, rv);
 
   int64_t fileSize;
   rv = storeFile->GetFileSize(&fileSize);
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (fileSize < 0 || fileSize > UINT32_MAX) {
     return NS_ERROR_FAILURE;
--- a/toolkit/components/url-classifier/LookupCache.cpp
+++ b/toolkit/components/url-classifier/LookupCache.cpp
@@ -3,16 +3,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "LookupCache.h"
 #include "HashStore.h"
 #include "nsISeekableStream.h"
 #include "mozilla/Telemetry.h"
 #include "mozilla/Logging.h"
+#include "nsDirectoryServiceDefs.h"
 #include "nsNetUtil.h"
 #include "prprf.h"
 #include "Classifier.h"
 
 // We act as the main entry point for all the real lookups,
 // so note that those are not done to the actual HashStore.
 // The latter solely exists to store the data needed to handle
 // the updates from the protocol.
@@ -406,16 +407,45 @@ LookupCache::LoadPrefixSet()
   if (exists) {
     LOG(("stored PrefixSet exists, loading from disk"));
     rv = LoadFromFile(psFile);
     if (NS_FAILED(rv)) {
       return rv;
     }
     mPrimed = true;
   } else {
+    nsCOMPtr<nsIFile> defaultPsFile;
+    rv = NS_GetSpecialDirectory(NS_GRE_DIR,
+                                getter_AddRefs(defaultPsFile));
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    rv = defaultPsFile->AppendNative(NS_LITERAL_CSTRING("defaults"));
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    rv = defaultPsFile->AppendNative(NS_LITERAL_CSTRING("safebrowsing"));
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    rv = defaultPsFile->AppendNative(mTableName + NS_LITERAL_CSTRING(PREFIXSET_SUFFIX));
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    bool defaultExists;
+    rv = defaultPsFile->Exists(&defaultExists);
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    if (defaultExists) {
+      LOG(("default PrefixSet exists, loading from disk"));
+      rv = LoadFromFile(defaultPsFile);
+      if (NS_FAILED(rv)) {
+        return rv;
+      }
+      mPrimed = true;
+    }
+  }
+
+  if (!mPrimed) {
     LOG(("no (usable) stored PrefixSet found"));
   }
 
 #ifdef DEBUG
   if (mPrimed) {
     uint32_t size = SizeOfPrefixSet();
     LOG(("SB tree done, size = %d bytes\n", size));
   }