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

Side by Side 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 unified diff | Download patch
OLDNEW
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
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
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 // 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
335 // in the meantime. In this case, report "Not Connected" as the error.
336 device::BluetoothDevice* device = adapter->GetDevice(device_address);
337 if (device && !device->IsConnected())
338 SetError(kDeviceNotConnectedError);
339 else
340 SetError(kDisconnectError);
341
342 SendResponse(false);
343 }
344
288 } // namespace core_api 345 } // namespace core_api
289 346
290 } // namespace extensions 347 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698