Bug 1395102 - P4 - Reuse the code for MaybeUpgradeClients() in the upgrade helpers; r=janv
authorTom Tung <shes050117@gmail.com>
Fri, 19 Oct 2018 11:36:08 +0200
changeset 490623 c768d21501f987b2bef5293994c5b772b48991b0
parent 490622 47335300b3377e6a3c696fb9392d30d567a2bac6
child 490624 fd985440b6e293c33048c5f90ad3e88d6dfe3c3b
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersjanv
bugs1395102
milestone64.0a1
Bug 1395102 - P4 - Reuse the code for MaybeUpgradeClients() in the upgrade helpers; r=janv This patch moves MaybeUpgradeClients to RepositoryOperationBase class from UpgradeStorageFrom1_0To2_0Helper and UpgradeStorageFrom2_0To2_1Helper so that lines of code can be reused.
dom/quota/ActorsParent.cpp
--- a/dom/quota/ActorsParent.cpp
+++ b/dom/quota/ActorsParent.cpp
@@ -78,17 +78,17 @@
 #if DISABLE_ASSERTS_FOR_FUZZING
 #define ASSERT_UNLESS_FUZZING(...) do { } while (0)
 #else
 #define ASSERT_UNLESS_FUZZING(...) MOZ_ASSERT(false, __VA_ARGS__)
 #endif
 
 #define UNKNOWN_FILE_WARNING(_leafName) \
   QM_WARNING("Something (%s) in the directory that doesn't belong!", \
-             NS_ConvertUTF16toUTF8(leafName).get())
+             NS_ConvertUTF16toUTF8(_leafName).get())
 
 // The amount of time, in milliseconds, that our IO thread will stay alive
 // after the last event it processes.
 #define DEFAULT_THREAD_TIMEOUT_MS 30000
 
 // The amount of time, in milliseconds, that we will wait for active storage
 // transactions on shutdown before aborting them.
 #define DEFAULT_SHUTDOWN_TIMER_MS 30000
@@ -1812,16 +1812,21 @@ public:
 
   nsresult
   ProcessRepository();
 
 protected:
   virtual ~RepositoryOperationBase()
   { }
 
+  template<typename UpgradeMethod>
+  nsresult
+  MaybeUpgradeClients(const OriginProps& aOriginsProps,
+                      UpgradeMethod aMethod);
+
 private:
   virtual nsresult
   PrepareOriginDirectory(OriginProps& aOriginProps, bool* aRemoved) = 0;
 };
 
 class CreateOrUpgradeDirectoryMetadataHelper final
   : public RepositoryOperationBase
 {
@@ -1867,17 +1872,17 @@ class UpgradeStorageFrom1_0To2_0Helper f
 public:
   UpgradeStorageFrom1_0To2_0Helper(nsIFile* aDirectory,
                                    bool aPersistent)
     : RepositoryOperationBase(aDirectory, aPersistent)
   { }
 
 private:
   nsresult
-  MaybeUpgradeClients(const OriginProps& aOriginProps);
+  MaybeRemoveMorgueDirectory(const OriginProps& aOriginProps);
 
   nsresult
   MaybeRemoveAppsData(const OriginProps& aOriginProps,
                       bool* aRemoved);
 
   nsresult
   MaybeStripObsoleteOriginAttributes(const OriginProps& aOriginProps,
                                      bool* aStripped);
@@ -1895,19 +1900,16 @@ class UpgradeStorageFrom2_0To2_1Helper f
 public:
   UpgradeStorageFrom2_0To2_1Helper(nsIFile* aDirectory,
                                    bool aPersistent)
     : RepositoryOperationBase(aDirectory, aPersistent)
   { }
 
 private:
   nsresult
-  MaybeUpgradeClients(const OriginProps& aOriginProps);
-
-  nsresult
   PrepareOriginDirectory(OriginProps& aOriginProps, bool* aRemoved) override;
 
   nsresult
   ProcessOriginDirectory(const OriginProps& aOriginProps) override;
 };
 
 class RestoreDirectoryMetadata2Helper final
   : public StorageOperationBase
@@ -8850,16 +8852,86 @@ RepositoryOperationBase::ProcessReposito
   rv = ProcessOriginDirectories();
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   return NS_OK;
 }
 
