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 "extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_api.h " | 5 #include "extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_api.h " |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <iterator> | 9 #include <iterator> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 1071 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1082 DCHECK(advertisements_manager_); | 1082 DCHECK(advertisements_manager_); |
| 1083 return advertisements_manager_->Get(extension_id(), advertisement_id); | 1083 return advertisements_manager_->Get(extension_id(), advertisement_id); |
| 1084 } | 1084 } |
| 1085 | 1085 |
| 1086 void BluetoothLowEnergyAdvertisementFunction::RemoveAdvertisement( | 1086 void BluetoothLowEnergyAdvertisementFunction::RemoveAdvertisement( |
| 1087 int advertisement_id) { | 1087 int advertisement_id) { |
| 1088 DCHECK(advertisements_manager_); | 1088 DCHECK(advertisements_manager_); |
| 1089 advertisements_manager_->Remove(extension_id(), advertisement_id); | 1089 advertisements_manager_->Remove(extension_id(), advertisement_id); |
| 1090 } | 1090 } |
| 1091 | 1091 |
| 1092 const base::hash_set<int>* | |
| 1093 BluetoothLowEnergyAdvertisementFunction::GetAdvertisementIds() { | |
| 1094 return advertisements_manager_->GetResourceIds(extension_id()); | |
| 1095 } | |
| 1096 | |
| 1092 bool BluetoothLowEnergyAdvertisementFunction::RunAsync() { | 1097 bool BluetoothLowEnergyAdvertisementFunction::RunAsync() { |
| 1093 Initialize(); | 1098 Initialize(); |
| 1094 return BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync(); | 1099 return BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync(); |
| 1095 } | 1100 } |
| 1096 | 1101 |
| 1097 void BluetoothLowEnergyAdvertisementFunction::Initialize() { | 1102 void BluetoothLowEnergyAdvertisementFunction::Initialize() { |
| 1098 advertisements_manager_ = | 1103 advertisements_manager_ = |
| 1099 ApiResourceManager<BluetoothApiAdvertisement>::Get(browser_context()); | 1104 ApiResourceManager<BluetoothApiAdvertisement>::Get(browser_context()); |
| 1100 } | 1105 } |
| 1101 | 1106 |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1260 case device::BluetoothAdvertisement::ErrorCode:: | 1265 case device::BluetoothAdvertisement::ErrorCode:: |
| 1261 ERROR_ADVERTISEMENT_DOES_NOT_EXIST: | 1266 ERROR_ADVERTISEMENT_DOES_NOT_EXIST: |
| 1262 SetError(kStatusAdvertisementDoesNotExist); | 1267 SetError(kStatusAdvertisementDoesNotExist); |
| 1263 break; | 1268 break; |
| 1264 default: | 1269 default: |
| 1265 SetError(kErrorOperationFailed); | 1270 SetError(kErrorOperationFailed); |
| 1266 } | 1271 } |
| 1267 SendResponse(false); | 1272 SendResponse(false); |
| 1268 } | 1273 } |
| 1269 | 1274 |
| 1275 // ResetAdvertising: | |
| 1276 | |
| 1277 bool BluetoothLowEnergyResetAdvertisingFunction::DoWork() { | |
| 1278 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 1279 | |
| 1280 // Check permission in the manifest. | |
| 1281 if (!BluetoothManifestData::CheckPeripheralPermitted(extension())) { | |
| 1282 error_ = kErrorPermissionDenied; | |
| 1283 SendResponse(false); | |
| 1284 return false; | |
| 1285 } | |
| 1286 | |
| 1287 // For this API to be available the app has to be either auto | |
| 1288 // launched in Kiosk Mode or the enable-ble-advertisement-in-apps | |
| 1289 // should be set. | |
| 1290 if (!(IsAutoLaunchedKioskApp(extension()->id()) || | |
|
tbarzic
2017/06/27 19:04:57
is this the case for all advertisement functions?
Sonny Sasaka
2017/06/27 22:03:14
Done.
| |
| 1291 IsPeripheralFlagEnabled())) { | |
| 1292 error_ = kErrorPermissionDenied; | |
| 1293 SendResponse(false); | |
| 1294 return false; | |
| 1295 } | |
| 1296 | |
| 1297 BluetoothLowEnergyEventRouter* event_router = | |
| 1298 GetEventRouter(browser_context()); | |
| 1299 | |
| 1300 // If we don't have an initialized adapter, resetting advertisements is a | |
|
tbarzic
2017/06/27 19:04:57
nit:
If the adapter is not initialized, there is n
Sonny Sasaka
2017/06/27 22:03:14
Done.
| |
| 1301 // no-op. | |
| 1302 if (!event_router->HasAdapter()) | |
| 1303 return true; | |
|
tbarzic
2017/06/27 19:04:58
SendResponse?
Sonny Sasaka
2017/06/27 22:03:14
Done.
| |
| 1304 | |
| 1305 base::hash_set<int> advertisement_ids; | |
|
tbarzic
2017/06/27 19:04:57
can you comment why you have to copy IDs.
maybe s
Sonny Sasaka
2017/06/27 22:03:14
Done.
| |
| 1306 if (GetAdvertisementIds()) { | |
| 1307 advertisement_ids = *GetAdvertisementIds(); | |
| 1308 } | |
| 1309 | |
| 1310 if (advertisement_ids.size() == 0) { | |
|
tbarzic
2017/06/27 19:04:57
nit: empty()
Sonny Sasaka
2017/06/27 22:03:14
Done.
| |
| 1311 SendResponse(true); | |
| 1312 return true; | |
| 1313 } | |
| 1314 | |
| 1315 for (int advertisement_id : advertisement_ids) { | |
| 1316 RemoveAdvertisement(advertisement_id); | |
| 1317 } | |
| 1318 | |
| 1319 event_router->adapter()->ResetAdvertising( | |
| 1320 base::Bind(&BluetoothLowEnergyResetAdvertisingFunction::SuccessCallback, | |
| 1321 this), | |
| 1322 base::Bind(&BluetoothLowEnergyResetAdvertisingFunction::ErrorCallback, | |
| 1323 this)); | |
| 1324 | |
| 1325 return true; | |
| 1326 } | |
| 1327 | |
| 1328 void BluetoothLowEnergyResetAdvertisingFunction::SuccessCallback() { | |
| 1329 Respond(NoArguments()); | |
|
tbarzic
2017/06/27 19:04:57
I'd be consistent with DoWork and call SendRespons
Sonny Sasaka
2017/06/27 22:03:14
Done.
| |
| 1330 } | |
| 1331 | |
| 1332 void BluetoothLowEnergyResetAdvertisingFunction::ErrorCallback( | |
| 1333 device::BluetoothAdvertisement::ErrorCode status) { | |
| 1334 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | |
| 1335 Respond(Error(kErrorOperationFailed)); | |
|
tbarzic
2017/06/27 19:04:57
error_ = kErrorOperationFailed;
SendResponse(false
Sonny Sasaka
2017/06/27 22:03:14
Done.
| |
| 1336 #endif | |
| 1337 } | |
| 1338 | |
| 1270 // SetAdvertisingInterval: | 1339 // SetAdvertisingInterval: |
| 1271 | 1340 |
| 1272 template class BLEPeripheralExtensionFunction< | 1341 template class BLEPeripheralExtensionFunction< |
| 1273 apibtle::SetAdvertisingInterval::Params>; | 1342 apibtle::SetAdvertisingInterval::Params>; |
| 1274 | 1343 |
| 1275 void BluetoothLowEnergySetAdvertisingIntervalFunction::DoWork() { | 1344 void BluetoothLowEnergySetAdvertisingIntervalFunction::DoWork() { |
| 1276 #if defined(OS_CHROMEOS) || defined(OS_LINUX) | 1345 #if defined(OS_CHROMEOS) || defined(OS_LINUX) |
| 1277 BluetoothLowEnergyEventRouter* event_router = | 1346 BluetoothLowEnergyEventRouter* event_router = |
| 1278 GetEventRouter(browser_context()); | 1347 GetEventRouter(browser_context()); |
| 1279 event_router->adapter()->SetAdvertisingInterval( | 1348 event_router->adapter()->SetAdvertisingInterval( |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1496 params_->response.value->end()); | 1565 params_->response.value->end()); |
| 1497 } | 1566 } |
| 1498 event_router_->HandleRequestResponse( | 1567 event_router_->HandleRequestResponse( |
| 1499 extension(), params_->response.request_id, params_->response.is_error, | 1568 extension(), params_->response.request_id, params_->response.is_error, |
| 1500 uint8_vector); | 1569 uint8_vector); |
| 1501 Respond(NoArguments()); | 1570 Respond(NoArguments()); |
| 1502 } | 1571 } |
| 1503 | 1572 |
| 1504 } // namespace api | 1573 } // namespace api |
| 1505 } // namespace extensions | 1574 } // namespace extensions |
| OLD | NEW |