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/DOMDataView.h" | 10 #include "core/dom/DOMDataView.h" |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 { | 138 { |
139 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); | 139 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); |
140 | 140 |
141 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 141 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
142 ScriptPromise promise = resolver->promise(); | 142 ScriptPromise promise = resolver->promise(); |
143 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, new R
eadValueCallback(this, resolver)); | 143 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, new R
eadValueCallback(this, resolver)); |
144 | 144 |
145 return promise; | 145 return promise; |
146 } | 146 } |
147 | 147 |
| 148 class WriteValueCallback : public WebBluetoothWriteValueCallbacks { |
| 149 public: |
| 150 WriteValueCallback(BluetoothGATTCharacteristic* characteristic, ScriptPromis
eResolver* resolver) : m_webCharacteristic(characteristic), m_resolver(resolver)
{} |
| 151 |
| 152 void onSuccess(const WebVector<uint8_t>& value) override |
| 153 { |
| 154 if (!m_resolver->executionContext() || m_resolver->executionContext()->a
ctiveDOMObjectsAreStopped()) |
| 155 return; |
| 156 |
| 157 if (m_webCharacteristic) { |
| 158 m_webCharacteristic->setValue(ConvertWebVectorToDataView(value)); |
| 159 } |
| 160 m_resolver->resolve(); |
| 161 } |
| 162 |
| 163 void onError(const WebBluetoothError& e) override |
| 164 { |
| 165 if (!m_resolver->executionContext() || m_resolver->executionContext()->a
ctiveDOMObjectsAreStopped()) |
| 166 return; |
| 167 m_resolver->reject(BluetoothError::take(m_resolver, e)); |
| 168 } |
| 169 |
| 170 private: |
| 171 WeakPersistent<BluetoothGATTCharacteristic> m_webCharacteristic; |
| 172 Persistent<ScriptPromiseResolver> m_resolver; |
| 173 }; |
| 174 |
148 ScriptPromise BluetoothGATTCharacteristic::writeValue(ScriptState* scriptState,
const DOMArrayPiece& value) | 175 ScriptPromise BluetoothGATTCharacteristic::writeValue(ScriptState* scriptState,
const DOMArrayPiece& value) |
149 { | 176 { |
150 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); | 177 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); |
151 // Partial implementation of writeValue algorithm: | 178 // Partial implementation of writeValue algorithm: |
152 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothgattchar
acteristic-writevalue | 179 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothgattchar
acteristic-writevalue |
153 | 180 |
154 // If bytes is more than 512 bytes long (the maximum length of an attribute | 181 // If bytes is more than 512 bytes long (the maximum length of an attribute |
155 // value, per Long Attribute Values) return a promise rejected with an | 182 // value, per Long Attribute Values) return a promise rejected with an |
156 // InvalidModificationError and abort. | 183 // InvalidModificationError and abort. |
157 if (value.byteLength() > 512) | 184 if (value.byteLength() > 512) |
158 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidModificationError, "Value can't exceed 512 bytes.")); | 185 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidModificationError, "Value can't exceed 512 bytes.")); |
159 | 186 |
160 // Let valueVector be a copy of the bytes held by value. | 187 // Let valueVector be a copy of the bytes held by value. |
161 WebVector<uint8_t> valueVector(value.bytes(), value.byteLength()); | 188 WebVector<uint8_t> valueVector(value.bytes(), value.byteLength()); |
162 | 189 |
163 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 190 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
164 | 191 |
165 ScriptPromise promise = resolver->promise(); | 192 ScriptPromise promise = resolver->promise(); |
166 webbluetooth->writeValue(m_webCharacteristic->characteristicInstanceID, valu
eVector, new CallbackPromiseAdapter<void, BluetoothError>(resolver)); | 193 webbluetooth->writeValue(m_webCharacteristic->characteristicInstanceID, valu
eVector, new WriteValueCallback(this, resolver)); |
167 | 194 |
168 return promise; | 195 return promise; |
169 } | 196 } |
170 | 197 |
171 ScriptPromise BluetoothGATTCharacteristic::startNotifications(ScriptState* scrip
tState) | 198 ScriptPromise BluetoothGATTCharacteristic::startNotifications(ScriptState* scrip
tState) |
172 { | 199 { |
173 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); | 200 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); |
174 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 201 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
175 ScriptPromise promise = resolver->promise(); | 202 ScriptPromise promise = resolver->promise(); |
176 webbluetooth->startNotifications(m_webCharacteristic->characteristicInstance
ID, this, new CallbackPromiseAdapter<void, BluetoothError>(resolver)); | 203 webbluetooth->startNotifications(m_webCharacteristic->characteristicInstance
ID, this, new CallbackPromiseAdapter<void, BluetoothError>(resolver)); |
(...skipping 10 matching lines...) Expand all Loading... |
187 } | 214 } |
188 | 215 |
189 DEFINE_TRACE(BluetoothGATTCharacteristic) | 216 DEFINE_TRACE(BluetoothGATTCharacteristic) |
190 { | 217 { |
191 RefCountedGarbageCollectedEventTargetWithInlineData<BluetoothGATTCharacteris
tic>::trace(visitor); | 218 RefCountedGarbageCollectedEventTargetWithInlineData<BluetoothGATTCharacteris
tic>::trace(visitor); |
192 ActiveDOMObject::trace(visitor); | 219 ActiveDOMObject::trace(visitor); |
193 visitor->trace(m_properties); | 220 visitor->trace(m_properties); |
194 } | 221 } |
195 | 222 |
196 } // namespace blink | 223 } // namespace blink |
OLD | NEW |