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 "chrome/browser/policy/cloud/component_cloud_policy_service.h" | 5 #include "chrome/browser/policy/cloud/component_cloud_policy_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/message_loop/message_loop_proxy.h" | 11 #include "base/message_loop/message_loop_proxy.h" |
12 #include "base/pickle.h" | 12 #include "base/pickle.h" |
13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "chrome/browser/policy/cloud/component_cloud_policy_store.h" | 15 #include "chrome/browser/policy/cloud/component_cloud_policy_store.h" |
16 #include "chrome/browser/policy/cloud/component_cloud_policy_updater.h" | 16 #include "chrome/browser/policy/cloud/component_cloud_policy_updater.h" |
17 #include "chrome/browser/policy/cloud/resource_cache.h" | 17 #include "chrome/browser/policy/cloud/resource_cache.h" |
18 #include "chrome/browser/policy/policy_domain_descriptor.h" | |
18 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" | 19 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" |
19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
20 #include "net/url_request/url_request_context_getter.h" | 21 #include "net/url_request/url_request_context_getter.h" |
21 | 22 |
22 namespace em = enterprise_management; | 23 namespace em = enterprise_management; |
23 | 24 |
24 namespace policy { | 25 namespace policy { |
25 | 26 |
27 namespace { | |
28 | |
29 void GetComponentIds(scoped_refptr<const PolicyDomainDescriptor>& descriptor, | |
30 std::set<std::string>* set) { | |
31 const PolicyDomainDescriptor::SchemaMap& map = descriptor->components(); | |
32 for (PolicyDomainDescriptor::SchemaMap::const_iterator it = map.begin(); | |
33 it != map.end(); ++it) { | |
34 set->insert(it->first); | |
35 } | |
36 } | |
37 | |
38 } // namespace | |
39 | |
26 const char ComponentCloudPolicyService::kComponentNamespaceCache[] = | 40 const char ComponentCloudPolicyService::kComponentNamespaceCache[] = |
27 "component-namespace-cache"; | 41 "component-namespace-cache"; |
28 | 42 |
29 ComponentCloudPolicyService::Delegate::~Delegate() {} | 43 ComponentCloudPolicyService::Delegate::~Delegate() {} |
30 | 44 |
31 // Owns the objects that live on the background thread, and posts back to UI | 45 // Owns the objects that live on the background thread, and posts back to UI |
32 // to the service whenever the policy changes. | 46 // to the service whenever the policy changes. |
33 class ComponentCloudPolicyService::Backend | 47 class ComponentCloudPolicyService::Backend |
34 : public ComponentCloudPolicyStore::Delegate { | 48 : public ComponentCloudPolicyStore::Delegate { |
35 public: | 49 public: |
(...skipping 20 matching lines...) Expand all Loading... | |
56 void SetCredentials(const std::string& username, const std::string& dm_token); | 70 void SetCredentials(const std::string& username, const std::string& dm_token); |
57 | 71 |
58 // Passes a policy protobuf to the backend, to start its validation and | 72 // Passes a policy protobuf to the backend, to start its validation and |
59 // eventual download of the policy data on the background thread. | 73 // eventual download of the policy data on the background thread. |
60 // This is ignored if the backend isn't connected. | 74 // This is ignored if the backend isn't connected. |
61 void UpdateExternalPolicy(scoped_ptr<em::PolicyFetchResponse> response); | 75 void UpdateExternalPolicy(scoped_ptr<em::PolicyFetchResponse> response); |
62 | 76 |
63 // ComponentCloudPolicyStore::Delegate implementation: | 77 // ComponentCloudPolicyStore::Delegate implementation: |
64 virtual void OnComponentCloudPolicyStoreUpdated() OVERRIDE; | 78 virtual void OnComponentCloudPolicyStoreUpdated() OVERRIDE; |
65 | 79 |
66 // Passes the current list of components in |domain|, so that the disk cache | 80 // Passes the current descriptor of a domain, so that the disk cache |
67 // can purge components that aren't being tracked anymore. | 81 // can purge components that aren't being tracked anymore. |
68 void SetCurrentComponents(PolicyDomain domain, const StringSet* components); | 82 void RegisterPolicyDomain( |
83 scoped_refptr<const PolicyDomainDescriptor> descriptor); | |
69 | 84 |
70 private: | 85 private: |
86 typedef std::map<PolicyDomain, scoped_refptr<const PolicyDomainDescriptor> > | |
87 DomainMap; | |
88 | |
71 scoped_ptr<ComponentMap> ReadCachedComponents(); | 89 scoped_ptr<ComponentMap> ReadCachedComponents(); |
72 | 90 |
73 base::WeakPtr<ComponentCloudPolicyService> service_; | 91 base::WeakPtr<ComponentCloudPolicyService> service_; |
74 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 92 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
75 scoped_ptr<ResourceCache> cache_; | 93 scoped_ptr<ResourceCache> cache_; |
76 scoped_ptr<ComponentCloudPolicyStore> store_; | 94 scoped_ptr<ComponentCloudPolicyStore> store_; |
77 scoped_ptr<ComponentCloudPolicyUpdater> updater_; | 95 scoped_ptr<ComponentCloudPolicyUpdater> updater_; |
96 DomainMap domain_map_; | |
78 | 97 |
79 DISALLOW_COPY_AND_ASSIGN(Backend); | 98 DISALLOW_COPY_AND_ASSIGN(Backend); |
80 }; | 99 }; |
81 | 100 |
82 ComponentCloudPolicyService::Backend::Backend( | 101 ComponentCloudPolicyService::Backend::Backend( |
83 base::WeakPtr<ComponentCloudPolicyService> service, | 102 base::WeakPtr<ComponentCloudPolicyService> service, |
84 scoped_refptr<base::SequencedTaskRunner> task_runner, | 103 scoped_refptr<base::SequencedTaskRunner> task_runner, |
85 scoped_ptr<ResourceCache> cache) | 104 scoped_ptr<ResourceCache> cache) |
86 : service_(service), | 105 : service_(service), |
87 task_runner_(task_runner), | 106 task_runner_(task_runner), |
88 cache_(cache.Pass()) {} | 107 cache_(cache.Pass()) {} |
89 | 108 |
90 ComponentCloudPolicyService::Backend::~Backend() {} | 109 ComponentCloudPolicyService::Backend::~Backend() {} |
91 | 110 |
92 void ComponentCloudPolicyService::Backend::Init() { | 111 void ComponentCloudPolicyService::Backend::Init() { |
93 DCHECK(!store_); | 112 DCHECK(!store_); |
94 store_.reset(new ComponentCloudPolicyStore(this, cache_.get())); | 113 store_.reset(new ComponentCloudPolicyStore(this, cache_.get())); |
95 } | 114 } |
96 | 115 |
97 void ComponentCloudPolicyService::Backend::FinalizeInit() { | 116 void ComponentCloudPolicyService::Backend::FinalizeInit() { |
98 // Read the components that were cached in the last SetCurrentComponents() | 117 // Read the components that were cached in the last RegisterPolicyDomain() |
99 // calls for each domain. | 118 // calls for each domain. |
100 scoped_ptr<ComponentMap> components = ReadCachedComponents(); | 119 scoped_ptr<ComponentMap> components = ReadCachedComponents(); |
101 | 120 |
102 // Read the initial policy. | 121 // Read the initial policy. |
103 store_->Load(); | 122 store_->Load(); |
104 scoped_ptr<PolicyBundle> policy(new PolicyBundle); | 123 scoped_ptr<PolicyBundle> policy(new PolicyBundle); |
105 policy->CopyFrom(store_->policy()); | 124 policy->CopyFrom(store_->policy()); |
106 | 125 |
107 content::BrowserThread::PostTask( | 126 content::BrowserThread::PostTask( |
108 content::BrowserThread::UI, FROM_HERE, | 127 content::BrowserThread::UI, FROM_HERE, |
(...skipping 22 matching lines...) Expand all Loading... | |
131 void ComponentCloudPolicyService::Backend::UpdateExternalPolicy( | 150 void ComponentCloudPolicyService::Backend::UpdateExternalPolicy( |
132 scoped_ptr<em::PolicyFetchResponse> response) { | 151 scoped_ptr<em::PolicyFetchResponse> response) { |
133 if (updater_) | 152 if (updater_) |
134 updater_->UpdateExternalPolicy(response.Pass()); | 153 updater_->UpdateExternalPolicy(response.Pass()); |
135 } | 154 } |
136 | 155 |
137 void ComponentCloudPolicyService::Backend:: | 156 void ComponentCloudPolicyService::Backend:: |
138 OnComponentCloudPolicyStoreUpdated() { | 157 OnComponentCloudPolicyStoreUpdated() { |
139 scoped_ptr<PolicyBundle> bundle(new PolicyBundle); | 158 scoped_ptr<PolicyBundle> bundle(new PolicyBundle); |
140 bundle->CopyFrom(store_->policy()); | 159 bundle->CopyFrom(store_->policy()); |
160 for (DomainMap::iterator i = domain_map_.begin(); i != domain_map_.end(); ++i) | |
161 i->second->FilterBundle(bundle.get()); | |
Mattias Nissler (ping if slow)
2013/05/15 10:24:06
Similar comment regarding surfacing typos/unsuppor
Joao da Silva
2013/05/19 13:17:55
This only applies to component policy, and the bun
| |
162 | |
141 content::BrowserThread::PostTask( | 163 content::BrowserThread::PostTask( |
142 content::BrowserThread::UI, FROM_HERE, | 164 content::BrowserThread::UI, FROM_HERE, |
143 base::Bind(&ComponentCloudPolicyService::OnPolicyUpdated, | 165 base::Bind(&ComponentCloudPolicyService::OnPolicyUpdated, |
144 service_, | 166 service_, |
145 base::Passed(&bundle))); | 167 base::Passed(&bundle))); |
146 } | 168 } |
147 | 169 |
148 void ComponentCloudPolicyService::Backend::SetCurrentComponents( | 170 void ComponentCloudPolicyService::Backend::RegisterPolicyDomain( |
149 PolicyDomain domain, | 171 scoped_refptr<const PolicyDomainDescriptor> descriptor) { |
150 const StringSet* components) { | |
151 // Store the current list of components in the cache. | 172 // Store the current list of components in the cache. |
173 StringSet ids; | |
152 std::string policy_type; | 174 std::string policy_type; |
153 if (ComponentCloudPolicyStore::GetPolicyType(domain, &policy_type)) { | 175 if (ComponentCloudPolicyStore::GetPolicyType(descriptor->domain(), |
176 &policy_type)) { | |
177 GetComponentIds(descriptor, &ids); | |
154 Pickle pickle; | 178 Pickle pickle; |
155 for (StringSet::const_iterator it = components->begin(); | 179 for (StringSet::iterator it = ids.begin(); it != ids.end(); ++it) |
156 it != components->end(); ++it) { | |
157 pickle.WriteString(*it); | 180 pickle.WriteString(*it); |
158 } | |
159 std::string data(reinterpret_cast<const char*>(pickle.data()), | 181 std::string data(reinterpret_cast<const char*>(pickle.data()), |
160 pickle.size()); | 182 pickle.size()); |
161 cache_->Store(kComponentNamespaceCache, policy_type, data); | 183 cache_->Store(kComponentNamespaceCache, policy_type, data); |
162 } | 184 } |
163 | 185 |
186 domain_map_[descriptor->domain()] = descriptor; | |
187 | |
164 // Purge any components that have been removed. | 188 // Purge any components that have been removed. |
165 if (store_) | 189 if (store_) |
166 store_->Purge(domain, *components); | 190 store_->Purge(descriptor->domain(), ids); |
167 } | 191 } |
168 | 192 |
169 scoped_ptr<ComponentCloudPolicyService::ComponentMap> | 193 scoped_ptr<ComponentCloudPolicyService::ComponentMap> |
170 ComponentCloudPolicyService::Backend::ReadCachedComponents() { | 194 ComponentCloudPolicyService::Backend::ReadCachedComponents() { |
171 scoped_ptr<ComponentMap> components(new ComponentMap); | 195 scoped_ptr<ComponentMap> components(new ComponentMap); |
172 std::map<std::string, std::string> contents; | 196 std::map<std::string, std::string> contents; |
173 cache_->LoadAllSubkeys(kComponentNamespaceCache, &contents); | 197 cache_->LoadAllSubkeys(kComponentNamespaceCache, &contents); |
174 for (std::map<std::string, std::string>::iterator it = contents.begin(); | 198 for (std::map<std::string, std::string>::iterator it = contents.begin(); |
175 it != contents.end(); ++it) { | 199 it != contents.end(); ++it) { |
176 PolicyDomain domain; | 200 PolicyDomain domain; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
255 client_->RemoveObserver(this); | 279 client_->RemoveObserver(this); |
256 client_ = NULL; | 280 client_ = NULL; |
257 | 281 |
258 backend_task_runner_->PostTask( | 282 backend_task_runner_->PostTask( |
259 FROM_HERE, | 283 FROM_HERE, |
260 base::Bind(&Backend::Disconnect, base::Unretained(backend_))); | 284 base::Bind(&Backend::Disconnect, base::Unretained(backend_))); |
261 } | 285 } |
262 } | 286 } |
263 | 287 |
264 void ComponentCloudPolicyService::RegisterPolicyDomain( | 288 void ComponentCloudPolicyService::RegisterPolicyDomain( |
265 PolicyDomain domain, | 289 scoped_refptr<const PolicyDomainDescriptor> descriptor) { |
266 const std::set<std::string>& current_ids) { | |
267 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 290 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
268 DCHECK(SupportsDomain(domain)); | 291 DCHECK(SupportsDomain(descriptor->domain())); |
269 | 292 |
270 // Send the new set to the backend, to purge the cache. | 293 // Send the new descriptor to the backend, to purge the cache. |
271 backend_task_runner_->PostTask( | 294 backend_task_runner_->PostTask(FROM_HERE, |
272 FROM_HERE, | 295 base::Bind(&Backend::RegisterPolicyDomain, |
273 base::Bind(&Backend::SetCurrentComponents, | 296 base::Unretained(backend_), |
274 base::Unretained(backend_), | 297 descriptor)); |
275 domain, | |
276 base::Owned(new StringSet(current_ids)))); | |
277 | 298 |
278 // Register the current list of components for |domain| at the |client_|. | 299 // Register the current list of components for |domain| at the |client_|. |
279 StringSet& registered_ids = registered_components_[domain]; | 300 StringSet current_ids; |
301 GetComponentIds(descriptor, ¤t_ids); | |
302 StringSet& previous_ids = registered_components_[descriptor->domain()]; | |
280 if (client_ && is_initialized()) { | 303 if (client_ && is_initialized()) { |
281 if (UpdateClientNamespaces(domain, registered_ids, current_ids)) | 304 if (UpdateClientNamespaces(descriptor->domain(), previous_ids, current_ids)) |
282 delegate_->OnComponentCloudPolicyRefreshNeeded(); | 305 delegate_->OnComponentCloudPolicyRefreshNeeded(); |
283 } | 306 } |
284 registered_ids = current_ids; | 307 |
308 previous_ids = current_ids; | |
285 } | 309 } |
286 | 310 |
287 void ComponentCloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) { | 311 void ComponentCloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) { |
288 DCHECK_EQ(client_, client); | 312 DCHECK_EQ(client_, client); |
289 // Pass each PolicyFetchResponse whose policy type is registered to the | 313 // Pass each PolicyFetchResponse whose policy type is registered to the |
290 // Backend. | 314 // Backend. |
291 const CloudPolicyClient::ResponseMap& responses = client_->responses(); | 315 const CloudPolicyClient::ResponseMap& responses = client_->responses(); |
292 for (CloudPolicyClient::ResponseMap::const_iterator it = responses.begin(); | 316 for (CloudPolicyClient::ResponseMap::const_iterator it = responses.begin(); |
293 it != responses.end(); ++it) { | 317 it != responses.end(); ++it) { |
294 const PolicyNamespaceKey& key(it->first); | 318 const PolicyNamespaceKey& key(it->first); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
437 PolicyDomain domain, | 461 PolicyDomain domain, |
438 const StringSet& set) { | 462 const StringSet& set) { |
439 std::string policy_type; | 463 std::string policy_type; |
440 if (ComponentCloudPolicyStore::GetPolicyType(domain, &policy_type)) { | 464 if (ComponentCloudPolicyStore::GetPolicyType(domain, &policy_type)) { |
441 for (StringSet::const_iterator it = set.begin(); it != set.end(); ++it) | 465 for (StringSet::const_iterator it = set.begin(); it != set.end(); ++it) |
442 client_->RemoveNamespaceToFetch(PolicyNamespaceKey(policy_type, *it)); | 466 client_->RemoveNamespaceToFetch(PolicyNamespaceKey(policy_type, *it)); |
443 } | 467 } |
444 } | 468 } |
445 | 469 |
446 } // namespace policy | 470 } // namespace policy |
OLD | NEW |