| 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" |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, | 189 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, |
| 190 new ReadValueCallback(this, resolver)); | 190 new ReadValueCallback(this, resolver)); |
| 191 | 191 |
| 192 return promise; | 192 return promise; |
| 193 } | 193 } |
| 194 | 194 |
| 195 class WriteValueCallback : public WebBluetoothWriteValueCallbacks { | 195 class WriteValueCallback : public WebBluetoothWriteValueCallbacks { |
| 196 public: | 196 public: |
| 197 WriteValueCallback(BluetoothRemoteGATTCharacteristic* characteristic, | 197 WriteValueCallback(BluetoothRemoteGATTCharacteristic* characteristic, |
| 198 ScriptPromiseResolver* resolver) | 198 ScriptPromiseResolver* resolver) |
| 199 : m_webCharacteristic(characteristic), m_resolver(resolver) {} | 199 : m_characteristic(characteristic), m_resolver(resolver) { |
| 200 // We always check that the device is connected before constructing this |
| 201 // object. |
| 202 CHECK(m_characteristic->gatt()->connected()); |
| 203 m_characteristic->gatt()->AddToActiveAlgorithms(m_resolver.get()); |
| 204 } |
| 200 | 205 |
| 201 void onSuccess(const WebVector<uint8_t>& value) override { | 206 void onSuccess(const WebVector<uint8_t>& value) override { |
| 202 if (!m_resolver->getExecutionContext() || | 207 if (!m_resolver->getExecutionContext() || |
| 203 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) | 208 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
| 204 return; | 209 return; |
| 205 | 210 |
| 206 if (m_webCharacteristic) { | 211 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms( |
| 207 m_webCharacteristic->setValue(ConvertWebVectorToDataView(value)); | 212 m_resolver.get())) { |
| 213 m_resolver->reject( |
| 214 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 215 return; |
| 216 } |
| 217 |
| 218 if (m_characteristic) { |
| 219 m_characteristic->setValue(ConvertWebVectorToDataView(value)); |
| 208 } | 220 } |
| 209 m_resolver->resolve(); | 221 m_resolver->resolve(); |
| 210 } | 222 } |
| 211 | 223 |
| 212 void onError( | 224 void onError( |
| 213 int32_t | 225 int32_t |
| 214 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) | 226 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) |
| 215 override { | 227 override { |
| 216 if (!m_resolver->getExecutionContext() || | 228 if (!m_resolver->getExecutionContext() || |
| 217 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) | 229 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
| 218 return; | 230 return; |
| 231 |
| 232 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms( |
| 233 m_resolver.get())) { |
| 234 m_resolver->reject( |
| 235 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 236 return; |
| 237 } |
| 238 |
| 219 m_resolver->reject(BluetoothError::take(m_resolver, error)); | 239 m_resolver->reject(BluetoothError::take(m_resolver, error)); |
| 220 } | 240 } |
| 221 | 241 |
| 222 private: | 242 private: |
| 223 WeakPersistent<BluetoothRemoteGATTCharacteristic> m_webCharacteristic; | 243 WeakPersistent<BluetoothRemoteGATTCharacteristic> m_characteristic; |
| 224 Persistent<ScriptPromiseResolver> m_resolver; | 244 Persistent<ScriptPromiseResolver> m_resolver; |
| 225 }; | 245 }; |
| 226 | 246 |
| 227 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue( | 247 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue( |
| 228 ScriptState* scriptState, | 248 ScriptState* scriptState, |
| 229 const DOMArrayPiece& value) { | 249 const DOMArrayPiece& value) { |
| 250 if (!gatt()->connected()) { |
| 251 return ScriptPromise::rejectWithDOMException( |
| 252 scriptState, |
| 253 DOMException::create(NetworkError, kGATTServerNotConnected)); |
| 254 } |
| 255 |
| 230 WebBluetooth* webbluetooth = | 256 WebBluetooth* webbluetooth = |
| 231 BluetoothSupplement::fromScriptState(scriptState); | 257 BluetoothSupplement::fromScriptState(scriptState); |
| 232 // Partial implementation of writeValue algorithm: | 258 // Partial implementation of writeValue algorithm: |
| 233 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothgattcharac
teristic-writevalue | 259 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothgattcharac
teristic-writevalue |
| 234 | 260 |
| 235 // If bytes is more than 512 bytes long (the maximum length of an attribute | 261 // If bytes is more than 512 bytes long (the maximum length of an attribute |
| 236 // value, per Long Attribute Values) return a promise rejected with an | 262 // value, per Long Attribute Values) return a promise rejected with an |
| 237 // InvalidModificationError and abort. | 263 // InvalidModificationError and abort. |
| 238 if (value.byteLength() > 512) | 264 if (value.byteLength() > 512) |
| 239 return ScriptPromise::rejectWithDOMException( | 265 return ScriptPromise::rejectWithDOMException( |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 | 340 |
| 315 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { | 341 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { |
| 316 visitor->trace(m_service); | 342 visitor->trace(m_service); |
| 317 visitor->trace(m_properties); | 343 visitor->trace(m_properties); |
| 318 visitor->trace(m_value); | 344 visitor->trace(m_value); |
| 319 EventTargetWithInlineData::trace(visitor); | 345 EventTargetWithInlineData::trace(visitor); |
| 320 ActiveDOMObject::trace(visitor); | 346 ActiveDOMObject::trace(visitor); |
| 321 } | 347 } |
| 322 | 348 |
| 323 } // namespace blink | 349 } // namespace blink |
| OLD | NEW |