Home | History | Annotate | Download | only in chromeos
      1 // Copyright (c) 2012 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_OPTIONS_CHROMEOS_BLUETOOTH_OPTIONS_HANDLER_H_
      6 #define CHROME_BROWSER_UI_WEBUI_OPTIONS_CHROMEOS_BLUETOOTH_OPTIONS_HANDLER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/callback.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "chrome/browser/ui/webui/options/options_ui.h"
     15 #include "device/bluetooth/bluetooth_adapter.h"
     16 #include "device/bluetooth/bluetooth_device.h"
     17 
     18 namespace base {
     19 class DictionaryValue;
     20 }
     21 
     22 namespace chromeos {
     23 namespace options {
     24 
     25 // Handler for Bluetooth options on the system options page.
     26 class BluetoothOptionsHandler
     27     : public ::options::OptionsPageUIHandler,
     28       public device::BluetoothAdapter::Observer,
     29       public device::BluetoothDevice::PairingDelegate {
     30  public:
     31   BluetoothOptionsHandler();
     32   virtual ~BluetoothOptionsHandler();
     33 
     34   // OptionsPageUIHandler implementation.
     35   virtual void GetLocalizedValues(
     36       base::DictionaryValue* localized_strings) OVERRIDE;
     37   virtual void RegisterMessages() OVERRIDE;
     38   virtual void InitializeHandler() OVERRIDE;
     39   virtual void InitializePage() OVERRIDE;
     40 
     41   void InitializeAdapter(scoped_refptr<device::BluetoothAdapter> adapter);
     42 
     43   // Sends a notification to the Web UI of the status of a Bluetooth device.
     44   // |device| is the Bluetooth device.
     45   // |params| is an optional set of parameters.
     46   void SendDeviceNotification(const device::BluetoothDevice* device,
     47                               base::DictionaryValue* params);
     48 
     49   // device::BluetoothDevice::PairingDelegate override.
     50   //
     51   // This method will be called when the Bluetooth daemon requires a
     52   // PIN Code for authentication of the device |device|, the UI will display
     53   // a blank entry form to obtain the PIN code from the user.
     54   //
     55   // PIN Codes are generally required for Bluetooth 2.0 and earlier devices
     56   // for which there is no automatic pairing or special handling.
     57   virtual void RequestPinCode(device::BluetoothDevice* device) OVERRIDE;
     58 
     59   // device::BluetoothDevice::PairingDelegate override.
     60   //
     61   // This method will be called when the Bluetooth daemon requires a
     62   // Passkey for authentication of the device |device|, the UI will display
     63   // a blank entry form to obtain the passkey from the user (a numeric in the
     64   // range 0-999999).
     65   //
     66   // Passkeys are generally required for Bluetooth 2.1 and later devices
     67   // which cannot provide input or display on their own, and don't accept
     68   // passkey-less pairing.
     69   virtual void RequestPasskey(device::BluetoothDevice* device) OVERRIDE;
     70 
     71   // device::BluetoothDevice::PairingDelegate override.
     72   //
     73   // This method will be called when the Bluetooth daemon requires that the
     74   // user enter the PIN code |pincode| into the device |device| so that it
     75   // may be authenticated, the UI will display the PIN code with accompanying
     76   // instructions.
     77   //
     78   // This is used for Bluetooth 2.0 and earlier keyboard devices, the
     79   // |pincode| will always be a six-digit numeric in the range 000000-999999
     80   // for compatibilty with later specifications.
     81   virtual void DisplayPinCode(device::BluetoothDevice* device,
     82                               const std::string& pincode) OVERRIDE;
     83 
     84   // device::BluetoothDevice::PairingDelegate override.
     85   //
     86   // This method will be called when the Bluetooth daemon requires that the
     87   // user enter the Passkey |passkey| into the device |device| so that it
     88   // may be authenticated, the UI will display the passkey with accompanying
     89   // instructions.
     90   //
     91   // This is used for Bluetooth 2.1 and later devices that support input
     92   // but not display, such as keyboards. The Passkey is a numeric in the
     93   // range 0-999999 and should be always presented zero-padded to six
     94   // digits.
     95   virtual void DisplayPasskey(
     96       device::BluetoothDevice* device, uint32 passkey) OVERRIDE;
     97 
     98   // device::BluetoothDevice::PairingDelegate override.
     99   //
    100   // This method will be called when the Bluetooth daemon gets a notification
    101   // of a key entered on the device |device| while pairing with the device
    102   // using a PIN code or a Passkey.
    103   //
    104   // The UI will show a visual indication that a given key was pressed in the
    105   // same pairing overlay where the PIN code or Passkey is displayed.
    106   //
    107   // A first call with |entered| as 0 will indicate that this notification
    108   // mechanism is supported by the device allowing the UI to display this fact.
    109   // A last call with |entered| as the length of the key plus one will indicate
    110   // that the [enter] key was pressed.
    111   virtual void KeysEntered(device::BluetoothDevice* device,
    112                            uint32 entered) OVERRIDE;
    113 
    114   // device::BluetoothDevice::PairingDelegate override.
    115   //
    116   // This method will be called when the Bluetooth daemon requires that the
    117   // user confirm that the Passkey |passkey| is displayed on the screen
    118   // of the device |device| so that it may be authenticated, the UI will
    119   // display the passkey with accompanying instructions.
    120   //
    121   // This is used for Bluetooth 2.1 and later devices that support display,
    122   // such as other computers or phones. The Passkey is a numeric in the
    123   // range 0-999999 and should be always present zero-padded to six
    124   // digits.
    125   virtual void ConfirmPasskey(
    126       device::BluetoothDevice* device, uint32 passkey) OVERRIDE;
    127 
    128   // device::BluetoothDevice::PairingDelegate override.
    129   //
    130   // This method will be called when any previous DisplayPinCode(),
    131   // DisplayPasskey() or ConfirmPasskey() request should be concluded
    132   // and removed from the user.
    133   virtual void DismissDisplayOrConfirm() OVERRIDE;
    134 
    135   // Displays a Bluetooth error.
    136   // |error| maps to a localized resource for the error message.
    137   // |address| is the address of the Bluetooth device.  May be an empty
    138   // string if the error is not specific to a single device.
    139   void ReportError(const std::string& error, const std::string& address);
    140 
    141   // device::BluetoothAdapter::Observer implementation.
    142   virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter,
    143                                      bool present) OVERRIDE;
    144   virtual void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
    145                                      bool powered) OVERRIDE;
    146   virtual void DeviceAdded(device::BluetoothAdapter* adapter,
    147                            device::BluetoothDevice* device) OVERRIDE;
    148   virtual void DeviceChanged(device::BluetoothAdapter* adapter,
    149                              device::BluetoothDevice* device) OVERRIDE;
    150   virtual void DeviceRemoved(device::BluetoothAdapter* adapter,
    151                              device::BluetoothDevice* device) OVERRIDE;
    152 
    153  private:
    154   // Displays in the UI a connecting to the device |device| message.
    155   void DeviceConnecting(device::BluetoothDevice* device);
    156 
    157   // Called by device::BluetoothAdapter in response to a failure to
    158   // change the power status of the adapter.
    159   void EnableChangeError();
    160 
    161   // Called by device::BluetoothAdapter in response to a failure to
    162   // set the adapter into discovery mode.
    163   void FindDevicesError();
    164 
    165   // Called by device::BluetoothAdapter in response to a failure to
    166   // remove the adapter from discovery mode.
    167   void StopDiscoveryError();
    168 
    169   // Called by device::BluetoothDevice on a successful pairing and connection
    170   // to a device.
    171   void Connected();
    172 
    173   // Called by device::BluetoothDevice in response to a failure to
    174   // connect to the device with bluetooth address |address| due to an error
    175   // encoded in |error_code|.
    176   void ConnectError(const std::string& address,
    177                     device::BluetoothDevice::ConnectErrorCode error_code);
    178 
    179   // Called by device::BluetoothDevice in response to a failure to
    180   // disconnect the device with bluetooth address |address|.
    181   void DisconnectError(const std::string& address);
    182 
    183   // Called by device::BluetoothDevice in response to a failure to
    184   // disconnect and unpair the device with bluetooth address |address|.
    185   void ForgetError(const std::string& address);
    186 
    187   // Called when the 'Enable bluetooth' checkbox value is changed.
    188   // |args| will contain the checkbox checked state as a string
    189   // ("true" or "false").
    190   void EnableChangeCallback(const base::ListValue* args);
    191 
    192   // Called when the 'Find Devices' button is pressed from the Bluetooth
    193   // ssettings.
    194   // |args| will be an empty list.
    195   void FindDevicesCallback(const base::ListValue* args);
    196 
    197   // Called when the user requests to connect to or disconnect from a Bluetooth
    198   // device.
    199   // |args| will be a list containing two or three arguments, the first argument
    200   // is the device ID and the second is the requested action.  If a third
    201   // argument is present, it is the passkey for pairing confirmation.
    202   void UpdateDeviceCallback(const base::ListValue* args);
    203 
    204   // Called when the "Add a device" dialog closes to stop the discovery
    205   // process.
    206   // |args| will be an empty list.
    207   void StopDiscoveryCallback(const base::ListValue* args);
    208 
    209   // Called when the list of paired devices is initialized in order to
    210   // populate the list.
    211   // |args| will be an empty list.
    212   void GetPairedDevicesCallback(const base::ListValue* args);
    213 
    214   // Default bluetooth adapter, used for all operations.
    215   scoped_refptr<device::BluetoothAdapter> adapter_;
    216 
    217   // True while performing device discovery.
    218   bool discovering_;
    219 
    220   // Cached information about the current pairing device, if any.
    221   std::string pairing_device_address_;
    222   std::string pairing_device_pairing_;
    223   std::string pairing_device_pincode_;
    224   int pairing_device_passkey_;
    225   int pairing_device_entered_;
    226 
    227   // Weak pointer factory for generating 'this' pointers that might live longer
    228   // than this object does.
    229   base::WeakPtrFactory<BluetoothOptionsHandler> weak_ptr_factory_;
    230 
    231   DISALLOW_COPY_AND_ASSIGN(BluetoothOptionsHandler);
    232 };
    233 
    234 }  // namespace options
    235 }  // namespace chromeos
    236 
    237 #endif  // CHROME_BROWSER_UI_WEBUI_OPTIONS_CHROMEOS_BLUETOOTH_OPTIONS_HANDLER_H_
    238