Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: chrome/browser/chromeos/policy/device_local_account_policy_service.cc

Issue 342233005: Move ownership of the ComponentCloudPolicyService to the broker. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix clang build Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/chromeos/policy/device_local_account_policy_service.h" 5 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h"
bartfab (slow) 2014/06/20 09:44:23 Nit: Move this to the header file.
Joao da Silva 2014/06/20 15:36:07 Done.
11 #include "base/command_line.h"
10 #include "base/file_util.h" 12 #include "base/file_util.h"
11 #include "base/files/file_enumerator.h" 13 #include "base/files/file_enumerator.h"
12 #include "base/logging.h" 14 #include "base/logging.h"
13 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
14 #include "base/message_loop/message_loop_proxy.h" 16 #include "base/message_loop/message_loop_proxy.h"
15 #include "base/path_service.h" 17 #include "base/path_service.h"
16 #include "base/sequenced_task_runner.h" 18 #include "base/sequenced_task_runner.h"
17 #include "base/stl_util.h" 19 #include "base/stl_util.h"
18 #include "base/strings/string_number_conversions.h" 20 #include "base/strings/string_number_conversions.h"
19 #include "chrome/browser/browser_process.h" 21 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/chromeos/policy/device_local_account.h" 22 #include "chrome/browser/chromeos/policy/device_local_account.h"
21 #include "chrome/browser/chromeos/policy/device_local_account_external_data_serv ice.h" 23 #include "chrome/browser/chromeos/policy/device_local_account_external_data_serv ice.h"
22 #include "chrome/browser/chromeos/policy/device_local_account_policy_store.h" 24 #include "chrome/browser/chromeos/policy/device_local_account_policy_store.h"
23 #include "chrome/browser/chromeos/settings/device_settings_service.h" 25 #include "chrome/browser/chromeos/settings/device_settings_service.h"
26 #include "chrome/browser/policy/schema_registry_service.h"
24 #include "chrome/common/chrome_content_client.h" 27 #include "chrome/common/chrome_content_client.h"
25 #include "chromeos/chromeos_paths.h" 28 #include "chromeos/chromeos_paths.h"
26 #include "chromeos/dbus/session_manager_client.h" 29 #include "chromeos/dbus/session_manager_client.h"
27 #include "chromeos/settings/cros_settings_names.h" 30 #include "chromeos/settings/cros_settings_names.h"
28 #include "chromeos/settings/cros_settings_provider.h" 31 #include "chromeos/settings/cros_settings_provider.h"
32 #include "components/policy/core/browser/browser_policy_connector.h"
29 #include "components/policy/core/common/cloud/cloud_policy_client.h" 33 #include "components/policy/core/common/cloud/cloud_policy_client.h"
30 #include "components/policy/core/common/cloud/cloud_policy_constants.h" 34 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
31 #include "components/policy/core/common/cloud/cloud_policy_refresh_scheduler.h" 35 #include "components/policy/core/common/cloud/cloud_policy_refresh_scheduler.h"
32 #include "components/policy/core/common/cloud/device_management_service.h" 36 #include "components/policy/core/common/cloud/device_management_service.h"
37 #include "components/policy/core/common/cloud/resource_cache.h"
33 #include "components/policy/core/common/cloud/system_policy_request_context.h" 38 #include "components/policy/core/common/cloud/system_policy_request_context.h"
39 #include "components/policy/core/common/policy_switches.h"
34 #include "content/public/browser/browser_thread.h" 40 #include "content/public/browser/browser_thread.h"
35 #include "net/url_request/url_request_context_getter.h" 41 #include "net/url_request/url_request_context_getter.h"
36 #include "policy/policy_constants.h" 42 #include "policy/policy_constants.h"
37 #include "policy/proto/device_management_backend.pb.h" 43 #include "policy/proto/device_management_backend.pb.h"
38 #include "url/gurl.h" 44 #include "url/gurl.h"
39 45
40 namespace em = enterprise_management; 46 namespace em = enterprise_management;
41 47
42 namespace policy { 48 namespace policy {
43 49
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 base::DeleteFile(path, true); 115 base::DeleteFile(path, true);
110 } 116 }
111 117
112 } // namespace 118 } // namespace
113 119
114 DeviceLocalAccountPolicyBroker::DeviceLocalAccountPolicyBroker( 120 DeviceLocalAccountPolicyBroker::DeviceLocalAccountPolicyBroker(
115 const DeviceLocalAccount& account, 121 const DeviceLocalAccount& account,
116 const base::FilePath& component_policy_cache_path, 122 const base::FilePath& component_policy_cache_path,
117 scoped_ptr<DeviceLocalAccountPolicyStore> store, 123 scoped_ptr<DeviceLocalAccountPolicyStore> store,
118 scoped_refptr<DeviceLocalAccountExternalDataManager> external_data_manager, 124 scoped_refptr<DeviceLocalAccountExternalDataManager> external_data_manager,
125 const base::Closure& policy_update_callback,
119 const scoped_refptr<base::SequencedTaskRunner>& task_runner) 126 const scoped_refptr<base::SequencedTaskRunner>& task_runner)
120 : account_id_(account.account_id), 127 : account_id_(account.account_id),
121 user_id_(account.user_id), 128 user_id_(account.user_id),
122 component_policy_cache_path_(component_policy_cache_path), 129 component_policy_cache_path_(component_policy_cache_path),
123 store_(store.Pass()), 130 store_(store.Pass()),
124 external_data_manager_(external_data_manager), 131 external_data_manager_(external_data_manager),
125 core_(PolicyNamespaceKey(dm_protocol::kChromePublicAccountPolicyType, 132 core_(PolicyNamespaceKey(dm_protocol::kChromePublicAccountPolicyType,
126 store_->account_id()), 133 store_->account_id()),
127 store_.get(), 134 store_.get(),
128 task_runner) { 135 task_runner),
136 policy_update_callback_(policy_update_callback) {
129 base::FilePath cache_root_dir; 137 base::FilePath cache_root_dir;
130 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_EXTENSIONS, 138 CHECK(PathService::Get(chromeos::DIR_DEVICE_LOCAL_ACCOUNT_EXTENSIONS,
131 &cache_root_dir)); 139 &cache_root_dir));
132 extension_loader_ = new chromeos::DeviceLocalAccountExternalPolicyLoader( 140 extension_loader_ = new chromeos::DeviceLocalAccountExternalPolicyLoader(
133 store_.get(), cache_root_dir.Append(EncodeAccountId(account.account_id))); 141 store_.get(), cache_root_dir.Append(EncodeAccountId(account.account_id)));
142 store_->AddObserver(this);
143
144 BrowserPolicyConnector* connector =
145 g_browser_process->browser_policy_connector();
146 schema_registry_.reset(new SchemaRegistryService(
147 connector->GetChromeSchema(), connector->GetSchemaRegistry()));
134 } 148 }
135 149
136 DeviceLocalAccountPolicyBroker::~DeviceLocalAccountPolicyBroker() { 150 DeviceLocalAccountPolicyBroker::~DeviceLocalAccountPolicyBroker() {
151 store_->RemoveObserver(this);
137 external_data_manager_->SetPolicyStore(NULL); 152 external_data_manager_->SetPolicyStore(NULL);
138 external_data_manager_->Disconnect(); 153 external_data_manager_->Disconnect();
154 component_policy_service_.reset();
155 if (schema_registry_)
156 schema_registry_->Shutdown();
139 } 157 }
140 158
141 void DeviceLocalAccountPolicyBroker::Initialize() { 159 void DeviceLocalAccountPolicyBroker::Initialize() {
142 store_->Load(); 160 store_->Load();
143 } 161 }
144 162
163 void DeviceLocalAccountPolicyBroker::OnSchemaRegistryShutdown() {
164 // Make sure the |component_policy_service_| isn't observing the
165 // SchemaRegistryService anymore.
166 component_policy_service_.reset();
167 }
168
169 scoped_ptr<SchemaRegistryService>
170 DeviceLocalAccountPolicyBroker::release_schema_registry() {
171 return schema_registry_.Pass();
172 }
173
145 void DeviceLocalAccountPolicyBroker::ConnectIfPossible( 174 void DeviceLocalAccountPolicyBroker::ConnectIfPossible(
146 chromeos::DeviceSettingsService* device_settings_service, 175 chromeos::DeviceSettingsService* device_settings_service,
147 DeviceManagementService* device_management_service, 176 DeviceManagementService* device_management_service,
148 scoped_refptr<net::URLRequestContextGetter> request_context) { 177 scoped_refptr<net::URLRequestContextGetter> request_context) {
149 if (core_.client()) 178 if (core_.client())
150 return; 179 return;
151 180
152 scoped_ptr<CloudPolicyClient> client(CreateClient(device_settings_service, 181 scoped_ptr<CloudPolicyClient> client(CreateClient(device_settings_service,
153 device_management_service, 182 device_management_service,
154 request_context)); 183 request_context));
155 if (!client) 184 if (!client)
156 return; 185 return;
157 186
158 core_.Connect(client.Pass()); 187 core_.Connect(client.Pass());
159 external_data_manager_->Connect(request_context); 188 external_data_manager_->Connect(request_context);
160 core_.StartRefreshScheduler(); 189 core_.StartRefreshScheduler();
161 UpdateRefreshDelay(); 190 UpdateRefreshDelay();
191 CreateComponentCloudPolicyService(request_context);
162 } 192 }
163 193
164 void DeviceLocalAccountPolicyBroker::UpdateRefreshDelay() { 194 void DeviceLocalAccountPolicyBroker::UpdateRefreshDelay() {
165 if (core_.refresh_scheduler()) { 195 if (core_.refresh_scheduler()) {
166 const base::Value* policy_value = 196 const base::Value* policy_value =
167 store_->policy_map().GetValue(key::kPolicyRefreshRate); 197 store_->policy_map().GetValue(key::kPolicyRefreshRate);
168 int delay = 0; 198 int delay = 0;
169 if (policy_value && policy_value->GetAsInteger(&delay)) 199 if (policy_value && policy_value->GetAsInteger(&delay))
170 core_.refresh_scheduler()->SetRefreshDelay(delay); 200 core_.refresh_scheduler()->SetRefreshDelay(delay);
171 } 201 }
172 } 202 }
173 203
174 std::string DeviceLocalAccountPolicyBroker::GetDisplayName() const { 204 std::string DeviceLocalAccountPolicyBroker::GetDisplayName() const {
175 std::string display_name; 205 std::string display_name;
176 const base::Value* display_name_value = 206 const base::Value* display_name_value =
177 store_->policy_map().GetValue(policy::key::kUserDisplayName); 207 store_->policy_map().GetValue(policy::key::kUserDisplayName);
178 if (display_name_value) 208 if (display_name_value)
179 display_name_value->GetAsString(&display_name); 209 display_name_value->GetAsString(&display_name);
180 return display_name; 210 return display_name;
181 } 211 }
182 212
183 base::FilePath DeviceLocalAccountPolicyBroker::GetComponentPolicyCachePath() 213 void DeviceLocalAccountPolicyBroker::OnStoreLoaded(CloudPolicyStore* store) {
184 const { 214 UpdateRefreshDelay();
185 return component_policy_cache_path_; 215 policy_update_callback_.Run();
216 }
217
218 void DeviceLocalAccountPolicyBroker::OnStoreError(CloudPolicyStore* store) {
219 policy_update_callback_.Run();
220 }
221
222 void DeviceLocalAccountPolicyBroker::OnComponentCloudPolicyUpdated() {
223 policy_update_callback_.Run();
224 }
225
226 void DeviceLocalAccountPolicyBroker::CreateComponentCloudPolicyService(
227 const scoped_refptr<net::URLRequestContextGetter>& request_context) {
228 if (CommandLine::ForCurrentProcess()->HasSwitch(
229 switches::kDisableComponentCloudPolicy)) {
230 // Disabled via the command line.
231 return;
232 }
233
234 scoped_ptr<ResourceCache> resource_cache(
235 new ResourceCache(component_policy_cache_path_,
236 content::BrowserThread::GetMessageLoopProxyForThread(
237 content::BrowserThread::FILE)));
238
239 component_policy_service_.reset(new ComponentCloudPolicyService(
240 this,
241 schema_registry_.get(),
242 core(),
243 resource_cache.Pass(),
244 request_context,
245 content::BrowserThread::GetMessageLoopProxyForThread(
246 content::BrowserThread::FILE),
247 content::BrowserThread::GetMessageLoopProxyForThread(
248 content::BrowserThread::IO)));
186 } 249 }
187 250
188 DeviceLocalAccountPolicyService::DeviceLocalAccountPolicyService( 251 DeviceLocalAccountPolicyService::DeviceLocalAccountPolicyService(
189 chromeos::SessionManagerClient* session_manager_client, 252 chromeos::SessionManagerClient* session_manager_client,
190 chromeos::DeviceSettingsService* device_settings_service, 253 chromeos::DeviceSettingsService* device_settings_service,
191 chromeos::CrosSettings* cros_settings, 254 chromeos::CrosSettings* cros_settings,
192 scoped_refptr<base::SequencedTaskRunner> store_background_task_runner, 255 scoped_refptr<base::SequencedTaskRunner> store_background_task_runner,
193 scoped_refptr<base::SequencedTaskRunner> extension_cache_task_runner, 256 scoped_refptr<base::SequencedTaskRunner> extension_cache_task_runner,
194 scoped_refptr<base::SequencedTaskRunner> 257 scoped_refptr<base::SequencedTaskRunner>
195 external_data_service_backend_task_runner, 258 external_data_service_backend_task_runner,
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 316
254 return entry->second; 317 return entry->second;
255 } 318 }
256 319
257 bool DeviceLocalAccountPolicyService::IsPolicyAvailableForUser( 320 bool DeviceLocalAccountPolicyService::IsPolicyAvailableForUser(
258 const std::string& user_id) { 321 const std::string& user_id) {
259 DeviceLocalAccountPolicyBroker* broker = GetBrokerForUser(user_id); 322 DeviceLocalAccountPolicyBroker* broker = GetBrokerForUser(user_id);
260 return broker && broker->core()->store()->is_managed(); 323 return broker && broker->core()->store()->is_managed();
261 } 324 }
262 325
263 scoped_refptr<net::URLRequestContextGetter>
264 DeviceLocalAccountPolicyService::request_context() const {
265 return request_context_;
266 }
267
268 void DeviceLocalAccountPolicyService::AddObserver(Observer* observer) { 326 void DeviceLocalAccountPolicyService::AddObserver(Observer* observer) {
269 observers_.AddObserver(observer); 327 observers_.AddObserver(observer);
270 } 328 }
271 329
272 void DeviceLocalAccountPolicyService::RemoveObserver(Observer* observer) { 330 void DeviceLocalAccountPolicyService::RemoveObserver(Observer* observer) {
273 observers_.RemoveObserver(observer); 331 observers_.RemoveObserver(observer);
274 } 332 }
275 333
276 void DeviceLocalAccountPolicyService::OnStoreLoaded(CloudPolicyStore* store) {
277 DeviceLocalAccountPolicyBroker* broker = GetBrokerForStore(store);
278 DCHECK(broker);
279 if (!broker)
280 return;
281 broker->UpdateRefreshDelay();
282 FOR_EACH_OBSERVER(Observer, observers_, OnPolicyUpdated(broker->user_id()));
283 }
284
285 void DeviceLocalAccountPolicyService::OnStoreError(CloudPolicyStore* store) {
286 DeviceLocalAccountPolicyBroker* broker = GetBrokerForStore(store);
287 DCHECK(broker);
288 if (!broker)
289 return;
290 FOR_EACH_OBSERVER(Observer, observers_, OnPolicyUpdated(broker->user_id()));
291 }
292
293 bool DeviceLocalAccountPolicyService::IsExtensionCacheDirectoryBusy( 334 bool DeviceLocalAccountPolicyService::IsExtensionCacheDirectoryBusy(
294 const std::string& account_id) { 335 const std::string& account_id) {
295 return busy_extension_cache_directories_.find(account_id) != 336 return busy_extension_cache_directories_.find(account_id) !=
296 busy_extension_cache_directories_.end(); 337 busy_extension_cache_directories_.end();
297 } 338 }
298 339
299 void DeviceLocalAccountPolicyService::StartExtensionCachesIfPossible() { 340 void DeviceLocalAccountPolicyService::StartExtensionCachesIfPossible() {
300 for (PolicyBrokerMap::iterator it = policy_brokers_.begin(); 341 for (PolicyBrokerMap::iterator it = policy_brokers_.begin();
301 it != policy_brokers_.end(); ++it) { 342 it != policy_brokers_.end(); ++it) {
302 if (!it->second->extension_loader()->IsCacheRunning() && 343 if (!it->second->extension_loader()->IsCacheRunning() &&
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 // Reuse the existing broker if present. 451 // Reuse the existing broker if present.
411 broker.reset(broker_it->second); 452 broker.reset(broker_it->second);
412 old_policy_brokers.erase(broker_it); 453 old_policy_brokers.erase(broker_it);
413 broker_initialized = true; 454 broker_initialized = true;
414 } else { 455 } else {
415 scoped_ptr<DeviceLocalAccountPolicyStore> store( 456 scoped_ptr<DeviceLocalAccountPolicyStore> store(
416 new DeviceLocalAccountPolicyStore(it->account_id, 457 new DeviceLocalAccountPolicyStore(it->account_id,
417 session_manager_client_, 458 session_manager_client_,
418 device_settings_service_, 459 device_settings_service_,
419 store_background_task_runner_)); 460 store_background_task_runner_));
420 store->AddObserver(this);
421 scoped_refptr<DeviceLocalAccountExternalDataManager> 461 scoped_refptr<DeviceLocalAccountExternalDataManager>
422 external_data_manager = 462 external_data_manager =
423 external_data_service_->GetExternalDataManager(it->account_id, 463 external_data_service_->GetExternalDataManager(it->account_id,
424 store.get()); 464 store.get());
425 broker.reset(new DeviceLocalAccountPolicyBroker( 465 broker.reset(new DeviceLocalAccountPolicyBroker(
426 *it, 466 *it,
427 component_policy_cache_root_.Append(EncodeAccountId(it->account_id)), 467 component_policy_cache_root_.Append(EncodeAccountId(it->account_id)),
428 store.Pass(), 468 store.Pass(),
429 external_data_manager, 469 external_data_manager,
470 base::Bind(&DeviceLocalAccountPolicyService::NotifyPolicyUpdated,
471 base::Unretained(this),
bartfab (slow) 2014/06/20 09:44:23 Nit: #include "base/bind_helpers.h"
Joao da Silva 2014/06/20 15:36:07 Done.
472 it->user_id),
430 base::MessageLoopProxy::current())); 473 base::MessageLoopProxy::current()));
431 } 474 }
432 475
433 // Fire up the cloud connection for fetching policy for the account from 476 // Fire up the cloud connection for fetching policy for the account from
434 // the cloud if this is an enterprise-managed device. 477 // the cloud if this is an enterprise-managed device.
435 broker->ConnectIfPossible(device_settings_service_, 478 broker->ConnectIfPossible(device_settings_service_,
436 device_management_service_, 479 device_management_service_,
437 request_context_); 480 request_context_);
438 481
439 policy_brokers_[it->user_id] = broker.release(); 482 policy_brokers_[it->user_id] = broker.release();
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE, 534 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE,
492 base::Bind(&DeleteOrphanedCaches, 535 base::Bind(&DeleteOrphanedCaches,
493 component_policy_cache_root_, 536 component_policy_cache_root_,
494 subdirectories_to_keep)); 537 subdirectories_to_keep));
495 538
496 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceLocalAccountsChanged()); 539 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceLocalAccountsChanged());
497 } 540 }
498 541
499 void DeviceLocalAccountPolicyService::DeleteBrokers(PolicyBrokerMap* map) { 542 void DeviceLocalAccountPolicyService::DeleteBrokers(PolicyBrokerMap* map) {
500 for (PolicyBrokerMap::iterator it = map->begin(); it != map->end(); ++it) { 543 for (PolicyBrokerMap::iterator it = map->begin(); it != map->end(); ++it) {
501 it->second->core()->store()->RemoveObserver(this);
502 scoped_refptr<chromeos::DeviceLocalAccountExternalPolicyLoader> 544 scoped_refptr<chromeos::DeviceLocalAccountExternalPolicyLoader>
503 extension_loader = it->second->extension_loader(); 545 extension_loader = it->second->extension_loader();
504 if (extension_loader->IsCacheRunning()) { 546 if (extension_loader->IsCacheRunning()) {
505 DCHECK(!IsExtensionCacheDirectoryBusy(it->second->account_id())); 547 DCHECK(!IsExtensionCacheDirectoryBusy(it->second->account_id()));
506 busy_extension_cache_directories_.insert(it->second->account_id()); 548 busy_extension_cache_directories_.insert(it->second->account_id());
507 extension_loader->StopCache(base::Bind( 549 extension_loader->StopCache(base::Bind(
508 &DeviceLocalAccountPolicyService::OnObsoleteExtensionCacheShutdown, 550 &DeviceLocalAccountPolicyService::OnObsoleteExtensionCacheShutdown,
509 weak_factory_.GetWeakPtr(), 551 weak_factory_.GetWeakPtr(),
510 it->second->account_id())); 552 it->second->account_id()));
511 } 553 }
512 FOR_EACH_OBSERVER(Observer, observers_, OnBrokerShutdown(it->second)); 554
513 delete it->second; 555 delete it->second;
514 } 556 }
515 map->clear(); 557 map->clear();
516 } 558 }
517 559
518 DeviceLocalAccountPolicyBroker* 560 DeviceLocalAccountPolicyBroker*
519 DeviceLocalAccountPolicyService::GetBrokerForStore( 561 DeviceLocalAccountPolicyService::GetBrokerForStore(
520 CloudPolicyStore* store) { 562 CloudPolicyStore* store) {
521 for (PolicyBrokerMap::iterator it(policy_brokers_.begin()); 563 for (PolicyBrokerMap::iterator it(policy_brokers_.begin());
522 it != policy_brokers_.end(); ++it) { 564 it != policy_brokers_.end(); ++it) {
523 if (it->second->core()->store() == store) 565 if (it->second->core()->store() == store)
524 return it->second; 566 return it->second;
525 } 567 }
526 return NULL; 568 return NULL;
527 } 569 }
528 570
571 void DeviceLocalAccountPolicyService::NotifyPolicyUpdated(
572 const std::string& user_id) {
573 FOR_EACH_OBSERVER(Observer, observers_, OnPolicyUpdated(user_id));
574 }
575
529 } // namespace policy 576 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698