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_device(device), |
35 m_device(device) { | 35 m_clientBinding(this) { |
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 WTF::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 if (m_clientBinding.is_bound()) |
67 } | 67 m_clientBinding.Close(); |
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 } | 68 } |
76 | 69 |
77 const WTF::AtomicString& BluetoothRemoteGATTCharacteristic::interfaceName() | 70 const WTF::AtomicString& BluetoothRemoteGATTCharacteristic::interfaceName() |
78 const { | 71 const { |
79 return EventTargetNames::BluetoothRemoteGATTCharacteristic; | 72 return EventTargetNames::BluetoothRemoteGATTCharacteristic; |
80 } | 73 } |
81 | 74 |
82 ExecutionContext* BluetoothRemoteGATTCharacteristic::getExecutionContext() | 75 ExecutionContext* BluetoothRemoteGATTCharacteristic::getExecutionContext() |
83 const { | 76 const { |
84 return ContextLifecycleObserver::getExecutionContext(); | 77 return ContextLifecycleObserver::getExecutionContext(); |
85 } | 78 } |
86 | 79 |
87 void BluetoothRemoteGATTCharacteristic::addedEventListener( | 80 void BluetoothRemoteGATTCharacteristic::addedEventListener( |
88 const AtomicString& eventType, | 81 const AtomicString& eventType, |
89 RegisteredEventListener& registeredListener) { | 82 RegisteredEventListener& registeredListener) { |
90 EventTargetWithInlineData::addedEventListener(eventType, registeredListener); | 83 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 } | 84 } |
98 | 85 |
99 void BluetoothRemoteGATTCharacteristic::ReadValueCallback( | 86 void BluetoothRemoteGATTCharacteristic::ReadValueCallback( |
100 ScriptPromiseResolver* resolver, | 87 ScriptPromiseResolver* resolver, |
101 mojom::blink::WebBluetoothResult result, | 88 mojom::blink::WebBluetoothResult result, |
102 const Optional<Vector<uint8_t>>& value) { | 89 const Optional<Vector<uint8_t>>& value) { |
103 if (!resolver->getExecutionContext() || | 90 if (!resolver->getExecutionContext() || |
104 resolver->getExecutionContext()->isContextDestroyed()) | 91 resolver->getExecutionContext()->isContextDestroyed()) |
105 return; | 92 return; |
106 | 93 |
107 // If the device is disconnected, reject. | 94 // If the device is disconnected, reject. |
108 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { | 95 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { |
109 resolver->reject( | 96 resolver->reject( |
110 BluetoothError::createNotConnectedException(BluetoothOperation::GATT)); | 97 BluetoothError::createNotConnectedException(BluetoothOperation::GATT)); |
111 return; | 98 return; |
112 } | 99 } |
113 | 100 |
114 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 101 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
115 DCHECK(value); | 102 DCHECK(value); |
116 DOMDataView* domDataView = | 103 DOMDataView* domDataView = |
117 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value()); | 104 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value()); |
118 setValue(domDataView); | 105 setValue(domDataView); |
| 106 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); |
119 resolver->resolve(domDataView); | 107 resolver->resolve(domDataView); |
120 } else { | 108 } else { |
121 resolver->reject(BluetoothError::createDOMException(result)); | 109 resolver->reject(BluetoothError::createDOMException(result)); |
122 } | 110 } |
123 } | 111 } |
124 | 112 |
125 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( | 113 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( |
126 ScriptState* scriptState) { | 114 ScriptState* scriptState) { |
127 if (!getGatt()->connected()) { | 115 if (!getGatt()->connected()) { |
128 return ScriptPromise::rejectWithDOMException( | 116 return ScriptPromise::rejectWithDOMException( |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 m_characteristic->instance_id, valueVector, | 200 m_characteristic->instance_id, valueVector, |
213 convertToBaseCallback(WTF::bind( | 201 convertToBaseCallback(WTF::bind( |
214 &BluetoothRemoteGATTCharacteristic::WriteValueCallback, | 202 &BluetoothRemoteGATTCharacteristic::WriteValueCallback, |
215 wrapPersistent(this), wrapPersistent(resolver), valueVector))); | 203 wrapPersistent(this), wrapPersistent(resolver), valueVector))); |
216 | 204 |
217 return promise; | 205 return promise; |
218 } | 206 } |
219 | 207 |
220 void BluetoothRemoteGATTCharacteristic::NotificationsCallback( | 208 void BluetoothRemoteGATTCharacteristic::NotificationsCallback( |
221 ScriptPromiseResolver* resolver, | 209 ScriptPromiseResolver* resolver, |
222 mojom::blink::WebBluetoothResult result) { | 210 mojom::blink::WebBluetoothResult result, |
| 211 mojom::blink::WebBluetoothCharacteristicClientAssociatedRequest request) { |
223 if (!resolver->getExecutionContext() || | 212 if (!resolver->getExecutionContext() || |
224 resolver->getExecutionContext()->isContextDestroyed()) | 213 resolver->getExecutionContext()->isContextDestroyed()) |
225 return; | 214 return; |
226 | 215 |
227 // If the device is disconnected, reject. | 216 // If the device is disconnected, reject. |
228 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { | 217 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { |
229 resolver->reject( | 218 resolver->reject( |
230 BluetoothError::createNotConnectedException(BluetoothOperation::GATT)); | 219 BluetoothError::createNotConnectedException(BluetoothOperation::GATT)); |
231 return; | 220 return; |
232 } | 221 } |
233 | 222 |
234 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 223 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 224 if (request.is_pending()) |
| 225 m_clientBinding.Bind(std::move(request)); |
235 resolver->resolve(this); | 226 resolver->resolve(this); |
236 } else { | 227 } else { |
237 resolver->reject(BluetoothError::createDOMException(result)); | 228 resolver->reject(BluetoothError::createDOMException(result)); |
238 } | 229 } |
239 } | 230 } |
240 | 231 |
241 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( | 232 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( |
242 ScriptState* scriptState) { | 233 ScriptState* scriptState) { |
243 if (!getGatt()->connected()) { | 234 if (!getGatt()->connected()) { |
244 return ScriptPromise::rejectWithDOMException( | 235 return ScriptPromise::rejectWithDOMException( |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 274 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
284 ScriptPromise promise = resolver->promise(); | 275 ScriptPromise promise = resolver->promise(); |
285 getGatt()->AddToActiveAlgorithms(resolver); | 276 getGatt()->AddToActiveAlgorithms(resolver); |
286 | 277 |
287 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 278 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
288 service->RemoteCharacteristicStopNotifications( | 279 service->RemoteCharacteristicStopNotifications( |
289 m_characteristic->instance_id, | 280 m_characteristic->instance_id, |
290 convertToBaseCallback( | 281 convertToBaseCallback( |
291 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, | 282 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, |
292 wrapPersistent(this), wrapPersistent(resolver), | 283 wrapPersistent(this), wrapPersistent(resolver), |
293 mojom::blink::WebBluetoothResult::SUCCESS))); | 284 mojom::blink::WebBluetoothResult::SUCCESS, nullptr))); |
294 return promise; | 285 return promise; |
295 } | 286 } |
296 | 287 |
297 ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptor( | 288 ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptor( |
298 ScriptState* scriptState, | 289 ScriptState* scriptState, |
299 const StringOrUnsignedLong& descriptorUUID, | 290 const StringOrUnsignedLong& descriptorUUID, |
300 ExceptionState& exceptionState) { | 291 ExceptionState& exceptionState) { |
301 String descriptor = | 292 String descriptor = |
302 BluetoothUUID::getDescriptor(descriptorUUID, exceptionState); | 293 BluetoothUUID::getDescriptor(descriptorUUID, exceptionState); |
303 if (exceptionState.hadException()) | 294 if (exceptionState.hadException()) |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { | 417 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { |
427 visitor->trace(m_service); | 418 visitor->trace(m_service); |
428 visitor->trace(m_properties); | 419 visitor->trace(m_properties); |
429 visitor->trace(m_value); | 420 visitor->trace(m_value); |
430 visitor->trace(m_device); | 421 visitor->trace(m_device); |
431 EventTargetWithInlineData::trace(visitor); | 422 EventTargetWithInlineData::trace(visitor); |
432 ContextLifecycleObserver::trace(visitor); | 423 ContextLifecycleObserver::trace(visitor); |
433 } | 424 } |
434 | 425 |
435 } // namespace blink | 426 } // namespace blink |
OLD | NEW |