Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "modules/bluetooth/BluetoothRemoteGATTCharacteristic.h" | 5 #include "modules/bluetooth/BluetoothRemoteGATTCharacteristic.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromise.h" | 7 #include "bindings/core/v8/ScriptPromise.h" |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 9 #include "core/dom/DOMDataView.h" | 9 #include "core/dom/DOMDataView.h" |
| 10 #include "core/dom/DOMException.h" | 10 #include "core/dom/DOMException.h" |
| 11 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
| 12 #include "core/events/Event.h" | 12 #include "core/events/Event.h" |
| 13 #include "core/inspector/ConsoleMessage.h" | 13 #include "core/inspector/ConsoleMessage.h" |
| 14 #include "modules/bluetooth/Bluetooth.h" | 14 #include "modules/bluetooth/Bluetooth.h" |
| 15 #include "modules/bluetooth/BluetoothCharacteristicProperties.h" | 15 #include "modules/bluetooth/BluetoothCharacteristicProperties.h" |
| 16 #include "modules/bluetooth/BluetoothDevice.h" | 16 #include "modules/bluetooth/BluetoothDevice.h" |
| 17 #include "modules/bluetooth/BluetoothError.h" | 17 #include "modules/bluetooth/BluetoothError.h" |
| 18 #include "modules/bluetooth/BluetoothRemoteGATTDescriptor.h" | |
| 18 #include "modules/bluetooth/BluetoothRemoteGATTService.h" | 19 #include "modules/bluetooth/BluetoothRemoteGATTService.h" |
| 20 #include "modules/bluetooth/BluetoothUUID.h" | |
| 21 #include <memory> | |
| 19 #include <utility> | 22 #include <utility> |
| 20 | 23 |
| 21 namespace blink { | 24 namespace blink { |
| 22 | 25 |
| 23 namespace { | 26 namespace { |
| 24 | 27 |
| 25 const char kGATTServerDisconnected[] = | 28 const char kGATTServerDisconnected[] = |
| 26 "GATT Server disconnected while performing a GATT operation."; | 29 "GATT Server disconnected while performing a GATT operation."; |
| 27 const char kGATTServerNotConnected[] = | 30 const char kGATTServerNotConnected[] = |
| 28 "GATT Server is disconnected. Cannot perform GATT operations."; | 31 "GATT Server is disconnected. Cannot perform GATT operations."; |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 308 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 311 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
| 309 service->RemoteCharacteristicStopNotifications( | 312 service->RemoteCharacteristicStopNotifications( |
| 310 m_characteristic->instance_id, | 313 m_characteristic->instance_id, |
| 311 convertToBaseCallback( | 314 convertToBaseCallback( |
| 312 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, | 315 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, |
| 313 wrapPersistent(this), wrapPersistent(resolver), | 316 wrapPersistent(this), wrapPersistent(resolver), |
| 314 mojom::blink::WebBluetoothResult::SUCCESS))); | 317 mojom::blink::WebBluetoothResult::SUCCESS))); |
| 315 return promise; | 318 return promise; |
| 316 } | 319 } |
| 317 | 320 |
| 321 ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptor( | |
| 322 ScriptState* scriptState, | |
| 323 const StringOrUnsignedLong& descriptorUUID, | |
| 324 ExceptionState& exceptionState) { | |
| 325 String descriptor = | |
| 326 BluetoothUUID::getDescriptor(descriptorUUID, exceptionState); | |
| 327 if (exceptionState.hadException()) | |
| 328 return exceptionState.reject(scriptState); | |
| 329 | |
| 330 return getDescriptorsImpl(scriptState, | |
| 331 mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE, | |
| 332 descriptor); | |
| 333 } | |
| 334 | |
| 335 ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptors( | |
| 336 ScriptState* scriptState, | |
| 337 ExceptionState&) { | |
| 338 return getDescriptorsImpl( | |
| 339 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE); | |
| 340 } | |
| 341 | |
| 342 ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptors( | |
| 343 ScriptState* scriptState, | |
| 344 const StringOrUnsignedLong& descriptorUUID, | |
| 345 ExceptionState& exceptionState) { | |
| 346 String descriptor = | |
| 347 BluetoothUUID::getDescriptor(descriptorUUID, exceptionState); | |
| 348 if (exceptionState.hadException()) | |
| 349 return exceptionState.reject(scriptState); | |
| 350 | |
| 351 return getDescriptorsImpl( | |
| 352 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE, | |
| 353 descriptor); | |
| 354 } | |
| 355 | |
| 356 ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptorsImpl( | |
| 357 ScriptState* scriptState, | |
| 358 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | |
| 359 const String& descriptor) { | |
| 360 if (!gatt()->connected()) { | |
| 361 return ScriptPromise::rejectWithDOMException( | |
| 362 scriptState, | |
| 363 DOMException::create(NetworkError, kGATTServerNotConnected)); | |
| 364 } | |
| 365 | |
| 366 if (!gatt()->device()->isValidCharacteristic(m_characteristic->instance_id)) { | |
| 367 return ScriptPromise::rejectWithDOMException( | |
| 368 scriptState, | |
| 369 DOMException::create(InvalidStateError, kInvalidCharacteristic)); | |
| 370 } | |
| 371 | |
| 372 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | |
| 373 ScriptPromise promise = resolver->promise(); | |
| 374 gatt()->AddToActiveAlgorithms(resolver); | |
| 375 | |
| 376 auto service = m_device->bluetooth()->service(); | |
|
dcheng
2017/01/13 09:28:42
As the type of this expression is unclear, let's w
dougt
2017/01/13 20:08:03
Done.
| |
| 377 WTF::Optional<String> uuid = WTF::nullopt; | |
|
dcheng
2017/01/13 09:28:42
Two nits:
1. Optional's default ctor is to constru
dougt
2017/01/13 20:08:03
I am going to fix this in a follow up. This exist
| |
| 378 if (!descriptor.isEmpty()) | |
| 379 uuid = descriptor; | |
| 380 service->RemoteCharacteristicGetDescriptors( | |
| 381 m_characteristic->instance_id, quantity, uuid, | |
| 382 convertToBaseCallback( | |
| 383 WTF::bind(&BluetoothRemoteGATTCharacteristic::GetDescriptorsCallback, | |
| 384 wrapPersistent(this), m_characteristic->instance_id, | |
| 385 quantity, wrapPersistent(resolver)))); | |
| 386 | |
| 387 return promise; | |
| 388 } | |
| 389 | |
| 390 // Callback that allows us to resolve the promise with a single descriptor | |
| 391 // or with a vector owning the descriptors. | |
| 392 void BluetoothRemoteGATTCharacteristic::GetDescriptorsCallback( | |
| 393 const String& characteristicInstanceId, | |
| 394 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | |
| 395 ScriptPromiseResolver* resolver, | |
| 396 mojom::blink::WebBluetoothResult result, | |
| 397 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTDescriptorPtr>> | |
| 398 descriptors) { | |
| 399 if (!resolver->getExecutionContext() || | |
| 400 resolver->getExecutionContext()->isContextDestroyed()) | |
| 401 return; | |
| 402 | |
| 403 // If the resolver is not in the set of ActiveAlgorithms then the frame | |
| 404 // disconnected so we reject. | |
| 405 if (!service()->device()->gatt()->RemoveFromActiveAlgorithms(resolver)) { | |
| 406 resolver->reject( | |
| 407 DOMException::create(NetworkError, kGATTServerDisconnected)); | |
| 408 return; | |
| 409 } | |
| 410 | |
| 411 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | |
| 412 DCHECK(descriptors); | |
| 413 | |
| 414 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { | |
| 415 DCHECK_EQ(1u, descriptors->size()); | |
| 416 resolver->resolve( | |
| 417 service()->device()->getOrCreateBluetoothRemoteGATTDescriptor( | |
| 418 std::move(descriptors.value()[0]), this)); | |
| 419 return; | |
| 420 } | |
| 421 | |
| 422 HeapVector<Member<BluetoothRemoteGATTDescriptor>> gattDescriptors; | |
| 423 gattDescriptors.reserveInitialCapacity(descriptors->size()); | |
| 424 for (auto& descriptor : descriptors.value()) { | |
| 425 gattDescriptors.push_back( | |
| 426 service()->device()->getOrCreateBluetoothRemoteGATTDescriptor( | |
| 427 std::move(descriptor), this)); | |
| 428 } | |
| 429 resolver->resolve(gattDescriptors); | |
| 430 } else { | |
| 431 resolver->reject(BluetoothError::take(resolver, result)); | |
| 432 } | |
| 433 } | |
| 434 | |
| 318 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { | 435 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { |
| 319 visitor->trace(m_service); | 436 visitor->trace(m_service); |
| 320 visitor->trace(m_properties); | 437 visitor->trace(m_properties); |
| 321 visitor->trace(m_value); | 438 visitor->trace(m_value); |
| 322 visitor->trace(m_device); | 439 visitor->trace(m_device); |
| 323 EventTargetWithInlineData::trace(visitor); | 440 EventTargetWithInlineData::trace(visitor); |
| 324 ContextLifecycleObserver::trace(visitor); | 441 ContextLifecycleObserver::trace(visitor); |
| 325 } | 442 } |
| 326 | 443 |
| 327 } // namespace blink | 444 } // namespace blink |
| OLD | NEW |