OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.policy; |
| 6 |
| 7 import android.os.Bundle; |
| 8 |
| 9 import org.chromium.base.CalledByNative; |
| 10 import org.chromium.base.ThreadUtils; |
| 11 |
| 12 import java.util.ArrayList; |
| 13 import java.util.List; |
| 14 |
| 15 /** |
| 16 * Reads enterprise policies from one or more policy providers |
| 17 * and plumbs them through to the policy subsystem. |
| 18 */ |
| 19 public class PolicyManager { |
| 20 private long mNativePolicyManager; |
| 21 |
| 22 private final List<PolicyProvider> mPolicyProviders = new ArrayList<>(); |
| 23 private final List<Bundle> mCachedPolicies = new ArrayList<>(); |
| 24 private final List<PolicyChangeListener> mPolicyChangeListeners = new ArrayL
ist<>(); |
| 25 |
| 26 public PolicyManager() { |
| 27 ThreadUtils.assertOnUiThread(); |
| 28 mNativePolicyManager = nativeInit(); |
| 29 } |
| 30 |
| 31 /** |
| 32 * PolicyProviders are assigned a unique precedence based on their order of
registration. |
| 33 * Later Registration -> Higher Precedence. |
| 34 * This precedence is also used as a 'source' tag for disambiguating updates
. |
| 35 */ |
| 36 public void registerProvider(PolicyProvider provider) { |
| 37 mPolicyProviders.add(provider); |
| 38 mCachedPolicies.add(null); |
| 39 provider.setManagerAndSource(this, mPolicyProviders.size() - 1); |
| 40 provider.refresh(); |
| 41 } |
| 42 |
| 43 public void destroy() { |
| 44 // All the activities registered should have been destroyed by then. |
| 45 assert mPolicyChangeListeners.isEmpty(); |
| 46 |
| 47 for (PolicyProvider provider : mPolicyProviders) { |
| 48 provider.destroy(); |
| 49 } |
| 50 mPolicyProviders.clear(); |
| 51 |
| 52 nativeDestroy(mNativePolicyManager); |
| 53 mNativePolicyManager = 0; |
| 54 } |
| 55 |
| 56 void onSettingsAvailable(int source, Bundle newSettings) { |
| 57 mCachedPolicies.set(source, newSettings); |
| 58 // Check if we have policies from all the providers before applying them
. |
| 59 for (Bundle settings : mCachedPolicies) { |
| 60 if (settings == null) return; |
| 61 } |
| 62 for (Bundle settings : mCachedPolicies) { |
| 63 for (String key : settings.keySet()) { |
| 64 Object value = settings.get(key); |
| 65 if (value instanceof Boolean) { |
| 66 nativeSetPolicyBoolean(mNativePolicyManager, key, (Boolean)
value); |
| 67 continue; |
| 68 } |
| 69 if (value instanceof String) { |
| 70 nativeSetPolicyString(mNativePolicyManager, key, (String) va
lue); |
| 71 continue; |
| 72 } |
| 73 if (value instanceof Integer) { |
| 74 nativeSetPolicyInteger(mNativePolicyManager, key, (Integer)
value); |
| 75 continue; |
| 76 } |
| 77 if (value instanceof String[]) { |
| 78 nativeSetPolicyStringArray(mNativePolicyManager, key, (Strin
g[]) value); |
| 79 continue; |
| 80 } |
| 81 assert false : "Invalid setting " + value + " for key " + key; |
| 82 } |
| 83 } |
| 84 nativeFlushPolicies(mNativePolicyManager); |
| 85 } |
| 86 |
| 87 void terminateIncognitoSession() { |
| 88 for (PolicyChangeListener listener : mPolicyChangeListeners) { |
| 89 listener.terminateIncognitoSession(); |
| 90 } |
| 91 } |
| 92 |
| 93 public void addPolicyChangeListener(PolicyChangeListener listener) { |
| 94 mPolicyChangeListeners.add(listener); |
| 95 } |
| 96 |
| 97 public void removePolicyChangeListener(PolicyChangeListener listener) { |
| 98 mPolicyChangeListeners.remove(listener); |
| 99 } |
| 100 |
| 101 @CalledByNative |
| 102 private void refreshPolicies() { |
| 103 assert mPolicyProviders.size() == mCachedPolicies.size(); |
| 104 for (int i = 0; i < mCachedPolicies.size(); ++i) { |
| 105 mCachedPolicies.set(i, null); |
| 106 } |
| 107 for (PolicyProvider provider : mPolicyProviders) { |
| 108 provider.refresh(); |
| 109 } |
| 110 } |
| 111 |
| 112 /** |
| 113 * Interface to handle actions related with policy changes. |
| 114 */ |
| 115 public interface PolicyChangeListener { |
| 116 /** |
| 117 * Call to notify the listener that incognito browsing is unavailable du
e to policy. |
| 118 */ |
| 119 void terminateIncognitoSession(); |
| 120 } |
| 121 |
| 122 private native long nativeInit(); |
| 123 private native void nativeDestroy(long nativePolicyManager); |
| 124 |
| 125 private native void nativeSetPolicyBoolean( |
| 126 long nativePolicyManager, String policyKey, boolean value); |
| 127 private native void nativeSetPolicyInteger( |
| 128 long nativePolicyManager, String policyKey, int value); |
| 129 private native void nativeSetPolicyString( |
| 130 long nativePolicyManager, String policyKey, String value); |
| 131 private native void nativeSetPolicyStringArray( |
| 132 long nativePolicyManager, String policyKey, String[] value); |
| 133 private native void nativeFlushPolicies(long nativePolicyManager); |
| 134 } |
OLD | NEW |