Index: third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTService.cpp |
diff --git a/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTService.cpp b/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTService.cpp |
index 4292557b483d1fac71d76c23d1fa9e13ba21d9d6..02e3908e7989382734cc2ad971a8dfa98ad980ec 100644 |
--- a/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTService.cpp |
+++ b/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTService.cpp |
@@ -30,25 +30,51 @@ BluetoothRemoteGATTService* BluetoothRemoteGATTService::take(ScriptPromiseResolv |
return new BluetoothRemoteGATTService(std::move(webService)); |
} |
-ScriptPromise BluetoothRemoteGATTService::getCharacteristic(ScriptState* scriptState, |
- const StringOrUnsignedLong& characteristic, ExceptionState& exceptionState) |
-{ |
- WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptState); |
+// Class that allows us to resolve the promise with a single Characteristic or |
+// with a vector owning the characteristics. |
+class GetCharacteristicsCallback : public WebBluetoothGetCharacteristicsCallbacks { |
+public: |
+ GetCharacteristicsCallback(mojom::WebBluetoothGATTQueryQuantity quantity, ScriptPromiseResolver* resolver) |
+ : m_resolver(resolver) |
+ , m_quantity(quantity) {} |
- String characteristicUUID = BluetoothUUID::getCharacteristic(characteristic, exceptionState); |
- if (exceptionState.hadException()) |
- return exceptionState.reject(scriptState); |
+ void onSuccess(const WebVector<WebBluetoothRemoteGATTCharacteristicInit*>& webCharacteristics) override |
+ { |
+ if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
+ return; |
- ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
- ScriptPromise promise = resolver->promise(); |
- webbluetooth->getCharacteristic(m_webService->serviceInstanceID, characteristicUUID, new CallbackPromiseAdapter<BluetoothRemoteGATTCharacteristic, BluetoothError>(resolver)); |
+ if (m_quantity == mojom::WebBluetoothGATTQueryQuantity::SINGLE) { |
+ DCHECK_EQ(1u, webCharacteristics.size()); |
+ m_resolver->resolve(BluetoothRemoteGATTCharacteristic::take(m_resolver, adoptPtr(webCharacteristics[0]))); |
+ return; |
+ } |
- return promise; |
-} |
+ HeapVector<Member<BluetoothRemoteGATTCharacteristic>> characteristics; |
+ characteristics.reserveInitialCapacity(webCharacteristics.size()); |
+ for (WebBluetoothRemoteGATTCharacteristicInit* webCharacteristic : webCharacteristics) { |
+ characteristics.append(BluetoothRemoteGATTCharacteristic::take(m_resolver, adoptPtr(webCharacteristic))); |
+ } |
+ m_resolver->resolve(characteristics); |
+ } |
-ScriptPromise BluetoothRemoteGATTService::getCharacteristics(ScriptState* scriptState, ExceptionState&) |
+ void onError(const WebBluetoothError& e) override |
+ { |
+ if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
+ return; |
+ m_resolver->reject(BluetoothError::take(m_resolver, e)); |
+ } |
+private: |
+ Persistent<ScriptPromiseResolver> m_resolver; |
+ mojom::WebBluetoothGATTQueryQuantity m_quantity; |
+}; |
+ |
+ScriptPromise BluetoothRemoteGATTService::getCharacteristic(ScriptState* scriptState, const StringOrUnsignedLong& characteristic, ExceptionState& exceptionState) |
{ |
- return getCharacteristicsImpl(scriptState, String()); |
+ String characteristicUUID = BluetoothUUID::getCharacteristic(characteristic, exceptionState); |
+ if (exceptionState.hadException()) |
+ return exceptionState.reject(scriptState); |
+ |
+ return getCharacteristicsImpl(scriptState, mojom::WebBluetoothGATTQueryQuantity::SINGLE, characteristicUUID); |
} |
ScriptPromise BluetoothRemoteGATTService::getCharacteristics(ScriptState* scriptState, const StringOrUnsignedLong& characteristic, ExceptionState& exceptionState) |
@@ -57,33 +83,21 @@ ScriptPromise BluetoothRemoteGATTService::getCharacteristics(ScriptState* script |
if (exceptionState.hadException()) |
return exceptionState.reject(scriptState); |
- return getCharacteristicsImpl(scriptState, characteristicUUID); |
+ return getCharacteristicsImpl(scriptState, mojom::WebBluetoothGATTQueryQuantity::MULTIPLE, characteristicUUID); |
} |
-// Class that allows us to use CallbackPromiseAdapter to resolve a promise with a |
-// vector owning BluetoothRemoteGATTCharacteristics. |
-class RemoteCharacteristicArray { |
- STATIC_ONLY(RemoteCharacteristicArray); |
-public: |
- using WebType = OwnPtr<WebVector<WebBluetoothRemoteGATTCharacteristicInit*>>; |
- static HeapVector<Member<BluetoothRemoteGATTCharacteristic>> take(ScriptPromiseResolver* resolver, PassOwnPtr<WebVector<WebBluetoothRemoteGATTCharacteristicInit*>> webCharacteristics) |
- { |
- HeapVector<Member<BluetoothRemoteGATTCharacteristic>> characteristics; |
- characteristics.reserveInitialCapacity(webCharacteristics->size()); |
- for (WebBluetoothRemoteGATTCharacteristicInit* webCharacteristic : *webCharacteristics) { |
- characteristics.append(BluetoothRemoteGATTCharacteristic::take(resolver, adoptPtr(webCharacteristic))); |
- } |
- return characteristics; |
- } |
-}; |
+ScriptPromise BluetoothRemoteGATTService::getCharacteristics(ScriptState* scriptState, ExceptionState&) |
+{ |
+ return getCharacteristicsImpl(scriptState, mojom::WebBluetoothGATTQueryQuantity::MULTIPLE); |
+} |
-ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl(ScriptState* scriptState, String characteristicsUUID) |
+ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl(ScriptState* scriptState, mojom::WebBluetoothGATTQueryQuantity quantity, String characteristicsUUID) |
{ |
ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
ScriptPromise promise = resolver->promise(); |
WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptState); |
- webbluetooth->getCharacteristics(m_webService->serviceInstanceID, characteristicsUUID, new CallbackPromiseAdapter<RemoteCharacteristicArray, BluetoothError>(resolver)); |
+ webbluetooth->getCharacteristics(m_webService->serviceInstanceID, quantity, characteristicsUUID, new GetCharacteristicsCallback(quantity, resolver)); |
return promise; |
} |