Index: extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_api.cc |
diff --git a/extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_api.cc b/extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_api.cc |
index 515838fcc5b58ea8564c49baf6508f8f6ab43e29..cf10b2ffd6e95dd0da36b89f239fc82e029265b9 100644 |
--- a/extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_api.cc |
+++ b/extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_api.cc |
@@ -1089,6 +1089,11 @@ void BluetoothLowEnergyAdvertisementFunction::RemoveAdvertisement( |
advertisements_manager_->Remove(extension_id(), advertisement_id); |
} |
+const base::hash_set<int>* |
+BluetoothLowEnergyAdvertisementFunction::GetAdvertisementIds() { |
+ return advertisements_manager_->GetResourceIds(extension_id()); |
+} |
+ |
bool BluetoothLowEnergyAdvertisementFunction::RunAsync() { |
Initialize(); |
return BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync(); |
@@ -1267,6 +1272,82 @@ void BluetoothLowEnergyUnregisterAdvertisementFunction::ErrorCallback( |
SendResponse(false); |
} |
+// ResetAllAdvertisements: |
+ |
+bool BluetoothLowEnergyResetAllAdvertisementsFunction::DoWork() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ |
+ // Check permission in the manifest. |
+ if (!BluetoothManifestData::CheckPeripheralPermitted(extension())) { |
+ error_ = kErrorPermissionDenied; |
+ SendResponse(false); |
+ return false; |
+ } |
+ |
+ // For this API to be available the app has to be either auto |
+ // launched in Kiosk Mode or the enable-ble-advertisement-in-apps |
+ // should be set. |
+ if (!(IsAutoLaunchedKioskApp(extension()->id()) || |
+ IsPeripheralFlagEnabled())) { |
+ error_ = kErrorPermissionDenied; |
+ SendResponse(false); |
+ return false; |
+ } |
+ |
+ BluetoothLowEnergyEventRouter* event_router = |
+ GetEventRouter(browser_context()); |
+ |
+ // If we don't have an initialized adapter, resetting advertisements is a |
+ // no-op. |
+ if (!event_router->HasAdapter()) |
+ return true; |
+ |
+ base::hash_set<int> advertisement_ids; |
+ if (GetAdvertisementIds()) { |
rkc
2017/05/19 23:44:26
This is not what we want to do. The need for this
Sonny Sasaka
2017/06/01 22:28:11
Done.
|
+ advertisement_ids = *GetAdvertisementIds(); |
+ } |
+ |
+ if (advertisement_ids.size() == 0) { |
+ SendResponse(true); |
+ return true; |
+ } |
+ |
+ for (int advertisement_id : advertisement_ids) { |
+ BluetoothApiAdvertisement* advertisement = |
+ GetAdvertisement(advertisement_id); |
+ advertisement->advertisement()->Unregister( |
+ base::Bind(&BluetoothLowEnergyResetAllAdvertisementsFunction:: |
+ UnregisterSuccessCallback, |
+ this, advertisement_id), |
+ base::Bind(&BluetoothLowEnergyResetAllAdvertisementsFunction:: |
+ UnregisterErrorCallback, |
+ this, advertisement_id)); |
+ } |
+ |
+ return true; |
+} |
+ |
+void BluetoothLowEnergyResetAllAdvertisementsFunction::OnUnregister( |
+ int advertisement_id) { |
+ RemoveAdvertisement(advertisement_id); |
+ const base::hash_set<int>* advertisement_ids = GetAdvertisementIds(); |
+ // The last advertisement has been unregistered, we can send response now. |
+ if (advertisement_ids && advertisement_ids->size() == 0) { |
+ SendResponse(true); |
+ } |
+} |
+ |
+void BluetoothLowEnergyResetAllAdvertisementsFunction:: |
+ UnregisterSuccessCallback(int advertisement_id) { |
+ OnUnregister(advertisement_id); |
+} |
+ |
+void BluetoothLowEnergyResetAllAdvertisementsFunction::UnregisterErrorCallback( |
+ int advertisement_id, |
+ device::BluetoothAdvertisement::ErrorCode status) { |
+ OnUnregister(advertisement_id); |
+} |
+ |
// SetAdvertisingInterval: |
template class BLEPeripheralExtensionFunction< |