Chromium Code Reviews| Index: chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_api.cc |
| diff --git a/chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_api.cc b/chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_api.cc |
| index d1a6cddbf50dadbec9bcfd5e97ebb120d027f894..8dc02e192aa5732fcd287795be11154e470dfd35 100644 |
| --- a/chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_api.cc |
| +++ b/chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_api.cc |
| @@ -4,73 +4,212 @@ |
| #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_api.h" |
| +#include "base/bind.h" |
| +#include "base/lazy_instance.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_event_router.h" |
| +#include "chrome/common/extensions/api/bluetooth_low_energy.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "extensions/browser/event_router.h" |
| + |
| +using content::BrowserContext; |
| +using content::BrowserThread; |
| + |
| +namespace apibtle = extensions::api::bluetooth_low_energy; |
| + |
| +namespace { |
| + |
| +const char kErrorAdapterNotInitialized[] = |
| + "Could not initialize Bluetooth adapter."; |
| +const char kErrorDeviceNotFoundFormat[] = |
| + "Device with address \"%s\" not found."; |
| +const char kErrorServiceNotFoundFormat[] = "Service with ID \"%s\" not found."; |
| +const char kErrorPlatformNotSupported[] = |
| + "This operation is not supported on the current platform"; |
| + |
| +extensions::BluetoothLowEnergyEventRouter* GetEventRouter( |
| + BrowserContext* context) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); |
| +} |
| + |
| +} // namespace |
| + |
| namespace extensions { |
| + |
| +static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI> > |
| + g_factory = LAZY_INSTANCE_INITIALIZER; |
| + |
| +// static |
| +BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>* |
| +BluetoothLowEnergyAPI::GetFactoryInstance() { |
| + return g_factory.Pointer(); |
| +} |
| + |
| +// static |
| +BluetoothLowEnergyAPI* BluetoothLowEnergyAPI::Get(BrowserContext* context) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + return GetFactoryInstance()->Get(context); |
| +} |
| + |
| +BluetoothLowEnergyAPI::BluetoothLowEnergyAPI(BrowserContext* context) |
| + : event_router_(new BluetoothLowEnergyEventRouter(context)), |
| + browser_context_(context) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| +} |
| + |
| +BluetoothLowEnergyAPI::~BluetoothLowEnergyAPI() { |
| +} |
| + |
| +void BluetoothLowEnergyAPI::Shutdown() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| +} |
| + |
| namespace api { |
| -bool BluetoothLowEnergyGetServiceFunction::RunImpl() { |
| - // TODO(armansito): Implement. |
| - SetError("Call not supported."); |
| - return false; |
| +BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() |
| + : weak_ptr_factory_(this) { |
| } |
| -bool BluetoothLowEnergyGetServicesFunction::RunImpl() { |
| - // TODO(armansito): Implement. |
| - SetError("Call not supported."); |
| - return false; |
| +BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() { |
| +} |
| + |
| +bool BluetoothLowEnergyExtensionFunction::RunImpl() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + BluetoothLowEnergyEventRouter* event_router = |
| + GetEventRouter(browser_context()); |
| + if (!event_router->IsBluetoothSupported()) { |
| + SetError(kErrorPlatformNotSupported); |
| + return false; |
| + } |
| + |
| + if (!event_router->InitializeAdapterAndInvokeCallback( |
| + base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, |
| + weak_ptr_factory_.GetWeakPtr()))) { |
|
rpaquay
2014/04/24 20:38:45
Does this work? I would think you should pass "thi
armansito
2014/04/24 22:02:00
I'm confused, if you actually pass "this" here and
rpaquay
2014/04/24 22:12:46
AFAIK:
1) If you pass "this", Bind calls "this->A
armansito
2014/04/24 22:38:33
Ah got it, I didn't realize that ExtensionFunction
|
| + SetError(kErrorAdapterNotInitialized); |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +void BluetoothLowEnergyGetServiceFunction::DoWork() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + BluetoothLowEnergyEventRouter* event_router = |
| + GetEventRouter(browser_context()); |
| + |
| + // The adapter must be initialized at this point, but return an error instead |
| + // of asserting. |
| + if (!event_router->HasAdapter()) { |
| + SetError(kErrorAdapterNotInitialized); |
| + SendResponse(false); |
| + return; |
| + } |
| + |
| + scoped_ptr<apibtle::GetService::Params> params( |
| + apibtle::GetService::Params::Create(*args_)); |
| + EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| + |
| + std::string service_id = params->service_id; |
| + |
| + apibtle::Service service; |
| + if (!event_router->GetService(service_id, &service)) { |
| + SetError( |
| + base::StringPrintf(kErrorServiceNotFoundFormat, service_id.c_str())); |
| + SendResponse(false); |
| + return; |
| + } |
| + |
| + SetResult(service.ToValue().release()); |
| + SendResponse(true); |
| +} |
| + |
| +void BluetoothLowEnergyGetServicesFunction::DoWork() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + |
| + BluetoothLowEnergyEventRouter* event_router = |
| + GetEventRouter(browser_context()); |
| + |
| + // The adapter must be initialized at this point, but return an error instead |
| + // of asserting. |
| + if (!event_router->HasAdapter()) { |
| + SetError(kErrorAdapterNotInitialized); |
| + SendResponse(false); |
| + return; |
| + } |
| + |
| + scoped_ptr<apibtle::GetServices::Params> params( |
| + apibtle::GetServices::Params::Create(*args_)); |
| + EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| + |
| + std::string device_address = params->device_address; |
| + |
| + base::ListValue* service_list = event_router->GetServices(device_address); |
| + if (!service_list) { |
| + SetError( |
| + base::StringPrintf(kErrorDeviceNotFoundFormat, device_address.c_str())); |
| + SendResponse(false); |
| + return; |
| + } |
| + |
| + SetResult(service_list); |
| + SendResponse(true); |
| } |
| -bool BluetoothLowEnergyGetCharacteristicFunction::RunImpl() { |
| +void BluetoothLowEnergyGetCharacteristicFunction::DoWork() { |
| // TODO(armansito): Implement. |
| SetError("Call not supported."); |
| - return false; |
| + SendResponse(false); |
| } |
| -bool BluetoothLowEnergyGetCharacteristicsFunction::RunImpl() { |
| +void BluetoothLowEnergyGetCharacteristicsFunction::DoWork() { |
| // TODO(armansito): Implement. |
| SetError("Call not supported."); |
| - return false; |
| + SendResponse(false); |
| } |
| -bool BluetoothLowEnergyGetIncludedServicesFunction::RunImpl() { |
| +void BluetoothLowEnergyGetIncludedServicesFunction::DoWork() { |
| // TODO(armansito): Implement. |
| SetError("Call not supported."); |
| - return false; |
| + SendResponse(false); |
| } |
| -bool BluetoothLowEnergyGetDescriptorFunction::RunImpl() { |
| +void BluetoothLowEnergyGetDescriptorFunction::DoWork() { |
| // TODO(armansito): Implement. |
| SetError("Call not supported."); |
| - return false; |
| + SendResponse(false); |
| } |
| -bool BluetoothLowEnergyGetDescriptorsFunction::RunImpl() { |
| +void BluetoothLowEnergyGetDescriptorsFunction::DoWork() { |
| // TODO(armansito): Implement. |
| SetError("Call not supported."); |
| - return false; |
| + SendResponse(false); |
| } |
| -bool BluetoothLowEnergyReadCharacteristicValueFunction::RunImpl() { |
| +void BluetoothLowEnergyReadCharacteristicValueFunction::DoWork() { |
| // TODO(armansito): Implement. |
| SetError("Call not supported."); |
| - return false; |
| + SendResponse(false); |
| } |
| -bool BluetoothLowEnergyWriteCharacteristicValueFunction::RunImpl() { |
| +void BluetoothLowEnergyWriteCharacteristicValueFunction::DoWork() { |
| // TODO(armansito): Implement. |
| SetError("Call not supported."); |
| - return false; |
| + SendResponse(false); |
| } |
| -bool BluetoothLowEnergyReadDescriptorValueFunction::RunImpl() { |
| +void BluetoothLowEnergyReadDescriptorValueFunction::DoWork() { |
| // TODO(armansito): Implement. |
| SetError("Call not supported."); |
| - return false; |
| + SendResponse(false); |
| } |
| -bool BluetoothLowEnergyWriteDescriptorValueFunction::RunImpl() { |
| +void BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { |
| // TODO(armansito): Implement. |
| SetError("Call not supported."); |
| - return false; |
| + SendResponse(false); |
| } |
| } // namespace api |