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

Side by Side Diff: chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_api.cc

Issue 1976453002: Add permission parsing and the removeService API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@idl_changes
Patch Set: Created 4 years, 7 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
« no previous file with comments | « no previous file | chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_apitest_chromeos.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "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 <iterator> 9 #include <iterator>
10 #include <vector> 10 #include <vector>
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 173
174 bool HasProperty( 174 bool HasProperty(
175 const std::vector<apibtle::CharacteristicProperty>& api_properties, 175 const std::vector<apibtle::CharacteristicProperty>& api_properties,
176 apibtle::CharacteristicProperty property) { 176 apibtle::CharacteristicProperty property) {
177 if (find(api_properties.begin(), api_properties.end(), property) != 177 if (find(api_properties.begin(), api_properties.end(), property) !=
178 api_properties.end()) 178 api_properties.end())
179 return true; 179 return true;
180 return false; 180 return false;
181 } 181 }
182 182
183 bool HasPermission(
184 const std::vector<apibtle::DescriptorPermission>& api_permissions,
185 apibtle::DescriptorPermission permission) {
186 if (find(api_permissions.begin(), api_permissions.end(), permission) !=
xiyuan 2016/05/12 19:25:37 nit: just a return ? return find(api_permission
rkc 2016/05/12 20:32:53 Done.
187 api_permissions.end())
188 return true;
189 return false;
190 }
191
183 device::BluetoothGattCharacteristic::Properties GetBluetoothProperties( 192 device::BluetoothGattCharacteristic::Properties GetBluetoothProperties(
184 const std::vector<apibtle::CharacteristicProperty>& api_properties) { 193 const std::vector<apibtle::CharacteristicProperty>& api_properties) {
185 device::BluetoothGattCharacteristic::Properties properties = 194 device::BluetoothGattCharacteristic::Properties properties =
186 device::BluetoothGattCharacteristic::PROPERTY_NONE; 195 device::BluetoothGattCharacteristic::PROPERTY_NONE;
187 196
188 static_assert( 197 static_assert(
189 apibtle::CHARACTERISTIC_PROPERTY_LAST == 14, 198 apibtle::CHARACTERISTIC_PROPERTY_LAST == 14,
190 "Update required if the number of characteristic properties changes."); 199 "Update required if the number of characteristic properties changes.");
191 200
192 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_BROADCAST)) 201 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_BROADCAST))
193 properties |= device::BluetoothGattCharacteristic::PROPERTY_BROADCAST; 202 properties |= device::BluetoothGattCharacteristic::PROPERTY_BROADCAST;
194 203
195 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_READ)) 204 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_READ))
196 properties |= device::BluetoothGattCharacteristic::PROPERTY_READ; 205 properties |= device::BluetoothGattCharacteristic::PROPERTY_READ;
206
197 if (HasProperty(api_properties, 207 if (HasProperty(api_properties,
198 apibtle::CHARACTERISTIC_PROPERTY_WRITEWITHOUTRESPONSE)) 208 apibtle::CHARACTERISTIC_PROPERTY_WRITEWITHOUTRESPONSE))
199 properties |= 209 properties |=
xiyuan 2016/05/12 19:25:37 nit: wrap with {} if condition and branch takes mo
rkc 2016/05/12 20:32:53 Done.
200 device::BluetoothGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE; 210 device::BluetoothGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE;
211
201 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_WRITE)) 212 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_WRITE))
202 properties |= device::BluetoothGattCharacteristic::PROPERTY_WRITE; 213 properties |= device::BluetoothGattCharacteristic::PROPERTY_WRITE;
203 214
204 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_NOTIFY)) 215 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_NOTIFY))
205 properties |= device::BluetoothGattCharacteristic::PROPERTY_NOTIFY; 216 properties |= device::BluetoothGattCharacteristic::PROPERTY_NOTIFY;
206 217
207 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_INDICATE)) 218 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_INDICATE))
208 properties |= device::BluetoothGattCharacteristic::PROPERTY_INDICATE; 219 properties |= device::BluetoothGattCharacteristic::PROPERTY_INDICATE;
209 220
210 if (HasProperty(api_properties, 221 if (HasProperty(api_properties,
211 apibtle::CHARACTERISTIC_PROPERTY_AUTHENTICATEDSIGNEDWRITES)) 222 apibtle::CHARACTERISTIC_PROPERTY_AUTHENTICATEDSIGNEDWRITES))
212 properties |= device::BluetoothGattCharacteristic:: 223 properties |= device::BluetoothGattCharacteristic::
213 PROPERTY_AUTHENTICATED_SIGNED_WRITES; 224 PROPERTY_AUTHENTICATED_SIGNED_WRITES;
225
214 if (HasProperty(api_properties, 226 if (HasProperty(api_properties,
215 apibtle::CHARACTERISTIC_PROPERTY_EXTENDEDPROPERTIES)) 227 apibtle::CHARACTERISTIC_PROPERTY_EXTENDEDPROPERTIES))
216 properties |= 228 properties |=
217 device::BluetoothGattCharacteristic::PROPERTY_EXTENDED_PROPERTIES; 229 device::BluetoothGattCharacteristic::PROPERTY_EXTENDED_PROPERTIES;
230
218 if (HasProperty(api_properties, 231 if (HasProperty(api_properties,
219 apibtle::CHARACTERISTIC_PROPERTY_RELIABLEWRITE)) 232 apibtle::CHARACTERISTIC_PROPERTY_RELIABLEWRITE))
220 properties |= device::BluetoothGattCharacteristic::PROPERTY_RELIABLE_WRITE; 233 properties |= device::BluetoothGattCharacteristic::PROPERTY_RELIABLE_WRITE;
234
221 if (HasProperty(api_properties, 235 if (HasProperty(api_properties,
222 apibtle::CHARACTERISTIC_PROPERTY_WRITABLEAUXILIARIES)) 236 apibtle::CHARACTERISTIC_PROPERTY_WRITABLEAUXILIARIES))
223 properties |= 237 properties |=
224 device::BluetoothGattCharacteristic::PROPERTY_WRITABLE_AUXILIARIES; 238 device::BluetoothGattCharacteristic::PROPERTY_WRITABLE_AUXILIARIES;
225 239
240 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_ENCRYPTREAD))
241 properties |= device::BluetoothGattCharacteristic::PROPERTY_READ_ENCRYPTED;
242
243 if (HasProperty(api_properties,
244 apibtle::CHARACTERISTIC_PROPERTY_ENCRYPTWRITE))
245 properties |= device::BluetoothGattCharacteristic::PROPERTY_WRITE_ENCRYPTED;
246
247 if (HasProperty(api_properties,
248 apibtle::CHARACTERISTIC_PROPERTY_ENCRYPTAUTHENTICATEDREAD))
249 properties |= device::BluetoothGattCharacteristic::
250 PROPERTY_READ_ENCRYPTED_AUTHENTICATED;
251
252 if (HasProperty(api_properties,
253 apibtle::CHARACTERISTIC_PROPERTY_ENCRYPTAUTHENTICATEDWRITE))
254 properties |= device::BluetoothGattCharacteristic::
255 PROPERTY_WRITE_ENCRYPTED_AUTHENTICATED;
256
226 return properties; 257 return properties;
227 } 258 }
228 259
260 device::BluetoothGattCharacteristic::Permissions GetBluetoothPermissions(
261 const std::vector<apibtle::DescriptorPermission>& api_permissions) {
262 device::BluetoothGattCharacteristic::Permissions permissions =
263 device::BluetoothGattCharacteristic::PERMISSION_NONE;
264
265 static_assert(
266 apibtle::DESCRIPTOR_PERMISSION_LAST == 6,
267 "Update required if the number of characteristic properties changes.");
268
269 if (HasPermission(api_permissions, apibtle::DESCRIPTOR_PERMISSION_READ))
270 permissions |= device::BluetoothGattCharacteristic::PERMISSION_READ;
271
272 if (HasPermission(api_permissions, apibtle::DESCRIPTOR_PERMISSION_WRITE))
273 permissions |= device::BluetoothGattCharacteristic::PERMISSION_WRITE;
274
275 if (HasPermission(api_permissions,
276 apibtle::DESCRIPTOR_PERMISSION_ENCRYPTEDREAD))
277 permissions |=
278 device::BluetoothGattCharacteristic::PERMISSION_READ_ENCRYPTED;
279
280 if (HasPermission(api_permissions,
281 apibtle::DESCRIPTOR_PERMISSION_ENCRYPTEDWRITE))
282 permissions |=
283 device::BluetoothGattCharacteristic::PERMISSION_WRITE_ENCRYPTED;
284
285 if (HasPermission(api_permissions,
286 apibtle::DESCRIPTOR_PERMISSION_ENCRYPTEDAUTHENTICATEDREAD))
287 permissions |= device::BluetoothGattCharacteristic::
288 PERMISSION_READ_ENCRYPTED_AUTHENTICATED;
289
290 if (HasPermission(api_permissions,
291 apibtle::DESCRIPTOR_PERMISSION_ENCRYPTEDAUTHENTICATEDWRITE))
292 permissions |= device::BluetoothGattCharacteristic::
293 PERMISSION_WRITE_ENCRYPTED_AUTHENTICATED;
294
295 return permissions;
296 }
297
229 } // namespace 298 } // namespace
230 299
231 300
232 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI> > 301 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI> >
233 g_factory = LAZY_INSTANCE_INITIALIZER; 302 g_factory = LAZY_INSTANCE_INITIALIZER;
234 303
235 // static 304 // static
236 BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>* 305 BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>*
237 BluetoothLowEnergyAPI::GetFactoryInstance() { 306 BluetoothLowEnergyAPI::GetFactoryInstance() {
238 return g_factory.Pointer(); 307 return g_factory.Pointer();
(...skipping 1018 matching lines...) Expand 10 before | Expand all | Expand 10 after
1257 device::BluetoothLocalGattCharacteristic* characteristic = 1326 device::BluetoothLocalGattCharacteristic* characteristic =
1258 event_router_->GetLocalCharacteristic(params_->characteristic_id); 1327 event_router_->GetLocalCharacteristic(params_->characteristic_id);
1259 if (!characteristic) { 1328 if (!characteristic) {
1260 Respond(Error(kErrorInvalidCharacteristicId)); 1329 Respond(Error(kErrorInvalidCharacteristicId));
1261 return; 1330 return;
1262 } 1331 }
1263 1332
1264 base::WeakPtr<device::BluetoothLocalGattDescriptor> descriptor = 1333 base::WeakPtr<device::BluetoothLocalGattDescriptor> descriptor =
1265 device::BluetoothLocalGattDescriptor::Create( 1334 device::BluetoothLocalGattDescriptor::Create(
1266 device::BluetoothUUID(params_->descriptor.uuid), 1335 device::BluetoothUUID(params_->descriptor.uuid),
1267 device::BluetoothGattCharacteristic::Permissions(), characteristic); 1336 GetBluetoothPermissions(params_->descriptor.permissions),
1337 characteristic);
1268 1338
1269 Respond(ArgumentList( 1339 Respond(ArgumentList(
1270 apibtle::CreateDescriptor::Results::Create(descriptor->GetIdentifier()))); 1340 apibtle::CreateDescriptor::Results::Create(descriptor->GetIdentifier())));
1271 } 1341 }
1272 1342
1273 // registerService: 1343 // registerService:
1274 1344
1275 template class BLEPeripheralExtensionFunction<apibtle::RegisterService::Params>; 1345 template class BLEPeripheralExtensionFunction<apibtle::RegisterService::Params>;
1276 1346
1277 void BluetoothLowEnergyRegisterServiceFunction::DoWork() { 1347 void BluetoothLowEnergyRegisterServiceFunction::DoWork() {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1322 1392
1323 void BluetoothLowEnergyNotifyCharacteristicValueChangedFunction::DoWork() { 1393 void BluetoothLowEnergyNotifyCharacteristicValueChangedFunction::DoWork() {
1324 Respond(Error(kErrorPermissionDenied)); 1394 Respond(Error(kErrorPermissionDenied));
1325 } 1395 }
1326 1396
1327 // removeService: 1397 // removeService:
1328 1398
1329 template class BLEPeripheralExtensionFunction<apibtle::RemoveService::Params>; 1399 template class BLEPeripheralExtensionFunction<apibtle::RemoveService::Params>;
1330 1400
1331 void BluetoothLowEnergyRemoveServiceFunction::DoWork() { 1401 void BluetoothLowEnergyRemoveServiceFunction::DoWork() {
1332 Respond(Error(kErrorPermissionDenied)); 1402 device::BluetoothLocalGattService* service =
1403 event_router_->adapter()->GetGattService(params_->service_id);
1404 if (!service) {
1405 Respond(Error(kErrorInvalidServiceId));
1406 return;
1407 }
1408 event_router_->RemoveServiceFromApp(extension_id(), service->GetIdentifier());
1409 service->Delete();
1410 Respond(NoArguments());
1333 } 1411 }
1334 1412
1335 // sendRequestResponse: 1413 // sendRequestResponse:
1336 1414
1337 template class BLEPeripheralExtensionFunction< 1415 template class BLEPeripheralExtensionFunction<
1338 apibtle::SendRequestResponse::Params>; 1416 apibtle::SendRequestResponse::Params>;
1339 1417
1340 void BluetoothLowEnergySendRequestResponseFunction::DoWork() { 1418 void BluetoothLowEnergySendRequestResponseFunction::DoWork() {
1341 std::vector<uint8_t> uint8_vector; 1419 std::vector<uint8_t> uint8_vector;
1342 if (params_->response.value) { 1420 if (params_->response.value) {
1343 uint8_vector.assign(params_->response.value->begin(), 1421 uint8_vector.assign(params_->response.value->begin(),
1344 params_->response.value->end()); 1422 params_->response.value->end());
1345 } 1423 }
1346 event_router_->HandleRequestResponse( 1424 event_router_->HandleRequestResponse(
1347 extension(), params_->response.request_id, params_->response.is_error, 1425 extension(), params_->response.request_id, params_->response.is_error,
1348 uint8_vector); 1426 uint8_vector);
1349 Respond(NoArguments()); 1427 Respond(NoArguments());
1350 } 1428 }
1351 1429
1352 } // namespace api 1430 } // namespace api
1353 } // namespace extensions 1431 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_apitest_chromeos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698