1 // Copyright 2014 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 CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_HID_DETECTION_SCREEN_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_HID_DETECTION_SCREEN_HANDLER_H_
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/prefs/pref_registry_simple.h"
15 #include "base/values.h"
16 #include "chrome/browser/chromeos/device/input_service_proxy.h"
17 #include "chrome/browser/chromeos/login/screens/hid_detection_screen_actor.h"
18 #include "chrome/browser/ui/webui/chromeos/login/base_screen_handler.h"
19 #include "content/public/browser/web_ui.h"
20 #include "device/bluetooth/bluetooth_adapter.h"
21 #include "device/bluetooth/bluetooth_device.h"
22 #include "device/bluetooth/bluetooth_discovery_session.h"
25 class DictionaryValue;
32 // WebUI implementation of HIDDetectionScreenActor.
33 class HIDDetectionScreenHandler
34 : public HIDDetectionScreenActor,
35 public BaseScreenHandler,
36 public device::BluetoothAdapter::Observer,
37 public device::BluetoothDevice::PairingDelegate,
38 public InputServiceProxy::Observer {
40 typedef device::InputServiceLinux::InputDeviceInfo InputDeviceInfo;
42 explicit HIDDetectionScreenHandler(CoreOobeActor* core_oobe_actor);
43 virtual ~HIDDetectionScreenHandler();
45 // HIDDetectionScreenActor implementation:
46 virtual void Show() override;
47 virtual void Hide() override;
48 virtual void SetDelegate(Delegate* delegate) override;
49 virtual void CheckIsScreenRequired(
50 const base::Callback<void(bool)>& on_check_done) override;
52 // BaseScreenHandler implementation:
53 virtual void DeclareLocalizedValues(LocalizedValuesBuilder* builder) override;
54 virtual void Initialize() override;
56 // WebUIMessageHandler implementation:
57 virtual void RegisterMessages() override;
59 // device::BluetoothDevice::PairingDelegate implementation:
60 virtual void RequestPinCode(device::BluetoothDevice* device) override;
61 virtual void RequestPasskey(device::BluetoothDevice* device) override;
62 virtual void DisplayPinCode(device::BluetoothDevice* device,
63 const std::string& pincode) override;
64 virtual void DisplayPasskey(
65 device::BluetoothDevice* device, uint32 passkey) override;
66 virtual void KeysEntered(device::BluetoothDevice* device,
67 uint32 entered) override;
68 virtual void ConfirmPasskey(
69 device::BluetoothDevice* device, uint32 passkey) override;
70 virtual void AuthorizePairing(device::BluetoothDevice* device) override;
72 // device::BluetoothAdapter::Observer implementation.
73 virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter,
74 bool present) override;
75 virtual void DeviceAdded(device::BluetoothAdapter* adapter,
76 device::BluetoothDevice* device) override;
77 virtual void DeviceChanged(device::BluetoothAdapter* adapter,
78 device::BluetoothDevice* device) override;
79 virtual void DeviceRemoved(device::BluetoothAdapter* adapter,
80 device::BluetoothDevice* device) override;
82 // InputServiceProxy::Observer implementation.
83 virtual void OnInputDeviceAdded(const InputDeviceInfo& info) override;
84 virtual void OnInputDeviceRemoved(const std::string& id) override;
86 // Registers the preference for derelict state.
87 static void RegisterPrefs(PrefRegistrySimple* registry);
90 // Types of dialog leaving scenarios for UMA metric.
91 enum ContinueScenarioType {
92 // Only pointing device detected, user pressed 'Continue'.
93 POINTING_DEVICE_ONLY_DETECTED,
95 // Only keyboard detected, user pressed 'Continue'.
96 KEYBOARD_DEVICE_ONLY_DETECTED,
98 // All devices detected.
101 // Must be last enum element.
102 CONTINUE_SCENARIO_TYPE_SIZE
105 void InitializeAdapter(scoped_refptr<device::BluetoothAdapter> adapter);
107 // Sends a notification to the Web UI of the status of available Bluetooth/USB
109 void SendPointingDeviceNotification();
111 // Sends a notification to the Web UI of the status of available Bluetooth/USB
113 void SendKeyboardDeviceNotification(base::DictionaryValue* params);
115 // Updates internal state and UI (if ready) using list of connected devices.
116 void ProcessConnectedDevicesList(const std::vector<InputDeviceInfo>& devices);
118 // Checks for lack of mouse or keyboard. If found starts BT devices update.
119 // Initiates BTAdapter if it's not active and BT devices update required.
120 void TryInitiateBTDevicesUpdate();
122 // Processes list of input devices returned by InputServiceProxy on the check
123 // request. Calls the callback that expects true if screen is required.
124 void OnGetInputDevicesListForCheck(
125 const base::Callback<void(bool)>& on_check_done,
126 const std::vector<InputDeviceInfo>& devices);
128 // Processes list of input devices returned by InputServiceProxy on regular
130 void OnGetInputDevicesList(const std::vector<InputDeviceInfo>& devices);
132 void StartBTDiscoverySession();
134 // Called by device::BluetoothDevice on a successful pairing and connection
136 void BTConnected(device::BluetoothDevice::DeviceType device_type);
138 // Called by device::BluetoothDevice in response to a failure to
139 // connect to the device with bluetooth address |address| due to an error
140 // encoded in |error_code|.
141 void BTConnectError(const std::string& address,
142 device::BluetoothDevice::DeviceType device_type,
143 device::BluetoothDevice::ConnectErrorCode error_code);
145 // JS messages handlers.
146 void HandleOnContinue();
150 CoreOobeActor* core_oobe_actor_;
152 // Keeps whether screen should be shown right after initialization.
155 // Displays in the UI a connecting to the device |device| message.
156 void DeviceConnecting(device::BluetoothDevice* device);
158 // Called by device::BluetoothAdapter in response to a successful request
159 // to initiate a discovery session.
160 void OnStartDiscoverySession(
161 scoped_ptr<device::BluetoothDiscoverySession> discovery_session);
163 // Called by device::BluetoothAdapter in response to a failure to
164 // initiate a discovery session.
165 void FindDevicesError();
167 // Called by device::BluetoothAdapter in response to a failure to
169 void SetPoweredError();
171 // Called by device::BluetoothAdapter in response to a failure to
172 // power off BT adapter.
173 void SetPoweredOffError();
175 // Called for revision of active devices. If current-placement is available
176 // for mouse or keyboard device, sets one of active devices as current or
177 // tries to connect some BT device if no appropriate devices are connected.
178 void UpdateDevices();
180 // Tries to connect some BT devices if no type-appropriate devices are
182 void UpdateBTDevices();
184 // Tries to connect given BT device.
185 void ConnectBTDevice(device::BluetoothDevice* device);
187 // Tries to connect given BT device as pointing one.
188 void TryPairingAsPointingDevice(device::BluetoothDevice* device);
190 // Tries to connect given BT device as keyboard.
191 void TryPairingAsKeyboardDevice(device::BluetoothDevice* device);
193 // Default bluetooth adapter, used for all operations.
194 scoped_refptr<device::BluetoothAdapter> adapter_;
196 InputServiceProxy input_service_proxy_;
198 // The current device discovery session. Only one active discovery session is
199 // kept at a time and the instance that |discovery_session_| points to gets
200 // replaced by a new one when a new discovery session is initiated.
201 scoped_ptr<device::BluetoothDiscoverySession> discovery_session_;
203 // Current pointing device, if any.
204 std::string pointing_device_name_;
205 std::string pointing_device_id_;
206 bool mouse_is_pairing_;
207 InputDeviceInfo::Type pointing_device_connect_type_;
209 // Current keyboard device, if any.
210 std::string keyboard_device_name_;
211 std::string keyboard_device_id_;
212 bool keyboard_is_pairing_;
213 InputDeviceInfo::Type keyboard_device_connect_type_;
215 bool switch_on_adapter_when_ready_;
217 // State of BT adapter before screen-initiated changes.
218 scoped_ptr<bool> adapter_initially_powered_;
220 base::WeakPtrFactory<HIDDetectionScreenHandler> weak_ptr_factory_;
222 DISALLOW_COPY_AND_ASSIGN(HIDDetectionScreenHandler);
225 } // namespace chromeos
227 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_HID_DETECTION_SCREEN_HANDLER_H_