Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(20)

Unified Diff: extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_api.cc

Issue 2861533004: Implement chrome.bluetoothLowEnergy.resetAdvertising(). (Closed)
Patch Set: Implement chrome.bluetoothLowEnergy.resetAllAdvertisements(). Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..99121ed12a9fb7d2f87a74f5a5a9c6bba7b2b87a 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,8 +1089,29 @@ 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();
+
+ // Check permission in the manifest.
+ if (!BluetoothManifestData::CheckPeripheralPermitted(extension())) {
+ SetError(kErrorPermissionDenied);
+ return false;
+ }
+
+ // For advertisement 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())) {
+ SetError(kErrorPermissionDenied);
+ return false;
+ }
+
return BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync();
}
@@ -1104,23 +1125,6 @@ void BluetoothLowEnergyAdvertisementFunction::Initialize() {
bool BluetoothLowEnergyRegisterAdvertisementFunction::DoWork() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
- // Check permissions in 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());
@@ -1199,23 +1203,6 @@ void BluetoothLowEnergyRegisterAdvertisementFunction::ErrorCallback(
bool BluetoothLowEnergyUnregisterAdvertisementFunction::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());
@@ -1267,6 +1254,54 @@ void BluetoothLowEnergyUnregisterAdvertisementFunction::ErrorCallback(
SendResponse(false);
}
+// ResetAdvertising:
+
+bool BluetoothLowEnergyResetAdvertisingFunction::DoWork() {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+
+ BluetoothLowEnergyEventRouter* event_router =
+ GetEventRouter(browser_context());
+
+ // If the adapter is not initialized, there is nothing to reset.
+ if (!event_router->HasAdapter()) {
+ SendResponse(true);
+ return true;
+ }
+
+ const base::hash_set<int>* advertisement_ids = GetAdvertisementIds();
+ if (!advertisement_ids || advertisement_ids->empty()) {
+ SendResponse(true);
+ return true;
+ }
+
+ // Copy the hash set, as RemoveAdvertisement can change advertisement_ids
+ // while we are iterating on it.
tbarzic 2017/06/27 22:49:12 nit: s/iterating on/iterating over/
Sonny Sasaka 2017/06/27 23:14:01 Done.
+ base::hash_set<int> advertisement_ids_tmp = *advertisement_ids;
+ for (int advertisement_id : advertisement_ids_tmp) {
+ RemoveAdvertisement(advertisement_id);
+ }
+
+ event_router->adapter()->ResetAdvertising(
+ base::Bind(&BluetoothLowEnergyResetAdvertisingFunction::SuccessCallback,
+ this),
+ base::Bind(&BluetoothLowEnergyResetAdvertisingFunction::ErrorCallback,
+ this));
+
+ return true;
+}
+
+void BluetoothLowEnergyResetAdvertisingFunction::SuccessCallback() {
+ SendResponse(true);
+}
+
+void BluetoothLowEnergyResetAdvertisingFunction::ErrorCallback(
+ device::BluetoothAdvertisement::ErrorCode status) {
+#if defined(OS_CHROMEOS) || defined(OS_LINUX)
tbarzic 2017/06/27 22:49:12 no ifdefs?
Sonny Sasaka 2017/06/27 23:14:01 Done.
+ error_ = kErrorOperationFailed;
+ SendResponse(false);
+#endif
+}
+
// SetAdvertisingInterval:
template class BLEPeripheralExtensionFunction<

Powered by Google App Engine
This is Rietveld 408576698