From be9ba0fd90dca6f837a3648c9b8e2e6e363567d9 Mon Sep 17 00:00:00 2001 From: Jan Henning Date: Thu, 21 Apr 2016 20:43:47 +0200 Subject: [PATCH] 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 --HG-- extra : transplant_source : %2Ac%83%F40i%1Ah%3F%B1QI%D6%84%FF%7B%E8%157%F1 extra : histedit_source : 61cc196e844b26e00cf47363c69e6f1672d90f14 --- .../base/java/org/mozilla/gecko/GeckoApp.java | 7 ++-- .../java/org/mozilla/gecko/GeckoProfile.java | 34 ++++++++++++------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java b/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java index 0b01c24832f6..662a1b03baae 100644 --- a/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java +++ b/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java @@ -1536,11 +1536,8 @@ public abstract class GeckoApp 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); diff --git a/mobile/android/base/java/org/mozilla/gecko/GeckoProfile.java b/mobile/android/base/java/org/mozilla/gecko/GeckoProfile.java index bec2b3766be7..7b6c825c310b 100644 --- a/mobile/android/base/java/org/mozilla/gecko/GeckoProfile.java +++ b/mobile/android/base/java/org/mozilla/gecko/GeckoProfile.java @@ -76,6 +76,7 @@ public final class GeckoProfile { // 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 sProfileCache = new HashMap(); private static String sDefaultProfileName; @@ -761,20 +762,29 @@ public final class GeckoProfile { } /** - * 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"). 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. + * 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. + * + * @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(); + } } }