From: csagan5 <32685696+csagan5@users.noreply.github.com>
Date: Tue, 28 Aug 2018 23:27:23 +0200
Subject: Add site settings option for session-only cookies

---
 chrome/android/java/res/values/values.xml          |  3 +++
 .../android/java/res/xml/website_preferences.xml   |  7 ++++++
 .../browser/preferences/PrefServiceBridge.java     | 10 +++++++++
 .../website/SingleCategoryPreferences.java         | 21 +++++++++++++++++-
 .../java/strings/android_chrome_strings.grd        |  6 ++++++
 .../android/preferences/pref_service_bridge.cc     | 25 ++++++++++++++++++++++
 6 files changed, 71 insertions(+), 1 deletion(-)

diff --git a/chrome/android/java/res/values/values.xml b/chrome/android/java/res/values/values.xml
--- a/chrome/android/java/res/values/values.xml
+++ b/chrome/android/java/res/values/values.xml
@@ -39,6 +39,9 @@
     <integer name="reload_button_level_reload">0</integer>
     <integer name="reload_button_level_stop">1</integer>
 
+    <string name="allow_cookies_session_only_title">Keep cookies only until you quit your browser</string>
+    <string name="allow_cookies_session_only_summary">When enabled, all cookies will be erased when the browsing session is over</string>
+
     <!-- Help and Feedback
          These constants should be in sync with the context names on go/mobilehelprecs.
          If the context ID cannot be found, the default help page will be shown to the user.-->
diff --git a/chrome/android/java/res/xml/website_preferences.xml b/chrome/android/java/res/xml/website_preferences.xml
--- a/chrome/android/java/res/xml/website_preferences.xml
+++ b/chrome/android/java/res/xml/website_preferences.xml
@@ -26,6 +26,13 @@
         android:summary="@string/block_third_party_cookies_summary"
         android:defaultValue="true"
         android:persistent="false" />
+    <!-- A toggle for cookies to be saved only until session exit, only shown for the Cookies category. -->
+    <org.chromium.chrome.browser.preferences.ChromeBaseCheckBoxPreference
+        android:key="cookies_session_only"
+        android:title="@string/allow_cookies_session_only_title"
+        android:summary="@string/allow_cookies_session_only_summary"
+        android:defaultValue="true"
+        android:persistent="false" />
     <!-- A toggle for enabling vibration in notifications. -->
     <org.chromium.chrome.browser.preferences.ChromeBaseCheckBoxPreference
         android:key="notifications_vibrate"
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/preferences/PrefServiceBridge.java b/chrome/android/java/src/org/chromium/chrome/browser/preferences/PrefServiceBridge.java
--- a/chrome/android/java/src/org/chromium/chrome/browser/preferences/PrefServiceBridge.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/preferences/PrefServiceBridge.java
@@ -240,6 +240,10 @@ public class PrefServiceBridge {
         return nativeGetBlockThirdPartyCookiesEnabled();
     }
 
+    public boolean isCookiesSessionOnlyEnabled() {
+        return nativeGetCookiesSessionOnlyEnabled();
+    }
+
     /**
      * @return Whether third-party cookie blocking is configured by policy
      */
@@ -626,6 +630,10 @@ public class PrefServiceBridge {
         nativeSetBlockThirdPartyCookiesEnabled(enabled);
     }
 
+    public void setCookiesSessionOnlyEnabled(boolean enabled) {
+        nativeSetCookiesSessionOnlyEnabled(enabled);
+    }
+
     public void setDoNotTrackEnabled(boolean enabled) {
         nativeSetDoNotTrackEnabled(enabled);
     }
