| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "base/time.h" | 11 #include "base/time.h" |
| 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/cloud_policy_constants.h" | 13 #include "chrome/browser/policy/cloud_policy_constants.h" |
| 14 #include "chrome/browser/policy/cloud_policy_data_store.h" | 14 #include "chrome/browser/policy/cloud_policy_data_store.h" |
| 15 #include "chrome/browser/policy/delayed_work_scheduler.h" | 15 #include "chrome/browser/policy/delayed_work_scheduler.h" |
| 16 #include "chrome/browser/policy/device_management_service.h" | 16 #include "chrome/browser/policy/device_management_service.h" |
| 17 #include "chrome/browser/policy/enterprise_metrics.h" | 17 #include "chrome/browser/policy/enterprise_metrics.h" |
| 18 #include "chrome/browser/policy/policy_notifier.h" | 18 #include "chrome/browser/policy/policy_notifier.h" |
| 19 #include "chrome/browser/policy/proto/device_management_local.pb.h" | 19 #include "chrome/browser/policy/proto/device_management_local.pb.h" |
| 20 | 20 |
| 21 namespace em = enterprise_management; |
| 22 |
| 21 namespace policy { | 23 namespace policy { |
| 22 | 24 |
| 23 namespace { | 25 namespace { |
| 24 | 26 |
| 25 // Retry after 5 minutes (with exponential backoff) after token fetch errors. | 27 // Retry after 5 minutes (with exponential backoff) after token fetch errors. |
| 26 const int64 kTokenFetchErrorDelayMilliseconds = 5 * 60 * 1000; | 28 const int64 kTokenFetchErrorDelayMilliseconds = 5 * 60 * 1000; |
| 27 // Retry after max 3 hours after token fetch errors. | 29 // Retry after max 3 hours after token fetch errors. |
| 28 const int64 kTokenFetchErrorMaxDelayMilliseconds = 3 * 60 * 60 * 1000; | 30 const int64 kTokenFetchErrorMaxDelayMilliseconds = 3 * 60 * 60 * 1000; |
| 29 // For unmanaged devices, check once per day whether they're still unmanaged. | 31 // For unmanaged devices, check once per day whether they're still unmanaged. |
| 30 const int64 kUnmanagedDeviceRefreshRateMilliseconds = 24 * 60 * 60 * 1000; | 32 const int64 kUnmanagedDeviceRefreshRateMilliseconds = 24 * 60 * 60 * 1000; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 case DM_STATUS_HTTP_STATUS_ERROR: | 66 case DM_STATUS_HTTP_STATUS_ERROR: |
| 65 sample = kMetricTokenFetchServerFailed; | 67 sample = kMetricTokenFetchServerFailed; |
| 66 break; | 68 break; |
| 67 } | 69 } |
| 68 if (sample != -1) | 70 if (sample != -1) |
| 69 UMA_HISTOGRAM_ENUMERATION(kMetricToken, sample, kMetricTokenSize); | 71 UMA_HISTOGRAM_ENUMERATION(kMetricToken, sample, kMetricTokenSize); |
| 70 else | 72 else |
| 71 NOTREACHED(); | 73 NOTREACHED(); |
| 72 } | 74 } |
| 73 | 75 |
| 76 // Translates the DeviceRegisterResponse::DeviceMode |mode| to the enum used |
| 77 // internally to represent different device modes. |
| 78 DeviceMode TranslateProtobufDeviceMode( |
| 79 em::DeviceRegisterResponse::DeviceMode mode) { |
| 80 switch (mode) { |
| 81 case em::DeviceRegisterResponse::ENTERPRISE: |
| 82 return DEVICE_MODE_ENTERPRISE; |
| 83 case em::DeviceRegisterResponse::KIOSK: |
| 84 return DEVICE_MODE_KIOSK; |
| 85 } |
| 86 LOG(ERROR) << "Unknown enrollment mode in registration response: " << mode; |
| 87 return DEVICE_MODE_UNKNOWN; |
| 88 } |
| 89 |
| 74 } // namespace | 90 } // namespace |
| 75 | 91 |
| 76 namespace em = enterprise_management; | |
| 77 | |
| 78 DeviceTokenFetcher::DeviceTokenFetcher( | 92 DeviceTokenFetcher::DeviceTokenFetcher( |
| 79 DeviceManagementService* service, | 93 DeviceManagementService* service, |
| 80 CloudPolicyCacheBase* cache, | 94 CloudPolicyCacheBase* cache, |
| 81 CloudPolicyDataStore* data_store, | 95 CloudPolicyDataStore* data_store, |
| 82 PolicyNotifier* notifier) | 96 PolicyNotifier* notifier) |
| 83 : effective_token_fetch_error_delay_ms_( | 97 : effective_token_fetch_error_delay_ms_( |
| 84 kTokenFetchErrorDelayMilliseconds) { | 98 kTokenFetchErrorDelayMilliseconds) { |
| 85 Initialize(service, | 99 Initialize(service, |
| 86 cache, | 100 cache, |
| 87 data_store, | 101 data_store, |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 | 194 |
| 181 SampleErrorStatus(status); | 195 SampleErrorStatus(status); |
| 182 | 196 |
| 183 switch (status) { | 197 switch (status) { |
| 184 case DM_STATUS_SUCCESS: { | 198 case DM_STATUS_SUCCESS: { |
| 185 const em::DeviceRegisterResponse& register_response = | 199 const em::DeviceRegisterResponse& register_response = |
| 186 response.register_response(); | 200 response.register_response(); |
| 187 if (register_response.has_device_management_token()) { | 201 if (register_response.has_device_management_token()) { |
| 188 UMA_HISTOGRAM_ENUMERATION(kMetricToken, kMetricTokenFetchOK, | 202 UMA_HISTOGRAM_ENUMERATION(kMetricToken, kMetricTokenFetchOK, |
| 189 kMetricTokenSize); | 203 kMetricTokenSize); |
| 204 |
| 205 if (data_store_->policy_register_type() == |
| 206 em::DeviceRegisterRequest::DEVICE) { |
| 207 // TODO(pastarmovj): Default to DEVICE_MODE_UNKNOWN once DM server has |
| 208 // been updated. http://crosbug.com/26624 |
| 209 DeviceMode mode = DEVICE_MODE_ENTERPRISE; |
| 210 if (register_response.has_enrollment_type()) { |
| 211 mode = TranslateProtobufDeviceMode( |
| 212 register_response.enrollment_type()); |
| 213 } |
| 214 if (mode == DEVICE_MODE_UNKNOWN) { |
| 215 LOG(ERROR) << "Enrollment mode missing or unknown!"; |
| 216 SetState(STATE_BAD_ENROLLMENT_MODE); |
| 217 return; |
| 218 } |
| 219 data_store_->set_device_mode(mode); |
| 220 } |
| 190 data_store_->SetDeviceToken(register_response.device_management_token(), | 221 data_store_->SetDeviceToken(register_response.device_management_token(), |
| 191 false); | 222 false); |
| 192 SetState(STATE_TOKEN_AVAILABLE); | 223 SetState(STATE_TOKEN_AVAILABLE); |
| 193 } else { | 224 } else { |
| 194 NOTREACHED(); | 225 NOTREACHED(); |
| 195 UMA_HISTOGRAM_ENUMERATION(kMetricToken, kMetricTokenFetchBadResponse, | 226 UMA_HISTOGRAM_ENUMERATION(kMetricToken, kMetricTokenFetchBadResponse, |
| 196 kMetricTokenSize); | 227 kMetricTokenSize); |
| 197 SetState(STATE_ERROR); | 228 SetState(STATE_ERROR); |
| 198 } | 229 } |
| 199 return; | 230 return; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 case STATE_TOKEN_AVAILABLE: | 275 case STATE_TOKEN_AVAILABLE: |
| 245 notifier_->Inform(CloudPolicySubsystem::SUCCESS, | 276 notifier_->Inform(CloudPolicySubsystem::SUCCESS, |
| 246 CloudPolicySubsystem::NO_DETAILS, | 277 CloudPolicySubsystem::NO_DETAILS, |
| 247 PolicyNotifier::TOKEN_FETCHER); | 278 PolicyNotifier::TOKEN_FETCHER); |
| 248 break; | 279 break; |
| 249 case STATE_BAD_SERIAL: | 280 case STATE_BAD_SERIAL: |
| 250 notifier_->Inform(CloudPolicySubsystem::UNENROLLED, | 281 notifier_->Inform(CloudPolicySubsystem::UNENROLLED, |
| 251 CloudPolicySubsystem::BAD_SERIAL_NUMBER, | 282 CloudPolicySubsystem::BAD_SERIAL_NUMBER, |
| 252 PolicyNotifier::TOKEN_FETCHER); | 283 PolicyNotifier::TOKEN_FETCHER); |
| 253 break; | 284 break; |
| 285 case STATE_BAD_ENROLLMENT_MODE: |
| 286 notifier_->Inform(CloudPolicySubsystem::UNENROLLED, |
| 287 CloudPolicySubsystem::BAD_ENROLLMENT_MODE, |
| 288 PolicyNotifier::TOKEN_FETCHER); |
| 289 break; |
| 254 case STATE_UNMANAGED: | 290 case STATE_UNMANAGED: |
| 255 delayed_work_at = cache_->last_policy_refresh_time() + | 291 delayed_work_at = cache_->last_policy_refresh_time() + |
| 256 base::TimeDelta::FromMilliseconds( | 292 base::TimeDelta::FromMilliseconds( |
| 257 kUnmanagedDeviceRefreshRateMilliseconds); | 293 kUnmanagedDeviceRefreshRateMilliseconds); |
| 258 notifier_->Inform(CloudPolicySubsystem::UNMANAGED, | 294 notifier_->Inform(CloudPolicySubsystem::UNMANAGED, |
| 259 CloudPolicySubsystem::NO_DETAILS, | 295 CloudPolicySubsystem::NO_DETAILS, |
| 260 PolicyNotifier::TOKEN_FETCHER); | 296 PolicyNotifier::TOKEN_FETCHER); |
| 261 break; | 297 break; |
| 262 case STATE_TEMPORARY_ERROR: | 298 case STATE_TEMPORARY_ERROR: |
| 263 delayed_work_at = base::Time::Now() + | 299 delayed_work_at = base::Time::Now() + |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 // Inform the cache if a token fetch attempt has failed. | 335 // Inform the cache if a token fetch attempt has failed. |
| 300 if (state_ != STATE_INACTIVE && state_ != STATE_TOKEN_AVAILABLE) | 336 if (state_ != STATE_INACTIVE && state_ != STATE_TOKEN_AVAILABLE) |
| 301 cache_->SetFetchingDone(); | 337 cache_->SetFetchingDone(); |
| 302 } | 338 } |
| 303 | 339 |
| 304 void DeviceTokenFetcher::DoWork() { | 340 void DeviceTokenFetcher::DoWork() { |
| 305 switch (state_) { | 341 switch (state_) { |
| 306 case STATE_INACTIVE: | 342 case STATE_INACTIVE: |
| 307 case STATE_TOKEN_AVAILABLE: | 343 case STATE_TOKEN_AVAILABLE: |
| 308 case STATE_BAD_SERIAL: | 344 case STATE_BAD_SERIAL: |
| 345 case STATE_BAD_ENROLLMENT_MODE: |
| 309 break; | 346 break; |
| 310 case STATE_UNMANAGED: | 347 case STATE_UNMANAGED: |
| 311 case STATE_ERROR: | 348 case STATE_ERROR: |
| 312 case STATE_TEMPORARY_ERROR: | 349 case STATE_TEMPORARY_ERROR: |
| 313 case STATE_BAD_AUTH: | 350 case STATE_BAD_AUTH: |
| 314 FetchTokenInternal(); | 351 FetchTokenInternal(); |
| 315 break; | 352 break; |
| 316 } | 353 } |
| 317 } | 354 } |
| 318 | 355 |
| 319 } // namespace policy | 356 } // namespace policy |
| OLD | NEW |