Chromium Code Reviews| Index: extensions/browser/api/bluetooth/bluetooth_private_api.cc |
| diff --git a/extensions/browser/api/bluetooth/bluetooth_private_api.cc b/extensions/browser/api/bluetooth/bluetooth_private_api.cc |
| index 8729f73b108488a895bd50d54e6f093b6118634e..c04496da25a899761d3164236f6736161b83763d 100644 |
| --- a/extensions/browser/api/bluetooth/bluetooth_private_api.cc |
| +++ b/extensions/browser/api/bluetooth/bluetooth_private_api.cc |
| @@ -71,6 +71,8 @@ const char kSetAdapterPropertyError[] = "Error setting adapter properties: $1"; |
| const char kDeviceNotFoundError[] = |
| "Given address is not a valid Bluetooth device."; |
| +const char kDeviceNotConnectedError[] = "Device is not connected"; |
| + |
| const char kPairingNotEnabled[] = |
| "Pairing must be enabled to set a pairing response."; |
| @@ -80,6 +82,8 @@ const char kInvalidPairingResponseOptions[] = |
| const char kAdapterNotPresent[] = |
| "Could not find a Bluetooth adapter."; |
| +const char kDisconnectError[] = "Failed to disconnect device"; |
| + |
| // Returns true if the pairing response options passed into the |
| // setPairingResponse function are valid. |
| bool ValidatePairingResponseOptions( |
| @@ -285,6 +289,59 @@ bool BluetoothPrivateSetPairingResponseFunction::DoWork( |
| return true; |
| } |
| +BluetoothPrivateDisconnectAllFunction::BluetoothPrivateDisconnectAllFunction() { |
| +} |
| + |
| +BluetoothPrivateDisconnectAllFunction:: |
| + ~BluetoothPrivateDisconnectAllFunction() { |
| +} |
| + |
| +bool BluetoothPrivateDisconnectAllFunction::DoWork( |
| + scoped_refptr<device::BluetoothAdapter> adapter) { |
| + scoped_ptr<bt_private::DisconnectAll::Params> params( |
| + bt_private::DisconnectAll::Params::Create(*args_)); |
| + EXTENSION_FUNCTION_VALIDATE(params.get()); |
| + |
| + device::BluetoothDevice* device = adapter->GetDevice(params->device_address); |
| + if (!device) { |
| + SetError(kDeviceNotFoundError); |
| + SendResponse(false); |
| + return true; |
| + } |
| + |
| + if (!device->IsConnected()) { |
| + SetError(kDeviceNotConnectedError); |
| + SendResponse(false); |
| + return true; |
| + } |
| + |
| + device->Disconnect( |
| + base::Bind(&BluetoothPrivateDisconnectAllFunction::OnSuccessCallback, |
| + this), |
| + base::Bind(&BluetoothPrivateDisconnectAllFunction::OnErrorCallback, this, |
| + adapter, params->device_address)); |
| + |
| + return true; |
| +} |
| + |
| +void BluetoothPrivateDisconnectAllFunction::OnSuccessCallback() { |
| + SendResponse(true); |
| +} |
| + |
| +void BluetoothPrivateDisconnectAllFunction::OnErrorCallback( |
| + scoped_refptr<device::BluetoothAdapter> adapter, |
| + const std::string& device_address) { |
| + // Despite the failure, the device may have become unexpectedly disconnected |
|
Jeffrey Yasskin
2015/01/06 19:22:32
"Despite the failure" isn't clear here. Do you mea
armansito
2015/01/06 20:10:11
I clarified the message, thanks for your comment o
|
| + // in the meantime. In this case, report "Not Connected" as the error. |
| + device::BluetoothDevice* device = adapter->GetDevice(device_address); |
| + if (device && !device->IsConnected()) |
| + SetError(kDeviceNotConnectedError); |
| + else |
| + SetError(kDisconnectError); |
| + |
| + SendResponse(false); |
| +} |
| + |
| } // namespace core_api |
| } // namespace extensions |