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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/policy/device_local_account_policy_provider.h"
8 #include "chrome/browser/chromeos/policy/device_local_account_external_data_manager.h"
9 #include "components/policy/core/common/cloud/cloud_policy_core.h"
10 #include "components/policy/core/common/cloud/cloud_policy_service.h"
11 #include "components/policy/core/common/policy_bundle.h"
12 #include "components/policy/core/common/policy_namespace.h"
16 DeviceLocalAccountPolicyProvider::DeviceLocalAccountPolicyProvider(
17 const std::string
& user_id
,
18 DeviceLocalAccountPolicyService
* service
)
21 store_initialized_(false),
22 waiting_for_policy_refresh_(false),
24 service_
->AddObserver(this);
28 DeviceLocalAccountPolicyProvider::~DeviceLocalAccountPolicyProvider() {
29 service_
->RemoveObserver(this);
32 bool DeviceLocalAccountPolicyProvider::IsInitializationComplete(
33 PolicyDomain domain
) const {
34 if (domain
== POLICY_DOMAIN_CHROME
)
35 return store_initialized_
;
39 void DeviceLocalAccountPolicyProvider::RefreshPolicies() {
40 DeviceLocalAccountPolicyBroker
* broker
= GetBroker();
41 if (broker
&& broker
->core()->service()) {
42 waiting_for_policy_refresh_
= true;
43 broker
->core()->service()->RefreshPolicy(
44 base::Bind(&DeviceLocalAccountPolicyProvider::ReportPolicyRefresh
,
45 weak_factory_
.GetWeakPtr()));
51 void DeviceLocalAccountPolicyProvider::OnPolicyUpdated(
52 const std::string
& user_id
) {
53 if (user_id
== user_id_
)
57 void DeviceLocalAccountPolicyProvider::OnDeviceLocalAccountsChanged() {
61 DeviceLocalAccountPolicyBroker
* DeviceLocalAccountPolicyProvider::GetBroker() {
62 return service_
->GetBrokerForUser(user_id_
);
65 void DeviceLocalAccountPolicyProvider::ReportPolicyRefresh(bool success
) {
66 waiting_for_policy_refresh_
= false;
70 void DeviceLocalAccountPolicyProvider::UpdateFromBroker() {
71 DeviceLocalAccountPolicyBroker
* broker
= GetBroker();
72 scoped_ptr
<PolicyBundle
> bundle(new PolicyBundle());
74 store_initialized_
|= broker
->core()->store()->is_initialized();
75 if (!waiting_for_policy_refresh_
) {
76 // Copy policy from the broker.
77 bundle
->Get(PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()))
78 .CopyFrom(broker
->core()->store()->policy_map());
79 external_data_manager_
= broker
->external_data_manager();
81 // Wait for the refresh to finish.
85 // Keep existing policy, but do send an update.
86 waiting_for_policy_refresh_
= false;
87 weak_factory_
.InvalidateWeakPtrs();
88 bundle
->CopyFrom(policies());
90 UpdatePolicy(bundle
.Pass());