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 "components/proximity_auth/connection_observer.h"
15 #include "components/proximity_auth/cryptauth/cryptauth_client.h"
16 #include "components/proximity_auth/screenlock_bridge.h"
18 class PrefRegistrySimple
;
22 class BluetoothGattConnection
;
25 namespace proximity_auth
{
27 class BluetoothLowEnergyConnection
;
28 class BluetoothLowEnergyConnectionFinder
;
29 class BluetoothLowEnergyDeviceWhitelist
;
30 class BluetoothThrottler
;
32 class ConnectionFinder
;
33 class ProximityAuthClient
;
35 // This is the main entry point to start Proximity Auth over Bluetooth Low
36 // Energy. This is the underlying system for the Smart Lock features. It will
37 // discover Bluetooth Low Energy phones and unlock the lock screen if the phone
38 // passes an authorization and authentication protocol.
39 class ProximityAuthBleSystem
: public ScreenlockBridge::Observer
,
40 public ConnectionObserver
{
42 ProximityAuthBleSystem(
43 ScreenlockBridge
* screenlock_bridge
,
44 ProximityAuthClient
* proximity_auth_client
,
45 scoped_ptr
<CryptAuthClientFactory
> cryptauth_client_factory
,
46 PrefService
* pref_service
);
47 ~ProximityAuthBleSystem() override
;
49 // Registers the prefs used by this class
50 static void RegisterPrefs(PrefRegistrySimple
* registry
);
52 // ScreenlockBridge::Observer:
54 ScreenlockBridge::LockHandler::ScreenType screen_type
) override
;
55 void OnScreenDidUnlock(
56 ScreenlockBridge::LockHandler::ScreenType screen_type
) override
;
57 void OnFocusedUserChanged(const std::string
& user_id
) override
;
59 // proximity_auth::ConnectionObserver:
60 void OnConnectionStatusChanged(Connection
* connection
,
61 Connection::Status old_status
,
62 Connection::Status new_status
) override
;
63 void OnMessageReceived(const Connection
& connection
,
64 const WireMessage
& message
) override
;
67 class ScreenlockBridgeAdapter
{
69 ScreenlockBridgeAdapter(ScreenlockBridge
* screenlock_bridge
);
70 virtual ~ScreenlockBridgeAdapter();
72 virtual void AddObserver(ScreenlockBridge::Observer
* observer
);
73 virtual void RemoveObserver(ScreenlockBridge::Observer
* observer
);
74 virtual void Unlock(ProximityAuthClient
* client
);
77 ScreenlockBridgeAdapter();
80 // Not owned. Must outlive this object.
81 ScreenlockBridge
* screenlock_bridge_
;
85 ProximityAuthBleSystem(scoped_ptr
<ScreenlockBridgeAdapter
> screenlock_bridge
,
86 ProximityAuthClient
* proximity_auth_client
);
88 // Virtual for testing.
89 virtual ConnectionFinder
* CreateConnectionFinder();
92 // Fetches the the public keys of devices that can be used as unlock keys.
95 // Checks if the devices in |device_whitelist_| have their public keys
96 // registered in CryptAuth (|unlock_keys_|), removes the ones that do not.
97 void RemoveStaleWhitelistedDevices();
99 // Callbacks for cryptauth::CryptAuthClient::GetMyDevices.
100 void OnGetMyDevices(const cryptauth::GetMyDevicesResponse
& response
);
101 void OnGetMyDevicesError(const std::string
& error
);
103 // Handler for a new connection found event.
104 void OnConnectionFound(scoped_ptr
<Connection
> connection
);
106 // Start (recurrently) polling every |polling_interval_| ms for the screen
107 // state of the remote device.
108 void StartPollingScreenState();
110 // Stop polling for screen state of the remote device, if currently active.
111 void StopPollingScreenState();
113 // Checks if |message| contains a valid public key (registered in
114 // |unlock_keys_|). If so, returns the public key in |out_public_key|.
115 bool HasUnlockKey(const std::string
& message
, std::string
* out_public_key
);
117 scoped_ptr
<ScreenlockBridgeAdapter
> screenlock_bridge_
;
119 // Not owned. Must outlive this object.
120 ProximityAuthClient
* proximity_auth_client_
;
122 // Creates CryptAuth client instances to make API calls.
123 scoped_ptr
<CryptAuthClientFactory
> cryptauth_client_factory_
;
125 // We only support one concurrent API call.
126 scoped_ptr
<CryptAuthClient
> cryptauth_client_
;
128 // Maps devices public keys to the device friendly name.
129 std::map
<std::string
, std::string
> unlock_keys_
;
131 scoped_ptr
<ConnectionFinder
> connection_finder_
;
133 scoped_ptr
<Connection
> connection_
;
135 scoped_ptr
<BluetoothLowEnergyDeviceWhitelist
> device_whitelist_
;
137 scoped_ptr
<BluetoothThrottler
> bluetooth_throttler_
;
139 const base::TimeDelta polling_interval_
;
141 // True if the remote device sent public key contained in |unlock_keyes_| or
142 // |device_whitelist_|.
143 bool device_authenticated_
;
145 // True if the screen is locked and call to |screenlock_bridge_->Unlock()| was
146 // made, but |OnScreenDidUnlock| was not called yet. This is a guard to avoid
147 // a double |screenlock_bridge_->Unlock()| call.
148 bool unlock_requested_
;
150 bool is_polling_screen_state_
;
152 // True if a call to |GetUnlockKeys()| was already made.
153 bool unlock_keys_requested_
;
155 base::WeakPtrFactory
<ProximityAuthBleSystem
> weak_ptr_factory_
;
157 DISALLOW_COPY_AND_ASSIGN(ProximityAuthBleSystem
);
160 } // namespace proximity_auth
162 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_PROXIMITY_AUTH_BLE_SYSTEM_H_