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 12 matching lines...) Expand all Loading... |
23 const char kErrorAdapterNotInitialized[] = | 23 const char kErrorAdapterNotInitialized[] = |
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 kErrorDescriptorNotFoundFormat[] = | 27 const char kErrorDescriptorNotFoundFormat[] = |
28 "Descriptor with ID \"%s\" not found."; | 28 "Descriptor with ID \"%s\" not found."; |
29 const char kErrorDeviceNotFoundFormat[] = | 29 const char kErrorDeviceNotFoundFormat[] = |
30 "Device with address \"%s\" not found."; | 30 "Device with address \"%s\" not found."; |
31 const char kErrorReadCharacteristicValueFailedFormat[] = | 31 const char kErrorReadCharacteristicValueFailedFormat[] = |
32 "Failed to read value of characteristic with ID \"%s\"."; | 32 "Failed to read value of characteristic with ID \"%s\"."; |
| 33 const char kErrorReadDescriptorValueFailedFormat[] = |
| 34 "Failed to read value of descriptor with ID \"%s\"."; |
33 const char kErrorServiceNotFoundFormat[] = "Service with ID \"%s\" not found."; | 35 const char kErrorServiceNotFoundFormat[] = "Service with ID \"%s\" not found."; |
34 const char kErrorPlatformNotSupported[] = | 36 const char kErrorPlatformNotSupported[] = |
35 "This operation is not supported on the current platform"; | 37 "This operation is not supported on the current platform"; |
36 const char kErrorWriteCharacteristicValueFailedFormat[] = | 38 const char kErrorWriteCharacteristicValueFailedFormat[] = |
37 "Failed to write value of characteristic with ID \"%s\"."; | 39 "Failed to write value of characteristic with ID \"%s\"."; |
38 | 40 |
39 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( | 41 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( |
40 BrowserContext* context) { | 42 BrowserContext* context) { |
41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
42 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); | 44 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); |
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 SendResponse(true); | 481 SendResponse(true); |
480 } | 482 } |
481 | 483 |
482 void BluetoothLowEnergyWriteCharacteristicValueFunction::ErrorCallback() { | 484 void BluetoothLowEnergyWriteCharacteristicValueFunction::ErrorCallback() { |
483 SetError(base::StringPrintf(kErrorWriteCharacteristicValueFailedFormat, | 485 SetError(base::StringPrintf(kErrorWriteCharacteristicValueFailedFormat, |
484 instance_id_.c_str())); | 486 instance_id_.c_str())); |
485 SendResponse(false); | 487 SendResponse(false); |
486 } | 488 } |
487 | 489 |
488 bool BluetoothLowEnergyReadDescriptorValueFunction::DoWork() { | 490 bool BluetoothLowEnergyReadDescriptorValueFunction::DoWork() { |
489 // TODO(armansito): Implement. | 491 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
490 SetError("Call not supported."); | 492 |
| 493 BluetoothLowEnergyEventRouter* event_router = |
| 494 GetEventRouter(browser_context()); |
| 495 |
| 496 // The adapter must be initialized at this point, but return an error instead |
| 497 // of asserting. |
| 498 if (!event_router->HasAdapter()) { |
| 499 SetError(kErrorAdapterNotInitialized); |
| 500 SendResponse(false); |
| 501 return false; |
| 502 } |
| 503 |
| 504 scoped_ptr<apibtle::ReadDescriptorValue::Params> params( |
| 505 apibtle::ReadDescriptorValue::Params::Create(*args_)); |
| 506 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| 507 |
| 508 instance_id_ = params->descriptor_id; |
| 509 |
| 510 if (!event_router->ReadDescriptorValue( |
| 511 instance_id_, |
| 512 base::Bind( |
| 513 &BluetoothLowEnergyReadDescriptorValueFunction::SuccessCallback, |
| 514 this), |
| 515 base::Bind( |
| 516 &BluetoothLowEnergyReadDescriptorValueFunction::ErrorCallback, |
| 517 this))) { |
| 518 SetError(base::StringPrintf(kErrorDescriptorNotFoundFormat, |
| 519 instance_id_.c_str())); |
| 520 SendResponse(false); |
| 521 return false; |
| 522 } |
| 523 |
| 524 return true; |
| 525 } |
| 526 |
| 527 void BluetoothLowEnergyReadDescriptorValueFunction::SuccessCallback() { |
| 528 // Obtain info on the descriptor and see whether or not the descriptor is |
| 529 // still around. |
| 530 apibtle::Descriptor descriptor; |
| 531 if (!GetEventRouter(browser_context()) |
| 532 ->GetDescriptor(instance_id_, &descriptor)) { |
| 533 SetError(base::StringPrintf(kErrorDescriptorNotFoundFormat, |
| 534 instance_id_.c_str())); |
| 535 SendResponse(false); |
| 536 return; |
| 537 } |
| 538 |
| 539 // Manually construct the result instead of using |
| 540 // apibtle::GetDescriptor::Results::Create as it doesn't convert lists of |
| 541 // enums correctly. |
| 542 SetResult(apibtle::DescriptorToValue(&descriptor).release()); |
| 543 SendResponse(true); |
| 544 } |
| 545 |
| 546 void BluetoothLowEnergyReadDescriptorValueFunction::ErrorCallback() { |
| 547 SetError(base::StringPrintf(kErrorReadDescriptorValueFailedFormat, |
| 548 instance_id_.c_str())); |
491 SendResponse(false); | 549 SendResponse(false); |
492 return false; | |
493 } | 550 } |
494 | 551 |
495 bool BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { | 552 bool BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { |
496 // TODO(armansito): Implement. | 553 // TODO(armansito): Implement. |
497 SetError("Call not supported."); | 554 SetError("Call not supported."); |
498 SendResponse(false); | 555 SendResponse(false); |
499 return false; | 556 return false; |
500 } | 557 } |
501 | 558 |
502 } // namespace api | 559 } // namespace api |
503 } // namespace extensions | 560 } // namespace extensions |
OLD | NEW |