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

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

Issue 2637343002: Implement WebBluetooth descriptor.readValue() (Closed)
Patch Set: run bad_message_reasons Created 3 years, 10 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/events/Event.h" 9 #include "core/events/Event.h"
10 #include "core/inspector/ConsoleMessage.h" 10 #include "core/inspector/ConsoleMessage.h"
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 } 94 }
95 95
96 void BluetoothRemoteGATTCharacteristic::ReadValueCallback( 96 void BluetoothRemoteGATTCharacteristic::ReadValueCallback(
97 ScriptPromiseResolver* resolver, 97 ScriptPromiseResolver* resolver,
98 mojom::blink::WebBluetoothResult result, 98 mojom::blink::WebBluetoothResult result,
99 const Optional<Vector<uint8_t>>& value) { 99 const Optional<Vector<uint8_t>>& value) {
100 if (!resolver->getExecutionContext() || 100 if (!resolver->getExecutionContext() ||
101 resolver->getExecutionContext()->isContextDestroyed()) 101 resolver->getExecutionContext()->isContextDestroyed())
102 return; 102 return;
103 103
104 // If the resolver is not in the set of ActiveAlgorithms then the frame 104 // If the device is disconnected, reject.
105 // disconnected so we reject.
106 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { 105 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) {
107 resolver->reject(BluetoothRemoteGATTUtils::CreateDOMException( 106 resolver->reject(BluetoothRemoteGATTUtils::CreateDOMException(
108 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerDisconnected)); 107 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerDisconnected));
109 return; 108 return;
110 } 109 }
111 110
112 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { 111 if (result == mojom::blink::WebBluetoothResult::SUCCESS) {
113 DCHECK(value); 112 DCHECK(value);
114 DOMDataView* domDataView = 113 DOMDataView* domDataView =
115 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value()); 114 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value());
116 setValue(domDataView); 115 setValue(domDataView);
117 resolver->resolve(domDataView); 116 resolver->resolve(domDataView);
118 } else { 117 } else {
119 resolver->reject(BluetoothError::take(resolver, result)); 118 resolver->reject(BluetoothError::take(resolver, result));
120 } 119 }
121 } 120 }
122 121
123 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( 122 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue(
124 ScriptState* scriptState) { 123 ScriptState* scriptState) {
125 // We always check that the device is connected.
126 if (!getGatt()->connected()) { 124 if (!getGatt()->connected()) {
127 return ScriptPromise::rejectWithDOMException( 125 return ScriptPromise::rejectWithDOMException(
128 scriptState, 126 scriptState,
129 BluetoothRemoteGATTUtils::CreateDOMException( 127 BluetoothRemoteGATTUtils::CreateDOMException(
130 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerNotConnected)); 128 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerNotConnected));
131 } 129 }
132 130
133 if (!getGatt()->device()->isValidCharacteristic( 131 if (!getGatt()->device()->isValidCharacteristic(
134 m_characteristic->instance_id)) { 132 m_characteristic->instance_id)) {
135 return ScriptPromise::rejectWithDOMException( 133 return ScriptPromise::rejectWithDOMException(
(...skipping 17 matching lines...) Expand all
153 } 151 }
154 152
155 void BluetoothRemoteGATTCharacteristic::WriteValueCallback( 153 void BluetoothRemoteGATTCharacteristic::WriteValueCallback(
156 ScriptPromiseResolver* resolver, 154 ScriptPromiseResolver* resolver,
157 const Vector<uint8_t>& value, 155 const Vector<uint8_t>& value,
158 mojom::blink::WebBluetoothResult result) { 156 mojom::blink::WebBluetoothResult result) {
159 if (!resolver->getExecutionContext() || 157 if (!resolver->getExecutionContext() ||
160 resolver->getExecutionContext()->isContextDestroyed()) 158 resolver->getExecutionContext()->isContextDestroyed())
161 return; 159 return;
162 160
163 // If the resolver is not in the set of ActiveAlgorithms then the frame 161 // If the device is disconnected, reject.
164 // disconnected so we reject.
165 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { 162 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) {
166 resolver->reject(BluetoothRemoteGATTUtils::CreateDOMException( 163 resolver->reject(BluetoothRemoteGATTUtils::CreateDOMException(
167 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerDisconnected)); 164 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerDisconnected));
168 return; 165 return;
169 } 166 }
170 167
171 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { 168 if (result == mojom::blink::WebBluetoothResult::SUCCESS) {
172 setValue(BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value)); 169 setValue(BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value));
173 resolver->resolve(); 170 resolver->resolve();
174 } else { 171 } else {
175 resolver->reject(BluetoothError::take(resolver, result)); 172 resolver->reject(BluetoothError::take(resolver, result));
176 } 173 }
177 } 174 }
178 175
179 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue( 176 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue(
180 ScriptState* scriptState, 177 ScriptState* scriptState,
181 const DOMArrayPiece& value) { 178 const DOMArrayPiece& value) {
182 // We always check that the device is connected.
183 if (!getGatt()->connected()) { 179 if (!getGatt()->connected()) {
184 return ScriptPromise::rejectWithDOMException( 180 return ScriptPromise::rejectWithDOMException(
185 scriptState, 181 scriptState,
186 BluetoothRemoteGATTUtils::CreateDOMException( 182 BluetoothRemoteGATTUtils::CreateDOMException(
187 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerNotConnected)); 183 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerNotConnected));
188 } 184 }
189 185
190 if (!getGatt()->device()->isValidCharacteristic( 186 if (!getGatt()->device()->isValidCharacteristic(
191 m_characteristic->instance_id)) { 187 m_characteristic->instance_id)) {
192 return ScriptPromise::rejectWithDOMException( 188 return ScriptPromise::rejectWithDOMException(
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 return promise; 220 return promise;
225 } 221 }
226 222
227 void BluetoothRemoteGATTCharacteristic::NotificationsCallback( 223 void BluetoothRemoteGATTCharacteristic::NotificationsCallback(
228 ScriptPromiseResolver* resolver, 224 ScriptPromiseResolver* resolver,
229 mojom::blink::WebBluetoothResult result) { 225 mojom::blink::WebBluetoothResult result) {
230 if (!resolver->getExecutionContext() || 226 if (!resolver->getExecutionContext() ||
231 resolver->getExecutionContext()->isContextDestroyed()) 227 resolver->getExecutionContext()->isContextDestroyed())
232 return; 228 return;
233 229
234 // If the resolver is not in the set of ActiveAlgorithms then the frame 230 // If the device is disconnected, reject.
235 // disconnected so we reject.
236 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { 231 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) {
237 resolver->reject(BluetoothRemoteGATTUtils::CreateDOMException( 232 resolver->reject(BluetoothRemoteGATTUtils::CreateDOMException(
238 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerDisconnected)); 233 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerDisconnected));
239 return; 234 return;
240 } 235 }
241 236
242 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { 237 if (result == mojom::blink::WebBluetoothResult::SUCCESS) {
243 resolver->resolve(this); 238 resolver->resolve(this);
244 } else { 239 } else {
245 resolver->reject(BluetoothError::take(resolver, result)); 240 resolver->reject(BluetoothError::take(resolver, result));
246 } 241 }
247 } 242 }
248 243
249 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( 244 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications(
250 ScriptState* scriptState) { 245 ScriptState* scriptState) {
251 // We always check that the device is connected.
252 if (!getGatt()->connected()) { 246 if (!getGatt()->connected()) {
253 return ScriptPromise::rejectWithDOMException( 247 return ScriptPromise::rejectWithDOMException(
254 scriptState, 248 scriptState,
255 BluetoothRemoteGATTUtils::CreateDOMException( 249 BluetoothRemoteGATTUtils::CreateDOMException(
256 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerNotConnected)); 250 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerNotConnected));
257 } 251 }
258 252
259 if (!getGatt()->device()->isValidCharacteristic( 253 if (!getGatt()->device()->isValidCharacteristic(
260 m_characteristic->instance_id)) { 254 m_characteristic->instance_id)) {
261 return ScriptPromise::rejectWithDOMException( 255 return ScriptPromise::rejectWithDOMException(
(...skipping 11 matching lines...) Expand all
273 m_characteristic->instance_id, 267 m_characteristic->instance_id,
274 convertToBaseCallback( 268 convertToBaseCallback(
275 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, 269 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback,
276 wrapPersistent(this), wrapPersistent(resolver)))); 270 wrapPersistent(this), wrapPersistent(resolver))));
277 271
278 return promise; 272 return promise;
279 } 273 }
280 274
281 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications( 275 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications(
282 ScriptState* scriptState) { 276 ScriptState* scriptState) {
283 // We always check that the device is connected.
284 if (!getGatt()->connected()) { 277 if (!getGatt()->connected()) {
285 return ScriptPromise::rejectWithDOMException( 278 return ScriptPromise::rejectWithDOMException(
286 scriptState, 279 scriptState,
287 BluetoothRemoteGATTUtils::CreateDOMException( 280 BluetoothRemoteGATTUtils::CreateDOMException(
288 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerNotConnected)); 281 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerNotConnected));
289 } 282 }
290 283
291 if (!getGatt()->device()->isValidCharacteristic( 284 if (!getGatt()->device()->isValidCharacteristic(
292 m_characteristic->instance_id)) { 285 m_characteristic->instance_id)) {
293 return ScriptPromise::rejectWithDOMException( 286 return ScriptPromise::rejectWithDOMException(
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 const String& characteristicInstanceId, 381 const String& characteristicInstanceId,
389 mojom::blink::WebBluetoothGATTQueryQuantity quantity, 382 mojom::blink::WebBluetoothGATTQueryQuantity quantity,
390 ScriptPromiseResolver* resolver, 383 ScriptPromiseResolver* resolver,
391 mojom::blink::WebBluetoothResult result, 384 mojom::blink::WebBluetoothResult result,
392 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTDescriptorPtr>> 385 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTDescriptorPtr>>
393 descriptors) { 386 descriptors) {
394 if (!resolver->getExecutionContext() || 387 if (!resolver->getExecutionContext() ||
395 resolver->getExecutionContext()->isContextDestroyed()) 388 resolver->getExecutionContext()->isContextDestroyed())
396 return; 389 return;
397 390
398 // If the resolver is not in the set of ActiveAlgorithms then the frame 391 // If the device is disconnected, reject.
399 // disconnected so we reject.
400 if (!service()->device()->gatt()->RemoveFromActiveAlgorithms(resolver)) { 392 if (!service()->device()->gatt()->RemoveFromActiveAlgorithms(resolver)) {
401 resolver->reject(BluetoothRemoteGATTUtils::CreateDOMException( 393 resolver->reject(BluetoothRemoteGATTUtils::CreateDOMException(
402 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerDisconnected)); 394 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerDisconnected));
403 return; 395 return;
404 } 396 }
405 397
406 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { 398 if (result == mojom::blink::WebBluetoothResult::SUCCESS) {
407 DCHECK(descriptors); 399 DCHECK(descriptors);
408 400
409 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { 401 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) {
(...skipping 20 matching lines...) Expand all
430 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { 422 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) {
431 visitor->trace(m_service); 423 visitor->trace(m_service);
432 visitor->trace(m_properties); 424 visitor->trace(m_properties);
433 visitor->trace(m_value); 425 visitor->trace(m_value);
434 visitor->trace(m_device); 426 visitor->trace(m_device);
435 EventTargetWithInlineData::trace(visitor); 427 EventTargetWithInlineData::trace(visitor);
436 ContextLifecycleObserver::trace(visitor); 428 ContextLifecycleObserver::trace(visitor);
437 } 429 }
438 430
439 } // namespace blink 431 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698