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/BluetoothRemoteGATTService.h" | 5 #include "modules/bluetooth/BluetoothRemoteGATTService.h" |
6 | 6 |
7 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
8 #include "bindings/core/v8/ScriptPromise.h" | 8 #include "bindings/core/v8/ScriptPromise.h" |
9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
10 #include "core/dom/DOMException.h" | 10 #include "core/dom/DOMException.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 } | 23 } |
24 | 24 |
25 BluetoothRemoteGATTService* BluetoothRemoteGATTService::take(ScriptPromiseResolv
er*, PassOwnPtr<WebBluetoothRemoteGATTService> webService) | 25 BluetoothRemoteGATTService* BluetoothRemoteGATTService::take(ScriptPromiseResolv
er*, PassOwnPtr<WebBluetoothRemoteGATTService> webService) |
26 { | 26 { |
27 if (!webService) { | 27 if (!webService) { |
28 return nullptr; | 28 return nullptr; |
29 } | 29 } |
30 return new BluetoothRemoteGATTService(std::move(webService)); | 30 return new BluetoothRemoteGATTService(std::move(webService)); |
31 } | 31 } |
32 | 32 |
33 ScriptPromise BluetoothRemoteGATTService::getCharacteristic(ScriptState* scriptS
tate, | 33 // Class that allows us to resolve the promise with a single Characteristic or |
34 const StringOrUnsignedLong& characteristic, ExceptionState& exceptionState) | 34 // with a vector owning the characteristics. |
| 35 class GetCharacteristicsCallback : public WebBluetoothGetCharacteristicsCallback
s { |
| 36 public: |
| 37 GetCharacteristicsCallback(mojom::WebBluetoothQueryType type, ScriptPromiseR
esolver* resolver) |
| 38 : m_resolver(resolver) |
| 39 , m_type(type) {} |
| 40 |
| 41 void onSuccess(const WebVector<WebBluetoothRemoteGATTCharacteristicInit*>& w
ebCharacteristics) override |
| 42 { |
| 43 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
| 44 return; |
| 45 |
| 46 if (m_type == mojom::WebBluetoothQueryType::SINGLE) { |
| 47 DCHECK_EQ(1u, webCharacteristics.size()); |
| 48 m_resolver->resolve(BluetoothRemoteGATTCharacteristic::take(m_resolv
er, adoptPtr(webCharacteristics[0]))); |
| 49 return; |
| 50 } |
| 51 |
| 52 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> characteristics; |
| 53 characteristics.reserveInitialCapacity(webCharacteristics.size()); |
| 54 for (WebBluetoothRemoteGATTCharacteristicInit* webCharacteristic : webCh
aracteristics) { |
| 55 characteristics.append(BluetoothRemoteGATTCharacteristic::take(m_res
olver, adoptPtr(webCharacteristic))); |
| 56 } |
| 57 m_resolver->resolve(characteristics); |
| 58 } |
| 59 |
| 60 void onError(const WebBluetoothError& e) override |
| 61 { |
| 62 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
| 63 return; |
| 64 m_resolver->reject(BluetoothError::take(m_resolver, e)); |
| 65 } |
| 66 private: |
| 67 Persistent<ScriptPromiseResolver> m_resolver; |
| 68 mojom::WebBluetoothQueryType m_type; |
| 69 }; |
| 70 |
| 71 ScriptPromise BluetoothRemoteGATTService::getCharacteristic(ScriptState* scriptS
tate, const StringOrUnsignedLong& characteristic, ExceptionState& exceptionState
) |
35 { | 72 { |
36 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); | |
37 | |
38 String characteristicUUID = BluetoothUUID::getCharacteristic(characteristic,
exceptionState); | 73 String characteristicUUID = BluetoothUUID::getCharacteristic(characteristic,
exceptionState); |
39 if (exceptionState.hadException()) | 74 if (exceptionState.hadException()) |
40 return exceptionState.reject(scriptState); | 75 return exceptionState.reject(scriptState); |
41 | 76 |
42 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 77 return getCharacteristicsImpl(scriptState, mojom::WebBluetoothQueryType::SIN
GLE, characteristicUUID); |
43 ScriptPromise promise = resolver->promise(); | |
44 webbluetooth->getCharacteristic(m_webService->serviceInstanceID, characteris
ticUUID, new CallbackPromiseAdapter<BluetoothRemoteGATTCharacteristic, Bluetooth
Error>(resolver)); | |
45 | |
46 return promise; | |
47 } | |
48 | |
49 ScriptPromise BluetoothRemoteGATTService::getCharacteristics(ScriptState* script
State, ExceptionState&) | |
50 { | |
51 return getCharacteristicsImpl(scriptState, String()); | |
52 } | 78 } |
53 | 79 |
54 ScriptPromise BluetoothRemoteGATTService::getCharacteristics(ScriptState* script
State, const StringOrUnsignedLong& characteristic, ExceptionState& exceptionStat
e) | 80 ScriptPromise BluetoothRemoteGATTService::getCharacteristics(ScriptState* script
State, const StringOrUnsignedLong& characteristic, ExceptionState& exceptionStat
e) |
55 { | 81 { |
56 String characteristicUUID = BluetoothUUID::getCharacteristic(characteristic,
exceptionState); | 82 String characteristicUUID = BluetoothUUID::getCharacteristic(characteristic,
exceptionState); |
57 if (exceptionState.hadException()) | 83 if (exceptionState.hadException()) |
58 return exceptionState.reject(scriptState); | 84 return exceptionState.reject(scriptState); |
59 | 85 |
60 return getCharacteristicsImpl(scriptState, characteristicUUID); | 86 return getCharacteristicsImpl(scriptState, mojom::WebBluetoothQueryType::MUL
TIPLE, characteristicUUID); |
61 } | 87 } |
62 | 88 |
63 // Class that allows us to use CallbackPromiseAdapter to resolve a promise with
a | 89 ScriptPromise BluetoothRemoteGATTService::getCharacteristics(ScriptState* script
State, ExceptionState&) |
64 // vector owning BluetoothRemoteGATTCharacteristics. | 90 { |
65 class RemoteCharacteristicArray { | 91 return getCharacteristicsImpl(scriptState, mojom::WebBluetoothQueryType::MUL
TIPLE); |
66 STATIC_ONLY(RemoteCharacteristicArray); | 92 } |
67 public: | |
68 using WebType = OwnPtr<WebVector<WebBluetoothRemoteGATTCharacteristicInit*>>
; | |
69 static HeapVector<Member<BluetoothRemoteGATTCharacteristic>> take(ScriptProm
iseResolver* resolver, PassOwnPtr<WebVector<WebBluetoothRemoteGATTCharacteristic
Init*>> webCharacteristics) | |
70 { | |
71 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> characteristics; | |
72 characteristics.reserveInitialCapacity(webCharacteristics->size()); | |
73 for (WebBluetoothRemoteGATTCharacteristicInit* webCharacteristic : *webC
haracteristics) { | |
74 characteristics.append(BluetoothRemoteGATTCharacteristic::take(resol
ver, adoptPtr(webCharacteristic))); | |
75 } | |
76 return characteristics; | |
77 } | |
78 }; | |
79 | 93 |
80 ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl(ScriptState* sc
riptState, String characteristicsUUID) | 94 ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl(ScriptState* sc
riptState, mojom::WebBluetoothQueryType type, String characteristicsUUID) |
81 { | 95 { |
82 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 96 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
83 ScriptPromise promise = resolver->promise(); | 97 ScriptPromise promise = resolver->promise(); |
84 | 98 |
85 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); | 99 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); |
86 webbluetooth->getCharacteristics(m_webService->serviceInstanceID, characteri
sticsUUID, new CallbackPromiseAdapter<RemoteCharacteristicArray, BluetoothError>
(resolver)); | 100 webbluetooth->getCharacteristics(m_webService->serviceInstanceID, type, char
acteristicsUUID, new GetCharacteristicsCallback(type, resolver)); |
87 | 101 |
88 return promise; | 102 return promise; |
89 } | 103 } |
90 | 104 |
91 } // namespace blink | 105 } // namespace blink |
OLD | NEW |