| 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 "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 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 std::vector<uint8_t> data(it.data.size()); | 167 std::vector<uint8_t> data(it.data.size()); |
| 168 std::copy(it.data.begin(), it.data.end(), data.begin()); | 168 std::copy(it.data.begin(), it.data.end(), data.begin()); |
| 169 (*created_data)[it.uuid] = data; | 169 (*created_data)[it.uuid] = data; |
| 170 } | 170 } |
| 171 return created_data; | 171 return created_data; |
| 172 } | 172 } |
| 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 return find(api_properties.begin(), api_properties.end(), property) != |
| 178 api_properties.end()) | 178 api_properties.end(); |
| 179 return true; | 179 } |
| 180 return false; | 180 |
| 181 bool HasPermission( |
| 182 const std::vector<apibtle::DescriptorPermission>& api_permissions, |
| 183 apibtle::DescriptorPermission permission) { |
| 184 return find(api_permissions.begin(), api_permissions.end(), permission) != |
| 185 api_permissions.end(); |
| 181 } | 186 } |
| 182 | 187 |
| 183 device::BluetoothGattCharacteristic::Properties GetBluetoothProperties( | 188 device::BluetoothGattCharacteristic::Properties GetBluetoothProperties( |
| 184 const std::vector<apibtle::CharacteristicProperty>& api_properties) { | 189 const std::vector<apibtle::CharacteristicProperty>& api_properties) { |
| 185 device::BluetoothGattCharacteristic::Properties properties = | 190 device::BluetoothGattCharacteristic::Properties properties = |
| 186 device::BluetoothGattCharacteristic::PROPERTY_NONE; | 191 device::BluetoothGattCharacteristic::PROPERTY_NONE; |
| 187 | 192 |
| 188 static_assert( | 193 static_assert( |
| 189 apibtle::CHARACTERISTIC_PROPERTY_LAST == 14, | 194 apibtle::CHARACTERISTIC_PROPERTY_LAST == 14, |
| 190 "Update required if the number of characteristic properties changes."); | 195 "Update required if the number of characteristic properties changes."); |
| 191 | 196 |
| 192 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_BROADCAST)) | 197 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_BROADCAST)) { |
| 193 properties |= device::BluetoothGattCharacteristic::PROPERTY_BROADCAST; | 198 properties |= device::BluetoothGattCharacteristic::PROPERTY_BROADCAST; |
| 199 } |
| 194 | 200 |
| 195 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_READ)) | 201 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_READ)) { |
| 196 properties |= device::BluetoothGattCharacteristic::PROPERTY_READ; | 202 properties |= device::BluetoothGattCharacteristic::PROPERTY_READ; |
| 203 } |
| 204 |
| 197 if (HasProperty(api_properties, | 205 if (HasProperty(api_properties, |
| 198 apibtle::CHARACTERISTIC_PROPERTY_WRITEWITHOUTRESPONSE)) | 206 apibtle::CHARACTERISTIC_PROPERTY_WRITEWITHOUTRESPONSE)) { |
| 199 properties |= | 207 properties |= |
| 200 device::BluetoothGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE; | 208 device::BluetoothGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE; |
| 201 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_WRITE)) | 209 } |
| 210 |
| 211 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_WRITE)) { |
| 202 properties |= device::BluetoothGattCharacteristic::PROPERTY_WRITE; | 212 properties |= device::BluetoothGattCharacteristic::PROPERTY_WRITE; |
| 213 } |
| 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; |
| 217 } |
| 206 | 218 |
| 207 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_INDICATE)) | 219 if (HasProperty(api_properties, apibtle::CHARACTERISTIC_PROPERTY_INDICATE)) { |
| 208 properties |= device::BluetoothGattCharacteristic::PROPERTY_INDICATE; | 220 properties |= device::BluetoothGattCharacteristic::PROPERTY_INDICATE; |
| 221 } |
| 209 | 222 |
| 210 if (HasProperty(api_properties, | 223 if (HasProperty(api_properties, |
| 211 apibtle::CHARACTERISTIC_PROPERTY_AUTHENTICATEDSIGNEDWRITES)) | 224 apibtle::CHARACTERISTIC_PROPERTY_AUTHENTICATEDSIGNEDWRITES)) { |
| 212 properties |= device::BluetoothGattCharacteristic:: | 225 properties |= device::BluetoothGattCharacteristic:: |
| 213 PROPERTY_AUTHENTICATED_SIGNED_WRITES; | 226 PROPERTY_AUTHENTICATED_SIGNED_WRITES; |
| 227 } |
| 228 |
| 214 if (HasProperty(api_properties, | 229 if (HasProperty(api_properties, |
| 215 apibtle::CHARACTERISTIC_PROPERTY_EXTENDEDPROPERTIES)) | 230 apibtle::CHARACTERISTIC_PROPERTY_EXTENDEDPROPERTIES)) { |
| 216 properties |= | 231 properties |= |
| 217 device::BluetoothGattCharacteristic::PROPERTY_EXTENDED_PROPERTIES; | 232 device::BluetoothGattCharacteristic::PROPERTY_EXTENDED_PROPERTIES; |
| 233 } |
| 234 |
| 218 if (HasProperty(api_properties, | 235 if (HasProperty(api_properties, |
| 219 apibtle::CHARACTERISTIC_PROPERTY_RELIABLEWRITE)) | 236 apibtle::CHARACTERISTIC_PROPERTY_RELIABLEWRITE)) { |
| 220 properties |= device::BluetoothGattCharacteristic::PROPERTY_RELIABLE_WRITE; | 237 properties |= device::BluetoothGattCharacteristic::PROPERTY_RELIABLE_WRITE; |
| 238 } |
| 239 |
| 221 if (HasProperty(api_properties, | 240 if (HasProperty(api_properties, |
| 222 apibtle::CHARACTERISTIC_PROPERTY_WRITABLEAUXILIARIES)) | 241 apibtle::CHARACTERISTIC_PROPERTY_WRITABLEAUXILIARIES)) { |
| 223 properties |= | 242 properties |= |
| 224 device::BluetoothGattCharacteristic::PROPERTY_WRITABLE_AUXILIARIES; | 243 device::BluetoothGattCharacteristic::PROPERTY_WRITABLE_AUXILIARIES; |
| 244 } |
| 245 |
| 246 if (HasProperty(api_properties, |
| 247 apibtle::CHARACTERISTIC_PROPERTY_ENCRYPTREAD)) { |
| 248 properties |= device::BluetoothGattCharacteristic::PROPERTY_READ_ENCRYPTED; |
| 249 } |
| 250 |
| 251 if (HasProperty(api_properties, |
| 252 apibtle::CHARACTERISTIC_PROPERTY_ENCRYPTWRITE)) { |
| 253 properties |= device::BluetoothGattCharacteristic::PROPERTY_WRITE_ENCRYPTED; |
| 254 } |
| 255 |
| 256 if (HasProperty(api_properties, |
| 257 apibtle::CHARACTERISTIC_PROPERTY_ENCRYPTAUTHENTICATEDREAD)) { |
| 258 properties |= device::BluetoothGattCharacteristic:: |
| 259 PROPERTY_READ_ENCRYPTED_AUTHENTICATED; |
| 260 } |
| 261 |
| 262 if (HasProperty(api_properties, |
| 263 apibtle::CHARACTERISTIC_PROPERTY_ENCRYPTAUTHENTICATEDWRITE)) { |
| 264 properties |= device::BluetoothGattCharacteristic:: |
| 265 PROPERTY_WRITE_ENCRYPTED_AUTHENTICATED; |
| 266 } |
| 225 | 267 |
| 226 return properties; | 268 return properties; |
| 227 } | 269 } |
| 228 | 270 |
| 271 device::BluetoothGattCharacteristic::Permissions GetBluetoothPermissions( |
| 272 const std::vector<apibtle::DescriptorPermission>& api_permissions) { |
| 273 device::BluetoothGattCharacteristic::Permissions permissions = |
| 274 device::BluetoothGattCharacteristic::PERMISSION_NONE; |
| 275 |
| 276 static_assert( |
| 277 apibtle::DESCRIPTOR_PERMISSION_LAST == 6, |
| 278 "Update required if the number of descriptor permissions changes."); |
| 279 |
| 280 if (HasPermission(api_permissions, apibtle::DESCRIPTOR_PERMISSION_READ)) { |
| 281 permissions |= device::BluetoothGattCharacteristic::PERMISSION_READ; |
| 282 } |
| 283 |
| 284 if (HasPermission(api_permissions, apibtle::DESCRIPTOR_PERMISSION_WRITE)) { |
| 285 permissions |= device::BluetoothGattCharacteristic::PERMISSION_WRITE; |
| 286 } |
| 287 |
| 288 if (HasPermission(api_permissions, |
| 289 apibtle::DESCRIPTOR_PERMISSION_ENCRYPTEDREAD)) { |
| 290 permissions |= |
| 291 device::BluetoothGattCharacteristic::PERMISSION_READ_ENCRYPTED; |
| 292 } |
| 293 |
| 294 if (HasPermission(api_permissions, |
| 295 apibtle::DESCRIPTOR_PERMISSION_ENCRYPTEDWRITE)) { |
| 296 permissions |= |
| 297 device::BluetoothGattCharacteristic::PERMISSION_WRITE_ENCRYPTED; |
| 298 } |
| 299 |
| 300 if (HasPermission( |
| 301 api_permissions, |
| 302 apibtle::DESCRIPTOR_PERMISSION_ENCRYPTEDAUTHENTICATEDREAD)) { |
| 303 permissions |= device::BluetoothGattCharacteristic:: |
| 304 PERMISSION_READ_ENCRYPTED_AUTHENTICATED; |
| 305 } |
| 306 |
| 307 if (HasPermission( |
| 308 api_permissions, |
| 309 apibtle::DESCRIPTOR_PERMISSION_ENCRYPTEDAUTHENTICATEDWRITE)) { |
| 310 permissions |= device::BluetoothGattCharacteristic:: |
| 311 PERMISSION_WRITE_ENCRYPTED_AUTHENTICATED; |
| 312 } |
| 313 |
| 314 return permissions; |
| 315 } |
| 316 |
| 229 } // namespace | 317 } // namespace |
| 230 | 318 |
| 231 | 319 |
| 232 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI> > | 320 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI> > |
| 233 g_factory = LAZY_INSTANCE_INITIALIZER; | 321 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 234 | 322 |
| 235 // static | 323 // static |
| 236 BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>* | 324 BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>* |
| 237 BluetoothLowEnergyAPI::GetFactoryInstance() { | 325 BluetoothLowEnergyAPI::GetFactoryInstance() { |
| 238 return g_factory.Pointer(); | 326 return g_factory.Pointer(); |
| (...skipping 1018 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1257 device::BluetoothLocalGattCharacteristic* characteristic = | 1345 device::BluetoothLocalGattCharacteristic* characteristic = |
| 1258 event_router_->GetLocalCharacteristic(params_->characteristic_id); | 1346 event_router_->GetLocalCharacteristic(params_->characteristic_id); |
| 1259 if (!characteristic) { | 1347 if (!characteristic) { |
| 1260 Respond(Error(kErrorInvalidCharacteristicId)); | 1348 Respond(Error(kErrorInvalidCharacteristicId)); |
| 1261 return; | 1349 return; |
| 1262 } | 1350 } |
| 1263 | 1351 |
| 1264 base::WeakPtr<device::BluetoothLocalGattDescriptor> descriptor = | 1352 base::WeakPtr<device::BluetoothLocalGattDescriptor> descriptor = |
| 1265 device::BluetoothLocalGattDescriptor::Create( | 1353 device::BluetoothLocalGattDescriptor::Create( |
| 1266 device::BluetoothUUID(params_->descriptor.uuid), | 1354 device::BluetoothUUID(params_->descriptor.uuid), |
| 1267 device::BluetoothGattCharacteristic::Permissions(), characteristic); | 1355 GetBluetoothPermissions(params_->descriptor.permissions), |
| 1356 characteristic); |
| 1268 | 1357 |
| 1269 Respond(ArgumentList( | 1358 Respond(ArgumentList( |
| 1270 apibtle::CreateDescriptor::Results::Create(descriptor->GetIdentifier()))); | 1359 apibtle::CreateDescriptor::Results::Create(descriptor->GetIdentifier()))); |
| 1271 } | 1360 } |
| 1272 | 1361 |
| 1273 // registerService: | 1362 // registerService: |
| 1274 | 1363 |
| 1275 template class BLEPeripheralExtensionFunction<apibtle::RegisterService::Params>; | 1364 template class BLEPeripheralExtensionFunction<apibtle::RegisterService::Params>; |
| 1276 | 1365 |
| 1277 void BluetoothLowEnergyRegisterServiceFunction::DoWork() { | 1366 void BluetoothLowEnergyRegisterServiceFunction::DoWork() { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1322 | 1411 |
| 1323 void BluetoothLowEnergyNotifyCharacteristicValueChangedFunction::DoWork() { | 1412 void BluetoothLowEnergyNotifyCharacteristicValueChangedFunction::DoWork() { |
| 1324 Respond(Error(kErrorPermissionDenied)); | 1413 Respond(Error(kErrorPermissionDenied)); |
| 1325 } | 1414 } |
| 1326 | 1415 |
| 1327 // removeService: | 1416 // removeService: |
| 1328 | 1417 |
| 1329 template class BLEPeripheralExtensionFunction<apibtle::RemoveService::Params>; | 1418 template class BLEPeripheralExtensionFunction<apibtle::RemoveService::Params>; |
| 1330 | 1419 |
| 1331 void BluetoothLowEnergyRemoveServiceFunction::DoWork() { | 1420 void BluetoothLowEnergyRemoveServiceFunction::DoWork() { |
| 1332 Respond(Error(kErrorPermissionDenied)); | 1421 device::BluetoothLocalGattService* service = |
| 1422 event_router_->adapter()->GetGattService(params_->service_id); |
| 1423 if (!service) { |
| 1424 Respond(Error(kErrorInvalidServiceId)); |
| 1425 return; |
| 1426 } |
| 1427 event_router_->RemoveServiceFromApp(extension_id(), service->GetIdentifier()); |
| 1428 service->Delete(); |
| 1429 Respond(NoArguments()); |
| 1333 } | 1430 } |
| 1334 | 1431 |
| 1335 // sendRequestResponse: | 1432 // sendRequestResponse: |
| 1336 | 1433 |
| 1337 template class BLEPeripheralExtensionFunction< | 1434 template class BLEPeripheralExtensionFunction< |
| 1338 apibtle::SendRequestResponse::Params>; | 1435 apibtle::SendRequestResponse::Params>; |
| 1339 | 1436 |
| 1340 void BluetoothLowEnergySendRequestResponseFunction::DoWork() { | 1437 void BluetoothLowEnergySendRequestResponseFunction::DoWork() { |
| 1341 std::vector<uint8_t> uint8_vector; | 1438 std::vector<uint8_t> uint8_vector; |
| 1342 if (params_->response.value) { | 1439 if (params_->response.value) { |
| 1343 uint8_vector.assign(params_->response.value->begin(), | 1440 uint8_vector.assign(params_->response.value->begin(), |
| 1344 params_->response.value->end()); | 1441 params_->response.value->end()); |
| 1345 } | 1442 } |
| 1346 event_router_->HandleRequestResponse( | 1443 event_router_->HandleRequestResponse( |
| 1347 extension(), params_->response.request_id, params_->response.is_error, | 1444 extension(), params_->response.request_id, params_->response.is_error, |
| 1348 uint8_vector); | 1445 uint8_vector); |
| 1349 Respond(NoArguments()); | 1446 Respond(NoArguments()); |
| 1350 } | 1447 } |
| 1351 | 1448 |
| 1352 } // namespace api | 1449 } // namespace api |
| 1353 } // namespace extensions | 1450 } // namespace extensions |
| OLD | NEW |