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), | |
35 m_device(device) { | 34 m_device(device) { |
36 m_properties = | 35 m_properties = |
37 BluetoothCharacteristicProperties::Create(m_characteristic->properties); | 36 BluetoothCharacteristicProperties::Create(m_characteristic->properties); |
38 } | 37 } |
39 | 38 |
40 BluetoothRemoteGATTCharacteristic* BluetoothRemoteGATTCharacteristic::Create( | 39 BluetoothRemoteGATTCharacteristic* BluetoothRemoteGATTCharacteristic::Create( |
41 ExecutionContext* context, | 40 ExecutionContext* context, |
42 mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr characteristic, | 41 mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr characteristic, |
43 BluetoothRemoteGATTService* service, | 42 BluetoothRemoteGATTService* service, |
44 BluetoothDevice* device) { | 43 BluetoothDevice* device) { |
45 return new BluetoothRemoteGATTCharacteristic( | 44 return new BluetoothRemoteGATTCharacteristic( |
46 context, std::move(characteristic), service, device); | 45 context, std::move(characteristic), service, device); |
47 } | 46 } |
48 | 47 |
49 void BluetoothRemoteGATTCharacteristic::SetValue(DOMDataView* domDataView) { | 48 void BluetoothRemoteGATTCharacteristic::SetValue(DOMDataView* domDataView) { |
50 m_value = domDataView; | 49 m_value = domDataView; |
51 } | 50 } |
52 | 51 |
53 void BluetoothRemoteGATTCharacteristic::DispatchCharacteristicValueChanged( | 52 void BluetoothRemoteGATTCharacteristic::RemoteCharacteristicValueChanged( |
54 const Vector<uint8_t>& value) { | 53 const Vector<uint8_t>& value) { |
55 if (!GetGatt()->connected()) | 54 if (!GetGatt()->connected()) |
56 return; | 55 return; |
57 this->SetValue(BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value)); | 56 this->SetValue(BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value)); |
58 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); | 57 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); |
59 } | 58 } |
60 | 59 |
61 void BluetoothRemoteGATTCharacteristic::contextDestroyed(ExecutionContext*) { | 60 void BluetoothRemoteGATTCharacteristic::contextDestroyed(ExecutionContext*) { |
62 NotifyCharacteristicObjectRemoved(); | 61 Dispose(); |
63 } | 62 } |
64 | 63 |
65 void BluetoothRemoteGATTCharacteristic::Dispose() { | 64 void BluetoothRemoteGATTCharacteristic::Dispose() { |
66 NotifyCharacteristicObjectRemoved(); | 65 CloseClientBindings(); |
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 } | 66 } |
76 | 67 |
77 const WTF::AtomicString& BluetoothRemoteGATTCharacteristic::interfaceName() | 68 const WTF::AtomicString& BluetoothRemoteGATTCharacteristic::interfaceName() |
78 const { | 69 const { |
79 return EventTargetNames::BluetoothRemoteGATTCharacteristic; | 70 return EventTargetNames::BluetoothRemoteGATTCharacteristic; |
80 } | 71 } |
81 | 72 |
82 ExecutionContext* BluetoothRemoteGATTCharacteristic::getExecutionContext() | 73 ExecutionContext* BluetoothRemoteGATTCharacteristic::getExecutionContext() |
83 const { | 74 const { |
84 return ContextLifecycleObserver::getExecutionContext(); | 75 return ContextLifecycleObserver::getExecutionContext(); |
85 } | 76 } |
86 | 77 |
87 void BluetoothRemoteGATTCharacteristic::addedEventListener( | 78 void BluetoothRemoteGATTCharacteristic::addedEventListener( |
88 const AtomicString& eventType, | 79 const AtomicString& eventType, |
89 RegisteredEventListener& registeredListener) { | 80 RegisteredEventListener& registeredListener) { |
90 EventTargetWithInlineData::addedEventListener(eventType, registeredListener); | 81 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 } | 82 } |
98 | 83 |
99 void BluetoothRemoteGATTCharacteristic::ReadValueCallback( | 84 void BluetoothRemoteGATTCharacteristic::ReadValueCallback( |
100 ScriptPromiseResolver* resolver, | 85 ScriptPromiseResolver* resolver, |
101 mojom::blink::WebBluetoothResult result, | 86 mojom::blink::WebBluetoothResult result, |
102 const Optional<Vector<uint8_t>>& value) { | 87 const Optional<Vector<uint8_t>>& value) { |
103 if (!resolver->getExecutionContext() || | 88 if (!resolver->getExecutionContext() || |
104 resolver->getExecutionContext()->isContextDestroyed()) | 89 resolver->getExecutionContext()->isContextDestroyed()) |
105 return; | 90 return; |
106 | 91 |
107 // If the device is disconnected, reject. | 92 // If the device is disconnected, reject. |
108 if (!GetGatt()->RemoveFromActiveAlgorithms(resolver)) { | 93 if (!GetGatt()->RemoveFromActiveAlgorithms(resolver)) { |
109 resolver->reject( | 94 resolver->reject( |
110 BluetoothError::CreateNotConnectedException(BluetoothOperation::GATT)); | 95 BluetoothError::CreateNotConnectedException(BluetoothOperation::GATT)); |
111 return; | 96 return; |
112 } | 97 } |
113 | 98 |
114 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 99 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
115 DCHECK(value); | 100 DCHECK(value); |
116 DOMDataView* domDataView = | 101 DOMDataView* domDataView = |
117 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value()); | 102 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value()); |
118 SetValue(domDataView); | 103 SetValue(domDataView); |
| 104 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); |
119 resolver->resolve(domDataView); | 105 resolver->resolve(domDataView); |
120 } else { | 106 } else { |
121 resolver->reject(BluetoothError::CreateDOMException(result)); | 107 resolver->reject(BluetoothError::CreateDOMException(result)); |
122 } | 108 } |
123 } | 109 } |
124 | 110 |
125 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( | 111 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( |
126 ScriptState* scriptState) { | 112 ScriptState* scriptState) { |
127 if (!GetGatt()->connected()) { | 113 if (!GetGatt()->connected()) { |
128 return ScriptPromise::rejectWithDOMException( | 114 return ScriptPromise::rejectWithDOMException( |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 m_characteristic->instance_id)) { | 236 m_characteristic->instance_id)) { |
251 return ScriptPromise::rejectWithDOMException( | 237 return ScriptPromise::rejectWithDOMException( |
252 scriptState, CreateInvalidCharacteristicError()); | 238 scriptState, CreateInvalidCharacteristicError()); |
253 } | 239 } |
254 | 240 |
255 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 241 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
256 ScriptPromise promise = resolver->promise(); | 242 ScriptPromise promise = resolver->promise(); |
257 GetGatt()->AddToActiveAlgorithms(resolver); | 243 GetGatt()->AddToActiveAlgorithms(resolver); |
258 | 244 |
259 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); | 245 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); |
| 246 mojom::blink::WebBluetoothCharacteristicClientAssociatedPtrInfo ptrInfo; |
| 247 std::unique_ptr< |
| 248 mojo::AssociatedBinding<mojom::blink::WebBluetoothCharacteristicClient>> |
| 249 clientBinding(new mojo::AssociatedBinding< |
| 250 mojom::blink::WebBluetoothCharacteristicClient>(this)); |
| 251 clientBinding->Bind(&ptrInfo); |
| 252 m_clientBindings.push_back(std::move(clientBinding)); |
260 service->RemoteCharacteristicStartNotifications( | 253 service->RemoteCharacteristicStartNotifications( |
261 m_characteristic->instance_id, | 254 m_characteristic->instance_id, std::move(ptrInfo), |
262 convertToBaseCallback( | 255 convertToBaseCallback( |
263 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, | 256 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, |
264 wrapPersistent(this), wrapPersistent(resolver)))); | 257 wrapPersistent(this), wrapPersistent(resolver)))); |
265 | 258 |
266 return promise; | 259 return promise; |
267 } | 260 } |
268 | 261 |
269 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications( | 262 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications( |
270 ScriptState* scriptState) { | 263 ScriptState* scriptState) { |
271 if (!GetGatt()->connected()) { | 264 if (!GetGatt()->connected()) { |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 | 407 |
415 DOMException* | 408 DOMException* |
416 BluetoothRemoteGATTCharacteristic::CreateInvalidCharacteristicError() { | 409 BluetoothRemoteGATTCharacteristic::CreateInvalidCharacteristicError() { |
417 return BluetoothError::CreateDOMException( | 410 return BluetoothError::CreateDOMException( |
418 BluetoothErrorCode::InvalidCharacteristic, | 411 BluetoothErrorCode::InvalidCharacteristic, |
419 "Characteristic with UUID " + uuid() + | 412 "Characteristic with UUID " + uuid() + |
420 " is no longer valid. Remember to retrieve the characteristic again " | 413 " is no longer valid. Remember to retrieve the characteristic again " |
421 "after reconnecting."); | 414 "after reconnecting."); |
422 } | 415 } |
423 | 416 |
| 417 void BluetoothRemoteGATTCharacteristic::CloseClientBindings() { |
| 418 for (auto& clientBinding : m_clientBindings) { |
| 419 clientBinding->Close(); |
| 420 } |
| 421 } |
| 422 |
424 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { | 423 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { |
425 visitor->trace(m_service); | 424 visitor->trace(m_service); |
426 visitor->trace(m_properties); | 425 visitor->trace(m_properties); |
427 visitor->trace(m_value); | 426 visitor->trace(m_value); |
428 visitor->trace(m_device); | 427 visitor->trace(m_device); |
429 EventTargetWithInlineData::trace(visitor); | 428 EventTargetWithInlineData::trace(visitor); |
430 ContextLifecycleObserver::trace(visitor); | 429 ContextLifecycleObserver::trace(visitor); |
431 } | 430 } |
432 | 431 |
433 } // namespace blink | 432 } // namespace blink |
OLD | NEW |