device_local_account_policy_provider.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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.
4
5#include "chrome/browser/chromeos/policy/device_local_account_policy_provider.h"
6
7#include "base/bind.h"
8#include "chrome/browser/policy/cloud/cloud_policy_service.h"
9#include "chrome/browser/policy/policy_bundle.h"
10#include "chrome/browser/policy/policy_service.h"
11
12namespace policy {
13
14DeviceLocalAccountPolicyProvider::DeviceLocalAccountPolicyProvider(
15    const std::string& account_id,
16    DeviceLocalAccountPolicyService* service)
17    : account_id_(account_id),
18      service_(service),
19      store_initialized_(false),
20      waiting_for_policy_refresh_(false),
21      ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
22  service_->AddObserver(this);
23  UpdateFromBroker();
24}
25
26DeviceLocalAccountPolicyProvider::~DeviceLocalAccountPolicyProvider() {
27  service_->RemoveObserver(this);
28}
29
30bool DeviceLocalAccountPolicyProvider::IsInitializationComplete(
31    PolicyDomain domain) const {
32  if (domain == POLICY_DOMAIN_CHROME)
33    return store_initialized_;
34  return true;
35}
36
37void DeviceLocalAccountPolicyProvider::RefreshPolicies() {
38  DeviceLocalAccountPolicyBroker* broker = GetBroker();
39  if (broker && broker->core()->service()) {
40    waiting_for_policy_refresh_ = true;
41    broker->core()->service()->RefreshPolicy(
42        base::Bind(&DeviceLocalAccountPolicyProvider::ReportPolicyRefresh,
43                   weak_factory_.GetWeakPtr()));
44  } else {
45    UpdateFromBroker();
46  }
47}
48
49void DeviceLocalAccountPolicyProvider::OnPolicyUpdated(
50    const std::string& account_id) {
51  if (account_id == account_id_)
52    UpdateFromBroker();
53}
54
55void DeviceLocalAccountPolicyProvider::OnDeviceLocalAccountsChanged() {
56  UpdateFromBroker();
57}
58
59DeviceLocalAccountPolicyBroker* DeviceLocalAccountPolicyProvider::GetBroker() {
60  return service_->GetBrokerForAccount(account_id_);
61}
62
63void DeviceLocalAccountPolicyProvider::ReportPolicyRefresh(bool success) {
64  waiting_for_policy_refresh_ = false;
65  UpdateFromBroker();
66}
67
68void DeviceLocalAccountPolicyProvider::UpdateFromBroker() {
69  DeviceLocalAccountPolicyBroker* broker = GetBroker();
70  scoped_ptr<PolicyBundle> bundle(new PolicyBundle());
71  if (broker) {
72    store_initialized_ |= broker->core()->store()->is_initialized();
73    if (!waiting_for_policy_refresh_) {
74      // Copy policy from the broker.
75      bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
76          .CopyFrom(broker->core()->store()->policy_map());
77    } else {
78      // Wait for the refresh to finish.
79      return;
80    }
81  } else {
82    // Keep existing policy, but do send an update.
83    waiting_for_policy_refresh_ = false;
84    weak_factory_.InvalidateWeakPtrs();
85    bundle->CopyFrom(policies());
86  }
87  UpdatePolicy(bundle.Pass());
88}
89
90}  // namespace policy
91