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"
17 #include "device/bluetooth/bluetooth_discovery_session.h"
20 class DictionaryValue
;
26 // Handler for Bluetooth options on the system options page.
27 class BluetoothOptionsHandler
28 : public ::options::OptionsPageUIHandler
,
29 public device::BluetoothAdapter::Observer
,
30 public device::BluetoothDevice::PairingDelegate
{
32 BluetoothOptionsHandler();
33 virtual ~BluetoothOptionsHandler();
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
;
42 void InitializeAdapter(scoped_refptr
<device::BluetoothAdapter
> adapter
);
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
);
50 // device::BluetoothDevice::PairingDelegate override.
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.
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
;
60 // device::BluetoothDevice::PairingDelegate override.
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
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
;
72 // device::BluetoothDevice::PairingDelegate override.
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
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
;
85 // device::BluetoothDevice::PairingDelegate override.
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
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
96 virtual void DisplayPasskey(
97 device::BluetoothDevice
* device
, uint32 passkey
) OVERRIDE
;
99 // device::BluetoothDevice::PairingDelegate override.
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.
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.
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
;
115 // device::BluetoothDevice::PairingDelegate override.
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.
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
126 virtual void ConfirmPasskey(
127 device::BluetoothDevice
* device
, uint32 passkey
) OVERRIDE
;
129 // device::BluetoothDevice::PairingDelegate override.
130 virtual void AuthorizePairing(device::BluetoothDevice
* device
) OVERRIDE
;
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
);
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
;
153 // Displays in the UI a connecting to the device |device| message.
154 void DeviceConnecting(device::BluetoothDevice
* device
);
156 // Called by device::BluetoothAdapter in response to a failure to
157 // change the power status of the adapter.
158 void EnableChangeError();
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
);
165 // Called by device::BluetoothAdapter in response to a failure to
166 // initiate a discovery session.
167 void FindDevicesError();
169 // Called by device::BluetoothAdapter in response to a failure to
170 // terminate a discovery session.
171 void StopDiscoveryError();
173 // Called by device::BluetoothDevice on a successful pairing and connection
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
);
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
);
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
);
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
);
196 // Called when the 'Find Devices' button is pressed from the Bluetooth
198 // |args| will be an empty list.
199 void FindDevicesCallback(const base::ListValue
* args
);
201 // Called when the user requests to connect to or disconnect from a Bluetooth
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
);
208 // Called when the "Add a device" dialog closes to stop the discovery
210 // |args| will be an empty list.
211 void StopDiscoveryCallback(const base::ListValue
* args
);
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
);
218 // Default bluetooth adapter, used for all operations.
219 scoped_refptr
<device::BluetoothAdapter
> adapter_
;
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
224 bool should_run_device_discovery_
;
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_
;
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_
;
238 // Weak pointer factory for generating 'this' pointers that might live longer
239 // than this object does.
240 base::WeakPtrFactory
<BluetoothOptionsHandler
> weak_ptr_factory_
;
242 DISALLOW_COPY_AND_ASSIGN(BluetoothOptionsHandler
);
245 } // namespace options
246 } // namespace chromeos
248 #endif // CHROME_BROWSER_UI_WEBUI_OPTIONS_CHROMEOS_BLUETOOTH_OPTIONS_HANDLER_H_