Commit 24b1c3c5 authored by Henrique Nakashima's avatar Henrique Nakashima Committed by Commit Bot

Implement DoubleCachedFieldTrialParameter.

This allows caching double-typed field trial parameters for use
before native is loaded.

Also implement methods to read and write double-typed values with
SharedPreferencesManager.

Bug: 1041865, 1012975
Change-Id: Ieba6e8b41ed566bbd087ddba84c53260a16d481c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2087429Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Reviewed-by: default avatarXi Han <hanxi@chromium.org>
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Cr-Commit-Position: refs/heads/master@{#747760}
parent 4093df97
...@@ -118,6 +118,7 @@ public class CachedFeatureFlags { ...@@ -118,6 +118,7 @@ public class CachedFeatureFlags {
private static Map<String, Boolean> sBoolValuesReturned = new HashMap<>(); private static Map<String, Boolean> sBoolValuesReturned = new HashMap<>();
private static Map<String, String> sStringValuesReturned = new HashMap<>(); private static Map<String, String> sStringValuesReturned = new HashMap<>();
private static Map<String, Integer> sIntValuesReturned = new HashMap<>(); private static Map<String, Integer> sIntValuesReturned = new HashMap<>();
private static Map<String, Double> sDoubleValuesReturned = new HashMap<>();
private static String sReachedCodeProfilerTrialGroup; private static String sReachedCodeProfilerTrialGroup;
/** /**
...@@ -380,6 +381,15 @@ public class CachedFeatureFlags { ...@@ -380,6 +381,15 @@ public class CachedFeatureFlags {
return value; return value;
} }
static double getConsistentDoubleValue(String preferenceName, double defaultValue) {
Double value = sDoubleValuesReturned.get(preferenceName);
if (value == null) {
value = SharedPreferencesManager.getInstance().readDouble(preferenceName, defaultValue);
sDoubleValuesReturned.put(preferenceName, value);
}
return value;
}
private static String getPrefForFeatureFlag(String featureName) { private static String getPrefForFeatureFlag(String featureName) {
String grandfatheredPrefKey = sNonDynamicPrefKeys.get(featureName); String grandfatheredPrefKey = sNonDynamicPrefKeys.get(featureName);
if (grandfatheredPrefKey == null) { if (grandfatheredPrefKey == null) {
...@@ -394,6 +404,7 @@ public class CachedFeatureFlags { ...@@ -394,6 +404,7 @@ public class CachedFeatureFlags {
sBoolValuesReturned.clear(); sBoolValuesReturned.clear();
sStringValuesReturned.clear(); sStringValuesReturned.clear();
sIntValuesReturned.clear(); sIntValuesReturned.clear();
sDoubleValuesReturned.clear();
} }
@VisibleForTesting @VisibleForTesting
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.flags;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
/**
* A double-type {@link CachedFieldTrialParameter}.
*/
public class DoubleCachedFieldTrialParameter extends CachedFieldTrialParameter {
private double mDefaultValue;
public DoubleCachedFieldTrialParameter(
String featureName, String variationName, double defaultValue) {
super(featureName, variationName, FieldTrialParameterType.DOUBLE, null);
mDefaultValue = defaultValue;
}
public double getDefaultValue() {
return mDefaultValue;
}
@Override
void cacheToDisk() {
double value = ChromeFeatureList.getFieldTrialParamByFeatureAsDouble(
getFeatureName(), getParameterName(), getDefaultValue());
SharedPreferencesManager.getInstance().writeDouble(getSharedPreferenceKey(), value);
}
}
...@@ -316,6 +316,7 @@ public class SharedPreferencesManager { ...@@ -316,6 +316,7 @@ public class SharedPreferencesManager {
/** /**
* Writes the given float value to the named shared preference and immediately commit to disk. * Writes the given float value to the named shared preference and immediately commit to disk.
*
* @param key The name of the preference to modify. * @param key The name of the preference to modify.
* @param value The new value for the preference. * @param value The new value for the preference.
* @return Whether the operation succeeded. * @return Whether the operation succeeded.
...@@ -344,6 +345,39 @@ public class SharedPreferencesManager { ...@@ -344,6 +345,39 @@ public class SharedPreferencesManager {
} }
} }
/**
* Writes the given double value to the named shared preference.
*
* @param key The name of the preference to modify.
* @param value The new value for the preference.
*/
public void writeDouble(String key, double value) {
mKeyChecker.checkIsKeyInUse(key);
SharedPreferences.Editor ed = ContextUtils.getAppSharedPreferences().edit();
long ieee754LongValue = Double.doubleToRawLongBits(value);
ed.putLong(key, ieee754LongValue);
ed.apply();
}
/**
* Reads the given double value from the named shared preference.
*
* @param key The name of the preference to return.
* @param defaultValue The default value to return if there's no value stored.
* @return The value of the preference if stored; defaultValue otherwise.
*/
public Double readDouble(String key, double defaultValue) {
mKeyChecker.checkIsKeyInUse(key);
SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
try (StrictModeContext ignored = StrictModeContext.allowDiskReads()) {
if (!prefs.contains(key)) {
return defaultValue;
}
long ieee754LongValue = prefs.getLong(key, 0L);
return Double.longBitsToDouble(ieee754LongValue);
}
}
/** /**
* Writes the given boolean to the named shared preference. * Writes the given boolean to the named shared preference.
* *
......
...@@ -178,6 +178,28 @@ public class SharedPreferencesManagerTest { ...@@ -178,6 +178,28 @@ public class SharedPreferencesManagerTest {
assertFalse(mSubject.contains("float_key")); assertFalse(mSubject.contains("float_key"));
} }
@Test
@SmallTest
public void testWriteReadDouble() {
// Verify default return values when no value is written.
assertEquals(1.5d, mSubject.readDouble("double_key", 1.5d), 0.001f);
assertFalse(mSubject.contains("double_key"));
// Write a value.
mSubject.writeDouble("double_key", 42.42f);
// Verify value written can be read.
assertEquals(42.42d, mSubject.readDouble("double_key", 1.5d), 0.001f);
assertTrue(mSubject.contains("double_key"));
// Remove the value.
mSubject.removeKey("double_key");
// Verify the removed value is not returned anymore.
assertEquals(1.5d, mSubject.readDouble("double_key", 1.5d), 0.001f);
assertFalse(mSubject.contains("double_key"));
}
@Test @Test
@SmallTest @SmallTest
public void testWriteReadStringSet() { public void testWriteReadStringSet() {
...@@ -371,6 +393,11 @@ public class SharedPreferencesManagerTest { ...@@ -371,6 +393,11 @@ public class SharedPreferencesManagerTest {
mSubject.readFloat("float_key", 0f); mSubject.readFloat("float_key", 0f);
verify(mChecker, times(2)).checkIsKeyInUse("float_key"); verify(mChecker, times(2)).checkIsKeyInUse("float_key");
mSubject.writeDouble("double_key", 2.5d);
verify(mChecker, times(1)).checkIsKeyInUse("double_key");
mSubject.readDouble("double_key", 0d);
verify(mChecker, times(2)).checkIsKeyInUse("double_key");
mSubject.writeStringSet("string_set_key", new HashSet<>()); mSubject.writeStringSet("string_set_key", new HashSet<>());
verify(mChecker, times(1)).checkIsKeyInUse("string_set_key"); verify(mChecker, times(1)).checkIsKeyInUse("string_set_key");
mSubject.readStringSet("string_set_key"); mSubject.readStringSet("string_set_key");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment