Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / components / policy / core / common / cloud / cloud_policy_service.cc
blobf54756317d12405260bee4e0fba938c211ba2519
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 "components/policy/core/common/cloud/cloud_policy_service.h"
7 #include "base/callback.h"
8 #include "policy/proto/device_management_backend.pb.h"
10 namespace em = enterprise_management;
12 namespace policy {
14 CloudPolicyService::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);
26 // Make sure we initialize |client_| from the policy data that might be
27 // already present in |store_|.
28 OnStoreLoaded(store_);
31 CloudPolicyService::~CloudPolicyService() {
32 client_->RemoveNamespaceToFetch(policy_ns_key_);
33 client_->RemoveObserver(this);
34 store_->RemoveObserver(this);
37 std::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);
45 return std::string();
48 void 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;
55 // Else, trigger a refresh.
56 refresh_callbacks_.push_back(callback);
57 refresh_state_ = REFRESH_POLICY_FETCH;
58 client_->FetchPolicy();
61 void CloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) {
62 if (client_->status() != DM_STATUS_SUCCESS) {
63 RefreshCompleted(false);
64 return;
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);
77 void CloudPolicyService::OnRegistrationStateChanged(CloudPolicyClient* client) {
80 void CloudPolicyService::OnClientError(CloudPolicyClient* client) {
81 if (refresh_state_ == REFRESH_POLICY_FETCH)
82 RefreshCompleted(false);
85 void CloudPolicyService::OnStoreLoaded(CloudPolicyStore* store) {
86 // Update the client with state from the store.
87 const em::PolicyData* policy(store_->policy());
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());
96 client_->set_last_policy_timestamp(policy_timestamp);
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();
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);
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());
119 if (refresh_state_ == REFRESH_POLICY_STORE)
120 RefreshCompleted(true);
122 CheckInitializationCompleted();
125 void CloudPolicyService::OnStoreError(CloudPolicyStore* store) {
126 if (refresh_state_ == REFRESH_POLICY_STORE)
127 RefreshCompleted(false);
128 CheckInitializationCompleted();
131 void CloudPolicyService::CheckInitializationCompleted() {
132 if (!IsInitializationComplete() && store_->is_initialized()) {
133 initialization_complete_ = true;
134 FOR_EACH_OBSERVER(Observer, observers_, OnInitializationCompleted(this));
138 void 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;
145 for (std::vector<RefreshPolicyCallback>::iterator callback(callbacks.begin());
146 callback != callbacks.end();
147 ++callback) {
148 callback->Run(success);
152 void CloudPolicyService::AddObserver(Observer* observer) {
153 observers_.AddObserver(observer);
156 void CloudPolicyService::RemoveObserver(Observer* observer) {
157 observers_.RemoveObserver(observer);
160 } // namespace policy