Home | History | Annotate | Download | only in login
      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 by device::BluetoothAdapter in response to a failure to
    174   // power off BT adapter.
    175   void SetPoweredOffError();
    176 
    177   // Called for revision of active devices. If current-placement is available
    178   // for mouse or keyboard device, sets one of active devices as current or
    179   // tries to connect some BT device if no appropriate devices are connected.
    180   void UpdateDevices();
    181 
    182   // Tries to connect some BT devices if no type-appropriate devices are
    183   // connected.
    184   void UpdateBTDevices();
    185 
    186   // Tries to connect given BT device.
    187   void ConnectBTDevice(device::BluetoothDevice* device);
    188 
    189   // Tries to connect given BT device as pointing one.
    190   void TryPairingAsPointingDevice(device::BluetoothDevice* device);
    191 
    192   // Tries to connect given BT device as keyboard.
    193   void TryPairingAsKeyboardDevice(device::BluetoothDevice* device);
    194 
    195   // Default bluetooth adapter, used for all operations.
    196   scoped_refptr<device::BluetoothAdapter> adapter_;
    197 
    198   InputServiceProxy input_service_proxy_;
    199 
    200   // The current device discovery session. Only one active discovery session is
    201   // kept at a time and the instance that |discovery_session_| points to gets
    202   // replaced by a new one when a new discovery session is initiated.
    203   scoped_ptr<device::BluetoothDiscoverySession> discovery_session_;
    204 
    205   // Current pointing device, if any.
    206   std::string pointing_device_name_;
    207   std::string pointing_device_id_;
    208   bool mouse_is_pairing_;
    209   InputDeviceInfo::Type pointing_device_connect_type_;
    210 
    211   // Current keyboard device, if any.
    212   std::string keyboard_device_name_;
    213   std::string keyboard_device_id_;
    214   bool keyboard_is_pairing_;
    215   InputDeviceInfo::Type keyboard_device_connect_type_;
    216 
    217   bool switch_on_adapter_when_ready_;
    218 
    219   bool first_time_screen_show_;
    220 
    221   // State of BT adapter before screen-initiated changes.
    222   scoped_ptr<bool> adapter_initially_powered_;
    223 
    224   base::WeakPtrFactory<HIDDetectionScreenHandler> weak_ptr_factory_;
    225 
    226   DISALLOW_COPY_AND_ASSIGN(HIDDetectionScreenHandler);
    227 };
    228 
    229 }  // namespace chromeos
    230 
    231 #endif  // CHROME_BROWSER_UI_WEBUI_CHROMEOS_LOGIN_HID_DETECTION_SCREEN_HANDLER_H_
    232 
    233