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) { |
dcheng
2017/03/16 22:20:29
Nit: No WTF:: is needed (this is not currently the
juncai
2017/03/17 00:53:56
Done.
| |
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()) |
dcheng
2017/03/16 22:20:29
Nit: no is_bound()
juncai
2017/03/17 00:53:56
Done.
| |
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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
250 m_characteristic->instance_id)) { | 238 m_characteristic->instance_id)) { |
251 return ScriptPromise::rejectWithDOMException( | 239 return ScriptPromise::rejectWithDOMException( |
252 scriptState, CreateInvalidCharacteristicError()); | 240 scriptState, CreateInvalidCharacteristicError()); |
253 } | 241 } |
254 | 242 |
255 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 243 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
256 ScriptPromise promise = resolver->promise(); | 244 ScriptPromise promise = resolver->promise(); |
257 GetGatt()->AddToActiveAlgorithms(resolver); | 245 GetGatt()->AddToActiveAlgorithms(resolver); |
258 | 246 |
259 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); | 247 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); |
248 mojom::blink::WebBluetoothCharacteristicClientAssociatedPtrInfo ptrInfo; | |
249 m_clientBinding.Bind(&ptrInfo); | |
260 service->RemoteCharacteristicStartNotifications( | 250 service->RemoteCharacteristicStartNotifications( |
261 m_characteristic->instance_id, | 251 m_characteristic->instance_id, std::move(ptrInfo), |
262 convertToBaseCallback( | 252 convertToBaseCallback( |
263 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, | 253 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, |
264 wrapPersistent(this), wrapPersistent(resolver)))); | 254 wrapPersistent(this), wrapPersistent(resolver)))); |
265 | 255 |
266 return promise; | 256 return promise; |
267 } | 257 } |
268 | 258 |
269 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications( | 259 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications( |
270 ScriptState* scriptState) { | 260 ScriptState* scriptState) { |
271 if (!GetGatt()->connected()) { | 261 if (!GetGatt()->connected()) { |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
424 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { | 414 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { |
425 visitor->trace(m_service); | 415 visitor->trace(m_service); |
426 visitor->trace(m_properties); | 416 visitor->trace(m_properties); |
427 visitor->trace(m_value); | 417 visitor->trace(m_value); |
428 visitor->trace(m_device); | 418 visitor->trace(m_device); |
429 EventTargetWithInlineData::trace(visitor); | 419 EventTargetWithInlineData::trace(visitor); |
430 ContextLifecycleObserver::trace(visitor); | 420 ContextLifecycleObserver::trace(visitor); |
431 } | 421 } |
432 | 422 |
433 } // namespace blink | 423 } // namespace blink |
OLD | NEW |