Bug 1527704 - Store install information in profiles.ini and use installs.ini as a backup in case an earlier Firefox throws it away. r=froydnj a=pascalc
authorDave Townsend <dtownsend@oxymoronical.com>
Wed, 27 Mar 2019 14:41:04 -0700
changeset 525894 626d4588cc90b132a7005c700a2b094c01a10267
parent 525893 cba236106e276ca96713e2ccd921f59f9139a589
child 525895 39a03fc4bac62ce4def8cd2cdaffe350b3df2417
push id2032
push userffxbld-merge
push dateMon, 13 May 2019 09:36:57 +0000
treeherdermozilla-release@455c1065dcbe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj, pascalc
bugs1527704
milestone67.0
Bug 1527704 - Store install information in profiles.ini and use installs.ini as a backup in case an earlier Firefox throws it away. r=froydnj a=pascalc Originally we stored the new information about installation defaults in installs.ini since older versions of Firefox would throw away any new data in profiles.ini any time they made changes to the profiles. That does however mean we have to load two files on startup. This changes things so that we save all the data in profiles.ini as well as a version tag and still save the install data into installs.ini. An older version will throw away the install data and version tag from profiles.ini but leave installs.ini alone. On startup if the version tag is gone from profiles.ini then we reload the install data from installs.ini and put it back into profiles.ini. At some point in the future where we don't care about supporting older versions of Firefox we can just drop installs.ini entirely. A lot of the changes here involve moving to loading profiles.ini into an in-memory ini, keeping it up to date and flushing it to disk. This means that we no longer throw away any information in the ini file that this version does not understand allowing the possibility of adding new data to this file in the future. Differential Revision: https://phabricator.services.mozilla.com/D22576
toolkit/profile/nsIToolkitProfile.idl
toolkit/profile/nsToolkitProfileService.cpp
toolkit/profile/nsToolkitProfileService.h
toolkit/profile/xpcshell/head.js
toolkit/profile/xpcshell/test_check_backup.js
toolkit/profile/xpcshell/test_claim_locked.js
toolkit/profile/xpcshell/test_clean.js
toolkit/profile/xpcshell/test_create_default.js
toolkit/profile/xpcshell/test_lock.js
toolkit/profile/xpcshell/test_missing_profilesini.js
toolkit/profile/xpcshell/test_new_default.js
toolkit/profile/xpcshell/test_previous_dedicated.js
toolkit/profile/xpcshell/test_remove.js
toolkit/profile/xpcshell/test_remove_default.js
toolkit/profile/xpcshell/test_select_default.js
toolkit/profile/xpcshell/test_single_profile_selected.js
toolkit/profile/xpcshell/test_single_profile_unselected.js
toolkit/profile/xpcshell/test_skip_locked_environment.js
toolkit/profile/xpcshell/test_snatch_environment.js
toolkit/profile/xpcshell/test_snatch_environment_default.js
toolkit/profile/xpcshell/test_steal_inuse.js
toolkit/profile/xpcshell/test_update_selected_dedicated.js
toolkit/profile/xpcshell/test_update_unknown_dedicated.js
toolkit/profile/xpcshell/test_update_unselected_dedicated.js
toolkit/profile/xpcshell/test_use_dedicated.js
toolkit/profile/xpcshell/xpcshell.ini
xpcom/base/nsINIParser.cpp
xpcom/base/nsINIParser.h
--- a/toolkit/profile/nsIToolkitProfile.idl
+++ b/toolkit/profile/nsIToolkitProfile.idl
@@ -41,17 +41,17 @@ interface nsIProfileLock : nsISupports
     void unlock();
 };
 
 /**
  * A interface representing a profile.
  * @note THIS INTERFACE SHOULD BE IMPLEMENTED BY THE TOOLKIT CODE ONLY! DON'T
  *       EVEN THINK ABOUT IMPLEMENTING THIS IN JAVASCRIPT!
  */
-[scriptable, uuid(7422b090-4a86-4407-972e-75468a625388)]
+[scriptable, builtinclass, uuid(7422b090-4a86-4407-972e-75468a625388)]
 interface nsIToolkitProfile : nsISupports
 {
     /**
      * The location of the profile directory.
      */
     readonly attribute nsIFile rootDir;
 
     /**
--- a/toolkit/profile/nsToolkitProfileService.cpp
+++ b/toolkit/profile/nsToolkitProfileService.cpp
@@ -47,30 +47,77 @@
 #include "nsIToolkitShellService.h"
 #include "mozilla/Telemetry.h"
 
 using namespace mozilla;
 
 #define DEV_EDITION_NAME "dev-edition-default"
 #define DEFAULT_NAME "default"
 #define COMPAT_FILE NS_LITERAL_STRING("compatibility.ini")
+#define PROFILE_DB_VERSION "2"
+#define INSTALL_PREFIX "Install"
+#define INSTALL_PREFIX_LENGTH 7
+
+struct KeyValue {
+  KeyValue(const char* aKey, const char* aValue) : key(aKey), value(aValue) {}
+
+  nsCString key;
+  nsCString value;
+};
+
+static bool GetStrings(const char* aString, const char* aValue,
+                       void* aClosure) {
+  nsTArray<UniquePtr<KeyValue>>* array =
+      static_cast<nsTArray<UniquePtr<KeyValue>>*>(aClosure);
+  array->AppendElement(MakeUnique<KeyValue>(aString, aValue));
+
+  return true;
+}
+
+/**
+ * Returns an array of the strings inside a section of an ini file.
+ */
+nsTArray<UniquePtr<KeyValue>> GetSectionStrings(nsINIParser* aParser,
+                                                const char* aSection) {
+  nsTArray<UniquePtr<KeyValue>> result;
+  aParser->GetStrings(aSection, &GetStrings, &result);
+  return result;
+}
 
 nsToolkitProfile::nsToolkitProfile(const nsACString& aName, nsIFile* aRootDir,
-                                   nsIFile* aLocalDir, nsToolkitProfile* aPrev)
-    : mPrev(aPrev),
-      mName(aName),
+                                   nsIFile* aLocalDir, bool aFromDB)
+    : mName(aName),
       mRootDir(aRootDir),
       mLocalDir(aLocalDir),
