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