Index: components/policy/android/java/src/org/chromium/policy/AbstractAppRestrictionsProvider.java |
diff --git a/components/policy/android/java/src/org/chromium/policy/AbstractAppRestrictionsProvider.java b/components/policy/android/java/src/org/chromium/policy/AbstractAppRestrictionsProvider.java |
index 60b40bd139a467abdc88342ece61ae473486b3ac..ea7290325a932eb0f5b3c6308cdfd4c23d6cf816 100644 |
--- a/components/policy/android/java/src/org/chromium/policy/AbstractAppRestrictionsProvider.java |
+++ b/components/policy/android/java/src/org/chromium/policy/AbstractAppRestrictionsProvider.java |
@@ -30,6 +30,12 @@ |
public abstract class AbstractAppRestrictionsProvider extends PolicyProvider { |
private static final String PREFERENCE_KEY = "App Restrictions"; |
+ /** |
+ * Key of a flag to be set in the cached policy bundle. If present, the cached policies will |
+ * be used instead of the ones returned by the system. |
+ */ |
+ @VisibleForTesting public static final String STICKY_CACHE_KEY = "StickyCacheForTests"; |
+ |
private final Context mContext; |
private final SharedPreferences mSharedPreferences; |
private final BroadcastReceiver mAppRestrictionsChangedReceiver = new BroadcastReceiver() { |
@@ -61,6 +67,10 @@ public AbstractAppRestrictionsProvider(Context context) { |
*/ |
protected abstract String getRestrictionChangeIntentAction(); |
+ protected String getPreferenceCacheKey() { |
+ return getClass().getCanonicalName(); |
+ } |
+ |
/** |
* Start listening for restrictions changes. Does nothing if this is not supported by the |
* platform. |
@@ -95,8 +105,12 @@ protected Bundle doInBackground(Void... params) { |
@Override |
protected void onPostExecute(Bundle result) { |
- cachePolicies(result); |
- notifySettingsAvailable(result); |
+ if (cachedResult != null && cachedResult.getBoolean(STICKY_CACHE_KEY)) { |
aberent
2015/10/07 11:13:02
If the cachedResult is null then this will take th
dgn
2015/10/07 14:36:57
As I understand it, the only way to get null would
aberent
2015/10/15 14:12:25
I don't like test only anything, but if you need s
|
+ notifySettingsAvailable(cachedResult); |
+ } else { |
+ cachePolicies(mSharedPreferences, result); |
+ notifySettingsAvailable(result); |
+ } |
} |
}.executeOnExecutor(mExecutor); |
} |
@@ -117,12 +131,12 @@ public void stopListening() { |
} |
} |
- private void cachePolicies(Bundle policies) { |
+ private static void cachePolicies(SharedPreferences prefs, Bundle policies) { |
Parcel p = Parcel.obtain(); |
p.writeBundle(policies); |
byte bytes[] = p.marshall(); |
String s = Base64.encodeToString(bytes, 0); |
- SharedPreferences.Editor ed = mSharedPreferences.edit(); |
+ SharedPreferences.Editor ed = prefs.edit(); |
ed.putString(PREFERENCE_KEY, s); |
ed.apply(); |
} |
@@ -169,4 +183,11 @@ protected void recordStartTimeHistogram(long startTime) { |
void setTaskExecutor(Executor testExecutor) { |
mExecutor = testExecutor; |
} |
+ |
+ /** Context has to be the same that will be used to load the policies. */ |
+ @VisibleForTesting |
+ public static void setCachedPoliciesForTesting(Context context, Bundle policies) { |
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); |
+ cachePolicies(prefs, policies); |
+ } |
} |