| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_POLICY_CLOUD_POLICY_CACHE_BASE_H_ | 5 #ifndef CHROME_BROWSER_POLICY_CLOUD_POLICY_CACHE_BASE_H_ |
| 6 #define CHROME_BROWSER_POLICY_CLOUD_POLICY_CACHE_BASE_H_ | 6 #define CHROME_BROWSER_POLICY_CLOUD_POLICY_CACHE_BASE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/observer_list.h" | 9 #include "base/observer_list.h" |
| 10 #include "base/threading/non_thread_safe.h" | 10 #include "base/threading/non_thread_safe.h" |
| 11 #include "base/time.h" | 11 #include "base/time.h" |
| 12 #include "chrome/browser/policy/cloud_policy_subsystem.h" | 12 #include "chrome/browser/policy/cloud_policy_subsystem.h" |
| 13 #include "chrome/browser/policy/policy_map.h" | 13 #include "chrome/browser/policy/policy_map.h" |
| 14 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 14 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 15 | 15 |
| 16 namespace policy { | 16 namespace policy { |
| 17 | 17 |
| 18 class PolicyNotifier; | 18 class PolicyNotifier; |
| 19 | 19 |
| 20 // Caches policy information, as set by calls to |SetPolicy()|, persists | 20 // Caches policy information, as set by calls to |SetPolicy()|, persists |
| 21 // it to disk or session_manager (depending on subclass implementation), | 21 // it to disk or session_manager (depending on subclass implementation), |
| 22 // and makes it available via policy providers. | 22 // and makes it available via policy providers. |
| 23 class CloudPolicyCacheBase : public base::NonThreadSafe { | 23 class CloudPolicyCacheBase : public base::NonThreadSafe { |
| 24 public: | 24 public: |
| 25 // Used to distinguish mandatory from recommended policies. | |
| 26 enum PolicyLevel { | |
| 27 // Policy is forced upon the user and should always take effect. | |
| 28 POLICY_LEVEL_MANDATORY, | |
| 29 // The value is just a recommendation that the user may override. | |
| 30 POLICY_LEVEL_RECOMMENDED, | |
| 31 }; | |
| 32 | |
| 33 class Observer { | 25 class Observer { |
| 34 public: | 26 public: |
| 35 virtual ~Observer() {} | 27 virtual ~Observer() {} |
| 36 virtual void OnCacheGoingAway(CloudPolicyCacheBase*) = 0; | 28 virtual void OnCacheGoingAway(CloudPolicyCacheBase*) = 0; |
| 37 virtual void OnCacheUpdate(CloudPolicyCacheBase*) = 0; | 29 virtual void OnCacheUpdate(CloudPolicyCacheBase*) = 0; |
| 38 }; | 30 }; |
| 39 | 31 |
| 40 CloudPolicyCacheBase(); | 32 CloudPolicyCacheBase(); |
| 41 virtual ~CloudPolicyCacheBase(); | 33 virtual ~CloudPolicyCacheBase(); |
| 42 | 34 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 72 } | 64 } |
| 73 | 65 |
| 74 // Get the version of the encryption key currently used for decoding policy. | 66 // Get the version of the encryption key currently used for decoding policy. |
| 75 // Returns true if the version is available, in which case |version| is filled | 67 // Returns true if the version is available, in which case |version| is filled |
| 76 // in. | 68 // in. |
| 77 bool GetPublicKeyVersion(int* version); | 69 bool GetPublicKeyVersion(int* version); |
| 78 | 70 |
| 79 void AddObserver(Observer* observer); | 71 void AddObserver(Observer* observer); |
| 80 void RemoveObserver(Observer* observer); | 72 void RemoveObserver(Observer* observer); |
| 81 | 73 |
| 82 // Accessor for the underlying PolicyMaps. | 74 // Accessor for the underlying PolicyMap. |
| 83 const PolicyMap* policy(PolicyLevel level); | 75 const PolicyMap* policy() { return &policies_; } |
| 84 | 76 |
| 85 // Resets the cache, clearing the policy currently stored in memory and the | 77 // Resets the cache, clearing the policy currently stored in memory and the |
| 86 // last refresh time. | 78 // last refresh time. |
| 87 void Reset(); | 79 void Reset(); |
| 88 | 80 |
| 89 // true if the cache contains data that is ready to be served as policies. | 81 // true if the cache contains data that is ready to be served as policies. |
| 90 // This usually means that the local policy storage has been loaded. | 82 // This usually means that the local policy storage has been loaded. |
| 91 // Note that Profile creation will block until the cache is ready. | 83 // Note that Profile creation will block until the cache is ready. |
| 92 // On enrolled devices and for users of the enrolled domain, the cache only | 84 // On enrolled devices and for users of the enrolled domain, the cache only |
| 93 // becomes ready after a user policy fetch is completed. | 85 // becomes ready after a user policy fetch is completed. |
| 94 bool IsReady(); | 86 bool IsReady(); |
| 95 | 87 |
| 96 protected: | 88 protected: |
| 97 // Wraps public key version and validity. | 89 // Wraps public key version and validity. |
| 98 struct PublicKeyVersion { | 90 struct PublicKeyVersion { |
| 99 int version; | 91 int version; |
| 100 bool valid; | 92 bool valid; |
| 101 }; | 93 }; |
| 102 | 94 |
| 103 // Decodes the given |policy| using |DecodePolicyResponse()|, applies the | 95 // Decodes the given |policy| using |DecodePolicyResponse()|, applies the |
| 104 // contents to |{mandatory,recommended}_policy_|, and notifies observers. | 96 // contents to |policies_|, and notifies observers. |
| 105 // |timestamp| returns the timestamp embedded in |policy|, callers can pass | 97 // |timestamp| returns the timestamp embedded in |policy|, callers can pass |
| 106 // NULL if they don't care. |check_for_timestamp_validity| tells this method | 98 // NULL if they don't care. |check_for_timestamp_validity| tells this method |
| 107 // to discard policy data with a timestamp from the future. | 99 // to discard policy data with a timestamp from the future. |
| 108 // Returns true upon success. | 100 // Returns true upon success. |
| 109 bool SetPolicyInternal( | 101 bool SetPolicyInternal( |
| 110 const enterprise_management::PolicyFetchResponse& policy, | 102 const enterprise_management::PolicyFetchResponse& policy, |
| 111 base::Time* timestamp, | 103 base::Time* timestamp, |
| 112 bool check_for_timestamp_validity); | 104 bool check_for_timestamp_validity); |
| 113 | 105 |
| 114 void SetUnmanagedInternal(const base::Time& timestamp); | 106 void SetUnmanagedInternal(const base::Time& timestamp); |
| 115 | 107 |
| 116 // Indicates that initialization is now complete. Observers will be notified. | 108 // Indicates that initialization is now complete. Observers will be notified. |
| 117 void SetReady(); | 109 void SetReady(); |
| 118 | 110 |
| 119 // Decodes |policy_data|, populating |mandatory| and |recommended| with | 111 // Decodes |policy_data|, populating |mandatory| and |recommended| with |
| 120 // the results. | 112 // the results. |
| 121 virtual bool DecodePolicyData( | 113 virtual bool DecodePolicyData( |
| 122 const enterprise_management::PolicyData& policy_data, | 114 const enterprise_management::PolicyData& policy_data, |
| 123 PolicyMap* mandatory, | 115 PolicyMap* policies) = 0; |
| 124 PolicyMap* recommended) = 0; | |
| 125 | 116 |
| 126 // Decodes a PolicyFetchResponse into two PolicyMaps and a timestamp. | 117 // Decodes a PolicyFetchResponse into two PolicyMaps and a timestamp. |
| 127 // Also performs verification, returns NULL if any check fails. | 118 // Also performs verification, returns NULL if any check fails. |
| 128 bool DecodePolicyResponse( | 119 bool DecodePolicyResponse( |
| 129 const enterprise_management::PolicyFetchResponse& policy_response, | 120 const enterprise_management::PolicyFetchResponse& policy_response, |
| 130 PolicyMap* mandatory, | 121 PolicyMap* policies, |
| 131 PolicyMap* recommended, | |
| 132 base::Time* timestamp, | 122 base::Time* timestamp, |
| 133 PublicKeyVersion* public_key_version); | 123 PublicKeyVersion* public_key_version); |
| 134 | 124 |
| 135 // Notifies observers if the cache IsReady(). | 125 // Notifies observers if the cache IsReady(). |
| 136 void NotifyObservers(); | 126 void NotifyObservers(); |
| 137 | 127 |
| 138 void InformNotifier(CloudPolicySubsystem::PolicySubsystemState state, | 128 void InformNotifier(CloudPolicySubsystem::PolicySubsystemState state, |
| 139 CloudPolicySubsystem::ErrorDetails error_details); | 129 CloudPolicySubsystem::ErrorDetails error_details); |
| 140 | 130 |
| 141 void set_last_policy_refresh_time(base::Time timestamp) { | 131 void set_last_policy_refresh_time(base::Time timestamp) { |
| 142 last_policy_refresh_time_ = timestamp; | 132 last_policy_refresh_time_ = timestamp; |
| 143 } | 133 } |
| 144 | 134 |
| 145 private: | 135 private: |
| 146 friend class DevicePolicyCacheTest; | 136 friend class DevicePolicyCacheTest; |
| 147 friend class UserPolicyCacheTest; | 137 friend class UserPolicyCacheTest; |
| 148 friend class MockCloudPolicyCache; | 138 friend class MockCloudPolicyCache; |
| 149 | 139 |
| 150 // Policy key-value information. | 140 // Policy key-value information. |
| 151 PolicyMap mandatory_policy_; | 141 PolicyMap policies_; |
| 152 PolicyMap recommended_policy_; | |
| 153 | 142 |
| 154 PolicyNotifier* notifier_; | 143 PolicyNotifier* notifier_; |
| 155 | 144 |
| 156 // The time at which the policy was last refreshed. Is updated both upon | 145 // The time at which the policy was last refreshed. Is updated both upon |
| 157 // successful and unsuccessful refresh attempts. | 146 // successful and unsuccessful refresh attempts. |
| 158 base::Time last_policy_refresh_time_; | 147 base::Time last_policy_refresh_time_; |
| 159 | 148 |
| 160 // Whether initialization has been completed. This is the case when we have | 149 // Whether initialization has been completed. This is the case when we have |
| 161 // valid policy, learned that the device is unmanaged or ran into | 150 // valid policy, learned that the device is unmanaged or ran into |
| 162 // unrecoverable errors. | 151 // unrecoverable errors. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 174 | 163 |
| 175 // Cache observers that are registered with this cache. | 164 // Cache observers that are registered with this cache. |
| 176 ObserverList<Observer, true> observer_list_; | 165 ObserverList<Observer, true> observer_list_; |
| 177 | 166 |
| 178 DISALLOW_COPY_AND_ASSIGN(CloudPolicyCacheBase); | 167 DISALLOW_COPY_AND_ASSIGN(CloudPolicyCacheBase); |
| 179 }; | 168 }; |
| 180 | 169 |
| 181 } // namespace policy | 170 } // namespace policy |
| 182 | 171 |
| 183 #endif // CHROME_BROWSER_POLICY_CLOUD_POLICY_CACHE_BASE_H_ | 172 #endif // CHROME_BROWSER_POLICY_CLOUD_POLICY_CACHE_BASE_H_ |
| OLD | NEW |