Bug 1395102 - P3 - Reuse the code for QuotaManager::UpgradeStorageFromxxToxx(); r=janv
authorTom Tung <shes050117@gmail.com>
Wed, 17 Oct 2018 15:37:16 +0200
changeset 490622 47335300b3377e6a3c696fb9392d30d567a2bac6
parent 490621 a0de0c0ec6b01e9d74cd2bcfe6333e1cbaf566dc
child 490623 c768d21501f987b2bef5293994c5b772b48991b0
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersjanv
bugs1395102
milestone64.0a1
Bug 1395102 - P3 - Reuse the code for QuotaManager::UpgradeStorageFromxxToxx(); r=janv This patch adds a new function QM::UpgradeStorage() to reuse lines of code in QM:UpgradeStorageFrom0_0To1_0(), QM:UpgradeStorageFrom1_0To2_0(), and QM:UpgradeStorageFrom2_0To2_1().
dom/quota/ActorsParent.cpp
dom/quota/QuotaManager.h
--- a/dom/quota/ActorsParent.cpp
+++ b/dom/quota/ActorsParent.cpp
@@ -4583,36 +4583,28 @@ QuotaManager::MaybeRemoveOldDirectories(
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
   }
 
   return NS_OK;
 }
 
+template<typename Helper>
 nsresult
-QuotaManager::UpgradeStorageFrom0_0To1_0(mozIStorageConnection* aConnection)
+QuotaManager::UpgradeStorage(const int32_t aOldVersion,
+                             const int32_t aNewVersion,
+                             mozIStorageConnection* aConnection)
 {
   AssertIsOnIOThread();
+  MOZ_ASSERT(aNewVersion > aOldVersion);
+  MOZ_ASSERT(aNewVersion <= kStorageVersion);
   MOZ_ASSERT(aConnection);
 
-  nsresult rv = MaybeUpgradeIndexedDBDirectory();
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
-  }
-
-  rv = MaybeUpgradePersistentStorageDirectory();
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
-  }
-
-  rv = MaybeRemoveOldDirectories();
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return rv;
-  }
+  nsresult rv;
 
   for (const PersistenceType persistenceType : kAllPersistenceTypes) {
     nsCOMPtr<nsIFile> directory;
     rv = NS_NewLocalFile(GetStoragePath(persistenceType), false,
                          getter_AddRefs(directory));
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
@@ -4623,38 +4615,68 @@ QuotaManager::UpgradeStorageFrom0_0To1_0
       return rv;
     }
 
     if (!exists) {
       continue;
     }
 
     bool persistent = persistenceType == PERSISTENCE_TYPE_PERSISTENT;
-    RefPtr<UpgradeStorageFrom0_0To1_0Helper> helper =
-      new UpgradeStorageFrom0_0To1_0Helper(directory, persistent);
-
+    RefPtr<RepositoryOperationBase> helper = new Helper(directory, persistent);
     rv = helper->ProcessRepository();
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
   }
 
 #ifdef DEBUG
   {
     int32_t storageVersion;
     rv = aConnection->GetSchemaVersion(&storageVersion);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
 
-    MOZ_ASSERT(storageVersion == 0);
+    MOZ_ASSERT(storageVersion == aOldVersion);
   }
 #endif
 
-  rv = aConnection->SetSchemaVersion(MakeStorageVersion(1, 0));
+  rv = aConnection->SetSchemaVersion(aNewVersion);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  return NS_OK;
+}
+
+nsresult
+QuotaManager::UpgradeStorageFrom0_0To1_0(mozIStorageConnection* aConnection)
+{
+  AssertIsOnIOThread();
+  MOZ_ASSERT(aConnection);
+
+  nsresult rv = MaybeUpgradeIndexedDBDirectory();
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  rv = MaybeUpgradePersistentStorageDirectory();
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  rv = MaybeRemoveOldDirectories();
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return rv;
+  }
+
+  rv =
+    UpgradeStorage<UpgradeStorageFrom0_0To1_0Helper>(0,
+                                                     MakeStorageVersion(1, 0),
+                                                     aConnection);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   return NS_OK;
 }
 
 nsresult
@@ -4725,118 +4747,40 @@ QuotaManager::UpgradeStorageFrom1_0To2_0
   // [Downgrade-incompatible changes]:
   // File manager directories with the ".files" suffix prevent older versions of
   // Firefox from initializing and using the storage.
   // File manager directories without the ".files" suffix can appear if user
   // runs an already upgraded profile in an older version of Firefox. File
   // manager directories without the ".files" suffix then prevent current
   // Firefox from initializing and using the storage.
 