@@ -1074,6 +1082,7 @@ public class PrefServiceBridge {
     private native boolean nativeGetAutoplayEnabled();
     private native boolean nativeGetBackgroundSyncEnabled();
     private native boolean nativeGetBlockThirdPartyCookiesEnabled();
+    private native boolean nativeGetCookiesSessionOnlyEnabled();
     private native boolean nativeGetBlockThirdPartyCookiesManaged();
     private native boolean nativeGetRememberPasswordsEnabled();
     private native boolean nativeGetPasswordManagerAutoSigninEnabled();
@@ -1120,6 +1129,7 @@ public class PrefServiceBridge {
     private native void nativeSetAutoplayEnabled(boolean enabled);
     private native void nativeSetAllowCookiesEnabled(boolean enabled);
     private native void nativeSetBackgroundSyncEnabled(boolean enabled);
+    private native void nativeSetCookiesSessionOnlyEnabled(boolean enabled);
     private native void nativeSetBlockThirdPartyCookiesEnabled(boolean enabled);
     private native void nativeSetClipboardEnabled(boolean enabled);
     private native void nativeSetDoNotTrackEnabled(boolean enabled);
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/preferences/website/SingleCategoryPreferences.java b/chrome/android/java/src/org/chromium/chrome/browser/preferences/website/SingleCategoryPreferences.java
--- a/chrome/android/java/src/org/chromium/chrome/browser/preferences/website/SingleCategoryPreferences.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/preferences/website/SingleCategoryPreferences.java
@@ -121,6 +121,7 @@ public class SingleCategoryPreferences extends PreferenceFragment
 
     // Keys for category-specific preferences (toggle, link, button etc.), dynamically shown.
     public static final String THIRD_PARTY_COOKIES_TOGGLE_KEY = "third_party_cookies";
+    public static final String COOKIES_SESSION_ONLY_TOGGLE_KEY = "cookies_session_only";
     public static final String NOTIFICATIONS_VIBRATE_TOGGLE_KEY = "notifications_vibrate";
     public static final String EXPLAIN_PROTECTED_MEDIA_KEY = "protected_content_learn_more";
     private static final String ADD_EXCEPTION_KEY = "add_exception";
@@ -441,6 +442,7 @@ public class SingleCategoryPreferences extends PreferenceFragment
 
                 if (type == SiteSettingsCategory.Type.COOKIES) {
                     updateThirdPartyCookiesCheckBox();
+                    updateCookiesSessionOnlyCheckBox();
                 } else if (type == SiteSettingsCategory.Type.NOTIFICATIONS) {
                     updateNotificationsVibrateCheckBox();
                 }
@@ -479,6 +481,8 @@ public class SingleCategoryPreferences extends PreferenceFragment
             int setting = (int) newValue;
             prefServiceBridge.setContentSetting(mCategory.getContentSettingsType(), setting);
             getInfoForOrigins();
+        } else if (COOKIES_SESSION_ONLY_TOGGLE_KEY.equals(preference.getKey())) {
+            prefServiceBridge.setCookiesSessionOnlyEnabled((boolean) newValue);
         } else if (THIRD_PARTY_COOKIES_TOGGLE_KEY.equals(preference.getKey())) {
             prefServiceBridge.setBlockThirdPartyCookiesEnabled(((boolean) newValue));
         } else if (NOTIFICATIONS_VIBRATE_TOGGLE_KEY.equals(preference.getKey())) {
@@ -769,6 +773,8 @@ public class SingleCategoryPreferences extends PreferenceFragment
         TriStateSiteSettingsPreference triStateToggle =
                 (TriStateSiteSettingsPreference) screen.findPreference(TRI_STATE_TOGGLE_KEY);
         Preference thirdPartyCookies = screen.findPreference(THIRD_PARTY_COOKIES_TOGGLE_KEY);
+        Preference cookiesSessionOnly = getPreferenceScreen().findPreference(
+                COOKIES_SESSION_ONLY_TOGGLE_KEY);
         Preference notificationsVibrate = screen.findPreference(NOTIFICATIONS_VIBRATE_TOGGLE_KEY);
         Preference explainProtectedMediaKey = screen.findPreference(EXPLAIN_PROTECTED_MEDIA_KEY);
         PreferenceGroup allowedGroup = (PreferenceGroup) screen.findPreference(ALLOWED_GROUP);
@@ -799,6 +805,7 @@ public class SingleCategoryPreferences extends PreferenceFragment
 
         if (hideSecondaryToggles) {
             screen.removePreference(thirdPartyCookies);
+            screen.removePreference(cookiesSessionOnly);
             screen.removePreference(notificationsVibrate);
             screen.removePreference(explainProtectedMediaKey);
             screen.removePreference(allowedGroup);
@@ -808,12 +815,15 @@ public class SingleCategoryPreferences extends PreferenceFragment
             return;
         }
 
-        // Configure/hide the third-party cookie toggle, as needed.
+        // Configure/hide the third-party cookie toggle and session-only cookie toggle, as needed.
         if (mCategory.showSites(SiteSettingsCategory.Type.COOKIES)) {
             thirdPartyCookies.setOnPreferenceChangeListener(this);
             updateThirdPartyCookiesCheckBox();
+            cookiesSessionOnly.setOnPreferenceChangeListener(this);
+            updateCookiesSessionOnlyCheckBox();
         } else {
             screen.removePreference(thirdPartyCookies);
+            screen.removePreference(cookiesSessionOnly);
         }
 
         // Configure/hide the notifications vibrate toggle, as needed.
@@ -918,6 +928,15 @@ public class SingleCategoryPreferences extends PreferenceFragment
                 preference -> PrefServiceBridge.getInstance().isBlockThirdPartyCookiesManaged());
     }
 
