OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "components/policy/core/common/cloud/component_cloud_policy_store.h" | 5 #include "components/policy/core/common/cloud/component_cloud_policy_store.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <utility> | 10 #include <utility> |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 | 75 |
76 } // namespace | 76 } // namespace |
77 | 77 |
78 ComponentCloudPolicyStore::Delegate::~Delegate() {} | 78 ComponentCloudPolicyStore::Delegate::~Delegate() {} |
79 | 79 |
80 ComponentCloudPolicyStore::ComponentCloudPolicyStore(Delegate* delegate, | 80 ComponentCloudPolicyStore::ComponentCloudPolicyStore(Delegate* delegate, |
81 ResourceCache* cache) | 81 ResourceCache* cache) |
82 : delegate_(delegate), cache_(cache) { | 82 : delegate_(delegate), cache_(cache) { |
83 // Allow the store to be created on a different thread than the thread that | 83 // Allow the store to be created on a different thread than the thread that |
84 // will end up using it. | 84 // will end up using it. |
85 DetachFromThread(); | 85 DETACH_FROM_SEQUENCE(sequence_checker_); |
86 } | 86 } |
87 | 87 |
88 ComponentCloudPolicyStore::~ComponentCloudPolicyStore() { | 88 ComponentCloudPolicyStore::~ComponentCloudPolicyStore() { |
89 DCHECK(CalledOnValidThread()); | 89 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
90 } | 90 } |
91 | 91 |
92 // static | 92 // static |
93 bool ComponentCloudPolicyStore::SupportsDomain(PolicyDomain domain) { | 93 bool ComponentCloudPolicyStore::SupportsDomain(PolicyDomain domain) { |
94 return GetDomainConstants(domain) != nullptr; | 94 return GetDomainConstants(domain) != nullptr; |
95 } | 95 } |
96 | 96 |
97 // static | 97 // static |
98 bool ComponentCloudPolicyStore::GetPolicyType(PolicyDomain domain, | 98 bool ComponentCloudPolicyStore::GetPolicyType(PolicyDomain domain, |
99 std::string* policy_type) { | 99 std::string* policy_type) { |
100 const DomainConstants* constants = GetDomainConstants(domain); | 100 const DomainConstants* constants = GetDomainConstants(domain); |
101 if (constants) | 101 if (constants) |
102 *policy_type = constants->policy_type; | 102 *policy_type = constants->policy_type; |
103 return constants != nullptr; | 103 return constants != nullptr; |
104 } | 104 } |
105 | 105 |
106 // static | 106 // static |
107 bool ComponentCloudPolicyStore::GetPolicyDomain(const std::string& policy_type, | 107 bool ComponentCloudPolicyStore::GetPolicyDomain(const std::string& policy_type, |
108 PolicyDomain* domain) { | 108 PolicyDomain* domain) { |
109 const DomainConstants* constants = GetDomainConstantsForType(policy_type); | 109 const DomainConstants* constants = GetDomainConstantsForType(policy_type); |
110 if (constants) | 110 if (constants) |
111 *domain = constants->domain; | 111 *domain = constants->domain; |
112 return constants != nullptr; | 112 return constants != nullptr; |
113 } | 113 } |
114 | 114 |
115 const std::string& ComponentCloudPolicyStore::GetCachedHash( | 115 const std::string& ComponentCloudPolicyStore::GetCachedHash( |
116 const PolicyNamespace& ns) const { | 116 const PolicyNamespace& ns) const { |
117 DCHECK(CalledOnValidThread()); | 117 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
118 std::map<PolicyNamespace, std::string>::const_iterator it = | 118 std::map<PolicyNamespace, std::string>::const_iterator it = |
119 cached_hashes_.find(ns); | 119 cached_hashes_.find(ns); |
120 return it == cached_hashes_.end() ? base::EmptyString() : it->second; | 120 return it == cached_hashes_.end() ? base::EmptyString() : it->second; |
121 } | 121 } |
122 | 122 |
123 void ComponentCloudPolicyStore::SetCredentials(const std::string& username, | 123 void ComponentCloudPolicyStore::SetCredentials(const std::string& username, |
124 const std::string& dm_token, | 124 const std::string& dm_token, |
125 const std::string& device_id, | 125 const std::string& device_id, |
126 const std::string& public_key, | 126 const std::string& public_key, |
127 int public_key_version) { | 127 int public_key_version) { |
128 DCHECK(CalledOnValidThread()); | 128 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
129 DCHECK(username_.empty() || username == username_); | 129 DCHECK(username_.empty() || username == username_); |
130 DCHECK(dm_token_.empty() || dm_token == dm_token_); | 130 DCHECK(dm_token_.empty() || dm_token == dm_token_); |
131 DCHECK(device_id_.empty() || device_id == device_id_); | 131 DCHECK(device_id_.empty() || device_id == device_id_); |
132 username_ = username; | 132 username_ = username; |
133 dm_token_ = dm_token; | 133 dm_token_ = dm_token; |
134 device_id_ = device_id; | 134 device_id_ = device_id; |
135 public_key_ = public_key; | 135 public_key_ = public_key; |
136 public_key_version_ = public_key_version; | 136 public_key_version_ = public_key_version; |
137 } | 137 } |
138 | 138 |
139 void ComponentCloudPolicyStore::Load() { | 139 void ComponentCloudPolicyStore::Load() { |
140 DCHECK(CalledOnValidThread()); | 140 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
141 typedef std::map<std::string, std::string> ContentMap; | 141 typedef std::map<std::string, std::string> ContentMap; |
142 | 142 |
143 // Load all cached policy protobufs for each domain. | 143 // Load all cached policy protobufs for each domain. |
144 for (const DomainConstants& constants : kDomains) { | 144 for (const DomainConstants& constants : kDomains) { |
145 ContentMap protos; | 145 ContentMap protos; |
146 cache_->LoadAllSubkeys(constants.proto_cache_key, &protos); | 146 cache_->LoadAllSubkeys(constants.proto_cache_key, &protos); |
147 for (ContentMap::iterator it = protos.begin(); it != protos.end(); ++it) { | 147 for (ContentMap::iterator it = protos.begin(); it != protos.end(); ++it) { |
148 const std::string& id(it->first); | 148 const std::string& id(it->first); |
149 const PolicyNamespace ns(constants.domain, id); | 149 const PolicyNamespace ns(constants.domain, id); |
150 | 150 |
(...skipping 25 matching lines...) Expand all Loading... |
176 } | 176 } |
177 } | 177 } |
178 } | 178 } |
179 | 179 |
180 bool ComponentCloudPolicyStore::Store( | 180 bool ComponentCloudPolicyStore::Store( |
181 const PolicyNamespace& ns, | 181 const PolicyNamespace& ns, |
182 const std::string& serialized_policy, | 182 const std::string& serialized_policy, |
183 std::unique_ptr<em::PolicyData> policy_data, | 183 std::unique_ptr<em::PolicyData> policy_data, |
184 const std::string& secure_hash, | 184 const std::string& secure_hash, |
185 const std::string& data) { | 185 const std::string& data) { |
186 DCHECK(CalledOnValidThread()); | 186 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
187 const DomainConstants* constants = GetDomainConstants(ns.domain); | 187 const DomainConstants* constants = GetDomainConstants(ns.domain); |
188 PolicyMap policy; | 188 PolicyMap policy; |
189 // |serialized_policy| has already been validated; validate the data now. | 189 // |serialized_policy| has already been validated; validate the data now. |
190 if (!constants) | 190 if (!constants) |
191 return false; | 191 return false; |
192 if (!ValidateData(data, secure_hash, &policy)) | 192 if (!ValidateData(data, secure_hash, &policy)) |
193 return false; | 193 return false; |
194 | 194 |
195 // Flush the proto and the data to the cache. | 195 // Flush the proto and the data to the cache. |
196 cache_->Store(constants->proto_cache_key, ns.component_id, serialized_policy); | 196 cache_->Store(constants->proto_cache_key, ns.component_id, serialized_policy); |
197 cache_->Store(constants->data_cache_key, ns.component_id, data); | 197 cache_->Store(constants->data_cache_key, ns.component_id, data); |
198 // And expose the policy. | 198 // And expose the policy. |
199 policy_bundle_.Get(ns).Swap(&policy); | 199 policy_bundle_.Get(ns).Swap(&policy); |
200 cached_hashes_[ns] = secure_hash; | 200 cached_hashes_[ns] = secure_hash; |
201 stored_policy_times_[ns] = base::Time::FromJavaTime(policy_data->timestamp()); | 201 stored_policy_times_[ns] = base::Time::FromJavaTime(policy_data->timestamp()); |
202 delegate_->OnComponentCloudPolicyStoreUpdated(); | 202 delegate_->OnComponentCloudPolicyStoreUpdated(); |
203 return true; | 203 return true; |
204 } | 204 } |
205 | 205 |
206 void ComponentCloudPolicyStore::Delete(const PolicyNamespace& ns) { | 206 void ComponentCloudPolicyStore::Delete(const PolicyNamespace& ns) { |
207 DCHECK(CalledOnValidThread()); | 207 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
208 const DomainConstants* constants = GetDomainConstants(ns.domain); | 208 const DomainConstants* constants = GetDomainConstants(ns.domain); |
209 if (!constants) | 209 if (!constants) |
210 return; | 210 return; |
211 | 211 |
212 cache_->Delete(constants->proto_cache_key, ns.component_id); | 212 cache_->Delete(constants->proto_cache_key, ns.component_id); |
213 cache_->Delete(constants->data_cache_key, ns.component_id); | 213 cache_->Delete(constants->data_cache_key, ns.component_id); |
214 | 214 |
215 if (!policy_bundle_.Get(ns).empty()) { | 215 if (!policy_bundle_.Get(ns).empty()) { |
216 policy_bundle_.Get(ns).Clear(); | 216 policy_bundle_.Get(ns).Clear(); |
217 delegate_->OnComponentCloudPolicyStoreUpdated(); | 217 delegate_->OnComponentCloudPolicyStoreUpdated(); |
218 } | 218 } |
219 } | 219 } |
220 | 220 |
221 void ComponentCloudPolicyStore::Purge( | 221 void ComponentCloudPolicyStore::Purge( |
222 PolicyDomain domain, | 222 PolicyDomain domain, |
223 const ResourceCache::SubkeyFilter& filter) { | 223 const ResourceCache::SubkeyFilter& filter) { |
224 DCHECK(CalledOnValidThread()); | 224 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
225 const DomainConstants* constants = GetDomainConstants(domain); | 225 const DomainConstants* constants = GetDomainConstants(domain); |
226 if (!constants) | 226 if (!constants) |
227 return; | 227 return; |
228 | 228 |
229 cache_->FilterSubkeys(constants->proto_cache_key, filter); | 229 cache_->FilterSubkeys(constants->proto_cache_key, filter); |
230 cache_->FilterSubkeys(constants->data_cache_key, filter); | 230 cache_->FilterSubkeys(constants->data_cache_key, filter); |
231 | 231 |
232 // Stop serving policies for purged namespaces. | 232 // Stop serving policies for purged namespaces. |
233 bool purged_current_policies = false; | 233 bool purged_current_policies = false; |
234 for (PolicyBundle::const_iterator it = policy_bundle_.begin(); | 234 for (PolicyBundle::const_iterator it = policy_bundle_.begin(); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 // this must support a configurable scope; assuming POLICY_SCOPE_USER is | 398 // this must support a configurable scope; assuming POLICY_SCOPE_USER is |
399 // fine for now. | 399 // fine for now. |
400 policy->Set(it.key(), level, POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, | 400 policy->Set(it.key(), level, POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD, |
401 std::move(value), nullptr); | 401 std::move(value), nullptr); |
402 } | 402 } |
403 | 403 |
404 return true; | 404 return true; |
405 } | 405 } |
406 | 406 |
407 } // namespace policy | 407 } // namespace policy |
OLD | NEW |