Bug 1376558 - Should not migrate session for Firefox auto-refresh, r=MattN draft
authorFischer.json <fischer.json@gmail.com>
Tue, 27 Jun 2017 11:27:15 -0700
changeset 605907 d472b5a78fafa705085adbfc5f2c38d31a474821
parent 605055 20f32734df750bddada9d1edca665c2ea53946f0
child 636622 ad54b4d2b6a23029b009f55c2eb30558a7402be3
push id67547
push userbmo:fliu@mozilla.com
push dateMon, 10 Jul 2017 02:39:15 +0000
reviewersMattN
bugs1376558
milestone56.0a1
Bug 1376558 - Should not migrate session for Firefox auto-refresh, r=MattN This patch introduces an new environment variable called MOZ_RESET_PROFILE_MIGRATE_SESSION. Only when MOZ_RESET_PROFILE_MIGRATE_SESSION is set then Firefox profile migrator would migrate the old profile session data. MozReview-Commit-ID: 5XNBSPzx9AR
browser/components/migration/FirefoxProfileMigrator.js
toolkit/xre/nsAppRunner.cpp
--- a/browser/components/migration/FirefoxProfileMigrator.js
+++ b/browser/components/migration/FirefoxProfileMigrator.js
@@ -133,47 +133,55 @@ FirefoxProfileMigrator.prototype._getRes
   let passwords = getFileResource(types.PASSWORDS,
     ["signons.sqlite", "logins.json", "key3.db",
      "signedInUser.json"]);
   let formData = getFileResource(types.FORMDATA, ["formhistory.sqlite"]);
   let bookmarksBackups = getFileResource(types.OTHERDATA,
     [PlacesBackups.profileRelativeFolderPath]);
   let dictionary = getFileResource(types.OTHERDATA, ["persdict.dat"]);
 
-  let sessionCheckpoints = this._getFileObject(sourceProfileDir, "sessionCheckpoints.json");
-  let sessionFile = this._getFileObject(sourceProfileDir, "sessionstore.jsonlz4");
   let session;
-  if (sessionFile) {
-    session = {
-      type: types.SESSION,
-      migrate(aCallback) {
-        sessionCheckpoints.copyTo(currentProfileDir, "sessionCheckpoints.json");
-        let newSessionFile = currentProfileDir.clone();
-        newSessionFile.append("sessionstore.jsonlz4");
-        let migrationPromise = SessionMigration.migrate(sessionFile.path, newSessionFile.path);
-        migrationPromise.then(function() {
-          let buildID = Services.appinfo.platformBuildID;
-          let mstone = Services.appinfo.platformVersion;
-          // Force the browser to one-off resume the session that we give it:
-          Services.prefs.setBoolPref("browser.sessionstore.resume_session_once", true);
-          // Reset the homepage_override prefs so that the browser doesn't override our
-          // session with the "what's new" page:
-          Services.prefs.setCharPref("browser.startup.homepage_override.mstone", mstone);
-          Services.prefs.setCharPref("browser.startup.homepage_override.buildID", buildID);
-          // It's too early in startup for the pref service to have a profile directory,
-          // so we have to manually tell it where to save the prefs file.
-          let newPrefsFile = currentProfileDir.clone();
-          newPrefsFile.append("prefs.js");
-          Services.prefs.savePrefFile(newPrefsFile);
-          aCallback(true);
-        }, function() {
-          aCallback(false);
-        });
-      }
-    };
+  let env = Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment);
+  if (env.get("MOZ_RESET_PROFILE_MIGRATE_SESSION")) {
+    // We only want to restore the previous firefox session if the profile refresh was
+    // triggered by user. The MOZ_RESET_PROFILE_MIGRATE_SESSION would be set when a user-triggered
+    // profile refresh happened in nsAppRunner.cpp. Hence, we detect the MOZ_RESET_PROFILE_MIGRATE_SESSION
+    // to see if session data migration is required.
+    env.set("MOZ_RESET_PROFILE_MIGRATE_SESSION", "");
+    let sessionCheckpoints = this._getFileObject(sourceProfileDir, "sessionCheckpoints.json");
+    let sessionFile = this._getFileObject(sourceProfileDir, "sessionstore.jsonlz4");
+    if (sessionFile) {
+      session = {
+        type: types.SESSION,
+        migrate(aCallback) {
+          sessionCheckpoints.copyTo(currentProfileDir, "sessionCheckpoints.json");
+          let newSessionFile = currentProfileDir.clone();
+          newSessionFile.append("sessionstore.jsonlz4");
+          let migrationPromise = SessionMigration.migrate(sessionFile.path, newSessionFile.path);
+          migrationPromise.then(function() {
+            let buildID = Services.appinfo.platformBuildID;
+            let mstone = Services.appinfo.platformVersion;
+            // Force the browser to one-off resume the session that we give it:
+            Services.prefs.setBoolPref("browser.sessionstore.resume_session_once", true);
+            // Reset the homepage_override prefs so that the browser doesn't override our
+            // session with the "what's new" page:
+            Services.prefs.setCharPref("browser.startup.homepage_override.mstone", mstone);
+            Services.prefs.setCharPref("browser.startup.homepage_override.buildID", buildID);
+            // It's too early in startup for the pref service to have a profile directory,
+            // so we have to manually tell it where to save the prefs file.
+            let newPrefsFile = currentProfileDir.clone();
+            newPrefsFile.append("prefs.js");
+            Services.prefs.savePrefFile(newPrefsFile);
+            aCallback(true);
+          }, function() {
+            aCallback(false);
+          });
+        }
+      };
+    }
   }
 
   // Telemetry related migrations.
   let times = {
     name: "times", // name is used only by tests.
     type: types.OTHERDATA,
     migrate: aCallback => {
       let file = this._getFileObject(sourceProfileDir, "times.json");
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -2273,16 +2273,22 @@ SelectProfile(nsIProfileLock* *aResult, 
 
   if (ar || EnvHasValue("XRE_START_OFFLINE"))
     *aStartOffline = true;
 
   if (EnvHasValue("MOZ_RESET_PROFILE_RESTART")) {
     gDoProfileReset = true;
     gDoMigration = true;
     SaveToEnv("MOZ_RESET_PROFILE_RESTART=");
+    // We only want to restore the previous session if the profile refresh was
+    // triggered by user. And if it was a user-triggered profile refresh
+    // through, say, the safeMode dialog or the troubleshooting page, the MOZ_RESET_PROFILE_RESTART
+    // env variable would be set. Hence we set MOZ_RESET_PROFILE_MIGRATE_SESSION here so that
+    // Firefox profile migrator would migrate old session data later.
+    SaveToEnv("MOZ_RESET_PROFILE_MIGRATE_SESSION=1");
   }
 
   // reset-profile and migration args need to be checked before any profiles are chosen below.
   ar = CheckArg("reset-profile", true);
   if (ar == ARG_BAD) {
     PR_fprintf(PR_STDERR, "Error: argument --reset-profile is invalid when argument --osint is specified\n");
     return NS_ERROR_FAILURE;
   }