OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/options2/chromeos/bluetooth_options_handler2.h " | 5 #include "chrome/browser/ui/webui/options2/chromeos/bluetooth_options_handler2.h " |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | 11 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" |
12 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" | 12 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" |
13 #include "chrome/browser/chromeos/system/runtime_environment.h" | 13 #include "chrome/browser/chromeos/system/runtime_environment.h" |
14 #include "chrome/browser/ui/webui/options2/chromeos/system_settings_provider2.h" | 14 #include "chrome/browser/ui/webui/options2/chromeos/system_settings_provider2.h" |
15 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
16 #include "content/public/browser/web_ui.h" | 16 #include "content/public/browser/web_ui.h" |
17 #include "grit/chromium_strings.h" | 17 #include "grit/chromium_strings.h" |
18 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
19 #include "third_party/cros_system_api/dbus/service_constants.h" | 19 #include "third_party/cros_system_api/dbus/service_constants.h" |
20 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 // |UpdateDeviceCallback| takes a variable length list as an argument. The | 24 // |UpdateDeviceCallback| takes a variable length list as an argument. The |
25 // value stored in each list element is indicated by the following constants. | 25 // value stored in each list element is indicated by the following constants. |
26 const int kUpdateDeviceAddressIndex = 0; | 26 const int kUpdateDeviceAddressIndex = 0; |
27 const int kUpdateDeviceCommandIndex = 1; | 27 const int kUpdateDeviceCommandIndex = 1; |
28 const int kUpdateDevicePasskeyIndex = 2; | 28 const int kUpdateDevicePasskeyIndex = 2; |
29 | 29 |
30 // |UpdateDeviceCallback| provides a command value of one of the following | |
31 // constants that indicates what update it is providing to us. | |
32 const std::string kConnectCommand = "connect"; | |
33 const std::string kCancelCommand = "cancel"; | |
34 const std::string kAcceptCommand = "accept"; | |
35 const std::string kRejectCommand = "reject"; | |
36 const std::string kDisconnectCommand = "disconnect"; | |
37 const std::string kForgetCommand = "forget"; | |
38 | |
39 // |SendDeviceNotification| may include a pairing parameter whose value | |
40 // is one of the following constants instructing the UI to perform a certain | |
41 // action. | |
42 const std::string kEnterPinCode = "bluetoothEnterPinCode"; | |
43 const std::string kEnterPasskey = "bluetoothEnterPasskey"; | |
44 const std::string kRemotePinCode = "bluetoothRemotePinCode"; | |
45 const std::string kRemotePasskey = "bluetoothRemotePasskey"; | |
46 const std::string kConfirmPasskey = "bluetoothConfirmPasskey"; | |
47 | |
30 } // namespace | 48 } // namespace |
31 | 49 |
32 namespace chromeos { | 50 namespace chromeos { |
33 namespace options2 { | 51 namespace options2 { |
34 | 52 |
35 BluetoothOptionsHandler::BluetoothOptionsHandler() { | 53 BluetoothOptionsHandler::BluetoothOptionsHandler() : weak_ptr_factory_(this) { |
36 } | 54 } |
37 | 55 |
38 BluetoothOptionsHandler::~BluetoothOptionsHandler() { | 56 BluetoothOptionsHandler::~BluetoothOptionsHandler() { |
39 if (!CommandLine::ForCurrentProcess() | 57 if (!CommandLine::ForCurrentProcess() |
40 ->HasSwitch(switches::kEnableBluetooth)) { | 58 ->HasSwitch(switches::kEnableBluetooth)) { |
41 return; | 59 return; |
42 } | 60 } |
43 | 61 |
44 adapter_->RemoveObserver(this); | 62 adapter_->RemoveObserver(this); |
45 } | 63 } |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
183 AdapterPresentChanged(adapter_.get(), adapter_->IsPresent()); | 201 AdapterPresentChanged(adapter_.get(), adapter_->IsPresent()); |
184 } | 202 } |
185 | 203 |
186 void BluetoothOptionsHandler::EnableChangeCallback( | 204 void BluetoothOptionsHandler::EnableChangeCallback( |
187 const ListValue* args) { | 205 const ListValue* args) { |
188 bool bluetooth_enabled; | 206 bool bluetooth_enabled; |
189 args->GetBoolean(0, &bluetooth_enabled); | 207 args->GetBoolean(0, &bluetooth_enabled); |
190 | 208 |
191 adapter_->SetPowered(bluetooth_enabled, | 209 adapter_->SetPowered(bluetooth_enabled, |
192 base::Bind(&BluetoothOptionsHandler::ErrorCallback, | 210 base::Bind(&BluetoothOptionsHandler::ErrorCallback, |
193 base::Unretained(this))); | 211 weak_ptr_factory_.GetWeakPtr())); |
194 } | 212 } |
195 | 213 |
196 void BluetoothOptionsHandler::FindDevicesCallback( | 214 void BluetoothOptionsHandler::FindDevicesCallback( |
197 const ListValue* args) { | 215 const ListValue* args) { |
198 adapter_->SetDiscovering(true, | 216 adapter_->SetDiscovering(true, |
199 base::Bind(&BluetoothOptionsHandler::ErrorCallback, | 217 base::Bind(&BluetoothOptionsHandler::ErrorCallback, |
200 base::Unretained(this))); | 218 weak_ptr_factory_.GetWeakPtr())); |
201 } | 219 } |
202 | 220 |
203 void BluetoothOptionsHandler::UpdateDeviceCallback( | 221 void BluetoothOptionsHandler::UpdateDeviceCallback( |
204 const ListValue* args) { | 222 const ListValue* args) { |
205 // TODO(kevers): Trigger connect/disconnect. | |
206 int size = args->GetSize(); | |
207 std::string address; | 223 std::string address; |
224 args->GetString(kUpdateDeviceAddressIndex, &address); | |
225 | |
226 BluetoothDevice* device = adapter_->GetDevice(address); | |
227 if (!device) | |
228 return; | |
229 | |
208 std::string command; | 230 std::string command; |
209 args->GetString(kUpdateDeviceAddressIndex, &address); | |
210 args->GetString(kUpdateDeviceCommandIndex, &command); | 231 args->GetString(kUpdateDeviceCommandIndex, &command); |
211 if (size > kUpdateDevicePasskeyIndex) { | 232 |
212 // Passkey confirmation as part of the pairing process. | 233 if (command == kConnectCommand) { |
213 std::string passkey; | 234 int size = args->GetSize(); |
214 args->GetString(kUpdateDevicePasskeyIndex, &passkey); | 235 if (size > kUpdateDevicePasskeyIndex) { |
215 DVLOG(1) << "UpdateDeviceCallback: " << address << ": " << command | 236 // PIN code or Passkey entry during the pairing process. |
216 << " [" << passkey << "]"; | 237 // TODO(keybuk, kevers): disambiguate PIN (string) vs. Passkey (integer) |
238 std::string pincode; | |
239 args->GetString(kUpdateDevicePasskeyIndex, &pincode); | |
240 DVLOG(1) << "PIN code supplied: " << address << ": " << pincode; | |
241 | |
242 device->SetPinCode(pincode); | |
243 | |
244 } else { | |
245 // Connection request. | |
246 DVLOG(1) << "Connect: " << address; | |
247 if (device->WasDiscovered()) { | |
248 device->PairAndConnect( | |
249 this, base::Bind(&BluetoothOptionsHandler::ErrorCallback, | |
250 weak_ptr_factory_.GetWeakPtr())); | |
251 } else { | |
252 device->Connect(base::Bind(&BluetoothOptionsHandler::ErrorCallback, | |
253 weak_ptr_factory_.GetWeakPtr())); | |
254 } | |
255 } | |
256 | |
James Hawkins
2012/03/13 23:12:45
nit: Remove blank line. Here and elsewhere at the
keybuk
2012/03/14 00:15:26
Done.
| |
257 } else if (command == kCancelCommand) { | |
258 // Cancel pairing. | |
259 DVLOG(1) << "Cancel pairing: " << address; | |
260 device->CancelPairing(); | |
261 | |
262 } else if (command == kAcceptCommand) { | |
263 // Confirm displayed Passkey. | |
264 DVLOG(1) << "Confirm pairing: " << address; | |
265 device->ConfirmPairing(); | |
266 | |
267 } else if (command == kRejectCommand) { | |
268 // Reject displayed Passkey. | |
269 DVLOG(1) << "Reject pairing: " << address; | |
270 device->RejectPairing(); | |
271 | |
272 } else if (command == kDisconnectCommand) { | |
273 // TODO(keybuk): implement | |
274 DVLOG(1) << "Disconnect device: " << address; | |
275 | |
276 } else if (command == kForgetCommand) { | |
277 // TODO(keybuk): implement | |
278 DVLOG(1) << "Forget device: " << address; | |
279 | |
217 } else { | 280 } else { |
218 // Initiating a device connection or disconnecting | 281 LOG(WARNING) << "Unknown updateBluetoothDevice command: " << command; |
219 DVLOG(1) << "UpdateDeviceCallback: " << address << ": " << command; | |
220 } | 282 } |
221 } | 283 } |
222 | 284 |
223 void BluetoothOptionsHandler::StopDiscoveryCallback( | 285 void BluetoothOptionsHandler::StopDiscoveryCallback( |
224 const ListValue* args) { | 286 const ListValue* args) { |
225 adapter_->SetDiscovering(false, | 287 adapter_->SetDiscovering(false, |
226 base::Bind(&BluetoothOptionsHandler::ErrorCallback, | 288 base::Bind(&BluetoothOptionsHandler::ErrorCallback, |
227 base::Unretained(this))); | 289 weak_ptr_factory_.GetWeakPtr())); |
228 } | 290 } |
229 | 291 |
230 void BluetoothOptionsHandler::GetPairedDevicesCallback( | 292 void BluetoothOptionsHandler::GetPairedDevicesCallback( |
231 const ListValue* args) { | 293 const ListValue* args) { |
232 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | 294 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); |
233 | 295 |
234 for (BluetoothAdapter::DeviceList::iterator iter = devices.begin(); | 296 for (BluetoothAdapter::DeviceList::iterator iter = devices.begin(); |
235 iter != devices.end(); ++iter) | 297 iter != devices.end(); ++iter) |
236 SendDeviceNotification(*iter, NULL); | 298 SendDeviceNotification(*iter, NULL); |
237 } | 299 } |
238 | 300 |
239 void BluetoothOptionsHandler::SendDeviceNotification( | 301 void BluetoothOptionsHandler::SendDeviceNotification( |
240 const BluetoothDevice* device, | 302 const BluetoothDevice* device, |
241 base::DictionaryValue* params) { | 303 base::DictionaryValue* params) { |
242 base::DictionaryValue js_properties; | 304 base::DictionaryValue js_properties; |
243 js_properties.SetString("name", device->GetName()); | 305 js_properties.SetString("name", device->GetName()); |
244 js_properties.SetString("address", device->address()); | 306 js_properties.SetString("address", device->address()); |
245 js_properties.SetBoolean("discovered", device->WasDiscovered()); | 307 js_properties.SetBoolean("discovered", device->WasDiscovered()); |
246 js_properties.SetBoolean("paired", device->IsPaired()); | 308 js_properties.SetBoolean("paired", device->IsPaired()); |
247 js_properties.SetBoolean("connected", device->IsConnected()); | 309 js_properties.SetBoolean("connected", device->IsConnected()); |
248 if (params) { | 310 if (params) { |
249 js_properties.MergeDictionary(params); | 311 js_properties.MergeDictionary(params); |
250 } | 312 } |
251 web_ui()->CallJavascriptFunction( | 313 web_ui()->CallJavascriptFunction( |
252 "options.BrowserOptions.addBluetoothDevice", | 314 "options.BrowserOptions.addBluetoothDevice", |
253 js_properties); | 315 js_properties); |
254 } | 316 } |
255 | 317 |
256 void BluetoothOptionsHandler::DisplayPinCode( | 318 void BluetoothOptionsHandler::RequestPinCode(BluetoothDevice* device) { |
257 const BluetoothDevice* device, | |
258 const std::string& pincode) { | |
259 DictionaryValue params; | 319 DictionaryValue params; |
260 params.SetString("pairing", "bluetoothRemotePinCode"); | 320 params.SetString("pairing", kEnterPinCode); |
321 SendDeviceNotification(device, ¶ms); | |
322 } | |
323 | |
324 void BluetoothOptionsHandler::RequestPasskey(BluetoothDevice* device) { | |
325 DictionaryValue params; | |
326 params.SetString("pairing", kEnterPasskey); | |
327 SendDeviceNotification(device, ¶ms); | |
328 } | |
329 | |
330 void BluetoothOptionsHandler::DisplayPinCode(BluetoothDevice* device, | |
331 const std::string& pincode) { | |
332 DictionaryValue params; | |
333 params.SetString("pairing", kRemotePinCode); | |
261 params.SetString("pincode", pincode); | 334 params.SetString("pincode", pincode); |
262 SendDeviceNotification(device, ¶ms); | 335 SendDeviceNotification(device, ¶ms); |
263 } | 336 } |
264 | 337 |
265 void BluetoothOptionsHandler::DisplayPasskey( | 338 void BluetoothOptionsHandler::DisplayPasskey(BluetoothDevice* device, |
266 const BluetoothDevice* device, | 339 uint32 passkey) { |
267 int passkey, | |
268 int entered) { | |
269 DictionaryValue params; | 340 DictionaryValue params; |
270 params.SetString("pairing", "bluetoothRemotePasskey"); | 341 params.SetString("pairing", kRemotePasskey); |
271 params.SetInteger("passkey", passkey); | 342 params.SetInteger("passkey", passkey); |
272 params.SetInteger("entered", entered); | |
273 SendDeviceNotification(device, ¶ms); | 343 SendDeviceNotification(device, ¶ms); |
274 } | 344 } |
275 | 345 |
276 void BluetoothOptionsHandler::RequestPinCode( | 346 void BluetoothOptionsHandler::ConfirmPasskey(BluetoothDevice* device, |
277 const BluetoothDevice* device) { | 347 uint32 passkey) { |
278 DictionaryValue params; | 348 DictionaryValue params; |
279 params.SetString("pairing", "bluetoothEnterPinCode"); | 349 params.SetString("pairing", kConfirmPasskey); |
350 params.SetInteger("passkey", passkey); | |
280 SendDeviceNotification(device, ¶ms); | 351 SendDeviceNotification(device, ¶ms); |
281 } | 352 } |
282 | 353 |
283 void BluetoothOptionsHandler::RequestPasskey( | 354 void BluetoothOptionsHandler::DismissDisplayOrConfirm() { |
284 const BluetoothDevice* device) { | 355 // TODO(kevers): please fill this in |
285 DictionaryValue params; | |
286 params.SetString("pairing", "bluetoothEnterPasskey"); | |
287 SendDeviceNotification(device, ¶ms); | |
288 } | |
289 | |
290 void BluetoothOptionsHandler::RequestConfirmation( | |
291 const BluetoothDevice* device, | |
292 int passkey) { | |
293 DictionaryValue params; | |
294 params.SetString("pairing", "bluetoothConfirmPasskey"); | |
295 params.SetInteger("passkey", passkey); | |
296 SendDeviceNotification(device, ¶ms); | |
297 } | 356 } |
298 | 357 |
299 void BluetoothOptionsHandler::ReportError( | 358 void BluetoothOptionsHandler::ReportError( |
300 const BluetoothDevice* device, | 359 const BluetoothDevice* device, |
301 ConnectionError error) { | 360 ConnectionError error) { |
361 // TODO(keybuk): not called from anywhere | |
302 std::string errorCode; | 362 std::string errorCode; |
303 switch (error) { | 363 switch (error) { |
304 case DEVICE_NOT_FOUND: | 364 case DEVICE_NOT_FOUND: |
305 errorCode = "bluetoothErrorNoDevice"; | 365 errorCode = "bluetoothErrorNoDevice"; |
306 break; | 366 break; |
307 case INCORRECT_PIN: | 367 case INCORRECT_PIN: |
308 errorCode = "bluetoothErrorIncorrectPin"; | 368 errorCode = "bluetoothErrorIncorrectPin"; |
309 break; | 369 break; |
310 case CONNECTION_TIMEOUT: | 370 case CONNECTION_TIMEOUT: |
311 errorCode = "bluetoothErrorTimeout"; | 371 errorCode = "bluetoothErrorTimeout"; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
347 void BluetoothOptionsHandler::ErrorCallback() { | 407 void BluetoothOptionsHandler::ErrorCallback() { |
348 // TODO(keybuk): we don't get any form of error response from dbus:: | 408 // TODO(keybuk): we don't get any form of error response from dbus:: |
349 // yet, other than an error occurred. I'm going to fix that, then this | 409 // yet, other than an error occurred. I'm going to fix that, then this |
350 // gets replaced by genuine error information from the method which we | 410 // gets replaced by genuine error information from the method which we |
351 // can act on, rather than a debug log statement. | 411 // can act on, rather than a debug log statement. |
352 DVLOG(1) << "Failed."; | 412 DVLOG(1) << "Failed."; |
353 } | 413 } |
354 | 414 |
355 } // namespace options2 | 415 } // namespace options2 |
356 } // namespace chromeos | 416 } // namespace chromeos |
OLD | NEW |