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

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

Issue 2466223002: Implement WebBluetooth getDescriptor[s] (Closed)
Patch Set: Ensure that we throw a kGattServerNotConnected error if getDescriptor[s] is called while not connec… Created 4 years, 1 month 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 1177d433d2343380f65a46325157c8119712f8ab..d12676128742157d7085fe913091aff6958bc024 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>
@@ -117,6 +119,137 @@ void BluetoothRemoteGATTCharacteristic::addedEventListener(
}
}
+// TODO dft: maybe we can share with GetCharacteristicsCallback
ortuno 2016/11/21 03:34:09 TODO(crbug.com/[new bug number]): Make class that
dougt 2016/11/22 01:47:16 Onion Soup for the win. I'll remove my comment fo
+// Class that allows us to resolve the promise with a single Descriptor or
+// with a vector owning the descriptors.
+class GetDescriptorsCallback : public WebBluetoothGetDescriptorsCallbacks {
+ public:
+ GetDescriptorsCallback(BluetoothRemoteGATTService* service,
ortuno 2016/11/21 03:34:09 I think you forgot to change this to BluetoothRemo
dougt 2016/11/22 01:47:16 Acknowledged.
+ mojom::blink::WebBluetoothGATTQueryQuantity quantity,
+ ScriptPromiseResolver* resolver)
+ : m_service(service), m_quantity(quantity), m_resolver(resolver) {
+ // We always check that the device is connected before constructing this
+ // object.
+ CHECK(m_service->device()->gatt()->connected());
+ m_service->device()->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_service->device()->gatt()->RemoveFromActiveAlgorithms(
+ m_resolver.get())) {
+ m_resolver->reject(
+ DOMException::create(NetworkError, kGATTServerDisconnected));
ortuno 2016/11/21 03:34:09 Add a new error for GetDescriptor(s) similar to Se
dougt 2016/11/22 01:47:16 For most of the calls on characteristics, you'll g
+ return;
+ }
+
+ if (m_quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) {
+ DCHECK_EQ(1u, webDescriptors.size());
+ m_resolver->resolve(BluetoothRemoteGATTDescriptor::take(
ortuno 2016/11/21 03:34:09 Add TODO for adding the descriptor to the map e.g.
dougt 2016/11/22 01:47:16 Acknowledged.
+ m_resolver, wrapUnique(webDescriptors[0]), m_service));
+ return;
+ }
+
+ HeapVector<Member<BluetoothRemoteGATTDescriptor>> descriptors;
+ descriptors.reserveInitialCapacity(webDescriptors.size());
+ for (WebBluetoothRemoteGATTDescriptorInit* webDescriptor : webDescriptors) {
+ descriptors.append(BluetoothRemoteGATTDescriptor::take(
ortuno 2016/11/21 03:34:09 Add same TODO here.
dougt 2016/11/22 01:47:16 Acknowledged.
+ m_resolver, wrapUnique(webDescriptor), m_service));
+ }
+ 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_service->device()->gatt()->RemoveFromActiveAlgorithms(
+ m_resolver.get())) {
+ m_resolver->reject(
+ DOMException::create(NetworkError, kGATTServerDisconnected));
+ return;
+ }
+
+ m_resolver->reject(BluetoothError::take(m_resolver, error));
+ }
+
+ private:
+ Persistent<BluetoothRemoteGATTService> m_service;
+ mojom::blink::WebBluetoothGATTQueryQuantity m_quantity;
+ const Persistent<ScriptPromiseResolver> m_resolver;
+};
+
+ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptorsImpl(
+ ScriptState* scriptState,
+ mojom::blink::WebBluetoothGATTQueryQuantity quantity,
+ const String& descriptor) {
+ WebBluetooth* webbluetooth =
+ BluetoothSupplement::fromScriptState(scriptState);
+
+ if (!gatt()->connected()) {
+ return ScriptPromise::rejectWithDOMException(
+ scriptState,
+ DOMException::create(NetworkError, kGATTServerNotConnected));
+ }
+
ortuno 2016/11/21 03:34:09 Also check that the characteristic is still valid
dougt 2016/11/22 01:47:16 Acknowledged.
+ ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
+ ScriptPromise promise = resolver->promise();
+
+ webbluetooth->getDescriptors(
+ m_webCharacteristic->characteristicInstanceID,
+ static_cast<int32_t>(quantity), descriptor,
+ new GetDescriptorsCallback(m_service, 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 ReadValueCallback : public WebBluetoothReadValueCallbacks {
public:
ReadValueCallback(BluetoothRemoteGATTCharacteristic* characteristic,
@@ -183,8 +316,9 @@ 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 ReadValueCallback(this, resolver));
return promise;
}
@@ -269,8 +403,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 WriteValueCallback(this, resolver));
return promise;
}

Powered by Google App Engine
This is Rietveld 408576698