Bug 789945: Part 3. Flush late in the shutdown, to give a last save a chance to complete and for the flush to not be blocking. r?bsmedberg" draft
authorMilan Sreckovic <milan@mozilla.com>
Wed, 14 Jun 2017 13:38:08 -0400
changeset 594218 0a8ee15fb58c14633da6de615349ef5a479faea7
parent 594217 163e747a9872ef4489defc39937b15dcb0203cef
child 594219 6ee547a0ddf1048c7ebcd3a67712c8c9c3f2c3ab
push id63952
push userbmo:milan@mozilla.com
push dateWed, 14 Jun 2017 17:40:02 +0000
reviewersbsmedberg
bugs789945
milestone56.0a1
Bug 789945: Part 3. Flush late in the shutdown, to give a last save a chance to complete and for the flush to not be blocking. r?bsmedberg" MozReview-Commit-ID: 7w5Y16C7mTn
modules/libpref/Preferences.cpp
--- a/modules/libpref/Preferences.cpp
+++ b/modules/libpref/Preferences.cpp
@@ -743,16 +743,17 @@ Preferences::Init()
                                   static_cast<nsISupports *>(static_cast<void *>(this)),
                                   "pref-config-startup");    
 
   nsCOMPtr<nsIObserverService> observerService =
     mozilla::services::GetObserverService();
   if (!observerService)
     return NS_ERROR_FAILURE;
 
+  observerService->AddObserver(this, "profile-before-change-telemetry", true);
   rv = observerService->AddObserver(this, "profile-before-change", true);
 
   observerService->AddObserver(this, "load-extension-defaults", true);
   observerService->AddObserver(this, "suspend_process_notification", true);
 
   return(rv);
 }
 
@@ -771,42 +772,52 @@ Preferences::Observe(nsISupports *aSubje
   if (MOZ_UNLIKELY(!XRE_IsParentProcess())) {
     return NS_ERROR_NOT_AVAILABLE;
   }
 
   nsresult rv = NS_OK;
 
   if (!nsCRT::strcmp(aTopic, "profile-before-change")) {
     rv = SavePrefFile(nullptr);
+  } else if (!nsCRT::strcmp(aTopic, "profile-before-change-telemetry")) {
+    if (AllowOffMainThreadSave()) {
+      PreferencesWriter::Flush();
+    }
   } else if (!strcmp(aTopic, "load-extension-defaults")) {
     pref_LoadPrefsInDirList(NS_EXT_PREFS_DEFAULTS_DIR_LIST);
   } else if (!nsCRT::strcmp(aTopic, "reload-default-prefs")) {
     // Reload the default prefs from file.
     pref_InitInitialObjects();
   } else if (!nsCRT::strcmp(aTopic, "suspend_process_notification")) {
     // Our process is being suspended. The OS may wake our process later,
     // or it may kill the process. In case our process is going to be killed
     // from the suspended state, we save preferences before suspending.
-    rv = SavePrefFile(nullptr);
+    rv = SavePrefFileBlocking();
   }
   return rv;
 }
 
 
 NS_IMETHODIMP
 Preferences::ReadUserPrefs(nsIFile *aFile)
 {
   if (MOZ_UNLIKELY(!XRE_IsParentProcess())) {
     NS_ERROR("must load prefs from parent process");
     return NS_ERROR_NOT_AVAILABLE;
   }
 
   nsresult rv;
 
   if (nullptr == aFile) {
+    // We should not be re-reading the user preferences, but if we
+    // are going to try, make sure there are no outstanding saves
+    if (AllowOffMainThreadSave()) {
+      PreferencesWriter::Flush();
+    }
+
     rv = UseDefaultPrefFile();
     // A user pref file is optional.
     // Ignore all errors related to it, so we retain 'rv' value :-|
     (void) UseUserPrefFile();
 
     // Migrate the old prerelease telemetry pref
     if (!Preferences::GetBool(kOldTelemetryPref, true)) {
       Preferences::SetBool(kTelemetryPref, false);
@@ -1118,16 +1129,23 @@ Preferences::MakeBackupPrefFile(nsIFile 
 
 nsresult
 Preferences::ReadAndOwnUserPrefFile(nsIFile *aFile)
 {
   NS_ENSURE_ARG(aFile);
   
   if (mCurrentFile == aFile)
     return NS_OK;
+
+  // Since we're changing the pref file, we may have to make
+  // sure the outstanding writes are handled first.
+  if (AllowOffMainThreadSave()) {
+    PreferencesWriter::Flush();
+  }
+
   mCurrentFile = aFile;
 
   nsresult rv = NS_OK;
   bool exists = false;
   mCurrentFile->Exists(&exists);
   if (exists) {
     rv = openPrefFile(mCurrentFile);
     if (NS_FAILED(rv)) {