+template<typename UpgradeMethod>
+nsresult
+RepositoryOperationBase::MaybeUpgradeClients(const OriginProps& aOriginProps,
+                                             UpgradeMethod aMethod)
+{
+  AssertIsOnIOThread();
+  MOZ_ASSERT(aOriginProps.mDirectory);
+  MOZ_ASSERT(aMethod);
+
+  QuotaManager* quotaManager = QuotaManager::Get();
+  MOZ_ASSERT(quotaManager);
+
+  nsCOMPtr<nsIDirectoryEnumerator> entries;
+  nsresult rv =
+    aOriginProps.mDirectory->GetDirectoryEntries(getter_AddRefs(entries));
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  while (true) {
+    nsCOMPtr<nsIFile> file;
+    rv = entries->GetNextFile(getter_AddRefs(file));
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
+
+    if (!file) {
+      break;
+    }
+
+    bool isDirectory;
+    rv = file->IsDirectory(&isDirectory);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
+
+    nsString leafName;
+    rv = file->GetLeafName(leafName);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
+
+    if (!isDirectory) {
+      // Unknown files during upgrade are allowed. Just warn if we find them.
+      if (!IsOriginMetadata(leafName) &&
+          !IsTempMetadata(leafName)) {
+        UNKNOWN_FILE_WARNING(leafName);
+      }
+      continue;
+    }
+
+    Client::Type clientType;
+    rv = Client::TypeFromText(leafName, clientType);
+    if (NS_FAILED(rv)) {
+      UNKNOWN_FILE_WARNING(leafName);
+      continue;
+    }
+
+    Client* client = quotaManager->GetClient(clientType);
+    MOZ_ASSERT(client);
+
+    rv = (client->*aMethod)(file);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
+  }
+
+  return NS_OK;
+}
+
 nsresult
 CreateOrUpgradeDirectoryMetadataHelper::MaybeUpgradeOriginDirectory(
                                                             nsIFile* aDirectory)
 {
   AssertIsOnIOThread();
   MOZ_ASSERT(aDirectory);
 
   nsCOMPtr<nsIFile> metadataFile;
@@ -9180,87 +9252,51 @@ UpgradeStorageFrom0_0To1_0Helper::Proces
       return rv;
     }
   }
 
   return NS_OK;
 }
 
 nsresult
-UpgradeStorageFrom1_0To2_0Helper::MaybeUpgradeClients(
+UpgradeStorageFrom1_0To2_0Helper::MaybeRemoveMorgueDirectory(
                                                 const OriginProps& aOriginProps)
 {
   AssertIsOnIOThread();
   MOZ_ASSERT(aOriginProps.mDirectory);
 
-  QuotaManager* quotaManager = QuotaManager::Get();
-  MOZ_ASSERT(quotaManager);
-
-  nsCOMPtr<nsIDirectoryEnumerator> entries;
-  nsresult rv =
-    aOriginProps.mDirectory->GetDirectoryEntries(getter_AddRefs(entries));
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
-  }
-
-  nsCOMPtr<nsIFile> file;
-  while (NS_SUCCEEDED((rv = entries->GetNextFile(getter_AddRefs(file)))) && file) {
-    bool isDirectory;
-    rv = file->IsDirectory(&isDirectory);
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      return rv;
-    }
-
-    nsString leafName;
-    rv = file->GetLeafName(leafName);
+  // The Cache API was creating top level morgue directories by accident for
+  // a short time in nightly.  This unfortunately prevents all storage from
+  // working.  So recover these profiles permanently by removing these corrupt
+  // directories as part of this upgrade.
+
+  nsCOMPtr<nsIFile> morgueDir;
+  nsresult rv = aOriginProps.mDirectory->Clone(getter_AddRefs(morgueDir));
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  rv = morgueDir->Append(NS_LITERAL_STRING("morgue"));
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  bool exists;
+  rv = morgueDir->Exists(&exists);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  if (exists) {
+    QM_WARNING("Deleting accidental morgue directory!");
+
+    rv = morgueDir->Remove(/* recursive */ true);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
-
-    if (!isDirectory) {
-      // Unknown files during upgrade are allowed. Just warn if we find them.
-      if (!IsOriginMetadata(leafName) &&
-          !IsTempMetadata(leafName)) {
-        UNKNOWN_FILE_WARNING(leafName);
-      }
-      continue;
-    }
-
-    // The Cache API was creating top level morgue directories by accident for
-    // a short time in nightly.  This unfortunately prevents all storage from
-    // working.  So recover these profiles permanently by removing these corrupt
-    // directories as part of this upgrade.
-    if (leafName.EqualsLiteral("morgue")) {
-      QM_WARNING("Deleting accidental morgue directory!");
-
-      rv = file->Remove(/* recursive */ true);
-      if (NS_WARN_IF(NS_FAILED(rv))) {
-        return rv;
-      }
-
-      continue;
-    }
-
-    Client::Type clientType;
-    rv = Client::TypeFromText(leafName, clientType);
-    if (NS_FAILED(rv)) {
-      UNKNOWN_FILE_WARNING(leafName);
-      continue;
-    }
-
-    Client* client = quotaManager->GetClient(clientType);
-    MOZ_ASSERT(client);
-
-    rv = client->UpgradeStorageFrom1_0To2_0(file);
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      return rv;
-    }
-  }
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
   }
 
   return NS_OK;
 }
 
 nsresult
 UpgradeStorageFrom1_0To2_0Helper::MaybeRemoveAppsData(
                                                 const OriginProps& aOriginProps,
@@ -9363,17 +9399,23 @@ nsresult
 UpgradeStorageFrom1_0To2_0Helper::PrepareOriginDirectory(
                                                       OriginProps& aOriginProps,
                                                       bool* aRemoved)
 {
   AssertIsOnIOThread();
   MOZ_ASSERT(aOriginProps.mDirectory);
   MOZ_ASSERT(aRemoved);
 
-  nsresult rv = MaybeUpgradeClients(aOriginProps);
+  nsresult rv = MaybeRemoveMorgueDirectory(aOriginProps);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  rv = MaybeUpgradeClients(aOriginProps,
+                           &Client::UpgradeStorageFrom1_0To2_0);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   bool removed;
   rv = MaybeRemoveAppsData(aOriginProps, &removed);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
@@ -9452,87 +9494,26 @@ UpgradeStorageFrom1_0To2_0Helper::Proces
       return rv;
     }
   }
 
   return NS_OK;
 }
 
 nsresult
