From: csagan5 <32685696+csagan5@users.noreply.github.com>
Date: Sat, 7 Sep 2019 15:07:42 +0200
Subject: Add option to not persist tabs across sessions

License: GPL-3.0-only - https://spdx.org/licenses/GPL-3.0-only.html
---
 .../android/java/res/xml/privacy_preferences.xml |  5 +++++
 .../chrome/browser/ChromeTabbedActivity.java     |  5 ++++-
 .../privacy/settings/PrivacySettings.java        | 16 +++++++++++++++-
 .../android/strings/android_chrome_strings.grd   |  6 ++++++
 4 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/chrome/android/java/res/xml/privacy_preferences.xml b/chrome/android/java/res/xml/privacy_preferences.xml
--- a/chrome/android/java/res/xml/privacy_preferences.xml
+++ b/chrome/android/java/res/xml/privacy_preferences.xml
@@ -45,6 +45,11 @@ found in the LICENSE file.
         android:title="@string/incognito_settings_title"
         android:summary="@string/incognito_settings_summary"
         android:fragment="org.chromium.chrome.browser.privacy.settings.IncognitoSettings"/>
+    <org.chromium.components.browser_ui.settings.ChromeSwitchPreference
+        android:key="close_tabs_on_exit"
+        android:title="@string/close_tabs_on_exit_title"
+        android:summary="@string/close_tabs_on_exit_summary"
+        android:defaultValue="false" />
     <Preference
         android:fragment="org.chromium.chrome.browser.privacy.settings.DoNotTrackSettings"
         android:key="do_not_track"
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ChromeTabbedActivity.java b/chrome/android/java/src/org/chromium/chrome/browser/ChromeTabbedActivity.java
--- a/chrome/android/java/src/org/chromium/chrome/browser/ChromeTabbedActivity.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ChromeTabbedActivity.java
@@ -40,6 +40,7 @@ import androidx.lifecycle.LifecycleRegistry;
 import org.chromium.base.BuildInfo;
 import org.chromium.base.CallbackController;
 import org.chromium.base.CommandLine;
+import org.chromium.base.ContextUtils;
 import org.chromium.base.IntentUtils;
 import org.chromium.base.Log;
 import org.chromium.base.MemoryPressureListener;
@@ -1322,8 +1323,10 @@ public class ChromeTabbedActivity extends ChromeActivity<ChromeActivityComponent
             boolean hadCipherData =
                     CipherFactory.getInstance().restoreFromBundle(getSavedInstanceState());
 
+            String PREF_CLOSE_TABS_ON_EXIT = "close_tabs_on_exit";
             boolean noRestoreState =
-                    CommandLine.getInstance().hasSwitch(ChromeSwitches.NO_RESTORE_STATE);
+                CommandLine.getInstance().hasSwitch(ChromeSwitches.NO_RESTORE_STATE) ||
+                ContextUtils.getAppSharedPreferences().getBoolean(PREF_CLOSE_TABS_ON_EXIT, false);
             if (noRestoreState) {
                 // Clear the state files because they are inconsistent and useless from now on.
                 mTabModelOrchestrator.clearState();
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/privacy/settings/PrivacySettings.java b/chrome/android/java/src/org/chromium/chrome/browser/privacy/settings/PrivacySettings.java
--- a/chrome/android/java/src/org/chromium/chrome/browser/privacy/settings/PrivacySettings.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/privacy/settings/PrivacySettings.java
@@ -5,6 +5,7 @@
 package org.chromium.chrome.browser.privacy.settings;
 
 import android.os.Build;
+import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.text.SpannableString;
 import android.view.Menu;
@@ -15,6 +16,8 @@ import androidx.preference.Preference;
 import androidx.preference.PreferenceFragmentCompat;
 import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;
 
+import org.chromium.base.ContextUtils;
+import org.chromium.base.BuildInfo;
 import org.chromium.chrome.R;
 import org.chromium.chrome.browser.feedback.HelpAndFeedbackLauncherImpl;
 import org.chromium.chrome.browser.flags.ChromeFeatureList;
@@ -67,6 +70,8 @@ public class PrivacySettings
             SharedPreferencesManager.getInstance();
     private final PrefService prefService = UserPrefs.get(Profile.getLastUsedRegularProfile());
 
+    private static final String PREF_CLOSE_TABS_ON_EXIT = "close_tabs_on_exit";
+
     private ManagedPreferenceDelegate mManagedPreferenceDelegate;
     private IncognitoLockSettings mIncognitoLockSettings;
 
@@ -132,7 +137,11 @@ public class PrivacySettings
     @Override
     public boolean onPreferenceChange(Preference preference, Object newValue) {
         String key = preference.getKey();
-        if (PREF_CAN_MAKE_PAYMENT.equals(key)) {
+        if (PREF_CLOSE_TABS_ON_EXIT.equals(key)) {
+            SharedPreferences.Editor sharedPreferencesEditor = ContextUtils.getAppSharedPreferences().edit();
+            sharedPreferencesEditor.putBoolean(PREF_CLOSE_TABS_ON_EXIT, (boolean)newValue);
+            sharedPreferencesEditor.apply();
+        } else if (PREF_CAN_MAKE_PAYMENT.equals(key)) {
             UserPrefs.get(Profile.getLastUsedRegularProfile())
                     .setBoolean(Pref.CAN_MAKE_PAYMENT_ENABLED, (boolean) newValue);
         } else if (PREF_HTTPS_FIRST_MODE.equals(key)) {
@@ -176,6 +185,11 @@ public class PrivacySettings
                     PreloadPagesSettingsFragment.getPreloadPagesSummaryString(getContext()));
         }
 
+        ChromeSwitchPreference closeTabsOnExitPref =
+                (ChromeSwitchPreference) findPreference(PREF_CLOSE_TABS_ON_EXIT);
+        closeTabsOnExitPref.setOnPreferenceChangeListener(this);
+        closeTabsOnExitPref.setManagedPreferenceDelegate(mManagedPreferenceDelegate);
+
         Preference secureDnsPref = findPreference(PREF_SECURE_DNS);
         if (secureDnsPref != null && secureDnsPref.isVisible()) {
             secureDnsPref.setSummary(SecureDnsSettings.getSummary(getContext()));
diff --git a/chrome/browser/ui/android/strings/android_chrome_strings.grd b/chrome/browser/ui/android/strings/android_chrome_strings.grd
--- a/chrome/browser/ui/android/strings/android_chrome_strings.grd
+++ b/chrome/browser/ui/android/strings/android_chrome_strings.grd
@@ -4420,6 +4420,12 @@ To change this setting, <ph name="BEGIN_LINK">&lt;resetlink&gt;</ph>reset sync<p
       <message name="IDS_CONTEXTMENU_IMAGE_TITLE" desc="The title of a context menu tab when the item pressed contains more than one type. This indicates that all the actions are related to the image.">
         IMAGE
       </message>
+      <message name="IDS_CLOSE_TABS_ON_EXIT_TITLE" desc="Text for 'Close tabs on exit' settings-privacy option.">
+        Close all open tabs on exit
+      </message>
+      <message name="IDS_CLOSE_TABS_ON_EXIT_SUMMARY" desc="Summary text for 'Close tabs on exit' settings-privacy option.">
+        Don't persist tabs between browsing sessions
+      </message>
       <message name="IDS_CONTEXTMENU_LINK_TITLE" desc="The title of a context menu tab when the item pressed contains more than one type. This indicates that all the actions are related to the link.">
         LINK
       </message>
--
2.25.1
