Rename GetIconID to GetIconId
[chromium-blink-merge.git] / components / proximity_auth / unlock_manager.h
blob9f40541bc528dd206472d1d81e01d20d7ec319a3
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/controller.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"
20 #endif
22 namespace proximity_auth {
24 class Client;
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 {
36 public:
37 enum class ScreenlockType {
38 SESSION_LOCK,
39 SIGN_IN,
42 // The |proximity_auth_client| is not owned and should outlive the constructed
43 // unlock manager.
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 |controller| to which local events are dispatched. A null
58 // controller indicates that proximity-based authentication is inactive.
59 void SetController(Controller* controller);
61 // Called when the controller's state changes.
62 void OnControllerStateChanged();
64 protected:
65 // Called when the user pod is clicked for an authentication attempt of type
66 // |auth_type|.
67 // Exposed for testing.
68 void OnAuthAttempted(ScreenlockBridge::LockHandler::AuthType auth_type);
70 private:
71 // The possible lock screen states for the remote device.
72 enum class RemoteScreenlockState {
73 UNKNOWN,
74 UNLOCKED,
75 DISABLED,
76 LOCKED,
79 // ClientObserver:
80 void OnUnlockEventSent(bool success) override;
81 void OnRemoteStatusUpdate(const RemoteStatusUpdate& status_update) override;
82 void OnDecryptResponse(scoped_ptr<std::string> decrypted_bytes) override;
83 void OnUnlockResponse(bool success) override;
84 void OnDisconnected() override;
86 // ScreenlockBridge::Observer
87 void OnScreenDidLock(
88 ScreenlockBridge::LockHandler::ScreenType screen_type) override;
89 void OnScreenDidUnlock(
90 ScreenlockBridge::LockHandler::ScreenType screen_type) override;
91 void OnFocusedUserChanged(const std::string& user_id) override;
93 // Called when the screenlock state changes.
94 void OnScreenLockedOrUnlocked(bool is_locked);
96 // Called when the Bluetooth adapter is initialized.
97 void OnBluetoothAdapterInitialized(
98 scoped_refptr<device::BluetoothAdapter> adapter);
100 // device::BluetoothAdapter::Observer:
101 void AdapterPresentChanged(device::BluetoothAdapter* adapter,
102 bool present) override;
103 void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
104 bool powered) override;
106 #if defined(OS_CHROMEOS)
107 // chromeos::PowerManagerClient::Observer:
108 void SuspendDone(const base::TimeDelta& sleep_duration) override;
109 #endif // defined(OS_CHROMEOS)
111 // Called when auth is attempted to send the sign-in challenge to the remote
112 // device for decryption.
113 void SendSignInChallenge();
115 // Returns the current state for the screen lock UI.
116 ScreenlockState GetScreenlockState();
118 // Updates the lock screen based on the manager's current state.
119 void UpdateLockScreen();
121 // Activates or deactivates the proximity monitor, as appropriate given the
122 // current state of |this| unlock manager.
123 void UpdateProximityMonitorState();
125 // Sets waking up state.
126 void SetWakingUpState(bool is_waking_up);
128 // Accepts or rejects the current auth attempt according to |should_accept|.
129 // If the auth attempt is accepted, unlocks the screen.
130 void AcceptAuthAttempt(bool should_accept);
132 // Returns the screen lock state corresponding to the given remote |status|
133 // update.
134 RemoteScreenlockState GetScreenlockStateFromRemoteUpdate(
135 RemoteStatusUpdate update);
137 // Whether |this| manager is being used for sign-in or session unlock.
138 const ScreenlockType screenlock_type_;
140 // Whether the user is present at the remote device. Unset if no remote status
141 // update has yet been received.
142 scoped_ptr<RemoteScreenlockState> remote_screenlock_state_;
144 // Controls the proximity auth flow logic. Not owned, and expcted to outlive
145 // |this| instance.
146 Controller* controller_;
148 // The client used to communicate with the remote device once a secure channel
149 // is established. Null if no secure channel has been established yet. Not
150 // owned, and expected to outlive |this| instance.
151 Client* client_;
153 // Tracks whether the remote device is currently in close enough proximity to
154 // the local device to allow unlocking.
155 scoped_ptr<ProximityMonitor> proximity_monitor_;
157 // Used to call into the embedder. Expected to outlive |this| instance.
158 ProximityAuthClient* proximity_auth_client_;
160 // Whether the screen is currently locked.
161 bool is_locked_;
163 // True if the manager is currently processing a user-initiated authentication
164 // attempt, which is initiated when the user pod is clicked.
165 bool is_attempting_auth_;
167 // Whether the system is waking up from sleep.
168 bool is_waking_up_;
170 // The Bluetooth adapter. Null if there is no adapter present on the local
171 // device.
172 scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_;
174 // The sign-in secret received from the remote device by decrypting the
175 // sign-in challenge.
176 scoped_ptr<std::string> sign_in_secret_;
178 // The state of the current screen lock UI.
179 ScreenlockState screenlock_state_;
181 // Used to clear the waking up state after a timeout.
182 base::WeakPtrFactory<UnlockManager> clear_waking_up_state_weak_ptr_factory_;
184 // Used to reject auth attempts after a timeout. An in-progress auth attempt
185 // blocks the sign-in screen UI, so it's important to prevent the auth attempt
186 // from blocking the UI in case a step in the code path hangs.
187 base::WeakPtrFactory<UnlockManager> reject_auth_attempt_weak_ptr_factory_;
189 // Used to vend all other weak pointers.
190 base::WeakPtrFactory<UnlockManager> weak_ptr_factory_;
192 DISALLOW_COPY_AND_ASSIGN(UnlockManager);
195 } // namespace proximity_auth
197 #endif // COMPONENTS_PROXIMITY_AUTH_UNLOCK_MANAGER_H