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 #include "chrome/browser/policy/cloud_policy_cache_base.h" | 5 #include "chrome/browser/policy/cloud_policy_cache_base.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/browser/policy/configuration_policy_pref_store.h" | |
12 #include "chrome/browser/policy/policy_notifier.h" | 11 #include "chrome/browser/policy/policy_notifier.h" |
13 | 12 |
14 namespace policy { | 13 namespace policy { |
15 | 14 |
16 // A thin ConfigurationPolicyProvider implementation sitting on top of | |
17 // CloudPolicyCacheBase for hooking up with ConfigurationPolicyPrefStore. | |
18 class CloudPolicyCacheBase::CloudPolicyProvider | |
19 : public ConfigurationPolicyProvider { | |
20 public: | |
21 CloudPolicyProvider(const PolicyDefinitionList* policy_list, | |
22 CloudPolicyCacheBase* cache, | |
23 CloudPolicyCacheBase::PolicyLevel level) | |
24 : ConfigurationPolicyProvider(policy_list), | |
25 cache_(cache), | |
26 level_(level) {} | |
27 virtual ~CloudPolicyProvider() {} | |
28 | |
29 virtual bool Provide(ConfigurationPolicyStoreInterface* store) { | |
30 if (level_ == POLICY_LEVEL_MANDATORY) | |
31 ApplyPolicyMap(&cache_->mandatory_policy_, store); | |
32 else if (level_ == POLICY_LEVEL_RECOMMENDED) | |
33 ApplyPolicyMap(&cache_->recommended_policy_, store); | |
34 return true; | |
35 } | |
36 | |
37 virtual bool IsInitializationComplete() const { | |
38 return cache_->initialization_complete_; | |
39 } | |
40 | |
41 virtual void AddObserver(ConfigurationPolicyProvider::Observer* observer) { | |
42 cache_->observer_list_.AddObserver(observer); | |
43 } | |
44 virtual void RemoveObserver(ConfigurationPolicyProvider::Observer* observer) { | |
45 cache_->observer_list_.RemoveObserver(observer); | |
46 } | |
47 | |
48 private: | |
49 // The underlying policy cache. | |
50 CloudPolicyCacheBase* cache_; | |
51 // Policy level this provider will handle. | |
52 CloudPolicyCacheBase::PolicyLevel level_; | |
53 | |
54 DISALLOW_COPY_AND_ASSIGN(CloudPolicyProvider); | |
55 }; | |
56 | |
57 CloudPolicyCacheBase::CloudPolicyCacheBase() | 15 CloudPolicyCacheBase::CloudPolicyCacheBase() |
58 : notifier_(NULL), | 16 : notifier_(NULL), |
59 initialization_complete_(false), | 17 initialization_complete_(false), |
60 is_unmanaged_(false) { | 18 is_unmanaged_(false) { |
61 public_key_version_.valid = false; | 19 public_key_version_.valid = false; |
62 managed_policy_provider_.reset( | |
63 new CloudPolicyProvider( | |
64 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), | |
65 this, | |
66 POLICY_LEVEL_MANDATORY)); | |
67 recommended_policy_provider_.reset( | |
68 new CloudPolicyProvider( | |
69 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), | |
70 this, | |
71 POLICY_LEVEL_RECOMMENDED)); | |
72 } | 20 } |
73 | 21 |
74 CloudPolicyCacheBase::~CloudPolicyCacheBase() { | 22 CloudPolicyCacheBase::~CloudPolicyCacheBase() { |
75 FOR_EACH_OBSERVER(ConfigurationPolicyProvider::Observer, | 23 FOR_EACH_OBSERVER(Observer, observer_list_, OnCacheGoingAway(this)); |
76 observer_list_, OnProviderGoingAway()); | 24 } |
| 25 |
| 26 void CloudPolicyCacheBase::AddObserver(Observer* observer) { |
| 27 observer_list_.AddObserver(observer); |
| 28 } |
| 29 |
| 30 void CloudPolicyCacheBase::RemoveObserver(Observer* observer) { |
| 31 observer_list_.RemoveObserver(observer); |
| 32 } |
| 33 |
| 34 const PolicyMap* CloudPolicyCacheBase::policy(PolicyLevel level) { |
| 35 switch (level) { |
| 36 case POLICY_LEVEL_MANDATORY: |
| 37 return &mandatory_policy_; |
| 38 case POLICY_LEVEL_RECOMMENDED: |
| 39 return &recommended_policy_; |
| 40 default: |
| 41 NOTREACHED(); |
| 42 } |
| 43 return NULL; |
77 } | 44 } |
78 | 45 |
79 bool CloudPolicyCacheBase::GetPublicKeyVersion(int* version) { | 46 bool CloudPolicyCacheBase::GetPublicKeyVersion(int* version) { |
80 if (public_key_version_.valid) | 47 if (public_key_version_.valid) |
81 *version = public_key_version_.version; | 48 *version = public_key_version_.version; |
82 | 49 |
83 return public_key_version_.valid; | 50 return public_key_version_.valid; |
84 } | 51 } |
85 | 52 |
86 bool CloudPolicyCacheBase::SetPolicyInternal( | 53 bool CloudPolicyCacheBase::SetPolicyInternal( |
(...skipping 25 matching lines...) Expand all Loading... |
112 public_key_version_.valid = temp_public_key_version.valid; | 79 public_key_version_.valid = temp_public_key_version.valid; |
113 | 80 |
114 const bool new_policy_differs = | 81 const bool new_policy_differs = |
115 !mandatory_policy_.Equals(mandatory_policy) || | 82 !mandatory_policy_.Equals(mandatory_policy) || |
116 !recommended_policy_.Equals(recommended_policy); | 83 !recommended_policy_.Equals(recommended_policy); |
117 mandatory_policy_.Swap(&mandatory_policy); | 84 mandatory_policy_.Swap(&mandatory_policy); |
118 recommended_policy_.Swap(&recommended_policy); | 85 recommended_policy_.Swap(&recommended_policy); |
119 initialization_complete_ = true; | 86 initialization_complete_ = true; |
120 | 87 |
121 if (new_policy_differs || initialization_was_not_complete) { | 88 if (new_policy_differs || initialization_was_not_complete) { |
122 FOR_EACH_OBSERVER(ConfigurationPolicyProvider::Observer, | 89 FOR_EACH_OBSERVER(Observer, observer_list_, OnCacheUpdate(this)); |
123 observer_list_, OnUpdatePolicy()); | |
124 } | 90 } |
125 InformNotifier(CloudPolicySubsystem::SUCCESS, | 91 InformNotifier(CloudPolicySubsystem::SUCCESS, |
126 CloudPolicySubsystem::NO_DETAILS); | 92 CloudPolicySubsystem::NO_DETAILS); |
127 return true; | 93 return true; |
128 } | 94 } |
129 | 95 |
130 void CloudPolicyCacheBase::SetUnmanagedInternal(const base::Time& timestamp) { | 96 void CloudPolicyCacheBase::SetUnmanagedInternal(const base::Time& timestamp) { |
131 is_unmanaged_ = true; | 97 is_unmanaged_ = true; |
132 initialization_complete_ = true; | 98 initialization_complete_ = true; |
133 public_key_version_.valid = false; | 99 public_key_version_.valid = false; |
134 mandatory_policy_.Clear(); | 100 mandatory_policy_.Clear(); |
135 recommended_policy_.Clear(); | 101 recommended_policy_.Clear(); |
136 last_policy_refresh_time_ = timestamp; | 102 last_policy_refresh_time_ = timestamp; |
137 | 103 |
138 FOR_EACH_OBSERVER(ConfigurationPolicyProvider::Observer, | 104 FOR_EACH_OBSERVER(Observer, observer_list_, OnCacheUpdate(this)); |
139 observer_list_, OnUpdatePolicy()); | |
140 } | |
141 | |
142 ConfigurationPolicyProvider* CloudPolicyCacheBase::GetManagedPolicyProvider() { | |
143 DCHECK(CalledOnValidThread()); | |
144 return managed_policy_provider_.get(); | |
145 } | |
146 | |
147 ConfigurationPolicyProvider* | |
148 CloudPolicyCacheBase::GetRecommendedPolicyProvider() { | |
149 DCHECK(CalledOnValidThread()); | |
150 return recommended_policy_provider_.get(); | |
151 } | 105 } |
152 | 106 |
153 bool CloudPolicyCacheBase::DecodePolicyResponse( | 107 bool CloudPolicyCacheBase::DecodePolicyResponse( |
154 const em::PolicyFetchResponse& policy_response, | 108 const em::PolicyFetchResponse& policy_response, |
155 PolicyMap* mandatory, | 109 PolicyMap* mandatory, |
156 PolicyMap* recommended, | 110 PolicyMap* recommended, |
157 base::Time* timestamp, | 111 base::Time* timestamp, |
158 PublicKeyVersion* public_key_version) { | 112 PublicKeyVersion* public_key_version) { |
159 std::string data = policy_response.policy_data(); | 113 std::string data = policy_response.policy_data(); |
160 em::PolicyData policy_data; | 114 em::PolicyData policy_data; |
(...skipping 17 matching lines...) Expand all Loading... |
178 void CloudPolicyCacheBase::InformNotifier( | 132 void CloudPolicyCacheBase::InformNotifier( |
179 CloudPolicySubsystem::PolicySubsystemState state, | 133 CloudPolicySubsystem::PolicySubsystemState state, |
180 CloudPolicySubsystem::ErrorDetails error_details) { | 134 CloudPolicySubsystem::ErrorDetails error_details) { |
181 // TODO(jkummerow): To obsolete this NULL-check, make all uses of | 135 // TODO(jkummerow): To obsolete this NULL-check, make all uses of |
182 // UserPolicyCache explicitly set a notifier using |set_policy_notifier()|. | 136 // UserPolicyCache explicitly set a notifier using |set_policy_notifier()|. |
183 if (notifier_) | 137 if (notifier_) |
184 notifier_->Inform(state, error_details, PolicyNotifier::POLICY_CACHE); | 138 notifier_->Inform(state, error_details, PolicyNotifier::POLICY_CACHE); |
185 } | 139 } |
186 | 140 |
187 } // namespace policy | 141 } // namespace policy |
OLD | NEW |