Chromium Code Reviews| 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" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 namespace blink { | 24 namespace blink { |
| 25 | 25 |
| 26 BluetoothRemoteGATTCharacteristic::BluetoothRemoteGATTCharacteristic( | 26 BluetoothRemoteGATTCharacteristic::BluetoothRemoteGATTCharacteristic( |
| 27 ExecutionContext* context, | 27 ExecutionContext* context, |
| 28 mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr characteristic, | 28 mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr characteristic, |
| 29 BluetoothRemoteGATTService* service, | 29 BluetoothRemoteGATTService* service, |
| 30 BluetoothDevice* device) | 30 BluetoothDevice* device) |
| 31 : ContextLifecycleObserver(context), | 31 : ContextLifecycleObserver(context), |
| 32 m_characteristic(std::move(characteristic)), | 32 m_characteristic(std::move(characteristic)), |
| 33 m_service(service), | 33 m_service(service), |
| 34 m_stopped(false), | 34 m_clientBinding(this), |
| 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::contextDestroyed(ExecutionContext*) { |
| 54 const Vector<uint8_t>& value) { | 54 notifyCharacteristicObjectRemoved(); |
| 55 } | |
| 56 | |
| 57 void BluetoothRemoteGATTCharacteristic::RemoteCharacteristicValueChanged( | |
| 58 const WTF::Vector<uint8_t>& value) { | |
| 55 if (!getGatt()->connected()) | 59 if (!getGatt()->connected()) |
| 56 return; | 60 return; |
| 57 this->setValue(BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value)); | 61 this->setValue(BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value)); |
| 58 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); | 62 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); |
| 59 } | 63 } |
| 60 | 64 |
| 61 void BluetoothRemoteGATTCharacteristic::contextDestroyed(ExecutionContext*) { | |
| 62 notifyCharacteristicObjectRemoved(); | |
| 63 } | |
| 64 | |
| 65 void BluetoothRemoteGATTCharacteristic::dispose() { | 65 void BluetoothRemoteGATTCharacteristic::dispose() { |
| 66 notifyCharacteristicObjectRemoved(); | 66 notifyCharacteristicObjectRemoved(); |
| 67 } | 67 } |
| 68 | 68 |
| 69 void BluetoothRemoteGATTCharacteristic::notifyCharacteristicObjectRemoved() { | 69 void BluetoothRemoteGATTCharacteristic::notifyCharacteristicObjectRemoved() { |
|
ortuno
2017/03/01 04:52:06
Can you change the name of this function? It doesn
juncai
2017/03/02 03:23:49
Done.
| |
| 70 if (!m_stopped) { | 70 if (m_clientBinding.is_bound()) |
| 71 m_stopped = true; | 71 m_clientBinding.Close(); |
| 72 m_device->bluetooth()->characteristicObjectRemoved( | |
| 73 m_characteristic->instance_id); | |
| 74 } | |
| 75 } | 72 } |
| 76 | 73 |
| 77 const WTF::AtomicString& BluetoothRemoteGATTCharacteristic::interfaceName() | 74 const WTF::AtomicString& BluetoothRemoteGATTCharacteristic::interfaceName() |
| 78 const { | 75 const { |
| 79 return EventTargetNames::BluetoothRemoteGATTCharacteristic; | 76 return EventTargetNames::BluetoothRemoteGATTCharacteristic; |
| 80 } | 77 } |
| 81 | 78 |
| 82 ExecutionContext* BluetoothRemoteGATTCharacteristic::getExecutionContext() | 79 ExecutionContext* BluetoothRemoteGATTCharacteristic::getExecutionContext() |
| 83 const { | 80 const { |
| 84 return ContextLifecycleObserver::getExecutionContext(); | 81 return ContextLifecycleObserver::getExecutionContext(); |
| 85 } | 82 } |
| 86 | 83 |
| 87 void BluetoothRemoteGATTCharacteristic::addedEventListener( | 84 void BluetoothRemoteGATTCharacteristic::addedEventListener( |
| 88 const AtomicString& eventType, | 85 const AtomicString& eventType, |
| 89 RegisteredEventListener& registeredListener) { | 86 RegisteredEventListener& registeredListener) { |
| 90 EventTargetWithInlineData::addedEventListener(eventType, registeredListener); | 87 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) { | |
|
ortuno
2017/03/01 04:52:06
Forgot about this. We should keep an eye out for h
juncai
2017/03/02 03:23:49
Thanks!
| |
| 94 m_device->bluetooth()->registerCharacteristicObject( | |
| 95 m_characteristic->instance_id, this); | |
| 96 } | |
| 97 } | 88 } |
| 98 | 89 |
| 99 void BluetoothRemoteGATTCharacteristic::ReadValueCallback( | 90 void BluetoothRemoteGATTCharacteristic::ReadValueCallback( |
| 100 ScriptPromiseResolver* resolver, | 91 ScriptPromiseResolver* resolver, |
| 101 mojom::blink::WebBluetoothResult result, | 92 mojom::blink::WebBluetoothResult result, |
| 102 const Optional<Vector<uint8_t>>& value) { | 93 const Optional<Vector<uint8_t>>& value) { |
| 103 if (!resolver->getExecutionContext() || | 94 if (!resolver->getExecutionContext() || |
| 104 resolver->getExecutionContext()->isContextDestroyed()) | 95 resolver->getExecutionContext()->isContextDestroyed()) |
| 105 return; | 96 return; |
| 106 | 97 |
| 107 // If the device is disconnected, reject. | 98 // If the device is disconnected, reject. |
| 108 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { | 99 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { |
| 109 resolver->reject(BluetoothError::createDOMException( | 100 resolver->reject(BluetoothError::createDOMException( |
| 110 blink::mojom::WebBluetoothResult::GATT_SERVER_DISCONNECTED)); | 101 blink::mojom::WebBluetoothResult::GATT_SERVER_DISCONNECTED)); |
| 111 return; | 102 return; |
| 112 } | 103 } |
| 113 | 104 |
| 114 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 105 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 115 DCHECK(value); | 106 DCHECK(value); |
| 116 DOMDataView* domDataView = | 107 DOMDataView* domDataView = |
| 117 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value()); | 108 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value()); |
| 118 setValue(domDataView); | 109 setValue(domDataView); |
| 110 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); | |
|
ortuno
2017/03/01 04:52:06
This test https://cs.chromium.org/chromium/src/thi
juncai
2017/03/02 03:23:48
I opened an issue for it:
https://bugs.chromium.or
| |
| 119 resolver->resolve(domDataView); | 111 resolver->resolve(domDataView); |
| 120 } else { | 112 } else { |
| 121 resolver->reject(BluetoothError::createDOMException(result)); | 113 resolver->reject(BluetoothError::createDOMException(result)); |
| 122 } | 114 } |
| 123 } | 115 } |
| 124 | 116 |
| 125 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( | 117 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( |
| 126 ScriptState* scriptState) { | 118 ScriptState* scriptState) { |
| 127 if (!getGatt()->connected()) { | 119 if (!getGatt()->connected()) { |
| 128 return ScriptPromise::rejectWithDOMException( | 120 return ScriptPromise::rejectWithDOMException( |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 m_characteristic->instance_id, valueVector, | 206 m_characteristic->instance_id, valueVector, |
| 215 convertToBaseCallback(WTF::bind( | 207 convertToBaseCallback(WTF::bind( |
| 216 &BluetoothRemoteGATTCharacteristic::WriteValueCallback, | 208 &BluetoothRemoteGATTCharacteristic::WriteValueCallback, |
| 217 wrapPersistent(this), wrapPersistent(resolver), valueVector))); | 209 wrapPersistent(this), wrapPersistent(resolver), valueVector))); |
| 218 | 210 |
| 219 return promise; | 211 return promise; |
| 220 } | 212 } |
| 221 | 213 |
| 222 void BluetoothRemoteGATTCharacteristic::NotificationsCallback( | 214 void BluetoothRemoteGATTCharacteristic::NotificationsCallback( |
| 223 ScriptPromiseResolver* resolver, | 215 ScriptPromiseResolver* resolver, |
| 224 mojom::blink::WebBluetoothResult result) { | 216 mojom::blink::WebBluetoothResult result, |
| 217 mojom::blink::WebBluetoothCharacteristicClientAssociatedRequest request) { | |
| 225 if (!resolver->getExecutionContext() || | 218 if (!resolver->getExecutionContext() || |
| 226 resolver->getExecutionContext()->isContextDestroyed()) | 219 resolver->getExecutionContext()->isContextDestroyed()) |
| 227 return; | 220 return; |
| 228 | 221 |
| 229 // If the device is disconnected, reject. | 222 // If the device is disconnected, reject. |
| 230 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { | 223 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { |
| 231 resolver->reject(BluetoothError::createDOMException( | 224 resolver->reject(BluetoothError::createDOMException( |
| 232 blink::mojom::WebBluetoothResult::GATT_SERVER_DISCONNECTED)); | 225 blink::mojom::WebBluetoothResult::GATT_SERVER_DISCONNECTED)); |
| 233 return; | 226 return; |
| 234 } | 227 } |
| 235 | 228 |
| 229 m_clientBinding.Bind(std::move(request)); | |
| 230 | |
| 236 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 231 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 237 resolver->resolve(this); | 232 resolver->resolve(this); |
| 238 } else { | 233 } else { |
| 239 resolver->reject(BluetoothError::createDOMException(result)); | 234 resolver->reject(BluetoothError::createDOMException(result)); |
| 240 } | 235 } |
| 241 } | 236 } |
| 242 | 237 |
| 243 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( | 238 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( |
| 244 ScriptState* scriptState) { | 239 ScriptState* scriptState) { |
| 245 if (!getGatt()->connected()) { | 240 if (!getGatt()->connected()) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 287 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 282 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 288 ScriptPromise promise = resolver->promise(); | 283 ScriptPromise promise = resolver->promise(); |
| 289 getGatt()->AddToActiveAlgorithms(resolver); | 284 getGatt()->AddToActiveAlgorithms(resolver); |
| 290 | 285 |
| 291 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 286 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
| 292 service->RemoteCharacteristicStopNotifications( | 287 service->RemoteCharacteristicStopNotifications( |
| 293 m_characteristic->instance_id, | 288 m_characteristic->instance_id, |
| 294 convertToBaseCallback( | 289 convertToBaseCallback( |
| 295 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, | 290 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, |
| 296 wrapPersistent(this), wrapPersistent(resolver), | 291 wrapPersistent(this), wrapPersistent(resolver), |
| 297 mojom::blink::WebBluetoothResult::SUCCESS))); | 292 mojom::blink::WebBluetoothResult::SUCCESS, nullptr))); |
| 298 return promise; | 293 return promise; |
| 299 } | 294 } |
| 300 | 295 |
| 301 ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptor( | 296 ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptor( |
| 302 ScriptState* scriptState, | 297 ScriptState* scriptState, |
| 303 const StringOrUnsignedLong& descriptorUUID, | 298 const StringOrUnsignedLong& descriptorUUID, |
| 304 ExceptionState& exceptionState) { | 299 ExceptionState& exceptionState) { |
| 305 String descriptor = | 300 String descriptor = |
| 306 BluetoothUUID::getDescriptor(descriptorUUID, exceptionState); | 301 BluetoothUUID::getDescriptor(descriptorUUID, exceptionState); |
| 307 if (exceptionState.hadException()) | 302 if (exceptionState.hadException()) |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 429 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { | 424 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { |
| 430 visitor->trace(m_service); | 425 visitor->trace(m_service); |
| 431 visitor->trace(m_properties); | 426 visitor->trace(m_properties); |
| 432 visitor->trace(m_value); | 427 visitor->trace(m_value); |
| 433 visitor->trace(m_device); | 428 visitor->trace(m_device); |
| 434 EventTargetWithInlineData::trace(visitor); | 429 EventTargetWithInlineData::trace(visitor); |
| 435 ContextLifecycleObserver::trace(visitor); | 430 ContextLifecycleObserver::trace(visitor); |
| 436 } | 431 } |
| 437 | 432 |
| 438 } // namespace blink | 433 } // namespace blink |
| OLD | NEW |