| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/signin/easy_unlock_service_signin_chromeos.h" | 5 #include "chrome/browser/signin/easy_unlock_service_signin_chromeos.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | |
| 9 #include "base/location.h" | 8 #include "base/location.h" |
| 10 #include "base/logging.h" | 9 #include "base/logging.h" |
| 11 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 12 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
| 13 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 14 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.h" | 13 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.h" |
| 15 #include "chrome/browser/chromeos/login/session/user_session_manager.h" | 14 #include "chrome/browser/chromeos/login/session/user_session_manager.h" |
| 16 #include "chromeos/chromeos_switches.h" | |
| 17 #include "chromeos/login/auth/user_context.h" | 15 #include "chromeos/login/auth/user_context.h" |
| 18 | 16 |
| 19 namespace { | 17 namespace { |
| 20 | 18 |
| 21 // The maximum allowed backoff interval when waiting for cryptohome to start. | 19 // The maximum allowed backoff interval when waiting for cryptohome to start. |
| 22 size_t kMaxCryptohomeBackoffIntervalMs = 10000u; | 20 size_t kMaxCryptohomeBackoffIntervalMs = 10000u; |
| 23 | 21 |
| 24 // If the data load fails, the initial interval after which the load will be | 22 // If the data load fails, the initial interval after which the load will be |
| 25 // retried. Further intervals will exponentially increas by factor 2. | 23 // retried. Further intervals will exponentially increas by factor 2. |
| 26 size_t kInitialCryptohomeBackoffIntervalMs = 200u; | 24 size_t kInitialCryptohomeBackoffIntervalMs = 200u; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 | 83 |
| 86 EasyUnlockServiceSignin::UserData::UserData() | 84 EasyUnlockServiceSignin::UserData::UserData() |
| 87 : state(EasyUnlockServiceSignin::USER_DATA_STATE_INITIAL) { | 85 : state(EasyUnlockServiceSignin::USER_DATA_STATE_INITIAL) { |
| 88 } | 86 } |
| 89 | 87 |
| 90 EasyUnlockServiceSignin::UserData::~UserData() {} | 88 EasyUnlockServiceSignin::UserData::~UserData() {} |
| 91 | 89 |
| 92 EasyUnlockServiceSignin::EasyUnlockServiceSignin(Profile* profile) | 90 EasyUnlockServiceSignin::EasyUnlockServiceSignin(Profile* profile) |
| 93 : EasyUnlockService(profile), | 91 : EasyUnlockService(profile), |
| 94 allow_cryptohome_backoff_(true), | 92 allow_cryptohome_backoff_(true), |
| 93 service_active_(false), |
| 95 weak_ptr_factory_(this) { | 94 weak_ptr_factory_(this) { |
| 96 } | 95 } |
| 97 | 96 |
| 98 EasyUnlockServiceSignin::~EasyUnlockServiceSignin() { | 97 EasyUnlockServiceSignin::~EasyUnlockServiceSignin() { |
| 99 STLDeleteContainerPairSecondPointers(user_data_.begin(), user_data_.end()); | |
| 100 } | 98 } |
| 101 | 99 |
| 102 EasyUnlockService::Type EasyUnlockServiceSignin::GetType() const { | 100 EasyUnlockService::Type EasyUnlockServiceSignin::GetType() const { |
| 103 return EasyUnlockService::TYPE_SIGNIN; | 101 return EasyUnlockService::TYPE_SIGNIN; |
| 104 } | 102 } |
| 105 | 103 |
| 106 std::string EasyUnlockServiceSignin::GetUserEmail() const { | 104 std::string EasyUnlockServiceSignin::GetUserEmail() const { |
| 107 return user_id_; | 105 return user_id_; |
| 108 } | 106 } |
| 109 | 107 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 std::string EasyUnlockServiceSignin::GetChallenge() const { | 154 std::string EasyUnlockServiceSignin::GetChallenge() const { |
| 157 const UserData* data = FindLoadedDataForCurrentUser(); | 155 const UserData* data = FindLoadedDataForCurrentUser(); |
| 158 // TODO(xiyuan): Use correct remote device instead of hard coded first one. | 156 // TODO(xiyuan): Use correct remote device instead of hard coded first one. |
| 159 size_t device_index = 0; | 157 size_t device_index = 0; |
| 160 if (!data || data->devices.size() <= device_index) | 158 if (!data || data->devices.size() <= device_index) |
| 161 return std::string(); | 159 return std::string(); |
| 162 return data->devices[device_index].challenge; | 160 return data->devices[device_index].challenge; |
| 163 } | 161 } |
| 164 | 162 |
| 165 void EasyUnlockServiceSignin::InitializeInternal() { | 163 void EasyUnlockServiceSignin::InitializeInternal() { |
| 164 if (chromeos::LoginState::Get()->IsUserLoggedIn()) |
| 165 return; |
| 166 |
| 167 service_active_ = true; |
| 168 |
| 169 chromeos::LoginState::Get()->AddObserver(this); |
| 170 ScreenlockBridge* screenlock_bridge = ScreenlockBridge::Get(); |
| 171 screenlock_bridge->AddObserver(this); |
| 172 if (!screenlock_bridge->focused_user_id().empty()) |
| 173 OnFocusedUserChanged(screenlock_bridge->focused_user_id()); |
| 174 } |
| 175 |
| 176 void EasyUnlockServiceSignin::ShutdownInternal() { |
| 177 if (!service_active_) |
| 178 return; |
| 179 service_active_ = false; |
| 180 |
| 181 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 182 ScreenlockBridge::Get()->RemoveObserver(this); |
| 183 chromeos::LoginState::Get()->RemoveObserver(this); |
| 184 STLDeleteContainerPairSecondPointers(user_data_.begin(), user_data_.end()); |
| 185 user_data_.clear(); |
| 166 } | 186 } |
| 167 | 187 |
| 168 bool EasyUnlockServiceSignin::IsAllowedInternal() { | 188 bool EasyUnlockServiceSignin::IsAllowedInternal() { |
| 169 return !user_id_.empty() && | 189 return service_active_ && |
| 170 FindLoadedDataForCurrentUser() && | 190 !user_id_.empty() && |
| 171 CommandLine::ForCurrentProcess()->HasSwitch( | 191 !chromeos::LoginState::Get()->IsUserLoggedIn(); |
| 172 chromeos::switches::kEnableEasySignin); | 192 } |
| 193 |
| 194 void EasyUnlockServiceSignin::OnScreenDidLock() { |
| 195 } |
| 196 |
| 197 void EasyUnlockServiceSignin::OnScreenDidUnlock() { |
| 198 } |
| 199 |
| 200 void EasyUnlockServiceSignin::OnFocusedUserChanged(const std::string& user_id) { |
| 201 if (user_id_ == user_id) |
| 202 return; |
| 203 |
| 204 // Setting or clearing the user_id may changed |IsAllowed| value, so in these |
| 205 // cases update the app state. Otherwise, it's enough to notify the app the |
| 206 // user data has been updated. |
| 207 bool should_update_app_state = user_id_.empty() != user_id.empty(); |
| 208 user_id_ = user_id; |
| 209 |
| 210 ResetScreenlockStateHandler(); |
| 211 |
| 212 if (should_update_app_state) { |
| 213 UpdateAppState(); |
| 214 } else { |
| 215 NotifyUserUpdated(); |
| 216 } |
| 217 |
| 218 LoadCurrentUserDataIfNeeded(); |
| 219 } |
| 220 |
| 221 void EasyUnlockServiceSignin::LoggedInStateChanged() { |
| 222 if (!chromeos::LoginState::Get()->IsUserLoggedIn()) |
| 223 return; |
| 224 UnloadApp(); |
| 225 Shutdown(); |
| 173 } | 226 } |
| 174 | 227 |
| 175 void EasyUnlockServiceSignin::LoadCurrentUserDataIfNeeded() { | 228 void EasyUnlockServiceSignin::LoadCurrentUserDataIfNeeded() { |
| 176 if (user_id_.empty() || | 229 if (user_id_.empty() || !service_active_) |
| 177 !CommandLine::ForCurrentProcess()->HasSwitch( | |
| 178 chromeos::switches::kEnableEasySignin)) | |
| 179 return; | 230 return; |
| 180 | 231 |
| 181 std::map<std::string, UserData*>::iterator it = user_data_.find(user_id_); | 232 std::map<std::string, UserData*>::iterator it = user_data_.find(user_id_); |
| 182 if (it == user_data_.end()) | 233 if (it == user_data_.end()) |
| 183 user_data_.insert(std::make_pair(user_id_, new UserData())); | 234 user_data_.insert(std::make_pair(user_id_, new UserData())); |
| 184 | 235 |
| 185 UserData* data = user_data_[user_id_]; | 236 UserData* data = user_data_[user_id_]; |
| 186 | 237 |
| 187 if (data->state != USER_DATA_STATE_INITIAL) | 238 if (data->state != USER_DATA_STATE_INITIAL) |
| 188 return; | 239 return; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 202 const chromeos::EasyUnlockDeviceKeyDataList& devices) { | 253 const chromeos::EasyUnlockDeviceKeyDataList& devices) { |
| 203 allow_cryptohome_backoff_ = false; | 254 allow_cryptohome_backoff_ = false; |
| 204 | 255 |
| 205 UserData* data = user_data_[user_id_]; | 256 UserData* data = user_data_[user_id_]; |
| 206 data->state = USER_DATA_STATE_LOADED; | 257 data->state = USER_DATA_STATE_LOADED; |
| 207 if (success) { | 258 if (success) { |
| 208 data->devices = devices; | 259 data->devices = devices; |
| 209 chromeos::EasyUnlockKeyManager::DeviceDataListToRemoteDeviceList( | 260 chromeos::EasyUnlockKeyManager::DeviceDataListToRemoteDeviceList( |
| 210 user_id, devices, &data->remote_devices_value); | 261 user_id, devices, &data->remote_devices_value); |
| 211 } | 262 } |
| 263 |
| 264 // If the fetched data belongs to the currently focused user, notify the app |
| 265 // that it has to refresh it's user data. |
| 266 if (user_id == user_id_) |
| 267 NotifyUserUpdated(); |
| 212 } | 268 } |
| 213 | 269 |
| 214 const EasyUnlockServiceSignin::UserData* | 270 const EasyUnlockServiceSignin::UserData* |
| 215 EasyUnlockServiceSignin::FindLoadedDataForCurrentUser() const { | 271 EasyUnlockServiceSignin::FindLoadedDataForCurrentUser() const { |
| 216 if (user_id_.empty()) | 272 if (user_id_.empty()) |
| 217 return NULL; | 273 return NULL; |
| 274 |
| 218 std::map<std::string, UserData*>::const_iterator it = | 275 std::map<std::string, UserData*>::const_iterator it = |
| 219 user_data_.find(user_id_); | 276 user_data_.find(user_id_); |
| 220 if (it == user_data_.end()) | 277 if (it == user_data_.end()) |
| 221 return NULL; | 278 return NULL; |
| 222 if (it->second->state != USER_DATA_STATE_LOADED) | 279 if (it->second->state != USER_DATA_STATE_LOADED) |
| 223 return NULL; | 280 return NULL; |
| 224 return it->second; | 281 return it->second; |
| 225 } | 282 } |
| OLD | NEW |