bluetooth_options_handler.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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
18namespace base {
19class DictionaryValue;
20}
21
22namespace chromeos {
23namespace options {
24
25// Handler for Bluetooth options on the system options page.
26class 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 requires that the
101  // user confirm that the Passkey |passkey| is displayed on the screen
102  // of the device |device| so that it may be authenticated, the UI will
103  // display the passkey with accompanying instructions.
104  //
105  // This is used for Bluetooth 2.1 and later devices that support display,
106  // such as other computers or phones. The Passkey is a numeric in the
107  // range 0-999999 and should be always present zero-padded to six
108  // digits.
109  virtual void ConfirmPasskey(
110      device::BluetoothDevice* device, uint32 passkey) OVERRIDE;
111
112  // device::BluetoothDevice::PairingDelegate override.
113  //
114  // This method will be called when any previous DisplayPinCode(),
115  // DisplayPasskey() or ConfirmPasskey() request should be concluded
116  // and removed from the user.
117  virtual void DismissDisplayOrConfirm() OVERRIDE;
118
119  // Displays a Bluetooth error.
120  // |error| maps to a localized resource for the error message.
121  // |address| is the address of the Bluetooth device.  May be an empty
122  // string if the error is not specific to a single device.
123  void ReportError(const std::string& error, const std::string& address);
124
125  // device::BluetoothAdapter::Observer implementation.
126  virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter,
127                                     bool present) OVERRIDE;
128  virtual void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
129                                     bool powered) OVERRIDE;
130  virtual void DeviceAdded(device::BluetoothAdapter* adapter,
131                           device::BluetoothDevice* device) OVERRIDE;
132  virtual void DeviceChanged(device::BluetoothAdapter* adapter,
133                             device::BluetoothDevice* device) OVERRIDE;
134  virtual void DeviceRemoved(device::BluetoothAdapter* adapter,
135                             device::BluetoothDevice* device) OVERRIDE;
136
137 private:
138  // Displays in the UI a connecting to the device |device| message.
139  void DeviceConnecting(device::BluetoothDevice* device);
140
141  // Called by device::BluetoothAdapter in response to a failure to
142  // change the power status of the adapter.
143  void EnableChangeError();
144
145  // Called by device::BluetoothAdapter in response to a failure to
146  // set the adapter into discovery mode.
147  void FindDevicesError();
148
149  // Called by device::BluetoothAdapter in response to a failure to
150  // remove the adapter from discovery mode.
151  void StopDiscoveryError();
152
153  // Called by device::BluetoothDevice in response to a failure to
154  // connect to the device with bluetooth address |address| due to an error
155  // encoded in |error_code|.
156  void ConnectError(const std::string& address,
157                    device::BluetoothDevice::ConnectErrorCode error_code);
158
159  // Called by device::BluetoothDevice in response to a failure to
160  // disconnect the device with bluetooth address |address|.
161  void DisconnectError(const std::string& address);
162
163  // Called by device::BluetoothDevice in response to a failure to
164  // disconnect and unpair the device with bluetooth address |address|.
165  void ForgetError(const std::string& address);
166
167  // Called when the 'Enable bluetooth' checkbox value is changed.
168  // |args| will contain the checkbox checked state as a string
169  // ("true" or "false").
170  void EnableChangeCallback(const base::ListValue* args);
171
172  // Called when the 'Find Devices' button is pressed from the Bluetooth
173  // ssettings.
174  // |args| will be an empty list.
175  void FindDevicesCallback(const base::ListValue* args);
176
177  // Called when the user requests to connect to or disconnect from a Bluetooth
178  // device.
179  // |args| will be a list containing two or three arguments, the first argument
180  // is the device ID and the second is the requested action.  If a third
181  // argument is present, it is the passkey for pairing confirmation.
182  void UpdateDeviceCallback(const base::ListValue* args);
183
184  // Called when the "Add a device" dialog closes to stop the discovery
185  // process.
186  // |args| will be an empty list.
187  void StopDiscoveryCallback(const base::ListValue* args);
188
189  // Called when the list of paired devices is initialized in order to
190  // populate the list.
191  // |args| will be an empty list.
192  void GetPairedDevicesCallback(const base::ListValue* args);
193
194  // Default bluetooth adapter, used for all operations.
195  scoped_refptr<device::BluetoothAdapter> adapter_;
196
197  // True while performing device discovery.
198  bool discovering_;
199
200  // Weak pointer factory for generating 'this' pointers that might live longer
201  // than this object does.
202  base::WeakPtrFactory<BluetoothOptionsHandler> weak_ptr_factory_;
203
204  DISALLOW_COPY_AND_ASSIGN(BluetoothOptionsHandler);
205};
206
207}  // namespace options
208}  // namespace chromeos
209
210#endif  // CHROME_BROWSER_UI_WEBUI_OPTIONS_CHROMEOS_BLUETOOTH_OPTIONS_HANDLER_H_
211