From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: fgei Date: Tue, 11 Apr 2023 07:55:52 +0000 Subject: [PATCH] Utility class for modifying preferences at Privacy section --- chrome/android/chrome_ext_java_resources.gni | 1 + chrome/android/chrome_ext_java_sources.gni | 1 + .../java/res/xml/privacy_preferences_ext.xml | 9 +++ .../privacy/settings/PrivacySettings.java | 6 ++ .../privacy/settings/PrivacySettingsExt.java | 72 +++++++++++++++++++ 5 files changed, 89 insertions(+) create mode 100644 chrome/android/java/res/xml/privacy_preferences_ext.xml create mode 100644 chrome/android/java/src/org/chromium/chrome/browser/privacy/settings/PrivacySettingsExt.java diff --git a/chrome/android/chrome_ext_java_resources.gni b/chrome/android/chrome_ext_java_resources.gni index 9980a7c5f6d84..164ef483d5a44 100644 --- a/chrome/android/chrome_ext_java_resources.gni +++ b/chrome/android/chrome_ext_java_resources.gni @@ -3,4 +3,5 @@ # found in the LICENSE file. chrome_ext_java_resources = [ + "java/res/xml/privacy_preferences_ext.xml", ] diff --git a/chrome/android/chrome_ext_java_sources.gni b/chrome/android/chrome_ext_java_sources.gni index b3a489f124aa8..d580fb6e83875 100644 --- a/chrome/android/chrome_ext_java_sources.gni +++ b/chrome/android/chrome_ext_java_sources.gni @@ -4,4 +4,5 @@ chrome_ext_java_sources = [ "java/src/org/chromium/chrome/browser/settings/SettingsExtUtils.java", + "java/src/org/chromium/chrome/browser/privacy/settings/PrivacySettingsExt.java", ] diff --git a/chrome/android/java/res/xml/privacy_preferences_ext.xml b/chrome/android/java/res/xml/privacy_preferences_ext.xml new file mode 100644 index 0000000000000..a352cd657b1b9 --- /dev/null +++ b/chrome/android/java/res/xml/privacy_preferences_ext.xml @@ -0,0 +1,9 @@ + + + + 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 index 8ffb0e6dac424..17dc4ad79a508 100644 --- 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 @@ -84,6 +84,8 @@ public class PrivacySettings SettingsUtils.addPreferencesFromResource(this, R.xml.privacy_preferences); } + PrivacySettingsExt.initializePreferences(this, getProfile()); + Preference sandboxPreference = findPreference(PREF_PRIVACY_SANDBOX); // Overwrite the click listener to pass a correct referrer to the fragment. sandboxPreference.setOnPreferenceClickListener(preference -> { @@ -187,6 +189,8 @@ public class PrivacySettings SingleCategorySettings.EXTRA_TITLE, thirdPartyCookies.getTitle().toString()); } + PrivacySettingsExt.removeUnwantedPreferences(this); + updatePreferences(); } @@ -304,6 +308,8 @@ public class PrivacySettings UserPrefs.get(getProfile()).getInteger(COOKIE_CONTROLS_MODE))); } + PrivacySettingsExt.updatePreferences(this, getProfile()); + updatePrivacyGuidePreferenceTitle(); } diff --git a/chrome/android/java/src/org/chromium/chrome/browser/privacy/settings/PrivacySettingsExt.java b/chrome/android/java/src/org/chromium/chrome/browser/privacy/settings/PrivacySettingsExt.java new file mode 100644 index 0000000000000..fa6ef4589d6e9 --- /dev/null +++ b/chrome/android/java/src/org/chromium/chrome/browser/privacy/settings/PrivacySettingsExt.java @@ -0,0 +1,72 @@ +// Copyright 2023 GrapheneOS +// Use of this source code is governed by a GPLv2-style license that can be +// found in the LICENSE file. + +package org.chromium.chrome.browser.privacy.settings; + +import androidx.annotation.NonNull; +import androidx.preference.Preference; +import androidx.preference.PreferenceFragmentCompat; +import androidx.preference.SwitchPreferenceCompat; + +import org.chromium.base.ThreadUtils; +import org.chromium.base.shared_preferences.SharedPrefsUtils.SharedPrefsExt; +import org.chromium.chrome.R; +import org.chromium.chrome.browser.flags.ChromeFeatureList; +import org.chromium.chrome.browser.preferences.Pref; +import org.chromium.chrome.browser.profiles.Profile; +import org.chromium.chrome.browser.settings.ChromeManagedPreferenceDelegate; +import org.chromium.chrome.browser.settings.SettingsExtUtils; +import org.chromium.components.browser_ui.settings.ChromeBasePreference; +import org.chromium.components.browser_ui.settings.ChromeSwitchPreference; +import org.chromium.components.browser_ui.settings.SettingsUtils; +import org.chromium.components.prefs.PrefService; +import org.chromium.components.user_prefs.UserPrefs; + +final class PrivacySettingsExt { + + private static final Preference.OnPreferenceChangeListener getListener(@NonNull Profile profile) { + return (pref, val) -> { + PrefService prefService = UserPrefs.get(profile); + if (pref == null) { + return false; + } + String key = pref.getKey(); + return true; + }; + } + + private static final ChromeManagedPreferenceDelegate getDelegate(@NonNull Profile profile) { + return new ChromeManagedPreferenceDelegate(profile) { + @Override + public boolean isPreferenceControlledByPolicy(Preference pref) { + PrefService prefService = UserPrefs.get(profile); + if (pref == null) { + return false; + } + String key = pref.getKey(); + return false; + } + }; + } + + static void removeUnwantedPreferences(@NonNull PreferenceFragmentCompat prefFragment) { + ThreadUtils.checkUiThread(); + } + + static void initializePreferences(@NonNull PreferenceFragmentCompat prefFragment, @NonNull Profile profile) { + ThreadUtils.checkUiThread(); + // This is such that privacy preferences are added at Privacy Section in newer UI, + // and mostly kept on previous order at older UI. + int PRIVACY_PREFERENCES_ORDER = + ChromeFeatureList.isEnabled(ChromeFeatureList.PRIVACY_SANDBOX_SETTINGS_4) ? 2 : 6; + int SECURITY_PREFERENCES_ORDER = + ChromeFeatureList.isEnabled(ChromeFeatureList.PRIVACY_SANDBOX_SETTINGS_4) ? 2 : 9999; + SettingsUtils.addPreferencesFromResource(prefFragment, R.xml.privacy_preferences_ext); + } + + static void updatePreferences(@NonNull PreferenceFragmentCompat prefFragment, @NonNull Profile profile) { + ThreadUtils.checkUiThread(); + PrefService prefService = UserPrefs.get(profile); + } +}