Extension syncing: Introduce a NeedsSync pref
[chromium-blink-merge.git] / components / proximity_auth / ble / proximity_auth_ble_system.h
blobf40c9f22d3c38fbf98497b6c8896cbf7494ca919
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_
8 #include <map>
9 #include <string>
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;
19 class PrefService;
21 namespace device {
22 class BluetoothGattConnection;
25 namespace proximity_auth {
27 class BluetoothLowEnergyConnection;
28 class BluetoothLowEnergyConnectionFinder;
29 class BluetoothLowEnergyDeviceWhitelist;
30 class Connection;
31 class ConnectionFinder;
32 class ProximityAuthClient;
34 // This is the main entry point to start Proximity Auth over Bluetooth Low
35 // Energy. This is the underlying system for the Smart Lock features. It will
36 // discover Bluetooth Low Energy phones and unlock the lock screen if the phone
37 // passes an authorization and authentication protocol.
38 class ProximityAuthBleSystem : public ScreenlockBridge::Observer,
39 public ConnectionObserver {
40 public:
41 ProximityAuthBleSystem(
42 ScreenlockBridge* screenlock_bridge,
43 ProximityAuthClient* proximity_auth_client,
44 scoped_ptr<CryptAuthClientFactory> cryptauth_client_factory,
45 PrefService* pref_service);
46 ~ProximityAuthBleSystem() override;
48 // Registers the prefs used by this class
49 static void RegisterPrefs(PrefRegistrySimple* registry);
51 // ScreenlockBridge::Observer:
52 void OnScreenDidLock(
53 ScreenlockBridge::LockHandler::ScreenType screen_type) override;
54 void OnScreenDidUnlock(
55 ScreenlockBridge::LockHandler::ScreenType screen_type) override;
56 void OnFocusedUserChanged(const std::string& user_id) override;
58 // proximity_auth::ConnectionObserver:
59 void OnConnectionStatusChanged(Connection* connection,
60 Connection::Status old_status,
61 Connection::Status new_status) override;
62 void OnMessageReceived(const Connection& connection,
63 const WireMessage& message) override;
65 protected:
66 class ScreenlockBridgeAdapter {
67 public:
68 ScreenlockBridgeAdapter(ScreenlockBridge* screenlock_bridge);
69 virtual ~ScreenlockBridgeAdapter();
71 virtual void AddObserver(ScreenlockBridge::Observer* observer);
72 virtual void RemoveObserver(ScreenlockBridge::Observer* observer);
73 virtual void Unlock(ProximityAuthClient* client);
75 protected:
76 ScreenlockBridgeAdapter();
78 private:
79 // Not owned. Must outlive this object.
80 ScreenlockBridge* screenlock_bridge_;
83 // Used for testing.
84 ProximityAuthBleSystem(scoped_ptr<ScreenlockBridgeAdapter> screenlock_bridge,
85 ProximityAuthClient* proximity_auth_client);
87 // Virtual for testing.
88 virtual ConnectionFinder* CreateConnectionFinder();
90 private:
91 // Fetches the the public keys of devices that can be used as unlock keys.
92 void GetUnlockKeys();
94 // Checks if the devices in |device_whitelist_| have their public keys
95 // registered in CryptAuth (|unlock_keys_|), removes the ones that do not.
96 void RemoveStaleWhitelistedDevices();
98 // Callbacks for cryptauth::CryptAuthClient::GetMyDevices.
99 void OnGetMyDevices(const cryptauth::GetMyDevicesResponse& response);
100 void OnGetMyDevicesError(const std::string& error);
102 // Handler for a new connection found event.
103 void OnConnectionFound(scoped_ptr<Connection> connection);
105 // Start (recurrently) polling every |polling_interval_| ms for the screen
106 // state of the remote device.
107 void StartPollingScreenState();
109 // Stop polling for screen state of the remote device, if currently active.
110 void StopPollingScreenState();
112 // Checks if |message| contains a valid public key (registered in
113 // |unlock_keys_|). If so, returns the public key in |out_public_key|.
114 bool HasUnlockKey(const std::string& message, std::string* out_public_key);
116 scoped_ptr<ScreenlockBridgeAdapter> screenlock_bridge_;
118 // Not owned. Must outlive this object.
119 ProximityAuthClient* proximity_auth_client_;
121 // Creates CryptAuth client instances to make API calls.
122 scoped_ptr<CryptAuthClientFactory> cryptauth_client_factory_;
124 // We only support one concurrent API call.
125 scoped_ptr<CryptAuthClient> cryptauth_client_;
127 // Maps devices public keys to the device friendly name.
128 std::map<std::string, std::string> unlock_keys_;
130 scoped_ptr<ConnectionFinder> connection_finder_;
132 scoped_ptr<Connection> connection_;
134 scoped_ptr<BluetoothLowEnergyDeviceWhitelist> device_whitelist_;
136 const base::TimeDelta polling_interval_;
138 // True if the remote device sent public key contained in |unlock_keyes_| or
139 // |device_whitelist_|.
140 bool device_authenticated_;
142 // True if the screen is locked and call to |screenlock_bridge_->Unlock()| was
143 // made, but |OnScreenDidUnlock| was not called yet. This is a guard to avoid
144 // a double |screenlock_bridge_->Unlock()| call.
145 bool unlock_requested_;
147 bool is_polling_screen_state_;
149 base::WeakPtrFactory<ProximityAuthBleSystem> weak_ptr_factory_;
151 DISALLOW_COPY_AND_ASSIGN(ProximityAuthBleSystem);
154 } // namespace proximity_auth
156 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_PROXIMITY_AUTH_BLE_SYSTEM_H_