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 "components/policy/core/common/cloud/cloud_policy_service.h"
6
7#include "base/callback.h"
8#include "policy/proto/device_management_backend.pb.h"
9
10namespace em = enterprise_management;
11
12namespace policy {
13
14CloudPolicyService::CloudPolicyService(const PolicyNamespaceKey& policy_ns_key,
15                                       CloudPolicyClient* client,
16                                       CloudPolicyStore* store)
17    : policy_ns_key_(policy_ns_key),
18      client_(client),
19      store_(store),
20      refresh_state_(REFRESH_NONE),
21      initialization_complete_(false) {
22  client_->AddNamespaceToFetch(policy_ns_key_);
23  client_->AddObserver(this);
24  store_->AddObserver(this);
25
26  // Make sure we initialize |client_| from the policy data that might be
27  // already present in |store_|.
28  OnStoreLoaded(store_);
29}
30
31CloudPolicyService::~CloudPolicyService() {
32  client_->RemoveNamespaceToFetch(policy_ns_key_);
33  client_->RemoveObserver(this);
34  store_->RemoveObserver(this);
35}
36
37std::string CloudPolicyService::ManagedBy() const {
38  const em::PolicyData* policy = store_->policy();
39  if (policy) {
40    std::string username = policy->username();
41    std::size_t pos = username.find('@');
42    if (pos != std::string::npos)
43      return username.substr(pos + 1);
44  }
45  return std::string();
46}
47
48void CloudPolicyService::RefreshPolicy(const RefreshPolicyCallback& callback) {
49  // If the client is not registered, bail out.
50  if (!client_->is_registered()) {
51    callback.Run(false);
52    return;
53  }
54
55  // Else, trigger a refresh.
56  refresh_callbacks_.push_back(callback);
57  refresh_state_ = REFRESH_POLICY_FETCH;
58  client_->FetchPolicy();
59}
60
61void CloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) {
62  if (client_->status() != DM_STATUS_SUCCESS) {
63    RefreshCompleted(false);
64    return;
65  }
66
67  const em::PolicyFetchResponse* policy = client_->GetPolicyFor(policy_ns_key_);
68  if (policy) {
69    if (refresh_state_ != REFRESH_NONE)
70      refresh_state_ = REFRESH_POLICY_STORE;
71    store_->Store(*policy, client->fetched_invalidation_version());
72  } else {
73    RefreshCompleted(false);
74  }
75}
76
77void CloudPolicyService::OnRegistrationStateChanged(CloudPolicyClient* client) {
78}
79
80void CloudPolicyService::OnClientError(CloudPolicyClient* client) {
81  if (refresh_state_ == REFRESH_POLICY_FETCH)
82    RefreshCompleted(false);
83}
84
85void CloudPolicyService::OnStoreLoaded(CloudPolicyStore* store) {
86  // Update the client with state from the store.
87  const em::PolicyData* policy(store_->policy());
88
89  // Timestamp.
90  base::Time policy_timestamp;
91  if (policy && policy->has_timestamp()) {
92    policy_timestamp =
93        base::Time::UnixEpoch() +
94        base::TimeDelta::FromMilliseconds(policy->timestamp());
95  }
96  client_->set_last_policy_timestamp(policy_timestamp);
97
98  // Public key version.
99  if (policy && policy->has_public_key_version())
100    client_->set_public_key_version(policy->public_key_version());
101  else
102    client_->clear_public_key_version();
103
104  // Whether to submit the machine ID.
105  bool submit_machine_id = false;
106  if (policy && policy->has_valid_serial_number_missing())
107    submit_machine_id = policy->valid_serial_number_missing();
108  client_->set_submit_machine_id(submit_machine_id);
109
110  // Finally, set up registration if necessary.
111  if (policy && policy->has_request_token() && policy->has_device_id() &&
112      !client_->is_registered()) {
113    DVLOG(1) << "Setting up registration with request token: "
114             << policy->request_token();
115    client_->SetupRegistration(policy->request_token(),
116                               policy->device_id());
117  }
118
119  if (refresh_state_ == REFRESH_POLICY_STORE)
120    RefreshCompleted(true);
121
122  CheckInitializationCompleted();
123}
124
125void CloudPolicyService::OnStoreError(CloudPolicyStore* store) {
126  if (refresh_state_ == REFRESH_POLICY_STORE)
127    RefreshCompleted(false);
128  CheckInitializationCompleted();
129}
130
131void CloudPolicyService::CheckInitializationCompleted() {
132  if (!IsInitializationComplete() && store_->is_initialized()) {
133    initialization_complete_ = true;
134    FOR_EACH_OBSERVER(Observer, observers_, OnInitializationCompleted(this));
135  }
136}
137
138void CloudPolicyService::RefreshCompleted(bool success) {
139  // Clear state and |refresh_callbacks_| before actually invoking them, s.t.
140  // triggering new policy fetches behaves as expected.
141  std::vector<RefreshPolicyCallback> callbacks;
142  callbacks.swap(refresh_callbacks_);
143  refresh_state_ = REFRESH_NONE;
144
145  for (std::vector<RefreshPolicyCallback>::iterator callback(callbacks.begin());
146       callback != callbacks.end();
147       ++callback) {
148    callback->Run(success);
149  }
150}
151
152void CloudPolicyService::AddObserver(Observer* observer) {
153  observers_.AddObserver(observer);
154}
155
156void CloudPolicyService::RemoveObserver(Observer* observer) {
157  observers_.RemoveObserver(observer);
158}
159
160}  // namespace policy
161