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_BLE_PROXIMITY_AUTH_BLE_SYSTEM_H_
6 #define COMPONENTS_PROXIMITY_AUTH_BLE_PROXIMITY_AUTH_BLE_SYSTEM_H_
11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/time/time.h"
14 #include "base/timer/timer.h"
15 #include "components/proximity_auth/connection_observer.h"
16 #include "components/proximity_auth/cryptauth/cryptauth_client.h"
17 #include "components/proximity_auth/screenlock_bridge.h"
19 class PrefRegistrySimple
;
23 class BluetoothGattConnection
;
26 namespace proximity_auth
{
28 class BluetoothLowEnergyConnection
;
29 class BluetoothLowEnergyConnectionFinder
;
30 class BluetoothLowEnergyDeviceWhitelist
;
31 class BluetoothThrottler
;
33 class ConnectionFinder
;
34 class ProximityAuthClient
;
36 // This is the main entry point to start Proximity Auth over Bluetooth Low
37 // Energy. This is the underlying system for the Smart Lock features. It will
38 // discover Bluetooth Low Energy phones and unlock the lock screen if the phone
39 // passes an authorization and authentication protocol.
40 class ProximityAuthBleSystem
: public ScreenlockBridge::Observer
,
41 public ConnectionObserver
{
43 ProximityAuthBleSystem(
44 ScreenlockBridge
* screenlock_bridge
,
45 ProximityAuthClient
* proximity_auth_client
,
46 scoped_ptr
<CryptAuthClientFactory
> cryptauth_client_factory
,
47 PrefService
* pref_service
);
48 ~ProximityAuthBleSystem() override
;
50 // Registers the prefs used by this class
51 static void RegisterPrefs(PrefRegistrySimple
* registry
);
53 // ScreenlockBridge::Observer:
55 ScreenlockBridge::LockHandler::ScreenType screen_type
) override
;
56 void OnScreenDidUnlock(
57 ScreenlockBridge::LockHandler::ScreenType screen_type
) override
;
58 void OnFocusedUserChanged(const std::string
& user_id
) override
;
60 // proximity_auth::ConnectionObserver:
61 void OnConnectionStatusChanged(Connection
* connection
,
62 Connection::Status old_status
,
63 Connection::Status new_status
) override
;
64 void OnMessageReceived(const Connection
& connection
,
65 const WireMessage
& message
) override
;
67 // Called by EasyUnlockService when the user clicks their user pod and
68 // initiates an auth attempt.
69 void OnAuthAttempted(const std::string
& user_id
);
72 // Virtual for testing.
73 virtual ConnectionFinder
* CreateConnectionFinder();
76 // Fetches the the public keys of devices that can be used as unlock keys.
79 // Checks if the devices in |device_whitelist_| have their public keys
80 // registered in CryptAuth (|unlock_keys_|), removes the ones that do not.
81 void RemoveStaleWhitelistedDevices();
83 // Callbacks for cryptauth::CryptAuthClient::GetMyDevices.
84 void OnGetMyDevices(const cryptauth::GetMyDevicesResponse
& response
);
85 void OnGetMyDevicesError(const std::string
& error
);
87 // Handler for a new connection found event.
88 void OnConnectionFound(scoped_ptr
<Connection
> connection
);
90 // Start (recurrently) polling every |polling_interval_| ms for the screen
91 // state of the remote device.
92 void StartPollingScreenState();
94 // Stop polling for screen state of the remote device, if currently active.
95 void StopPollingScreenState();
97 // Checks if |message| contains a valid public key (registered in
98 // |unlock_keys_|). If so, returns the public key in |out_public_key|.
99 bool HasUnlockKey(const std::string
& message
, std::string
* out_public_key
);
101 // Called when |spinner_timer_| is fired to stop showing the spinner on the
103 void OnSpinnerTimerFired();
105 // Called to update the lock screen's UI for the current authentication state
106 // with the remote device.
107 void UpdateLockScreenUI();
109 ScreenlockBridge
* screenlock_bridge_
;
111 // Not owned. Must outlive this object.
112 ProximityAuthClient
* proximity_auth_client_
;
114 // Creates CryptAuth client instances to make API calls.
115 scoped_ptr
<CryptAuthClientFactory
> cryptauth_client_factory_
;
117 // We only support one concurrent API call.
118 scoped_ptr
<CryptAuthClient
> cryptauth_client_
;
120 // Maps devices public keys to the device friendly name.
121 std::map
<std::string
, std::string
> unlock_keys_
;
123 scoped_ptr
<ConnectionFinder
> connection_finder_
;
125 scoped_ptr
<Connection
> connection_
;
127 scoped_ptr
<BluetoothLowEnergyDeviceWhitelist
> device_whitelist_
;
129 scoped_ptr
<BluetoothThrottler
> bluetooth_throttler_
;
131 const base::TimeDelta polling_interval_
;
133 // True if the remote device sent public key contained in |unlock_keyes_| or
134 // |device_whitelist_|.
135 bool device_authenticated_
;
137 // True if |this| instance is currently polling the phone for the phone's
139 bool is_polling_screen_state_
;
141 // True if a call to |GetUnlockKeys()| was already made.
142 bool unlock_keys_requested_
;
144 // The user id or email of the last focused user on the lock screen.
145 std::string last_focused_user_
;
147 // True if the the last status update from the phone reports that the phone's
148 // screen is locked. If no status update has been received, this value is true
150 bool is_remote_screen_locked_
;
152 // The timer controlling the time the spinner for the user pod icon is shown
153 // right after the screen is locked.
154 base::OneShotTimer
<ProximityAuthBleSystem
> spinner_timer_
;
156 // The different UI states that the lock screen can be in.
157 enum class ScreenlockUIState
{
163 ScreenlockUIState screenlock_ui_state_
;
165 base::WeakPtrFactory
<ProximityAuthBleSystem
> weak_ptr_factory_
;
167 DISALLOW_COPY_AND_ASSIGN(ProximityAuthBleSystem
);
170 } // namespace proximity_auth
172 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_PROXIMITY_AUTH_BLE_SYSTEM_H_