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

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: " 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" 11 #include "base/message_loop.h"
Joao da Silva 2011/07/06 16:45:14 Nit: callback.h and message_loop.h not used
Joao da Silva 2011/07/07 16:54:16 These seem still not used, am I missing something?
gfeher 2011/07/08 09:19:14 Nope. I was missing something.
12 #include "chrome/browser/policy/cloud_policy_cache_base.h" 12 #include "chrome/browser/policy/cloud_policy_cache_base.h"
13 #include "chrome/browser/policy/device_management_service.h" 13 #include "chrome/browser/policy/device_management_service.h"
14 #include "chrome/browser/policy/proto/device_management_constants.h" 14 #include "chrome/browser/policy/proto/device_management_constants.h"
15 #include "chrome/browser/policy/proto/device_management_local.pb.h" 15 #include "chrome/browser/policy/proto/device_management_local.pb.h"
16 16
17 namespace { 17 namespace {
18 18
19 // Retry after 5 minutes (with exponential backoff) after token fetch errors. 19 // Retry after 5 minutes (with exponential backoff) after token fetch errors.
20 const int64 kTokenFetchErrorDelayMilliseconds = 5 * 60 * 1000; 20 const int64 kTokenFetchErrorDelayMilliseconds = 5 * 60 * 1000;
21 // Retry after max 3 hours after token fetch errors. 21 // Retry after max 3 hours after token fetch errors.
22 const int64 kTokenFetchErrorMaxDelayMilliseconds = 3 * 60 * 60 * 1000; 22 const int64 kTokenFetchErrorMaxDelayMilliseconds = 3 * 60 * 60 * 1000;
23 // For unmanaged devices, check once per day whether they're still unmanaged. 23 // For unmanaged devices, check once per day whether they're still unmanaged.
24 const int64 kUnmanagedDeviceRefreshRateMilliseconds = 24 * 60 * 60 * 1000; 24 const int64 kUnmanagedDeviceRefreshRateMilliseconds = 24 * 60 * 60 * 1000;
25 25
26 } // namespace 26 } // namespace
27 27
28 namespace policy { 28 namespace policy {
29 29
30 namespace em = enterprise_management; 30 namespace em = enterprise_management;
31 31
32 DeviceTokenFetcher::DeviceTokenFetcher( 32 DeviceTokenFetcher::DeviceTokenFetcher(
33 DeviceManagementService* service, 33 DeviceManagementService* service,
34 CloudPolicyCacheBase* cache, 34 CloudPolicyCacheBase* cache,
35 CloudPolicyData* data,
35 PolicyNotifier* notifier) { 36 PolicyNotifier* notifier) {
36 Initialize(service, 37 Initialize(service,
37 cache, 38 cache,
39 data,
38 notifier, 40 notifier,
39 new DelayedWorkScheduler); 41 new DelayedWorkScheduler);
40 } 42 }
41 43
42 DeviceTokenFetcher::DeviceTokenFetcher( 44 DeviceTokenFetcher::DeviceTokenFetcher(
43 DeviceManagementService* service, 45 DeviceManagementService* service,
44 CloudPolicyCacheBase* cache, 46 CloudPolicyCacheBase* cache,
47 CloudPolicyData* data,
45 PolicyNotifier* notifier, 48 PolicyNotifier* notifier,
46 DelayedWorkScheduler* scheduler) { 49 DelayedWorkScheduler* scheduler) {
47 Initialize(service, 50 Initialize(service,
48 cache, 51 cache,
52 data,
49 notifier, 53 notifier,
50 scheduler); 54 scheduler);
51 } 55 }
52 56
53 DeviceTokenFetcher::~DeviceTokenFetcher() { 57 DeviceTokenFetcher::~DeviceTokenFetcher() {
54 scheduler_->CancelDelayedWork(); 58 scheduler_->CancelDelayedWork();
55 } 59 }
56 60
57 void DeviceTokenFetcher::FetchToken( 61 void DeviceTokenFetcher::FetchToken() {
58 const std::string& auth_token,
59 const std::string& device_id,
60 em::DeviceRegisterRequest_Type policy_type,
61 const std::string& machine_id,
62 const std::string& machine_model) {
63 SetState(STATE_INACTIVE); 62 SetState(STATE_INACTIVE);
64 auth_token_ = auth_token;
65 device_id_ = device_id;
66 policy_type_ = policy_type;
67 machine_id_ = machine_id;
68 machine_model_ = machine_model;
69 FetchTokenInternal(); 63 FetchTokenInternal();
70 } 64 }
71 65
72 void DeviceTokenFetcher::FetchTokenInternal() { 66 void DeviceTokenFetcher::FetchTokenInternal() {
73 DCHECK(state_ != STATE_TOKEN_AVAILABLE); 67 DCHECK(state_ != STATE_TOKEN_AVAILABLE);
74 if (auth_token_.empty() || device_id_.empty()) { 68 if (data_->gaia_token().empty() || data_->device_id().empty()) {
75 // Maybe this device is unmanaged, just exit. The CloudPolicyController 69 // Maybe this device is unmanaged, just exit. The CloudPolicyController
76 // will call FetchToken() again if something changes. 70 // will call FetchToken() again if something changes.
77 return; 71 return;
78 } 72 }
79 // Construct a new backend, which will discard any previous requests. 73 // Construct a new backend, which will discard any previous requests.
80 backend_.reset(service_->CreateBackend()); 74 backend_.reset(service_->CreateBackend());
81 em::DeviceRegisterRequest request; 75 em::DeviceRegisterRequest request;
82 request.set_type(policy_type_); 76 request.set_type(data_->policy_register_type());
83 if (!machine_id_.empty()) 77 if (!data_->machine_id().empty())
84 request.set_machine_id(machine_id_); 78 request.set_machine_id(data_->machine_id());
85 if (!machine_model_.empty()) 79 if (!data_->machine_model().empty())
86 request.set_machine_model(machine_model_); 80 request.set_machine_model(data_->machine_model());
87 backend_->ProcessRegisterRequest(auth_token_, device_id_, request, this); 81 backend_->ProcessRegisterRequest(data_->gaia_token(), data_->device_id(),
82 request, this);
88 } 83 }
89 84
90 void DeviceTokenFetcher::SetUnmanagedState() { 85 void DeviceTokenFetcher::SetUnmanagedState() {
91 // The call to |cache_->SetUnmanaged()| has to happen first because it sets 86 // The call to |cache_->SetUnmanaged()| has to happen first because it sets
92 // the timestamp that |SetState()| needs to determine the correct refresh 87 // the timestamp that |SetState()| needs to determine the correct refresh
93 // time. 88 // time.
94 cache_->SetUnmanaged(); 89 cache_->SetUnmanaged();
95 SetState(STATE_UNMANAGED); 90 SetState(STATE_UNMANAGED);
96 } 91 }
97 92
98 const std::string& DeviceTokenFetcher::GetDeviceToken() {
99 return device_token_;
100 }
101
102 void DeviceTokenFetcher::StopAutoRetry() { 93 void DeviceTokenFetcher::StopAutoRetry() {
103 scheduler_->CancelDelayedWork(); 94 scheduler_->CancelDelayedWork();
104 backend_.reset(); 95 backend_.reset();
105 device_token_.clear();
106 auth_token_.clear();
107 device_id_.clear();
108 }
109
110 void DeviceTokenFetcher::AddObserver(DeviceTokenFetcher::Observer* observer) {
111 observer_list_.AddObserver(observer);
112 }
113
114 void DeviceTokenFetcher::RemoveObserver(
115 DeviceTokenFetcher::Observer* observer) {
116 observer_list_.RemoveObserver(observer);
117 } 96 }
118 97
119 void DeviceTokenFetcher::HandleRegisterResponse( 98 void DeviceTokenFetcher::HandleRegisterResponse(
120 const em::DeviceRegisterResponse& response) { 99 const em::DeviceRegisterResponse& response) {
121 if (response.has_device_management_token()) { 100 if (response.has_device_management_token()) {
122 device_token_ = response.device_management_token(); 101 data_->SetDeviceToken(response.device_management_token(), false);
123 SetState(STATE_TOKEN_AVAILABLE); 102 SetState(STATE_TOKEN_AVAILABLE);
124 } else { 103 } else {
125 NOTREACHED(); 104 NOTREACHED();
126 SetState(STATE_ERROR); 105 SetState(STATE_ERROR);
127 } 106 }
128 } 107 }
129 108
130 void DeviceTokenFetcher::OnError(DeviceManagementBackend::ErrorCode code) { 109 void DeviceTokenFetcher::OnError(DeviceManagementBackend::ErrorCode code) {
131 switch (code) { 110 switch (code) {
132 case DeviceManagementBackend::kErrorServiceManagementNotSupported: 111 case DeviceManagementBackend::kErrorServiceManagementNotSupported:
(...skipping 10 matching lines...) Expand all
143 // until the user logs-in again. 122 // until the user logs-in again.
144 SetState(STATE_BAD_AUTH); 123 SetState(STATE_BAD_AUTH);
145 break; 124 break;
146 default: 125 default:
147 SetState(STATE_ERROR); 126 SetState(STATE_ERROR);
148 } 127 }
149 } 128 }
150 129
151 void DeviceTokenFetcher::Initialize(DeviceManagementService* service, 130 void DeviceTokenFetcher::Initialize(DeviceManagementService* service,
152 CloudPolicyCacheBase* cache, 131 CloudPolicyCacheBase* cache,
132 CloudPolicyData* data,
153 PolicyNotifier* notifier, 133 PolicyNotifier* notifier,
154 DelayedWorkScheduler* scheduler) { 134 DelayedWorkScheduler* scheduler) {
155 service_ = service; 135 service_ = service;
156 cache_ = cache; 136 cache_ = cache;
157 notifier_ = notifier; 137 notifier_ = notifier;
138 data_ = data;
158 effective_token_fetch_error_delay_ms_ = kTokenFetchErrorDelayMilliseconds; 139 effective_token_fetch_error_delay_ms_ = kTokenFetchErrorDelayMilliseconds;
159 state_ = STATE_INACTIVE; 140 state_ = STATE_INACTIVE;
160 scheduler_.reset(scheduler); 141 scheduler_.reset(scheduler);
161 142
162 if (cache_->is_unmanaged()) 143 if (cache_->is_unmanaged())
163 SetState(STATE_UNMANAGED); 144 SetState(STATE_UNMANAGED);
164 } 145 }
165 146
166 void DeviceTokenFetcher::SetState(FetcherState state) { 147 void DeviceTokenFetcher::SetState(FetcherState state) {
167 state_ = state; 148 state_ = state;
168 if (state_ != STATE_TEMPORARY_ERROR) 149 if (state_ != STATE_TEMPORARY_ERROR)
169 effective_token_fetch_error_delay_ms_ = kTokenFetchErrorDelayMilliseconds; 150 effective_token_fetch_error_delay_ms_ = kTokenFetchErrorDelayMilliseconds;
170 151
171 base::Time delayed_work_at; 152 base::Time delayed_work_at;
172 switch (state_) { 153 switch (state_) {
173 case STATE_INACTIVE: 154 case STATE_INACTIVE:
174 device_token_.clear();
175 auth_token_.clear();
176 device_id_.clear();
177 notifier_->Inform(CloudPolicySubsystem::UNENROLLED, 155 notifier_->Inform(CloudPolicySubsystem::UNENROLLED,
178 CloudPolicySubsystem::NO_DETAILS, 156 CloudPolicySubsystem::NO_DETAILS,
179 PolicyNotifier::TOKEN_FETCHER); 157 PolicyNotifier::TOKEN_FETCHER);
180 break; 158 break;
181 case STATE_TOKEN_AVAILABLE: 159 case STATE_TOKEN_AVAILABLE:
182 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceTokenAvailable());
183 notifier_->Inform(CloudPolicySubsystem::SUCCESS, 160 notifier_->Inform(CloudPolicySubsystem::SUCCESS,
184 CloudPolicySubsystem::NO_DETAILS, 161 CloudPolicySubsystem::NO_DETAILS,
185 PolicyNotifier::TOKEN_FETCHER); 162 PolicyNotifier::TOKEN_FETCHER);
186 break; 163 break;
187 case STATE_UNMANAGED: 164 case STATE_UNMANAGED:
188 delayed_work_at = cache_->last_policy_refresh_time() + 165 delayed_work_at = cache_->last_policy_refresh_time() +
189 base::TimeDelta::FromMilliseconds( 166 base::TimeDelta::FromMilliseconds(
190 kUnmanagedDeviceRefreshRateMilliseconds); 167 kUnmanagedDeviceRefreshRateMilliseconds);
191 notifier_->Inform(CloudPolicySubsystem::UNMANAGED, 168 notifier_->Inform(CloudPolicySubsystem::UNMANAGED,
192 CloudPolicySubsystem::NO_DETAILS, 169 CloudPolicySubsystem::NO_DETAILS,
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 case STATE_UNMANAGED: 215 case STATE_UNMANAGED:
239 case STATE_ERROR: 216 case STATE_ERROR:
240 case STATE_TEMPORARY_ERROR: 217 case STATE_TEMPORARY_ERROR:
241 case STATE_BAD_AUTH: 218 case STATE_BAD_AUTH:
242 FetchTokenInternal(); 219 FetchTokenInternal();
243 break; 220 break;
244 } 221 }
245 } 222 }
246 223
247 } // namespace policy 224 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698