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