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