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 // See comment for |initialization_complete_|. |
| 77 bool initialization_complete() { |
| 78 return initialization_complete_; |
| 79 } |
| 80 |
70 protected: | 81 protected: |
71 // Wraps public key version and validity. | 82 // Wraps public key version and validity. |
72 struct PublicKeyVersion { | 83 struct PublicKeyVersion { |
73 int version; | 84 int version; |
74 bool valid; | 85 bool valid; |
75 }; | 86 }; |
76 | 87 |
77 // Decodes the given |policy| using |DecodePolicyResponse()|, applies the | 88 // Decodes the given |policy| using |DecodePolicyResponse()|, applies the |
78 // contents to |{mandatory,recommended}_policy_|, and notifies observers. | 89 // contents to |{mandatory,recommended}_policy_|, and notifies observers. |
79 // |timestamp| returns the timestamp embedded in |policy|, callers can pass | 90 // |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. | 107 // Also performs verification, returns NULL if any check fails. |
97 bool DecodePolicyResponse(const em::PolicyFetchResponse& policy_response, | 108 bool DecodePolicyResponse(const em::PolicyFetchResponse& policy_response, |
98 PolicyMap* mandatory, | 109 PolicyMap* mandatory, |
99 PolicyMap* recommended, | 110 PolicyMap* recommended, |
100 base::Time* timestamp, | 111 base::Time* timestamp, |
101 PublicKeyVersion* public_key_version); | 112 PublicKeyVersion* public_key_version); |
102 | 113 |
103 void InformNotifier(CloudPolicySubsystem::PolicySubsystemState state, | 114 void InformNotifier(CloudPolicySubsystem::PolicySubsystemState state, |
104 CloudPolicySubsystem::ErrorDetails error_details); | 115 CloudPolicySubsystem::ErrorDetails error_details); |
105 | 116 |
106 // See comment for |initialization_complete_|. | |
107 bool initialization_complete() { | |
108 return initialization_complete_; | |
109 } | |
110 | |
111 void set_last_policy_refresh_time(base::Time timestamp) { | 117 void set_last_policy_refresh_time(base::Time timestamp) { |
112 last_policy_refresh_time_ = timestamp; | 118 last_policy_refresh_time_ = timestamp; |
113 } | 119 } |
114 | 120 |
115 private: | 121 private: |
116 class CloudPolicyProvider; | |
117 | |
118 friend class DevicePolicyCacheTest; | 122 friend class DevicePolicyCacheTest; |
119 friend class UserPolicyCacheTest; | 123 friend class UserPolicyCacheTest; |
| 124 friend class MockCloudPolicyCacheBase; |
120 | 125 |
121 // Policy key-value information. | 126 // Policy key-value information. |
122 PolicyMap mandatory_policy_; | 127 PolicyMap mandatory_policy_; |
123 PolicyMap recommended_policy_; | 128 PolicyMap recommended_policy_; |
124 | 129 |
125 // Policy providers. | |
126 scoped_ptr<ConfigurationPolicyProvider> managed_policy_provider_; | |
127 scoped_ptr<ConfigurationPolicyProvider> recommended_policy_provider_; | |
128 | |
129 PolicyNotifier* notifier_; | 130 PolicyNotifier* notifier_; |
130 | 131 |
131 // The time at which the policy was last refreshed. Is updated both upon | 132 // The time at which the policy was last refreshed. Is updated both upon |
132 // successful and unsuccessful refresh attempts. | 133 // successful and unsuccessful refresh attempts. |
133 base::Time last_policy_refresh_time_; | 134 base::Time last_policy_refresh_time_; |
134 | 135 |
135 // Whether initialization has been completed. This is the case when we have | 136 // Whether initialization has been completed. This is the case when we have |
136 // valid policy, learned that the device is unmanaged or ran into | 137 // valid policy, learned that the device is unmanaged or ran into |
137 // unrecoverable errors. | 138 // unrecoverable errors. |
138 bool initialization_complete_; | 139 bool initialization_complete_; |
139 | 140 |
140 // Whether the the server has indicated this device is unmanaged. | 141 // Whether the the server has indicated this device is unmanaged. |
141 bool is_unmanaged_; | 142 bool is_unmanaged_; |
142 | 143 |
143 // Currently used public key version, if available. | 144 // Currently used public key version, if available. |
144 PublicKeyVersion public_key_version_; | 145 PublicKeyVersion public_key_version_; |
145 | 146 |
146 // Provider observers that are registered with this cache's providers. | 147 // Cache observers that are registered with this cache. |
147 ObserverList<ConfigurationPolicyProvider::Observer, true> observer_list_; | 148 ObserverList<Observer, true> observer_list_; |
148 | 149 |
149 DISALLOW_COPY_AND_ASSIGN(CloudPolicyCacheBase); | 150 DISALLOW_COPY_AND_ASSIGN(CloudPolicyCacheBase); |
150 }; | 151 }; |
151 | 152 |
152 } // namespace policy | 153 } // namespace policy |
153 | 154 |
154 #endif // CHROME_BROWSER_POLICY_CLOUD_POLICY_CACHE_BASE_H_ | 155 #endif // CHROME_BROWSER_POLICY_CLOUD_POLICY_CACHE_BASE_H_ |
OLD | NEW |