OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/policy/cloud_policy_cache_base.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/values.h" | |
11 #include "chrome/browser/policy/configuration_policy_pref_store.h" | |
12 | |
13 namespace policy { | |
14 | |
15 // A thin ConfigurationPolicyProvider implementation sitting on top of | |
16 // CloudPolicyCache for hooking up with ConfigurationPolicyPrefStore. | |
Mattias Nissler (ping if slow)
2011/03/29 08:58:39
s/CloudPolicyCache/CloudPolicyCacheBase/
Jakob Kummerow
2011/03/29 09:33:37
Done.
| |
17 class CloudPolicyCacheBase::CloudPolicyProvider | |
18 : public ConfigurationPolicyProvider { | |
19 public: | |
20 CloudPolicyProvider(const PolicyDefinitionList* policy_list, | |
21 CloudPolicyCacheBase* cache, | |
22 CloudPolicyCacheBase::PolicyLevel level) | |
23 : ConfigurationPolicyProvider(policy_list), | |
24 cache_(cache), | |
25 level_(level) {} | |
26 virtual ~CloudPolicyProvider() {} | |
27 | |
28 virtual bool Provide(ConfigurationPolicyStoreInterface* store) { | |
29 if (level_ == POLICY_LEVEL_MANDATORY) | |
30 ApplyPolicyMap(&cache_->mandatory_policy_, store); | |
31 else if (level_ == POLICY_LEVEL_RECOMMENDED) | |
32 ApplyPolicyMap(&cache_->recommended_policy_, store); | |
33 return true; | |
34 } | |
35 | |
36 virtual bool IsInitializationComplete() const { | |
37 return cache_->initialization_complete_; | |
38 } | |
39 | |
40 virtual void AddObserver(ConfigurationPolicyProvider::Observer* observer) { | |
41 cache_->observer_list_.AddObserver(observer); | |
42 } | |
43 virtual void RemoveObserver(ConfigurationPolicyProvider::Observer* observer) { | |
44 cache_->observer_list_.RemoveObserver(observer); | |
45 } | |
46 | |
47 private: | |
48 // The underlying policy cache. | |
49 CloudPolicyCacheBase* cache_; | |
50 // Policy level this provider will handle. | |
51 CloudPolicyCacheBase::PolicyLevel level_; | |
52 | |
53 DISALLOW_COPY_AND_ASSIGN(CloudPolicyProvider); | |
54 }; | |
55 | |
56 CloudPolicyCacheBase::CloudPolicyCacheBase() | |
57 : initialization_complete_(false), | |
58 is_unmanaged_(false) { | |
59 managed_policy_provider_.reset( | |
60 new CloudPolicyProvider( | |
61 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), | |
62 this, | |
63 POLICY_LEVEL_MANDATORY)); | |
64 recommended_policy_provider_.reset( | |
65 new CloudPolicyProvider( | |
66 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), | |
67 this, | |
68 POLICY_LEVEL_RECOMMENDED)); | |
69 } | |
70 | |
71 CloudPolicyCacheBase::~CloudPolicyCacheBase() { | |
72 FOR_EACH_OBSERVER(ConfigurationPolicyProvider::Observer, | |
73 observer_list_, OnProviderGoingAway()); | |
74 } | |
75 | |
76 bool CloudPolicyCacheBase::SetPolicyInternal( | |
77 const em::PolicyFetchResponse& policy, | |
78 base::Time* timestamp, | |
79 bool check_for_timestamp_validity) { | |
80 DCHECK(CalledOnValidThread()); | |
81 base::Time temp_timestamp; | |
82 if (!timestamp && check_for_timestamp_validity) | |
83 timestamp = &temp_timestamp; | |
Mattias Nissler (ping if slow)
2011/03/29 08:58:39
Why not just DCHECK(timestamp)?
Jakob Kummerow
2011/03/29 09:33:37
Because we need a non-NULL |timestamp| to perform
Mattias Nissler (ping if slow)
2011/03/29 10:25:48
This is a nit, so I'd be happy either way.
My poi
| |
84 bool initialization_was_not_complete = !initialization_complete_; | |
85 is_unmanaged_ = false; | |
86 PolicyMap mandatory_policy; | |
87 PolicyMap recommended_policy; | |
88 bool ok = DecodePolicyResponse(policy, &mandatory_policy, &recommended_policy, | |
89 timestamp); | |
90 if (!ok) { | |
91 LOG(WARNING) << "Decoding policy data failed."; | |
92 return false; | |
93 } | |
94 if (timestamp && check_for_timestamp_validity && | |
95 *timestamp > base::Time::NowFromSystemTime()) { | |
96 LOG(WARNING) << "Rejected policy data, file is from the future."; | |
97 return false; | |
98 } | |
99 | |
100 const bool new_policy_differs = | |
101 !mandatory_policy_.Equals(mandatory_policy) || | |
102 !recommended_policy_.Equals(recommended_policy); | |
103 mandatory_policy_.Swap(&mandatory_policy); | |
104 recommended_policy_.Swap(&recommended_policy); | |
105 initialization_complete_ = true; | |
106 | |
107 if (new_policy_differs || initialization_was_not_complete) { | |
108 FOR_EACH_OBSERVER(ConfigurationPolicyProvider::Observer, | |
109 observer_list_, OnUpdatePolicy()); | |
110 } | |
111 return true; | |
112 } | |
113 | |
114 void CloudPolicyCacheBase::SetUnmanagedInternal(const base::Time& timestamp) { | |
115 is_unmanaged_ = true; | |
116 initialization_complete_ = true; | |
117 mandatory_policy_.Clear(); | |
118 recommended_policy_.Clear(); | |
119 last_policy_refresh_time_ = timestamp; | |
120 | |
121 FOR_EACH_OBSERVER(ConfigurationPolicyProvider::Observer, | |
122 observer_list_, OnUpdatePolicy()); | |
123 } | |
124 | |
125 ConfigurationPolicyProvider* CloudPolicyCacheBase::GetManagedPolicyProvider() { | |
126 DCHECK(CalledOnValidThread()); | |
127 return managed_policy_provider_.get(); | |
128 } | |
129 | |
130 ConfigurationPolicyProvider* | |
131 CloudPolicyCacheBase::GetRecommendedPolicyProvider() { | |
132 DCHECK(CalledOnValidThread()); | |
133 return recommended_policy_provider_.get(); | |
134 } | |
135 | |
136 bool CloudPolicyCacheBase::DecodePolicyResponse( | |
137 const em::PolicyFetchResponse& policy_response, | |
138 PolicyMap* mandatory, | |
139 PolicyMap* recommended, | |
140 base::Time* timestamp) { | |
141 std::string data = policy_response.policy_data(); | |
142 em::PolicyData policy_data; | |
143 if (!policy_data.ParseFromString(data)) { | |
144 LOG(WARNING) << "Failed to parse PolicyData protobuf."; | |
145 return false; | |
146 } | |
147 if (timestamp) { | |
148 *timestamp = base::Time::UnixEpoch() + | |
149 base::TimeDelta::FromMilliseconds(policy_data.timestamp()); | |
150 } | |
151 return DecodePolicyData(policy_data, mandatory, recommended); | |
152 } | |
153 | |
154 } // namespace policy | |
OLD | NEW |