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 "config.h" | 5 #include "config.h" |
6 #include "modules/bluetooth/BluetoothGATTCharacteristic.h" | 6 #include "modules/bluetooth/BluetoothGATTCharacteristic.h" |
7 | 7 |
8 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
9 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
10 #include "bindings/core/v8/ScriptPromiseResolver.h" | 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 if (!webCharacteristic) { | 27 if (!webCharacteristic) { |
28 return nullptr; | 28 return nullptr; |
29 } | 29 } |
30 return new BluetoothGATTCharacteristic(webCharacteristic); | 30 return new BluetoothGATTCharacteristic(webCharacteristic); |
31 } | 31 } |
32 | 32 |
33 ScriptPromise BluetoothGATTCharacteristic::readValue(ScriptState* scriptState) | 33 ScriptPromise BluetoothGATTCharacteristic::readValue(ScriptState* scriptState) |
34 { | 34 { |
35 WebBluetooth* webbluetooth = Platform::current()->bluetooth(); | 35 WebBluetooth* webbluetooth = Platform::current()->bluetooth(); |
36 | 36 |
37 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 37 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
38 ScriptPromise promise = resolver->promise(); | 38 ScriptPromise promise = resolver->promise(); |
39 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, new C
allbackPromiseAdapter<ConvertWebVectorToArrayBuffer, BluetoothError>(resolver)); | 39 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, new C
allbackPromiseAdapter<ConvertWebVectorToArrayBuffer, BluetoothError>(resolver)); |
40 | 40 |
41 return promise; | 41 return promise; |
42 } | 42 } |
43 | 43 |
44 ScriptPromise BluetoothGATTCharacteristic::writeValue(ScriptState* scriptState,
const DOMArrayPiece& value) | 44 ScriptPromise BluetoothGATTCharacteristic::writeValue(ScriptState* scriptState,
const DOMArrayPiece& value) |
45 { | 45 { |
46 WebBluetooth* webbluetooth = Platform::current()->bluetooth(); | 46 WebBluetooth* webbluetooth = Platform::current()->bluetooth(); |
47 // Partial implementation of writeValue algorithm: | 47 // Partial implementation of writeValue algorithm: |
48 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothgattchar
acteristic-writevalue | 48 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothgattchar
acteristic-writevalue |
49 | 49 |
50 // Let valueVector be a copy of the bytes held by value. | 50 // Let valueVector be a copy of the bytes held by value. |
51 std::vector<uint8_t> valueVector(value.bytes(), value.bytes() + value.byteLe
ngth()); | 51 std::vector<uint8_t> valueVector(value.bytes(), value.bytes() + value.byteLe
ngth()); |
52 // If bytes is more than 512 bytes long (the maximum length of an attribute | 52 // If bytes is more than 512 bytes long (the maximum length of an attribute |
53 // value, per Long Attribute Values) return a promise rejected with an | 53 // value, per Long Attribute Values) return a promise rejected with an |
54 // InvalidModificationError and abort. | 54 // InvalidModificationError and abort. |
55 if (valueVector.size() > 512) | 55 if (valueVector.size() > 512) |
56 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidModificationError, "Value can't exceed 512 bytes.")); | 56 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidModificationError, "Value can't exceed 512 bytes.")); |
57 | 57 |
58 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 58 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
59 | 59 |
60 ScriptPromise promise = resolver->promise(); | 60 ScriptPromise promise = resolver->promise(); |
61 webbluetooth->writeValue(m_webCharacteristic->characteristicInstanceID, valu
eVector, new CallbackPromiseAdapter<void, BluetoothError>(resolver)); | 61 webbluetooth->writeValue(m_webCharacteristic->characteristicInstanceID, valu
eVector, new CallbackPromiseAdapter<void, BluetoothError>(resolver)); |
62 | 62 |
63 return promise; | 63 return promise; |
64 } | 64 } |
65 | 65 |
66 } // namespace blink | 66 } // namespace blink |
OLD | NEW |