OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/api/bluetooth/bluetooth_private_api.h" | 5 #include "extensions/browser/api/bluetooth/bluetooth_private_api.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "device/bluetooth/bluetooth_adapter.h" | 10 #include "device/bluetooth/bluetooth_adapter.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 | 64 |
65 const char kNameProperty[] = "name"; | 65 const char kNameProperty[] = "name"; |
66 const char kPoweredProperty[] = "powered"; | 66 const char kPoweredProperty[] = "powered"; |
67 const char kDiscoverableProperty[] = "discoverable"; | 67 const char kDiscoverableProperty[] = "discoverable"; |
68 | 68 |
69 const char kSetAdapterPropertyError[] = "Error setting adapter properties: $1"; | 69 const char kSetAdapterPropertyError[] = "Error setting adapter properties: $1"; |
70 | 70 |
71 const char kDeviceNotFoundError[] = | 71 const char kDeviceNotFoundError[] = |
72 "Given address is not a valid Bluetooth device."; | 72 "Given address is not a valid Bluetooth device."; |
73 | 73 |
| 74 const char kDeviceNotConnectedError[] = "Device is not connected"; |
| 75 |
74 const char kPairingNotEnabled[] = | 76 const char kPairingNotEnabled[] = |
75 "Pairing must be enabled to set a pairing response."; | 77 "Pairing must be enabled to set a pairing response."; |
76 | 78 |
77 const char kInvalidPairingResponseOptions[] = | 79 const char kInvalidPairingResponseOptions[] = |
78 "Invalid pairing response options"; | 80 "Invalid pairing response options"; |
79 | 81 |
80 const char kAdapterNotPresent[] = | 82 const char kAdapterNotPresent[] = |
81 "Could not find a Bluetooth adapter."; | 83 "Could not find a Bluetooth adapter."; |
82 | 84 |
| 85 const char kDisconnectError[] = "Failed to disconnect device"; |
| 86 |
83 // Returns true if the pairing response options passed into the | 87 // Returns true if the pairing response options passed into the |
84 // setPairingResponse function are valid. | 88 // setPairingResponse function are valid. |
85 bool ValidatePairingResponseOptions( | 89 bool ValidatePairingResponseOptions( |
86 const device::BluetoothDevice* device, | 90 const device::BluetoothDevice* device, |
87 const bt_private::SetPairingResponseOptions& options) { | 91 const bt_private::SetPairingResponseOptions& options) { |
88 bool response = options.response != bt_private::PAIRING_RESPONSE_NONE; | 92 bool response = options.response != bt_private::PAIRING_RESPONSE_NONE; |
89 bool pincode = options.pincode.get() != NULL; | 93 bool pincode = options.pincode.get() != NULL; |
90 bool passkey = options.passkey.get() != NULL; | 94 bool passkey = options.passkey.get() != NULL; |
91 | 95 |
92 if (!response && !pincode && !passkey) | 96 if (!response && !pincode && !passkey) |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 break; | 282 break; |
279 default: | 283 default: |
280 NOTREACHED(); | 284 NOTREACHED(); |
281 } | 285 } |
282 } | 286 } |
283 | 287 |
284 SendResponse(true); | 288 SendResponse(true); |
285 return true; | 289 return true; |
286 } | 290 } |
287 | 291 |
| 292 BluetoothPrivateDisconnectAllFunction::BluetoothPrivateDisconnectAllFunction() { |
| 293 } |
| 294 |
| 295 BluetoothPrivateDisconnectAllFunction:: |
| 296 ~BluetoothPrivateDisconnectAllFunction() { |
| 297 } |
| 298 |
| 299 bool BluetoothPrivateDisconnectAllFunction::DoWork( |
| 300 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 301 scoped_ptr<bt_private::DisconnectAll::Params> params( |
| 302 bt_private::DisconnectAll::Params::Create(*args_)); |
| 303 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 304 |
| 305 device::BluetoothDevice* device = adapter->GetDevice(params->device_address); |
| 306 if (!device) { |
| 307 SetError(kDeviceNotFoundError); |
| 308 SendResponse(false); |
| 309 return true; |
| 310 } |
| 311 |
| 312 if (!device->IsConnected()) { |
| 313 SetError(kDeviceNotConnectedError); |
| 314 SendResponse(false); |
| 315 return true; |
| 316 } |
| 317 |
| 318 device->Disconnect( |
| 319 base::Bind(&BluetoothPrivateDisconnectAllFunction::OnSuccessCallback, |
| 320 this), |
| 321 base::Bind(&BluetoothPrivateDisconnectAllFunction::OnErrorCallback, this, |
| 322 adapter, params->device_address)); |
| 323 |
| 324 return true; |
| 325 } |
| 326 |
| 327 void BluetoothPrivateDisconnectAllFunction::OnSuccessCallback() { |
| 328 SendResponse(true); |
| 329 } |
| 330 |
| 331 void BluetoothPrivateDisconnectAllFunction::OnErrorCallback( |
| 332 scoped_refptr<device::BluetoothAdapter> adapter, |
| 333 const std::string& device_address) { |
| 334 // The call to Disconnect may report an error if the device was disconnected |
| 335 // due to an external reason. In this case, report "Not Connected" as the |
| 336 // error. |
| 337 device::BluetoothDevice* device = adapter->GetDevice(device_address); |
| 338 if (device && !device->IsConnected()) |
| 339 SetError(kDeviceNotConnectedError); |
| 340 else |
| 341 SetError(kDisconnectError); |
| 342 |
| 343 SendResponse(false); |
| 344 } |
| 345 |
288 } // namespace core_api | 346 } // namespace core_api |
289 | 347 |
290 } // namespace extensions | 348 } // namespace extensions |
OLD | NEW |