-      mLock(nullptr) {
+      mLock(nullptr),
+      mIndex(0),
+      mSection("Profile") {
   NS_ASSERTION(aRootDir, "No file!");
 
-  if (aPrev) {
-    aPrev->mNext = this;
-  } else {
-    nsToolkitProfileService::gService->mFirst = this;
+  RefPtr<nsToolkitProfile> prev =
+      nsToolkitProfileService::gService->mProfiles.getLast();
+  if (prev) {
+    mIndex = prev->mIndex + 1;
+  }
+  mSection.AppendInt(mIndex);
+
+  nsToolkitProfileService::gService->mProfiles.insertBack(this);
+
+  // If this profile isn't in the database already add it.
+  if (!aFromDB) {
+    nsINIParser* db = &nsToolkitProfileService::gService->mProfileDB;
+    db->SetString(mSection.get(), "Name", mName.get());
+
+    bool isRelative = false;
+    nsCString descriptor;
+    nsToolkitProfileService::gService->GetProfileDescriptor(this, descriptor,
+                                                            &isRelative);
+
+    db->SetString(mSection.get(), "IsRelative", isRelative ? "1" : "0");
+    db->SetString(mSection.get(), "Path", descriptor.get());
   }
 }
 
 NS_IMPL_ISUPPORTS(nsToolkitProfile, nsIToolkitProfile)
 
 NS_IMETHODIMP
 nsToolkitProfile::GetRootDir(nsIFile** aResult) {
   NS_ADDREF(*aResult = mRootDir);
@@ -101,34 +148,39 @@ nsToolkitProfile::SetName(const nsACStri
   // profile no longer the dev-edition default.
   if (mName.EqualsLiteral(DEV_EDITION_NAME) &&
       nsToolkitProfileService::gService->mDevEditionDefault == this) {
     nsToolkitProfileService::gService->mDevEditionDefault = nullptr;
   }
 
   mName = aName;
 
+  nsresult rv = nsToolkitProfileService::gService->mProfileDB.SetString(
+      mSection.get(), "Name", mName.get());
+  NS_ENSURE_SUCCESS(rv, rv);
+
   // Setting the name to the dev-edition default profile name will cause this
   // profile to become the dev-edition default.
   if (aName.EqualsLiteral(DEV_EDITION_NAME) &&
       !nsToolkitProfileService::gService->mDevEditionDefault) {
     nsToolkitProfileService::gService->mDevEditionDefault = this;
   }
 
   return NS_OK;
 }
 
 nsresult nsToolkitProfile::RemoveInternal(bool aRemoveFiles,
                                           bool aInBackground) {
   NS_ASSERTION(nsToolkitProfileService::gService, "Whoa, my service is gone.");
 
   if (mLock) return NS_ERROR_FILE_IS_LOCKED;
 
-  if (!mPrev && !mNext && nsToolkitProfileService::gService->mFirst != this)
+  if (!isInList()) {
     return NS_ERROR_NOT_INITIALIZED;
+  }
 
   if (aRemoveFiles) {
     // Check if another instance is using this profile.
     nsCOMPtr<nsIProfileLock> lock;
     nsresult rv = Lock(nullptr, getter_AddRefs(lock));
     NS_ENSURE_SUCCESS(rv, rv);
 
     nsCOMPtr<nsIFile> rootDir(mRootDir);
@@ -155,25 +207,39 @@ nsresult nsToolkitProfile::RemoveInterna
       nsCOMPtr<nsIEventTarget> target =
           do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
       target->Dispatch(runnable, NS_DISPATCH_NORMAL);
     } else {
       runnable->Run();
     }
   }
 
-  if (mPrev)
-    mPrev->mNext = mNext;
-  else
-    nsToolkitProfileService::gService->mFirst = mNext;
+  nsINIParser* db = &nsToolkitProfileService::gService->mProfileDB;
+  db->DeleteSection(mSection.get());
 
-  if (mNext) mNext->mPrev = mPrev;
+  // We make some assumptions that the profile's index in the database is based
+  // on its position in the linked list. Removing a profile means we have to fix
+  // the index of later profiles in the list. The easiest way to do that is just
+  // to move the last profile into the profile's position and just update its
+  // index.
+  RefPtr<nsToolkitProfile> last =
+      nsToolkitProfileService::gService->mProfiles.getLast();
+  if (last != this) {
+    // Update the section in the db.
+    last->mIndex = mIndex;
+    db->RenameSection(last->mSection.get(), mSection.get());
+    last->mSection = mSection;
 
-  mPrev = nullptr;
-  mNext = nullptr;
+    if (last != getNext()) {
+      last->remove();
+      setNext(last);
+    }
+  }
+
+  remove();
 
   if (nsToolkitProfileService::gService->mNormalDefault == this) {
     nsToolkitProfileService::gService->mNormalDefault = nullptr;
   }
   if (nsToolkitProfileService::gService->mDevEditionDefault == this) {
     nsToolkitProfileService::gService->mDevEditionDefault = nullptr;
   }
   if (nsToolkitProfileService::gService->mDedicatedProfile == this) {
@@ -308,17 +374,20 @@ nsToolkitProfileService::nsToolkitProfil
       mStartupReason(NS_LITERAL_STRING("unknown")),
       mMaybeLockProfile(false) {
 #ifdef MOZ_DEV_EDITION
   mUseDevEditionProfile = true;
 #endif
   gService = this;
 }
 
-nsToolkitProfileService::~nsToolkitProfileService() { gService = nullptr; }
+nsToolkitProfileService::~nsToolkitProfileService() {
+  gService = nullptr;
+  mProfiles.clear();
+}
 
 void nsToolkitProfileService::CompleteStartup() {
   if (!mStartupProfileSelected) {
     return;
   }
 
   ScalarSet(mozilla::Telemetry::ScalarID::STARTUP_PROFILE_SELECTION_REASON,
             mStartupReason);
@@ -330,17 +399,17 @@ void nsToolkitProfileService::CompleteSt
       return;
     }
 
     bool isDefaultApp;
     nsresult rv = shell->IsDefaultApplication(&isDefaultApp);
     NS_ENSURE_SUCCESS_VOID(rv);
 
     if (isDefaultApp) {
-      mInstallData.SetString(mInstallHash.get(), "Locked", "1");
+      mProfileDB.SetString(mInstallSection.get(), "Locked", "1");
       Flush();
     }
   }
 }
 
 // Tests whether the passed profile was last used by this install.
 bool nsToolkitProfileService::IsProfileForCurrentInstall(
     nsIToolkitProfile* aProfile) {
@@ -435,120 +504,169 @@ bool nsToolkitProfileService::MaybeMakeD
   // Cache the installs that use the profile.
   nsTArray<nsCString> inUseInstalls;
 
   // See if the profile is already in use by an install that hasn't locked it.
   for (uint32_t i = 0; i < installs.Length(); i++) {
     const nsCString& install = installs[i];
 
     nsCString path;
-    rv = mInstallData.GetString(install.get(), "Default", path);
+    rv = mProfileDB.GetString(install.get(), "Default", path);
     if (NS_FAILED(rv)) {
       continue;
     }
 
     // Is this install using the profile we care about?
     if (!descriptor.Equals(path)) {
       continue;
     }
 
     // Is this profile locked to this other install?
     nsCString isLocked;
-    rv = mInstallData.GetString(install.get(), "Locked", isLocked);
+    rv = mProfileDB.GetString(install.get(), "Locked", isLocked);
     if (NS_SUCCEEDED(rv) && isLocked.Equals("1")) {
       return false;
     }
 
     inUseInstalls.AppendElement(install);
   }
 
   // At this point we've decided to take the profile. Strip it from other
   // installs.
   for (uint32_t i = 0; i < inUseInstalls.Length(); i++) {
     // Removing the default setting entirely will make the install go through
     // the first run process again at startup and create itself a new profile.
-    mInstallData.DeleteString(inUseInstalls[i].get(), "Default");
+    mProfileDB.DeleteString(inUseInstalls[i].get(), "Default");
   }
 
   // Set this as the default profile for this install.
   SetDefaultProfile(aProfile);
 
   // SetDefaultProfile will have locked this profile to this install so no
   // other installs will steal it, but this was auto-selected so we want to
   // unlock it so that other installs can potentially take it.
-  mInstallData.DeleteString(mInstallHash.get(), "Locked");
+  mProfileDB.DeleteString(mInstallSection.get(), "Locked");
 
   // Persist the changes.
   Flush();
 
   // Once XPCOM is available check if this is the default application and if so
   // lock the profile again.
   mMaybeLockProfile = true;
 
   return true;
 }
 
+struct ImportInstallsClosure {
+  nsINIParser* backupData;
+  nsINIParser* profileDB;
+};
+
+static bool ImportInstalls(const char* aSection, void* aClosure) {
+  ImportInstallsClosure* closure =
+      static_cast<ImportInstallsClosure*>(aClosure);
+
+  nsTArray<UniquePtr<KeyValue>> strings =
+      GetSectionStrings(closure->backupData, aSection);
+  if (strings.IsEmpty()) {
+    return true;
+  }
+
+  nsCString newSection(INSTALL_PREFIX);
+  newSection.Append(aSection);
+  nsCString buffer;
+
+  for (uint32_t i = 0; i < strings.Length(); i++) {
+    closure->profileDB->SetString(newSection.get(), strings[i]->key.get(),
+                                  strings[i]->value.get());
+  }
+
+  return true;
+}
+
 nsresult nsToolkitProfileService::Init() {
   NS_ASSERTION(gDirServiceProvider, "No dirserviceprovider!");
   nsresult rv;
 
   rv = nsXREDirProvider::GetUserAppDataDirectory(getter_AddRefs(mAppData));
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = nsXREDirProvider::GetUserLocalDataDirectory(getter_AddRefs(mTempData));
   NS_ENSURE_SUCCESS(rv, rv);
 
-  nsCString installProfilePath;
-
-  if (mUseDedicatedProfile) {
-    // Load the dedicated profiles database.
-    rv = mAppData->Clone(getter_AddRefs(mInstallFile));
-    NS_ENSURE_SUCCESS(rv, rv);
-
-    rv = mInstallFile->AppendNative(NS_LITERAL_CSTRING("installs.ini"));
-    NS_ENSURE_SUCCESS(rv, rv);
+  rv = mAppData->Clone(getter_AddRefs(mProfileDBFile));
+  NS_ENSURE_SUCCESS(rv, rv);
 
-    nsString installHash;
-    rv = gDirServiceProvider->GetInstallHash(installHash);
-    NS_ENSURE_SUCCESS(rv, rv);
-    CopyUTF16toUTF8(installHash, mInstallHash);
+  rv = mProfileDBFile->AppendNative(NS_LITERAL_CSTRING("profiles.ini"));
+  NS_ENSURE_SUCCESS(rv, rv);
 
-    rv = mInstallData.Init(mInstallFile);
-    if (NS_SUCCEEDED(rv)) {
-      // Try to find the descriptor for the default profile for this install.
-      rv = mInstallData.GetString(mInstallHash.get(), "Default",
-                                  installProfilePath);
-      // Not having a value means this install doesn't appear in installs.ini so
-      // this is the first run for this install.
-      mIsFirstRun = NS_FAILED(rv);
-    }
-  }
-
-  rv = mAppData->Clone(getter_AddRefs(mListFile));
+  rv = mAppData->Clone(getter_AddRefs(mInstallDBFile));
   NS_ENSURE_SUCCESS(rv, rv);
 
-  rv = mListFile->AppendNative(NS_LITERAL_CSTRING("profiles.ini"));
+  rv = mInstallDBFile->AppendNative(NS_LITERAL_CSTRING("installs.ini"));
   NS_ENSURE_SUCCESS(rv, rv);
 
-  nsINIParser parser;
+  nsAutoCString buffer;
 
   bool exists;
-  rv = mListFile->IsFile(&exists);
+  rv = mProfileDBFile->IsFile(&exists);
   if (NS_SUCCEEDED(rv) && exists) {
-    rv = parser.Init(mListFile);
+    rv = mProfileDB.Init(mProfileDBFile);
     // Init does not fail on parsing errors, only on OOM/really unexpected
     // conditions.
     if (NS_FAILED(rv)) {
       return rv;
     }
+
+    rv = mProfileDB.GetString("General", "StartWithLastProfile", buffer);
+    if (NS_SUCCEEDED(rv)) {
+      mStartWithLast = !buffer.EqualsLiteral("0");
+    }
+
+    rv = mProfileDB.GetString("General", "Version", buffer);
+    if (NS_FAILED(rv)) {
+      // This is a profiles.ini written by an older version. We must restore
+      // any install data from the backup.
+      nsINIParser installDB;
+
+      rv = mInstallDBFile->IsFile(&exists);
+      if (NS_SUCCEEDED(rv) && exists &&
+          NS_SUCCEEDED(installDB.Init(mInstallDBFile))) {
+        // There is install data to import.
+        ImportInstallsClosure closure = {&installDB, &mProfileDB};
+        installDB.GetSections(&ImportInstalls, &closure);
+      }
+
+      rv = mProfileDB.SetString("General", "Version", PROFILE_DB_VERSION);
+      NS_ENSURE_SUCCESS(rv, rv);
+    }
+  } else {
+    rv = mProfileDB.SetString("General", "StartWithLastProfile",
+                              mStartWithLast ? "1" : "0");
+    NS_ENSURE_SUCCESS(rv, rv);
+    rv = mProfileDB.SetString("General", "Version", PROFILE_DB_VERSION);
+    NS_ENSURE_SUCCESS(rv, rv);
   }
 
-  nsAutoCString buffer;
-  rv = parser.GetString("General", "StartWithLastProfile", buffer);
-  if (NS_SUCCEEDED(rv) && buffer.EqualsLiteral("0")) mStartWithLast = false;
+  nsCString installProfilePath;
+
+  if (mUseDedicatedProfile) {
+    nsString installHash;
+    rv = gDirServiceProvider->GetInstallHash(installHash);
+    NS_ENSURE_SUCCESS(rv, rv);
+    CopyUTF16toUTF8(installHash, mInstallSection);
+    mInstallSection.Insert(INSTALL_PREFIX, 0);
+
+    // Try to find the descriptor for the default profile for this install.
+    rv = mProfileDB.GetString(mInstallSection.get(), "Default",
+                              installProfilePath);
+    // Not having a value means this install doesn't appear in installs.ini so
+    // this is the first run for this install.
+    mIsFirstRun = NS_FAILED(rv);
+  }
 
   nsToolkitProfile* currentProfile = nullptr;
 
 #ifdef MOZ_DEV_EDITION
   nsCOMPtr<nsIFile> ignoreDevEditionProfile;
   rv = mAppData->Clone(getter_AddRefs(ignoreDevEditionProfile));
   if (NS_FAILED(rv)) {
     return rv;
@@ -570,32 +688,32 @@ nsresult nsToolkitProfileService::Init()
   nsCOMPtr<nsIToolkitProfile> autoSelectProfile;
 
   unsigned int nonDevEditionProfiles = 0;
   unsigned int c = 0;
   for (c = 0; true; ++c) {
     nsAutoCString profileID("Profile");
     profileID.AppendInt(c);
 
-    rv = parser.GetString(profileID.get(), "IsRelative", buffer);
+    rv = mProfileDB.GetString(profileID.get(), "IsRelative", buffer);
     if (NS_FAILED(rv)) break;
 
     bool isRelative = buffer.EqualsLiteral("1");
 
     nsAutoCString filePath;
 
-    rv = parser.GetString(profileID.get(), "Path", filePath);
+    rv = mProfileDB.GetString(profileID.get(), "Path", filePath);
     if (NS_FAILED(rv)) {
       NS_ERROR("Malformed profiles.ini: Path= not found");
       continue;
     }
 
     nsAutoCString name;
 
-    rv = parser.GetString(profileID.get(), "Name", name);
+    rv = mProfileDB.GetString(profileID.get(), "Name", name);
     if (NS_FAILED(rv)) {
       NS_ERROR("Malformed profiles.ini: Name= not found");
       continue;
     }
 
     nsCOMPtr<nsIFile> rootDir;
     rv = NS_NewNativeLocalFile(EmptyCString(), true, getter_AddRefs(rootDir));
     NS_ENSURE_SUCCESS(rv, rv);
@@ -613,21 +731,20 @@ nsresult nsToolkitProfileService::Init()
           NS_NewNativeLocalFile(EmptyCString(), true, getter_AddRefs(localDir));
       NS_ENSURE_SUCCESS(rv, rv);
 
       rv = localDir->SetRelativeDescriptor(mTempData, filePath);
     } else {
       localDir = rootDir;
     }
 
-    currentProfile =
-        new nsToolkitProfile(name, rootDir, localDir, currentProfile);
+    currentProfile = new nsToolkitProfile(name, rootDir, localDir, true);
     NS_ENSURE_TRUE(currentProfile, NS_ERROR_OUT_OF_MEMORY);
 
-    rv = parser.GetString(profileID.get(), "Default", buffer);
+    rv = mProfileDB.GetString(profileID.get(), "Default", buffer);
     if (NS_SUCCEEDED(rv) && buffer.EqualsLiteral("1")) {
       mNormalDefault = currentProfile;
     }
 
     // Is this the default profile for this install?
     if (mUseDedicatedProfile && !mDedicatedProfile &&
         installProfilePath.Equals(filePath)) {
       // Found a profile for this install.
@@ -639,17 +756,17 @@ nsresult nsToolkitProfileService::Init()
     } else {
       nonDevEditionProfiles++;
       autoSelectProfile = currentProfile;
     }
   }
 
   // If there is only one non-dev-edition profile then mark it as the default.
   if (!mNormalDefault && nonDevEditionProfiles == 1) {
-    mNormalDefault = autoSelectProfile;
+    SetNormalDefault(autoSelectProfile);
   }
 
   if (!mUseDedicatedProfile) {
     if (mUseDevEditionProfile) {
       // When using the separate dev-edition profile not finding it means this
       // is a first run.
       mIsFirstRun = !mDevEditionDefault;
     } else {
@@ -659,30 +776,33 @@ nsresult nsToolkitProfileService::Init()
   }
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsToolkitProfileService::SetStartWithLastProfile(bool aValue) {
   if (mStartWithLast != aValue) {
+    nsresult rv = mProfileDB.SetString("General", "StartWithLastProfile",
+                                       mStartWithLast ? "1" : "0");
+    NS_ENSURE_SUCCESS(rv, rv);
     mStartWithLast = aValue;
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsToolkitProfileService::GetStartWithLastProfile(bool* aResult) {
   *aResult = mStartWithLast;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsToolkitProfileService::GetProfiles(nsISimpleEnumerator** aResult) {
-  *aResult = new ProfileEnumerator(this->mFirst);
+  *aResult = new ProfileEnumerator(mProfiles.getFirst());
   if (!*aResult) return NS_ERROR_OUT_OF_MEMORY;
 
   NS_ADDREF(*aResult);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsToolkitProfileService::ProfileEnumerator::HasMoreElements(bool* aResult) {
@@ -691,17 +811,17 @@ nsToolkitProfileService::ProfileEnumerat
 }
 
 NS_IMETHODIMP
 nsToolkitProfileService::ProfileEnumerator::GetNext(nsISupports** aResult) {
   if (!mCurrent) return NS_ERROR_FAILURE;
 
   NS_ADDREF(*aResult = mCurrent);
 
-  mCurrent = mCurrent->mNext;
+  mCurrent = mCurrent->getNext();
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsToolkitProfileService::GetCurrentProfile(nsIToolkitProfile** aResult) {
   NS_IF_ADDREF(*aResult = mCurrent);
   return NS_OK;
 }
@@ -717,48 +837,69 @@ nsToolkitProfileService::GetDefaultProfi
     NS_IF_ADDREF(*aResult = mDevEditionDefault);
     return NS_OK;
   }
 
   NS_IF_ADDREF(*aResult = mNormalDefault);
   return NS_OK;
 }
 
+void nsToolkitProfileService::SetNormalDefault(nsIToolkitProfile* aProfile) {
+  if (mNormalDefault == aProfile) {
+    return;
+  }
+
+  if (mNormalDefault) {
+    nsToolkitProfile* profile =
+        static_cast<nsToolkitProfile*>(mNormalDefault.get());
+    mProfileDB.DeleteString(profile->mSection.get(), "Default");
+  }
+
+  mNormalDefault = aProfile;
+
+  if (mNormalDefault) {
+    nsToolkitProfile* profile =
+        static_cast<nsToolkitProfile*>(mNormalDefault.get());
+    mProfileDB.SetString(profile->mSection.get(), "Default", "1");
+  }
+}
+
 NS_IMETHODIMP
 nsToolkitProfileService::SetDefaultProfile(nsIToolkitProfile* aProfile) {
   if (mUseDedicatedProfile) {
     if (mDedicatedProfile != aProfile) {
       if (!aProfile) {
         // Setting this to the empty string means no profile will be found on
         // startup but we'll recognise that this install has been used
         // previously.
-        mInstallData.SetString(mInstallHash.get(), "Default", "");
+        mProfileDB.SetString(mInstallSection.get(), "Default", "");
       } else {
         nsCString profilePath;
         nsresult rv = GetProfileDescriptor(aProfile, profilePath, nullptr);
         NS_ENSURE_SUCCESS(rv, rv);
 
-        mInstallData.SetString(mInstallHash.get(), "Default",
-                               profilePath.get());
+        mProfileDB.SetString(mInstallSection.get(), "Default",
+                             profilePath.get());
       }
       mDedicatedProfile = aProfile;
 
       // Some kind of choice has happened here, lock this profile to this
       // install.
-      mInstallData.SetString(mInstallHash.get(), "Locked", "1");
+      mProfileDB.SetString(mInstallSection.get(), "Locked", "1");
     }
     return NS_OK;
   }
 
   if (mUseDevEditionProfile && aProfile != mDevEditionDefault) {
     // The separate profile is hardcoded.
     return NS_ERROR_FAILURE;
   }
 
-  mNormalDefault = aProfile;
+  SetNormalDefault(aProfile);
+
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsToolkitProfileService::GetCreatedAlternateProfile(bool* aResult) {
   *aResult = mCreatedAlternateProfile;
   return NS_OK;
 }
@@ -808,17 +949,17 @@ nsresult nsToolkitProfileService::Create
   nsresult rv = CreateUniqueProfile(nullptr, name, aResult);
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (mUseDedicatedProfile) {
     SetDefaultProfile(mCurrent);
   } else if (mUseDevEditionProfile) {
     mDevEditionDefault = mCurrent;
   } else {
-    mNormalDefault = mCurrent;
+    SetNormalDefault(mCurrent);
   }
 
   return NS_OK;
 }
 
 /**
  * An implementation of SelectStartupProfile callable from JavaScript via XPCOM.
  * See nsIToolkitProfileService.idl.
@@ -837,19 +978,19 @@ nsToolkitProfileService::SelectStartupPr
 
   for (int i = 0; i < argc; i++) {
     allocated[i].reset(ToNewCString(aArgv[i]));
     argv[i] = allocated[i].get();
   }
   argv[argc] = nullptr;
 
   bool wasDefault;
-  nsresult rv = SelectStartupProfile(&argc, argv.get(), aIsResetting, aRootDir,
-                                     aLocalDir, aProfile, aDidCreate,
-                                     &wasDefault);
+  nsresult rv =
+      SelectStartupProfile(&argc, argv.get(), aIsResetting, aRootDir, aLocalDir,
+                           aProfile, aDidCreate, &wasDefault);
 
   // Since we were called outside of the normal startup path complete any
   // startup tasks.
   if (NS_SUCCEEDED(rv)) {
     CompleteStartup();
   }
 
   return rv;
@@ -1158,20 +1299,22 @@ nsresult nsToolkitProfileService::Select
     }
 
     rv = CreateDefaultProfile(getter_AddRefs(mCurrent));
     if (NS_SUCCEEDED(rv)) {
       // If there is only one profile and it isn't meant to be the profile that
       // older versions of Firefox use then we must create a default profile
       // for older versions of Firefox to avoid the existing profile being
       // auto-selected.
-      if ((mUseDedicatedProfile || mUseDevEditionProfile) && mFirst &&
-          !mFirst->mNext) {
+      if ((mUseDedicatedProfile || mUseDevEditionProfile) &&
+          mProfiles.getFirst() == mProfiles.getLast()) {
+        nsCOMPtr<nsIToolkitProfile> newProfile;
         CreateProfile(nullptr, NS_LITERAL_CSTRING(DEFAULT_NAME),
-                      getter_AddRefs(mNormalDefault));
+                      getter_AddRefs(newProfile));
+        SetNormalDefault(newProfile);
       }
 
       Flush();
 
       if (mCreatedAlternateProfile) {
         mStartupReason = NS_LITERAL_STRING("firstrun-skipped-default");
       } else {
         mStartupReason = NS_LITERAL_STRING("firstrun-created-default");
@@ -1244,32 +1387,32 @@ nsresult nsToolkitProfileService::Create
  * current profile and if the old profile was default making the new profile
  * default as well.
  */
 nsresult nsToolkitProfileService::ApplyResetProfile(
     nsIToolkitProfile* aOldProfile) {
   // If the old profile would have been the default for old installs then mark
   // the new profile as such.
   if (mNormalDefault == aOldProfile) {
-    mNormalDefault = mCurrent;
+    SetNormalDefault(mCurrent);
   }
 
   if (mUseDedicatedProfile && mDedicatedProfile == aOldProfile) {
     bool wasLocked = false;
     nsCString val;
     if (NS_SUCCEEDED(
-            mInstallData.GetString(mInstallHash.get(), "Locked", val))) {
+            mProfileDB.GetString(mInstallSection.get(), "Locked", val))) {
       wasLocked = val.Equals("1");
     }
 
     SetDefaultProfile(mCurrent);
 
     // Make the locked state match if necessary.
     if (!wasLocked) {
-      mInstallData.DeleteString(mInstallHash.get(), "Locked");
+      mProfileDB.DeleteString(mInstallSection.get(), "Locked");
     }
   }
 
   nsCString name;
   nsresult rv = aOldProfile->GetName(name);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = aOldProfile->Remove(false);
@@ -1281,47 +1424,43 @@ nsresult nsToolkitProfileService::ApplyR
   NS_ENSURE_SUCCESS(rv, rv);
 
   return Flush();
 }
 
 NS_IMETHODIMP
 nsToolkitProfileService::GetProfileByName(const nsACString& aName,
                                           nsIToolkitProfile** aResult) {
-  nsToolkitProfile* curP = mFirst;
-  while (curP) {
-    if (curP->mName.Equals(aName)) {
-      NS_ADDREF(*aResult = curP);
+  for (RefPtr<nsToolkitProfile> profile : mProfiles) {
+    if (profile->mName.Equals(aName)) {
+      NS_ADDREF(*aResult = profile);
       return NS_OK;
     }
-    curP = curP->mNext;
   }
 
   return NS_ERROR_FAILURE;
 }
 
 /**
  * Finds a profile from the database that uses the given root and local
  * directories.
  */
 void nsToolkitProfileService::GetProfileByDir(nsIFile* aRootDir,
                                               nsIFile* aLocalDir,
                                               nsIToolkitProfile** aResult) {
-  nsToolkitProfile* curP = mFirst;
-  while (curP) {
+  for (RefPtr<nsToolkitProfile> profile : mProfiles) {
     bool equal;
-    nsresult rv = curP->mRootDir->Equals(aRootDir, &equal);
+    nsresult rv = profile->mRootDir->Equals(aRootDir, &equal);
     if (NS_SUCCEEDED(rv) && equal) {
-      rv = curP->mLocalDir->Equals(aLocalDir, &equal);
+      rv = profile->mLocalDir->Equals(aLocalDir, &equal);
       if (NS_SUCCEEDED(rv) && equal) {
-        NS_ADDREF(*aResult = curP);
+        NS_ADDREF(*aResult = profile);
         return;
       }
     }
-    curP = curP->mNext;
   }
 }
 
 nsresult NS_LockProfilePath(nsIFile* aPath, nsIFile* aTempPath,
                             nsIProfileUnlocker** aUnlocker,
                             nsIProfileLock** aResult) {
   RefPtr<nsToolkitProfileLock> lock = new nsToolkitProfileLock();
   if (!lock) return NS_ERROR_OUT_OF_MEMORY;
@@ -1444,25 +1583,18 @@ nsToolkitProfileService::CreateProfile(n
   }
 
   // We created a new profile dir. Let's store a creation timestamp.
   // Note that this code path does not apply if the profile dir was
   // created prior to launching.
   rv = CreateTimesInternal(rootDir);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  nsToolkitProfile* last = mFirst.get();
-  if (last) {
-    while (last->mNext) {
-      last = last->mNext;
-    }
-  }
-
   nsCOMPtr<nsIToolkitProfile> profile =
-      new nsToolkitProfile(aName, rootDir, localDir, last);
+      new nsToolkitProfile(aName, rootDir, localDir, false);
   if (!profile) return NS_ERROR_OUT_OF_MEMORY;
 
   if (aName.Equals(DEV_EDITION_NAME)) {
     mDevEditionDefault = profile;
   }
 
   profile.forget(aResult);
   return NS_OK;
@@ -1491,27 +1623,32 @@ bool nsToolkitProfileService::IsSnapEnvi
 struct FindInstallsClosure {
   nsINIParser* installData;
   nsTArray<nsCString>* installs;
 };
 
 static bool FindInstalls(const char* aSection, void* aClosure) {
   FindInstallsClosure* closure = static_cast<FindInstallsClosure*>(aClosure);
 
+  // Check if the section starts with "Install"
+  if (strncmp(aSection, INSTALL_PREFIX, INSTALL_PREFIX_LENGTH) != 0) {
+    return true;
+  }
+
   nsCString install(aSection);
   closure->installs->AppendElement(install);
 
   return true;
 }
 
 nsTArray<nsCString> nsToolkitProfileService::GetKnownInstalls() {
   nsTArray<nsCString> result;
-  FindInstallsClosure closure = {&mInstallData, &result};
+  FindInstallsClosure closure = {&mProfileDB, &result};
 
-  mInstallData.GetSections(&FindInstalls, &closure);
+  mProfileDB.GetSections(&FindInstalls, &closure);
 
   return result;
 }
 
 nsresult nsToolkitProfileService::CreateTimesInternal(nsIFile* aProfileDir) {
   nsresult rv = NS_ERROR_FAILURE;
   nsCOMPtr<nsIFile> creationLog;
   rv = aProfileDir->Clone(getter_AddRefs(creationLog));
@@ -1540,94 +1677,81 @@ nsresult nsToolkitProfileService::Create
   PR_fprintf(writeFile, "{\n\"created\": %lld,\n\"firstUse\": null\n}\n", msec);
   PR_Close(writeFile);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsToolkitProfileService::GetProfileCount(uint32_t* aResult) {
   *aResult = 0;
-  nsToolkitProfile* profile = mFirst;
-  while (profile) {
+  for (nsToolkitProfile* profile : mProfiles) {
+    Unused << profile;
     (*aResult)++;
-    profile = profile->mNext;
   }
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsToolkitProfileService::Flush() {
   nsresult rv;
 
+  // If we aren't using dedicated profiles then nothing about the list of
+  // installs can have changed, so no need to update the backup.
   if (mUseDedicatedProfile) {
-    rv = mInstallData.WriteToFile(mInstallFile);
-    NS_ENSURE_SUCCESS(rv, rv);
+    // Export the installs to the backup.
+    nsTArray<nsCString> installs = GetKnownInstalls();
+
+    if (!installs.IsEmpty()) {
+      nsCString data;
+      nsCString buffer;
+
+      for (uint32_t i = 0; i < installs.Length(); i++) {
+        nsTArray<UniquePtr<KeyValue>> strings =
+            GetSectionStrings(&mProfileDB, installs[i].get());
+        if (strings.IsEmpty()) {
+          continue;
+        }
+
+        // Strip "Install" from the start.
+        const nsDependentCSubstring& install =
+            Substring(installs[i], INSTALL_PREFIX_LENGTH);
+        data.AppendPrintf("[%s]\n", PromiseFlatCString(install).get());
+
+        for (uint32_t j = 0; j < strings.Length(); j++) {
+          data.AppendPrintf("%s=%s\n", strings[j]->key.get(),
+                            strings[j]->value.get());
+        }
+
+        data.Append("\n");
+      }
+
+      FILE* writeFile;
+      rv = mInstallDBFile->OpenANSIFileDesc("w", &writeFile);
+      NS_ENSURE_SUCCESS(rv, rv);
+
+      uint32_t length = data.Length();
+      if (fwrite(data.get(), sizeof(char), length, writeFile) != length) {
+        fclose(writeFile);
+        return NS_ERROR_UNEXPECTED;
+      }
+
+      fclose(writeFile);
+    } else {
+      rv = mInstallDBFile->Remove(false);
+      if (NS_FAILED(rv) && rv != NS_ERROR_FILE_TARGET_DOES_NOT_EXIST &&
+          rv != NS_ERROR_FILE_NOT_FOUND) {
+        return rv;
+      }
+    }
   }
 
-  // Errors during writing might cause unhappy semi-written files.
-  // To avoid this, write the entire thing to a buffer, then write
-  // that buffer to disk.
-
-  uint32_t pCount = 0;
-  nsToolkitProfile* cur;
-
-  for (cur = mFirst; cur != nullptr; cur = cur->mNext) ++pCount;
-
-  uint32_t length;
-  const int bufsize = 100 + MAXPATHLEN * pCount;
-  auto buffer = MakeUnique<char[]>(bufsize);
-
-  char* pos = buffer.get();
-  char* end = pos + bufsize;
-
-  pos += snprintf(pos, end - pos,
-                  "[General]\n"
-                  "StartWithLastProfile=%s\n\n",
-                  mStartWithLast ? "1" : "0");
-
-  nsAutoCString path;
-  cur = mFirst;
-  pCount = 0;
-
-  while (cur) {
-    bool isRelative;
-    nsresult rv = GetProfileDescriptor(cur, path, &isRelative);
-    NS_ENSURE_SUCCESS(rv, rv);
-
-    pos +=
-        snprintf(pos, end - pos,
-                 "[Profile%u]\n"
-                 "Name=%s\n"
-                 "IsRelative=%s\n"
-                 "Path=%s\n",
-                 pCount, cur->mName.get(), isRelative ? "1" : "0", path.get());
-
-    if (cur == mNormalDefault) {
-      pos += snprintf(pos, end - pos, "Default=1\n");
-    }
-
-    pos += snprintf(pos, end - pos, "\n");
-
-    cur = cur->mNext;
-    ++pCount;
-  }
-
-  FILE* writeFile;
-  rv = mListFile->OpenANSIFileDesc("w", &writeFile);
+  rv = mProfileDB.WriteToFile(mProfileDBFile);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  length = pos - buffer.get();
-
-  if (fwrite(buffer.get(), sizeof(char), length, writeFile) != length) {
-    fclose(writeFile);
-    return NS_ERROR_UNEXPECTED;
-  }
-
-  fclose(writeFile);
   return NS_OK;
 }
 
 NS_IMPL_ISUPPORTS(nsToolkitProfileFactory, nsIFactory)
 
 NS_IMETHODIMP
 nsToolkitProfileFactory::CreateInstance(nsISupports* aOuter, const nsID& aIID,
                                         void** aResult) {
--- a/toolkit/profile/nsToolkitProfileService.h
+++ b/toolkit/profile/nsToolkitProfileService.h
@@ -10,39 +10,41 @@
 
 #include "nsIToolkitProfileService.h"
 #include "nsIToolkitProfile.h"
 #include "nsIFactory.h"
 #include "nsSimpleEnumerator.h"
 #include "nsProfileLock.h"
 #include "nsINIParser.h"
 
-class nsToolkitProfile final : public nsIToolkitProfile {
+class nsToolkitProfile final
+    : public nsIToolkitProfile,
+      public mozilla::LinkedListElement<RefPtr<nsToolkitProfile>> {
  public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSITOOLKITPROFILE
 
   friend class nsToolkitProfileService;
-  RefPtr<nsToolkitProfile> mNext;
-  nsToolkitProfile* mPrev;
 
  private:
   ~nsToolkitProfile() = default;
 
   nsToolkitProfile(const nsACString& aName, nsIFile* aRootDir,
-                   nsIFile* aLocalDir, nsToolkitProfile* aPrev);
+                   nsIFile* aLocalDir, bool aFromDB);
 
   nsresult RemoveInternal(bool aRemoveFiles, bool aInBackground);
 
   friend class nsToolkitProfileLock;
 
   nsCString mName;
   nsCOMPtr<nsIFile> mRootDir;
   nsCOMPtr<nsIFile> mLocalDir;
   nsIProfileLock* mLock;
+  uint32_t mIndex;
+  nsCString mSection;
 };
 
 class nsToolkitProfileLock final : public nsIProfileLock {
  public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIPROFILELOCK
 
   nsresult Init(nsToolkitProfile* aProfile, nsIProfileUnlocker** aUnlocker);
@@ -98,46 +100,47 @@ class nsToolkitProfileService final : pu
 
   nsresult GetProfileDescriptor(nsIToolkitProfile* aProfile,
                                 nsACString& aDescriptor, bool* aIsRelative);
   bool IsProfileForCurrentInstall(nsIToolkitProfile* aProfile);
   void ClearProfileFromOtherInstalls(nsIToolkitProfile* aProfile);
   bool MaybeMakeDefaultDedicatedProfile(nsIToolkitProfile* aProfile);
   bool IsSnapEnvironment();
   nsresult CreateDefaultProfile(nsIToolkitProfile** aResult);
+  void SetNormalDefault(nsIToolkitProfile* aProfile);
 
   // Returns the known install hashes from the installs database. Modifying the
   // installs database is safe while iterating the returned array.
   nsTArray<nsCString> GetKnownInstalls();
 
   // Tracks whether SelectStartupProfile has been called.
   bool mStartupProfileSelected;
-  // The first profile in a linked list of profiles loaded from profiles.ini.
-  RefPtr<nsToolkitProfile> mFirst;
+  // The  profiles loaded from profiles.ini.
+  mozilla::LinkedList<RefPtr<nsToolkitProfile>> mProfiles;
   // The profile selected for use at startup, if it exists in profiles.ini.
   nsCOMPtr<nsIToolkitProfile> mCurrent;
   // The profile selected for this install in installs.ini.
   nsCOMPtr<nsIToolkitProfile> mDedicatedProfile;
   // The default profile used by non-dev-edition builds.
   nsCOMPtr<nsIToolkitProfile> mNormalDefault;
   // The profile used if mUseDevEditionProfile is true (the default on
   // dev-edition builds).
   nsCOMPtr<nsIToolkitProfile> mDevEditionDefault;
   // The directory that holds profiles.ini and profile directories.
   nsCOMPtr<nsIFile> mAppData;
   // The directory that holds the cache files for profiles.
   nsCOMPtr<nsIFile> mTempData;
   // The location of profiles.ini.
-  nsCOMPtr<nsIFile> mListFile;
+  nsCOMPtr<nsIFile> mProfileDBFile;
   // The location of installs.ini.
-  nsCOMPtr<nsIFile> mInstallFile;
-  // The data loaded from installs.ini.
-  nsINIParser mInstallData;
-  // The install hash for the currently running install.
-  nsCString mInstallHash;
+  nsCOMPtr<nsIFile> mInstallDBFile;
+  // The data loaded from profiles.ini.
+  nsINIParser mProfileDB;
+  // The section in the profiles db for the current install.
+  nsCString mInstallSection;
   // Whether to start with the selected profile by default.
   bool mStartWithLast;
   // True if during startup it appeared that this is the first run.
   bool mIsFirstRun;
   // True if the default profile is the separate dev-edition-profile.
   bool mUseDevEditionProfile;
   // True if this install should use a dedicated default profile.
   const bool mUseDedicatedProfile;
--- a/toolkit/profile/xpcshell/head.js
+++ b/toolkit/profile/xpcshell/head.js
@@ -164,17 +164,17 @@ function writeCompatibilityIni(dir, appD
 function writeProfilesIni(profileData) {
   let target = gDataHome.clone();
   target.append("profiles.ini");
 
   let factory = Cc["@mozilla.org/xpcom/ini-parser-factory;1"].
                 getService(Ci.nsIINIParserFactory);
   let ini = factory.createINIParser().QueryInterface(Ci.nsIINIParserWriter);
 
-  const { options = {}, profiles = [] } = profileData;
+  const { options = {}, profiles = [], installs = null } = profileData;
 
   let { startWithLastProfile = true } = options;
   ini.setString("General", "StartWithLastProfile", startWithLastProfile ? "1" : "0");
 
   for (let i = 0; i < profiles.length; i++) {
     let profile = profiles[i];
     let section = `Profile${i}`;
 
@@ -182,16 +182,31 @@ function writeProfilesIni(profileData) {
     ini.setString(section, "IsRelative", 1);
     ini.setString(section, "Path", profile.path);
 
     if (profile.default) {
       ini.setString(section, "Default", "1");
     }
   }
 
+  if (installs) {
+    ini.setString("General", "Version", "2");
+
+    for (let hash of Object.keys(installs)) {
+      ini.setString(`Install${hash}`, "Default", installs[hash].default);
+      if ("locked" in installs[hash]) {
+        ini.setString(`Install${hash}`, "Locked", installs[hash].locked ? "1" : "0");
+      }
+    }
+
+    writeInstallsIni({ installs });
+  } else {
+    writeInstallsIni(null);
+  }
+
   ini.writeFile(target);
 }
 
 /**
  * Reads the existing profiles.ini into the same structure as that accepted by
  * writeProfilesIni above. The profiles property is sorted according to name
  * because the order is irrelevant and it makes testing easier if we can make
  * that assumption.
@@ -200,66 +215,98 @@ function readProfilesIni() {
   let target = gDataHome.clone();
   target.append("profiles.ini");
 
   let profileData = {
     options: {
       startWithLastProfile: true,
     },
     profiles: [],
+    installs: null,
   };
 
   if (!target.exists()) {
     return profileData;
   }
 
   let factory = Cc["@mozilla.org/xpcom/ini-parser-factory;1"].
                 getService(Ci.nsIINIParserFactory);
   let ini = factory.createINIParser(target);
 
   profileData.options.startWithLastProfile = safeGet(ini, "General", "StartWithLastProfile") == "1";
-
-  for (let i = 0; true; i++) {
-    let section = `Profile${i}`;
-
-    let isRelative = safeGet(ini, section, "IsRelative");
-    if (isRelative === null) {
-      break;
-    }
-    Assert.equal(isRelative, "1", "Paths should always be relative in these tests.");
+  if (safeGet(ini, "General", "Version") == "2") {
+    profileData.installs = {};
+  }
 
-    let profile = {
-      name: safeGet(ini, section, "Name"),
-      path: safeGet(ini, section, "Path"),
-    };
+  let sections = ini.getSections();
+  while (sections.hasMore()) {
+    let section = sections.getNext();
 
-    try {
-      profile.default = ini.getString(section, "Default") == "1";
-      Assert.ok(profile.default, "The Default value is only written when true.");
-    } catch (e) {
-      profile.default = false;
+    if (section == "General") {
+      continue;
     }
 
-    profileData.profiles.push(profile);
+    if (section.startsWith("Profile")) {
+      let isRelative = safeGet(ini, section, "IsRelative");
+      if (isRelative === null) {
+        break;
+      }
+      Assert.equal(isRelative, "1", "Paths should always be relative in these tests.");
+
+      let profile = {
+        name: safeGet(ini, section, "Name"),
+        path: safeGet(ini, section, "Path"),
+      };
+
+      try {
+        profile.default = ini.getString(section, "Default") == "1";
+        Assert.ok(profile.default, "The Default value is only written when true.");
+      } catch (e) {
+        profile.default = false;
+      }
+
+      profileData.profiles.push(profile);
+    }
+
+    if (section.startsWith("Install")) {
+      Assert.ok(profileData.installs, "Should only see an install section if the ini version was correct.");
+
+      profileData.installs[section.substring(7)] = {
+        default: safeGet(ini, section, "Default"),
+      };
+
+      let locked = safeGet(ini, section, "Locked");
+      if (locked !== null) {
+        profileData.installs[section.substring(7)].locked = locked;
+      }
+    }
   }
 
   profileData.profiles.sort((a, b) => a.name.localeCompare(b.name));
 
   return profileData;
 }
 
 /**
  * Writes an installs.ini based on the supplied data. Should be an object with
  * keys for every installation hash each mapping to an object. Each object
  * should have a default property for the relative path to the profile.
  */
 function writeInstallsIni(installData) {
   let target = gDataHome.clone();
   target.append("installs.ini");
 
+  if (!installData) {
+    try {
+      target.remove(false);
+    } catch (e) {
+    }
+    return;
+  }
+
   const { installs = {} } = installData;
 
   let factory = Cc["@mozilla.org/xpcom/ini-parser-factory;1"].
                 getService(Ci.nsIINIParserFactory);
   let ini = factory.createINIParser(null).QueryInterface(Ci.nsIINIParserWriter);
 
   for (let hash of Object.keys(installs)) {
     ini.setString(hash, "Default", installs[hash].default);
@@ -278,54 +325,74 @@ function readInstallsIni() {
   let target = gDataHome.clone();
   target.append("installs.ini");
 
   let installData = {
     installs: {},
   };
 
   if (!target.exists()) {
+    dump("Missing installs.ini\n");
     return installData;
   }
 
   let factory = Cc["@mozilla.org/xpcom/ini-parser-factory;1"].
                 getService(Ci.nsIINIParserFactory);
   let ini = factory.createINIParser(target);
 
   let sections = ini.getSections();
   while (sections.hasMore()) {
     let hash = sections.getNext();
     if (hash != "General") {
       installData.installs[hash] = {
         default: safeGet(ini, hash, "Default"),
-        locked: safeGet(ini, hash, "Locked") == 1,
       };
+
+      let locked = safeGet(ini, hash, "Locked");
+      if (locked !== null) {
+        installData.installs[hash].locked = locked;
+      }
     }
   }
 
   return installData;
 }
 
 /**
+ * Check that the backup data in installs.ini matches the install data in
+ * profiles.ini.
+ */
+function checkBackup(profileData = readProfilesIni(), installData = readInstallsIni()) {
+  if (!profileData.installs) {
+    // If the profiles db isn't of the right version we wouldn't expect the
+    // backup to be accurate.
+    return;
+  }
+
+  Assert.deepEqual(profileData.installs, installData.installs, "Backup installs.ini should match installs in profiles.ini");
+}
+
+/**
  * Checks that the profile service seems to have the right data in it compared
  * to profile and install data structured as in the above functions.
  */
-function checkProfileService(profileData = readProfilesIni(), installData = readInstallsIni()) {
+function checkProfileService(profileData = readProfilesIni(), verifyBackup = true) {
   let service = getProfileService();
 
   let serviceProfiles = Array.from(service.profiles);
 
   Assert.equal(serviceProfiles.length, profileData.profiles.length, "Should be the same number of profiles.");
 
   // Sort to make matching easy.
   serviceProfiles.sort((a, b) => a.name.localeCompare(b.name));
   profileData.profiles.sort((a, b) => a.name.localeCompare(b.name));
 
   let hash = xreDirProvider.getInstallHash();
-  let defaultPath = hash in installData.installs ? installData.installs[hash].default : null;
+  let defaultPath = (profileData.installs && hash in profileData.installs) ?
+                    profileData.installs[hash].default : null;
   let dedicatedProfile = null;
   let snapProfile = null;
 
   for (let i = 0; i < serviceProfiles.length; i++) {
     let serviceProfile = serviceProfiles[i];
     let expectedProfile = profileData.profiles[i];
 
     Assert.equal(serviceProfile.name, expectedProfile.name, "Should have the same name.");
@@ -347,16 +414,20 @@ function checkProfileService(profileData
     }
   }
 
   if (gIsSnap) {
     Assert.equal(service.defaultProfile, snapProfile, "Should have seen the right profile selected.");
   } else {
     Assert.equal(service.defaultProfile, dedicatedProfile, "Should have seen the right profile selected.");
   }
+
+  if (verifyBackup) {
+    checkBackup(profileData);
+  }
 }
 
 /**
  * Asynchronously reads an nsIFile from disk.
  */
 async function readFile(file) {
   let decoder = new TextDecoder();
   let data = await OS.File.read(file.path);
new file mode 100644
--- /dev/null
+++ b/toolkit/profile/xpcshell/test_check_backup.js
@@ -0,0 +1,41 @@
+/*
+ * Tests that when the profiles DB is missing the install data we reload it.
+ */
+
+add_task(async () => {
+  let hash = xreDirProvider.getInstallHash();
+
+  let profileData = {
+    options: {
+      startWithLastProfile: true,
+    },
+    profiles: [{
+      name: "Profile1",
+      path: "Path1",
+    }, {
+      name: "Profile2",
+      path: "Path2",
+    }],
+  };
+
+  let installs = {
+    [hash]: {
+      default: "Path2",
+    },
+  };
+
+  writeProfilesIni(profileData);
+  writeInstallsIni({ installs });
+
+  let { profile, didCreate } = selectStartupProfile();
+  checkStartupReason("default");
+
+  let service = getProfileService();
+  // Should have added the backup data to the service, check that is true.
+  profileData.installs = installs;
+  checkProfileService(profileData);
+
+  Assert.ok(!didCreate, "Should not have created a new profile.");
+  Assert.equal(profile.name, "Profile2", "Should have selected the right profile");
+  Assert.ok(!service.createdAlternateProfile, "Should not have created an alternate profile.");
+});
--- a/toolkit/profile/xpcshell/test_claim_locked.js
+++ b/toolkit/profile/xpcshell/test_claim_locked.js
@@ -9,38 +9,34 @@ add_task(async () => {
   writeCompatibilityIni(defaultProfile);
 
   writeProfilesIni({
     profiles: [{
       name: PROFILE_DEFAULT,
       path: defaultProfile.leafName,
       default: true,
     }],
-  });
-
-  let hash = xreDirProvider.getInstallHash();
-  writeProfilesIni({
     installs: {
       other: {
         default: defaultProfile.leafName,
         locked: true,
       },
     },
   });
 
   let { profile: selectedProfile, didCreate } = selectStartupProfile();
 
   let profileData = readProfilesIni();
-  let installData = readInstallsIni();
 
   Assert.ok(profileData.options.startWithLastProfile, "Should be set to start with the last profile.");
   Assert.equal(profileData.profiles.length, 2, "Should have the right number of profiles.");
 
-  Assert.equal(Object.keys(installData.installs).length, 1, "Should be two known installs.");
-  Assert.notEqual(installData.installs[hash].default, defaultProfile.leafName, "Should not have marked the original default profile as the default for this install.");
-  Assert.ok(installData.installs[hash].locked, "Should have locked as we created this profile for this install.");
+  let hash = xreDirProvider.getInstallHash();
+  Assert.equal(Object.keys(profileData.installs).length, 2, "Should be two known installs.");
+  Assert.notEqual(profileData.installs[hash].default, defaultProfile.leafName, "Should not have marked the original default profile as the default for this install.");
+  Assert.ok(profileData.installs[hash].locked, "Should have locked as we created this profile for this install.");
 
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 
   Assert.ok(didCreate, "Should have created a new profile.");
   Assert.ok(!selectedProfile.rootDir.equals(defaultProfile), "Should be using a different directory.");
   Assert.equal(selectedProfile.name, DEDICATED_NAME);
 });
--- a/toolkit/profile/xpcshell/test_clean.js
+++ b/toolkit/profile/xpcshell/test_clean.js
@@ -13,98 +13,93 @@ add_task(async () => {
   target.leafName = "installs.ini";
   Assert.ok(!target.exists(), "installs.ini should not exist yet.");
 
   // Create a new profile to use.
   let newProfile = service.createProfile(null, "dedicated");
   service.flush();
 
   let profileData = readProfilesIni();
-  let installData = readInstallsIni();
 
   Assert.ok(profileData.options.startWithLastProfile, "Should be set to start with the last profile.");
   Assert.equal(profileData.profiles.length, 1, "Should have the right number of profiles.");
 
   let profile = profileData.profiles[0];
   Assert.equal(profile.name, "dedicated", "Should have the right name.");
   Assert.ok(!profile.default, "Should not be marked as the old-style default.");
 
   // The new profile hasn't been marked as the default yet!
-  Assert.equal(Object.keys(installData.installs).length, 0, "Should be no defaults for installs yet.");
+  Assert.equal(Object.keys(profileData.installs).length, 0, "Should be no defaults for installs yet.");
 
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 
   service.defaultProfile = newProfile;
   service.flush();
 
   profileData = readProfilesIni();
-  installData = readInstallsIni();
 
   Assert.ok(profileData.options.startWithLastProfile, "Should be set to start with the last profile.");
   Assert.equal(profileData.profiles.length, 1, "Should have the right number of profiles.");
 
   profile = profileData.profiles[0];
   Assert.equal(profile.name, "dedicated", "Should have the right name.");
   Assert.ok(!profile.default, "Should not be marked as the old-style default.");
 
   let hash = xreDirProvider.getInstallHash();
-  Assert.equal(Object.keys(installData.installs).length, 1, "Should be only one known install.");
-  Assert.equal(installData.installs[hash].default, profileData.profiles[0].path, "Should have marked the new profile as the default for this install.");
+  Assert.equal(Object.keys(profileData.installs).length, 1, "Should be only one known install.");
+  Assert.equal(profileData.installs[hash].default, profileData.profiles[0].path, "Should have marked the new profile as the default for this install.");
 
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 
   let otherProfile = service.createProfile(null, "another");
   service.defaultProfile = otherProfile;
 
   service.flush();
 
   profileData = readProfilesIni();
-  installData = readInstallsIni();
 
   Assert.ok(profileData.options.startWithLastProfile, "Should be set to start with the last profile.");
   Assert.equal(profileData.profiles.length, 2, "Should have the right number of profiles.");
 
   profile = profileData.profiles[0];
   Assert.equal(profile.name, "another", "Should have the right name.");
   Assert.ok(!profile.default, "Should not be marked as the old-style default.");
 
   profile = profileData.profiles[1];
   Assert.equal(profile.name, "dedicated", "Should have the right name.");
   Assert.ok(!profile.default, "Should not be marked as the old-style default.");
 
-  Assert.equal(Object.keys(installData.installs).length, 1, "Should be only one known install.");
-  Assert.equal(installData.installs[hash].default, profileData.profiles[0].path, "Should have marked the new profile as the default for this install.");
+  Assert.equal(Object.keys(profileData.installs).length, 1, "Should be only one known install.");
+  Assert.equal(profileData.installs[hash].default, profileData.profiles[0].path, "Should have marked the new profile as the default for this install.");
 
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 
   newProfile.remove(true);
   service.flush();
 
   profileData = readProfilesIni();
-  installData = readInstallsIni();
 
   Assert.ok(profileData.options.startWithLastProfile, "Should be set to start with the last profile.");
   Assert.equal(profileData.profiles.length, 1, "Should have the right number of profiles.");
 
   profile = profileData.profiles[0];
   Assert.equal(profile.name, "another", "Should have the right name.");
   Assert.ok(!profile.default, "Should not be marked as the old-style default.");
 
-  Assert.equal(Object.keys(installData.installs).length, 1, "Should be only one known install.");
-  Assert.equal(installData.installs[hash].default, profileData.profiles[0].path, "Should have marked the new profile as the default for this install.");
+  Assert.equal(Object.keys(profileData.installs).length, 1, "Should be only one known install.");
+  Assert.equal(profileData.installs[hash].default, profileData.profiles[0].path, "Should have marked the new profile as the default for this install.");
 
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 
   otherProfile.remove(true);
   service.flush();
 
   profileData = readProfilesIni();
-  installData = readInstallsIni();
 
   Assert.ok(profileData.options.startWithLastProfile, "Should be set to start with the last profile.");
   Assert.equal(profileData.profiles.length, 0, "Should have the right number of profiles.");
 
   // We leave a reference to the missing profile to stop us trying to steal the
   // old-style default profile on next startup.
-  Assert.equal(Object.keys(installData.installs).length, 1, "Should be only one known install.");
+  Assert.equal(Object.keys(profileData.installs).length, 1, "Should be only one known install.");
 
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 });
--- a/toolkit/profile/xpcshell/test_create_default.js
+++ b/toolkit/profile/xpcshell/test_create_default.js
@@ -4,18 +4,17 @@
 
 add_task(async () => {
   let service = getProfileService();
   let { profile, didCreate } = selectStartupProfile();
 
   checkStartupReason("firstrun-created-default");
 
   let profileData = readProfilesIni();
-  let installData = readInstallsIni();
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 
   Assert.ok(didCreate, "Should have created a new profile.");
   Assert.equal(profile, service.defaultProfile, "Should now be the default profile.");
   Assert.equal(profile.name, DEDICATED_NAME, "Should have created a new profile with the right name.");
   Assert.ok(!service.createdAlternateProfile, "Should not have created an alternate profile.");
 
   Assert.ok(profileData.options.startWithLastProfile, "Should be set to start with the last profile.");
   Assert.equal(profileData.profiles.length, 2, "Should have the right number of profiles.");
@@ -24,10 +23,10 @@ add_task(async () => {
   Assert.equal(profile.name, "default", "Should have the right name.");
   Assert.ok(profile.default, "Should be marked as the old-style default.");
 
   profile = profileData.profiles[1];
   Assert.equal(profile.name, DEDICATED_NAME, "Should have the right name.");
   Assert.ok(!profile.default, "Should not be marked as the old-style default.");
 
   let hash = xreDirProvider.getInstallHash();
-  Assert.ok(installData.installs[hash].locked, "Should have locked the profile");
+  Assert.ok(profileData.installs[hash].locked, "Should have locked the profile");
 });
--- a/toolkit/profile/xpcshell/test_lock.js
+++ b/toolkit/profile/xpcshell/test_lock.js
@@ -16,28 +16,27 @@ add_task(async () => {
       default: true,
     }],
   });
 
   let { profile: selectedProfile, didCreate } = selectStartupProfile();
 
   let hash = xreDirProvider.getInstallHash();
   let profileData = readProfilesIni();
-  let installData = readInstallsIni();
 
   Assert.ok(profileData.options.startWithLastProfile, "Should be set to start with the last profile.");
   Assert.equal(profileData.profiles.length, 1, "Should have the right number of profiles.");
 
   let profile = profileData.profiles[0];
   Assert.equal(profile.name, PROFILE_DEFAULT, "Should have the right name.");
   Assert.equal(profile.path, defaultProfile.leafName, "Should be the original default profile.");
   Assert.ok(profile.default, "Should be marked as the old-style default.");
 
-  Assert.equal(Object.keys(installData.installs).length, 1, "Should be only one known install.");
-  Assert.equal(installData.installs[hash].default, defaultProfile.leafName, "Should have marked the original default profile as the default for this install.");
-  Assert.ok(installData.installs[hash].locked, "Should have locked as we're the default app.");
+  Assert.equal(Object.keys(profileData.installs).length, 1, "Should be only one known install.");
+  Assert.equal(profileData.installs[hash].default, defaultProfile.leafName, "Should have marked the original default profile as the default for this install.");
+  Assert.ok(profileData.installs[hash].locked, "Should have locked as we're the default app.");
 
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 
   Assert.ok(!didCreate, "Should not have created a new profile.");
   Assert.ok(selectedProfile.rootDir.equals(defaultProfile), "Should be using the right directory.");
   Assert.equal(selectedProfile.name, PROFILE_DEFAULT);
 });
new file mode 100644
--- /dev/null
+++ b/toolkit/profile/xpcshell/test_missing_profilesini.js
@@ -0,0 +1,37 @@
+/**
+ * When profiles.ini is missing there isn't any point in restoring from any
+ * installs.ini, the profiles it refers to are gone anyway.
+ */
+
+add_task(async () => {
+  let hash = xreDirProvider.getInstallHash();
+
+  let installs = {
+    [hash]: {
+      default: "Path2",
+    },
+    otherhash: {
+      default: "foo",
+    },
+    anotherhash: {
+      default: "bar",
+    },
+  };
+
+  writeInstallsIni({ installs });
+
+  let { profile, didCreate } = selectStartupProfile();
+  checkStartupReason("firstrun-created-default");
+
+  let service = getProfileService();
+  Assert.ok(didCreate, "Should have created a new profile.");
+  Assert.equal(profile.name, DEDICATED_NAME, "Should have created the right profile");
+  Assert.ok(!service.createdAlternateProfile, "Should not have created an alternate profile.");
+
+  let profilesData = readProfilesIni();
+  Assert.equal(Object.keys(profilesData.installs).length, 1, "Should be only one known install");
+  Assert.ok(hash in profilesData.installs, "Should be the expected install.");
+  Assert.notEqual(profilesData.installs[hash].default, "Path2", "Didn't import the previous data.");
+
+  checkProfileService(profilesData);
+});
--- a/toolkit/profile/xpcshell/test_new_default.js
+++ b/toolkit/profile/xpcshell/test_new_default.js
@@ -26,17 +26,16 @@ add_task(async () => {
   });
 
   let service = getProfileService();
   let { profile: selectedProfile, didCreate } = selectStartupProfile();
   checkStartupReason("firstrun-claimed-default");
 
   let hash = xreDirProvider.getInstallHash();
   let profileData = readProfilesIni();
-  let installData = readInstallsIni();
 
   Assert.ok(profileData.options.startWithLastProfile, "Should be set to start with the last profile.");
   Assert.equal(profileData.profiles.length, 3, "Should have the right number of profiles.");
 
   let profile = profileData.profiles[0];
   Assert.equal(profile.name, "default", "Should have the right name.");
   Assert.equal(profile.path, defaultProfile.leafName, "Should be the original non-default profile.");
   Assert.ok(!profile.default, "Should not be marked as the old-style default.");
@@ -46,26 +45,26 @@ add_task(async () => {
   Assert.equal(profile.path, devDefaultProfile.leafName, "Should be the original dev default profile.");
   Assert.ok(!profile.default, "Should not be marked as the old-style default.");
 
   profile = profileData.profiles[2];
   Assert.equal(profile.name, "mydefault", "Should have the right name.");
   Assert.equal(profile.path, mydefaultProfile.leafName, "Should be the original default profile.");
   Assert.ok(profile.default, "Should be marked as the old-style default.");
 
-  Assert.equal(Object.keys(installData.installs).length, 1, "Should be only one known install.");
+  Assert.equal(Object.keys(profileData.installs).length, 1, "Should be only one known install.");
   if (AppConstants.MOZ_DEV_EDITION) {
-    Assert.equal(installData.installs[hash].default, devDefaultProfile.leafName, "Should have marked the original dev default profile as the default for this install.");
+    Assert.equal(profileData.installs[hash].default, devDefaultProfile.leafName, "Should have marked the original dev default profile as the default for this install.");
   } else {
-    Assert.equal(installData.installs[hash].default, mydefaultProfile.leafName, "Should have marked the original default profile as the default for this install.");
+    Assert.equal(profileData.installs[hash].default, mydefaultProfile.leafName, "Should have marked the original default profile as the default for this install.");
   }
 
-  Assert.ok(!installData.installs[hash].locked, "Should not be locked as we're not the default app.");
+  Assert.ok(!profileData.installs[hash].locked, "Should not be locked as we're not the default app.");
 
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 
   Assert.ok(!didCreate, "Should not have created a new profile.");
   if (AppConstants.MOZ_DEV_EDITION) {
     Assert.ok(selectedProfile.rootDir.equals(devDefaultProfile), "Should be using the right directory.");
     Assert.equal(selectedProfile.name, "dev-edition-default");
   } else {
     Assert.ok(selectedProfile.rootDir.equals(mydefaultProfile), "Should be using the right directory.");
     Assert.equal(selectedProfile.name, "mydefault");
--- a/toolkit/profile/xpcshell/test_previous_dedicated.js
+++ b/toolkit/profile/xpcshell/test_previous_dedicated.js
@@ -11,38 +11,34 @@ add_task(async () => {
   writeCompatibilityIni(defaultProfile);
 
   writeProfilesIni({
     profiles: [{
       name: "default",
       path: defaultProfile.leafName,
       default: true,
     }],
-  });
-
-  writeInstallsIni({
     installs: {
       [hash]: {
         default: "foobar",
       },
     },
   });
 
   let service = getProfileService();
   testStartsProfileManager();
 
   let profileData = readProfilesIni();
-  let installData = readInstallsIni();
 
   Assert.ok(profileData.options.startWithLastProfile, "Should be set to start with the last profile.");
   Assert.equal(profileData.profiles.length, 1, "Should have the right number of profiles.");
   Assert.ok(!service.createdAlternateProfile, "Should not have created an alternate profile.");
 
   let profile = profileData.profiles[0];
   Assert.equal(profile.name, "default", "Should have the right name.");
   Assert.equal(profile.path, defaultProfile.leafName, "Should be the original default profile.");
   Assert.ok(profile.default, "Should be marked as the old-style default.");
 
   // We keep the data here so we don't steal on the next reboot...
-  Assert.equal(Object.keys(installData.installs).length, 1, "Still list the broken reference.");
+  Assert.equal(Object.keys(profileData.installs).length, 1, "Still list the broken reference.");
 
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 });
new file mode 100644
--- /dev/null
+++ b/toolkit/profile/xpcshell/test_remove.js
@@ -0,0 +1,90 @@
+/*
+ * Tests adding and removing functions correctly.
+ */
+
+function compareLists(service, knownProfiles) {
+  Assert.equal(service.profileCount, knownProfiles.length, "profileCount should be correct.");
+  let serviceProfiles = Array.from(service.profiles);
+  Assert.equal(serviceProfiles.length, knownProfiles.length, "Enumerator length should be correct.");
+
+  for (let i = 0; i < knownProfiles.length; i++) {
+    // Cannot use strictEqual here, it attempts to print out a string
+    // representation of the profile objects and on some platforms that recurses
+    // infinitely.
+    Assert.ok(serviceProfiles[i] === knownProfiles[i], `Should have the right profile in position ${i}.`);
+  }
+}
+
+function removeProfile(profiles, position) {
+  dump(`Removing profile in position ${position}.`);
+  Assert.greaterOrEqual(position, 0, "Should be removing a valid position.");
+  Assert.less(position, profiles.length, "Should be removing a valid position.");
+
+  let last = profiles.pop();
+
+  if (profiles.length == position) {
+    // We were asked to remove the last profile.
+    last.remove(false);
+    return;
+  }
+
+  profiles[position].remove(false);
+  profiles[position] = last;
+}
+
+add_task(async () => {
+  let service = getProfileService();
+  let profiles = [];
+  compareLists(service, profiles);
+
+  profiles.push(service.createProfile(null, "profile1"));
+  profiles.push(service.createProfile(null, "profile2"));
+  profiles.push(service.createProfile(null, "profile3"));
+  profiles.push(service.createProfile(null, "profile4"));
+  profiles.push(service.createProfile(null, "profile5"));
+  profiles.push(service.createProfile(null, "profile6"));
+  profiles.push(service.createProfile(null, "profile7"));
+  profiles.push(service.createProfile(null, "profile8"));
+  profiles.push(service.createProfile(null, "profile9"));
+  compareLists(service, profiles);
+
+  // Test removing the first profile.
+  removeProfile(profiles, 0);
+  compareLists(service, profiles);
+
+  // And the last profile.
+  removeProfile(profiles, profiles.length - 1);
+  compareLists(service, profiles);
+
+  // Last but one...
+  removeProfile(profiles, profiles.length - 2);
+  compareLists(service, profiles);
+
+  // Second one...
+  removeProfile(profiles, 1);
+  compareLists(service, profiles);
+
+  // Something in the middle.
+  removeProfile(profiles, 2);
+  compareLists(service, profiles);
+
+  let expectedNames = [
+    "profile9",
+    "profile7",
+    "profile5",
+    "profile4",
+  ];
+
+  let serviceProfiles = Array.from(service.profiles);
+  for (let i = 0; i < expectedNames.length; i++) {
+    Assert.equal(serviceProfiles[i].name, expectedNames[i]);
+  }
+
+  removeProfile(profiles, 0);
+  removeProfile(profiles, 0);
+  removeProfile(profiles, 0);
+  removeProfile(profiles, 0);
+
+  Assert.equal(Array.from(service.profiles).length, 0, "All profiles gone.");
+  Assert.equal(service.profileCount, 0, "All profiles gone.");
+});
--- a/toolkit/profile/xpcshell/test_remove_default.js
+++ b/toolkit/profile/xpcshell/test_remove_default.js
@@ -8,54 +8,51 @@ add_task(async () => {
   let defaultProfile = makeRandomProfileDir("default");
 
   let profilesIni = {
     profiles: [{
       name: "default",
       path: defaultProfile.leafName,
       default: true,
     }],
-  };
-  writeProfilesIni(profilesIni);
-
-  let installsIni = {
     installs: {
       [hash]: {
         default: defaultProfile.leafName,
       },
     },
   };
-  writeInstallsIni(installsIni);
+  writeProfilesIni(profilesIni);
 
   let service = getProfileService();
-  checkProfileService(profilesIni, installsIni);
+  checkProfileService(profilesIni);
 
   let { profile, didCreate } = selectStartupProfile();
   Assert.ok(!didCreate, "Should have not created a new profile.");
   Assert.equal(profile.name, "default", "Should have selected the default profile.");
   Assert.equal(profile, service.defaultProfile, "Should have selected the default profile.");
 
-  checkProfileService(profilesIni, installsIni);
+  checkProfileService(profilesIni);
 
   // In an actual run of Firefox we wouldn't be able to delete the profile in
   // use because it would be locked. But we don't actually lock the profile in
   // tests.
   profile.remove(false);
 
   Assert.ok(!service.defaultProfile, "Should no longer be a default profile.");
   Assert.equal(profile, service.currentProfile, "Should still be the profile in use.");
 
   // These are the modifications that should have been made.
   profilesIni.profiles.pop();
-  installsIni.installs[hash].default = "";
+  profilesIni.installs[hash].default = "";
 
-  checkProfileService(profilesIni, installsIni);
+  // The data isn't flushed to disk so don't check the backup here.
+  checkProfileService(profilesIni, false);
 
   service.flush();
 
   // And that should have flushed to disk correctly.
   checkProfileService();
 
   // checkProfileService doesn't differentiate between a blank default profile
   // for the install and a missing install.
-  let installs = readInstallsIni();
-  Assert.equal(installs.installs[hash].default, "", "Should be a blank default profile.");
+  profilesIni = readProfilesIni();
+  Assert.equal(profilesIni.installs[hash].default, "", "Should be a blank default profile.");
 });
--- a/toolkit/profile/xpcshell/test_select_default.js
+++ b/toolkit/profile/xpcshell/test_select_default.js
@@ -11,18 +11,16 @@ add_task(async () => {
     },
     profiles: [{
       name: "Profile1",
       path: "Path1",
     }, {
       name: "Profile3",
       path: "Path3",
     }],
-  };
-  let installData = {
     installs: {
       [hash]: {
         default: "Path2",
       },
     },
   };
 
   if (AppConstants.MOZ_DEV_EDITION) {
@@ -38,17 +36,16 @@ add_task(async () => {
     profileData.profiles.push({
       name: PROFILE_DEFAULT,
       path: "Path2",
       default: true,
     });
   }
 
   writeProfilesIni(profileData);
-  writeInstallsIni(installData);
 
   let { profile, didCreate } = selectStartupProfile();
   checkStartupReason("default");
 
   let service = getProfileService();
   checkProfileService(profileData);
 
   Assert.ok(!didCreate, "Should not have created a new profile.");
--- a/toolkit/profile/xpcshell/test_single_profile_selected.js
+++ b/toolkit/profile/xpcshell/test_single_profile_selected.js
@@ -17,30 +17,29 @@ add_task(async () => {
     }],
   });
 
   let { profile: selectedProfile, didCreate } = selectStartupProfile();
   checkStartupReason("firstrun-claimed-default");
 
   let hash = xreDirProvider.getInstallHash();
   let profileData = readProfilesIni();
-  let installData = readInstallsIni();
 
   Assert.ok(profileData.options.startWithLastProfile, "Should be set to start with the last profile.");
   Assert.equal(profileData.profiles.length, 1, "Should have the right number of profiles.");
 
   let profile = profileData.profiles[0];
   Assert.equal(profile.name, "default", "Should have the right name.");
   Assert.equal(profile.path, defaultProfile.leafName, "Should be the original default profile.");
   Assert.ok(profile.default, "Should be marked as the old-style default.");
 
-  Assert.equal(Object.keys(installData.installs).length, 1, "Should be only one known install.");
-  Assert.equal(installData.installs[hash].default, defaultProfile.leafName, "Should have marked the original default profile as the default for this install.");
-  Assert.ok(!installData.installs[hash].locked, "Should not have locked as we're not the default app.");
+  Assert.equal(Object.keys(profileData.installs).length, 1, "Should be only one known install.");
+  Assert.equal(profileData.installs[hash].default, defaultProfile.leafName, "Should have marked the original default profile as the default for this install.");
+  Assert.ok(!profileData.installs[hash].locked, "Should not have locked as we're not the default app.");
 
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 
   Assert.ok(!didCreate, "Should not have created a new profile.");
   let service = getProfileService();
   Assert.ok(!service.createdAlternateProfile, "Should not have created an alternate profile.");
   Assert.ok(selectedProfile.rootDir.equals(defaultProfile), "Should be using the right directory.");
   Assert.equal(selectedProfile.name, "default");
 });
--- a/toolkit/profile/xpcshell/test_single_profile_unselected.js
+++ b/toolkit/profile/xpcshell/test_single_profile_unselected.js
@@ -18,29 +18,28 @@ add_task(async () => {
       path: defaultProfile.leafName,
       default: false,
     }],
   });
 
   let service = getProfileService();
 
   let profileData = readProfilesIni();
-  let installData = readInstallsIni();
 
   Assert.ok(profileData.options.startWithLastProfile, "Should be set to start with the last profile.");
   Assert.equal(profileData.profiles.length, 1, "Should have the right number of profiles.");
 
   let profile = profileData.profiles[0];
   Assert.equal(profile.name, "default", "Should have the right name.");
   Assert.equal(profile.path, defaultProfile.leafName, "Should be the original default profile.");
   Assert.ok(!profile.default, "Should not be marked as the old-style default.");
 
-  Assert.equal(Object.keys(installData.installs).length, 0, "Should be no defaults for installs yet.");
+  Assert.ok(!profileData.installs, "Should be no defaults for installs yet.");
 
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 
   let { profile: selectedProfile, didCreate } = selectStartupProfile();
   checkStartupReason("firstrun-skipped-default");
   Assert.ok(didCreate, "Should have created a new profile.");
   Assert.ok(service.createdAlternateProfile, "Should have created an alternate profile.");
   Assert.ok(!selectedProfile.rootDir.equals(defaultProfile), "Should be using the right directory.");
   Assert.equal(selectedProfile.name, DEDICATED_NAME);
 
--- a/toolkit/profile/xpcshell/test_skip_locked_environment.js
+++ b/toolkit/profile/xpcshell/test_skip_locked_environment.js
@@ -21,31 +21,27 @@ add_task(async () => {
       default: true,
     }, {
       name: "Profile2",
       path: "Path2",
     }, {
       name: "Profile3",
       path: "Path3",
     }],
-  };
-
-  // Another install is using the profile and it is locked.
-  let installData = {
+    // Another install is using the profile and it is locked.
     installs: {
       otherinstall: {
         default: root.leafName,
         locked: true,
       },
     },
   };
 
   writeProfilesIni(profileData);
-  writeInstallsIni(installData);
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 
   let env = Cc["@mozilla.org/process/environment;1"].
             getService(Ci.nsIEnvironment);
   env.set("XRE_PROFILE_PATH", root.path);
   env.set("XRE_PROFILE_LOCAL_PATH", local.path);
 
   let { rootDir, localDir, profile, didCreate } = selectStartupProfile();
   checkStartupReason("restart-skipped-default");
@@ -68,15 +64,14 @@ add_task(async () => {
 
   Assert.equal(profileData.profiles[0].name, PROFILE_DEFAULT, "Should be the right profile.");
   Assert.ok(profileData.profiles[0].default, "Should be the old default profile.");
   Assert.equal(profileData.profiles[0].path, root.leafName, "Should be the correct path.");
   Assert.equal(profileData.profiles[1].name, expectedName, "Should be the right profile.");
   Assert.ok(!profileData.profiles[1].default, "Should not be the old default profile.");
 
   let hash = xreDirProvider.getInstallHash();
-  installData = readInstallsIni();
-  Assert.equal(Object.keys(installData.installs).length, 2, "Should be one known install.");
-  Assert.notEqual(installData.installs[hash].default, root.leafName, "Should have marked the original default profile as the default for this install.");
-  Assert.ok(installData.installs[hash].locked, "Should have locked as we created the profile for this install.");
-  Assert.equal(installData.installs.otherinstall.default, root.leafName, "Should have left the other profile as the default for the other install.");
-  Assert.ok(installData.installs[hash].locked, "Should still be locked to the other install.");
+  Assert.equal(Object.keys(profileData.installs).length, 2, "Should be one known install.");
+  Assert.notEqual(profileData.installs[hash].default, root.leafName, "Should have marked the original default profile as the default for this install.");
+  Assert.ok(profileData.installs[hash].locked, "Should have locked as we created the profile for this install.");
+  Assert.equal(profileData.installs.otherinstall.default, root.leafName, "Should have left the other profile as the default for the other install.");
+  Assert.ok(profileData.installs[hash].locked, "Should still be locked to the other install.");
 });
--- a/toolkit/profile/xpcshell/test_snatch_environment.js
+++ b/toolkit/profile/xpcshell/test_snatch_environment.js
@@ -21,30 +21,26 @@ add_task(async () => {
       default: true,
     }, {
       name: "Profile2",
       path: "Path2",
     }, {
       name: "Profile3",
       path: "Path3",
     }],
-  };
-
-  // Another install is using the profile but it isn't locked.
-  let installData = {
+    // Another install is using the profile but it isn't locked.
     installs: {
       otherinstall: {
         default: root.leafName,
       },
     },
   };
 
   writeProfilesIni(profileData);
-  writeInstallsIni(installData);
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 
   let env = Cc["@mozilla.org/process/environment;1"].
             getService(Ci.nsIEnvironment);
   env.set("XRE_PROFILE_PATH", root.path);
   env.set("XRE_PROFILE_LOCAL_PATH", local.path);
 
   let { rootDir, localDir, profile, didCreate } = selectStartupProfile();
   checkStartupReason("restart-claimed-default");
@@ -59,14 +55,13 @@ add_task(async () => {
   Assert.equal(service.defaultProfile, profile, "Should be the default profile.");
   Assert.equal(service.currentProfile, profile, "Should be the current profile.");
 
   profileData = readProfilesIni();
   Assert.equal(profileData.profiles[0].name, PROFILE_DEFAULT, "Should be the right profile.");
   Assert.ok(profileData.profiles[0].default, "Should still be the old default profile.");
 
   let hash = xreDirProvider.getInstallHash();
-  installData = readInstallsIni();
   // The info about the other install will have been removed so it goes through first run on next startup.
-  Assert.equal(Object.keys(installData.installs).length, 1, "Should be one known install.");
-  Assert.equal(installData.installs[hash].default, root.leafName, "Should have marked the original default profile as the default for this install.");
-  Assert.ok(!installData.installs[hash].locked, "Should not have locked as we're not the default app.");
+  Assert.equal(Object.keys(profileData.installs).length, 1, "Should be one known install.");
+  Assert.equal(profileData.installs[hash].default, root.leafName, "Should have marked the original default profile as the default for this install.");
+  Assert.ok(!profileData.installs[hash].locked, "Should not have locked as we're not the default app.");
 });
--- a/toolkit/profile/xpcshell/test_snatch_environment_default.js
+++ b/toolkit/profile/xpcshell/test_snatch_environment_default.js
@@ -23,30 +23,26 @@ add_task(async () => {
       default: true,
     }, {
       name: "Profile2",
       path: "Path2",
     }, {
       name: "Profile3",
       path: "Path3",
     }],
-  };
-
-  // Another install is using the profile but it isn't locked.
-  let installData = {
+    // Another install is using the profile but it isn't locked.
     installs: {
       otherinstall: {
         default: root.leafName,
       },
     },
   };
 
   writeProfilesIni(profileData);
-  writeInstallsIni(installData);
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 
   let env = Cc["@mozilla.org/process/environment;1"].
             getService(Ci.nsIEnvironment);
   env.set("XRE_PROFILE_PATH", root.path);
   env.set("XRE_PROFILE_LOCAL_PATH", local.path);
 
   let { rootDir, localDir, profile, didCreate } = selectStartupProfile();
   checkStartupReason("restart-claimed-default");
@@ -61,14 +57,13 @@ add_task(async () => {
   Assert.equal(service.defaultProfile, profile, "Should be the default profile.");
   Assert.equal(service.currentProfile, profile, "Should be the current profile.");
 
   profileData = readProfilesIni();
   Assert.equal(profileData.profiles[0].name, PROFILE_DEFAULT, "Should be the right profile.");
   Assert.ok(profileData.profiles[0].default, "Should still be the old default profile.");
 
   let hash = xreDirProvider.getInstallHash();
-  installData = readInstallsIni();
   // The info about the other install will have been removed so it goes through first run on next startup.
-  Assert.equal(Object.keys(installData.installs).length, 1, "Should be one known install.");
-  Assert.equal(installData.installs[hash].default, root.leafName, "Should have marked the original default profile as the default for this install.");
-  Assert.ok(installData.installs[hash].locked, "Should have locked as we're the default app.");
+  Assert.equal(Object.keys(profileData.installs).length, 1, "Should be one known install.");
+  Assert.equal(profileData.installs[hash].default, root.leafName, "Should have marked the original default profile as the default for this install.");
+  Assert.ok(profileData.installs[hash].locked, "Should have locked as we're the default app.");
 });
--- a/toolkit/profile/xpcshell/test_steal_inuse.js
+++ b/toolkit/profile/xpcshell/test_steal_inuse.js
@@ -9,44 +9,41 @@ add_task(async () => {
   writeCompatibilityIni(defaultProfile);
 
   writeProfilesIni({
     profiles: [{
       name: PROFILE_DEFAULT,
       path: defaultProfile.leafName,
       default: true,
     }],
-  });
-  writeInstallsIni({
     installs: {
       otherhash: {
         default: defaultProfile.leafName,
       },
     },
   });
 
   let service = getProfileService();
   let { profile: selectedProfile, didCreate } = selectStartupProfile();
   checkStartupReason("firstrun-claimed-default");
 
   let hash = xreDirProvider.getInstallHash();
   let profileData = readProfilesIni();
-  let installData = readInstallsIni();
 
   Assert.ok(profileData.options.startWithLastProfile, "Should be set to start with the last profile.");
   Assert.equal(profileData.profiles.length, 1, "Should have the right number of profiles.");
 
   let profile = profileData.profiles[0];
   Assert.equal(profile.name, PROFILE_DEFAULT, "Should have the right name.");
   Assert.equal(profile.path, defaultProfile.leafName, "Should be the original default profile.");
   Assert.ok(profile.default, "Should be marked as the old-style default.");
 
-  Assert.equal(Object.keys(installData.installs).length, 1, "Should only be one known installs.");
-  Assert.equal(installData.installs[hash].default, defaultProfile.leafName, "Should have taken the original default profile as the default for the current install.");
-  Assert.ok(!installData.installs[hash].locked, "Should not have locked as we're not the default app.");
+  Assert.equal(Object.keys(profileData.installs).length, 1, "Should only be one known installs.");
+  Assert.equal(profileData.installs[hash].default, defaultProfile.leafName, "Should have taken the original default profile as the default for the current install.");
+  Assert.ok(!profileData.installs[hash].locked, "Should not have locked as we're not the default app.");
 
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 
   Assert.ok(!didCreate, "Should not have created a new profile.");
   Assert.ok(!service.createdAlternateProfile, "Should not have created an alternate profile.");
   Assert.ok(selectedProfile.rootDir.equals(defaultProfile), "Should be using the right directory.");
   Assert.equal(selectedProfile.name, PROFILE_DEFAULT);
 });
--- a/toolkit/profile/xpcshell/test_update_selected_dedicated.js
+++ b/toolkit/profile/xpcshell/test_update_selected_dedicated.js
@@ -17,29 +17,28 @@ add_task(async () => {
   });
 
   let service = getProfileService();
   let { profile: selectedProfile, didCreate } = selectStartupProfile();
   checkStartupReason("firstrun-claimed-default");
 
   let hash = xreDirProvider.getInstallHash();
   let profileData = readProfilesIni();
-  let installData = readInstallsIni();
 
   Assert.ok(profileData.options.startWithLastProfile, "Should be set to start with the last profile.");
   Assert.equal(profileData.profiles.length, 1, "Should have the right number of profiles.");
 
   let profile = profileData.profiles[0];
   Assert.equal(profile.name, PROFILE_DEFAULT, "Should have the right name.");
   Assert.equal(profile.path, defaultProfile.leafName, "Should be the original default profile.");
   Assert.ok(profile.default, "Should be marked as the old-style default.");
 
-  Assert.equal(Object.keys(installData.installs).length, 1, "Should be only one known install.");
-  Assert.equal(installData.installs[hash].default, defaultProfile.leafName, "Should have marked the original default profile as the default for this install.");
-  Assert.ok(!installData.installs[hash].locked, "Should not have locked as we're not the default app.");
+  Assert.equal(Object.keys(profileData.installs).length, 1, "Should be only one known install.");
+  Assert.equal(profileData.installs[hash].default, defaultProfile.leafName, "Should have marked the original default profile as the default for this install.");
+  Assert.ok(!profileData.installs[hash].locked, "Should not have locked as we're not the default app.");
 
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 
   Assert.ok(!didCreate, "Should not have created a new profile.");
   Assert.ok(!service.createdAlternateProfile, "Should not have created an alternate profile.");
   Assert.ok(selectedProfile.rootDir.equals(defaultProfile), "Should be using the right directory.");
   Assert.equal(selectedProfile.name, PROFILE_DEFAULT);
 });
--- a/toolkit/profile/xpcshell/test_update_unknown_dedicated.js
+++ b/toolkit/profile/xpcshell/test_update_unknown_dedicated.js
@@ -16,17 +16,16 @@ add_task(async () => {
     }],
   });
 
   let service = getProfileService();
   let { profile: selectedProfile, didCreate } = selectStartupProfile();
   checkStartupReason("firstrun-created-default");
 
   let profileData = readProfilesIni();
-  let installData = readInstallsIni();
 
   Assert.ok(profileData.options.startWithLastProfile, "Should be set to start with the last profile.");
   Assert.equal(profileData.profiles.length, 2, "Should have the right number of profiles.");
 
   // Since there is already a profile with the desired name on dev-edition, a
   // unique version will be used.
   let expectedName = AppConstants.MOZ_DEV_EDITION ? `${DEDICATED_NAME}-1` : DEDICATED_NAME;
 
@@ -34,19 +33,19 @@ add_task(async () => {
   Assert.equal(profile.name, PROFILE_DEFAULT, "Should have the right name.");
   Assert.equal(profile.path, defaultProfile.leafName, "Should be the original default profile.");
   Assert.ok(profile.default, "Should be marked as the old-style default.");
   profile = profileData.profiles[1];
   Assert.equal(profile.name, expectedName, "Should have the right name.");
   Assert.notEqual(profile.path, defaultProfile.leafName, "Should not be the original default profile.");
   Assert.ok(!profile.default, "Should not be marked as the old-style default.");
 
-  Assert.equal(Object.keys(installData.installs).length, 1, "Should be a default for installs.");
-  Assert.equal(installData.installs[hash].default, profile.path, "Should have the right default profile.");
-  Assert.ok(installData.installs[hash].locked, "Should have locked as we created this profile for this install.");
+  Assert.equal(Object.keys(profileData.installs).length, 1, "Should be a default for installs.");
+  Assert.equal(profileData.installs[hash].default, profile.path, "Should have the right default profile.");
+  Assert.ok(profileData.installs[hash].locked, "Should have locked as we created this profile for this install.");
 
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 
   Assert.ok(didCreate, "Should have created a new profile.");
   Assert.ok(!service.createdAlternateProfile, "Should not have created an alternate profile.");
   Assert.ok(!selectedProfile.rootDir.equals(defaultProfile), "Should not be using the old directory.");
   Assert.equal(selectedProfile.name, expectedName);
 });
--- a/toolkit/profile/xpcshell/test_update_unselected_dedicated.js
+++ b/toolkit/profile/xpcshell/test_update_unselected_dedicated.js
@@ -20,17 +20,16 @@ add_task(async () => {
     }],
   });
 
   let service = getProfileService();
   let { profile: selectedProfile, didCreate } = selectStartupProfile();
   checkStartupReason("firstrun-skipped-default");
 
   let profileData = readProfilesIni();
-  let installData = readInstallsIni();
 
   Assert.ok(profileData.options.startWithLastProfile, "Should be set to start with the last profile.");
   Assert.equal(profileData.profiles.length, 2, "Should have the right number of profiles.");
 
   // Since there is already a profile with the desired name on dev-edition, a
   // unique version will be used.
   let expectedName = AppConstants.MOZ_DEV_EDITION ? `${DEDICATED_NAME}-1` : DEDICATED_NAME;
 
@@ -38,19 +37,19 @@ add_task(async () => {
   Assert.equal(profile.name, PROFILE_DEFAULT, "Should have the right name.");
   Assert.equal(profile.path, defaultProfile.leafName, "Should be the original default profile.");
   Assert.ok(profile.default, "Should be marked as the old-style default.");
   profile = profileData.profiles[1];
   Assert.equal(profile.name, expectedName, "Should have the right name.");
   Assert.notEqual(profile.path, defaultProfile.leafName, "Should not be the original default profile.");
   Assert.ok(!profile.default, "Should not be marked as the old-style default.");
 
-  Assert.equal(Object.keys(installData.installs).length, 1, "Should be a default for this install.");
-  Assert.equal(installData.installs[hash].default, profile.path, "Should have marked the new profile as the default for this install.");
-  Assert.ok(installData.installs[hash].locked, "Should have locked as we created this profile for this install.");
+  Assert.equal(Object.keys(profileData.installs).length, 1, "Should be a default for this install.");
+  Assert.equal(profileData.installs[hash].default, profile.path, "Should have marked the new profile as the default for this install.");
+  Assert.ok(profileData.installs[hash].locked, "Should have locked as we created this profile for this install.");
 
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 
   Assert.ok(didCreate, "Should have created a new profile.");
   Assert.ok(service.createdAlternateProfile, "Should have created an alternate profile.");
   Assert.ok(!selectedProfile.rootDir.equals(defaultProfile), "Should be using the right directory.");
   Assert.equal(selectedProfile.name, expectedName);
 });
--- a/toolkit/profile/xpcshell/test_use_dedicated.js
+++ b/toolkit/profile/xpcshell/test_use_dedicated.js
@@ -18,50 +18,46 @@ add_task(async () => {
       default: true,
     }, {
       name: "dedicated",
       path: dedicatedProfile.leafName,
     }, {
       name: "dev-edition-default",
       path: devProfile.leafName,
     }],
-  });
-
-  writeInstallsIni({
     installs: {
       [hash]: {
         default: dedicatedProfile.leafName,
       },
       "otherhash": {
         default: "foobar",
       },
     },
   });
 
   let { profile: selectedProfile, didCreate } = selectStartupProfile();
   checkStartupReason("default");
 
   let profileData = readProfilesIni();
-  let installData = readInstallsIni();
 
   Assert.ok(profileData.options.startWithLastProfile, "Should be set to start with the last profile.");
   Assert.equal(profileData.profiles.length, 3, "Should have the right number of profiles.");
 
   let profile = profileData.profiles[0];
   Assert.equal(profile.name, `dedicated`, "Should have the right name.");
   Assert.equal(profile.path, dedicatedProfile.leafName, "Should be the expected dedicated profile.");
   Assert.ok(!profile.default, "Should not be marked as the old-style default.");
 
   profile = profileData.profiles[1];
   Assert.equal(profile.name, "default", "Should have the right name.");
   Assert.equal(profile.path, defaultProfile.leafName, "Should be the original default profile.");
   Assert.ok(profile.default, "Should be marked as the old-style default.");
 
-  Assert.equal(Object.keys(installData.installs).length, 2, "Should be two known installs.");
-  Assert.equal(installData.installs[hash].default, dedicatedProfile.leafName, "Should have kept the default for this install.");
-  Assert.equal(installData.installs.otherhash.default, "foobar", "Should have kept the default for the other install.");
+  Assert.equal(Object.keys(profileData.installs).length, 2, "Should be two known installs.");
+  Assert.equal(profileData.installs[hash].default, dedicatedProfile.leafName, "Should have kept the default for this install.");
+  Assert.equal(profileData.installs.otherhash.default, "foobar", "Should have kept the default for the other install.");
 
-  checkProfileService(profileData, installData);
+  checkProfileService(profileData);
 
   Assert.ok(!didCreate, "Should not have created a new profile.");
   Assert.ok(selectedProfile.rootDir.equals(dedicatedProfile), "Should be using the right directory.");
   Assert.equal(selectedProfile.name, "dedicated");
 });
--- a/toolkit/profile/xpcshell/xpcshell.ini
+++ b/toolkit/profile/xpcshell/xpcshell.ini
@@ -27,8 +27,11 @@ skip-if = devedition
 [test_snap_empty.js]
 [test_remove_default.js]
 [test_claim_locked.js]
 [test_lock.js]
 [test_startswithlast.js]
 [test_snatch_environment.js]
 [test_skip_locked_environment.js]
 [test_snatch_environment_default.js]
+[test_check_backup.js]
+[test_missing_profilesini.js]
+[test_remove.js]
--- a/xpcom/base/nsINIParser.cpp
+++ b/xpcom/base/nsINIParser.cpp
@@ -265,16 +265,36 @@ nsresult nsINIParser::DeleteSection(cons
   }
 
   if (!mSections.Remove(aSection)) {
     return NS_ERROR_FAILURE;
   }
   return NS_OK;
 }
 
+nsresult nsINIParser::RenameSection(const char* aSection,
+                                    const char* aNewName) {
+  if (!IsValidSection(aSection) || !IsValidSection(aNewName)) {
+    return NS_ERROR_INVALID_ARG;
+  }
+
+  if (mSections.Get(aNewName, nullptr)) {
+    return NS_ERROR_ILLEGAL_VALUE;
+  }
+
+  nsAutoPtr<INIValue> val;
+  if (mSections.Remove(aSection, &val)) {
+    mSections.Put(aNewName, val.forget());
+  } else {
+    return NS_ERROR_FAILURE;
+  }
+
+  return NS_OK;
+}
+
 nsresult nsINIParser::WriteToFile(nsIFile* aFile) {
   nsCString buffer;
 
   for (auto iter = mSections.Iter(); !iter.Done(); iter.Next()) {
     buffer.AppendPrintf("[%s]\n", iter.Key());
     INIValue* val = iter.Data();
     while (val) {
       buffer.AppendPrintf("%s=%s\n", val->key, val->value);
--- a/xpcom/base/nsINIParser.h
+++ b/xpcom/base/nsINIParser.h
@@ -112,16 +112,27 @@ class nsINIParser {
    *
    * @param aSection      section name
    *
    * @throws NS_ERROR_FAILURE if the section did not exist.
    */
   nsresult DeleteSection(const char* aSection);
 
   /**
+   * Renames the specified section.
+   *
+   * @param aSection      section name
+   * @param aNewName      new section name
+   *
+   * @throws NS_ERROR_FAILURE if the section did not exist.
+   * @throws NS_ERROR_ILLEGAL_VALUE if the new section name already exists.
+   */
+  nsresult RenameSection(const char* aSection, const char* aNewName);
+
+  /**
    * Writes the ini data to disk.
    * @param aFile         the file to write to
    * @throws NS_ERROR_FAILURE on failure.
    */
   nsresult WriteToFile(nsIFile* aFile);
 
  private:
   struct INIValue {