Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(996)

Unified Diff: extensions/browser/api/bluetooth/bluetooth_private_api.cc

Issue 819713002: bluetoothPrivate: Add disconnectAll function (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Renamed disconnect -> disconnectAll; addressed jyasskin@'s comments Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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

Powered by Google App Engine
This is Rietveld 408576698