Chromium Code Reviews| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <utility> | 9 #include <iterator> |
| 10 #include <vector> | |
| 10 | 11 |
| 11 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback.h" | |
| 14 #include "base/callback_forward.h" | |
| 12 #include "base/command_line.h" | 15 #include "base/command_line.h" |
| 13 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
| 14 #include "base/strings/stringprintf.h" | 17 #include "base/logging.h" |
| 15 #include "build/build_config.h" | 18 #include "base/values.h" |
| 16 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_api_adver tisement.h" | |
| 17 #include "chrome/browser/extensions/api/bluetooth_low_energy/utils.h" | 19 #include "chrome/browser/extensions/api/bluetooth_low_energy/utils.h" |
| 18 #include "chrome/common/extensions/api/bluetooth_low_energy.h" | 20 #include "chrome/common/extensions/api/bluetooth_low_energy.h" |
| 19 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 20 #include "extensions/browser/event_router.h" | 22 #include "device/bluetooth/bluetooth_adapter.h" |
| 21 #include "extensions/common/api/bluetooth/bluetooth_manifest_data.h" | 23 #include "extensions/common/api/bluetooth/bluetooth_manifest_data.h" |
| 22 #include "extensions/common/permissions/permissions_data.h" | 24 #include "extensions/common/extension.h" |
| 23 #include "extensions/common/switches.h" | 25 #include "extensions/common/switches.h" |
| 24 | 26 |
| 25 #if defined(OS_CHROMEOS) | 27 #if defined(OS_CHROMEOS) |
| 26 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" | 28 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" |
| 27 #endif | 29 #endif |
| 28 | 30 |
| 29 using content::BrowserContext; | 31 using content::BrowserContext; |
| 30 using content::BrowserThread; | 32 using content::BrowserThread; |
| 31 | 33 |
| 32 namespace apibtle = extensions::api::bluetooth_low_energy; | 34 namespace apibtle = extensions::api::bluetooth_low_energy; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 120 } | 122 } |
| 121 return ""; | 123 return ""; |
| 122 } | 124 } |
| 123 | 125 |
| 124 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( | 126 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( |
| 125 BrowserContext* context) { | 127 BrowserContext* context) { |
| 126 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 128 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 127 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); | 129 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); |
| 128 } | 130 } |
| 129 | 131 |
| 130 void DoWorkCallback(const base::Callback<bool()>& callback) { | 132 void DoWorkReturnBoolCallback(const base::Callback<bool()>& callback) { |
|
Devlin
2016/04/27 14:45:03
if you wanted to be extra fancy you could do somet
rkc
2016/04/27 20:38:56
Converting DoWork to return void would require cha
| |
| 131 DCHECK(!callback.is_null()); | 133 DCHECK(!callback.is_null()); |
| 132 callback.Run(); | 134 callback.Run(); |
| 133 } | 135 } |
| 136 | |
| 137 void DoWorkCallback(const base::Callback<void()>& callback) { | |
| 138 DCHECK(!callback.is_null()); | |
| 139 callback.Run(); | |
| 140 } | |
| 134 | 141 |
| 135 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData> | 142 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData> |
| 136 CreateManufacturerData( | 143 CreateManufacturerData( |
| 137 std::vector<apibtle::ManufacturerData>* manufacturer_data) { | 144 std::vector<apibtle::ManufacturerData>* manufacturer_data) { |
| 138 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData> | 145 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData> |
| 139 created_data(new device::BluetoothAdvertisement::ManufacturerData()); | 146 created_data(new device::BluetoothAdvertisement::ManufacturerData()); |
| 140 for (const auto& it : *manufacturer_data) { | 147 for (const auto& it : *manufacturer_data) { |
| 141 std::vector<uint8_t> data(it.data.size()); | 148 std::vector<uint8_t> data(it.data.size()); |
| 142 std::copy(it.data.begin(), it.data.end(), data.begin()); | 149 std::copy(it.data.begin(), it.data.end(), data.begin()); |
| 143 (*created_data)[it.id] = data; | 150 (*created_data)[it.id] = data; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 | 189 |
| 183 BluetoothLowEnergyAPI::~BluetoothLowEnergyAPI() { | 190 BluetoothLowEnergyAPI::~BluetoothLowEnergyAPI() { |
| 184 } | 191 } |
| 185 | 192 |
| 186 void BluetoothLowEnergyAPI::Shutdown() { | 193 void BluetoothLowEnergyAPI::Shutdown() { |
| 187 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 194 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 188 } | 195 } |
| 189 | 196 |
| 190 namespace api { | 197 namespace api { |
| 191 | 198 |
| 192 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() { | 199 BluetoothLowEnergyExtensionFunctionDeprecated:: |
| 193 } | 200 BluetoothLowEnergyExtensionFunctionDeprecated() {} |
| 194 | 201 |
| 195 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() { | 202 BluetoothLowEnergyExtensionFunctionDeprecated:: |
| 196 } | 203 ~BluetoothLowEnergyExtensionFunctionDeprecated() {} |
| 197 | 204 |
| 198 bool BluetoothLowEnergyExtensionFunction::RunAsync() { | 205 bool BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync() { |
| 199 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 206 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 200 | 207 |
| 201 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) { | 208 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) { |
| 202 error_ = kErrorPermissionDenied; | 209 error_ = kErrorPermissionDenied; |
| 203 return false; | 210 return false; |
| 204 } | 211 } |
| 205 | 212 |
| 206 BluetoothLowEnergyEventRouter* event_router = | 213 BluetoothLowEnergyEventRouter* event_router = |
| 207 GetEventRouter(browser_context()); | 214 GetEventRouter(browser_context()); |
| 208 if (!event_router->IsBluetoothSupported()) { | 215 if (!event_router->IsBluetoothSupported()) { |
| 209 SetError(kErrorPlatformNotSupported); | 216 SetError(kErrorPlatformNotSupported); |
| 210 return false; | 217 return false; |
| 211 } | 218 } |
| 212 | 219 |
| 213 // It is safe to pass |this| here as ExtensionFunction is refcounted. | 220 // It is safe to pass |this| here as ExtensionFunction is refcounted. |
| 214 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( | 221 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( |
| 215 &DoWorkCallback, | 222 &DoWorkReturnBoolCallback, |
| 216 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) { | 223 base::Bind(&BluetoothLowEnergyExtensionFunctionDeprecated::DoWork, |
| 224 this)))) { | |
| 217 SetError(kErrorAdapterNotInitialized); | 225 SetError(kErrorAdapterNotInitialized); |
| 218 return false; | 226 return false; |
| 219 } | 227 } |
| 220 | 228 |
| 221 return true; | 229 return true; |
| 222 } | 230 } |
| 223 | 231 |
| 232 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() {} | |
| 233 | |
| 234 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() {} | |
| 235 | |
| 236 ExtensionFunction::ResponseAction BluetoothLowEnergyExtensionFunction::Run() { | |
| 237 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 238 | |
| 239 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) { | |
| 240 return RespondNow(Error(kErrorPermissionDenied)); | |
| 241 } | |
| 242 | |
| 243 BluetoothLowEnergyEventRouter* event_router = | |
| 244 GetEventRouter(browser_context()); | |
| 245 if (!event_router->IsBluetoothSupported()) { | |
| 246 return RespondNow(Error(kErrorPlatformNotSupported)); | |
| 247 } | |
| 248 | |
| 249 // It is safe to pass |this| here as ExtensionFunction is refcounted. | |
| 250 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( | |
| 251 &DoWorkCallback, | |
| 252 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) { | |
| 253 return RespondNow(Error(kErrorAdapterNotInitialized)); | |
| 254 } | |
| 255 | |
| 256 return RespondLater(); | |
| 257 } | |
| 258 | |
| 224 bool BluetoothLowEnergyConnectFunction::DoWork() { | 259 bool BluetoothLowEnergyConnectFunction::DoWork() { |
| 225 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 260 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 226 | 261 |
| 227 BluetoothLowEnergyEventRouter* event_router = | 262 BluetoothLowEnergyEventRouter* event_router = |
| 228 GetEventRouter(browser_context()); | 263 GetEventRouter(browser_context()); |
| 229 | 264 |
| 230 // The adapter must be initialized at this point, but return an error instead | 265 // The adapter must be initialized at this point, but return an error instead |
| 231 // of asserting. | 266 // of asserting. |
| 232 if (!event_router->HasAdapter()) { | 267 if (!event_router->HasAdapter()) { |
| 233 SetError(kErrorAdapterNotInitialized); | 268 SetError(kErrorAdapterNotInitialized); |
| (...skipping 633 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 867 } | 902 } |
| 868 | 903 |
| 869 void BluetoothLowEnergyAdvertisementFunction::RemoveAdvertisement( | 904 void BluetoothLowEnergyAdvertisementFunction::RemoveAdvertisement( |
| 870 int advertisement_id) { | 905 int advertisement_id) { |
| 871 DCHECK(advertisements_manager_); | 906 DCHECK(advertisements_manager_); |
| 872 advertisements_manager_->Remove(extension_id(), advertisement_id); | 907 advertisements_manager_->Remove(extension_id(), advertisement_id); |
| 873 } | 908 } |
| 874 | 909 |
| 875 bool BluetoothLowEnergyAdvertisementFunction::RunAsync() { | 910 bool BluetoothLowEnergyAdvertisementFunction::RunAsync() { |
| 876 Initialize(); | 911 Initialize(); |
| 877 return BluetoothLowEnergyExtensionFunction::RunAsync(); | 912 return BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync(); |
| 878 } | 913 } |
| 879 | 914 |
| 880 void BluetoothLowEnergyAdvertisementFunction::Initialize() { | 915 void BluetoothLowEnergyAdvertisementFunction::Initialize() { |
| 881 advertisements_manager_ = | 916 advertisements_manager_ = |
| 882 ApiResourceManager<BluetoothApiAdvertisement>::Get(browser_context()); | 917 ApiResourceManager<BluetoothApiAdvertisement>::Get(browser_context()); |
| 883 } | 918 } |
| 884 | 919 |
| 885 static bool IsAutoLaunchedKioskApp(const ExtensionId& id) { | 920 static bool IsAutoLaunchedKioskApp(const ExtensionId& id) { |
| 886 #if defined(OS_CHROMEOS) | 921 #if defined(OS_CHROMEOS) |
| 887 chromeos::KioskAppManager::App app_info; | 922 chromeos::KioskAppManager::App app_info; |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1058 case device::BluetoothAdvertisement::ErrorCode:: | 1093 case device::BluetoothAdvertisement::ErrorCode:: |
| 1059 ERROR_ADVERTISEMENT_DOES_NOT_EXIST: | 1094 ERROR_ADVERTISEMENT_DOES_NOT_EXIST: |
| 1060 SetError(kStatusAdvertisementDoesNotExist); | 1095 SetError(kStatusAdvertisementDoesNotExist); |
| 1061 break; | 1096 break; |
| 1062 default: | 1097 default: |
| 1063 SetError(kErrorOperationFailed); | 1098 SetError(kErrorOperationFailed); |
| 1064 } | 1099 } |
| 1065 SendResponse(false); | 1100 SendResponse(false); |
| 1066 } | 1101 } |
| 1067 | 1102 |
| 1103 void BluetoothLowEnergyCreateServiceFunction::DoWork() { | |
| 1104 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 1105 Respond(ArgumentList(apibtle::CreateService::Results::Create(std::string()))); | |
| 1106 } | |
| 1107 | |
| 1108 void BluetoothLowEnergyCreateCharacteristicFunction::DoWork() { | |
| 1109 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 1110 Respond(ArgumentList( | |
| 1111 apibtle::CreateCharacteristic::Results::Create(std::string()))); | |
| 1112 } | |
| 1113 | |
| 1114 void BluetoothLowEnergyCreateDescriptorFunction::DoWork() { | |
| 1115 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 1116 Respond( | |
| 1117 ArgumentList(apibtle::CreateDescriptor::Results::Create(std::string()))); | |
| 1118 } | |
| 1119 | |
| 1120 void BluetoothLowEnergyRegisterServiceFunction::DoWork() { | |
| 1121 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 1122 Respond(ArgumentList(apibtle::RegisterService::Results::Create( | |
| 1123 apibtle::SERVICE_RESULT_SUCCESS))); | |
| 1124 } | |
| 1125 | |
| 1126 void BluetoothLowEnergyUnregisterServiceFunction::DoWork() { | |
| 1127 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 1128 Respond(ArgumentList(apibtle::UnregisterService::Results::Create( | |
| 1129 apibtle::SERVICE_RESULT_SUCCESS))); | |
| 1130 } | |
| 1131 | |
| 1068 } // namespace api | 1132 } // namespace api |
| 1069 } // namespace extensions | 1133 } // namespace extensions |
| OLD | NEW |