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