| 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 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 webbluetooth->writeValue(m_webCharacteristic->characteristicInstanceID, | 275 webbluetooth->writeValue(m_webCharacteristic->characteristicInstanceID, |
| 276 valueVector, new WriteValueCallback(this, resolver)); | 276 valueVector, new WriteValueCallback(this, resolver)); |
| 277 | 277 |
| 278 return promise; | 278 return promise; |
| 279 } | 279 } |
| 280 | 280 |
| 281 class NotificationsCallback : public WebBluetoothNotificationsCallbacks { | 281 class NotificationsCallback : public WebBluetoothNotificationsCallbacks { |
| 282 public: | 282 public: |
| 283 NotificationsCallback(BluetoothRemoteGATTCharacteristic* characteristic, | 283 NotificationsCallback(BluetoothRemoteGATTCharacteristic* characteristic, |
| 284 ScriptPromiseResolver* resolver) | 284 ScriptPromiseResolver* resolver) |
| 285 : m_webCharacteristic(characteristic), m_resolver(resolver) {} | 285 : m_characteristic(characteristic), m_resolver(resolver) { |
| 286 // We always check that the device is connected before constructing this |
| 287 // object. |
| 288 CHECK(m_characteristic->gatt()->connected()); |
| 289 m_characteristic->gatt()->AddToActiveAlgorithms(m_resolver.get()); |
| 290 } |
| 286 | 291 |
| 287 void onSuccess() override { | 292 void onSuccess() override { |
| 288 if (!m_resolver->getExecutionContext() || | 293 if (!m_resolver->getExecutionContext() || |
| 289 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) | 294 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
| 290 return; | 295 return; |
| 291 | 296 |
| 292 m_resolver->resolve(m_webCharacteristic); | 297 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms( |
| 298 m_resolver.get())) { |
| 299 m_resolver->reject( |
| 300 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 301 return; |
| 302 } |
| 303 |
| 304 m_resolver->resolve(m_characteristic); |
| 293 } | 305 } |
| 294 | 306 |
| 295 void onError( | 307 void onError( |
| 296 int32_t | 308 int32_t |
| 297 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) | 309 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) |
| 298 override { | 310 override { |
| 299 if (!m_resolver->getExecutionContext() || | 311 if (!m_resolver->getExecutionContext() || |
| 300 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) | 312 m_resolver->getExecutionContext()->activeDOMObjectsAreStopped()) |
| 301 return; | 313 return; |
| 314 |
| 315 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms( |
| 316 m_resolver.get())) { |
| 317 m_resolver->reject( |
| 318 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 319 return; |
| 320 } |
| 321 |
| 302 m_resolver->reject(BluetoothError::take(m_resolver, error)); | 322 m_resolver->reject(BluetoothError::take(m_resolver, error)); |
| 303 } | 323 } |
| 304 | 324 |
| 305 private: | 325 private: |
| 306 Persistent<BluetoothRemoteGATTCharacteristic> m_webCharacteristic; | 326 Persistent<BluetoothRemoteGATTCharacteristic> m_characteristic; |
| 307 Persistent<ScriptPromiseResolver> m_resolver; | 327 Persistent<ScriptPromiseResolver> m_resolver; |
| 308 }; | 328 }; |
| 309 | 329 |
| 310 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( | 330 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( |
| 311 ScriptState* scriptState) { | 331 ScriptState* scriptState) { |
| 332 if (!gatt()->connected()) { |
| 333 return ScriptPromise::rejectWithDOMException( |
| 334 scriptState, |
| 335 DOMException::create(NetworkError, kGATTServerNotConnected)); |
| 336 } |
| 337 |
| 312 WebBluetooth* webbluetooth = | 338 WebBluetooth* webbluetooth = |
| 313 BluetoothSupplement::fromScriptState(scriptState); | 339 BluetoothSupplement::fromScriptState(scriptState); |
| 314 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 340 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 315 ScriptPromise promise = resolver->promise(); | 341 ScriptPromise promise = resolver->promise(); |
| 316 webbluetooth->startNotifications( | 342 webbluetooth->startNotifications( |
| 317 m_webCharacteristic->characteristicInstanceID, | 343 m_webCharacteristic->characteristicInstanceID, |
| 318 new NotificationsCallback(this, resolver)); | 344 new NotificationsCallback(this, resolver)); |
| 319 return promise; | 345 return promise; |
| 320 } | 346 } |
| 321 | 347 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 340 | 366 |
| 341 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { | 367 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { |
| 342 visitor->trace(m_service); | 368 visitor->trace(m_service); |
| 343 visitor->trace(m_properties); | 369 visitor->trace(m_properties); |
| 344 visitor->trace(m_value); | 370 visitor->trace(m_value); |
| 345 EventTargetWithInlineData::trace(visitor); | 371 EventTargetWithInlineData::trace(visitor); |
| 346 ActiveDOMObject::trace(visitor); | 372 ActiveDOMObject::trace(visitor); |
| 347 } | 373 } |
| 348 | 374 |
| 349 } // namespace blink | 375 } // namespace blink |
| OLD | NEW |