| 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 "components/proximity_auth/proximity_auth_system.h" | 5 #include "components/proximity_auth/proximity_auth_system.h" |
| 6 | 6 |
| 7 #include "base/thread_task_runner_handle.h" | |
| 8 #include "base/time/default_tick_clock.h" | |
| 9 #include "components/proximity_auth/logging/logging.h" | |
| 10 #include "components/proximity_auth/proximity_auth_client.h" | |
| 11 #include "components/proximity_auth/proximity_monitor_impl.h" | |
| 12 #include "components/proximity_auth/remote_device_life_cycle_impl.h" | |
| 13 #include "components/proximity_auth/unlock_manager.h" | |
| 14 | |
| 15 namespace proximity_auth { | 7 namespace proximity_auth { |
| 16 | 8 |
| 17 namespace { | |
| 18 | |
| 19 // The time to wait after the device wakes up before beginning to connect to the | |
| 20 // remote device. | |
| 21 const int kWakeUpTimeoutSeconds = 2; | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 ProximityAuthSystem::ProximityAuthSystem( | 9 ProximityAuthSystem::ProximityAuthSystem( |
| 26 RemoteDevice remote_device, | 10 const std::vector<RemoteDevice>& remote_devices) |
| 27 ProximityAuthClient* proximity_auth_client) | 11 : remote_devices_(remote_devices) { |
| 28 : remote_device_(remote_device), | 12 } |
| 29 proximity_auth_client_(proximity_auth_client), | |
| 30 unlock_manager_(new UnlockManager( | |
| 31 UnlockManager::ScreenlockType::SESSION_LOCK, | |
| 32 make_scoped_ptr<ProximityMonitor>(new ProximityMonitorImpl( | |
| 33 remote_device, | |
| 34 make_scoped_ptr(new base::DefaultTickClock()))), | |
| 35 proximity_auth_client)), | |
| 36 suspended_(false), | |
| 37 weak_ptr_factory_(this) {} | |
| 38 | 13 |
| 39 ProximityAuthSystem::~ProximityAuthSystem() { | 14 ProximityAuthSystem::~ProximityAuthSystem() { |
| 40 ScreenlockBridge::Get()->RemoveObserver(this); | |
| 41 unlock_manager_->SetRemoteDeviceLifeCycle(nullptr); | |
| 42 } | 15 } |
| 43 | 16 |
| 44 void ProximityAuthSystem::Start() { | 17 const std::vector<RemoteDevice>& ProximityAuthSystem::GetRemoteDevices() { |
| 45 ScreenlockBridge::Get()->AddObserver(this); | 18 return remote_devices_; |
| 46 if (remote_device_.user_id == ScreenlockBridge::Get()->GetFocusedUser()) | |
| 47 OnFocusedUserChanged(ScreenlockBridge::Get()->GetFocusedUser()); | |
| 48 } | |
| 49 | |
| 50 void ProximityAuthSystem::OnAuthAttempted(const std::string& user_id) { | |
| 51 // TODO(tengs): There is no reason to pass the |user_id| argument anymore. | |
| 52 unlock_manager_->OnAuthAttempted(ScreenlockBridge::LockHandler::USER_CLICK); | |
| 53 } | |
| 54 | |
| 55 void ProximityAuthSystem::OnSuspend() { | |
| 56 PA_LOG(INFO) << "Preparing for device suspension."; | |
| 57 DCHECK(!suspended_); | |
| 58 suspended_ = true; | |
| 59 remote_device_life_cycle_.reset(); | |
| 60 } | |
| 61 | |
| 62 void ProximityAuthSystem::OnSuspendDone() { | |
| 63 PA_LOG(INFO) << "Device resumed from suspension."; | |
| 64 DCHECK(suspended_); | |
| 65 | |
| 66 // TODO(tengs): On ChromeOS, the system's Bluetooth adapter is invalidated | |
| 67 // when the system suspends. However, Chrome does not receive this | |
| 68 // notification until a second or so after the system wakes up. That means | |
| 69 // using the adapter during this time will be problematic, so we wait instead. | |
| 70 // See crbug.com/537057. | |
| 71 proximity_auth_client_->UpdateScreenlockState( | |
| 72 ScreenlockState::BLUETOOTH_CONNECTING); | |
| 73 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 74 FROM_HERE, base::Bind(&ProximityAuthSystem::ResumeAfterWakeUpTimeout, | |
| 75 weak_ptr_factory_.GetWeakPtr()), | |
| 76 base::TimeDelta::FromSeconds(kWakeUpTimeoutSeconds)); | |
| 77 } | |
| 78 | |
| 79 void ProximityAuthSystem::ResumeAfterWakeUpTimeout() { | |
| 80 PA_LOG(INFO) << "Resume after suspend"; | |
| 81 suspended_ = false; | |
| 82 OnFocusedUserChanged(ScreenlockBridge::Get()->GetFocusedUser()); | |
| 83 } | |
| 84 | |
| 85 void ProximityAuthSystem::OnLifeCycleStateChanged( | |
| 86 RemoteDeviceLifeCycle::State old_state, | |
| 87 RemoteDeviceLifeCycle::State new_state) { | |
| 88 unlock_manager_->OnLifeCycleStateChanged(); | |
| 89 } | |
| 90 | |
| 91 void ProximityAuthSystem::OnScreenDidLock( | |
| 92 ScreenlockBridge::LockHandler::ScreenType screen_type) { | |
| 93 OnFocusedUserChanged(ScreenlockBridge::Get()->GetFocusedUser()); | |
| 94 } | |
| 95 | |
| 96 void ProximityAuthSystem::OnScreenDidUnlock( | |
| 97 ScreenlockBridge::LockHandler::ScreenType screen_type) { | |
| 98 unlock_manager_->SetRemoteDeviceLifeCycle(nullptr); | |
| 99 remote_device_life_cycle_.reset(); | |
| 100 } | |
| 101 | |
| 102 void ProximityAuthSystem::OnFocusedUserChanged(const std::string& user_id) { | |
| 103 if (!user_id.empty() && remote_device_.user_id != user_id) { | |
| 104 PA_LOG(INFO) << "Different user focused, destroying RemoteDeviceLifeCycle."; | |
| 105 unlock_manager_->SetRemoteDeviceLifeCycle(nullptr); | |
| 106 remote_device_life_cycle_.reset(); | |
| 107 } else if (!remote_device_life_cycle_ && !suspended_) { | |
| 108 PA_LOG(INFO) << "Creating RemoteDeviceLifeCycle for focused user."; | |
| 109 remote_device_life_cycle_.reset( | |
| 110 new RemoteDeviceLifeCycleImpl(remote_device_, proximity_auth_client_)); | |
| 111 unlock_manager_->SetRemoteDeviceLifeCycle(remote_device_life_cycle_.get()); | |
| 112 remote_device_life_cycle_->AddObserver(this); | |
| 113 remote_device_life_cycle_->Start(); | |
| 114 } | |
| 115 } | 19 } |
| 116 | 20 |
| 117 } // proximity_auth | 21 } // proximity_auth |
| OLD | NEW |