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 "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_api.h" | 5 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_event_router.h" | 10 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_event_router.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 "Could not initialize Bluetooth adapter."; | 24 "Could not initialize Bluetooth adapter."; |
25 const char kErrorCharacteristicNotFoundFormat[] = | 25 const char kErrorCharacteristicNotFoundFormat[] = |
26 "Characteristic with ID \"%s\" not found."; | 26 "Characteristic with ID \"%s\" not found."; |
27 const char kErrorDeviceNotFoundFormat[] = | 27 const char kErrorDeviceNotFoundFormat[] = |
28 "Device with address \"%s\" not found."; | 28 "Device with address \"%s\" not found."; |
29 const char kErrorReadCharacteristicValueFailedFormat[] = | 29 const char kErrorReadCharacteristicValueFailedFormat[] = |
30 "Failed to read value of characteristic with ID \"%s\"."; | 30 "Failed to read value of characteristic with ID \"%s\"."; |
31 const char kErrorServiceNotFoundFormat[] = "Service with ID \"%s\" not found."; | 31 const char kErrorServiceNotFoundFormat[] = "Service with ID \"%s\" not found."; |
32 const char kErrorPlatformNotSupported[] = | 32 const char kErrorPlatformNotSupported[] = |
33 "This operation is not supported on the current platform"; | 33 "This operation is not supported on the current platform"; |
| 34 const char kErrorWriteCharacteristicValueFailedFormat[] = |
| 35 "Failed to write value of characteristic with ID \"%s\"."; |
34 | 36 |
35 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( | 37 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( |
36 BrowserContext* context) { | 38 BrowserContext* context) { |
37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
38 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); | 40 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); |
39 } | 41 } |
40 | 42 |
41 void DoWorkCallback(const base::Callback<bool()>& callback) { | 43 void DoWorkCallback(const base::Callback<bool()>& callback) { |
42 DCHECK(!callback.is_null()); | 44 DCHECK(!callback.is_null()); |
43 callback.Run(); | 45 callback.Run(); |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 SendResponse(true); | 360 SendResponse(true); |
359 } | 361 } |
360 | 362 |
361 void BluetoothLowEnergyReadCharacteristicValueFunction::ErrorCallback() { | 363 void BluetoothLowEnergyReadCharacteristicValueFunction::ErrorCallback() { |
362 SetError(base::StringPrintf(kErrorReadCharacteristicValueFailedFormat, | 364 SetError(base::StringPrintf(kErrorReadCharacteristicValueFailedFormat, |
363 instance_id_.c_str())); | 365 instance_id_.c_str())); |
364 SendResponse(false); | 366 SendResponse(false); |
365 } | 367 } |
366 | 368 |
367 bool BluetoothLowEnergyWriteCharacteristicValueFunction::DoWork() { | 369 bool BluetoothLowEnergyWriteCharacteristicValueFunction::DoWork() { |
368 // TODO(armansito): Implement. | 370 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
369 SetError("Call not supported."); | 371 |
| 372 BluetoothLowEnergyEventRouter* event_router = |
| 373 GetEventRouter(browser_context()); |
| 374 |
| 375 // The adapter must be initialized at this point, but return an error instead |
| 376 // of asserting. |
| 377 if (!event_router->HasAdapter()) { |
| 378 SetError(kErrorAdapterNotInitialized); |
| 379 SendResponse(false); |
| 380 return false; |
| 381 } |
| 382 |
| 383 scoped_ptr<apibtle::WriteCharacteristicValue::Params> params( |
| 384 apibtle::WriteCharacteristicValue::Params::Create(*args_)); |
| 385 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| 386 |
| 387 instance_id_ = params->characteristic_id; |
| 388 std::vector<uint8> value(params->value.begin(), params->value.end()); |
| 389 |
| 390 if (!event_router->WriteCharacteristicValue( |
| 391 instance_id_, |
| 392 value, |
| 393 base::Bind(&BluetoothLowEnergyWriteCharacteristicValueFunction:: |
| 394 SuccessCallback, |
| 395 this), |
| 396 base::Bind(&BluetoothLowEnergyWriteCharacteristicValueFunction:: |
| 397 ErrorCallback, |
| 398 this))) { |
| 399 SetError(base::StringPrintf(kErrorCharacteristicNotFoundFormat, |
| 400 instance_id_.c_str())); |
| 401 SendResponse(false); |
| 402 return false; |
| 403 } |
| 404 |
| 405 return true; |
| 406 } |
| 407 |
| 408 void BluetoothLowEnergyWriteCharacteristicValueFunction::SuccessCallback() { |
| 409 results_ = apibtle::WriteCharacteristicValue::Results::Create(); |
| 410 SendResponse(true); |
| 411 } |
| 412 |
| 413 void BluetoothLowEnergyWriteCharacteristicValueFunction::ErrorCallback() { |
| 414 SetError(base::StringPrintf(kErrorWriteCharacteristicValueFailedFormat, |
| 415 instance_id_.c_str())); |
370 SendResponse(false); | 416 SendResponse(false); |
371 return false; | |
372 } | 417 } |
373 | 418 |
374 bool BluetoothLowEnergyReadDescriptorValueFunction::DoWork() { | 419 bool BluetoothLowEnergyReadDescriptorValueFunction::DoWork() { |
375 // TODO(armansito): Implement. | 420 // TODO(armansito): Implement. |
376 SetError("Call not supported."); | 421 SetError("Call not supported."); |
377 SendResponse(false); | 422 SendResponse(false); |
378 return false; | 423 return false; |
379 } | 424 } |
380 | 425 |
381 bool BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { | 426 bool BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { |
382 // TODO(armansito): Implement. | 427 // TODO(armansito): Implement. |
383 SetError("Call not supported."); | 428 SetError("Call not supported."); |
384 SendResponse(false); | 429 SendResponse(false); |
385 return false; | 430 return false; |
386 } | 431 } |
387 | 432 |
388 } // namespace api | 433 } // namespace api |
389 } // namespace extensions | 434 } // namespace extensions |
OLD | NEW |