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())) | |
| 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 // Causes link error on Windows. API will never be on Windows, so #ifdefing. | |
| 262 #if !defined(OS_WIN) | |
| 263 params_ = Params::Create(*args_); | |
| 264 EXTENSION_FUNCTION_VALIDATE(params_.get() != NULL); | |
| 265 #endif | |
| 266 | |
| 267 return BluetoothLowEnergyExtensionFunction::Run(); | |
| 268 } | |
| 269 | |
| 224 bool BluetoothLowEnergyConnectFunction::DoWork() { | 270 bool BluetoothLowEnergyConnectFunction::DoWork() { |
| 225 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 271 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 226 | 272 |
| 227 BluetoothLowEnergyEventRouter* event_router = | 273 BluetoothLowEnergyEventRouter* event_router = |
| 228 GetEventRouter(browser_context()); | 274 GetEventRouter(browser_context()); |
| 229 | 275 |
| 230 // The adapter must be initialized at this point, but return an error instead | 276 // The adapter must be initialized at this point, but return an error instead |
| 231 // of asserting. | 277 // of asserting. |
| 232 if (!event_router->HasAdapter()) { | 278 if (!event_router->HasAdapter()) { |
| 233 SetError(kErrorAdapterNotInitialized); | 279 SetError(kErrorAdapterNotInitialized); |
| (...skipping 633 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 867 } | 913 } |
| 868 | 914 |
| 869 void BluetoothLowEnergyAdvertisementFunction::RemoveAdvertisement( | 915 void BluetoothLowEnergyAdvertisementFunction::RemoveAdvertisement( |
| 870 int advertisement_id) { | 916 int advertisement_id) { |
| 871 DCHECK(advertisements_manager_); | 917 DCHECK(advertisements_manager_); |
| 872 advertisements_manager_->Remove(extension_id(), advertisement_id); | 918 advertisements_manager_->Remove(extension_id(), advertisement_id); |
| 873 } | 919 } |
| 874 | 920 |
| 875 bool BluetoothLowEnergyAdvertisementFunction::RunAsync() { | 921 bool BluetoothLowEnergyAdvertisementFunction::RunAsync() { |
| 876 Initialize(); | 922 Initialize(); |
| 877 return BluetoothLowEnergyExtensionFunction::RunAsync(); | 923 return BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync(); |
| 878 } | 924 } |
| 879 | 925 |
| 880 void BluetoothLowEnergyAdvertisementFunction::Initialize() { | 926 void BluetoothLowEnergyAdvertisementFunction::Initialize() { |
| 881 advertisements_manager_ = | 927 advertisements_manager_ = |
| 882 ApiResourceManager<BluetoothApiAdvertisement>::Get(browser_context()); | 928 ApiResourceManager<BluetoothApiAdvertisement>::Get(browser_context()); |
| 883 } | 929 } |
| 884 | 930 |
| 885 static bool IsAutoLaunchedKioskApp(const ExtensionId& id) { | 931 static bool IsAutoLaunchedKioskApp(const ExtensionId& id) { |
| 886 #if defined(OS_CHROMEOS) | 932 #if defined(OS_CHROMEOS) |
| 887 chromeos::KioskAppManager::App app_info; | 933 chromeos::KioskAppManager::App app_info; |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1058 case device::BluetoothAdvertisement::ErrorCode:: | 1104 case device::BluetoothAdvertisement::ErrorCode:: |
| 1059 ERROR_ADVERTISEMENT_DOES_NOT_EXIST: | 1105 ERROR_ADVERTISEMENT_DOES_NOT_EXIST: |
| 1060 SetError(kStatusAdvertisementDoesNotExist); | 1106 SetError(kStatusAdvertisementDoesNotExist); |
| 1061 break; | 1107 break; |
| 1062 default: | 1108 default: |
| 1063 SetError(kErrorOperationFailed); | 1109 SetError(kErrorOperationFailed); |
| 1064 } | 1110 } |
| 1065 SendResponse(false); | 1111 SendResponse(false); |
| 1066 } | 1112 } |
| 1067 | 1113 |
| 1114 template class BLEPeripheralExtensionFunction<apibtle::CreateService::Params>; | |
| 1115 | |
| 1116 void BluetoothLowEnergyCreateServiceFunction::DoWork() { | |
| 1117 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 1118 // Causes link error on Windows. API will never be on Windows, so #ifdefing. | |
| 1119 #if !defined(OS_WIN) | |
| 1120 Respond(ArgumentList(apibtle::CreateService::Results::Create(std::string()))); | |
|
Devlin
2016/05/02 22:27:58
So this is because of windows evil renaming to Cre
rkc
2016/05/02 23:25:50
Done.
| |
| 1121 #else | |
| 1122 Respond(Error(kErrorPlatformNotSupported)); | |
| 1123 #endif | |
| 1124 } | |
| 1125 | |
| 1126 template class BLEPeripheralExtensionFunction< | |
| 1127 apibtle::CreateCharacteristic::Params>; | |
| 1128 | |
| 1129 void BluetoothLowEnergyCreateCharacteristicFunction::DoWork() { | |
| 1130 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 1131 Respond(ArgumentList( | |
| 1132 apibtle::CreateCharacteristic::Results::Create(std::string()))); | |
| 1133 } | |
| 1134 | |
| 1135 template class BLEPeripheralExtensionFunction< | |
| 1136 apibtle::CreateDescriptor::Params>; | |
| 1137 | |
| 1138 void BluetoothLowEnergyCreateDescriptorFunction::DoWork() { | |
| 1139 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 1140 Respond( | |
| 1141 ArgumentList(apibtle::CreateDescriptor::Results::Create(std::string()))); | |
| 1142 } | |
| 1143 | |
| 1144 template class BLEPeripheralExtensionFunction<apibtle::RegisterService::Params>; | |
| 1145 | |
| 1146 void BluetoothLowEnergyRegisterServiceFunction::DoWork() { | |
| 1147 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 1148 Respond(ArgumentList(apibtle::RegisterService::Results::Create( | |
| 1149 apibtle::SERVICE_RESULT_SUCCESS))); | |
| 1150 } | |
| 1151 | |
| 1152 template class BLEPeripheralExtensionFunction< | |
| 1153 apibtle::UnregisterService::Params>; | |
| 1154 | |
| 1155 void BluetoothLowEnergyUnregisterServiceFunction::DoWork() { | |
| 1156 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 1157 Respond(ArgumentList(apibtle::UnregisterService::Results::Create( | |
| 1158 apibtle::SERVICE_RESULT_SUCCESS))); | |
| 1159 } | |
| 1160 | |
| 1161 template class BLEPeripheralExtensionFunction< | |
| 1162 apibtle::SendRequestResponse::Params>; | |
| 1163 | |
| 1164 void BluetoothLowEnergySendRequestResponseFunction::DoWork() { | |
| 1165 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 1166 Respond(NoArguments()); | |
| 1167 } | |
| 1168 | |
| 1068 } // namespace api | 1169 } // namespace api |
| 1069 } // namespace extensions | 1170 } // namespace extensions |
| OLD | NEW |