| 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/ScriptPromise.h" | 7 #include "bindings/core/v8/ScriptPromise.h" |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 9 #include "core/dom/DOMException.h" | 9 #include "core/dom/DOMException.h" |
| 10 #include "core/events/Event.h" | 10 #include "core/events/Event.h" |
| 11 #include "core/inspector/ConsoleMessage.h" | 11 #include "core/inspector/ConsoleMessage.h" |
| 12 #include "modules/bluetooth/Bluetooth.h" | 12 #include "modules/bluetooth/Bluetooth.h" |
| 13 #include "modules/bluetooth/BluetoothCharacteristicProperties.h" | 13 #include "modules/bluetooth/BluetoothCharacteristicProperties.h" |
| 14 #include "modules/bluetooth/BluetoothDevice.h" | 14 #include "modules/bluetooth/BluetoothDevice.h" |
| 15 #include "modules/bluetooth/BluetoothError.h" | 15 #include "modules/bluetooth/BluetoothError.h" |
| 16 #include "modules/bluetooth/BluetoothRemoteGATTDescriptor.h" | 16 #include "modules/bluetooth/BluetoothRemoteGATTDescriptor.h" |
| 17 #include "modules/bluetooth/BluetoothRemoteGATTService.h" | 17 #include "modules/bluetooth/BluetoothRemoteGATTService.h" |
| 18 #include "modules/bluetooth/BluetoothRemoteGATTUtils.h" | 18 #include "modules/bluetooth/BluetoothRemoteGATTUtils.h" |
| 19 #include "modules/bluetooth/BluetoothUUID.h" | 19 #include "modules/bluetooth/BluetoothUUID.h" |
| 20 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" |
| 20 | 21 |
| 21 #include <memory> | 22 #include <memory> |
| 22 #include <utility> | 23 #include <utility> |
| 23 | 24 |
| 24 namespace blink { | 25 namespace blink { |
| 25 | 26 |
| 26 BluetoothRemoteGATTCharacteristic::BluetoothRemoteGATTCharacteristic( | 27 BluetoothRemoteGATTCharacteristic::BluetoothRemoteGATTCharacteristic( |
| 27 ExecutionContext* context, | 28 ExecutionContext* context, |
| 28 mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr characteristic, | 29 mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr characteristic, |
| 29 BluetoothRemoteGATTService* service, | 30 BluetoothRemoteGATTService* service, |
| 30 BluetoothDevice* device) | 31 BluetoothDevice* device) |
| 31 : ContextLifecycleObserver(context), | 32 : ContextLifecycleObserver(context), |
| 32 m_characteristic(std::move(characteristic)), | 33 m_characteristic(std::move(characteristic)), |
| 33 m_service(service), | 34 m_service(service), |
| 34 m_stopped(false), | |
| 35 m_device(device) { | 35 m_device(device) { |
| 36 m_properties = | 36 m_properties = |
| 37 BluetoothCharacteristicProperties::Create(m_characteristic->properties); | 37 BluetoothCharacteristicProperties::Create(m_characteristic->properties); |
| 38 } | 38 } |
| 39 | 39 |
| 40 BluetoothRemoteGATTCharacteristic* BluetoothRemoteGATTCharacteristic::Create( | 40 BluetoothRemoteGATTCharacteristic* BluetoothRemoteGATTCharacteristic::Create( |
| 41 ExecutionContext* context, | 41 ExecutionContext* context, |
| 42 mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr characteristic, | 42 mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr characteristic, |
| 43 BluetoothRemoteGATTService* service, | 43 BluetoothRemoteGATTService* service, |
| 44 BluetoothDevice* device) { | 44 BluetoothDevice* device) { |
| 45 return new BluetoothRemoteGATTCharacteristic( | 45 return new BluetoothRemoteGATTCharacteristic( |
| 46 context, std::move(characteristic), service, device); | 46 context, std::move(characteristic), service, device); |
| 47 } | 47 } |
| 48 | 48 |
| 49 void BluetoothRemoteGATTCharacteristic::SetValue(DOMDataView* domDataView) { | 49 void BluetoothRemoteGATTCharacteristic::SetValue(DOMDataView* domDataView) { |
| 50 m_value = domDataView; | 50 m_value = domDataView; |
| 51 } | 51 } |
| 52 | 52 |
| 53 void BluetoothRemoteGATTCharacteristic::DispatchCharacteristicValueChanged( | 53 void BluetoothRemoteGATTCharacteristic::RemoteCharacteristicValueChanged( |
| 54 const Vector<uint8_t>& value) { | 54 const Vector<uint8_t>& value) { |
| 55 if (!GetGatt()->connected()) | 55 if (!GetGatt()->connected()) |
| 56 return; | 56 return; |
| 57 this->SetValue(BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value)); | 57 this->SetValue(BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value)); |
| 58 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); | 58 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); |
| 59 } | 59 } |
| 60 | 60 |
| 61 void BluetoothRemoteGATTCharacteristic::contextDestroyed(ExecutionContext*) { | 61 void BluetoothRemoteGATTCharacteristic::contextDestroyed(ExecutionContext*) { |
| 62 NotifyCharacteristicObjectRemoved(); | 62 Dispose(); |
| 63 } | 63 } |
| 64 | 64 |
| 65 void BluetoothRemoteGATTCharacteristic::Dispose() { | 65 void BluetoothRemoteGATTCharacteristic::Dispose() { |
| 66 NotifyCharacteristicObjectRemoved(); | 66 m_clientBindings.CloseAllBindings(); |
| 67 } | |
| 68 | |
| 69 void BluetoothRemoteGATTCharacteristic::NotifyCharacteristicObjectRemoved() { | |
| 70 if (!m_stopped) { | |
| 71 m_stopped = true; | |
| 72 m_device->bluetooth()->CharacteristicObjectRemoved( | |
| 73 m_characteristic->instance_id); | |
| 74 } | |
| 75 } | 67 } |
| 76 | 68 |
| 77 const WTF::AtomicString& BluetoothRemoteGATTCharacteristic::interfaceName() | 69 const WTF::AtomicString& BluetoothRemoteGATTCharacteristic::interfaceName() |
| 78 const { | 70 const { |
| 79 return EventTargetNames::BluetoothRemoteGATTCharacteristic; | 71 return EventTargetNames::BluetoothRemoteGATTCharacteristic; |
| 80 } | 72 } |
| 81 | 73 |
| 82 ExecutionContext* BluetoothRemoteGATTCharacteristic::getExecutionContext() | 74 ExecutionContext* BluetoothRemoteGATTCharacteristic::getExecutionContext() |
| 83 const { | 75 const { |
| 84 return ContextLifecycleObserver::getExecutionContext(); | 76 return ContextLifecycleObserver::getExecutionContext(); |
| 85 } | 77 } |
| 86 | 78 |
| 87 void BluetoothRemoteGATTCharacteristic::addedEventListener( | 79 void BluetoothRemoteGATTCharacteristic::addedEventListener( |
| 88 const AtomicString& eventType, | 80 const AtomicString& eventType, |
| 89 RegisteredEventListener& registeredListener) { | 81 RegisteredEventListener& registeredListener) { |
| 90 EventTargetWithInlineData::addedEventListener(eventType, registeredListener); | 82 EventTargetWithInlineData::addedEventListener(eventType, registeredListener); |
| 91 // We will also need to unregister a characteristic once all the event | |
| 92 // listeners have been removed. See http://crbug.com/541390 | |
| 93 if (eventType == EventTypeNames::characteristicvaluechanged) { | |
| 94 m_device->bluetooth()->RegisterCharacteristicObject( | |
| 95 m_characteristic->instance_id, this); | |
| 96 } | |
| 97 } | 83 } |
| 98 | 84 |
| 99 void BluetoothRemoteGATTCharacteristic::ReadValueCallback( | 85 void BluetoothRemoteGATTCharacteristic::ReadValueCallback( |
| 100 ScriptPromiseResolver* resolver, | 86 ScriptPromiseResolver* resolver, |
| 101 mojom::blink::WebBluetoothResult result, | 87 mojom::blink::WebBluetoothResult result, |
| 102 const Optional<Vector<uint8_t>>& value) { | 88 const Optional<Vector<uint8_t>>& value) { |
| 103 if (!resolver->getExecutionContext() || | 89 if (!resolver->getExecutionContext() || |
| 104 resolver->getExecutionContext()->isContextDestroyed()) | 90 resolver->getExecutionContext()->isContextDestroyed()) |
| 105 return; | 91 return; |
| 106 | 92 |
| 107 // If the device is disconnected, reject. | 93 // If the device is disconnected, reject. |
| 108 if (!GetGatt()->RemoveFromActiveAlgorithms(resolver)) { | 94 if (!GetGatt()->RemoveFromActiveAlgorithms(resolver)) { |
| 109 resolver->reject( | 95 resolver->reject( |
| 110 BluetoothError::CreateNotConnectedException(BluetoothOperation::GATT)); | 96 BluetoothError::CreateNotConnectedException(BluetoothOperation::GATT)); |
| 111 return; | 97 return; |
| 112 } | 98 } |
| 113 | 99 |
| 114 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 100 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 115 DCHECK(value); | 101 DCHECK(value); |
| 116 DOMDataView* domDataView = | 102 DOMDataView* domDataView = |
| 117 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value()); | 103 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value()); |
| 118 SetValue(domDataView); | 104 SetValue(domDataView); |
| 105 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); |
| 119 resolver->resolve(domDataView); | 106 resolver->resolve(domDataView); |
| 120 } else { | 107 } else { |
| 121 resolver->reject(BluetoothError::CreateDOMException(result)); | 108 resolver->reject(BluetoothError::CreateDOMException(result)); |
| 122 } | 109 } |
| 123 } | 110 } |
| 124 | 111 |
| 125 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( | 112 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( |
| 126 ScriptState* scriptState) { | 113 ScriptState* scriptState) { |
| 127 if (!GetGatt()->connected()) { | 114 if (!GetGatt()->connected()) { |
| 128 return ScriptPromise::rejectWithDOMException( | 115 return ScriptPromise::rejectWithDOMException( |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 m_characteristic->instance_id)) { | 237 m_characteristic->instance_id)) { |
| 251 return ScriptPromise::rejectWithDOMException( | 238 return ScriptPromise::rejectWithDOMException( |
| 252 scriptState, CreateInvalidCharacteristicError()); | 239 scriptState, CreateInvalidCharacteristicError()); |
| 253 } | 240 } |
| 254 | 241 |
| 255 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 242 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 256 ScriptPromise promise = resolver->promise(); | 243 ScriptPromise promise = resolver->promise(); |
| 257 GetGatt()->AddToActiveAlgorithms(resolver); | 244 GetGatt()->AddToActiveAlgorithms(resolver); |
| 258 | 245 |
| 259 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); | 246 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); |
| 247 mojom::blink::WebBluetoothCharacteristicClientAssociatedPtrInfo ptrInfo; |
| 248 auto request = mojo::MakeRequest(&ptrInfo); |
| 249 m_clientBindings.AddBinding(this, std::move(request)); |
| 250 |
| 260 service->RemoteCharacteristicStartNotifications( | 251 service->RemoteCharacteristicStartNotifications( |
| 261 m_characteristic->instance_id, | 252 m_characteristic->instance_id, std::move(ptrInfo), |
| 262 convertToBaseCallback( | 253 convertToBaseCallback( |
| 263 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, | 254 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, |
| 264 wrapPersistent(this), wrapPersistent(resolver)))); | 255 wrapPersistent(this), wrapPersistent(resolver)))); |
| 265 | 256 |
| 266 return promise; | 257 return promise; |
| 267 } | 258 } |
| 268 | 259 |
| 269 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications( | 260 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications( |
| 270 ScriptState* scriptState) { | 261 ScriptState* scriptState) { |
| 271 if (!GetGatt()->connected()) { | 262 if (!GetGatt()->connected()) { |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { | 415 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { |
| 425 visitor->trace(m_service); | 416 visitor->trace(m_service); |
| 426 visitor->trace(m_properties); | 417 visitor->trace(m_properties); |
| 427 visitor->trace(m_value); | 418 visitor->trace(m_value); |
| 428 visitor->trace(m_device); | 419 visitor->trace(m_device); |
| 429 EventTargetWithInlineData::trace(visitor); | 420 EventTargetWithInlineData::trace(visitor); |
| 430 ContextLifecycleObserver::trace(visitor); | 421 ContextLifecycleObserver::trace(visitor); |
| 431 } | 422 } |
| 432 | 423 |
| 433 } // namespace blink | 424 } // namespace blink |
| OLD | NEW |