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

Side by Side 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, 5 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 unified diff | Download patch
OLDNEW
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
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();
1099
1100 // Check permission in the manifest.
1101 if (!BluetoothManifestData::CheckPeripheralPermitted(extension())) {
1102 SetError(kErrorPermissionDenied);
1103 return false;
1104 }
1105
1106 // For advertisement API to be available the app has to be either auto
1107 // launched in Kiosk Mode or the enable-ble-advertisement-in-apps
1108 // should be set.
1109 if (!(IsAutoLaunchedKioskApp(extension()->id()) ||
1110 IsPeripheralFlagEnabled())) {
1111 SetError(kErrorPermissionDenied);
1112 return false;
1113 }
1114
1094 return BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync(); 1115 return BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync();
1095 } 1116 }
1096 1117
1097 void BluetoothLowEnergyAdvertisementFunction::Initialize() { 1118 void BluetoothLowEnergyAdvertisementFunction::Initialize() {
1098 advertisements_manager_ = 1119 advertisements_manager_ =
1099 ApiResourceManager<BluetoothApiAdvertisement>::Get(browser_context()); 1120 ApiResourceManager<BluetoothApiAdvertisement>::Get(browser_context());
1100 } 1121 }
1101 1122
1102 // RegisterAdvertisement: 1123 // RegisterAdvertisement:
1103 1124
1104 bool BluetoothLowEnergyRegisterAdvertisementFunction::DoWork() { 1125 bool BluetoothLowEnergyRegisterAdvertisementFunction::DoWork() {
1105 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1126 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1106 1127
1107 // Check permissions in manifest.
1108 if (!BluetoothManifestData::CheckPeripheralPermitted(extension())) {
1109 error_ = kErrorPermissionDenied;
1110 SendResponse(false);
1111 return false;
1112 }
1113
1114 // For this API to be available the app has to be either auto
1115 // launched in Kiosk Mode or the enable-ble-advertisement-in-apps
1116 // should be set.
1117 if (!(IsAutoLaunchedKioskApp(extension()->id()) ||
1118 IsPeripheralFlagEnabled())) {
1119 error_ = kErrorPermissionDenied;
1120 SendResponse(false);
1121 return false;
1122 }
1123
1124 BluetoothLowEnergyEventRouter* event_router = 1128 BluetoothLowEnergyEventRouter* event_router =
1125 GetEventRouter(browser_context()); 1129 GetEventRouter(browser_context());
1126 1130
1127 // The adapter must be initialized at this point, but return an error instead 1131 // The adapter must be initialized at this point, but return an error instead
1128 // of asserting. 1132 // of asserting.
1129 if (!event_router->HasAdapter()) { 1133 if (!event_router->HasAdapter()) {
1130 SetError(kErrorAdapterNotInitialized); 1134 SetError(kErrorAdapterNotInitialized);
1131 SendResponse(false); 1135 SendResponse(false);
1132 return false; 1136 return false;
1133 } 1137 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1192 SetError(kErrorOperationFailed); 1196 SetError(kErrorOperationFailed);
1193 } 1197 }
1194 SendResponse(false); 1198 SendResponse(false);
1195 } 1199 }
1196 1200
1197 // UnregisterAdvertisement: 1201 // UnregisterAdvertisement:
1198 1202
1199 bool BluetoothLowEnergyUnregisterAdvertisementFunction::DoWork() { 1203 bool BluetoothLowEnergyUnregisterAdvertisementFunction::DoWork() {
1200 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1204 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1201 1205
1202 // Check permission in the manifest.
1203 if (!BluetoothManifestData::CheckPeripheralPermitted(extension())) {
1204 error_ = kErrorPermissionDenied;
1205 SendResponse(false);
1206 return false;
1207 }
1208
1209 // For this API to be available the app has to be either auto
1210 // launched in Kiosk Mode or the enable-ble-advertisement-in-apps
1211 // should be set.
1212 if (!(IsAutoLaunchedKioskApp(extension()->id()) ||
1213 IsPeripheralFlagEnabled())) {
1214 error_ = kErrorPermissionDenied;
1215 SendResponse(false);
1216 return false;
1217 }
1218
1219 BluetoothLowEnergyEventRouter* event_router = 1206 BluetoothLowEnergyEventRouter* event_router =
1220 GetEventRouter(browser_context()); 1207 GetEventRouter(browser_context());
1221 1208
1222 // If we don't have an initialized adapter, unregistering is a no-op. 1209 // If we don't have an initialized adapter, unregistering is a no-op.
1223 if (!event_router->HasAdapter()) 1210 if (!event_router->HasAdapter())
1224 return true; 1211 return true;
1225 1212
1226 std::unique_ptr<apibtle::UnregisterAdvertisement::Params> params( 1213 std::unique_ptr<apibtle::UnregisterAdvertisement::Params> params(
1227 apibtle::UnregisterAdvertisement::Params::Create(*args_)); 1214 apibtle::UnregisterAdvertisement::Params::Create(*args_));
1228 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); 1215 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1260 case device::BluetoothAdvertisement::ErrorCode:: 1247 case device::BluetoothAdvertisement::ErrorCode::
1261 ERROR_ADVERTISEMENT_DOES_NOT_EXIST: 1248 ERROR_ADVERTISEMENT_DOES_NOT_EXIST:
1262 SetError(kStatusAdvertisementDoesNotExist); 1249 SetError(kStatusAdvertisementDoesNotExist);
1263 break; 1250 break;
1264 default: 1251 default:
1265 SetError(kErrorOperationFailed); 1252 SetError(kErrorOperationFailed);
1266 } 1253 }
1267 SendResponse(false); 1254 SendResponse(false);
1268 } 1255 }
1269 1256
1257 // ResetAdvertising:
1258
1259 bool BluetoothLowEnergyResetAdvertisingFunction::DoWork() {
1260 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1261
1262 BluetoothLowEnergyEventRouter* event_router =
1263 GetEventRouter(browser_context());
1264
1265 // If the adapter is not initialized, there is nothing to reset.
1266 if (!event_router->HasAdapter()) {
1267 SendResponse(true);
1268 return true;
1269 }
1270
1271 const base::hash_set<int>* advertisement_ids = GetAdvertisementIds();
1272 if (!advertisement_ids || advertisement_ids->empty()) {
1273 SendResponse(true);
1274 return true;
1275 }
1276
1277 // Copy the hash set, as RemoveAdvertisement can change advertisement_ids
1278 // 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.
1279 base::hash_set<int> advertisement_ids_tmp = *advertisement_ids;
1280 for (int advertisement_id : advertisement_ids_tmp) {
1281 RemoveAdvertisement(advertisement_id);
1282 }
1283
1284 event_router->adapter()->ResetAdvertising(
1285 base::Bind(&BluetoothLowEnergyResetAdvertisingFunction::SuccessCallback,
1286 this),
1287 base::Bind(&BluetoothLowEnergyResetAdvertisingFunction::ErrorCallback,
1288 this));
1289
1290 return true;
1291 }
1292
1293 void BluetoothLowEnergyResetAdvertisingFunction::SuccessCallback() {
1294 SendResponse(true);
1295 }
1296
1297 void BluetoothLowEnergyResetAdvertisingFunction::ErrorCallback(
1298 device::BluetoothAdvertisement::ErrorCode status) {
1299 #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.
1300 error_ = kErrorOperationFailed;
1301 SendResponse(false);
1302 #endif
1303 }
1304
1270 // SetAdvertisingInterval: 1305 // SetAdvertisingInterval:
1271 1306
1272 template class BLEPeripheralExtensionFunction< 1307 template class BLEPeripheralExtensionFunction<
1273 apibtle::SetAdvertisingInterval::Params>; 1308 apibtle::SetAdvertisingInterval::Params>;
1274 1309
1275 void BluetoothLowEnergySetAdvertisingIntervalFunction::DoWork() { 1310 void BluetoothLowEnergySetAdvertisingIntervalFunction::DoWork() {
1276 #if defined(OS_CHROMEOS) || defined(OS_LINUX) 1311 #if defined(OS_CHROMEOS) || defined(OS_LINUX)
1277 BluetoothLowEnergyEventRouter* event_router = 1312 BluetoothLowEnergyEventRouter* event_router =
1278 GetEventRouter(browser_context()); 1313 GetEventRouter(browser_context());
1279 event_router->adapter()->SetAdvertisingInterval( 1314 event_router->adapter()->SetAdvertisingInterval(
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
1496 params_->response.value->end()); 1531 params_->response.value->end());
1497 } 1532 }
1498 event_router_->HandleRequestResponse( 1533 event_router_->HandleRequestResponse(
1499 extension(), params_->response.request_id, params_->response.is_error, 1534 extension(), params_->response.request_id, params_->response.is_error,
1500 uint8_vector); 1535 uint8_vector);
1501 Respond(NoArguments()); 1536 Respond(NoArguments());
1502 } 1537 }
1503 1538
1504 } // namespace api 1539 } // namespace api
1505 } // namespace extensions 1540 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698