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 19 matching lines...) Expand all Loading... |
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[] = | 33 const char kErrorReadDescriptorValueFailedFormat[] = |
34 "Failed to read value of descriptor with ID \"%s\"."; | 34 "Failed to read value of descriptor with ID \"%s\"."; |
35 const char kErrorServiceNotFoundFormat[] = "Service with ID \"%s\" not found."; | 35 const char kErrorServiceNotFoundFormat[] = "Service with ID \"%s\" not found."; |
36 const char kErrorPlatformNotSupported[] = | 36 const char kErrorPlatformNotSupported[] = |
37 "This operation is not supported on the current platform"; | 37 "This operation is not supported on the current platform"; |
38 const char kErrorWriteCharacteristicValueFailedFormat[] = | 38 const char kErrorWriteCharacteristicValueFailedFormat[] = |
39 "Failed to write value of characteristic with ID \"%s\"."; | 39 "Failed to write value of characteristic with ID \"%s\"."; |
| 40 const char kErrorWriteDescriptorValueFailedFormat[] = |
| 41 "Failed to write value of descriptor with ID \"%s\"."; |
40 | 42 |
41 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( | 43 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( |
42 BrowserContext* context) { | 44 BrowserContext* context) { |
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
44 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); | 46 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); |
45 } | 47 } |
46 | 48 |
47 void DoWorkCallback(const base::Callback<bool()>& callback) { | 49 void DoWorkCallback(const base::Callback<bool()>& callback) { |
48 DCHECK(!callback.is_null()); | 50 DCHECK(!callback.is_null()); |
49 callback.Run(); | 51 callback.Run(); |
(...skipping 493 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
543 SendResponse(true); | 545 SendResponse(true); |
544 } | 546 } |
545 | 547 |
546 void BluetoothLowEnergyReadDescriptorValueFunction::ErrorCallback() { | 548 void BluetoothLowEnergyReadDescriptorValueFunction::ErrorCallback() { |
547 SetError(base::StringPrintf(kErrorReadDescriptorValueFailedFormat, | 549 SetError(base::StringPrintf(kErrorReadDescriptorValueFailedFormat, |
548 instance_id_.c_str())); | 550 instance_id_.c_str())); |
549 SendResponse(false); | 551 SendResponse(false); |
550 } | 552 } |
551 | 553 |
552 bool BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { | 554 bool BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { |
553 // TODO(armansito): Implement. | 555 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
554 SetError("Call not supported."); | 556 |
| 557 BluetoothLowEnergyEventRouter* event_router = |
| 558 GetEventRouter(browser_context()); |
| 559 |
| 560 // The adapter must be initialized at this point, but return an error instead |
| 561 // of asserting. |
| 562 if (!event_router->HasAdapter()) { |
| 563 SetError(kErrorAdapterNotInitialized); |
| 564 SendResponse(false); |
| 565 return false; |
| 566 } |
| 567 |
| 568 scoped_ptr<apibtle::WriteDescriptorValue::Params> params( |
| 569 apibtle::WriteDescriptorValue::Params::Create(*args_)); |
| 570 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| 571 |
| 572 instance_id_ = params->descriptor_id; |
| 573 std::vector<uint8> value(params->value.begin(), params->value.end()); |
| 574 |
| 575 if (!event_router->WriteDescriptorValue( |
| 576 instance_id_, |
| 577 value, |
| 578 base::Bind( |
| 579 &BluetoothLowEnergyWriteDescriptorValueFunction::SuccessCallback, |
| 580 this), |
| 581 base::Bind( |
| 582 &BluetoothLowEnergyWriteDescriptorValueFunction::ErrorCallback, |
| 583 this))) { |
| 584 SetError(base::StringPrintf(kErrorDescriptorNotFoundFormat, |
| 585 instance_id_.c_str())); |
| 586 SendResponse(false); |
| 587 return false; |
| 588 } |
| 589 |
| 590 return true; |
| 591 } |
| 592 |
| 593 void BluetoothLowEnergyWriteDescriptorValueFunction::SuccessCallback() { |
| 594 results_ = apibtle::WriteDescriptorValue::Results::Create(); |
| 595 SendResponse(true); |
| 596 } |
| 597 |
| 598 void BluetoothLowEnergyWriteDescriptorValueFunction::ErrorCallback() { |
| 599 SetError(base::StringPrintf(kErrorWriteDescriptorValueFailedFormat, |
| 600 instance_id_.c_str())); |
555 SendResponse(false); | 601 SendResponse(false); |
556 return false; | |
557 } | 602 } |
558 | 603 |
559 } // namespace api | 604 } // namespace api |
560 } // namespace extensions | 605 } // namespace extensions |
OLD | NEW |