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;
50 // BaseScreenHandler implementation:
51 virtual void DeclareLocalizedValues(LocalizedValuesBuilder* builder) OVERRIDE;
52 virtual void Initialize() OVERRIDE;
54 // WebUIMessageHandler implementation:
55 virtual void RegisterMessages() OVERRIDE;
57 // device::BluetoothDevice::PairingDelegate implementation:
58 virtual void RequestPinCode(device::BluetoothDevice* device) OVERRIDE;
59 virtual void RequestPasskey(device::BluetoothDevice* device) OVERRIDE;
60 virtual void DisplayPinCode(device::BluetoothDevice* device,
61 const std::string& pincode) OVERRIDE;
62 virtual void DisplayPasskey(
63 device::BluetoothDevice* device, uint32 passkey) OVERRIDE;
64 virtual void KeysEntered(device::BluetoothDevice* device,
65 uint32 entered) OVERRIDE;
66 virtual void ConfirmPasskey(
67 device::BluetoothDevice* device, uint32 passkey) OVERRIDE;
68 virtual void AuthorizePairing(device::BluetoothDevice* device) OVERRIDE;
70 // device::BluetoothAdapter::Observer implementation.
71 virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter,
72 bool present) OVERRIDE;
73 virtual void DeviceAdded(device::BluetoothAdapter* adapter,
74 device::BluetoothDevice* device) OVERRIDE;
75 virtual void DeviceChanged(device::BluetoothAdapter* adapter,
76 device::BluetoothDevice* device) OVERRIDE;
77 virtual void DeviceRemoved(device::BluetoothAdapter* adapter,
78 device::BluetoothDevice* device) OVERRIDE;
80 // InputServiceProxy::Observer implementation.
81 virtual void OnInputDeviceAdded(const InputDeviceInfo& info) OVERRIDE;
82 virtual void OnInputDeviceRemoved(const std::string& id) OVERRIDE;
84 // Registers the preference for derelict state.
85 static void RegisterPrefs(PrefRegistrySimple* registry);
88 // Types of dialog leaving scenarios for UMA metric.
89 enum ContinueScenarioType {
90 // Only pointing device detected, user pressed 'Continue'.
91 POINTING_DEVICE_ONLY_DETECTED,
93 // Only keyboard detected, user pressed 'Continue'.
94 KEYBOARD_DEVICE_ONLY_DETECTED,
96 // All devices detected.
99 // Must be last enum element.
100 CONTINUE_SCENARIO_TYPE_SIZE
103 void InitializeAdapter(scoped_refptr<device::BluetoothAdapter> adapter);
105 // Sends a notification to the Web UI of the status of available Bluetooth/USB
107 void SendPointingDeviceNotification();
109 // Sends a notification to the Web UI of the status of available Bluetooth/USB
111 void SendKeyboardDeviceNotification(base::DictionaryValue* params);
113 // Updates internal state and UI using list of connected devices.
114 void ProcessConnectedDevicesList(const std::vector<InputDeviceInfo>& devices);
116 // Checks for lack of mouse or keyboard. If found starts BT devices update.
117 // Initiates BTAdapter if it's not active and BT devices update required.
118 void TryInitiateBTDevicesUpdate();
120 // Processes list of input devices returned by InputServiceProxy on the first
121 // time the screen is initiated. Skips the screen if all required devices are
123 void OnGetInputDevicesListFirstTime(
124 const std::vector<InputDeviceInfo>& devices);
126 // Processes list of input devices returned by InputServiceProxy on regular
128 void OnGetInputDevicesList(const std::vector<InputDeviceInfo>& devices);
130 void StartBTDiscoverySession();
132 // Called by device::BluetoothDevice on a successful pairing and connection
134 void BTConnected(device::BluetoothDevice::DeviceType device_type);
136 // Called by device::BluetoothDevice in response to a failure to
137 // connect to the device with bluetooth address |address| due to an error
138 // encoded in |error_code|.
139 void BTConnectError(const std::string& address,
140 device::BluetoothDevice::DeviceType device_type,
141 device::BluetoothDevice::ConnectErrorCode error_code);
143 // JS messages handlers.
144 void HandleOnContinue();
148 CoreOobeActor* core_oobe_actor_;
150 // Keeps whether screen should be shown right after initialization.
153 // Displays in the UI a connecting to the device |device| message.
154 void DeviceConnecting(device::BluetoothDevice* device);
156 // Called by device::BluetoothAdapter in response to a successful request
157 // to initiate a discovery session.
158 void OnStartDiscoverySession(
159 scoped_ptr<device::BluetoothDiscoverySession> discovery_session);
161 // Called by device::BluetoothAdapter in response to a failure to
162 // initiate a discovery session.
163 void FindDevicesError();
165 // Called by device::BluetoothAdapter in response to a failure to
167 void SetPoweredError();
169 // Special case uf UpdateDevice. Called on first show, skips the dialog if
170 // all necessary devices (mouse and keyboard) already connected.
171 void GetDevicesFirstTime();
173 // Called for revision of active devices. If current-placement is available
174 // for mouse or keyboard device, sets one of active devices as current or
175 // tries to connect some BT device if no appropriate devices are connected.
176 void UpdateDevices();
178 // Tries to connect some BT devices if no type-appropriate devices are
180 void UpdateBTDevices();
182 // Tries to connect given BT device.
183 void ConnectBTDevice(device::BluetoothDevice* device);
185 // Tries to connect given BT device as pointing one.
186 void TryPairingAsPointingDevice(device::BluetoothDevice* device);
188 // Tries to connect given BT device as keyboard.
189 void TryPairingAsKeyboardDevice(device::BluetoothDevice* device);
191 // Default bluetooth adapter, used for all operations.
192 scoped_refptr<device::BluetoothAdapter> adapter_;
194 InputServiceProxy input_service_proxy_;
196 // The current device discovery session. Only one active discovery session is
197 // kept at a time and the instance that |discovery_session_| points to gets
198 // replaced by a new one when a new discovery session is initiated.
199 scoped_ptr<device::BluetoothDiscoverySession> discovery_session_;
201 // Current pointing device, if any.
202 std::string pointing_device_name_;
203 std::string pointing_device_id_;
204 bool mouse_is_pairing_;
205 InputDeviceInfo::Type pointing_device_connect_type_;
207 // Current keyboard device, if any.
208 std::string keyboard_device_name_;
209 std::string keyboard_device_id_;
210 bool keyboard_is_pairing_;
211 InputDeviceInfo::Type keyboard_device_connect_type_;
213 bool switch_on_adapter_when_ready_;
215 bool first_time_screen_show_;
217 base::WeakPtrFactory<HIDDetectionScreenHandler> weak_ptr_factory_;
219 DISALLOW_COPY_AND_ASSIGN(HIDDetectionScreenHandler);
222 } // namespace chromeos
224 #endif // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_HID_DETECTION_SCREEN_HANDLER_H_