-  nsresult rv;
-
-  for (const PersistenceType persistenceType : kAllPersistenceTypes) {
-    nsCOMPtr<nsIFile> directory;
-    rv = NS_NewLocalFile(GetStoragePath(persistenceType), false,
-                         getter_AddRefs(directory));
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      return rv;
-    }
-
-    bool exists;
-    rv = directory->Exists(&exists);
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      return rv;
-    }
-
-    if (!exists) {
-      continue;
-    }
-
-    bool persistent = persistenceType == PERSISTENCE_TYPE_PERSISTENT;
-    RefPtr<UpgradeStorageFrom1_0To2_0Helper> helper =
-      new UpgradeStorageFrom1_0To2_0Helper(directory, persistent);
-
-    rv = helper->ProcessRepository();
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      return rv;
-    }
-  }
-
-#ifdef DEBUG
-  {
-    int32_t storageVersion;
-    rv = aConnection->GetSchemaVersion(&storageVersion);
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      return rv;
-    }
-
-    MOZ_ASSERT(storageVersion == MakeStorageVersion(1, 0));
-  }
-#endif
-
-  rv = aConnection->SetSchemaVersion(MakeStorageVersion(2, 0));
+  nsresult rv =
+    UpgradeStorage<UpgradeStorageFrom1_0To2_0Helper>(MakeStorageVersion(1, 0),
+                                                     MakeStorageVersion(2, 0),
+                                                     aConnection);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   return NS_OK;
 }
 
 nsresult
 QuotaManager::UpgradeStorageFrom2_0To2_1(mozIStorageConnection* aConnection)
 {
   AssertIsOnIOThread();
   MOZ_ASSERT(aConnection);
 
   // The upgrade is mainly to create a directory padding file in DOM Cache
   // directory to record the overall padding size of an origin.
 
-  nsresult rv;
-
-  for (const PersistenceType persistenceType : kAllPersistenceTypes) {
-    nsCOMPtr<nsIFile> directory;
-    rv = NS_NewLocalFile(GetStoragePath(persistenceType), false,
-                         getter_AddRefs(directory));
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      return rv;
-    }
-
-    bool exists;
-    rv = directory->Exists(&exists);
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      return rv;
-    }
-
-    if (!exists) {
-      continue;
-    }
-
-    bool persistent = persistenceType == PERSISTENCE_TYPE_PERSISTENT;
-    RefPtr<UpgradeStorageFrom2_0To2_1Helper> helper =
-      new UpgradeStorageFrom2_0To2_1Helper(directory, persistent);
-
-    rv = helper->ProcessRepository();
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      return rv;
-    }
-  }
-
-#ifdef DEBUG
-  {
-    int32_t storageVersion;
-    rv = aConnection->GetSchemaVersion(&storageVersion);
-    if (NS_WARN_IF(NS_FAILED(rv))) {
-      return rv;
-    }
-
-    MOZ_ASSERT(storageVersion == MakeStorageVersion(2, 0));
-  }
-#endif
-
-  rv = aConnection->SetSchemaVersion(MakeStorageVersion(2, 1));
+  nsresult rv =
+    UpgradeStorage<UpgradeStorageFrom2_0To2_1Helper>(MakeStorageVersion(2, 0),
+                                                     MakeStorageVersion(2, 1),
+                                                     aConnection);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return rv;
   }
 
   return NS_OK;
 }
 
 nsresult
--- a/dom/quota/QuotaManager.h
+++ b/dom/quota/QuotaManager.h
@@ -472,16 +472,22 @@ private:
   MaybeUpgradeIndexedDBDirectory();
 
   nsresult
   MaybeUpgradePersistentStorageDirectory();
 
   nsresult
   MaybeRemoveOldDirectories();
 
+  template<typename Helper>
+  nsresult
+  UpgradeStorage(const int32_t aOldVersion,
+                 const int32_t aNewVersion,
+                 mozIStorageConnection* aConnection);
+
   nsresult
   UpgradeStorageFrom0_0To1_0(mozIStorageConnection* aConnection);
 
   nsresult
   UpgradeStorageFrom1_0To2_0(mozIStorageConnection* aConnection);
 
   nsresult
   UpgradeStorageFrom2_0To2_1(mozIStorageConnection* aConnection);