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 25 matching lines...) Expand all Loading... |
36 | 36 |
37 DEFINE_TRACE(BluetoothRemoteGATTService) | 37 DEFINE_TRACE(BluetoothRemoteGATTService) |
38 { | 38 { |
39 visitor->trace(m_device); | 39 visitor->trace(m_device); |
40 } | 40 } |
41 | 41 |
42 // Class that allows us to resolve the promise with a single Characteristic or | 42 // Class that allows us to resolve the promise with a single Characteristic or |
43 // with a vector owning the characteristics. | 43 // with a vector owning the characteristics. |
44 class GetCharacteristicsCallback : public WebBluetoothGetCharacteristicsCallback
s { | 44 class GetCharacteristicsCallback : public WebBluetoothGetCharacteristicsCallback
s { |
45 public: | 45 public: |
46 GetCharacteristicsCallback(BluetoothRemoteGATTService* service, mojom::WebBl
uetoothGATTQueryQuantity quantity, ScriptPromiseResolver* resolver) | 46 GetCharacteristicsCallback(BluetoothRemoteGATTService* service, mojom::blink
::WebBluetoothGATTQueryQuantity quantity, ScriptPromiseResolver* resolver) |
47 : m_service(service) | 47 : m_service(service) |
48 , m_quantity(quantity) | 48 , m_quantity(quantity) |
49 , m_resolver(resolver) {} | 49 , m_resolver(resolver) {} |
50 | 50 |
51 void onSuccess(const WebVector<WebBluetoothRemoteGATTCharacteristicInit*>& w
ebCharacteristics) override | 51 void onSuccess(const WebVector<WebBluetoothRemoteGATTCharacteristicInit*>& w
ebCharacteristics) override |
52 { | 52 { |
53 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) | 53 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
54 return; | 54 return; |
55 | 55 |
56 if (m_quantity == mojom::WebBluetoothGATTQueryQuantity::SINGLE) { | 56 if (m_quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { |
57 DCHECK_EQ(1u, webCharacteristics.size()); | 57 DCHECK_EQ(1u, webCharacteristics.size()); |
58 m_resolver->resolve(BluetoothRemoteGATTCharacteristic::take(m_resolv
er, wrapUnique(webCharacteristics[0]), m_service)); | 58 m_resolver->resolve(BluetoothRemoteGATTCharacteristic::take(m_resolv
er, wrapUnique(webCharacteristics[0]), m_service)); |
59 return; | 59 return; |
60 } | 60 } |
61 | 61 |
62 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> characteristics; | 62 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> characteristics; |
63 characteristics.reserveInitialCapacity(webCharacteristics.size()); | 63 characteristics.reserveInitialCapacity(webCharacteristics.size()); |
64 for (WebBluetoothRemoteGATTCharacteristicInit* webCharacteristic : webCh
aracteristics) { | 64 for (WebBluetoothRemoteGATTCharacteristicInit* webCharacteristic : webCh
aracteristics) { |
65 characteristics.append(BluetoothRemoteGATTCharacteristic::take(m_res
olver, wrapUnique(webCharacteristic), m_service)); | 65 characteristics.append(BluetoothRemoteGATTCharacteristic::take(m_res
olver, wrapUnique(webCharacteristic), m_service)); |
66 } | 66 } |
67 m_resolver->resolve(characteristics); | 67 m_resolver->resolve(characteristics); |
68 } | 68 } |
69 | 69 |
70 void onError(const WebBluetoothError& e) override | 70 void onError(int32_t error /* Corresponds to WebBluetoothError in web_blueto
oth.mojom */) override |
71 { | 71 { |
72 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) | 72 if (!m_resolver->getExecutionContext() || m_resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
73 return; | 73 return; |
74 m_resolver->reject(BluetoothError::take(m_resolver, e)); | 74 m_resolver->reject(BluetoothError::take(m_resolver, error)); |
75 } | 75 } |
76 private: | 76 private: |
77 Persistent<BluetoothRemoteGATTService> m_service; | 77 Persistent<BluetoothRemoteGATTService> m_service; |
78 mojom::WebBluetoothGATTQueryQuantity m_quantity; | 78 mojom::blink::WebBluetoothGATTQueryQuantity m_quantity; |
79 Persistent<ScriptPromiseResolver> m_resolver; | 79 Persistent<ScriptPromiseResolver> m_resolver; |
80 }; | 80 }; |
81 | 81 |
82 ScriptPromise BluetoothRemoteGATTService::getCharacteristic(ScriptState* scriptS
tate, const StringOrUnsignedLong& characteristic, ExceptionState& exceptionState
) | 82 ScriptPromise BluetoothRemoteGATTService::getCharacteristic(ScriptState* scriptS
tate, const StringOrUnsignedLong& characteristic, ExceptionState& exceptionState
) |
83 { | 83 { |
84 String characteristicUUID = BluetoothUUID::getCharacteristic(characteristic,
exceptionState); | 84 String characteristicUUID = BluetoothUUID::getCharacteristic(characteristic,
exceptionState); |
85 if (exceptionState.hadException()) | 85 if (exceptionState.hadException()) |
86 return exceptionState.reject(scriptState); | 86 return exceptionState.reject(scriptState); |
87 | 87 |
88 return getCharacteristicsImpl(scriptState, mojom::WebBluetoothGATTQueryQuant
ity::SINGLE, characteristicUUID); | 88 return getCharacteristicsImpl(scriptState, mojom::blink::WebBluetoothGATTQue
ryQuantity::SINGLE, characteristicUUID); |
89 } | 89 } |
90 | 90 |
91 ScriptPromise BluetoothRemoteGATTService::getCharacteristics(ScriptState* script
State, const StringOrUnsignedLong& characteristic, ExceptionState& exceptionStat
e) | 91 ScriptPromise BluetoothRemoteGATTService::getCharacteristics(ScriptState* script
State, const StringOrUnsignedLong& characteristic, ExceptionState& exceptionStat
e) |
92 { | 92 { |
93 String characteristicUUID = BluetoothUUID::getCharacteristic(characteristic,
exceptionState); | 93 String characteristicUUID = BluetoothUUID::getCharacteristic(characteristic,
exceptionState); |
94 if (exceptionState.hadException()) | 94 if (exceptionState.hadException()) |
95 return exceptionState.reject(scriptState); | 95 return exceptionState.reject(scriptState); |
96 | 96 |
97 return getCharacteristicsImpl(scriptState, mojom::WebBluetoothGATTQueryQuant
ity::MULTIPLE, characteristicUUID); | 97 return getCharacteristicsImpl(scriptState, mojom::blink::WebBluetoothGATTQue
ryQuantity::MULTIPLE, characteristicUUID); |
98 } | 98 } |
99 | 99 |
100 ScriptPromise BluetoothRemoteGATTService::getCharacteristics(ScriptState* script
State, ExceptionState&) | 100 ScriptPromise BluetoothRemoteGATTService::getCharacteristics(ScriptState* script
State, ExceptionState&) |
101 { | 101 { |
102 return getCharacteristicsImpl(scriptState, mojom::WebBluetoothGATTQueryQuant
ity::MULTIPLE); | 102 return getCharacteristicsImpl(scriptState, mojom::blink::WebBluetoothGATTQue
ryQuantity::MULTIPLE); |
103 } | 103 } |
104 | 104 |
105 ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl(ScriptState* sc
riptState, mojom::WebBluetoothGATTQueryQuantity quantity, String characteristics
UUID) | 105 ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl(ScriptState* sc
riptState, mojom::blink::WebBluetoothGATTQueryQuantity quantity, String characte
risticsUUID) |
106 { | 106 { |
107 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 107 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
108 ScriptPromise promise = resolver->promise(); | 108 ScriptPromise promise = resolver->promise(); |
109 | 109 |
110 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); | 110 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); |
111 webbluetooth->getCharacteristics(m_webService->serviceInstanceID, quantity,
characteristicsUUID, new GetCharacteristicsCallback(this, quantity, resolver)); | 111 webbluetooth->getCharacteristics(m_webService->serviceInstanceID, static_cas
t<int32_t>(quantity), characteristicsUUID, new GetCharacteristicsCallback(this,
quantity, resolver)); |
112 | 112 |
113 return promise; | 113 return promise; |
114 } | 114 } |
115 | 115 |
116 } // namespace blink | 116 } // namespace blink |
OLD | NEW |