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_regular.h" | 5 #include "chrome/browser/signin/easy_unlock_service_regular.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
10 #include "base/prefs/scoped_user_pref_update.h" | 11 #include "base/prefs/scoped_user_pref_update.h" |
| 12 #include "base/sys_info.h" |
| 13 #include "base/time/default_clock.h" |
11 #include "base/values.h" | 14 #include "base/values.h" |
12 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
| 18 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
14 #include "chrome/browser/signin/chrome_proximity_auth_client.h" | 19 #include "chrome/browser/signin/chrome_proximity_auth_client.h" |
| 20 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
15 #include "chrome/browser/signin/signin_manager_factory.h" | 21 #include "chrome/browser/signin/signin_manager_factory.h" |
16 #include "chrome/common/extensions/api/easy_unlock_private.h" | 22 #include "chrome/common/extensions/api/easy_unlock_private.h" |
17 #include "chrome/common/extensions/extension_constants.h" | 23 #include "chrome/common/extensions/extension_constants.h" |
18 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
19 #include "chromeos/login/user_names.h" | 25 #include "chromeos/login/user_names.h" |
20 #include "components/pref_registry/pref_registry_syncable.h" | 26 #include "components/pref_registry/pref_registry_syncable.h" |
21 #include "components/proximity_auth/cryptauth/cryptauth_access_token_fetcher.h" | 27 #include "components/proximity_auth/cryptauth/cryptauth_access_token_fetcher.h" |
22 #include "components/proximity_auth/cryptauth/cryptauth_client_impl.h" | 28 #include "components/proximity_auth/cryptauth/cryptauth_client_impl.h" |
| 29 #include "components/proximity_auth/cryptauth/cryptauth_device_manager.h" |
| 30 #include "components/proximity_auth/cryptauth/cryptauth_enrollment_manager.h" |
| 31 #include "components/proximity_auth/cryptauth/cryptauth_enrollment_utils.h" |
| 32 #include "components/proximity_auth/cryptauth/cryptauth_gcm_manager_impl.h" |
| 33 #include "components/proximity_auth/cryptauth/secure_message_delegate.h" |
| 34 #include "components/proximity_auth/cryptauth_enroller_factory_impl.h" |
| 35 #include "components/proximity_auth/logging/logging.h" |
23 #include "components/proximity_auth/screenlock_bridge.h" | 36 #include "components/proximity_auth/screenlock_bridge.h" |
24 #include "components/proximity_auth/switches.h" | 37 #include "components/proximity_auth/switches.h" |
| 38 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
25 #include "components/signin/core/browser/signin_manager.h" | 39 #include "components/signin/core/browser/signin_manager.h" |
| 40 #include "components/translate/core/browser/translate_download_manager.h" |
| 41 #include "components/version_info/version_info.h" |
26 #include "content/public/browser/browser_thread.h" | 42 #include "content/public/browser/browser_thread.h" |
27 #include "extensions/browser/event_router.h" | 43 #include "extensions/browser/event_router.h" |
28 #include "extensions/common/constants.h" | 44 #include "extensions/common/constants.h" |
29 #include "google_apis/gaia/gaia_auth_util.h" | 45 #include "google_apis/gaia/gaia_auth_util.h" |
30 | 46 |
31 #if defined(OS_CHROMEOS) | 47 #if defined(OS_CHROMEOS) |
32 #include "apps/app_lifetime_monitor_factory.h" | 48 #include "apps/app_lifetime_monitor_factory.h" |
| 49 #include "ash/display/display_info.h" |
| 50 #include "ash/display/display_manager.h" |
| 51 #include "ash/shell.h" |
| 52 #include "base/linux_util.h" |
33 #include "base/thread_task_runner_handle.h" | 53 #include "base/thread_task_runner_handle.h" |
34 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.h" | 54 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.h" |
35 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_reauth.h" | 55 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_reauth.h" |
36 #include "chrome/browser/chromeos/login/session/user_session_manager.h" | 56 #include "chrome/browser/chromeos/login/session/user_session_manager.h" |
37 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 57 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
38 #include "components/user_manager/user_manager.h" | 58 #include "components/user_manager/user_manager.h" |
39 #endif | 59 #endif |
40 | 60 |
41 namespace { | 61 namespace { |
42 | 62 |
43 // Key name of the local device permit record dictonary in kEasyUnlockPairing. | 63 // Key name of the local device permit record dictonary in kEasyUnlockPairing. |
44 const char kKeyPermitAccess[] = "permitAccess"; | 64 const char kKeyPermitAccess[] = "permitAccess"; |
45 | 65 |
46 // Key name of the remote device list in kEasyUnlockPairing. | 66 // Key name of the remote device list in kEasyUnlockPairing. |
47 const char kKeyDevices[] = "devices"; | 67 const char kKeyDevices[] = "devices"; |
48 | 68 |
49 } // namespace | 69 } // namespace |
50 | 70 |
51 EasyUnlockServiceRegular::EasyUnlockServiceRegular(Profile* profile) | 71 EasyUnlockServiceRegular::EasyUnlockServiceRegular(Profile* profile) |
52 : EasyUnlockService(profile), | 72 : EasyUnlockService(profile), |
53 turn_off_flow_status_(EasyUnlockService::IDLE), | 73 turn_off_flow_status_(EasyUnlockService::IDLE), |
54 will_unlock_using_easy_unlock_(false), | 74 will_unlock_using_easy_unlock_(false), |
55 lock_screen_last_shown_timestamp_(base::TimeTicks::Now()), | 75 lock_screen_last_shown_timestamp_(base::TimeTicks::Now()), |
56 weak_ptr_factory_(this) { | 76 weak_ptr_factory_(this) { |
57 } | 77 } |
58 | 78 |
59 EasyUnlockServiceRegular::~EasyUnlockServiceRegular() { | 79 EasyUnlockServiceRegular::~EasyUnlockServiceRegular() { |
60 } | 80 } |
61 | 81 |
| 82 proximity_auth::CryptAuthEnrollmentManager* |
| 83 EasyUnlockServiceRegular::GetCryptAuthEnrollmentManager() { |
| 84 return enrollment_manager_.get(); |
| 85 } |
| 86 |
| 87 proximity_auth::CryptAuthDeviceManager* |
| 88 EasyUnlockServiceRegular::GetCryptAuthDeviceManager() { |
| 89 return device_manager_.get(); |
| 90 } |
| 91 |
62 EasyUnlockService::Type EasyUnlockServiceRegular::GetType() const { | 92 EasyUnlockService::Type EasyUnlockServiceRegular::GetType() const { |
63 return EasyUnlockService::TYPE_REGULAR; | 93 return EasyUnlockService::TYPE_REGULAR; |
64 } | 94 } |
65 | 95 |
66 std::string EasyUnlockServiceRegular::GetUserEmail() const { | 96 std::string EasyUnlockServiceRegular::GetUserEmail() const { |
67 const SigninManagerBase* signin_manager = | 97 const SigninManagerBase* signin_manager = |
68 SigninManagerFactory::GetForProfileIfExists(profile()); | 98 SigninManagerFactory::GetForProfileIfExists(profile()); |
69 // |profile| has to be a signed-in profile with SigninManager already | 99 // |profile| has to be a signed-in profile with SigninManager already |
70 // created. Otherwise, just crash to collect stack. | 100 // created. Otherwise, just crash to collect stack. |
71 DCHECK(signin_manager); | 101 DCHECK(signin_manager); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 prefs::kEasyUnlockPairing); | 182 prefs::kEasyUnlockPairing); |
153 pairing_update->RemoveWithoutPathExpansion(kKeyPermitAccess, NULL); | 183 pairing_update->RemoveWithoutPathExpansion(kKeyPermitAccess, NULL); |
154 } | 184 } |
155 | 185 |
156 const base::ListValue* EasyUnlockServiceRegular::GetRemoteDevices() const { | 186 const base::ListValue* EasyUnlockServiceRegular::GetRemoteDevices() const { |
157 const base::DictionaryValue* pairing_dict = | 187 const base::DictionaryValue* pairing_dict = |
158 profile()->GetPrefs()->GetDictionary(prefs::kEasyUnlockPairing); | 188 profile()->GetPrefs()->GetDictionary(prefs::kEasyUnlockPairing); |
159 const base::ListValue* devices = NULL; | 189 const base::ListValue* devices = NULL; |
160 if (pairing_dict && pairing_dict->GetList(kKeyDevices, &devices)) | 190 if (pairing_dict && pairing_dict->GetList(kKeyDevices, &devices)) |
161 return devices; | 191 return devices; |
162 | |
163 return NULL; | 192 return NULL; |
164 } | 193 } |
165 | 194 |
166 void EasyUnlockServiceRegular::SetRemoteDevices( | 195 void EasyUnlockServiceRegular::SetRemoteDevices( |
167 const base::ListValue& devices) { | 196 const base::ListValue& devices) { |
168 DictionaryPrefUpdate pairing_update(profile()->GetPrefs(), | 197 DictionaryPrefUpdate pairing_update(profile()->GetPrefs(), |
169 prefs::kEasyUnlockPairing); | 198 prefs::kEasyUnlockPairing); |
170 if (devices.empty()) | 199 if (devices.empty()) |
171 pairing_update->RemoveWithoutPathExpansion(kKeyDevices, NULL); | 200 pairing_update->RemoveWithoutPathExpansion(kKeyDevices, NULL); |
172 else | 201 else |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 void EasyUnlockServiceRegular::InitializeInternal() { | 305 void EasyUnlockServiceRegular::InitializeInternal() { |
277 proximity_auth::ScreenlockBridge::Get()->AddObserver(this); | 306 proximity_auth::ScreenlockBridge::Get()->AddObserver(this); |
278 registrar_.Init(profile()->GetPrefs()); | 307 registrar_.Init(profile()->GetPrefs()); |
279 registrar_.Add( | 308 registrar_.Add( |
280 prefs::kEasyUnlockAllowed, | 309 prefs::kEasyUnlockAllowed, |
281 base::Bind(&EasyUnlockServiceRegular::OnPrefsChanged, | 310 base::Bind(&EasyUnlockServiceRegular::OnPrefsChanged, |
282 base::Unretained(this))); | 311 base::Unretained(this))); |
283 registrar_.Add(prefs::kEasyUnlockProximityRequired, | 312 registrar_.Add(prefs::kEasyUnlockProximityRequired, |
284 base::Bind(&EasyUnlockServiceRegular::OnPrefsChanged, | 313 base::Bind(&EasyUnlockServiceRegular::OnPrefsChanged, |
285 base::Unretained(this))); | 314 base::Unretained(this))); |
| 315 |
| 316 #if defined(OS_CHROMEOS) |
| 317 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 318 proximity_auth::switches::kEnableBluetoothLowEnergyDiscovery)) |
| 319 InitializeCryptAuth(); |
| 320 #endif |
| 321 |
286 OnPrefsChanged(); | 322 OnPrefsChanged(); |
287 } | 323 } |
288 | 324 |
289 void EasyUnlockServiceRegular::ShutdownInternal() { | 325 void EasyUnlockServiceRegular::ShutdownInternal() { |
290 #if defined(OS_CHROMEOS) | 326 #if defined(OS_CHROMEOS) |
291 short_lived_user_context_.reset(); | 327 short_lived_user_context_.reset(); |
292 #endif | 328 #endif |
293 | 329 |
294 turn_off_flow_status_ = EasyUnlockService::IDLE; | 330 turn_off_flow_status_ = EasyUnlockService::IDLE; |
295 registrar_.RemoveAll(); | 331 registrar_.RemoveAll(); |
(...skipping 26 matching lines...) Expand all Loading... |
322 } | 358 } |
323 | 359 |
324 void EasyUnlockServiceRegular::OnWillFinalizeUnlock(bool success) { | 360 void EasyUnlockServiceRegular::OnWillFinalizeUnlock(bool success) { |
325 will_unlock_using_easy_unlock_ = success; | 361 will_unlock_using_easy_unlock_ = success; |
326 } | 362 } |
327 | 363 |
328 void EasyUnlockServiceRegular::OnSuspendDone() { | 364 void EasyUnlockServiceRegular::OnSuspendDone() { |
329 lock_screen_last_shown_timestamp_ = base::TimeTicks::Now(); | 365 lock_screen_last_shown_timestamp_ = base::TimeTicks::Now(); |
330 } | 366 } |
331 | 367 |
| 368 void EasyUnlockServiceRegular::OnRefreshTokenAvailable( |
| 369 const std::string& account_id) { |
| 370 if (account_id == proximity_auth_client()->GetAccountId()) { |
| 371 OAuth2TokenService* token_service = |
| 372 ProfileOAuth2TokenServiceFactory::GetForProfile(profile()); |
| 373 token_service->RemoveObserver(this); |
| 374 #if defined(OS_CHROMEOS) |
| 375 InitializeCryptAuth(); |
| 376 #endif |
| 377 } |
| 378 } |
| 379 |
332 void EasyUnlockServiceRegular::OnScreenDidLock( | 380 void EasyUnlockServiceRegular::OnScreenDidLock( |
333 proximity_auth::ScreenlockBridge::LockHandler::ScreenType screen_type) { | 381 proximity_auth::ScreenlockBridge::LockHandler::ScreenType screen_type) { |
334 will_unlock_using_easy_unlock_ = false; | 382 will_unlock_using_easy_unlock_ = false; |
335 lock_screen_last_shown_timestamp_ = base::TimeTicks::Now(); | 383 lock_screen_last_shown_timestamp_ = base::TimeTicks::Now(); |
336 } | 384 } |
337 | 385 |
338 void EasyUnlockServiceRegular::OnScreenDidUnlock( | 386 void EasyUnlockServiceRegular::OnScreenDidUnlock( |
339 proximity_auth::ScreenlockBridge::LockHandler::ScreenType screen_type) { | 387 proximity_auth::ScreenlockBridge::LockHandler::ScreenType screen_type) { |
340 // Notifications of signin screen unlock events can also reach this code path; | 388 // Notifications of signin screen unlock events can also reach this code path; |
341 // disregard them. | 389 // disregard them. |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 new base::DictionaryValue()); | 450 new base::DictionaryValue()); |
403 user_prefs_dict->SetBooleanWithoutPathExpansion( | 451 user_prefs_dict->SetBooleanWithoutPathExpansion( |
404 prefs::kEasyUnlockProximityRequired, | 452 prefs::kEasyUnlockProximityRequired, |
405 profile_prefs->GetBoolean(prefs::kEasyUnlockProximityRequired)); | 453 profile_prefs->GetBoolean(prefs::kEasyUnlockProximityRequired)); |
406 | 454 |
407 DictionaryPrefUpdate update(local_state, | 455 DictionaryPrefUpdate update(local_state, |
408 prefs::kEasyUnlockLocalStateUserPrefs); | 456 prefs::kEasyUnlockLocalStateUserPrefs); |
409 std::string user_email = GetUserEmail(); | 457 std::string user_email = GetUserEmail(); |
410 update->SetWithoutPathExpansion(user_email, user_prefs_dict.Pass()); | 458 update->SetWithoutPathExpansion(user_email, user_prefs_dict.Pass()); |
411 } | 459 } |
| 460 |
| 461 cryptauth::GcmDeviceInfo EasyUnlockServiceRegular::GetGcmDeviceInfo() { |
| 462 cryptauth::GcmDeviceInfo device_info; |
| 463 device_info.set_long_device_id(EasyUnlockService::GetDeviceId()); |
| 464 device_info.set_device_type(cryptauth::CHROME); |
| 465 device_info.set_device_software_version(version_info::GetVersionNumber()); |
| 466 google::protobuf::int64 software_version_code = |
| 467 proximity_auth::HashStringToInt64(version_info::GetLastChange()); |
| 468 device_info.set_device_software_version_code(software_version_code); |
| 469 device_info.set_locale( |
| 470 translate::TranslateDownloadManager::GetInstance()->application_locale()); |
| 471 |
| 472 #if defined(OS_CHROMEOS) |
| 473 device_info.set_device_model(base::SysInfo::GetLsbReleaseBoard()); |
| 474 device_info.set_device_os_version(base::GetLinuxDistro()); |
| 475 // The Chrome OS version tracks the Chrome version, so fill in the same value |
| 476 // as |device_software_version_code|. |
| 477 device_info.set_device_os_version_code(software_version_code); |
| 478 |
| 479 // There may not be a Shell instance in tests. |
| 480 if (!ash::Shell::HasInstance()) |
| 481 return device_info; |
| 482 |
| 483 ash::DisplayManager* display_manager = |
| 484 ash::Shell::GetInstance()->display_manager(); |
| 485 int64 primary_display_id = display_manager->GetPrimaryDisplayCandidate().id(); |
| 486 ash::DisplayInfo display_info = |
| 487 display_manager->GetDisplayInfo(primary_display_id); |
| 488 gfx::Rect bounds = display_info.bounds_in_native(); |
| 489 |
| 490 // TODO(tengs): This is a heuristic to deterimine the DPI of the display, as |
| 491 // there is no convenient way of getting this information right now. |
| 492 const double dpi = display_info.device_scale_factor() > 1.0f ? 239.0f : 96.0f; |
| 493 double width_in_inches = (bounds.width() - bounds.x()) / dpi; |
| 494 double height_in_inches = (bounds.height() - bounds.y()) / dpi; |
| 495 double diagonal_in_inches = sqrt(width_in_inches * width_in_inches + |
| 496 height_in_inches * height_in_inches); |
| 497 |
| 498 // Note: The unit of this measument is in milli-inches. |
| 499 device_info.set_device_display_diagonal_mils(diagonal_in_inches * 1000.0); |
| 500 #else |
| 501 // TODO(tengs): Fill in device information for other platforms. |
| 502 #endif |
| 503 return device_info; |
| 504 } |
| 505 |
| 506 #if defined(OS_CHROMEOS) |
| 507 void EasyUnlockServiceRegular::InitializeCryptAuth() { |
| 508 OAuth2TokenService* token_service = |
| 509 ProfileOAuth2TokenServiceFactory::GetForProfile(profile()); |
| 510 if (!token_service->RefreshTokenIsAvailable( |
| 511 proximity_auth_client()->GetAccountId())) { |
| 512 PA_LOG(INFO) << "Refresh token not yet available."; |
| 513 token_service->AddObserver(this); |
| 514 return; |
| 515 } |
| 516 |
| 517 PA_LOG(INFO) << "Initializing CryptAuth managers."; |
| 518 // Initialize GCM manager. |
| 519 gcm_manager_.reset(new proximity_auth::CryptAuthGCMManagerImpl( |
| 520 gcm::GCMProfileServiceFactory::GetForProfile(profile())->driver(), |
| 521 proximity_auth_client()->GetPrefService())); |
| 522 gcm_manager_->StartListening(); |
| 523 |
| 524 // Initialize enrollment manager. |
| 525 cryptauth::GcmDeviceInfo device_info; |
| 526 enrollment_manager_.reset(new proximity_auth::CryptAuthEnrollmentManager( |
| 527 make_scoped_ptr(new base::DefaultClock()), |
| 528 make_scoped_ptr(new proximity_auth::CryptAuthEnrollerFactoryImpl( |
| 529 proximity_auth_client())), |
| 530 proximity_auth_client()->CreateSecureMessageDelegate(), |
| 531 GetGcmDeviceInfo(), gcm_manager_.get(), |
| 532 proximity_auth_client()->GetPrefService())); |
| 533 enrollment_manager_->Start(); |
| 534 |
| 535 // Initialize device manager. |
| 536 device_manager_.reset(new proximity_auth::CryptAuthDeviceManager( |
| 537 make_scoped_ptr(new base::DefaultClock()), |
| 538 proximity_auth_client()->CreateCryptAuthClientFactory(), |
| 539 gcm_manager_.get(), proximity_auth_client()->GetPrefService())); |
| 540 device_manager_->Start(); |
| 541 } |
| 542 #endif |
OLD | NEW |