Commit 0d92c444 authored by Owen Min's avatar Owen Min Committed by Chromium LUCI CQ

Add PolicyCache class

Add getter function of PolicyCache that reads from the SharedPreferences
https://source.chromium.org/chromium/chromium/src/+/master:components/policy/core/common/policy_service_impl.cc;l=146?q=IDS_POLICY_MIGRATED_NEW_POLICY&ss=chromium%2Fchromium%2Fsrc

Bug: 1127469
Change-Id: If6f8103e55625439a0607e358449ffddfe2949cc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2633927
Commit-Queue: Owen Min <zmin@chromium.org>
Reviewed-by: default avatarSky Malice <skym@chromium.org>
Cr-Commit-Position: refs/heads/master@{#845143}
parent f3c62799
...@@ -30,6 +30,7 @@ android_library("policy_java") { ...@@ -30,6 +30,7 @@ android_library("policy_java") {
sources = _jni_sources + [ sources = _jni_sources + [
"java/src/org/chromium/components/policy/AbstractAppRestrictionsProvider.java", "java/src/org/chromium/components/policy/AbstractAppRestrictionsProvider.java",
"java/src/org/chromium/components/policy/AppRestrictionsProvider.java", "java/src/org/chromium/components/policy/AppRestrictionsProvider.java",
"java/src/org/chromium/components/policy/PolicyCache.java",
"java/src/org/chromium/components/policy/PolicyProvider.java", "java/src/org/chromium/components/policy/PolicyProvider.java",
] ]
} }
...@@ -61,6 +62,7 @@ java_library("components_policy_junit_tests") { ...@@ -61,6 +62,7 @@ java_library("components_policy_junit_tests") {
sources = [ sources = [
"junit/src/org/chromium/components/policy/AbstractAppRestrictionsProviderTest.java", "junit/src/org/chromium/components/policy/AbstractAppRestrictionsProviderTest.java",
"junit/src/org/chromium/components/policy/CombinedPolicyProviderTest.java", "junit/src/org/chromium/components/policy/CombinedPolicyProviderTest.java",
"junit/src/org/chromium/components/policy/PolicyCacheTest.java",
"junit/src/org/chromium/components/policy/PolicyConverterTest.java", "junit/src/org/chromium/components/policy/PolicyConverterTest.java",
"junit/src/org/chromium/components/policy/test/annotations/PoliciesTest.java", "junit/src/org/chromium/components/policy/test/annotations/PoliciesTest.java",
] ]
......
// Copyright 2021 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.components.policy;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.VisibleForTesting;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.chromium.base.ContextUtils;
/**
* Manage policy cache that will be used during browser launch stage.
*
* Policy loading is async on Android and caching policy values makes them
* available during launch stage even before native library is ready.
*/
public class PolicyCache {
@VisibleForTesting
static final String POLICY_PREF = "Components.Policy";
private static SharedPreferences sSharedPreferences;
/**
* Creates and returns SharedPreferences instance that is used to cache policy
* value.
*
* @return The SharedPreferences instance that is used for policy caching. Returns null if
* application context is not available.
*/
private static SharedPreferences getSharedPreferences() {
if (sSharedPreferences == null) {
Context context = ContextUtils.getApplicationContext();
// Policy cache is not accessiable without application context.
if (context == null) return null;
sSharedPreferences = context.getSharedPreferences(POLICY_PREF, Context.MODE_PRIVATE);
}
return sSharedPreferences;
}
/**
* @param policy The name of policy.
* @return The value of cached integer policy, null if there is no valid
* cached policy.
*/
public static Integer getIntValue(String policy) {
SharedPreferences sSharedPreferences = getSharedPreferences();
if (sSharedPreferences == null) return null;
if (!sSharedPreferences.contains(policy)) return null;
return sSharedPreferences.getInt(policy, 0);
}
/**
* @param policy The name of policy.
* @return The value of cached boolean policy, null if there is no valid
* cached policy.
*/
public static Boolean getBooleanValue(String policy) {
SharedPreferences sSharedPreferences = getSharedPreferences();
if (sSharedPreferences == null) return null;
if (!sSharedPreferences.contains(policy)) return null;
return sSharedPreferences.getBoolean(policy, false);
}
/**
* @param policy The name of policy.
* @return The value of cached string policy, null if there is no valid
* cached policy.
*/
public static String getStringValue(String policy) {
SharedPreferences sSharedPreferences = getSharedPreferences();
if (sSharedPreferences == null) return null;
if (!sSharedPreferences.contains(policy)) return null;
return sSharedPreferences.getString(policy, null);
}
/**
* @param policy The name of policy.
* @return The value of cached list policy, null if there is no valid
* cached policy.
*/
public static JSONArray getListValue(String policy) {
SharedPreferences sSharedPreferences = getSharedPreferences();
if (sSharedPreferences == null) return null;
if (!sSharedPreferences.contains(policy)) return null;
try {
return new JSONArray(sSharedPreferences.getString(policy, null));
} catch (JSONException e) {
return null;
}
}
/**
* @param policy The name of policy.
* @return The value of cached dictionary policy, null if there is no valid
* cached policy.
*/
public static JSONObject getDictValue(String policy) {
SharedPreferences sSharedPreferences = getSharedPreferences();
if (sSharedPreferences == null) return null;
if (!sSharedPreferences.contains(policy)) return null;
try {
return new JSONObject(sSharedPreferences.getString(policy, null));
} catch (JSONException e) {
return null;
}
}
@VisibleForTesting
static void resetSharedPreferencesForTests() {
sSharedPreferences = null;
}
}
// Copyright 2021 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.components.policy;
import android.content.Context;
import android.content.SharedPreferences;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.chromium.base.ContextUtils;
import org.chromium.base.test.BaseRobolectricTestRunner;
/** Robolectric test for PolicyCache. */
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public final class PolicyCacheTest {
private static final String POLICY_NAME = "policy-name";
private SharedPreferences mSharedPreferences;
@Before
public void setUp() {
mSharedPreferences = ContextUtils.getApplicationContext().getSharedPreferences(
PolicyCache.POLICY_PREF, Context.MODE_PRIVATE);
PolicyCache.resetSharedPreferencesForTests();
}
@After
public void tearDown() {}
@Test
public void testGetInt() {
Assert.assertNull(PolicyCache.getIntValue(POLICY_NAME));
int expectedPolicyValue = 42;
mSharedPreferences.edit().putInt(POLICY_NAME, expectedPolicyValue).apply();
Assert.assertEquals(expectedPolicyValue, PolicyCache.getIntValue(POLICY_NAME).intValue());
}
@Test
public void testGetBoolean() {
Assert.assertNull(PolicyCache.getBooleanValue(POLICY_NAME));
boolean expectedPolicyValue = true;
mSharedPreferences.edit().putBoolean(POLICY_NAME, expectedPolicyValue).apply();
Assert.assertEquals(
expectedPolicyValue, PolicyCache.getBooleanValue(POLICY_NAME).booleanValue());
}
@Test
public void testGetString() {
Assert.assertNull(PolicyCache.getStringValue(POLICY_NAME));
String expectedPolicyValue = "test-value";
mSharedPreferences.edit().putString(POLICY_NAME, expectedPolicyValue).apply();
Assert.assertEquals(expectedPolicyValue, PolicyCache.getStringValue(POLICY_NAME));
}
@Test
public void testGetList() throws JSONException {
Assert.assertNull(PolicyCache.getListValue(POLICY_NAME));
String policyValue = "[42, \"test\", true]";
mSharedPreferences.edit().putString(POLICY_NAME, policyValue).apply();
JSONArray actualPolicyValue = PolicyCache.getListValue(POLICY_NAME);
Assert.assertNotNull(actualPolicyValue);
Assert.assertEquals(3, actualPolicyValue.length());
Assert.assertEquals(42, actualPolicyValue.getInt(0));
Assert.assertEquals("test", actualPolicyValue.getString(1));
Assert.assertEquals(true, actualPolicyValue.getBoolean(2));
}
@Test
public void testGetInvalidList() throws JSONException {
String policyValue = "[42, \"test\"";
mSharedPreferences.edit().putString(POLICY_NAME, policyValue).apply();
Assert.assertNull(PolicyCache.getListValue(POLICY_NAME));
}
@Test
public void testGetDict() throws JSONException {
Assert.assertNull(PolicyCache.getDictValue(POLICY_NAME));
String policyValue = "{\"key1\":\"value1\", \"key2\":{\"a\":1, \"b\":2}}";
mSharedPreferences.edit().putString(POLICY_NAME, policyValue).apply();
JSONObject actualPolicyValue = PolicyCache.getDictValue(POLICY_NAME);
Assert.assertNotNull(actualPolicyValue);
Assert.assertEquals(2, actualPolicyValue.length());
Assert.assertEquals("value1", actualPolicyValue.getString("key1"));
Assert.assertEquals(1, actualPolicyValue.getJSONObject("key2").getInt("a"));
Assert.assertEquals(2, actualPolicyValue.getJSONObject("key2").getInt("b"));
}
@Test
public void testGetInvalidDict() throws JSONException {
Assert.assertNull(PolicyCache.getDictValue(POLICY_NAME));
String policyValue = "{\"key1\":\"value1\", \"key2\":{\"a\":1, \"b\":2}";
mSharedPreferences.edit().putString(POLICY_NAME, policyValue).apply();
Assert.assertNull(PolicyCache.getListValue(POLICY_NAME));
}
}
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