+    private void updateCookiesSessionOnlyCheckBox() {
+        ChromeBaseCheckBoxPreference cookiesSessionOnlyPref = (ChromeBaseCheckBoxPreference)
+                getPreferenceScreen().findPreference(COOKIES_SESSION_ONLY_TOGGLE_KEY);
+        cookiesSessionOnlyPref.setChecked(
+                PrefServiceBridge.getInstance().isCookiesSessionOnlyEnabled());
+        cookiesSessionOnlyPref.setEnabled(PrefServiceBridge.getInstance().isCategoryEnabled(
+                ContentSettingsType.CONTENT_SETTINGS_TYPE_COOKIES));
+    }
+
     private void updateNotificationsVibrateCheckBox() {
         ChromeBaseCheckBoxPreference preference =
                 (ChromeBaseCheckBoxPreference) getPreferenceScreen().findPreference(
diff --git a/chrome/android/java/strings/android_chrome_strings.grd b/chrome/android/java/strings/android_chrome_strings.grd
--- a/chrome/android/java/strings/android_chrome_strings.grd
+++ b/chrome/android/java/strings/android_chrome_strings.grd
@@ -907,6 +907,12 @@ Your Google account may have other forms of browsing history like searches and a
       <message name="IDS_BLOCK_THIRD_PARTY_COOKIES_SUMMARY" desc="Summary text for Block Third Party Cookies preference">
         Prevent third-party websites from saving and reading cookie data
       </message>
+      <message name="IDS_ALLOW_COOKIES_SESSION_ONLY_TITLE" desc="Title for Allow Cookies Session Only preference">
+        Keep cookies only until you quit your browser
+      </message>
+      <message name="IDS_ALLOW_COOKIES_SESSION_ONLY_SUMMARY" desc="Summary text for Allow Session Only Cookies preference">
+        When enabled, all cookies will be erased when the browsing session is over
+      </message>
       <message name="IDS_JAVASCRIPT_PERMISSION_TITLE" desc="Title of the permission to run javascript [CHAR-LIMIT=32]">
         JavaScript
       </message>
diff --git a/chrome/browser/android/preferences/pref_service_bridge.cc b/chrome/browser/android/preferences/pref_service_bridge.cc
--- a/chrome/browser/android/preferences/pref_service_bridge.cc
+++ b/chrome/browser/android/preferences/pref_service_bridge.cc
@@ -311,6 +311,17 @@ static jboolean JNI_PrefServiceBridge_GetBlockThirdPartyCookiesEnabled(
   return GetPrefService()->GetBoolean(prefs::kBlockThirdPartyCookies);
 }
 
+static jboolean JNI_PrefServiceBridge_GetCookiesSessionOnlyEnabled(
+    JNIEnv* env,
+    const JavaParamRef<jobject>& obj) {
+  HostContentSettingsMap* host_content_settings_map =
+      HostContentSettingsMapFactory::GetForProfile(GetOriginalProfile());
+  auto value = host_content_settings_map->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES, nullptr);
+  auto enabled = CONTENT_SETTING_SESSION_ONLY == value;
+  LOG(INFO) << "GetCookiesSessionOnly := " << enabled << " (raw_value = " << value << ")";
+  return enabled;
+}
+
 static jboolean JNI_PrefServiceBridge_GetBlockThirdPartyCookiesManaged(
     JNIEnv* env,
     const JavaParamRef<jobject>& obj) {
@@ -768,6 +779,20 @@ static void JNI_PrefServiceBridge_SetBlockThirdPartyCookiesEnabled(
   GetPrefService()->SetBoolean(prefs::kBlockThirdPartyCookies, enabled);
 }
 
+static void JNI_PrefServiceBridge_SetCookiesSessionOnlyEnabled(
+    JNIEnv* env,
+    const JavaParamRef<jobject>& obj,
+    jboolean enabled) {
+  HostContentSettingsMap* host_content_settings_map =
+      HostContentSettingsMapFactory::GetForProfile(GetOriginalProfile());
+  LOG(INFO) << "SetCookiesSessionOnly -> " << (enabled ? "true" : "false") <<
+           "; false -> setting_allow: " << CONTENT_SETTING_ALLOW <<
+           "; true -> session_only: "   << CONTENT_SETTING_SESSION_ONLY;
+  host_content_settings_map->SetDefaultContentSetting(
+      CONTENT_SETTINGS_TYPE_COOKIES,
+      enabled ? CONTENT_SETTING_SESSION_ONLY : CONTENT_SETTING_ALLOW);
+}
+
 static void JNI_PrefServiceBridge_SetRememberPasswordsEnabled(
     JNIEnv* env,
     const JavaParamRef<jobject>& obj,
-- 
2.11.0

