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