200 lines
7.0 KiB
Diff
200 lines
7.0 KiB
Diff
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||
|
From: fgei <fgei@gmail.com>
|
||
|
Date: Mon, 13 Mar 2023 10:29:53 +0000
|
||
|
Subject: [PATCH] Utility class for adding shared preference keys
|
||
|
|
||
|
---
|
||
|
chrome/browser/preferences/BUILD.gn | 3 +
|
||
|
.../ChromePreferenceKeyChecker.java | 4 +
|
||
|
.../browser/preferences/SharedPrefsUtils.java | 152 ++++++++++++++++++
|
||
|
3 files changed, 159 insertions(+)
|
||
|
create mode 100644 chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/SharedPrefsUtils.java
|
||
|
|
||
|
diff --git a/chrome/browser/preferences/BUILD.gn b/chrome/browser/preferences/BUILD.gn
|
||
|
index c3038943a893b..e2a45f2c23b95 100644
|
||
|
--- a/chrome/browser/preferences/BUILD.gn
|
||
|
+++ b/chrome/browser/preferences/BUILD.gn
|
||
|
@@ -15,6 +15,9 @@ android_library("java") {
|
||
|
"android/java/src/org/chromium/chrome/browser/preferences/PrefChangeRegistrar.java",
|
||
|
"android/java/src/org/chromium/chrome/browser/preferences/SharedPreferencesManager.java",
|
||
|
]
|
||
|
+ sources += [
|
||
|
+ "android/java/src/org/chromium/chrome/browser/preferences/SharedPrefsUtils.java",
|
||
|
+ ]
|
||
|
deps = [
|
||
|
"//base:base_java",
|
||
|
"//base:jni_java",
|
||
|
diff --git a/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/ChromePreferenceKeyChecker.java b/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/ChromePreferenceKeyChecker.java
|
||
|
index 21e3be5ba9f77..915ee8be3def7 100644
|
||
|
--- a/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/ChromePreferenceKeyChecker.java
|
||
|
+++ b/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/ChromePreferenceKeyChecker.java
|
||
|
@@ -75,6 +75,10 @@ class ChromePreferenceKeyChecker extends BaseChromePreferenceKeyChecker {
|
||
|
* @return Whether |key| is in use.
|
||
|
*/
|
||
|
private boolean isKeyInUse(String key) {
|
||
|
+ if (SharedPrefsUtils.isKeyInUse(key)) {
|
||
|
+ return true;
|
||
|
+ }
|
||
|
+
|
||
|
// For non-dynamic legacy keys, a simple map check is enough.
|
||
|
if (mLegacyFormatKeys.contains(key)) {
|
||
|
return true;
|
||
|
diff --git a/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/SharedPrefsUtils.java b/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/SharedPrefsUtils.java
|
||
|
new file mode 100644
|
||
|
index 0000000000000..ceec302c0a9d6
|
||
|
--- /dev/null
|
||
|
+++ b/chrome/browser/preferences/android/java/src/org/chromium/chrome/browser/preferences/SharedPrefsUtils.java
|
||
|
@@ -0,0 +1,152 @@
|
||
|
+package org.chromium.chrome.browser.preferences;
|
||
|
+
|
||
|
+import org.chromium.build.annotations.CheckDiscard;
|
||
|
+
|
||
|
+import java.util.Arrays;
|
||
|
+
|
||
|
+/**
|
||
|
+ * Wrapper utility class for both (Base)ChromePreferenceKeys and SharedPreferencesManager,
|
||
|
+ * which also serves as storage of added PreferenceKeys.
|
||
|
+ */
|
||
|
+public final class SharedPrefsUtils {
|
||
|
+
|
||
|
+ public static class BoolSharedPref {
|
||
|
+ private final String key;
|
||
|
+ private boolean defValue;
|
||
|
+
|
||
|
+ BoolSharedPref(String key, boolean defValue) {
|
||
|
+ this.key = key;
|
||
|
+ this.defValue = defValue;
|
||
|
+ }
|
||
|
+
|
||
|
+ public String getKey() { return key; }
|
||
|
+
|
||
|
+ public boolean get() {
|
||
|
+ return getSharedPrefManager().readBoolean(key, defValue);
|
||
|
+ }
|
||
|
+
|
||
|
+ public void put(boolean newValue) {
|
||
|
+ getSharedPrefManager().writeBoolean(key, newValue);
|
||
|
+ }
|
||
|
+
|
||
|
+ public boolean putSync(boolean newValue) {
|
||
|
+ return getSharedPrefManager().writeBooleanSync(key, newValue);
|
||
|
+ }
|
||
|
+ }
|
||
|
+
|
||
|
+ public static class IntSharedPref {
|
||
|
+ private final String key;
|
||
|
+ private int defValue;
|
||
|
+ private int[] validValues;
|
||
|
+
|
||
|
+ IntSharedPref(String key, int defValue) {
|
||
|
+ this.key = key;
|
||
|
+ this.defValue = defValue;
|
||
|
+ }
|
||
|
+
|
||
|
+ IntSharedPref(String key, int defValue, int[] validValues) {
|
||
|
+ this.key = key;
|
||
|
+ this.defValue = defValue;
|
||
|
+ this.validValues = validValues;
|
||
|
+ }
|
||
|
+
|
||
|
+ public String getKey() { return key; }
|
||
|
+
|
||
|
+ public int get() {
|
||
|
+ return getSharedPrefManager().readInt(key, defValue);
|
||
|
+ }
|
||
|
+
|
||
|
+ public boolean validateValue(int val) {
|
||
|
+ if (validValues == null) {
|
||
|
+ return true;
|
||
|
+ }
|
||
|
+ // don't do sort() + bsearch() of validValues array, it's expected to have a small number of entries
|
||
|
+ for (int validValue : validValues) {
|
||
|
+ if (val == validValue) {
|
||
|
+ return true;
|
||
|
+ }
|
||
|
+ }
|
||
|
+ return false;
|
||
|
+ }
|
||
|
+
|
||
|
+ public void put(int newValue) {
|
||
|
+ if (!validateValue(newValue)) {
|
||
|
+ return;
|
||
|
+ }
|
||
|
+ getSharedPrefManager().writeInt(key, newValue);
|
||
|
+ }
|
||
|
+
|
||
|
+ public boolean putSync(int newValue) {
|
||
|
+ if (!validateValue(newValue)) {
|
||
|
+ return false;
|
||
|
+ }
|
||
|
+ return getSharedPrefManager().writeIntSync(key, newValue);
|
||
|
+ }
|
||
|
+ }
|
||
|
+
|
||
|
+ public static class StringSharedPref {
|
||
|
+ private final String key;
|
||
|
+ private String defValue;
|
||
|
+ private String[] validValues;
|
||
|
+
|
||
|
+ StringSharedPref(String key, String defValue) {
|
||
|
+ this.key = key;
|
||
|
+ this.defValue = defValue;
|
||
|
+ }
|
||
|
+
|
||
|
+ StringSharedPref(String key, String defValue, String[] validValues) {
|
||
|
+ this.key = key;
|
||
|
+ this.defValue = defValue;
|
||
|
+ this.validValues = validValues;
|
||
|
+ }
|
||
|
+
|
||
|
+ public String getKey() { return key; }
|
||
|
+
|
||
|
+ public String get() {
|
||
|
+ return getSharedPrefManager().readString(key, defValue);
|
||
|
+ }
|
||
|
+
|
||
|
+ public boolean validateValue(String val) {
|
||
|
+ if (validValues == null) {
|
||
|
+ return true;
|
||
|
+ }
|
||
|
+ // don't do sort() + bsearch() of validValues array, it's expected to have a small number of entries
|
||
|
+ for (String validValue : validValues) {
|
||
|
+ if (val == validValue) {
|
||
|
+ return true;
|
||
|
+ }
|
||
|
+ }
|
||
|
+ return false;
|
||
|
+ }
|
||
|
+
|
||
|
+ public void put(String newValue) {
|
||
|
+ if (!validateValue(newValue)) {
|
||
|
+ return;
|
||
|
+ }
|
||
|
+ getSharedPrefManager().writeString(key, newValue);
|
||
|
+ }
|
||
|
+
|
||
|
+ public boolean putSync(String newValue) {
|
||
|
+ if (!validateValue(newValue)) {
|
||
|
+ return false;
|
||
|
+ }
|
||
|
+ return getSharedPrefManager().writeStringSync(key, newValue);
|
||
|
+ }
|
||
|
+ }
|
||
|
+
|
||
|
+ // Stores SharedPreferences keys and its default value
|
||
|
+ public static class SharedPrefsExt {
|
||
|
+ }
|
||
|
+
|
||
|
+ static SharedPreferencesManager getSharedPrefManager() {
|
||
|
+ return SharedPreferencesManager.getInstance();
|
||
|
+ }
|
||
|
+
|
||
|
+ @CheckDiscard("Validation is performed in tests and in debug builds.")
|
||
|
+ static boolean isKeyInUse(String key) {
|
||
|
+ // clang-format off
|
||
|
+ return Arrays.asList(
|
||
|
+ ).contains(key);
|
||
|
+ // clang-format on
|
||
|
+ }
|
||
|
+}
|