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

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

Issue 2615093002: Typemap WebBluetoothDeviceId to WTF::String (Closed)
Patch Set: merge master Created 3 years, 11 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/DOMDataView.h" 9 #include "core/dom/DOMDataView.h"
10 #include "core/dom/DOMException.h" 10 #include "core/dom/DOMException.h"
11 #include "core/dom/ExceptionCode.h" 11 #include "core/dom/ExceptionCode.h"
12 #include "core/events/Event.h" 12 #include "core/events/Event.h"
13 #include "core/inspector/ConsoleMessage.h" 13 #include "core/inspector/ConsoleMessage.h"
14 #include "modules/bluetooth/Bluetooth.h" 14 #include "modules/bluetooth/Bluetooth.h"
15 #include "modules/bluetooth/BluetoothCharacteristicProperties.h" 15 #include "modules/bluetooth/BluetoothCharacteristicProperties.h"
16 #include "modules/bluetooth/BluetoothDevice.h" 16 #include "modules/bluetooth/BluetoothDevice.h"
17 #include "modules/bluetooth/BluetoothError.h" 17 #include "modules/bluetooth/BluetoothError.h"
18 #include "modules/bluetooth/BluetoothRemoteGATTService.h" 18 #include "modules/bluetooth/BluetoothRemoteGATTService.h"
19 #include <memory> 19 #include <utility>
20 20
21 namespace blink { 21 namespace blink {
22 22
23 namespace { 23 namespace {
24 24
25 const char kGATTServerDisconnected[] = 25 const char kGATTServerDisconnected[] =
26 "GATT Server disconnected while performing a GATT operation."; 26 "GATT Server disconnected while performing a GATT operation.";
27 const char kGATTServerNotConnected[] = 27 const char kGATTServerNotConnected[] =
28 "GATT Server is disconnected. Cannot perform GATT operations."; 28 "GATT Server is disconnected. Cannot perform GATT operations.";
29 const char kInvalidCharacteristic[] = 29 const char kInvalidCharacteristic[] =
30 "Characteristic is no longer valid. Remember to retrieve the " 30 "Characteristic is no longer valid. Remember to retrieve the "
31 "characteristic again after reconnecting."; 31 "characteristic again after reconnecting.";
32 32
33 DOMDataView* ConvertWTFVectorToDataView(const Vector<uint8_t>& wtfVector) { 33 DOMDataView* ConvertWTFVectorToDataView(const Vector<uint8_t>& wtfVector) {
34 static_assert(sizeof(*wtfVector.data()) == 1, 34 static_assert(sizeof(*wtfVector.data()) == 1,
35 "uint8_t should be a single byte"); 35 "uint8_t should be a single byte");
36 DOMArrayBuffer* domBuffer = 36 DOMArrayBuffer* domBuffer =
37 DOMArrayBuffer::create(wtfVector.data(), wtfVector.size()); 37 DOMArrayBuffer::create(wtfVector.data(), wtfVector.size());
38 return DOMDataView::create(domBuffer, 0, wtfVector.size()); 38 return DOMDataView::create(domBuffer, 0, wtfVector.size());
39 } 39 }
40 40
41 } // anonymous namespace 41 } // anonymous namespace
42 42
43 BluetoothRemoteGATTCharacteristic::BluetoothRemoteGATTCharacteristic( 43 BluetoothRemoteGATTCharacteristic::BluetoothRemoteGATTCharacteristic(
44 ExecutionContext* context, 44 ExecutionContext* context,
45 const String& characteristicInstanceId,
46 const String& serviceInstanceId, 45 const String& serviceInstanceId,
47 const String& uuid, 46 mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr characteristic,
48 uint32_t characteristicProperties,
49 BluetoothRemoteGATTService* service, 47 BluetoothRemoteGATTService* service,
50 BluetoothDevice* device) 48 BluetoothDevice* device)
51 : ContextLifecycleObserver(context), 49 : ContextLifecycleObserver(context),
52 m_characteristicInstanceId(characteristicInstanceId),
53 m_serviceInstanceId(serviceInstanceId), 50 m_serviceInstanceId(serviceInstanceId),
54 m_uuid(uuid), 51 m_characteristic(std::move(characteristic)),
55 m_characteristicProperties(characteristicProperties),
56 m_service(service), 52 m_service(service),
57 m_stopped(false), 53 m_stopped(false),
58 m_device(device) { 54 m_device(device) {
59 m_properties = 55 m_properties =
60 BluetoothCharacteristicProperties::create(m_characteristicProperties); 56 BluetoothCharacteristicProperties::create(m_characteristic->properties);
61 } 57 }
62 58
63 BluetoothRemoteGATTCharacteristic* BluetoothRemoteGATTCharacteristic::create( 59 BluetoothRemoteGATTCharacteristic* BluetoothRemoteGATTCharacteristic::create(
64 ExecutionContext* context, 60 ExecutionContext* context,
65 const String& characteristicInstanceId,
66 const String& serviceInstanceId, 61 const String& serviceInstanceId,
67 const String& uuid, 62 mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr characteristic,
68 uint32_t characteristicProperties,
69 BluetoothRemoteGATTService* service, 63 BluetoothRemoteGATTService* service,
70 BluetoothDevice* device) { 64 BluetoothDevice* device) {
71 return new BluetoothRemoteGATTCharacteristic( 65 return new BluetoothRemoteGATTCharacteristic(
72 context, characteristicInstanceId, serviceInstanceId, uuid, 66 context, serviceInstanceId, std::move(characteristic), service, device);
73 characteristicProperties, service, device);
74 } 67 }
75 68
76 void BluetoothRemoteGATTCharacteristic::setValue(DOMDataView* domDataView) { 69 void BluetoothRemoteGATTCharacteristic::setValue(DOMDataView* domDataView) {
77 m_value = domDataView; 70 m_value = domDataView;
78 } 71 }
79 72
80 void BluetoothRemoteGATTCharacteristic::dispatchCharacteristicValueChanged( 73 void BluetoothRemoteGATTCharacteristic::dispatchCharacteristicValueChanged(
81 const Vector<uint8_t>& value) { 74 const Vector<uint8_t>& value) {
82 this->setValue(ConvertWTFVectorToDataView(value)); 75 this->setValue(ConvertWTFVectorToDataView(value));
83 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); 76 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged));
84 } 77 }
85 78
86 void BluetoothRemoteGATTCharacteristic::contextDestroyed() { 79 void BluetoothRemoteGATTCharacteristic::contextDestroyed() {
87 notifyCharacteristicObjectRemoved(); 80 notifyCharacteristicObjectRemoved();
88 } 81 }
89 82
90 void BluetoothRemoteGATTCharacteristic::dispose() { 83 void BluetoothRemoteGATTCharacteristic::dispose() {
91 notifyCharacteristicObjectRemoved(); 84 notifyCharacteristicObjectRemoved();
92 } 85 }
93 86
94 void BluetoothRemoteGATTCharacteristic::notifyCharacteristicObjectRemoved() { 87 void BluetoothRemoteGATTCharacteristic::notifyCharacteristicObjectRemoved() {
95 if (!m_stopped) { 88 if (!m_stopped) {
96 m_stopped = true; 89 m_stopped = true;
97 m_device->bluetooth()->characteristicObjectRemoved( 90 m_device->bluetooth()->characteristicObjectRemoved(
98 m_characteristicInstanceId); 91 m_characteristic->instance_id);
99 } 92 }
100 } 93 }
101 94
102 const WTF::AtomicString& BluetoothRemoteGATTCharacteristic::interfaceName() 95 const WTF::AtomicString& BluetoothRemoteGATTCharacteristic::interfaceName()
103 const { 96 const {
104 return EventTargetNames::BluetoothRemoteGATTCharacteristic; 97 return EventTargetNames::BluetoothRemoteGATTCharacteristic;
105 } 98 }
106 99
107 ExecutionContext* BluetoothRemoteGATTCharacteristic::getExecutionContext() 100 ExecutionContext* BluetoothRemoteGATTCharacteristic::getExecutionContext()
108 const { 101 const {
109 return ContextLifecycleObserver::getExecutionContext(); 102 return ContextLifecycleObserver::getExecutionContext();
110 } 103 }
111 104
112 void BluetoothRemoteGATTCharacteristic::addedEventListener( 105 void BluetoothRemoteGATTCharacteristic::addedEventListener(
113 const AtomicString& eventType, 106 const AtomicString& eventType,
114 RegisteredEventListener& registeredListener) { 107 RegisteredEventListener& registeredListener) {
115 EventTargetWithInlineData::addedEventListener(eventType, registeredListener); 108 EventTargetWithInlineData::addedEventListener(eventType, registeredListener);
116 // We will also need to unregister a characteristic once all the event 109 // We will also need to unregister a characteristic once all the event
117 // listeners have been removed. See http://crbug.com/541390 110 // listeners have been removed. See http://crbug.com/541390
118 if (eventType == EventTypeNames::characteristicvaluechanged) { 111 if (eventType == EventTypeNames::characteristicvaluechanged) {
119 m_device->bluetooth()->registerCharacteristicObject( 112 m_device->bluetooth()->registerCharacteristicObject(
120 m_characteristicInstanceId, this); 113 m_characteristic->instance_id, this);
121 } 114 }
122 } 115 }
123 116
124 void BluetoothRemoteGATTCharacteristic::ReadValueCallback( 117 void BluetoothRemoteGATTCharacteristic::ReadValueCallback(
125 ScriptPromiseResolver* resolver, 118 ScriptPromiseResolver* resolver,
126 mojom::blink::WebBluetoothResult result, 119 mojom::blink::WebBluetoothResult result,
127 const Optional<Vector<uint8_t>>& value) { 120 const Optional<Vector<uint8_t>>& value) {
128 if (!resolver->getExecutionContext() || 121 if (!resolver->getExecutionContext() ||
129 resolver->getExecutionContext()->isContextDestroyed()) 122 resolver->getExecutionContext()->isContextDestroyed())
130 return; 123 return;
(...skipping 18 matching lines...) Expand all
149 142
150 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( 143 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue(
151 ScriptState* scriptState) { 144 ScriptState* scriptState) {
152 // We always check that the device is connected. 145 // We always check that the device is connected.
153 if (!gatt()->connected()) { 146 if (!gatt()->connected()) {
154 return ScriptPromise::rejectWithDOMException( 147 return ScriptPromise::rejectWithDOMException(
155 scriptState, 148 scriptState,
156 DOMException::create(NetworkError, kGATTServerNotConnected)); 149 DOMException::create(NetworkError, kGATTServerNotConnected));
157 } 150 }
158 151
159 if (!gatt()->device()->isValidCharacteristic(m_characteristicInstanceId)) { 152 if (!gatt()->device()->isValidCharacteristic(m_characteristic->instance_id)) {
160 return ScriptPromise::rejectWithDOMException( 153 return ScriptPromise::rejectWithDOMException(
161 scriptState, 154 scriptState,
162 DOMException::create(InvalidStateError, kInvalidCharacteristic)); 155 DOMException::create(InvalidStateError, kInvalidCharacteristic));
163 } 156 }
164 157
165 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 158 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
166 ScriptPromise promise = resolver->promise(); 159 ScriptPromise promise = resolver->promise();
167 gatt()->AddToActiveAlgorithms(resolver); 160 gatt()->AddToActiveAlgorithms(resolver);
168 161
169 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); 162 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service();
170 service->RemoteCharacteristicReadValue( 163 service->RemoteCharacteristicReadValue(
171 m_characteristicInstanceId, 164 m_characteristic->instance_id,
172 convertToBaseCallback( 165 convertToBaseCallback(
173 WTF::bind(&BluetoothRemoteGATTCharacteristic::ReadValueCallback, 166 WTF::bind(&BluetoothRemoteGATTCharacteristic::ReadValueCallback,
174 wrapPersistent(this), wrapPersistent(resolver)))); 167 wrapPersistent(this), wrapPersistent(resolver))));
175 168
176 return promise; 169 return promise;
177 } 170 }
178 171
179 void BluetoothRemoteGATTCharacteristic::WriteValueCallback( 172 void BluetoothRemoteGATTCharacteristic::WriteValueCallback(
180 ScriptPromiseResolver* resolver, 173 ScriptPromiseResolver* resolver,
181 const Vector<uint8_t>& value, 174 const Vector<uint8_t>& value,
(...skipping 21 matching lines...) Expand all
203 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue( 196 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue(
204 ScriptState* scriptState, 197 ScriptState* scriptState,
205 const DOMArrayPiece& value) { 198 const DOMArrayPiece& value) {
206 // We always check that the device is connected. 199 // We always check that the device is connected.
207 if (!gatt()->connected()) { 200 if (!gatt()->connected()) {
208 return ScriptPromise::rejectWithDOMException( 201 return ScriptPromise::rejectWithDOMException(
209 scriptState, 202 scriptState,
210 DOMException::create(NetworkError, kGATTServerNotConnected)); 203 DOMException::create(NetworkError, kGATTServerNotConnected));
211 } 204 }
212 205
213 if (!gatt()->device()->isValidCharacteristic(m_characteristicInstanceId)) { 206 if (!gatt()->device()->isValidCharacteristic(m_characteristic->instance_id)) {
214 return ScriptPromise::rejectWithDOMException( 207 return ScriptPromise::rejectWithDOMException(
215 scriptState, 208 scriptState,
216 DOMException::create(InvalidStateError, kInvalidCharacteristic)); 209 DOMException::create(InvalidStateError, kInvalidCharacteristic));
217 } 210 }
218 211
219 // Partial implementation of writeValue algorithm: 212 // Partial implementation of writeValue algorithm:
220 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattchar acteristic-writevalue 213 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattchar acteristic-writevalue
221 214
222 // If bytes is more than 512 bytes long (the maximum length of an attribute 215 // If bytes is more than 512 bytes long (the maximum length of an attribute
223 // value, per Long Attribute Values) return a promise rejected with an 216 // value, per Long Attribute Values) return a promise rejected with an
224 // InvalidModificationError and abort. 217 // InvalidModificationError and abort.
225 if (value.byteLength() > 512) 218 if (value.byteLength() > 512)
226 return ScriptPromise::rejectWithDOMException( 219 return ScriptPromise::rejectWithDOMException(
227 scriptState, DOMException::create(InvalidModificationError, 220 scriptState, DOMException::create(InvalidModificationError,
228 "Value can't exceed 512 bytes.")); 221 "Value can't exceed 512 bytes."));
229 222
230 // Let valueVector be a copy of the bytes held by value. 223 // Let valueVector be a copy of the bytes held by value.
231 Vector<uint8_t> valueVector; 224 Vector<uint8_t> valueVector;
232 valueVector.append(value.bytes(), value.byteLength()); 225 valueVector.append(value.bytes(), value.byteLength());
233 226
234 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 227 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
235 ScriptPromise promise = resolver->promise(); 228 ScriptPromise promise = resolver->promise();
236 gatt()->AddToActiveAlgorithms(resolver); 229 gatt()->AddToActiveAlgorithms(resolver);
237 230
238 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); 231 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service();
239 service->RemoteCharacteristicWriteValue( 232 service->RemoteCharacteristicWriteValue(
240 m_characteristicInstanceId, valueVector, 233 m_characteristic->instance_id, valueVector,
241 convertToBaseCallback(WTF::bind( 234 convertToBaseCallback(WTF::bind(
242 &BluetoothRemoteGATTCharacteristic::WriteValueCallback, 235 &BluetoothRemoteGATTCharacteristic::WriteValueCallback,
243 wrapPersistent(this), wrapPersistent(resolver), valueVector))); 236 wrapPersistent(this), wrapPersistent(resolver), valueVector)));
244 237
245 return promise; 238 return promise;
246 } 239 }
247 240
248 void BluetoothRemoteGATTCharacteristic::NotificationsCallback( 241 void BluetoothRemoteGATTCharacteristic::NotificationsCallback(
249 ScriptPromiseResolver* resolver, 242 ScriptPromiseResolver* resolver,
250 mojom::blink::WebBluetoothResult result) { 243 mojom::blink::WebBluetoothResult result) {
(...skipping 18 matching lines...) Expand all
269 262
270 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( 263 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications(
271 ScriptState* scriptState) { 264 ScriptState* scriptState) {
272 // We always check that the device is connected. 265 // We always check that the device is connected.
273 if (!gatt()->connected()) { 266 if (!gatt()->connected()) {
274 return ScriptPromise::rejectWithDOMException( 267 return ScriptPromise::rejectWithDOMException(
275 scriptState, 268 scriptState,
276 DOMException::create(NetworkError, kGATTServerNotConnected)); 269 DOMException::create(NetworkError, kGATTServerNotConnected));
277 } 270 }
278 271
279 if (!gatt()->device()->isValidCharacteristic(m_characteristicInstanceId)) { 272 if (!gatt()->device()->isValidCharacteristic(m_characteristic->instance_id)) {
280 return ScriptPromise::rejectWithDOMException( 273 return ScriptPromise::rejectWithDOMException(
281 scriptState, 274 scriptState,
282 DOMException::create(InvalidStateError, kInvalidCharacteristic)); 275 DOMException::create(InvalidStateError, kInvalidCharacteristic));
283 } 276 }
284 277
285 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 278 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
286 ScriptPromise promise = resolver->promise(); 279 ScriptPromise promise = resolver->promise();
287 gatt()->AddToActiveAlgorithms(resolver); 280 gatt()->AddToActiveAlgorithms(resolver);
288 281
289 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); 282 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service();
290 service->RemoteCharacteristicStartNotifications( 283 service->RemoteCharacteristicStartNotifications(
291 m_characteristicInstanceId, 284 m_characteristic->instance_id,
292 convertToBaseCallback( 285 convertToBaseCallback(
293 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, 286 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback,
294 wrapPersistent(this), wrapPersistent(resolver)))); 287 wrapPersistent(this), wrapPersistent(resolver))));
295 288
296 return promise; 289 return promise;
297 } 290 }
298 291
299 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications( 292 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications(
300 ScriptState* scriptState) { 293 ScriptState* scriptState) {
301 // We always check that the device is connected. 294 // We always check that the device is connected.
302 if (!gatt()->connected()) { 295 if (!gatt()->connected()) {
303 return ScriptPromise::rejectWithDOMException( 296 return ScriptPromise::rejectWithDOMException(
304 scriptState, 297 scriptState,
305 DOMException::create(NetworkError, kGATTServerNotConnected)); 298 DOMException::create(NetworkError, kGATTServerNotConnected));
306 } 299 }
307 300
308 if (!gatt()->device()->isValidCharacteristic(m_characteristicInstanceId)) { 301 if (!gatt()->device()->isValidCharacteristic(m_characteristic->instance_id)) {
309 return ScriptPromise::rejectWithDOMException( 302 return ScriptPromise::rejectWithDOMException(
310 scriptState, 303 scriptState,
311 DOMException::create(InvalidStateError, kInvalidCharacteristic)); 304 DOMException::create(InvalidStateError, kInvalidCharacteristic));
312 } 305 }
313 306
314 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 307 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
315 ScriptPromise promise = resolver->promise(); 308 ScriptPromise promise = resolver->promise();
316 gatt()->AddToActiveAlgorithms(resolver); 309 gatt()->AddToActiveAlgorithms(resolver);
317 310
318 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); 311 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service();
319 service->RemoteCharacteristicStopNotifications( 312 service->RemoteCharacteristicStopNotifications(
320 m_characteristicInstanceId, 313 m_characteristic->instance_id,
321 convertToBaseCallback( 314 convertToBaseCallback(
322 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, 315 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback,
323 wrapPersistent(this), wrapPersistent(resolver), 316 wrapPersistent(this), wrapPersistent(resolver),
324 mojom::blink::WebBluetoothResult::SUCCESS))); 317 mojom::blink::WebBluetoothResult::SUCCESS)));
325 return promise; 318 return promise;
326 } 319 }
327 320
328 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { 321 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) {
329 visitor->trace(m_service); 322 visitor->trace(m_service);
330 visitor->trace(m_properties); 323 visitor->trace(m_properties);
331 visitor->trace(m_value); 324 visitor->trace(m_value);
332 visitor->trace(m_device); 325 visitor->trace(m_device);
333 EventTargetWithInlineData::trace(visitor); 326 EventTargetWithInlineData::trace(visitor);
334 ContextLifecycleObserver::trace(visitor); 327 ContextLifecycleObserver::trace(visitor);
335 } 328 }
336 329
337 } // namespace blink 330 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698