-UpgradeStorageFrom2_0To2_1Helper::MaybeUpgradeClients(
-                                                const OriginProps& aOriginProps)
-{
-  AssertIsOnIOThread();
-  MOZ_ASSERT(aOriginProps.mDirectory);
-
-  QuotaManager* quotaManager = QuotaManager::Get();
-  MOZ_ASSERT(quotaManager);
-
-  nsCOMPtr<nsIDirectoryEnumerator> entries;
-  nsresult rv =
-    aOriginProps.mDirectory->GetDirectoryEntries(getter_AddRefs(entries));
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
-  }
-
-  nsCOMPtr<nsIFile> file;
-  while (NS_SUCCEEDED((rv = entries->GetNextFile(getter_AddRefs(file)))) && file) {
-    bool isDirectory;
-    rv = file->IsDirectory(&isDirectory);
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      return rv;
-    }
-
-    nsString leafName;
-    rv = file->GetLeafName(leafName);
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      return rv;
-    }
-
-    if (!isDirectory) {
-      // Unknown files during upgrade are allowed. Just warn if we find them.
-      if (!IsOriginMetadata(leafName) &&
-          !IsTempMetadata(leafName)) {
-        UNKNOWN_FILE_WARNING(leafName);
-      }
-      continue;
-    }
-
-    Client::Type clientType;
-    rv = Client::TypeFromText(leafName, clientType);
-    if (NS_FAILED(rv)) {
-      UNKNOWN_FILE_WARNING(leafName);
-      continue;
-    }
-
-    Client* client = quotaManager->GetClient(clientType);
-    MOZ_ASSERT(client);
-
-    rv = client->UpgradeStorageFrom2_0To2_1(file);
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      return rv;
-    }
-  }
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
-  }
-
-  return NS_OK;
-}
-
-nsresult
 UpgradeStorageFrom2_0To2_1Helper::PrepareOriginDirectory(
                                                       OriginProps& aOriginProps,
                                                       bool* aRemoved)
 {
   AssertIsOnIOThread();
   MOZ_ASSERT(aOriginProps.mDirectory);
   MOZ_ASSERT(aRemoved);
 
-  nsresult rv = MaybeUpgradeClients(aOriginProps);
+  nsresult rv = MaybeUpgradeClients(aOriginProps,
+                                    &Client::UpgradeStorageFrom2_0To2_1);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   int64_t timestamp;
   nsCString group;
   nsCString origin;
   Nullable<bool> isApp;