[Sync] Componentize HistoryModelWorker.
[chromium-blink-merge.git] / components / proximity_auth / ble / proximity_auth_ble_system.h
blobf3b64981806ec1f5ce605a6f48a0c24282204585
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 "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;
20 class PrefService;
22 namespace device {
23 class BluetoothGattConnection;
26 namespace proximity_auth {
28 class BluetoothLowEnergyConnection;
29 class BluetoothLowEnergyConnectionFinder;
30 class BluetoothLowEnergyDeviceWhitelist;
31 class BluetoothThrottler;
32 class Connection;
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 {
42 public:
43 ProximityAuthBleSystem(
44 ScreenlockBridge* screenlock_bridge,
45 ProximityAuthClient* proximity_auth_client,
46 PrefService* pref_service);
47 ~ProximityAuthBleSystem() override;
49 // Registers the prefs used by this class
50 static void RegisterPrefs(PrefRegistrySimple* registry);
52 // ScreenlockBridge::Observer:
53 void OnScreenDidLock(
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;
66 // Called by EasyUnlockService when the user clicks their user pod and
67 // initiates an auth attempt.
68 void OnAuthAttempted(const std::string& user_id);
70 protected:
71 // Virtual for testing.
72 virtual ConnectionFinder* CreateConnectionFinder();
74 private:
75 // Checks if the devices in |device_whitelist_| have their public keys
76 // registered in CryptAuth, removes the ones that do not.
77 void RemoveStaleWhitelistedDevices();
79 // Handler for a new connection found event.
80 void OnConnectionFound(scoped_ptr<Connection> connection);
82 // Start (recurrently) polling every |polling_interval_| ms for the screen
83 // state of the remote device.
84 void StartPollingScreenState();
86 // Stop polling for screen state of the remote device, if currently active.
87 void StopPollingScreenState();
89 // Returns true if any unlock key registered with CryptAuth is a BLE device.
90 bool IsAnyUnlockKeyBLE();
92 // Checks if |message| contains a valid whitelisted public key. If so, returns
93 // the public key in |out_public_key|.
94 bool HasUnlockKey(const std::string& message, std::string* out_public_key);
96 // Called when |spinner_timer_| is fired to stop showing the spinner on the
97 // user pod.
98 void OnSpinnerTimerFired();
100 // Called to update the lock screen's UI for the current authentication state
101 // with the remote device.
102 void UpdateLockScreenUI();
104 ScreenlockBridge* screenlock_bridge_;
106 // Not owned. Must outlive this object.
107 ProximityAuthClient* proximity_auth_client_;
109 scoped_ptr<ConnectionFinder> connection_finder_;
111 scoped_ptr<Connection> connection_;
113 scoped_ptr<BluetoothLowEnergyDeviceWhitelist> device_whitelist_;
115 scoped_ptr<BluetoothThrottler> bluetooth_throttler_;
117 const base::TimeDelta polling_interval_;
119 // True if the remote device sent public key contained in |unlock_keyes_| or
120 // |device_whitelist_|.
121 bool device_authenticated_;
123 // True if |this| instance is currently polling the phone for the phone's
124 // screenlock state.
125 bool is_polling_screen_state_;
127 // The user id or email of the last focused user on the lock screen.
128 std::string last_focused_user_;
130 // True if the the last status update from the phone reports that the phone's
131 // screen is locked. If no status update has been received, this value is true
132 // by default.
133 bool is_remote_screen_locked_;
135 // The timer controlling the time the spinner for the user pod icon is shown
136 // right after the screen is locked.
137 base::OneShotTimer<ProximityAuthBleSystem> spinner_timer_;
139 // The different UI states that the lock screen can be in.
140 enum class ScreenlockUIState {
141 NO_SCREENLOCK,
142 SPINNER,
143 UNAUTHENTICATED,
144 AUTHENTICATED
146 ScreenlockUIState screenlock_ui_state_;
148 base::WeakPtrFactory<ProximityAuthBleSystem> weak_ptr_factory_;
150 DISALLOW_COPY_AND_ASSIGN(ProximityAuthBleSystem);
153 } // namespace proximity_auth
155 #endif // COMPONENTS_PROXIMITY_AUTH_BLE_PROXIMITY_AUTH_BLE_SYSTEM_H_