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