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.
5 #ifndef CHROME_BROWSER_UI_WEBUI_OPTIONS_CHROMEOS_BLUETOOTH_OPTIONS_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_OPTIONS_CHROMEOS_BLUETOOTH_OPTIONS_HANDLER_H_
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"
19 class DictionaryValue;
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 {
31 BluetoothOptionsHandler();
32 virtual ~BluetoothOptionsHandler();
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;
41 void InitializeAdapter(scoped_refptr<device::BluetoothAdapter> adapter);
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);
49 // device::BluetoothDevice::PairingDelegate override.
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.
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;
59 // device::BluetoothDevice::PairingDelegate override.
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
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;
71 // device::BluetoothDevice::PairingDelegate override.
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
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;
84 // device::BluetoothDevice::PairingDelegate override.
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
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
95 virtual void DisplayPasskey(
96 device::BluetoothDevice* device, uint32 passkey) OVERRIDE;
98 // device::BluetoothDevice::PairingDelegate override.
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.
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.
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;
114 // device::BluetoothDevice::PairingDelegate override.
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.
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
125 virtual void ConfirmPasskey(
126 device::BluetoothDevice* device, uint32 passkey) OVERRIDE;
128 // device::BluetoothDevice::PairingDelegate override.
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;
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);
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;
154 // Displays in the UI a connecting to the device |device| message.
155 void DeviceConnecting(device::BluetoothDevice* device);
157 // Called by device::BluetoothAdapter in response to a failure to
158 // change the power status of the adapter.
159 void EnableChangeError();
161 // Called by device::BluetoothAdapter in response to a failure to
162 // set the adapter into discovery mode.
163 void FindDevicesError();
165 // Called by device::BluetoothAdapter in response to a failure to
166 // remove the adapter from discovery mode.
167 void StopDiscoveryError();
169 // Called by device::BluetoothDevice on a successful pairing and connection
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);
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);
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);
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);
192 // Called when the 'Find Devices' button is pressed from the Bluetooth
194 // |args| will be an empty list.
195 void FindDevicesCallback(const base::ListValue* args);
197 // Called when the user requests to connect to or disconnect from a Bluetooth
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);
204 // Called when the "Add a device" dialog closes to stop the discovery
206 // |args| will be an empty list.
207 void StopDiscoveryCallback(const base::ListValue* args);
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);
214 // Default bluetooth adapter, used for all operations.
215 scoped_refptr<device::BluetoothAdapter> adapter_;
217 // True while performing device discovery.
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_;
227 // Weak pointer factory for generating 'this' pointers that might live longer
228 // than this object does.
229 base::WeakPtrFactory<BluetoothOptionsHandler> weak_ptr_factory_;
231 DISALLOW_COPY_AND_ASSIGN(BluetoothOptionsHandler);
234 } // namespace options
235 } // namespace chromeos
237 #endif // CHROME_BROWSER_UI_WEBUI_OPTIONS_CHROMEOS_BLUETOOTH_OPTIONS_HANDLER_H_