Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTCharacteristic.cpp

Issue 2718583002: Refactor WebBluetoothServiceClient in the web_bluetooth.mojom (Closed)
Patch Set: address more comments Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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(BluetoothError::createDOMException( 96 resolver->reject(BluetoothError::createDOMException(
110 blink::mojom::WebBluetoothResult::GATT_SERVER_DISCONNECTED)); 97 blink::mojom::WebBluetoothResult::GATT_SERVER_DISCONNECTED));
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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 m_characteristic->instance_id, valueVector, 202 m_characteristic->instance_id, valueVector,
215 convertToBaseCallback(WTF::bind( 203 convertToBaseCallback(WTF::bind(
216 &BluetoothRemoteGATTCharacteristic::WriteValueCallback, 204 &BluetoothRemoteGATTCharacteristic::WriteValueCallback,
217 wrapPersistent(this), wrapPersistent(resolver), valueVector))); 205 wrapPersistent(this), wrapPersistent(resolver), valueVector)));
218 206
219 return promise; 207 return promise;
220 } 208 }
221 209
222 void BluetoothRemoteGATTCharacteristic::NotificationsCallback( 210 void BluetoothRemoteGATTCharacteristic::NotificationsCallback(
223 ScriptPromiseResolver* resolver, 211 ScriptPromiseResolver* resolver,
224 mojom::blink::WebBluetoothResult result) { 212 mojom::blink::WebBluetoothResult result,
213 mojom::blink::WebBluetoothCharacteristicClientAssociatedRequest request) {
225 if (!resolver->getExecutionContext() || 214 if (!resolver->getExecutionContext() ||
226 resolver->getExecutionContext()->isContextDestroyed()) 215 resolver->getExecutionContext()->isContextDestroyed())
227 return; 216 return;
228 217
229 // If the device is disconnected, reject. 218 // If the device is disconnected, reject.
230 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { 219 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) {
231 resolver->reject(BluetoothError::createDOMException( 220 resolver->reject(BluetoothError::createDOMException(
232 blink::mojom::WebBluetoothResult::GATT_SERVER_DISCONNECTED)); 221 blink::mojom::WebBluetoothResult::GATT_SERVER_DISCONNECTED));
233 return; 222 return;
234 } 223 }
235 224
225 m_clientBinding.Bind(std::move(request));
ortuno 2017/03/06 11:31:20 Wouldn't we bind an invalid request for: startNot
juncai 2017/03/09 07:30:57 Done.
226
236 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { 227 if (result == mojom::blink::WebBluetoothResult::SUCCESS) {
237 resolver->resolve(this); 228 resolver->resolve(this);
238 } else { 229 } else {
239 resolver->reject(BluetoothError::createDOMException(result)); 230 resolver->reject(BluetoothError::createDOMException(result));
240 } 231 }
241 } 232 }
242 233
243 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( 234 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications(
244 ScriptState* scriptState) { 235 ScriptState* scriptState) {
245 if (!getGatt()->connected()) { 236 if (!getGatt()->connected()) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 278 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
288 ScriptPromise promise = resolver->promise(); 279 ScriptPromise promise = resolver->promise();
289 getGatt()->AddToActiveAlgorithms(resolver); 280 getGatt()->AddToActiveAlgorithms(resolver);
290 281
291 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); 282 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service();
292 service->RemoteCharacteristicStopNotifications( 283 service->RemoteCharacteristicStopNotifications(
293 m_characteristic->instance_id, 284 m_characteristic->instance_id,
294 convertToBaseCallback( 285 convertToBaseCallback(
295 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, 286 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback,
296 wrapPersistent(this), wrapPersistent(resolver), 287 wrapPersistent(this), wrapPersistent(resolver),
297 mojom::blink::WebBluetoothResult::SUCCESS))); 288 mojom::blink::WebBluetoothResult::SUCCESS, nullptr)));
298 return promise; 289 return promise;
299 } 290 }
300 291
301 ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptor( 292 ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptor(
302 ScriptState* scriptState, 293 ScriptState* scriptState,
303 const StringOrUnsignedLong& descriptorUUID, 294 const StringOrUnsignedLong& descriptorUUID,
304 ExceptionState& exceptionState) { 295 ExceptionState& exceptionState) {
305 String descriptor = 296 String descriptor =
306 BluetoothUUID::getDescriptor(descriptorUUID, exceptionState); 297 BluetoothUUID::getDescriptor(descriptorUUID, exceptionState);
307 if (exceptionState.hadException()) 298 if (exceptionState.hadException())
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { 420 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) {
430 visitor->trace(m_service); 421 visitor->trace(m_service);
431 visitor->trace(m_properties); 422 visitor->trace(m_properties);
432 visitor->trace(m_value); 423 visitor->trace(m_value);
433 visitor->trace(m_device); 424 visitor->trace(m_device);
434 EventTargetWithInlineData::trace(visitor); 425 EventTargetWithInlineData::trace(visitor);
435 ContextLifecycleObserver::trace(visitor); 426 ContextLifecycleObserver::trace(visitor);
436 } 427 }
437 428
438 } // namespace blink 429 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698