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

Unified Diff: third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTCharacteristic.cpp

Issue 2466223002: Implement WebBluetooth getDescriptor[s] (Closed)
Patch Set: Rebase Created 4 years 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: third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTCharacteristic.cpp
diff --git a/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTCharacteristic.cpp b/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTCharacteristic.cpp
index d55e5e678faadcf9c756acef6b4d47e1ea467605..92fc2ea76dc94839e37cc498c70f589f9fb63e29 100644
--- a/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTCharacteristic.cpp
+++ b/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTCharacteristic.cpp
@@ -13,8 +13,10 @@
#include "core/inspector/ConsoleMessage.h"
#include "modules/bluetooth/BluetoothCharacteristicProperties.h"
#include "modules/bluetooth/BluetoothError.h"
+#include "modules/bluetooth/BluetoothRemoteGATTDescriptor.h"
#include "modules/bluetooth/BluetoothRemoteGATTService.h"
#include "modules/bluetooth/BluetoothSupplement.h"
+#include "modules/bluetooth/BluetoothUUID.h"
#include "public/platform/modules/bluetooth/WebBluetooth.h"
#include <memory>
@@ -120,10 +122,160 @@ void BluetoothRemoteGATTCharacteristic::addedEventListener(
}
}
-class ReadValueCallback : public WebBluetoothReadValueCallbacks {
+class GetDescriptorsCallback : public WebBluetoothGetDescriptorsCallbacks {
public:
- ReadValueCallback(BluetoothRemoteGATTCharacteristic* characteristic,
- ScriptPromiseResolver* resolver)
+ GetDescriptorsCallback(BluetoothRemoteGATTCharacteristic* characteristic,
+ mojom::blink::WebBluetoothGATTQueryQuantity quantity,
+ ScriptPromiseResolver* resolver)
+ : m_characteristic(characteristic),
+ m_quantity(quantity),
+ m_resolver(resolver) {
+ // We always check that the device is connected before constructing this
+ // object.
+ CHECK(m_characteristic->gatt()->connected());
+ m_characteristic->gatt()->AddToActiveAlgorithms(m_resolver.get());
+ }
+
+ void onSuccess(const WebVector<WebBluetoothRemoteGATTDescriptorInit*>&
+ webDescriptors) override {
+ if (!m_resolver->getExecutionContext() ||
+ m_resolver->getExecutionContext()->activeDOMObjectsAreStopped())
+ return;
+
+ // If the resolver is not in the set of ActiveAlgorithms then the frame
+ // disconnected so we reject.
+ if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms(
+ m_resolver.get())) {
+ m_resolver->reject(
+ DOMException::create(NetworkError, kGATTServerDisconnected));
ortuno 2016/12/02 06:14:51 I like that we can test all of the characteristic'
dougt 2016/12/02 18:31:29 If anything, a possible spec bug? I am not really
ortuno 2016/12/07 08:05:12 hmm then we should change the message since this i
ortuno 2016/12/08 05:42:51 As discussed offline, I think it's fine to use the
+ return;
+ }
+
+ if (m_quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) {
+ DCHECK_EQ(1u, webDescriptors.size());
+ m_resolver->resolve(
+ m_characteristic->gatt()
+ ->device()
+ ->getOrCreateBluetoothRemoteGATTDescriptor(
+ wrapUnique(webDescriptors[0]), m_characteristic));
+ return;
+ }
+
+ HeapVector<Member<BluetoothRemoteGATTDescriptor>> descriptors;
+ descriptors.reserveInitialCapacity(webDescriptors.size());
+ for (WebBluetoothRemoteGATTDescriptorInit* webDescriptor : webDescriptors) {
+ descriptors.append(m_characteristic->gatt()
+ ->device()
+ ->getOrCreateBluetoothRemoteGATTDescriptor(
+ wrapUnique(webDescriptor), m_characteristic));
+ }
+ m_resolver->resolve(descriptors);
+ }
+
+ void onError(
+ int32_t
+ error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */)
+ override {
+ if (!m_resolver->getExecutionContext() ||
+ m_resolver->getExecutionContext()->activeDOMObjectsAreStopped())
+ return;
+
+ // If the resolver is not in the set of ActiveAlgorithms then the frame
+ // disconnected so we reject.
+ if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms(
+ m_resolver.get())) {
+ m_resolver->reject(
+ DOMException::create(NetworkError, kGATTServerDisconnected));
+ return;
+ }
+
+ m_resolver->reject(BluetoothError::take(m_resolver, error));
+ }
+
+ private:
+ Persistent<BluetoothRemoteGATTCharacteristic> m_characteristic;
+ mojom::blink::WebBluetoothGATTQueryQuantity m_quantity;
+ const Persistent<ScriptPromiseResolver> m_resolver;
+};
+
+ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptorsImpl(
ortuno 2016/12/02 06:14:51 nit: Order of declaration should match order of im
dougt 2016/12/02 18:31:29 Done.
+ ScriptState* scriptState,
+ mojom::blink::WebBluetoothGATTQueryQuantity quantity,
+ const String& descriptor) {
+#if OS(MACOSX)
+ // TODO(ortuno): Remove when getDescriptor[s] is implemented.
ortuno 2016/12/02 06:14:51 nit: TODO(crbug.com/624017): Remove when descripto
dougt 2016/12/02 18:31:29 Done.
+ return ScriptPromise::rejectWithDOMException(
+ scriptState, DOMException::create(NotSupportedError,
+ "getDescriptor[s] is not implemented "
+ "yet. See https://goo.gl/J6ASzs"));
+#endif // OS(MACOSX)
+ WebBluetooth* webbluetooth =
+ BluetoothSupplement::fromScriptState(scriptState);
+
+ if (!gatt()->connected()) {
+ return ScriptPromise::rejectWithDOMException(
+ scriptState,
+ DOMException::create(NetworkError, kGATTServerNotConnected));
+ }
+
+ if (!gatt()->device()->isValidCharacteristic(
+ m_webCharacteristic->characteristicInstanceID)) {
+ return ScriptPromise::rejectWithDOMException(
+ scriptState,
+ DOMException::create(InvalidStateError, kInvalidCharacteristic));
+ }
+
+ ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
+ ScriptPromise promise = resolver->promise();
+
+ webbluetooth->getDescriptors(
+ m_webCharacteristic->characteristicInstanceID,
+ static_cast<int32_t>(quantity), descriptor,
+ new GetDescriptorsCallback(this, quantity, resolver));
+
+ return promise;
+}
+
+ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptor(
+ ScriptState* scriptState,
+ const StringOrUnsignedLong& descriptorUUID,
+ ExceptionState& exceptionState) {
+ String descriptor =
+ BluetoothUUID::getDescriptor(descriptorUUID, exceptionState);
+ if (exceptionState.hadException())
+ return exceptionState.reject(scriptState);
+
+ return getDescriptorsImpl(scriptState,
+ mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE,
+ descriptor);
+}
+
+ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptors(
+ ScriptState* scriptState,
+ ExceptionState&) {
+ return getDescriptorsImpl(
+ scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE);
+}
+
+ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptors(
+ ScriptState* scriptState,
+ const StringOrUnsignedLong& descriptorUUID,
+ ExceptionState& exceptionState) {
+ String descriptor =
+ BluetoothUUID::getDescriptor(descriptorUUID, exceptionState);
+ if (exceptionState.hadException())
+ return exceptionState.reject(scriptState);
+
+ return getDescriptorsImpl(
+ scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE,
+ descriptor);
+}
+
+class CharacteristicReadValueCallback : public WebBluetoothReadValueCallbacks {
+ public:
+ CharacteristicReadValueCallback(
+ BluetoothRemoteGATTCharacteristic* characteristic,
+ ScriptPromiseResolver* resolver)
: m_characteristic(characteristic), m_resolver(resolver) {
// We always check that the device is connected before constructing this
// object.
@@ -193,16 +345,19 @@ ScriptPromise BluetoothRemoteGATTCharacteristic::readValue(
ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
ScriptPromise promise = resolver->promise();
- webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID,
- new ReadValueCallback(this, resolver));
+ webbluetooth->characteristicReadValue(
+ m_webCharacteristic->characteristicInstanceID,
+ new CharacteristicReadValueCallback(this, resolver));
return promise;
}
-class WriteValueCallback : public WebBluetoothWriteValueCallbacks {
+class CharacteristicWriteValueCallback
+ : public WebBluetoothWriteValueCallbacks {
public:
- WriteValueCallback(BluetoothRemoteGATTCharacteristic* characteristic,
- ScriptPromiseResolver* resolver)
+ CharacteristicWriteValueCallback(
+ BluetoothRemoteGATTCharacteristic* characteristic,
+ ScriptPromiseResolver* resolver)
: m_characteristic(characteristic), m_resolver(resolver) {
// We always check that the device is connected before constructing this
// object.
@@ -235,7 +390,6 @@ class WriteValueCallback : public WebBluetoothWriteValueCallbacks {
if (!m_resolver->getExecutionContext() ||
m_resolver->getExecutionContext()->activeDOMObjectsAreStopped())
return;
-
if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms(
m_resolver.get())) {
m_resolver->reject(
@@ -286,8 +440,9 @@ ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue(
ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
ScriptPromise promise = resolver->promise();
- webbluetooth->writeValue(m_webCharacteristic->characteristicInstanceID,
- valueVector, new WriteValueCallback(this, resolver));
+ webbluetooth->characteristicWriteValue(
+ m_webCharacteristic->characteristicInstanceID, valueVector,
+ new CharacteristicWriteValueCallback(this, resolver));
return promise;
}

Powered by Google App Engine
This is Rietveld 408576698