f99074517afbc4ada018cdd42b8a34deea6bf191
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / chromeos / login / hid_detection_screen_handler.h
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.
4
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_
7
8 #include <string>
9 #include <vector>
10
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"
23
24 namespace base {
25 class DictionaryValue;
26 }
27
28 namespace chromeos {
29
30 class CoreOobeActor;
31
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 {
39  public:
40   typedef device::InputServiceLinux::InputDeviceInfo InputDeviceInfo;
41
42   explicit HIDDetectionScreenHandler(CoreOobeActor* core_oobe_actor);
43   virtual ~HIDDetectionScreenHandler();
44
45   // HIDDetectionScreenActor implementation:
46   virtual void Show() OVERRIDE;
47   virtual void Hide() OVERRIDE;
48   virtual void SetDelegate(Delegate* delegate) OVERRIDE;
49
50   // BaseScreenHandler implementation:
51   virtual void DeclareLocalizedValues(LocalizedValuesBuilder* builder) OVERRIDE;
52   virtual void Initialize() OVERRIDE;
53
54   // WebUIMessageHandler implementation:
55   virtual void RegisterMessages() OVERRIDE;
56
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;
69
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;
79
80   // InputServiceProxy::Observer implementation.
81   virtual void OnInputDeviceAdded(const InputDeviceInfo& info) OVERRIDE;
82   virtual void OnInputDeviceRemoved(const std::string& id) OVERRIDE;
83
84   // Registers the preference for derelict state.
85   static void RegisterPrefs(PrefRegistrySimple* registry);
86
87  private:
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,
92
93     // Only keyboard detected, user pressed 'Continue'.
94     KEYBOARD_DEVICE_ONLY_DETECTED,
95
96     // All devices detected.
97     All_DEVICES_DETECTED,
98
99     // Must be last enum element.
100     CONTINUE_SCENARIO_TYPE_SIZE
101   };
102
103   void InitializeAdapter(scoped_refptr<device::BluetoothAdapter> adapter);
104
105   // Sends a notification to the Web UI of the status of available Bluetooth/USB
106   // pointing device.
107   void SendPointingDeviceNotification();
108
109   // Sends a notification to the Web UI of the status of available Bluetooth/USB
110   // keyboard device.
111   void SendKeyboardDeviceNotification(base::DictionaryValue* params);
112
113   // Updates internal state and UI using list of connected devices.
114   void ProcessConnectedDevicesList(const std::vector<InputDeviceInfo>& devices);
115
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();
119
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
122   // present.
123   void OnGetInputDevicesListFirstTime(
124       const std::vector<InputDeviceInfo>& devices);
125
126   // Processes list of input devices returned by InputServiceProxy on regular
127   // request.
128   void OnGetInputDevicesList(const std::vector<InputDeviceInfo>& devices);
129
130   void StartBTDiscoverySession();
131
132   // Called by device::BluetoothDevice on a successful pairing and connection
133   // to a device.
134   void BTConnected(device::BluetoothDevice::DeviceType device_type);
135
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);
142
143   // JS messages handlers.
144   void HandleOnContinue();
145
146   Delegate* delegate_;
147
148   CoreOobeActor* core_oobe_actor_;
149
150   // Keeps whether screen should be shown right after initialization.
151   bool show_on_init_;
152
153   // Displays in the UI a connecting to the device |device| message.
154   void DeviceConnecting(device::BluetoothDevice* device);
155
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);
160
161   // Called by device::BluetoothAdapter in response to a failure to
162   // initiate a discovery session.
163   void FindDevicesError();
164
165   // Called by device::BluetoothAdapter in response to a failure to
166   // power BT adapter.
167   void SetPoweredError();
168
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();
172
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();
177
178   // Tries to connect some BT devices if no type-appropriate devices are
179   // connected.
180   void UpdateBTDevices();
181
182   // Tries to connect given BT device.
183   void ConnectBTDevice(device::BluetoothDevice* device);
184
185   // Tries to connect given BT device as pointing one.
186   void TryPairingAsPointingDevice(device::BluetoothDevice* device);
187
188   // Tries to connect given BT device as keyboard.
189   void TryPairingAsKeyboardDevice(device::BluetoothDevice* device);
190
191   // Default bluetooth adapter, used for all operations.
192   scoped_refptr<device::BluetoothAdapter> adapter_;
193
194   InputServiceProxy input_service_proxy_;
195
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_;
200
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_;
206
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_;
212
213   bool switch_on_adapter_when_ready_;
214
215   bool first_time_screen_show_;
216
217   base::WeakPtrFactory<HIDDetectionScreenHandler> weak_ptr_factory_;
218
219   DISALLOW_COPY_AND_ASSIGN(HIDDetectionScreenHandler);
220 };
221
222 }  // namespace chromeos
223
224 #endif  // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_HID_DETECTION_SCREEN_HANDLER_H_
225