Bug 1434206 - Use a TableUpdateV2 param in ApplyUpdate(). r?gcp draft
authorFrancois Marier <francois@mozilla.com>
Thu, 10 May 2018 16:06:49 -0700
changeset 805418 264867295deda8de89923bafdbf1840fe34d57b9
parent 805417 4f6092ac597941b88a41bbc4f4bd34714e05ab4d
child 805419 f4ad52217a006c87742198169cea8b483d4e29d9
push id112654
push userfmarier@mozilla.com
push dateThu, 07 Jun 2018 20:10:46 +0000
reviewersgcp
bugs1434206
milestone62.0a1
Bug 1434206 - Use a TableUpdateV2 param in ApplyUpdate(). r?gcp HashStore::ApplyUpdate() is a V2-only function and so we can be explicit about that and remove unnecessary casts. Add a new update error code for when we fail to cast a TableUpdate object to the expected protocol version. MozReview-Commit-ID: 65BBwiZJw6J
toolkit/components/url-classifier/Classifier.cpp
toolkit/components/url-classifier/HashStore.cpp
toolkit/components/url-classifier/HashStore.h
xpcom/base/ErrorList.py
--- a/toolkit/components/url-classifier/Classifier.cpp
+++ b/toolkit/components/url-classifier/Classifier.cpp
@@ -1230,36 +1230,37 @@ Classifier::UpdateHashStore(nsTArray<Tab
   rv = store.AugmentAdds(AddPrefixHashes);
   NS_ENSURE_SUCCESS(rv, rv);
   AddPrefixHashes.Clear();
 
   uint32_t applied = 0;
 
   for (uint32_t i = 0; i < aUpdates->Length(); i++) {
     TableUpdate *update = aUpdates->ElementAt(i);
-    if (!update || !update->TableName().Equals(store.TableName()))
+    if (!update || !update->TableName().Equals(store.TableName())) {
       continue;
+    }
 
-    rv = store.ApplyUpdate(*update);
+    TableUpdateV2* updateV2 = TableUpdate::Cast<TableUpdateV2>(update);
+    NS_ENSURE_TRUE(updateV2, NS_ERROR_UC_UPDATE_UNEXPECTED_VERSION);
+
+    rv = store.ApplyUpdate(updateV2);
     NS_ENSURE_SUCCESS(rv, rv);
 
     applied++;
 
-    auto updateV2 = TableUpdate::Cast<TableUpdateV2>(update);
-    if (updateV2) {
-      LOG(("Applied update to table %s:", store.TableName().get()));
-      LOG(("  %d add chunks", updateV2->AddChunks().Length()));
-      LOG(("  %zu add prefixes", updateV2->AddPrefixes().Length()));
-      LOG(("  %zu add completions", updateV2->AddCompletes().Length()));
-      LOG(("  %d sub chunks", updateV2->SubChunks().Length()));
-      LOG(("  %zu sub prefixes", updateV2->SubPrefixes().Length()));
-      LOG(("  %zu sub completions", updateV2->SubCompletes().Length()));
-      LOG(("  %d add expirations", updateV2->AddExpirations().Length()));
-      LOG(("  %d sub expirations", updateV2->SubExpirations().Length()));
-    }
+    LOG(("Applied update to table %s:", store.TableName().get()));
+    LOG(("  %d add chunks", updateV2->AddChunks().Length()));
+    LOG(("  %zu add prefixes", updateV2->AddPrefixes().Length()));
+    LOG(("  %zu add completions", updateV2->AddCompletes().Length()));
+    LOG(("  %d sub chunks", updateV2->SubChunks().Length()));
+    LOG(("  %zu sub prefixes", updateV2->SubPrefixes().Length()));
+    LOG(("  %zu sub completions", updateV2->SubCompletes().Length()));
+    LOG(("  %d add expirations", updateV2->AddExpirations().Length()));
+    LOG(("  %d sub expirations", updateV2->SubExpirations().Length()));
 
     aUpdates->ElementAt(i) = nullptr;
   }
 
   LOG(("Applied %d update(s) to %s.", applied, store.TableName().get()));
 
   rv = store.Rebuild();
   NS_ENSURE_SUCCESS(rv, rv);
@@ -1324,17 +1325,17 @@ Classifier::UpdateTableV4(nsTArray<Table
   TableUpdateV4* lastAppliedUpdate = nullptr;
   for (uint32_t i = 0; i < aUpdates->Length(); i++) {
     TableUpdate *update = aUpdates->ElementAt(i);
     if (!update || !update->TableName().Equals(aTable)) {
       continue;
     }
 
     auto updateV4 = TableUpdate::Cast<TableUpdateV4>(update);
-    NS_ENSURE_TRUE(updateV4, NS_ERROR_UC_UPDATE_TABLE_NOT_FOUND);
+    NS_ENSURE_TRUE(updateV4, NS_ERROR_UC_UPDATE_UNEXPECTED_VERSION);
 
     if (updateV4->IsFullUpdate()) {
       input->Clear();
       output->Clear();
       rv = lookupCache->ApplyUpdate(updateV4, *input, *output);
       if (NS_FAILED(rv)) {
         return rv;
       }
--- a/toolkit/components/url-classifier/HashStore.cpp
+++ b/toolkit/components/url-classifier/HashStore.cpp
@@ -614,46 +614,43 @@ Merge(ChunkSet* aStoreChunks,
     return NS_ERROR_OUT_OF_MEMORY;
 
   EntrySort(*aStorePrefixes);
 
   return NS_OK;
 }
 
 nsresult
-HashStore::ApplyUpdate(TableUpdate &aUpdate)
+HashStore::ApplyUpdate(TableUpdateV2 *aUpdate)
 {
-  auto updateV2 = TableUpdate::Cast<TableUpdateV2>(&aUpdate);
-  NS_ENSURE_TRUE(updateV2, NS_ERROR_FAILURE);
+  MOZ_ASSERT(mTableName.Equals(aUpdate->TableName()));
 
-  TableUpdateV2& update = *updateV2;
-
-  nsresult rv = mAddExpirations.Merge(update.AddExpirations());
+  nsresult rv = mAddExpirations.Merge(aUpdate->AddExpirations());
   NS_ENSURE_SUCCESS(rv, rv);
 
-  rv = mSubExpirations.Merge(update.SubExpirations());
+  rv = mSubExpirations.Merge(aUpdate->SubExpirations());
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = Expire();
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = Merge(&mAddChunks, &mAddPrefixes,
-             update.AddChunks(), update.AddPrefixes());
+             aUpdate->AddChunks(), aUpdate->AddPrefixes());
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = Merge(&mAddChunks, &mAddCompletes,
-             update.AddChunks(), update.AddCompletes(), true);
+             aUpdate->AddChunks(), aUpdate->AddCompletes(), true);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = Merge(&mSubChunks, &mSubPrefixes,
-             update.SubChunks(), update.SubPrefixes());
+             aUpdate->SubChunks(), aUpdate->SubPrefixes());
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = Merge(&mSubChunks, &mSubCompletes,
-             update.SubChunks(), update.SubCompletes(), true);
+             aUpdate->SubChunks(), aUpdate->SubCompletes(), true);
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
 
 nsresult
 HashStore::Rebuild()
 {
--- a/toolkit/components/url-classifier/HashStore.h
+++ b/toolkit/components/url-classifier/HashStore.h
@@ -215,17 +215,17 @@ public:
 
   // =======
   // Updates
   // =======
   // Begin the update process.  Reads the store into memory.
   nsresult BeginUpdate();
 
   // Imports the data from a TableUpdate.
-  nsresult ApplyUpdate(TableUpdate &aUpdate);
+  nsresult ApplyUpdate(TableUpdateV2 *aUpdate);
 
   // Process expired chunks
   nsresult Expire();
 
   // Rebuild the store, Incorporating all the applied updates.
   nsresult Rebuild();
 
   // Write the current state of the store to disk.
--- a/xpcom/base/ErrorList.py
+++ b/xpcom/base/ErrorList.py
@@ -1080,16 +1080,17 @@ with modules["URL_CLASSIFIER"]:
     errors["NS_ERROR_UC_UPDATE_INFINITE_LOOP"] = FAILURE(3)
     errors["NS_ERROR_UC_UPDATE_WRONG_REMOVAL_INDICES"] = FAILURE(4)
     errors["NS_ERROR_UC_UPDATE_CHECKSUM_MISMATCH"] = FAILURE(5)
     errors["NS_ERROR_UC_UPDATE_MISSING_CHECKSUM"] = FAILURE(6)
     errors["NS_ERROR_UC_UPDATE_SHUTDOWNING"] = FAILURE(7)
     errors["NS_ERROR_UC_UPDATE_TABLE_NOT_FOUND"] = FAILURE(8)
     errors["NS_ERROR_UC_UPDATE_BUILD_PREFIX_FAILURE"] = FAILURE(9)
     errors["NS_ERROR_UC_UPDATE_FAIL_TO_WRITE_DISK"] = FAILURE(10)
+    errors["NS_ERROR_UC_UPDATE_UNEXPECTED_VERSION"] = FAILURE(11)
 
     # Specific errors while parsing pver2/pver4 responses
     errors["NS_ERROR_UC_PARSER_MISSING_PARAM"] = FAILURE(12)
     errors["NS_ERROR_UC_PARSER_DECODE_FAILURE"] = FAILURE(13)
     errors["NS_ERROR_UC_PARSER_UNKNOWN_THREAT"] = FAILURE(14)
 
 
 # =======================================================================