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

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

Issue 7298012: Consolidate data storage and notifications in the cloud policy subsystem (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 9 years, 5 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/policy/device_token_fetcher.h" 5 #include "chrome/browser/policy/device_token_fetcher.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/message_loop.h"
12 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
13 #include "chrome/browser/policy/cloud_policy_cache_base.h" 12 #include "chrome/browser/policy/cloud_policy_cache_base.h"
13 #include "chrome/browser/policy/cloud_policy_data_store.h"
14 #include "chrome/browser/policy/device_management_service.h" 14 #include "chrome/browser/policy/device_management_service.h"
15 #include "chrome/browser/policy/enterprise_metrics.h" 15 #include "chrome/browser/policy/enterprise_metrics.h"
16 #include "chrome/browser/policy/proto/device_management_constants.h" 16 #include "chrome/browser/policy/proto/device_management_constants.h"
17 #include "chrome/browser/policy/proto/device_management_local.pb.h" 17 #include "chrome/browser/policy/proto/device_management_local.pb.h"
18 18
19 namespace { 19 namespace {
20 20
21 // Retry after 5 minutes (with exponential backoff) after token fetch errors. 21 // Retry after 5 minutes (with exponential backoff) after token fetch errors.
22 const int64 kTokenFetchErrorDelayMilliseconds = 5 * 60 * 1000; 22 const int64 kTokenFetchErrorDelayMilliseconds = 5 * 60 * 1000;
23 // Retry after max 3 hours after token fetch errors. 23 // Retry after max 3 hours after token fetch errors.
24 const int64 kTokenFetchErrorMaxDelayMilliseconds = 3 * 60 * 60 * 1000; 24 const int64 kTokenFetchErrorMaxDelayMilliseconds = 3 * 60 * 60 * 1000;
25 // For unmanaged devices, check once per day whether they're still unmanaged. 25 // For unmanaged devices, check once per day whether they're still unmanaged.
26 const int64 kUnmanagedDeviceRefreshRateMilliseconds = 24 * 60 * 60 * 1000; 26 const int64 kUnmanagedDeviceRefreshRateMilliseconds = 24 * 60 * 60 * 1000;
27 27
28 } // namespace 28 } // namespace
29 29
30 namespace policy { 30 namespace policy {
31 31
32 namespace em = enterprise_management; 32 namespace em = enterprise_management;
33 33
34 DeviceTokenFetcher::DeviceTokenFetcher( 34 DeviceTokenFetcher::DeviceTokenFetcher(
35 DeviceManagementService* service, 35 DeviceManagementService* service,
36 CloudPolicyCacheBase* cache, 36 CloudPolicyCacheBase* cache,
37 CloudPolicyDataStore* data_store,
37 PolicyNotifier* notifier) { 38 PolicyNotifier* notifier) {
38 Initialize(service, 39 Initialize(service,
39 cache, 40 cache,
41 data_store,
40 notifier, 42 notifier,
41 new DelayedWorkScheduler); 43 new DelayedWorkScheduler);
42 } 44 }
43 45
44 DeviceTokenFetcher::DeviceTokenFetcher( 46 DeviceTokenFetcher::DeviceTokenFetcher(
45 DeviceManagementService* service, 47 DeviceManagementService* service,
46 CloudPolicyCacheBase* cache, 48 CloudPolicyCacheBase* cache,
49 CloudPolicyDataStore* data_store,
47 PolicyNotifier* notifier, 50 PolicyNotifier* notifier,
48 DelayedWorkScheduler* scheduler) { 51 DelayedWorkScheduler* scheduler) {
49 Initialize(service, 52 Initialize(service, cache, data_store, notifier, scheduler);
50 cache,
51 notifier,
52 scheduler);
53 } 53 }
54 54
55 DeviceTokenFetcher::~DeviceTokenFetcher() { 55 DeviceTokenFetcher::~DeviceTokenFetcher() {
56 scheduler_->CancelDelayedWork(); 56 scheduler_->CancelDelayedWork();
57 } 57 }
58 58
59 void DeviceTokenFetcher::FetchToken( 59 void DeviceTokenFetcher::FetchToken() {
60 const std::string& auth_token,
61 const std::string& device_id,
62 em::DeviceRegisterRequest_Type policy_type,
63 const std::string& machine_id,
64 const std::string& machine_model) {
65 SetState(STATE_INACTIVE); 60 SetState(STATE_INACTIVE);
66 auth_token_ = auth_token;
67 device_id_ = device_id;
68 policy_type_ = policy_type;
69 machine_id_ = machine_id;
70 machine_model_ = machine_model;
71 FetchTokenInternal(); 61 FetchTokenInternal();
72 } 62 }
73 63
74 void DeviceTokenFetcher::FetchTokenInternal() { 64 void DeviceTokenFetcher::FetchTokenInternal() {
75 DCHECK(state_ != STATE_TOKEN_AVAILABLE); 65 DCHECK(state_ != STATE_TOKEN_AVAILABLE);
76 if (auth_token_.empty() || device_id_.empty()) { 66 if (data_store_->gaia_token().empty() || data_store_->device_id().empty()) {
77 // Maybe this device is unmanaged, just exit. The CloudPolicyController 67 // Maybe this device is unmanaged, just exit. The CloudPolicyController
78 // will call FetchToken() again if something changes. 68 // will call FetchToken() again if something changes.
79 return; 69 return;
80 } 70 }
81 // Construct a new backend, which will discard any previous requests. 71 // Construct a new backend, which will discard any previous requests.
82 backend_.reset(service_->CreateBackend()); 72 backend_.reset(service_->CreateBackend());
83 em::DeviceRegisterRequest request; 73 em::DeviceRegisterRequest request;
84 request.set_type(policy_type_); 74 request.set_type(data_store_->policy_register_type());
85 if (!machine_id_.empty()) 75 if (!data_store_->machine_id().empty())
86 request.set_machine_id(machine_id_); 76 request.set_machine_id(data_store_->machine_id());
87 if (!machine_model_.empty()) 77 if (!data_store_->machine_model().empty())
88 request.set_machine_model(machine_model_); 78 request.set_machine_model(data_store_->machine_model());
89 backend_->ProcessRegisterRequest(auth_token_, device_id_, request, this); 79 backend_->ProcessRegisterRequest(data_store_->gaia_token(),
80 data_store_->device_id(),
81 request, this);
90 } 82 }
91 83
92 void DeviceTokenFetcher::SetUnmanagedState() { 84 void DeviceTokenFetcher::SetUnmanagedState() {
93 // The call to |cache_->SetUnmanaged()| has to happen first because it sets 85 // The call to |cache_->SetUnmanaged()| has to happen first because it sets
94 // the timestamp that |SetState()| needs to determine the correct refresh 86 // the timestamp that |SetState()| needs to determine the correct refresh
95 // time. 87 // time.
96 cache_->SetUnmanaged(); 88 cache_->SetUnmanaged();
97 SetState(STATE_UNMANAGED); 89 SetState(STATE_UNMANAGED);
98 } 90 }
99 91
100 const std::string& DeviceTokenFetcher::GetDeviceToken() {
101 return device_token_;
102 }
103
104 void DeviceTokenFetcher::StopAutoRetry() { 92 void DeviceTokenFetcher::StopAutoRetry() {
105 scheduler_->CancelDelayedWork(); 93 scheduler_->CancelDelayedWork();
106 backend_.reset(); 94 backend_.reset();
107 device_token_.clear();
108 auth_token_.clear();
109 device_id_.clear();
110 }
111
112 void DeviceTokenFetcher::AddObserver(DeviceTokenFetcher::Observer* observer) {
113 observer_list_.AddObserver(observer);
114 }
115
116 void DeviceTokenFetcher::RemoveObserver(
117 DeviceTokenFetcher::Observer* observer) {
118 observer_list_.RemoveObserver(observer);
119 } 95 }
120 96
121 void DeviceTokenFetcher::HandleRegisterResponse( 97 void DeviceTokenFetcher::HandleRegisterResponse(
122 const em::DeviceRegisterResponse& response) { 98 const em::DeviceRegisterResponse& response) {
123 if (response.has_device_management_token()) { 99 if (response.has_device_management_token()) {
124 UMA_HISTOGRAM_ENUMERATION(kMetricToken, kMetricTokenFetchOK, 100 UMA_HISTOGRAM_ENUMERATION(kMetricToken, kMetricTokenFetchOK,
125 kMetricTokenSize); 101 kMetricTokenSize);
126 device_token_ = response.device_management_token(); 102 data_store_->SetDeviceToken(response.device_management_token(), false);
127 SetState(STATE_TOKEN_AVAILABLE); 103 SetState(STATE_TOKEN_AVAILABLE);
128 } else { 104 } else {
129 NOTREACHED(); 105 NOTREACHED();
130 UMA_HISTOGRAM_ENUMERATION(kMetricToken, kMetricTokenFetchBadResponse, 106 UMA_HISTOGRAM_ENUMERATION(kMetricToken, kMetricTokenFetchBadResponse,
131 kMetricTokenSize); 107 kMetricTokenSize);
132 SetState(STATE_ERROR); 108 SetState(STATE_ERROR);
133 } 109 }
134 } 110 }
135 111
136 void DeviceTokenFetcher::OnError(DeviceManagementBackend::ErrorCode code) { 112 void DeviceTokenFetcher::OnError(DeviceManagementBackend::ErrorCode code) {
(...skipping 12 matching lines...) Expand all
149 // until the user logs-in again. 125 // until the user logs-in again.
150 SetState(STATE_BAD_AUTH); 126 SetState(STATE_BAD_AUTH);
151 break; 127 break;
152 default: 128 default:
153 SetState(STATE_ERROR); 129 SetState(STATE_ERROR);
154 } 130 }
155 } 131 }
156 132
157 void DeviceTokenFetcher::Initialize(DeviceManagementService* service, 133 void DeviceTokenFetcher::Initialize(DeviceManagementService* service,
158 CloudPolicyCacheBase* cache, 134 CloudPolicyCacheBase* cache,
135 CloudPolicyDataStore* data_store,
159 PolicyNotifier* notifier, 136 PolicyNotifier* notifier,
160 DelayedWorkScheduler* scheduler) { 137 DelayedWorkScheduler* scheduler) {
161 service_ = service; 138 service_ = service;
162 cache_ = cache; 139 cache_ = cache;
163 notifier_ = notifier; 140 notifier_ = notifier;
141 data_store_ = data_store;
164 effective_token_fetch_error_delay_ms_ = kTokenFetchErrorDelayMilliseconds; 142 effective_token_fetch_error_delay_ms_ = kTokenFetchErrorDelayMilliseconds;
165 state_ = STATE_INACTIVE; 143 state_ = STATE_INACTIVE;
166 scheduler_.reset(scheduler); 144 scheduler_.reset(scheduler);
167 145
168 if (cache_->is_unmanaged()) 146 if (cache_->is_unmanaged())
169 SetState(STATE_UNMANAGED); 147 SetState(STATE_UNMANAGED);
170 } 148 }
171 149
172 void DeviceTokenFetcher::SetState(FetcherState state) { 150 void DeviceTokenFetcher::SetState(FetcherState state) {
173 state_ = state; 151 state_ = state;
174 if (state_ != STATE_TEMPORARY_ERROR) 152 if (state_ != STATE_TEMPORARY_ERROR)
175 effective_token_fetch_error_delay_ms_ = kTokenFetchErrorDelayMilliseconds; 153 effective_token_fetch_error_delay_ms_ = kTokenFetchErrorDelayMilliseconds;
176 154
177 base::Time delayed_work_at; 155 base::Time delayed_work_at;
178 switch (state_) { 156 switch (state_) {
179 case STATE_INACTIVE: 157 case STATE_INACTIVE:
180 device_token_.clear();
181 auth_token_.clear();
182 device_id_.clear();
183 notifier_->Inform(CloudPolicySubsystem::UNENROLLED, 158 notifier_->Inform(CloudPolicySubsystem::UNENROLLED,
184 CloudPolicySubsystem::NO_DETAILS, 159 CloudPolicySubsystem::NO_DETAILS,
185 PolicyNotifier::TOKEN_FETCHER); 160 PolicyNotifier::TOKEN_FETCHER);
186 break; 161 break;
187 case STATE_TOKEN_AVAILABLE: 162 case STATE_TOKEN_AVAILABLE:
188 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceTokenAvailable());
189 notifier_->Inform(CloudPolicySubsystem::SUCCESS, 163 notifier_->Inform(CloudPolicySubsystem::SUCCESS,
190 CloudPolicySubsystem::NO_DETAILS, 164 CloudPolicySubsystem::NO_DETAILS,
191 PolicyNotifier::TOKEN_FETCHER); 165 PolicyNotifier::TOKEN_FETCHER);
192 break; 166 break;
193 case STATE_UNMANAGED: 167 case STATE_UNMANAGED:
194 delayed_work_at = cache_->last_policy_refresh_time() + 168 delayed_work_at = cache_->last_policy_refresh_time() +
195 base::TimeDelta::FromMilliseconds( 169 base::TimeDelta::FromMilliseconds(
196 kUnmanagedDeviceRefreshRateMilliseconds); 170 kUnmanagedDeviceRefreshRateMilliseconds);
197 notifier_->Inform(CloudPolicySubsystem::UNMANAGED, 171 notifier_->Inform(CloudPolicySubsystem::UNMANAGED,
198 CloudPolicySubsystem::NO_DETAILS, 172 CloudPolicySubsystem::NO_DETAILS,
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 case STATE_UNMANAGED: 218 case STATE_UNMANAGED:
245 case STATE_ERROR: 219 case STATE_ERROR:
246 case STATE_TEMPORARY_ERROR: 220 case STATE_TEMPORARY_ERROR:
247 case STATE_BAD_AUTH: 221 case STATE_BAD_AUTH:
248 FetchTokenInternal(); 222 FetchTokenInternal();
249 break; 223 break;
250 } 224 }
251 } 225 }
252 226
253 } // namespace policy 227 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/device_token_fetcher.h ('k') | chrome/browser/policy/device_token_fetcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698