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..ba091f613442371373ad9b21b65553899865e0b5 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()))) { |
+ 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; |
+ } |
+ |
+ results_ = apibtle::GetService::Results::Create(service); |
+ 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; |
+ |
+ BluetoothLowEnergyEventRouter::ServiceList service_list; |
+ if (!event_router->GetServices(device_address, &service_list)) { |
+ SetError( |
+ base::StringPrintf(kErrorDeviceNotFoundFormat, device_address.c_str())); |
+ SendResponse(false); |
+ return; |
+ } |
+ |
+ results_ = apibtle::GetServices::Results::Create(service_list).Pass(); |
+ 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 |