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 8328f82cc883d6be535276e0460c8883534d1f20..0f6a0253da90c4794da043638c0bf94d507a1797 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 |
@@ -6,20 +6,22 @@ |
#include <stdint.h> |
#include <algorithm> |
-#include <utility> |
+#include <iterator> |
+#include <vector> |
#include "base/bind.h" |
+#include "base/callback.h" |
+#include "base/callback_forward.h" |
#include "base/command_line.h" |
#include "base/lazy_instance.h" |
-#include "base/strings/stringprintf.h" |
-#include "build/build_config.h" |
-#include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_api_advertisement.h" |
+#include "base/logging.h" |
+#include "base/values.h" |
#include "chrome/browser/extensions/api/bluetooth_low_energy/utils.h" |
#include "chrome/common/extensions/api/bluetooth_low_energy.h" |
#include "content/public/browser/browser_thread.h" |
-#include "extensions/browser/event_router.h" |
+#include "device/bluetooth/bluetooth_adapter.h" |
#include "extensions/common/api/bluetooth/bluetooth_manifest_data.h" |
-#include "extensions/common/permissions/permissions_data.h" |
+#include "extensions/common/extension.h" |
#include "extensions/common/switches.h" |
#if defined(OS_CHROMEOS) |
@@ -29,8 +31,6 @@ |
using content::BrowserContext; |
using content::BrowserThread; |
-namespace apibtle = extensions::api::bluetooth_low_energy; |
- |
namespace extensions { |
namespace { |
@@ -127,7 +127,8 @@ extensions::BluetoothLowEnergyEventRouter* GetEventRouter( |
return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); |
} |
-void DoWorkCallback(const base::Callback<bool()>& callback) { |
+template <typename T> |
+void DoWorkCallback(const base::Callback<T()>& callback) { |
DCHECK(!callback.is_null()); |
callback.Run(); |
} |
@@ -189,13 +190,13 @@ void BluetoothLowEnergyAPI::Shutdown() { |
namespace api { |
-BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() { |
-} |
+BluetoothLowEnergyExtensionFunctionDeprecated:: |
+ BluetoothLowEnergyExtensionFunctionDeprecated() {} |
-BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() { |
-} |
+BluetoothLowEnergyExtensionFunctionDeprecated:: |
+ ~BluetoothLowEnergyExtensionFunctionDeprecated() {} |
-bool BluetoothLowEnergyExtensionFunction::RunAsync() { |
+bool BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync() { |
DCHECK_CURRENTLY_ON(BrowserThread::UI); |
if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) { |
@@ -212,8 +213,9 @@ bool BluetoothLowEnergyExtensionFunction::RunAsync() { |
// It is safe to pass |this| here as ExtensionFunction is refcounted. |
if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( |
- &DoWorkCallback, |
- base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) { |
+ &DoWorkCallback<bool>, |
+ base::Bind(&BluetoothLowEnergyExtensionFunctionDeprecated::DoWork, |
+ this)))) { |
SetError(kErrorAdapterNotInitialized); |
return false; |
} |
@@ -221,6 +223,51 @@ bool BluetoothLowEnergyExtensionFunction::RunAsync() { |
return true; |
} |
+BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() {} |
+ |
+BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() {} |
+ |
+ExtensionFunction::ResponseAction BluetoothLowEnergyExtensionFunction::Run() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ |
+ if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) |
Devlin
2016/05/02 17:04:39
This still isn't ideal, because:
a) The bluetooth
|
+ return RespondNow(Error(kErrorPermissionDenied)); |
+ |
+ BluetoothLowEnergyEventRouter* event_router = |
+ GetEventRouter(browser_context()); |
+ if (!event_router->IsBluetoothSupported()) |
+ return RespondNow(Error(kErrorPlatformNotSupported)); |
+ |
+ // It is safe to pass |this| here as ExtensionFunction is refcounted. |
+ if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( |
+ &DoWorkCallback<void>, |
+ base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) { |
+ // DoWork will respond when the adapter gets initialized. |
+ return RespondNow(Error(kErrorAdapterNotInitialized)); |
+ } |
+ |
+ return RespondLater(); |
+} |
+ |
+template <typename Params> |
+BLEPeripheralExtensionFunction<Params>::BLEPeripheralExtensionFunction() {} |
+ |
+template <typename Params> |
+BLEPeripheralExtensionFunction<Params>::~BLEPeripheralExtensionFunction() {} |
+ |
+template <typename Params> |
+ExtensionFunction::ResponseAction |
+BLEPeripheralExtensionFunction<Params>::Run() { |
+ // Check permissions in manifest. |
+ if (!BluetoothManifestData::CheckPeripheralPermitted(extension())) |
+ return RespondNow(Error(kErrorPermissionDenied)); |
+ |
+ params_ = Params::Create(*args_); |
+ EXTENSION_FUNCTION_VALIDATE(params_.get() != NULL); |
+ |
+ return BluetoothLowEnergyExtensionFunction::Run(); |
+} |
+ |
bool BluetoothLowEnergyConnectFunction::DoWork() { |
DCHECK_CURRENTLY_ON(BrowserThread::UI); |
@@ -874,7 +921,7 @@ void BluetoothLowEnergyAdvertisementFunction::RemoveAdvertisement( |
bool BluetoothLowEnergyAdvertisementFunction::RunAsync() { |
Initialize(); |
- return BluetoothLowEnergyExtensionFunction::RunAsync(); |
+ return BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync(); |
} |
void BluetoothLowEnergyAdvertisementFunction::Initialize() { |
@@ -1065,5 +1112,63 @@ void BluetoothLowEnergyUnregisterAdvertisementFunction::ErrorCallback( |
SendResponse(false); |
} |
+template class BLEPeripheralExtensionFunction<apibtle::CreateService::Params>; |
+ |
+void BluetoothLowEnergyCreateServiceFunction::DoWork() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+// Using apibtle::CreateService::Results::Create causes a link error on |
+// Windows for some reason, despite it being almost identical to its |
+// characteristic and descriptor counterparts. Since there is no plan to |
+// provide this API on Windows, ifdef'ing this out is fine. |
+#if !defined(OS_WIN) |
+ Respond(ArgumentList(apibtle::CreateService::Results::Create(std::string()))); |
+#else |
+ Respond(Error(kErrorPlatformNotSupported)); |
+#endif |
+} |
+ |
+template class BLEPeripheralExtensionFunction< |
+ apibtle::CreateCharacteristic::Params>; |
+ |
+void BluetoothLowEnergyCreateCharacteristicFunction::DoWork() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ Respond(ArgumentList( |
+ apibtle::CreateCharacteristic::Results::Create(std::string()))); |
+} |
+ |
+template class BLEPeripheralExtensionFunction< |
+ apibtle::CreateDescriptor::Params>; |
+ |
+void BluetoothLowEnergyCreateDescriptorFunction::DoWork() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ Respond( |
+ ArgumentList(apibtle::CreateDescriptor::Results::Create(std::string()))); |
+} |
+ |
+template class BLEPeripheralExtensionFunction<apibtle::RegisterService::Params>; |
+ |
+void BluetoothLowEnergyRegisterServiceFunction::DoWork() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ Respond(ArgumentList(apibtle::RegisterService::Results::Create( |
+ apibtle::SERVICE_RESULT_SUCCESS))); |
+} |
+ |
+template class BLEPeripheralExtensionFunction< |
+ apibtle::UnregisterService::Params>; |
+ |
+void BluetoothLowEnergyUnregisterServiceFunction::DoWork() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ Respond(ArgumentList(apibtle::UnregisterService::Results::Create( |
+ apibtle::SERVICE_RESULT_SUCCESS))); |
+} |
+ |
+template class BLEPeripheralExtensionFunction< |
+ apibtle::SendRequestResponse::Params>; |
+ |
+void BluetoothLowEnergySendRequestResponseFunction::DoWork() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ Respond(NoArguments()); |
+} |
+ |
} // namespace api |
} // namespace extensions |