OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "modules/bluetooth/BluetoothRemoteGATTDescriptor.h" |
| 6 |
| 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 8 #include "bindings/core/v8/ScriptPromise.h" |
| 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 10 #include "core/dom/DOMDataView.h" |
| 11 #include "core/dom/DOMException.h" |
| 12 #include "core/dom/ExceptionCode.h" |
| 13 #include "core/events/Event.h" |
| 14 #include "core/inspector/ConsoleMessage.h" |
| 15 #include "modules/bluetooth/BluetoothError.h" |
| 16 #include "modules/bluetooth/BluetoothRemoteGATTService.h" |
| 17 #include "modules/bluetooth/BluetoothSupplement.h" |
| 18 #include "public/platform/modules/bluetooth/WebBluetooth.h" |
| 19 #include <memory> |
| 20 |
| 21 namespace blink { |
| 22 |
| 23 namespace { |
| 24 const char kGATTServerDisconnected[] = |
| 25 "GATT Server disconnected while performing a GATT operation."; |
| 26 const char kGATTServerNotConnected[] = |
| 27 "GATT Server is disconnected. Cannot perform GATT operations."; |
| 28 |
| 29 DOMDataView* ConvertWebVectorToDataView(const WebVector<uint8_t>& webVector) { |
| 30 static_assert(sizeof(*webVector.data()) == 1, |
| 31 "uint8_t should be a single byte"); |
| 32 DOMArrayBuffer* domBuffer = |
| 33 DOMArrayBuffer::create(webVector.data(), webVector.size()); |
| 34 return DOMDataView::create(domBuffer, 0, webVector.size()); |
| 35 } |
| 36 |
| 37 } // anonymous namespace |
| 38 |
| 39 BluetoothRemoteGATTDescriptor::BluetoothRemoteGATTDescriptor( |
| 40 std::unique_ptr<WebBluetoothRemoteGATTDescriptorInit> webDescriptor, |
| 41 BluetoothRemoteGATTCharacteristic* characteristic) |
| 42 : m_webDescriptor(std::move(webDescriptor)), |
| 43 m_characteristic(characteristic) {} |
| 44 |
| 45 BluetoothRemoteGATTDescriptor* BluetoothRemoteGATTDescriptor::take( |
| 46 std::unique_ptr<WebBluetoothRemoteGATTDescriptorInit> webDescriptor, |
| 47 BluetoothRemoteGATTCharacteristic* characteristic) { |
| 48 if (!webDescriptor) { |
| 49 return nullptr; |
| 50 } |
| 51 BluetoothRemoteGATTDescriptor* descriptor = new BluetoothRemoteGATTDescriptor( |
| 52 std::move(webDescriptor), characteristic); |
| 53 return descriptor; |
| 54 } |
| 55 |
| 56 void BluetoothRemoteGATTDescriptor::setValue(DOMDataView* domDataView) { |
| 57 m_value = domDataView; |
| 58 } |
| 59 |
| 60 class DescriptorReadValueCallback : public WebBluetoothReadValueCallbacks { |
| 61 public: |
| 62 DescriptorReadValueCallback(BluetoothRemoteGATTDescriptor* descriptor, |
| 63 ScriptPromiseResolver* resolver) |
| 64 : m_descriptor(descriptor), m_resolver(resolver) { |
| 65 // We always check that the device is connected before constructing this |
| 66 // object. |
| 67 CHECK(m_descriptor->getGatt()->connected()); |
| 68 m_descriptor->getGatt()->AddToActiveAlgorithms(m_resolver.get()); |
| 69 } |
| 70 |
| 71 void onSuccess(const WebVector<uint8_t>& value) override { |
| 72 if (!m_resolver->getExecutionContext() || |
| 73 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
| 74 return; |
| 75 |
| 76 if (!m_descriptor->getGatt()->RemoveFromActiveAlgorithms( |
| 77 m_resolver.get())) { |
| 78 m_resolver->reject( |
| 79 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 80 return; |
| 81 } |
| 82 |
| 83 DOMDataView* domDataView = ConvertWebVectorToDataView(value); |
| 84 if (m_descriptor) |
| 85 m_descriptor->setValue(domDataView); |
| 86 |
| 87 m_resolver->resolve(domDataView); |
| 88 } |
| 89 |
| 90 void onError( |
| 91 int32_t |
| 92 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) |
| 93 override { |
| 94 if (!m_resolver->getExecutionContext() || |
| 95 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
| 96 return; |
| 97 |
| 98 if (!m_descriptor->getGatt()->RemoveFromActiveAlgorithms( |
| 99 m_resolver.get())) { |
| 100 m_resolver->reject( |
| 101 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 102 return; |
| 103 } |
| 104 |
| 105 m_resolver->reject(BluetoothError::take(m_resolver, error)); |
| 106 } |
| 107 |
| 108 private: |
| 109 WeakPersistent<BluetoothRemoteGATTDescriptor> m_descriptor; |
| 110 Persistent<ScriptPromiseResolver> m_resolver; |
| 111 }; |
| 112 |
| 113 ScriptPromise BluetoothRemoteGATTDescriptor::readValue( |
| 114 ScriptState* scriptState) { |
| 115 if (!getGatt()->connected()) { |
| 116 return ScriptPromise::rejectWithDOMException( |
| 117 scriptState, |
| 118 DOMException::create(NetworkError, kGATTServerNotConnected)); |
| 119 } |
| 120 |
| 121 WebBluetooth* webbluetooth = |
| 122 BluetoothSupplement::fromScriptState(scriptState); |
| 123 |
| 124 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 125 ScriptPromise promise = resolver->promise(); |
| 126 |
| 127 webbluetooth->descriptorReadValue( |
| 128 m_webDescriptor->descriptorInstanceID, |
| 129 new DescriptorReadValueCallback(this, resolver)); |
| 130 return promise; |
| 131 } |
| 132 |
| 133 class DescriptorWriteValueCallback : public WebBluetoothWriteValueCallbacks { |
| 134 public: |
| 135 DescriptorWriteValueCallback(BluetoothRemoteGATTDescriptor* descriptor, |
| 136 ScriptPromiseResolver* resolver) |
| 137 : m_webDescriptor(descriptor), m_resolver(resolver) {} |
| 138 |
| 139 void onSuccess(const WebVector<uint8_t>& value) override { |
| 140 if (!m_resolver->getExecutionContext() || |
| 141 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
| 142 return; |
| 143 |
| 144 if (m_webDescriptor) { |
| 145 m_webDescriptor->setValue(ConvertWebVectorToDataView(value)); |
| 146 } |
| 147 m_resolver->resolve(); |
| 148 } |
| 149 |
| 150 void onError( |
| 151 int32_t |
| 152 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) |
| 153 override { |
| 154 if (!m_resolver->getExecutionContext() || |
| 155 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
| 156 return; |
| 157 m_resolver->reject(BluetoothError::take(m_resolver, error)); |
| 158 } |
| 159 |
| 160 private: |
| 161 WeakPersistent<BluetoothRemoteGATTDescriptor> m_webDescriptor; |
| 162 Persistent<ScriptPromiseResolver> m_resolver; |
| 163 }; |
| 164 |
| 165 ScriptPromise BluetoothRemoteGATTDescriptor::writeValue( |
| 166 ScriptState* scriptState, |
| 167 const DOMArrayPiece& value) { |
| 168 WebBluetooth* webbluetooth = |
| 169 BluetoothSupplement::fromScriptState(scriptState); |
| 170 |
| 171 // Partial implementation of writeValue algorithm: |
| 172 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothgattdescri
ptor-writevalue |
| 173 |
| 174 // If bytes is more than 512 bytes long (the maximum length of an attribute |
| 175 // value, per Long Attribute Values) return a promise rejected with an |
| 176 // InvalidModificationError and abort. |
| 177 if (value.byteLength() > 512) { |
| 178 return ScriptPromise::rejectWithDOMException( |
| 179 scriptState, DOMException::create(InvalidModificationError, |
| 180 "Value can't exceed 512 bytes.")); |
| 181 } |
| 182 |
| 183 // Let valueVector be a copy of the bytes held by value. |
| 184 WebVector<uint8_t> valueVector(value.bytes(), value.byteLength()); |
| 185 |
| 186 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 187 |
| 188 ScriptPromise promise = resolver->promise(); |
| 189 webbluetooth->descriptorWriteValue( |
| 190 m_webDescriptor->descriptorInstanceID, valueVector, |
| 191 new DescriptorWriteValueCallback(this, resolver)); |
| 192 |
| 193 return promise; |
| 194 } |
| 195 |
| 196 DEFINE_TRACE(BluetoothRemoteGATTDescriptor) { |
| 197 visitor->trace(m_characteristic); |
| 198 visitor->trace(m_value); |
| 199 } |
| 200 |
| 201 } // namespace blink |
OLD | NEW |