Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / device_local_account_policy_provider.cc
blobdafac174ea410ab38cdc8f6d4e968388f75fd138
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"
7 #include "base/bind.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"
14 namespace policy {
16 DeviceLocalAccountPolicyProvider::DeviceLocalAccountPolicyProvider(
17 const std::string& user_id,
18 DeviceLocalAccountPolicyService* service)
19 : user_id_(user_id),
20 service_(service),
21 store_initialized_(false),
22 waiting_for_policy_refresh_(false),
23 weak_factory_(this) {
24 service_->AddObserver(this);
25 UpdateFromBroker();
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_;
36 return true;
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()));
46 } else {
47 UpdateFromBroker();
51 void DeviceLocalAccountPolicyProvider::OnPolicyUpdated(
52 const std::string& user_id) {
53 if (user_id == user_id_)
54 UpdateFromBroker();
57 void DeviceLocalAccountPolicyProvider::OnDeviceLocalAccountsChanged() {
58 UpdateFromBroker();
61 DeviceLocalAccountPolicyBroker* DeviceLocalAccountPolicyProvider::GetBroker() {
62 return service_->GetBrokerForUser(user_id_);
65 void DeviceLocalAccountPolicyProvider::ReportPolicyRefresh(bool success) {
66 waiting_for_policy_refresh_ = false;
67 UpdateFromBroker();
70 void DeviceLocalAccountPolicyProvider::UpdateFromBroker() {
71 DeviceLocalAccountPolicyBroker* broker = GetBroker();
72 scoped_ptr<PolicyBundle> bundle(new PolicyBundle());
73 if (broker) {
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();
80 } else {
81 // Wait for the refresh to finish.
82 return;
84 } else {
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());
93 } // namespace policy