| 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/BluetoothGATTCharacteristic.h" | 5 #include "modules/bluetooth/BluetoothGATTCharacteristic.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" |
| 11 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
| 12 #include "core/events/Event.h" | 12 #include "core/events/Event.h" |
| 13 #include "modules/bluetooth/BluetoothCharacteristicProperties.h" | 13 #include "modules/bluetooth/BluetoothCharacteristicProperties.h" |
| 14 #include "modules/bluetooth/BluetoothError.h" | 14 #include "modules/bluetooth/BluetoothError.h" |
| 15 #include "modules/bluetooth/BluetoothSupplement.h" | 15 #include "modules/bluetooth/BluetoothSupplement.h" |
| 16 #include "modules/bluetooth/ConvertWebVectorToArrayBuffer.h" | |
| 17 #include "public/platform/modules/bluetooth/WebBluetooth.h" | 16 #include "public/platform/modules/bluetooth/WebBluetooth.h" |
| 18 | 17 |
| 19 namespace blink { | 18 namespace blink { |
| 20 | 19 |
| 20 namespace { |
| 21 |
| 22 PassRefPtr<DOMArrayBuffer> ConvertWebVectorToArrayBuffer( |
| 23 const WebVector<uint8_t>& webVector) |
| 24 { |
| 25 static_assert(sizeof(*webVector.data()) == 1, "uint8_t should be a single by
te"); |
| 26 RefPtr<DOMArrayBuffer> domBuffer = DOMArrayBuffer::create(webVector.data(),
webVector.size()); |
| 27 return domBuffer; |
| 28 } |
| 29 |
| 30 } // anonymous namespace |
| 31 |
| 21 BluetoothGATTCharacteristic::BluetoothGATTCharacteristic(ExecutionContext* conte
xt, PassOwnPtr<WebBluetoothGATTCharacteristicInit> webCharacteristic) | 32 BluetoothGATTCharacteristic::BluetoothGATTCharacteristic(ExecutionContext* conte
xt, PassOwnPtr<WebBluetoothGATTCharacteristicInit> webCharacteristic) |
| 22 : ActiveDOMObject(context) | 33 : ActiveDOMObject(context) |
| 23 , m_webCharacteristic(webCharacteristic) | 34 , m_webCharacteristic(webCharacteristic) |
| 24 , m_stopped(false) | 35 , m_stopped(false) |
| 25 { | 36 { |
| 26 m_properties = BluetoothCharacteristicProperties::create(m_webCharacteristic
->characteristicProperties); | 37 m_properties = BluetoothCharacteristicProperties::create(m_webCharacteristic
->characteristicProperties); |
| 27 // See example in Source/platform/heap/ThreadState.h | 38 // See example in Source/platform/heap/ThreadState.h |
| 28 ThreadState::current()->registerPreFinalizer(this); | 39 ThreadState::current()->registerPreFinalizer(this); |
| 29 } | 40 } |
| 30 | 41 |
| 31 BluetoothGATTCharacteristic* BluetoothGATTCharacteristic::take(ScriptPromiseReso
lver* resolver, PassOwnPtr<WebBluetoothGATTCharacteristicInit> webCharacteristic
) | 42 BluetoothGATTCharacteristic* BluetoothGATTCharacteristic::take(ScriptPromiseReso
lver* resolver, PassOwnPtr<WebBluetoothGATTCharacteristicInit> webCharacteristic
) |
| 32 { | 43 { |
| 33 if (!webCharacteristic) { | 44 if (!webCharacteristic) { |
| 34 return nullptr; | 45 return nullptr; |
| 35 } | 46 } |
| 36 BluetoothGATTCharacteristic* characteristic = new BluetoothGATTCharacteristi
c(resolver->executionContext(), webCharacteristic); | 47 BluetoothGATTCharacteristic* characteristic = new BluetoothGATTCharacteristi
c(resolver->executionContext(), webCharacteristic); |
| 37 // See note in ActiveDOMObject about suspendIfNeeded. | 48 // See note in ActiveDOMObject about suspendIfNeeded. |
| 38 characteristic->suspendIfNeeded(); | 49 characteristic->suspendIfNeeded(); |
| 39 return characteristic; | 50 return characteristic; |
| 40 } | 51 } |
| 41 | 52 |
| 53 void BluetoothGATTCharacteristic::setValue( |
| 54 const PassRefPtr<DOMArrayBuffer>& domBuffer) |
| 55 { |
| 56 m_value = domBuffer; |
| 57 } |
| 58 |
| 42 void BluetoothGATTCharacteristic::dispatchCharacteristicValueChanged( | 59 void BluetoothGATTCharacteristic::dispatchCharacteristicValueChanged( |
| 43 const WebVector<uint8_t>& value) | 60 const WebVector<uint8_t>& value) |
| 44 { | 61 { |
| 45 static_assert(sizeof(*value.data()) == 1, "uint8_t should be a single byte")
; | 62 RefPtr<DOMArrayBuffer> domBuffer = ConvertWebVectorToArrayBuffer(value); |
| 46 | 63 this->setValue(domBuffer); |
| 47 m_value = DOMArrayBuffer::create(value.data(), value.size()); | |
| 48 | |
| 49 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); | 64 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); |
| 50 } | 65 } |
| 51 | 66 |
| 52 void BluetoothGATTCharacteristic::stop() | 67 void BluetoothGATTCharacteristic::stop() |
| 53 { | 68 { |
| 54 notifyCharacteristicObjectRemoved(); | 69 notifyCharacteristicObjectRemoved(); |
| 55 } | 70 } |
| 56 | 71 |
| 57 void BluetoothGATTCharacteristic::dispose() | 72 void BluetoothGATTCharacteristic::dispose() |
| 58 { | 73 { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 82 { | 97 { |
| 83 // We will also need to unregister a characteristic once all the event | 98 // We will also need to unregister a characteristic once all the event |
| 84 // listeners have been removed. See http://crbug.com/541390 | 99 // listeners have been removed. See http://crbug.com/541390 |
| 85 if (eventType == EventTypeNames::characteristicvaluechanged) { | 100 if (eventType == EventTypeNames::characteristicvaluechanged) { |
| 86 WebBluetooth* webbluetooth = BluetoothSupplement::fromExecutionContext(e
xecutionContext()); | 101 WebBluetooth* webbluetooth = BluetoothSupplement::fromExecutionContext(e
xecutionContext()); |
| 87 webbluetooth->registerCharacteristicObject(m_webCharacteristic->characte
risticInstanceID, this); | 102 webbluetooth->registerCharacteristicObject(m_webCharacteristic->characte
risticInstanceID, this); |
| 88 } | 103 } |
| 89 return EventTarget::addEventListenerInternal(eventType, listener, options); | 104 return EventTarget::addEventListenerInternal(eventType, listener, options); |
| 90 } | 105 } |
| 91 | 106 |
| 107 class ReadValueCallback : public WebBluetoothReadValueCallbacks { |
| 108 public: |
| 109 ReadValueCallback(BluetoothGATTCharacteristic* characteristic, ScriptPromise
Resolver* resolver) : m_webCharacteristic(characteristic), m_resolver(resolver)
{} |
| 110 |
| 111 void onSuccess(const WebVector<uint8_t>& value) override |
| 112 { |
| 113 if (!m_resolver->executionContext() || m_resolver->executionContext()->a
ctiveDOMObjectsAreStopped()) |
| 114 return; |
| 115 |
| 116 RefPtr<DOMArrayBuffer> domBuffer = ConvertWebVectorToArrayBuffer(value); |
| 117 if (m_webCharacteristic) { |
| 118 m_webCharacteristic->setValue(domBuffer); |
| 119 } |
| 120 m_resolver->resolve(domBuffer); |
| 121 } |
| 122 |
| 123 void onError(const WebBluetoothError& e) override |
| 124 { |
| 125 if (!m_resolver->executionContext() || m_resolver->executionContext()->a
ctiveDOMObjectsAreStopped()) |
| 126 return; |
| 127 m_resolver->reject(BluetoothError::take(m_resolver, e)); |
| 128 } |
| 129 |
| 130 private: |
| 131 WeakPersistent<BluetoothGATTCharacteristic> m_webCharacteristic; |
| 132 Persistent<ScriptPromiseResolver> m_resolver; |
| 133 }; |
| 134 |
| 92 ScriptPromise BluetoothGATTCharacteristic::readValue(ScriptState* scriptState) | 135 ScriptPromise BluetoothGATTCharacteristic::readValue(ScriptState* scriptState) |
| 93 { | 136 { |
| 94 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); | 137 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); |
| 95 | 138 |
| 96 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 139 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 97 ScriptPromise promise = resolver->promise(); | 140 ScriptPromise promise = resolver->promise(); |
| 98 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, new C
allbackPromiseAdapter<ConvertWebVectorToArrayBuffer, BluetoothError>(resolver)); | 141 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, new R
eadValueCallback(this, resolver)); |
| 99 | 142 |
| 100 return promise; | 143 return promise; |
| 101 } | 144 } |
| 102 | 145 |
| 103 ScriptPromise BluetoothGATTCharacteristic::writeValue(ScriptState* scriptState,
const DOMArrayPiece& value) | 146 ScriptPromise BluetoothGATTCharacteristic::writeValue(ScriptState* scriptState,
const DOMArrayPiece& value) |
| 104 { | 147 { |
| 105 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); | 148 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); |
| 106 // Partial implementation of writeValue algorithm: | 149 // Partial implementation of writeValue algorithm: |
| 107 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothgattchar
acteristic-writevalue | 150 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothgattchar
acteristic-writevalue |
| 108 | 151 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 } | 185 } |
| 143 | 186 |
| 144 DEFINE_TRACE(BluetoothGATTCharacteristic) | 187 DEFINE_TRACE(BluetoothGATTCharacteristic) |
| 145 { | 188 { |
| 146 RefCountedGarbageCollectedEventTargetWithInlineData<BluetoothGATTCharacteris
tic>::trace(visitor); | 189 RefCountedGarbageCollectedEventTargetWithInlineData<BluetoothGATTCharacteris
tic>::trace(visitor); |
| 147 ActiveDOMObject::trace(visitor); | 190 ActiveDOMObject::trace(visitor); |
| 148 visitor->trace(m_properties); | 191 visitor->trace(m_properties); |
| 149 } | 192 } |
| 150 | 193 |
| 151 } // namespace blink | 194 } // namespace blink |
| OLD | NEW |