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 BluetoothPrivateDisconnectFunction::BluetoothPrivateDisconnectFunction() { | |
293 } | |
294 | |
295 BluetoothPrivateDisconnectFunction::~BluetoothPrivateDisconnectFunction() { | |
296 } | |
297 | |
298 bool BluetoothPrivateDisconnectFunction::DoWork( | |
299 scoped_refptr<device::BluetoothAdapter> adapter) { | |
300 scoped_ptr<bt_private::Disconnect::Params> params( | |
301 bt_private::Disconnect::Params::Create(*args_)); | |
302 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
303 | |
304 device::BluetoothDevice* device = adapter->GetDevice(params->device_address); | |
305 if (!device) { | |
306 SetError(kDeviceNotFoundError); | |
307 SendResponse(false); | |
308 return true; | |
309 } | |
310 | |
311 if (!device->IsConnected()) { | |
312 SetError(kDeviceNotConnectedError); | |
313 SendResponse(false); | |
314 return true; | |
315 } | |
316 | |
317 device->Disconnect( | |
318 base::Bind(&BluetoothPrivateDisconnectFunction::OnSuccessCallback, this), | |
319 base::Bind(&BluetoothPrivateDisconnectFunction::OnErrorCallback, this)); | |
320 | |
321 return true; | |
322 } | |
323 | |
324 void BluetoothPrivateDisconnectFunction::OnSuccessCallback() { | |
325 SendResponse(true); | |
326 } | |
327 | |
328 void BluetoothPrivateDisconnectFunction::OnErrorCallback() { | |
329 SetError(kDisconnectError); | |
Jeffrey Yasskin
2014/12/20 01:48:56
[Moving over to the API implementation.]
On 2014/
| |
330 SendResponse(false); | |
331 } | |
332 | |
288 } // namespace core_api | 333 } // namespace core_api |
289 | 334 |
290 } // namespace extensions | 335 } // namespace extensions |
OLD | NEW |