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; | |
| 33 | |
| 34 namespace extensions { | 34 namespace extensions { |
| 35 | 35 |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 const char kErrorAdapterNotInitialized[] = | 38 const char kErrorAdapterNotInitialized[] = |
| 39 "Could not initialize Bluetooth adapter"; | 39 "Could not initialize Bluetooth adapter"; |
| 40 const char kErrorAlreadyConnected[] = "Already connected"; | 40 const char kErrorAlreadyConnected[] = "Already connected"; |
| 41 const char kErrorAlreadyNotifying[] = "Already notifying"; | 41 const char kErrorAlreadyNotifying[] = "Already notifying"; |
| 42 const char kErrorAttributeLengthInvalid[] = "Attribute length invalid"; | 42 const char kErrorAttributeLengthInvalid[] = "Attribute length invalid"; |
| 43 const char kErrorAuthenticationFailed[] = "Authentication failed"; | 43 const char kErrorAuthenticationFailed[] = "Authentication failed"; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 120 } | 120 } |
| 121 return ""; | 121 return ""; |
| 122 } | 122 } |
| 123 | 123 |
| 124 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( | 124 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( |
| 125 BrowserContext* context) { | 125 BrowserContext* context) { |
| 126 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 126 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 127 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); | 127 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); |
| 128 } | 128 } |
| 129 | 129 |
| 130 void DoWorkCallback(const base::Callback<bool()>& callback) { | 130 template <typename T> |
| 131 void DoWorkCallback(const base::Callback<T()>& callback) { | |
| 131 DCHECK(!callback.is_null()); | 132 DCHECK(!callback.is_null()); |
| 132 callback.Run(); | 133 callback.Run(); |
| 133 } | 134 } |
| 134 | 135 |
| 135 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData> | 136 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData> |
| 136 CreateManufacturerData( | 137 CreateManufacturerData( |
| 137 std::vector<apibtle::ManufacturerData>* manufacturer_data) { | 138 std::vector<apibtle::ManufacturerData>* manufacturer_data) { |
| 138 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData> | 139 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData> |
| 139 created_data(new device::BluetoothAdvertisement::ManufacturerData()); | 140 created_data(new device::BluetoothAdvertisement::ManufacturerData()); |
| 140 for (const auto& it : *manufacturer_data) { | 141 for (const auto& it : *manufacturer_data) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 | 183 |
| 183 BluetoothLowEnergyAPI::~BluetoothLowEnergyAPI() { | 184 BluetoothLowEnergyAPI::~BluetoothLowEnergyAPI() { |
| 184 } | 185 } |
| 185 | 186 |
| 186 void BluetoothLowEnergyAPI::Shutdown() { | 187 void BluetoothLowEnergyAPI::Shutdown() { |
| 187 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 188 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 188 } | 189 } |
| 189 | 190 |
| 190 namespace api { | 191 namespace api { |
| 191 | 192 |
| 192 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() { | 193 BluetoothLowEnergyExtensionFunctionDeprecated:: |
| 193 } | 194 BluetoothLowEnergyExtensionFunctionDeprecated() {} |
| 194 | 195 |
| 195 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() { | 196 BluetoothLowEnergyExtensionFunctionDeprecated:: |
| 196 } | 197 ~BluetoothLowEnergyExtensionFunctionDeprecated() {} |
| 197 | 198 |
| 198 bool BluetoothLowEnergyExtensionFunction::RunAsync() { | 199 bool BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync() { |
| 199 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 200 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 200 | 201 |
| 201 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) { | 202 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) { |
| 202 error_ = kErrorPermissionDenied; | 203 error_ = kErrorPermissionDenied; |
| 203 return false; | 204 return false; |
| 204 } | 205 } |
| 205 | 206 |
| 206 BluetoothLowEnergyEventRouter* event_router = | 207 BluetoothLowEnergyEventRouter* event_router = |
| 207 GetEventRouter(browser_context()); | 208 GetEventRouter(browser_context()); |
| 208 if (!event_router->IsBluetoothSupported()) { | 209 if (!event_router->IsBluetoothSupported()) { |
| 209 SetError(kErrorPlatformNotSupported); | 210 SetError(kErrorPlatformNotSupported); |
| 210 return false; | 211 return false; |
| 211 } | 212 } |
| 212 | 213 |
| 213 // It is safe to pass |this| here as ExtensionFunction is refcounted. | 214 // It is safe to pass |this| here as ExtensionFunction is refcounted. |
| 214 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( | 215 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( |
| 215 &DoWorkCallback, | 216 &DoWorkCallback<bool>, |
| 216 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) { | 217 base::Bind(&BluetoothLowEnergyExtensionFunctionDeprecated::DoWork, |
| 218 this)))) { | |
| 217 SetError(kErrorAdapterNotInitialized); | 219 SetError(kErrorAdapterNotInitialized); |
| 218 return false; | 220 return false; |
| 219 } | 221 } |
| 220 | 222 |
| 221 return true; | 223 return true; |
| 222 } | 224 } |
| 223 | 225 |
| 226 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() {} | |
| 227 | |
| 228 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() {} | |
| 229 | |
| 230 ExtensionFunction::ResponseAction BluetoothLowEnergyExtensionFunction::Run() { | |
| 231 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 232 | |
| 233 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) | |
|
Devlin
2016/05/02 17:04:39
This still isn't ideal, because:
a) The bluetooth
| |
| 234 return RespondNow(Error(kErrorPermissionDenied)); | |
| 235 | |
| 236 BluetoothLowEnergyEventRouter* event_router = | |
| 237 GetEventRouter(browser_context()); | |
| 238 if (!event_router->IsBluetoothSupported()) | |
| 239 return RespondNow(Error(kErrorPlatformNotSupported)); | |
| 240 | |
| 241 // It is safe to pass |this| here as ExtensionFunction is refcounted. | |
| 242 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( | |
| 243 &DoWorkCallback<void>, | |
| 244 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) { | |
| 245 // DoWork will respond when the adapter gets initialized. | |
| 246 return RespondNow(Error(kErrorAdapterNotInitialized)); | |
| 247 } | |
| 248 | |
| 249 return RespondLater(); | |
| 250 } | |
| 251 | |
| 252 template <typename Params> | |
| 253 BLEPeripheralExtensionFunction<Params>::BLEPeripheralExtensionFunction() {} | |
| 254 | |
| 255 template <typename Params> | |
| 256 BLEPeripheralExtensionFunction<Params>::~BLEPeripheralExtensionFunction() {} | |
| 257 | |
| 258 template <typename Params> | |
| 259 ExtensionFunction::ResponseAction | |
| 260 BLEPeripheralExtensionFunction<Params>::Run() { | |
| 261 // Check permissions in manifest. | |
| 262 if (!BluetoothManifestData::CheckPeripheralPermitted(extension())) | |
| 263 return RespondNow(Error(kErrorPermissionDenied)); | |
| 264 | |
| 265 params_ = Params::Create(*args_); | |
| 266 EXTENSION_FUNCTION_VALIDATE(params_.get() != NULL); | |
| 267 | |
| 268 return BluetoothLowEnergyExtensionFunction::Run(); | |
| 269 } | |
| 270 | |
| 224 bool BluetoothLowEnergyConnectFunction::DoWork() { | 271 bool BluetoothLowEnergyConnectFunction::DoWork() { |
| 225 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 272 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 226 | 273 |
| 227 BluetoothLowEnergyEventRouter* event_router = | 274 BluetoothLowEnergyEventRouter* event_router = |
| 228 GetEventRouter(browser_context()); | 275 GetEventRouter(browser_context()); |
| 229 | 276 |
| 230 // The adapter must be initialized at this point, but return an error instead | 277 // The adapter must be initialized at this point, but return an error instead |
| 231 // of asserting. | 278 // of asserting. |
| 232 if (!event_router->HasAdapter()) { | 279 if (!event_router->HasAdapter()) { |
| 233 SetError(kErrorAdapterNotInitialized); | 280 SetError(kErrorAdapterNotInitialized); |
| (...skipping 633 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 867 } | 914 } |
| 868 | 915 |
| 869 void BluetoothLowEnergyAdvertisementFunction::RemoveAdvertisement( | 916 void BluetoothLowEnergyAdvertisementFunction::RemoveAdvertisement( |
| 870 int advertisement_id) { | 917 int advertisement_id) { |
| 871 DCHECK(advertisements_manager_); | 918 DCHECK(advertisements_manager_); |
| 872 advertisements_manager_->Remove(extension_id(), advertisement_id); | 919 advertisements_manager_->Remove(extension_id(), advertisement_id); |
| 873 } | 920 } |
| 874 | 921 |
| 875 bool BluetoothLowEnergyAdvertisementFunction::RunAsync() { | 922 bool BluetoothLowEnergyAdvertisementFunction::RunAsync() { |
| 876 Initialize(); | 923 Initialize(); |
| 877 return BluetoothLowEnergyExtensionFunction::RunAsync(); | 924 return BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync(); |
| 878 } | 925 } |
| 879 | 926 |
| 880 void BluetoothLowEnergyAdvertisementFunction::Initialize() { | 927 void BluetoothLowEnergyAdvertisementFunction::Initialize() { |
| 881 advertisements_manager_ = | 928 advertisements_manager_ = |
| 882 ApiResourceManager<BluetoothApiAdvertisement>::Get(browser_context()); | 929 ApiResourceManager<BluetoothApiAdvertisement>::Get(browser_context()); |
| 883 } | 930 } |
| 884 | 931 |
| 885 static bool IsAutoLaunchedKioskApp(const ExtensionId& id) { | 932 static bool IsAutoLaunchedKioskApp(const ExtensionId& id) { |
| 886 #if defined(OS_CHROMEOS) | 933 #if defined(OS_CHROMEOS) |
| 887 chromeos::KioskAppManager::App app_info; | 934 chromeos::KioskAppManager::App app_info; |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1058 case device::BluetoothAdvertisement::ErrorCode:: | 1105 case device::BluetoothAdvertisement::ErrorCode:: |
| 1059 ERROR_ADVERTISEMENT_DOES_NOT_EXIST: | 1106 ERROR_ADVERTISEMENT_DOES_NOT_EXIST: |
| 1060 SetError(kStatusAdvertisementDoesNotExist); | 1107 SetError(kStatusAdvertisementDoesNotExist); |
| 1061 break; | 1108 break; |
| 1062 default: | 1109 default: |
| 1063 SetError(kErrorOperationFailed); | 1110 SetError(kErrorOperationFailed); |
| 1064 } | 1111 } |
| 1065 SendResponse(false); | 1112 SendResponse(false); |
| 1066 } | 1113 } |
| 1067 | 1114 |
| 1115 template class BLEPeripheralExtensionFunction<apibtle::CreateService::Params>; | |
| 1116 | |
| 1117 void BluetoothLowEnergyCreateServiceFunction::DoWork() { | |
| 1118 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 1119 // Using apibtle::CreateService::Results::Create causes a link error on | |
| 1120 // Windows for some reason, despite it being almost identical to its | |
| 1121 // characteristic and descriptor counterparts. Since there is no plan to | |
| 1122 // provide this API on Windows, ifdef'ing this out is fine. | |
| 1123 #if !defined(OS_WIN) | |
| 1124 Respond(ArgumentList(apibtle::CreateService::Results::Create(std::string()))); | |
| 1125 #else | |
| 1126 Respond(Error(kErrorPlatformNotSupported)); | |
| 1127 #endif | |
| 1128 } | |
| 1129 | |
| 1130 template class BLEPeripheralExtensionFunction< | |
| 1131 apibtle::CreateCharacteristic::Params>; | |
| 1132 | |
| 1133 void BluetoothLowEnergyCreateCharacteristicFunction::DoWork() { | |
| 1134 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 1135 Respond(ArgumentList( | |
| 1136 apibtle::CreateCharacteristic::Results::Create(std::string()))); | |
| 1137 } | |
| 1138 | |
| 1139 template class BLEPeripheralExtensionFunction< | |
| 1140 apibtle::CreateDescriptor::Params>; | |
| 1141 | |
| 1142 void BluetoothLowEnergyCreateDescriptorFunction::DoWork() { | |
| 1143 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 1144 Respond( | |
| 1145 ArgumentList(apibtle::CreateDescriptor::Results::Create(std::string()))); | |
| 1146 } | |
| 1147 | |
| 1148 template class BLEPeripheralExtensionFunction<apibtle::RegisterService::Params>; | |
| 1149 | |
| 1150 void BluetoothLowEnergyRegisterServiceFunction::DoWork() { | |
| 1151 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 1152 Respond(ArgumentList(apibtle::RegisterService::Results::Create( | |
| 1153 apibtle::SERVICE_RESULT_SUCCESS))); | |
| 1154 } | |
| 1155 | |
| 1156 template class BLEPeripheralExtensionFunction< | |
| 1157 apibtle::UnregisterService::Params>; | |
| 1158 | |
| 1159 void BluetoothLowEnergyUnregisterServiceFunction::DoWork() { | |
| 1160 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 1161 Respond(ArgumentList(apibtle::UnregisterService::Results::Create( | |
| 1162 apibtle::SERVICE_RESULT_SUCCESS))); | |
| 1163 } | |
| 1164 | |
| 1165 template class BLEPeripheralExtensionFunction< | |
| 1166 apibtle::SendRequestResponse::Params>; | |
| 1167 | |
| 1168 void BluetoothLowEnergySendRequestResponseFunction::DoWork() { | |
| 1169 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 1170 Respond(NoArguments()); | |
| 1171 } | |
| 1172 | |
| 1068 } // namespace api | 1173 } // namespace api |
| 1069 } // namespace extensions | 1174 } // namespace extensions |
| OLD | NEW |