| 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/BluetoothRemoteGATTCharacteristic.h" | 5 #include "modules/bluetooth/BluetoothRemoteGATTCharacteristic.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" |
| 11 #include "core/dom/DOMException.h" | 11 #include "core/dom/DOMException.h" |
| 12 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
| 13 #include "core/events/Event.h" | 13 #include "core/events/Event.h" |
| 14 #include "core/inspector/ConsoleMessage.h" | 14 #include "core/inspector/ConsoleMessage.h" |
| 15 #include "modules/bluetooth/BluetoothCharacteristicProperties.h" | 15 #include "modules/bluetooth/BluetoothCharacteristicProperties.h" |
| 16 #include "modules/bluetooth/BluetoothError.h" | 16 #include "modules/bluetooth/BluetoothError.h" |
| 17 #include "modules/bluetooth/BluetoothRemoteGATTService.h" | 17 #include "modules/bluetooth/BluetoothRemoteGATTService.h" |
| 18 #include "modules/bluetooth/BluetoothSupplement.h" | 18 #include "modules/bluetooth/BluetoothSupplement.h" |
| 19 #include "public/platform/modules/bluetooth/WebBluetooth.h" | 19 #include "public/platform/modules/bluetooth/WebBluetooth.h" |
| 20 #include <memory> | 20 #include <memory> |
| 21 | 21 |
| 22 namespace blink { | 22 namespace blink { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 const char kGATTServerDisconnected[] = |
| 27 "GATT Server disconnected while performing a GATT operation."; |
| 28 const char kGATTServerNotConnected[] = |
| 29 "GATT Server is disconnected. Cannot perform GATT operations."; |
| 30 |
| 26 DOMDataView* ConvertWebVectorToDataView(const WebVector<uint8_t>& webVector) { | 31 DOMDataView* ConvertWebVectorToDataView(const WebVector<uint8_t>& webVector) { |
| 27 static_assert(sizeof(*webVector.data()) == 1, | 32 static_assert(sizeof(*webVector.data()) == 1, |
| 28 "uint8_t should be a single byte"); | 33 "uint8_t should be a single byte"); |
| 29 DOMArrayBuffer* domBuffer = | 34 DOMArrayBuffer* domBuffer = |
| 30 DOMArrayBuffer::create(webVector.data(), webVector.size()); | 35 DOMArrayBuffer::create(webVector.data(), webVector.size()); |
| 31 return DOMDataView::create(domBuffer, 0, webVector.size()); | 36 return DOMDataView::create(domBuffer, 0, webVector.size()); |
| 32 } | 37 } |
| 33 | 38 |
| 34 } // anonymous namespace | 39 } // anonymous namespace |
| 35 | 40 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 BluetoothSupplement::fromExecutionContext(getExecutionContext()); | 117 BluetoothSupplement::fromExecutionContext(getExecutionContext()); |
| 113 webbluetooth->registerCharacteristicObject( | 118 webbluetooth->registerCharacteristicObject( |
| 114 m_webCharacteristic->characteristicInstanceID, this); | 119 m_webCharacteristic->characteristicInstanceID, this); |
| 115 } | 120 } |
| 116 } | 121 } |
| 117 | 122 |
| 118 class ReadValueCallback : public WebBluetoothReadValueCallbacks { | 123 class ReadValueCallback : public WebBluetoothReadValueCallbacks { |
| 119 public: | 124 public: |
| 120 ReadValueCallback(BluetoothRemoteGATTCharacteristic* characteristic, | 125 ReadValueCallback(BluetoothRemoteGATTCharacteristic* characteristic, |
| 121 ScriptPromiseResolver* resolver) | 126 ScriptPromiseResolver* resolver) |
| 122 : m_webCharacteristic(characteristic), m_resolver(resolver) {} | 127 : m_characteristic(characteristic), m_resolver(resolver) { |
| 128 // We always check that the device is connected before constructing this |
| 129 // object. |
| 130 CHECK(m_characteristic->gatt()->connected()); |
| 131 m_characteristic->gatt()->AddToActiveAlgorithms(m_resolver.get()); |
| 132 } |
| 123 | 133 |
| 124 void onSuccess(const WebVector<uint8_t>& value) override { | 134 void onSuccess(const WebVector<uint8_t>& value) override { |
| 125 if (!m_resolver->getExecutionContext() || | 135 if (!m_resolver->getExecutionContext() || |
| 126 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) | 136 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
| 127 return; | 137 return; |
| 128 | 138 |
| 139 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms( |
| 140 m_resolver.get())) { |
| 141 m_resolver->reject( |
| 142 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 143 return; |
| 144 } |
| 145 |
| 129 DOMDataView* domDataView = ConvertWebVectorToDataView(value); | 146 DOMDataView* domDataView = ConvertWebVectorToDataView(value); |
| 130 if (m_webCharacteristic) | 147 if (m_characteristic) |
| 131 m_webCharacteristic->setValue(domDataView); | 148 m_characteristic->setValue(domDataView); |
| 132 | 149 |
| 133 m_resolver->resolve(domDataView); | 150 m_resolver->resolve(domDataView); |
| 134 } | 151 } |
| 135 | 152 |
| 136 void onError( | 153 void onError( |
| 137 int32_t | 154 int32_t |
| 138 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) | 155 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) |
| 139 override { | 156 override { |
| 140 if (!m_resolver->getExecutionContext() || | 157 if (!m_resolver->getExecutionContext() || |
| 141 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) | 158 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
| 142 return; | 159 return; |
| 160 |
| 161 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms( |
| 162 m_resolver.get())) { |
| 163 m_resolver->reject( |
| 164 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 165 return; |
| 166 } |
| 167 |
| 143 m_resolver->reject(BluetoothError::take(m_resolver, error)); | 168 m_resolver->reject(BluetoothError::take(m_resolver, error)); |
| 144 } | 169 } |
| 145 | 170 |
| 146 private: | 171 private: |
| 147 WeakPersistent<BluetoothRemoteGATTCharacteristic> m_webCharacteristic; | 172 WeakPersistent<BluetoothRemoteGATTCharacteristic> m_characteristic; |
| 148 Persistent<ScriptPromiseResolver> m_resolver; | 173 Persistent<ScriptPromiseResolver> m_resolver; |
| 149 }; | 174 }; |
| 150 | 175 |
| 151 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( | 176 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( |
| 152 ScriptState* scriptState) { | 177 ScriptState* scriptState) { |
| 178 if (!gatt()->connected()) { |
| 179 return ScriptPromise::rejectWithDOMException( |
| 180 scriptState, |
| 181 DOMException::create(NetworkError, kGATTServerNotConnected)); |
| 182 } |
| 183 |
| 153 WebBluetooth* webbluetooth = | 184 WebBluetooth* webbluetooth = |
| 154 BluetoothSupplement::fromScriptState(scriptState); | 185 BluetoothSupplement::fromScriptState(scriptState); |
| 155 | 186 |
| 156 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 187 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 157 ScriptPromise promise = resolver->promise(); | 188 ScriptPromise promise = resolver->promise(); |
| 158 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, | 189 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, |
| 159 new ReadValueCallback(this, resolver)); | 190 new ReadValueCallback(this, resolver)); |
| 160 | 191 |
| 161 return promise; | 192 return promise; |
| 162 } | 193 } |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 | 314 |
| 284 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { | 315 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { |
| 285 visitor->trace(m_service); | 316 visitor->trace(m_service); |
| 286 visitor->trace(m_properties); | 317 visitor->trace(m_properties); |
| 287 visitor->trace(m_value); | 318 visitor->trace(m_value); |
| 288 EventTargetWithInlineData::trace(visitor); | 319 EventTargetWithInlineData::trace(visitor); |
| 289 ActiveDOMObject::trace(visitor); | 320 ActiveDOMObject::trace(visitor); |
| 290 } | 321 } |
| 291 | 322 |
| 292 } // namespace blink | 323 } // namespace blink |
| OLD | NEW |