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 it = domain_map_.begin(); |
| 161 it != domain_map_.end(); ++it) { |
| 162 it->second->FilterBundle(bundle.get()); |
| 163 } |
| 164 |
141 content::BrowserThread::PostTask( | 165 content::BrowserThread::PostTask( |
142 content::BrowserThread::UI, FROM_HERE, | 166 content::BrowserThread::UI, FROM_HERE, |
143 base::Bind(&ComponentCloudPolicyService::OnPolicyUpdated, | 167 base::Bind(&ComponentCloudPolicyService::OnPolicyUpdated, |
144 service_, | 168 service_, |
145 base::Passed(&bundle))); | 169 base::Passed(&bundle))); |
146 } | 170 } |
147 | 171 |
148 void ComponentCloudPolicyService::Backend::SetCurrentComponents( | 172 void ComponentCloudPolicyService::Backend::RegisterPolicyDomain( |
149 PolicyDomain domain, | 173 scoped_refptr<const PolicyDomainDescriptor> descriptor) { |
150 const StringSet* components) { | |
151 // Store the current list of components in the cache. | 174 // Store the current list of components in the cache. |
| 175 StringSet ids; |
152 std::string policy_type; | 176 std::string policy_type; |
153 if (ComponentCloudPolicyStore::GetPolicyType(domain, &policy_type)) { | 177 if (ComponentCloudPolicyStore::GetPolicyType(descriptor->domain(), |
| 178 &policy_type)) { |
| 179 GetComponentIds(descriptor, &ids); |
154 Pickle pickle; | 180 Pickle pickle; |
155 for (StringSet::const_iterator it = components->begin(); | 181 for (StringSet::const_iterator it = ids.begin(); it != ids.end(); ++it) |
156 it != components->end(); ++it) { | |
157 pickle.WriteString(*it); | 182 pickle.WriteString(*it); |
158 } | |
159 std::string data(reinterpret_cast<const char*>(pickle.data()), | 183 std::string data(reinterpret_cast<const char*>(pickle.data()), |
160 pickle.size()); | 184 pickle.size()); |
161 cache_->Store(kComponentNamespaceCache, policy_type, data); | 185 cache_->Store(kComponentNamespaceCache, policy_type, data); |
162 } | 186 } |
163 | 187 |
| 188 domain_map_[descriptor->domain()] = descriptor; |
| 189 |
164 // Purge any components that have been removed. | 190 // Purge any components that have been removed. |
165 if (store_) | 191 if (store_) |
166 store_->Purge(domain, *components); | 192 store_->Purge(descriptor->domain(), ids); |
167 } | 193 } |
168 | 194 |
169 scoped_ptr<ComponentCloudPolicyService::ComponentMap> | 195 scoped_ptr<ComponentCloudPolicyService::ComponentMap> |
170 ComponentCloudPolicyService::Backend::ReadCachedComponents() { | 196 ComponentCloudPolicyService::Backend::ReadCachedComponents() { |
171 scoped_ptr<ComponentMap> components(new ComponentMap); | 197 scoped_ptr<ComponentMap> components(new ComponentMap); |
172 std::map<std::string, std::string> contents; | 198 std::map<std::string, std::string> contents; |
173 cache_->LoadAllSubkeys(kComponentNamespaceCache, &contents); | 199 cache_->LoadAllSubkeys(kComponentNamespaceCache, &contents); |
174 for (std::map<std::string, std::string>::iterator it = contents.begin(); | 200 for (std::map<std::string, std::string>::iterator it = contents.begin(); |
175 it != contents.end(); ++it) { | 201 it != contents.end(); ++it) { |
176 PolicyDomain domain; | 202 PolicyDomain domain; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 client_->RemoveObserver(this); | 281 client_->RemoveObserver(this); |
256 client_ = NULL; | 282 client_ = NULL; |
257 | 283 |
258 backend_task_runner_->PostTask( | 284 backend_task_runner_->PostTask( |
259 FROM_HERE, | 285 FROM_HERE, |
260 base::Bind(&Backend::Disconnect, base::Unretained(backend_))); | 286 base::Bind(&Backend::Disconnect, base::Unretained(backend_))); |
261 } | 287 } |
262 } | 288 } |
263 | 289 |
264 void ComponentCloudPolicyService::RegisterPolicyDomain( | 290 void ComponentCloudPolicyService::RegisterPolicyDomain( |
265 PolicyDomain domain, | 291 scoped_refptr<const PolicyDomainDescriptor> descriptor) { |
266 const std::set<std::string>& current_ids) { | |
267 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 292 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
268 DCHECK(SupportsDomain(domain)); | 293 DCHECK(SupportsDomain(descriptor->domain())); |
269 | 294 |
270 // Send the new set to the backend, to purge the cache. | 295 // Send the new descriptor to the backend, to purge the cache. |
271 backend_task_runner_->PostTask( | 296 backend_task_runner_->PostTask(FROM_HERE, |
272 FROM_HERE, | 297 base::Bind(&Backend::RegisterPolicyDomain, |
273 base::Bind(&Backend::SetCurrentComponents, | 298 base::Unretained(backend_), |
274 base::Unretained(backend_), | 299 descriptor)); |
275 domain, | |
276 base::Owned(new StringSet(current_ids)))); | |
277 | 300 |
278 // Register the current list of components for |domain| at the |client_|. | 301 // Register the current list of components for |domain| at the |client_|. |
279 StringSet& registered_ids = registered_components_[domain]; | 302 StringSet current_ids; |
| 303 GetComponentIds(descriptor, ¤t_ids); |
| 304 StringSet& registered_ids = registered_components_[descriptor->domain()]; |
280 if (client_ && is_initialized()) { | 305 if (client_ && is_initialized()) { |
281 if (UpdateClientNamespaces(domain, registered_ids, current_ids)) | 306 if (UpdateClientNamespaces( |
| 307 descriptor->domain(), registered_ids, current_ids)) { |
282 delegate_->OnComponentCloudPolicyRefreshNeeded(); | 308 delegate_->OnComponentCloudPolicyRefreshNeeded(); |
| 309 } |
283 } | 310 } |
| 311 |
284 registered_ids = current_ids; | 312 registered_ids = current_ids; |
285 } | 313 } |
286 | 314 |
287 void ComponentCloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) { | 315 void ComponentCloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) { |
288 DCHECK_EQ(client_, client); | 316 DCHECK_EQ(client_, client); |
289 // Pass each PolicyFetchResponse whose policy type is registered to the | 317 // Pass each PolicyFetchResponse whose policy type is registered to the |
290 // Backend. | 318 // Backend. |
291 const CloudPolicyClient::ResponseMap& responses = client_->responses(); | 319 const CloudPolicyClient::ResponseMap& responses = client_->responses(); |
292 for (CloudPolicyClient::ResponseMap::const_iterator it = responses.begin(); | 320 for (CloudPolicyClient::ResponseMap::const_iterator it = responses.begin(); |
293 it != responses.end(); ++it) { | 321 it != responses.end(); ++it) { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 PolicyDomain domain, | 465 PolicyDomain domain, |
438 const StringSet& set) { | 466 const StringSet& set) { |
439 std::string policy_type; | 467 std::string policy_type; |
440 if (ComponentCloudPolicyStore::GetPolicyType(domain, &policy_type)) { | 468 if (ComponentCloudPolicyStore::GetPolicyType(domain, &policy_type)) { |
441 for (StringSet::const_iterator it = set.begin(); it != set.end(); ++it) | 469 for (StringSet::const_iterator it = set.begin(); it != set.end(); ++it) |
442 client_->RemoveNamespaceToFetch(PolicyNamespaceKey(policy_type, *it)); | 470 client_->RemoveNamespaceToFetch(PolicyNamespaceKey(policy_type, *it)); |
443 } | 471 } |
444 } | 472 } |
445 | 473 |
446 } // namespace policy | 474 } // namespace policy |
OLD | NEW |