Bug 1266339 - Part 2 - Delete outdated sessionstore.bak files when restoring tabs automatically. r=margaret
authorJan Henning <jh+bugzilla@buttercookie.de>
Thu, 21 Apr 2016 20:43:47 +0200
changeset 297917 1633fe6e9c8159ef9ba25ab9192902a2d8d214ae
parent 297916 582c4b721a4f67b8b93f793ef35349e65db80382
child 297918 b4173da43e6a3e10ddd2d5d0809dcffc61cf0e6f
push id76943
push userryanvm@gmail.com
push dateWed, 18 May 2016 16:13:55 +0000
treeherdermozilla-inbound@c4449eab07d3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret
bugs1266339
milestone49.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1266339 - Part 2 - Delete outdated sessionstore.bak files when restoring tabs automatically. r=margaret sessionstore.bak, which powers the "tabs from last time" display, is only updated with the data of the previous session if the user chooses to "never restore" tabs, or if we've temporarily disabled session restoring because of too many successive crashes in a row. In all other cases, we restore tabs automatically so sessionstore.bak is never updated with fresh data, meaning its contents get stale pretty soon. With this patch, we clean up old copies of sessionstore.bak when doing an automatic restore, so they don't linger indefinitely in the "tabs from last time" section of the recent tabs panel. MozReview-Commit-ID: DrOx5TNwYMv
mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
mobile/android/base/java/org/mozilla/gecko/GeckoProfile.java
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
@@ -1531,21 +1531,18 @@ public abstract class GeckoApp
                 loadStartupTab(Tabs.LOADURL_NEW_TAB);
             }
 
             Tabs.getInstance().notifyListeners(null, Tabs.TabEvents.RESTORED);
 
             processTabQueue();
         }
 
-        // If we're not restoring, move the session file so it can be read for
-        // the last tabs section.
-        if (!mShouldRestore) {
-            getProfile().moveSessionFile();
-        }
+        // Make sure sessionstore.bak is either updated or deleted as necessary.
+        getProfile().updateSessionFile(mShouldRestore);
 
         recordStartupActionTelemetry(passedUri, action);
 
         // Check if launched from data reporting notification.
         if (ACTION_LAUNCH_SETTINGS.equals(action)) {
             Intent settingsIntent = new Intent(GeckoApp.this, GeckoPreferences.class);
             // Copy extras.
             settingsIntent.putExtras(intent.getUnsafe());
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoProfile.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoProfile.java
@@ -71,16 +71,17 @@ public final class GeckoProfile {
     public static final String DEFAULT_PROFILE = "default";
     // Profile is using a custom directory outside of the Mozilla directory.
     public static final String CUSTOM_PROFILE = "";
     public static final String GUEST_PROFILE = "guest";
 
     // Session store
     private static final String SESSION_FILE = "sessionstore.js";
     private static final String SESSION_FILE_BACKUP = "sessionstore.bak";
+    private static final long MAX_BACKUP_FILE_AGE = 1000 * 3600 * 24; // 24 hours
 
     private static final HashMap<String, GeckoProfile> sProfileCache = new HashMap<String, GeckoProfile>();
     private static String sDefaultProfileName;
 
     // Caches the guest profile dir.
     private static File sGuestDir;
     private static GeckoProfile sGuestProfile;
     private static boolean sShouldCheckForGuestProfile = true;
@@ -756,30 +757,39 @@ public final class GeckoProfile {
             // Don't log to avoid leaking data in JSONObject.
             throw new IOException("Unable to persist profile creation date to times file");
         }
         Log.d(LOGTAG, "Attempting to write new profile creation date");
         writeFile(TIMES_PATH, obj.toString()); // Ideally we'd throw here too.
     }
 
     /**
-     * Moves the session file to the backup session file.
+     * Updates the state of the old session data file.
+     *
+     * sessionstore.js should hold the current session, and sessionstore.bak should
+     * hold the previous session (where it is used to read the "tabs from last time").
+     * If we're not restoring tabs automatically, sessionstore.js needs to be moved to
+     * sessionstore.bak, so we can display the correct "tabs from last time".
+     * If we *are* restoring tabs, we need to delete outdated copies of sessionstore.bak,
+     * so we don't continue showing stale "tabs from last time" indefinitely.
      *
-     * sessionstore.js should hold the current session, and sessionstore.bak
-     * should hold the previous session (where it is used to read the "tabs
-     * from last time"). Normally, sessionstore.js is moved to sessionstore.bak
-     * on a clean quit, but this doesn't happen if Fennec crashed. Thus, this
-     * method should be called after a crash so sessionstore.bak correctly
-     * holds the previous session.
+     * @param shouldRestore Pass true if we are automatically restoring last session's tabs.
      */
-    public void moveSessionFile() {
-        File sessionFile = getFile(SESSION_FILE);
-        if (sessionFile != null && sessionFile.exists()) {
-            File sessionFileBackup = getFile(SESSION_FILE_BACKUP);
-            sessionFile.renameTo(sessionFileBackup);
+    public void updateSessionFile(boolean shouldRestore) {
+        File sessionFileBackup = getFile(SESSION_FILE_BACKUP);
+        if (!shouldRestore) {
+            File sessionFile = getFile(SESSION_FILE);
+            if (sessionFile != null && sessionFile.exists()) {
+                sessionFile.renameTo(sessionFileBackup);
+            }
+        } else {
+            if (sessionFileBackup != null && sessionFileBackup.exists() &&
+                    System.currentTimeMillis() - sessionFileBackup.lastModified() > MAX_BACKUP_FILE_AGE) {
+                sessionFileBackup.delete();
+            }
         }
     }
 
     /**
      * Get the string from a session file.
      *
      * The session can either be read from sessionstore.js or sessionstore.bak.
      * In general, sessionstore.js holds the current session, and