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 8328f82cc883d6be535276e0460c8883534d1f20..e01e0e3805c0a29e098a3308cdcf6797af2b4e44 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) |
| @@ -127,7 +129,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 +192,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 +215,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 +225,33 @@ bool BluetoothLowEnergyExtensionFunction::RunAsync() { |
| return true; |
| } |
| +BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() {} |
| + |
| +BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() {} |
| + |
| +ExtensionFunction::ResponseAction BluetoothLowEnergyExtensionFunction::Run() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + |
| + if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) { |
| + 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)))) { |
| + return RespondNow(Error(kErrorAdapterNotInitialized)); |
| + } |
| + |
| + return RespondLater(); |
| +} |
| + |
| bool BluetoothLowEnergyConnectFunction::DoWork() { |
| DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| @@ -874,7 +905,7 @@ void BluetoothLowEnergyAdvertisementFunction::RemoveAdvertisement( |
| bool BluetoothLowEnergyAdvertisementFunction::RunAsync() { |
| Initialize(); |
| - return BluetoothLowEnergyExtensionFunction::RunAsync(); |
| + return BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync(); |
| } |
| void BluetoothLowEnergyAdvertisementFunction::Initialize() { |
| @@ -1065,5 +1096,45 @@ void BluetoothLowEnergyUnregisterAdvertisementFunction::ErrorCallback( |
| SendResponse(false); |
| } |
| +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()))); |
|
Devlin
2016/04/29 17:13:13
What's the error?
This isn't quite fine, because
rkc
2016/04/29 20:15:58
Added to the permissions, this function will only
|
| +#endif |
| +} |
| + |
| +void BluetoothLowEnergyCreateCharacteristicFunction::DoWork() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + Respond(ArgumentList( |
| + apibtle::CreateCharacteristic::Results::Create(std::string()))); |
| +} |
| + |
| +void BluetoothLowEnergyCreateDescriptorFunction::DoWork() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + Respond( |
| + ArgumentList(apibtle::CreateDescriptor::Results::Create(std::string()))); |
| +} |
| + |
| +void BluetoothLowEnergyRegisterServiceFunction::DoWork() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + Respond(ArgumentList(apibtle::RegisterService::Results::Create( |
| + apibtle::SERVICE_RESULT_SUCCESS))); |
| +} |
| + |
| +void BluetoothLowEnergyUnregisterServiceFunction::DoWork() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + Respond(ArgumentList(apibtle::UnregisterService::Results::Create( |
| + apibtle::SERVICE_RESULT_SUCCESS))); |
| +} |
| + |
| +void BluetoothLowEnergySendRequestResponseFunction::DoWork() { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + Respond(NoArguments()); |
| +} |
| + |
| } // namespace api |
| } // namespace extensions |