Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(527)

Side by Side Diff: chrome/browser/policy/cloud_policy_service.cc

Issue 10092010: Add CloudPolicyService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix nit. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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/policy/cloud_policy_service.h"
6
7 #include "base/callback.h"
8 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
9
10 namespace em = enterprise_management;
11
12 namespace policy {
13
14 CloudPolicyService::CloudPolicyService(scoped_ptr<CloudPolicyClient> client,
15 scoped_ptr<CloudPolicyStore> store)
16 : client_(client.Pass()),
17 store_(store.Pass()),
18 refresh_state_(REFRESH_NONE) {
19 client_->AddObserver(this);
20 store_->AddObserver(this);
21 }
22
23 CloudPolicyService::~CloudPolicyService() {
24 client_->RemoveObserver(this);
25 store_->RemoveObserver(this);
26 }
27
28 std::string CloudPolicyService::ManagedBy() const {
29 const em::PolicyData* policy = store_->policy();
30 if (policy) {
31 std::string username = policy->username();
32 std::size_t pos = username.find('@');
33 if (pos != std::string::npos)
34 return username.substr(pos + 1);
35 }
36 return std::string();
37 }
38
39 void CloudPolicyService::RefreshPolicy(const base::Closure& callback) {
40 // If the client is not registered, bail out.
41 if (!client_->is_registered()) {
42 callback.Run();
43 return;
44 }
45
46 // Else, trigger a refresh.
47 refresh_callbacks_.push_back(callback);
48 refresh_state_ = REFRESH_POLICY_FETCH;
49 client_->FetchPolicy();
50 }
51
52 void CloudPolicyService::OnPolicyFetched(CloudPolicyClient* client) {
53 if (client_->status() != DM_STATUS_SUCCESS) {
54 RefreshCompleted();
55 return;
56 }
57
58 const em::PolicyFetchResponse* policy = client_->policy();
59 if (policy) {
60 if (refresh_state_ != REFRESH_NONE)
61 refresh_state_ = REFRESH_POLICY_STORE;
62 store_->Store(*policy);
63 } else {
64 RefreshCompleted();
65 }
66 }
67
68 void CloudPolicyService::OnRegistrationStateChanged(CloudPolicyClient* client) {
69 }
70
71 void CloudPolicyService::OnClientError(CloudPolicyClient* client) {
72 if (refresh_state_ == REFRESH_POLICY_FETCH)
73 RefreshCompleted();
74 }
75
76 void CloudPolicyService::OnStoreLoaded(CloudPolicyStore* store) {
77 // Update the client with state from the store.
78 const em::PolicyData* policy(store_->policy());
79
80 // Timestamp.
81 base::Time policy_timestamp;
82 if (policy && policy->has_timestamp()) {
83 policy_timestamp =
84 base::Time::UnixEpoch() +
85 base::TimeDelta::FromMilliseconds(policy->timestamp());
86 }
87 client_->set_last_policy_timestamp(policy_timestamp);
88
89 // Public key version.
90 if (policy && policy->has_public_key_version())
91 client_->set_public_key_version(policy->public_key_version());
92 else
93 client_->clear_public_key_version();
94
95 // Whether to submit the machine ID.
96 bool submit_machine_id = false;
97 if (policy && policy->has_valid_serial_number_missing())
98 submit_machine_id = policy->valid_serial_number_missing();
99 client_->set_submit_machine_id(submit_machine_id);
100
101 // Finally, set up registration if necessary.
102 if (policy && policy->has_request_token() && policy->has_device_id() &&
103 !client_->is_registered()) {
104 client_->SetupRegistration(policy->request_token(),
105 policy->device_id());
106 }
107
108 if (refresh_state_ == REFRESH_POLICY_STORE)
109 RefreshCompleted();
110 }
111
112 void CloudPolicyService::OnStoreError(CloudPolicyStore* store) {
113 if (refresh_state_ == REFRESH_POLICY_STORE)
114 RefreshCompleted();
115 }
116
117 void CloudPolicyService::RefreshCompleted() {
118 // Clear state and |refresh_callbacks_| before actually invoking them, s.t.
119 // triggering new policy fetches behaves as expected.
120 std::vector<base::Closure> callbacks;
121 callbacks.swap(refresh_callbacks_);
122 refresh_state_ = REFRESH_NONE;
123
124 for (std::vector<base::Closure>::iterator callback(callbacks.begin());
125 callback != callbacks.end();
126 ++callback) {
127 callback->Run();
128 }
129 }
130
131 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/cloud_policy_service.h ('k') | chrome/browser/policy/cloud_policy_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698