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 template <typename T> |
| 133 void DoWorkCallback(const base::Callback<T()>& callback) { |
131 DCHECK(!callback.is_null()); | 134 DCHECK(!callback.is_null()); |
132 callback.Run(); | 135 callback.Run(); |
133 } | 136 } |
134 | 137 |
135 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData> | 138 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData> |
136 CreateManufacturerData( | 139 CreateManufacturerData( |
137 std::vector<apibtle::ManufacturerData>* manufacturer_data) { | 140 std::vector<apibtle::ManufacturerData>* manufacturer_data) { |
138 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData> | 141 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData> |
139 created_data(new device::BluetoothAdvertisement::ManufacturerData()); | 142 created_data(new device::BluetoothAdvertisement::ManufacturerData()); |
140 for (const auto& it : *manufacturer_data) { | 143 for (const auto& it : *manufacturer_data) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 | 185 |
183 BluetoothLowEnergyAPI::~BluetoothLowEnergyAPI() { | 186 BluetoothLowEnergyAPI::~BluetoothLowEnergyAPI() { |
184 } | 187 } |
185 | 188 |
186 void BluetoothLowEnergyAPI::Shutdown() { | 189 void BluetoothLowEnergyAPI::Shutdown() { |
187 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 190 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
188 } | 191 } |
189 | 192 |
190 namespace api { | 193 namespace api { |
191 | 194 |
192 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() { | 195 BluetoothLowEnergyExtensionFunctionDeprecated:: |
193 } | 196 BluetoothLowEnergyExtensionFunctionDeprecated() {} |
194 | 197 |
195 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() { | 198 BluetoothLowEnergyExtensionFunctionDeprecated:: |
196 } | 199 ~BluetoothLowEnergyExtensionFunctionDeprecated() {} |
197 | 200 |
198 bool BluetoothLowEnergyExtensionFunction::RunAsync() { | 201 bool BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync() { |
199 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 202 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
200 | 203 |
201 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) { | 204 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) { |
202 error_ = kErrorPermissionDenied; | 205 error_ = kErrorPermissionDenied; |
203 return false; | 206 return false; |
204 } | 207 } |
205 | 208 |
206 BluetoothLowEnergyEventRouter* event_router = | 209 BluetoothLowEnergyEventRouter* event_router = |
207 GetEventRouter(browser_context()); | 210 GetEventRouter(browser_context()); |
208 if (!event_router->IsBluetoothSupported()) { | 211 if (!event_router->IsBluetoothSupported()) { |
209 SetError(kErrorPlatformNotSupported); | 212 SetError(kErrorPlatformNotSupported); |
210 return false; | 213 return false; |
211 } | 214 } |
212 | 215 |
213 // It is safe to pass |this| here as ExtensionFunction is refcounted. | 216 // It is safe to pass |this| here as ExtensionFunction is refcounted. |
214 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( | 217 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( |
215 &DoWorkCallback, | 218 &DoWorkCallback<bool>, |
216 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) { | 219 base::Bind(&BluetoothLowEnergyExtensionFunctionDeprecated::DoWork, |
| 220 this)))) { |
217 SetError(kErrorAdapterNotInitialized); | 221 SetError(kErrorAdapterNotInitialized); |
218 return false; | 222 return false; |
219 } | 223 } |
220 | 224 |
221 return true; | 225 return true; |
222 } | 226 } |
223 | 227 |
| 228 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() {} |
| 229 |
| 230 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() {} |
| 231 |
| 232 ExtensionFunction::ResponseAction BluetoothLowEnergyExtensionFunction::Run() { |
| 233 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 234 |
| 235 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) |
| 236 return RespondNow(Error(kErrorPermissionDenied)); |
| 237 |
| 238 BluetoothLowEnergyEventRouter* event_router = |
| 239 GetEventRouter(browser_context()); |
| 240 if (!event_router->IsBluetoothSupported()) |
| 241 return RespondNow(Error(kErrorPlatformNotSupported)); |
| 242 |
| 243 // It is safe to pass |this| here as ExtensionFunction is refcounted. |
| 244 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( |
| 245 &DoWorkCallback<void>, |
| 246 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) { |
| 247 // DoWork will respond when the adapter gets initialized. |
| 248 return RespondNow(Error(kErrorAdapterNotInitialized)); |
| 249 } |
| 250 |
| 251 return RespondLater(); |
| 252 } |
| 253 |
| 254 template <typename Params> |
| 255 BLEPeripheralExtensionFunction<Params>::BLEPeripheralExtensionFunction() {} |
| 256 |
| 257 template <typename Params> |
| 258 BLEPeripheralExtensionFunction<Params>::~BLEPeripheralExtensionFunction() {} |
| 259 |
| 260 template <typename Params> |
| 261 ExtensionFunction::ResponseAction |
| 262 BLEPeripheralExtensionFunction<Params>::Run() { |
| 263 // Causes link error on Windows. API will never be on Windows, so #ifdefing. |
| 264 #if !defined(OS_WIN) |
| 265 params_ = Params::Create(*args_); |
| 266 EXTENSION_FUNCTION_VALIDATE(params_.get() != NULL); |
| 267 #endif |
| 268 |
| 269 return BluetoothLowEnergyExtensionFunction::Run(); |
| 270 } |
| 271 |
224 bool BluetoothLowEnergyConnectFunction::DoWork() { | 272 bool BluetoothLowEnergyConnectFunction::DoWork() { |
225 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 273 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
226 | 274 |
227 BluetoothLowEnergyEventRouter* event_router = | 275 BluetoothLowEnergyEventRouter* event_router = |
228 GetEventRouter(browser_context()); | 276 GetEventRouter(browser_context()); |
229 | 277 |
230 // The adapter must be initialized at this point, but return an error instead | 278 // The adapter must be initialized at this point, but return an error instead |
231 // of asserting. | 279 // of asserting. |
232 if (!event_router->HasAdapter()) { | 280 if (!event_router->HasAdapter()) { |
233 SetError(kErrorAdapterNotInitialized); | 281 SetError(kErrorAdapterNotInitialized); |
(...skipping 633 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
867 } | 915 } |
868 | 916 |
869 void BluetoothLowEnergyAdvertisementFunction::RemoveAdvertisement( | 917 void BluetoothLowEnergyAdvertisementFunction::RemoveAdvertisement( |
870 int advertisement_id) { | 918 int advertisement_id) { |
871 DCHECK(advertisements_manager_); | 919 DCHECK(advertisements_manager_); |
872 advertisements_manager_->Remove(extension_id(), advertisement_id); | 920 advertisements_manager_->Remove(extension_id(), advertisement_id); |
873 } | 921 } |
874 | 922 |
875 bool BluetoothLowEnergyAdvertisementFunction::RunAsync() { | 923 bool BluetoothLowEnergyAdvertisementFunction::RunAsync() { |
876 Initialize(); | 924 Initialize(); |
877 return BluetoothLowEnergyExtensionFunction::RunAsync(); | 925 return BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync(); |
878 } | 926 } |
879 | 927 |
880 void BluetoothLowEnergyAdvertisementFunction::Initialize() { | 928 void BluetoothLowEnergyAdvertisementFunction::Initialize() { |
881 advertisements_manager_ = | 929 advertisements_manager_ = |
882 ApiResourceManager<BluetoothApiAdvertisement>::Get(browser_context()); | 930 ApiResourceManager<BluetoothApiAdvertisement>::Get(browser_context()); |
883 } | 931 } |
884 | 932 |
885 static bool IsAutoLaunchedKioskApp(const ExtensionId& id) { | 933 static bool IsAutoLaunchedKioskApp(const ExtensionId& id) { |
886 #if defined(OS_CHROMEOS) | 934 #if defined(OS_CHROMEOS) |
887 chromeos::KioskAppManager::App app_info; | 935 chromeos::KioskAppManager::App app_info; |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1058 case device::BluetoothAdvertisement::ErrorCode:: | 1106 case device::BluetoothAdvertisement::ErrorCode:: |
1059 ERROR_ADVERTISEMENT_DOES_NOT_EXIST: | 1107 ERROR_ADVERTISEMENT_DOES_NOT_EXIST: |
1060 SetError(kStatusAdvertisementDoesNotExist); | 1108 SetError(kStatusAdvertisementDoesNotExist); |
1061 break; | 1109 break; |
1062 default: | 1110 default: |
1063 SetError(kErrorOperationFailed); | 1111 SetError(kErrorOperationFailed); |
1064 } | 1112 } |
1065 SendResponse(false); | 1113 SendResponse(false); |
1066 } | 1114 } |
1067 | 1115 |
| 1116 template class BLEPeripheralExtensionFunction<apibtle::CreateService::Params>; |
| 1117 |
| 1118 void BluetoothLowEnergyCreateServiceFunction::DoWork() { |
| 1119 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1120 // Causes link error on Windows. API will never be on Windows, so #ifdefing. |
| 1121 // TODO: Ideally this should be handled by our feature system, so that this |
| 1122 // code doesn't even compile on OSes it isn't being used on, but currently this |
| 1123 // is not possible. |
| 1124 #if !defined(OS_WIN) |
| 1125 Respond(ArgumentList(apibtle::CreateService::Results::Create(std::string()))); |
| 1126 #else |
| 1127 Respond(Error(kErrorPlatformNotSupported)); |
| 1128 #endif |
| 1129 } |
| 1130 |
| 1131 template class BLEPeripheralExtensionFunction< |
| 1132 apibtle::CreateCharacteristic::Params>; |
| 1133 |
| 1134 void BluetoothLowEnergyCreateCharacteristicFunction::DoWork() { |
| 1135 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1136 Respond(ArgumentList( |
| 1137 apibtle::CreateCharacteristic::Results::Create(std::string()))); |
| 1138 } |
| 1139 |
| 1140 template class BLEPeripheralExtensionFunction< |
| 1141 apibtle::CreateDescriptor::Params>; |
| 1142 |
| 1143 void BluetoothLowEnergyCreateDescriptorFunction::DoWork() { |
| 1144 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1145 Respond( |
| 1146 ArgumentList(apibtle::CreateDescriptor::Results::Create(std::string()))); |
| 1147 } |
| 1148 |
| 1149 template class BLEPeripheralExtensionFunction<apibtle::RegisterService::Params>; |
| 1150 |
| 1151 void BluetoothLowEnergyRegisterServiceFunction::DoWork() { |
| 1152 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1153 Respond(ArgumentList(apibtle::RegisterService::Results::Create( |
| 1154 apibtle::SERVICE_RESULT_SUCCESS))); |
| 1155 } |
| 1156 |
| 1157 template class BLEPeripheralExtensionFunction< |
| 1158 apibtle::UnregisterService::Params>; |
| 1159 |
| 1160 void BluetoothLowEnergyUnregisterServiceFunction::DoWork() { |
| 1161 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1162 Respond(ArgumentList(apibtle::UnregisterService::Results::Create( |
| 1163 apibtle::SERVICE_RESULT_SUCCESS))); |
| 1164 } |
| 1165 |
| 1166 template class BLEPeripheralExtensionFunction< |
| 1167 apibtle::SendRequestResponse::Params>; |
| 1168 |
| 1169 void BluetoothLowEnergySendRequestResponseFunction::DoWork() { |
| 1170 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1171 Respond(NoArguments()); |
| 1172 } |
| 1173 |
1068 } // namespace api | 1174 } // namespace api |
1069 } // namespace extensions | 1175 } // namespace extensions |
OLD | NEW |