1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_PROXIMITY_AUTH_UNLOCK_MANAGER_H
6 #define COMPONENTS_PROXIMITY_AUTH_UNLOCK_MANAGER_H
8 #include "base/macros.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/weak_ptr.h"
11 #include "components/proximity_auth/client_observer.h"
12 #include "components/proximity_auth/remote_device_life_cycle.h"
13 #include "components/proximity_auth/remote_status_update.h"
14 #include "components/proximity_auth/screenlock_bridge.h"
15 #include "components/proximity_auth/screenlock_state.h"
16 #include "device/bluetooth/bluetooth_adapter.h"
18 #if defined(OS_CHROMEOS)
19 #include "chromeos/dbus/power_manager_client.h"
22 namespace proximity_auth
{
25 class ProximityAuthClient
;
26 class ProximityMonitor
;
28 // The unlock manager is responsible for controlling the lock screen UI based on
29 // the authentication status of the registered remote devices.
30 class UnlockManager
: public ClientObserver
,
31 public ScreenlockBridge::Observer
,
32 #if defined(OS_CHROMEOS)
33 chromeos::PowerManagerClient::Observer
,
34 #endif // defined(OS_CHROMEOS)
35 public device::BluetoothAdapter::Observer
{
37 enum class ScreenlockType
{
42 // The |proximity_auth_client| is not owned and should outlive the constructed
44 // TODO(isherman): Rather than passing a single ProximityMonitor instance, we
45 // should pass a factory, as the UnlockManager should create and destroy
46 // ProximityMonitors as needed. Currently, the expectations are misaligned
47 // between the ProximityMonitor and the UnlockManager classes.
48 UnlockManager(ScreenlockType screenlock_type
,
49 scoped_ptr
<ProximityMonitor
> proximity_monitor
,
50 ProximityAuthClient
* proximity_auth_client
);
51 ~UnlockManager() override
;
53 // Whether proximity-based unlocking is currently allowed. True if any one of
54 // the remote devices is authenticated and in range.
55 bool IsUnlockAllowed();
57 // Sets the |life_cycle| of the rmeote device to which local events are
58 // dispatched. A null |life_cycle| indicates that proximity-based
59 // authentication is inactive.
60 void SetRemoteDeviceLifeCycle(RemoteDeviceLifeCycle
* life_cycle
);
62 // Called when the life cycle's state changes.
63 void OnLifeCycleStateChanged();
66 // Called when the user pod is clicked for an authentication attempt of type
68 // Exposed for testing.
69 void OnAuthAttempted(ScreenlockBridge::LockHandler::AuthType auth_type
);
72 // The possible lock screen states for the remote device.
73 enum class RemoteScreenlockState
{
81 void OnUnlockEventSent(bool success
) override
;
82 void OnRemoteStatusUpdate(const RemoteStatusUpdate
& status_update
) override
;
83 void OnDecryptResponse(scoped_ptr
<std::string
> decrypted_bytes
) override
;
84 void OnUnlockResponse(bool success
) override
;
85 void OnDisconnected() override
;
87 // ScreenlockBridge::Observer
89 ScreenlockBridge::LockHandler::ScreenType screen_type
) override
;
90 void OnScreenDidUnlock(
91 ScreenlockBridge::LockHandler::ScreenType screen_type
) override
;
92 void OnFocusedUserChanged(const std::string
& user_id
) override
;
94 // Called when the screenlock state changes.
95 void OnScreenLockedOrUnlocked(bool is_locked
);
97 // Called when the Bluetooth adapter is initialized.
98 void OnBluetoothAdapterInitialized(
99 scoped_refptr
<device::BluetoothAdapter
> adapter
);
101 // device::BluetoothAdapter::Observer:
102 void AdapterPresentChanged(device::BluetoothAdapter
* adapter
,
103 bool present
) override
;
104 void AdapterPoweredChanged(device::BluetoothAdapter
* adapter
,
105 bool powered
) override
;
107 #if defined(OS_CHROMEOS)
108 // chromeos::PowerManagerClient::Observer:
109 void SuspendDone(const base::TimeDelta
& sleep_duration
) override
;
110 #endif // defined(OS_CHROMEOS)
112 // Called when auth is attempted to send the sign-in challenge to the remote
113 // device for decryption.
114 void SendSignInChallenge();
116 // Returns the current state for the screen lock UI.
117 ScreenlockState
GetScreenlockState();
119 // Updates the lock screen based on the manager's current state.
120 void UpdateLockScreen();
122 // Activates or deactivates the proximity monitor, as appropriate given the
123 // current state of |this| unlock manager.
124 void UpdateProximityMonitorState();
126 // Sets waking up state.
127 void SetWakingUpState(bool is_waking_up
);
129 // Accepts or rejects the current auth attempt according to |should_accept|.
130 // If the auth attempt is accepted, unlocks the screen.
131 void AcceptAuthAttempt(bool should_accept
);
133 // Returns the screen lock state corresponding to the given remote |status|
135 RemoteScreenlockState
GetScreenlockStateFromRemoteUpdate(
136 RemoteStatusUpdate update
);
138 // Whether |this| manager is being used for sign-in or session unlock.
139 const ScreenlockType screenlock_type_
;
141 // Whether the user is present at the remote device. Unset if no remote status
142 // update has yet been received.
143 scoped_ptr
<RemoteScreenlockState
> remote_screenlock_state_
;
145 // Controls the proximity auth flow logic for a remote device. Not owned, and
146 // expcted to outlive |this| instance.
147 RemoteDeviceLifeCycle
* life_cycle_
;
149 // The client used to communicate with the remote device once a secure channel
150 // is established. Null if no secure channel has been established yet. Not
151 // owned, and expected to outlive |this| instance.
154 // Tracks whether the remote device is currently in close enough proximity to
155 // the local device to allow unlocking.
156 scoped_ptr
<ProximityMonitor
> proximity_monitor_
;
158 // Used to call into the embedder. Expected to outlive |this| instance.
159 ProximityAuthClient
* proximity_auth_client_
;
161 // Whether the screen is currently locked.
164 // True if the manager is currently processing a user-initiated authentication
165 // attempt, which is initiated when the user pod is clicked.
166 bool is_attempting_auth_
;
168 // Whether the system is waking up from sleep.
171 // The Bluetooth adapter. Null if there is no adapter present on the local
173 scoped_refptr
<device::BluetoothAdapter
> bluetooth_adapter_
;
175 // The sign-in secret received from the remote device by decrypting the
176 // sign-in challenge.
177 scoped_ptr
<std::string
> sign_in_secret_
;
179 // The state of the current screen lock UI.
180 ScreenlockState screenlock_state_
;
182 // Used to clear the waking up state after a timeout.
183 base::WeakPtrFactory
<UnlockManager
> clear_waking_up_state_weak_ptr_factory_
;
185 // Used to reject auth attempts after a timeout. An in-progress auth attempt
186 // blocks the sign-in screen UI, so it's important to prevent the auth attempt
187 // from blocking the UI in case a step in the code path hangs.
188 base::WeakPtrFactory
<UnlockManager
> reject_auth_attempt_weak_ptr_factory_
;
190 // Used to vend all other weak pointers.
191 base::WeakPtrFactory
<UnlockManager
> weak_ptr_factory_
;
193 DISALLOW_COPY_AND_ASSIGN(UnlockManager
);
196 } // namespace proximity_auth
198 #endif // COMPONENTS_PROXIMITY_AUTH_UNLOCK_MANAGER_H