OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/gtest_prod_util.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/observer_list.h" | 9 #include "base/observer_list.h" |
12 #include "base/threading/non_thread_safe.h" | 10 #include "base/threading/non_thread_safe.h" |
13 #include "base/time.h" | 11 #include "base/time.h" |
14 #include "chrome/browser/policy/cloud_policy_subsystem.h" | 12 #include "chrome/browser/policy/cloud_policy_subsystem.h" |
15 #include "chrome/browser/policy/configuration_policy_provider.h" | |
16 #include "chrome/browser/policy/policy_map.h" | 13 #include "chrome/browser/policy/policy_map.h" |
17 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 14 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
18 | 15 |
19 namespace policy { | 16 namespace policy { |
20 | 17 |
21 class PolicyMap; | |
22 class PolicyNotifier; | 18 class PolicyNotifier; |
23 | 19 |
24 namespace em = enterprise_management; | 20 namespace em = enterprise_management; |
25 | 21 |
26 // Caches policy information, as set by calls to |SetPolicy()|, persists | 22 // Caches policy information, as set by calls to |SetPolicy()|, persists |
27 // it to disk or session_manager (depending on subclass implementation), | 23 // it to disk or session_manager (depending on subclass implementation), |
28 // and makes it available via policy providers. | 24 // and makes it available via policy providers. |
29 class CloudPolicyCacheBase : public base::NonThreadSafe { | 25 class CloudPolicyCacheBase : public base::NonThreadSafe { |
30 public: | 26 public: |
31 // Used to distinguish mandatory from recommended policies. | 27 // Used to distinguish mandatory from recommended policies. |
32 enum PolicyLevel { | 28 enum PolicyLevel { |
33 // Policy is forced upon the user and should always take effect. | 29 // Policy is forced upon the user and should always take effect. |
34 POLICY_LEVEL_MANDATORY, | 30 POLICY_LEVEL_MANDATORY, |
35 // The value is just a recommendation that the user may override. | 31 // The value is just a recommendation that the user may override. |
36 POLICY_LEVEL_RECOMMENDED, | 32 POLICY_LEVEL_RECOMMENDED, |
37 }; | 33 }; |
38 | 34 |
| 35 class Observer { |
| 36 public: |
| 37 virtual ~Observer() {} |
| 38 virtual void OnCacheGoingAway(CloudPolicyCacheBase*) = 0; |
| 39 virtual void OnCacheUpdate(CloudPolicyCacheBase*) = 0; |
| 40 }; |
| 41 |
39 CloudPolicyCacheBase(); | 42 CloudPolicyCacheBase(); |
40 virtual ~CloudPolicyCacheBase(); | 43 virtual ~CloudPolicyCacheBase(); |
41 | 44 |
42 void set_policy_notifier(PolicyNotifier* notifier) { | 45 void set_policy_notifier(PolicyNotifier* notifier) { |
43 notifier_ = notifier; | 46 notifier_ = notifier; |
44 } | 47 } |
45 | 48 |
46 // Loads persisted policy information. | 49 // Loads persisted policy information. |
47 virtual void Load() = 0; | 50 virtual void Load() = 0; |
48 | 51 |
49 // Resets the policy information. | 52 // Resets the policy information. |
50 virtual void SetPolicy(const em::PolicyFetchResponse& policy) = 0; | 53 virtual void SetPolicy(const em::PolicyFetchResponse& policy) = 0; |
51 | 54 |
52 ConfigurationPolicyProvider* GetManagedPolicyProvider(); | |
53 ConfigurationPolicyProvider* GetRecommendedPolicyProvider(); | |
54 | |
55 virtual void SetUnmanaged() = 0; | 55 virtual void SetUnmanaged() = 0; |
56 bool is_unmanaged() const { | 56 bool is_unmanaged() const { |
57 return is_unmanaged_; | 57 return is_unmanaged_; |
58 } | 58 } |
59 | 59 |
60 // Returns the time at which the policy was last fetched. | 60 // Returns the time at which the policy was last fetched. |
61 base::Time last_policy_refresh_time() const { | 61 base::Time last_policy_refresh_time() const { |
62 return last_policy_refresh_time_; | 62 return last_policy_refresh_time_; |
63 } | 63 } |
64 | 64 |
65 // Get the version of the encryption key currently used for decoding policy. | 65 // Get the version of the encryption key currently used for decoding policy. |
66 // Returns true if the version is available, in which case |version| is filled | 66 // Returns true if the version is available, in which case |version| is filled |
67 // in. | 67 // in. |
68 bool GetPublicKeyVersion(int* version); | 68 bool GetPublicKeyVersion(int* version); |
69 | 69 |
| 70 void AddObserver(Observer* observer); |
| 71 void RemoveObserver(Observer* observer); |
| 72 |
| 73 // Accessor for the underlying PolicyMaps. |
| 74 const PolicyMap* policy(PolicyLevel level); |
| 75 |
| 76 // true if the cache contains data that is ready to be served as policies. |
| 77 // This should mean that this method turns true as soon as a round-trip to |
| 78 // the local policy storage is complete. The creation of the Profile is |
| 79 // blocked on this method, so we shouldn't wait for successful network |
| 80 // round trips. |
| 81 virtual bool IsReady() = 0; |
| 82 |
70 protected: | 83 protected: |
71 // Wraps public key version and validity. | 84 // Wraps public key version and validity. |
72 struct PublicKeyVersion { | 85 struct PublicKeyVersion { |
73 int version; | 86 int version; |
74 bool valid; | 87 bool valid; |
75 }; | 88 }; |
76 | 89 |
77 // Decodes the given |policy| using |DecodePolicyResponse()|, applies the | 90 // Decodes the given |policy| using |DecodePolicyResponse()|, applies the |
78 // contents to |{mandatory,recommended}_policy_|, and notifies observers. | 91 // contents to |{mandatory,recommended}_policy_|, and notifies observers. |
79 // |timestamp| returns the timestamp embedded in |policy|, callers can pass | 92 // |timestamp| returns the timestamp embedded in |policy|, callers can pass |
(...skipping 16 matching lines...) Expand all Loading... |
96 // Also performs verification, returns NULL if any check fails. | 109 // Also performs verification, returns NULL if any check fails. |
97 bool DecodePolicyResponse(const em::PolicyFetchResponse& policy_response, | 110 bool DecodePolicyResponse(const em::PolicyFetchResponse& policy_response, |
98 PolicyMap* mandatory, | 111 PolicyMap* mandatory, |
99 PolicyMap* recommended, | 112 PolicyMap* recommended, |
100 base::Time* timestamp, | 113 base::Time* timestamp, |
101 PublicKeyVersion* public_key_version); | 114 PublicKeyVersion* public_key_version); |
102 | 115 |
103 void InformNotifier(CloudPolicySubsystem::PolicySubsystemState state, | 116 void InformNotifier(CloudPolicySubsystem::PolicySubsystemState state, |
104 CloudPolicySubsystem::ErrorDetails error_details); | 117 CloudPolicySubsystem::ErrorDetails error_details); |
105 | 118 |
| 119 void set_last_policy_refresh_time(base::Time timestamp) { |
| 120 last_policy_refresh_time_ = timestamp; |
| 121 } |
| 122 |
106 // See comment for |initialization_complete_|. | 123 // See comment for |initialization_complete_|. |
107 bool initialization_complete() { | 124 bool initialization_complete() { |
108 return initialization_complete_; | 125 return initialization_complete_; |
109 } | 126 } |
110 | 127 |
111 void set_last_policy_refresh_time(base::Time timestamp) { | |
112 last_policy_refresh_time_ = timestamp; | |
113 } | |
114 | |
115 private: | 128 private: |
116 class CloudPolicyProvider; | |
117 | |
118 friend class DevicePolicyCacheTest; | 129 friend class DevicePolicyCacheTest; |
119 friend class UserPolicyCacheTest; | 130 friend class UserPolicyCacheTest; |
| 131 friend class MockCloudPolicyCache; |
120 | 132 |
121 // Policy key-value information. | 133 // Policy key-value information. |
122 PolicyMap mandatory_policy_; | 134 PolicyMap mandatory_policy_; |
123 PolicyMap recommended_policy_; | 135 PolicyMap recommended_policy_; |
124 | 136 |
125 // Policy providers. | |
126 scoped_ptr<ConfigurationPolicyProvider> managed_policy_provider_; | |
127 scoped_ptr<ConfigurationPolicyProvider> recommended_policy_provider_; | |
128 | |
129 PolicyNotifier* notifier_; | 137 PolicyNotifier* notifier_; |
130 | 138 |
131 // The time at which the policy was last refreshed. Is updated both upon | 139 // The time at which the policy was last refreshed. Is updated both upon |
132 // successful and unsuccessful refresh attempts. | 140 // successful and unsuccessful refresh attempts. |
133 base::Time last_policy_refresh_time_; | 141 base::Time last_policy_refresh_time_; |
134 | 142 |
135 // Whether initialization has been completed. This is the case when we have | 143 // Whether initialization has been completed. This is the case when we have |
136 // valid policy, learned that the device is unmanaged or ran into | 144 // valid policy, learned that the device is unmanaged or ran into |
137 // unrecoverable errors. | 145 // unrecoverable errors. |
138 bool initialization_complete_; | 146 bool initialization_complete_; |
139 | 147 |
140 // Whether the the server has indicated this device is unmanaged. | 148 // Whether the the server has indicated this device is unmanaged. |
141 bool is_unmanaged_; | 149 bool is_unmanaged_; |
142 | 150 |
143 // Currently used public key version, if available. | 151 // Currently used public key version, if available. |
144 PublicKeyVersion public_key_version_; | 152 PublicKeyVersion public_key_version_; |
145 | 153 |
146 // Provider observers that are registered with this cache's providers. | 154 // Cache observers that are registered with this cache. |
147 ObserverList<ConfigurationPolicyProvider::Observer, true> observer_list_; | 155 ObserverList<Observer, true> observer_list_; |
148 | 156 |
149 DISALLOW_COPY_AND_ASSIGN(CloudPolicyCacheBase); | 157 DISALLOW_COPY_AND_ASSIGN(CloudPolicyCacheBase); |
150 }; | 158 }; |
151 | 159 |
152 } // namespace policy | 160 } // namespace policy |
153 | 161 |
154 #endif // CHROME_BROWSER_POLICY_CLOUD_POLICY_CACHE_BASE_H_ | 162 #endif // CHROME_BROWSER_POLICY_CLOUD_POLICY_CACHE_BASE_H_ |
OLD | NEW |