| 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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 void BluetoothRemoteGATTCharacteristic::ReadValueCallback( | 99 void BluetoothRemoteGATTCharacteristic::ReadValueCallback( |
| 100 ScriptPromiseResolver* resolver, | 100 ScriptPromiseResolver* resolver, |
| 101 mojom::blink::WebBluetoothResult result, | 101 mojom::blink::WebBluetoothResult result, |
| 102 const Optional<Vector<uint8_t>>& value) { | 102 const Optional<Vector<uint8_t>>& value) { |
| 103 if (!resolver->getExecutionContext() || | 103 if (!resolver->getExecutionContext() || |
| 104 resolver->getExecutionContext()->isContextDestroyed()) | 104 resolver->getExecutionContext()->isContextDestroyed()) |
| 105 return; | 105 return; |
| 106 | 106 |
| 107 // If the device is disconnected, reject. | 107 // If the device is disconnected, reject. |
| 108 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { | 108 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { |
| 109 resolver->reject(BluetoothError::createDOMException( | 109 resolver->reject( |
| 110 blink::mojom::WebBluetoothResult::GATT_SERVER_DISCONNECTED)); | 110 BluetoothError::createNotConnectedException(BluetoothOperation::GATT)); |
| 111 return; | 111 return; |
| 112 } | 112 } |
| 113 | 113 |
| 114 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 114 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 115 DCHECK(value); | 115 DCHECK(value); |
| 116 DOMDataView* domDataView = | 116 DOMDataView* domDataView = |
| 117 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value()); | 117 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value()); |
| 118 setValue(domDataView); | 118 setValue(domDataView); |
| 119 resolver->resolve(domDataView); | 119 resolver->resolve(domDataView); |
| 120 } else { | 120 } else { |
| 121 resolver->reject(BluetoothError::createDOMException(result)); | 121 resolver->reject(BluetoothError::createDOMException(result)); |
| 122 } | 122 } |
| 123 } | 123 } |
| 124 | 124 |
| 125 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( | 125 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( |
| 126 ScriptState* scriptState) { | 126 ScriptState* scriptState) { |
| 127 if (!getGatt()->connected()) { | 127 if (!getGatt()->connected()) { |
| 128 return ScriptPromise::rejectWithDOMException( | 128 return ScriptPromise::rejectWithDOMException( |
| 129 scriptState, | 129 scriptState, |
| 130 BluetoothError::createDOMException( | 130 BluetoothError::createNotConnectedException(BluetoothOperation::GATT)); |
| 131 blink::mojom::WebBluetoothResult::GATT_SERVER_NOT_CONNECTED)); | |
| 132 } | 131 } |
| 133 | 132 |
| 134 if (!getGatt()->device()->isValidCharacteristic( | 133 if (!getGatt()->device()->isValidCharacteristic( |
| 135 m_characteristic->instance_id)) { | 134 m_characteristic->instance_id)) { |
| 136 return ScriptPromise::rejectWithDOMException( | 135 return ScriptPromise::rejectWithDOMException( |
| 137 scriptState, createInvalidCharacteristicError()); | 136 scriptState, createInvalidCharacteristicError()); |
| 138 } | 137 } |
| 139 | 138 |
| 140 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 139 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 141 ScriptPromise promise = resolver->promise(); | 140 ScriptPromise promise = resolver->promise(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 154 void BluetoothRemoteGATTCharacteristic::WriteValueCallback( | 153 void BluetoothRemoteGATTCharacteristic::WriteValueCallback( |
| 155 ScriptPromiseResolver* resolver, | 154 ScriptPromiseResolver* resolver, |
| 156 const Vector<uint8_t>& value, | 155 const Vector<uint8_t>& value, |
| 157 mojom::blink::WebBluetoothResult result) { | 156 mojom::blink::WebBluetoothResult result) { |
| 158 if (!resolver->getExecutionContext() || | 157 if (!resolver->getExecutionContext() || |
| 159 resolver->getExecutionContext()->isContextDestroyed()) | 158 resolver->getExecutionContext()->isContextDestroyed()) |
| 160 return; | 159 return; |
| 161 | 160 |
| 162 // If the device is disconnected, reject. | 161 // If the device is disconnected, reject. |
| 163 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { | 162 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { |
| 164 resolver->reject(BluetoothError::createDOMException( | 163 resolver->reject( |
| 165 blink::mojom::WebBluetoothResult::GATT_SERVER_DISCONNECTED)); | 164 BluetoothError::createNotConnectedException(BluetoothOperation::GATT)); |
| 166 return; | 165 return; |
| 167 } | 166 } |
| 168 | 167 |
| 169 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 168 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 170 setValue(BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value)); | 169 setValue(BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value)); |
| 171 resolver->resolve(); | 170 resolver->resolve(); |
| 172 } else { | 171 } else { |
| 173 resolver->reject(BluetoothError::createDOMException(result)); | 172 resolver->reject(BluetoothError::createDOMException(result)); |
| 174 } | 173 } |
| 175 } | 174 } |
| 176 | 175 |
| 177 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue( | 176 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue( |
| 178 ScriptState* scriptState, | 177 ScriptState* scriptState, |
| 179 const DOMArrayPiece& value) { | 178 const DOMArrayPiece& value) { |
| 180 if (!getGatt()->connected()) { | 179 if (!getGatt()->connected()) { |
| 181 return ScriptPromise::rejectWithDOMException( | 180 return ScriptPromise::rejectWithDOMException( |
| 182 scriptState, | 181 scriptState, |
| 183 BluetoothError::createDOMException( | 182 BluetoothError::createNotConnectedException(BluetoothOperation::GATT)); |
| 184 blink::mojom::WebBluetoothResult::GATT_SERVER_NOT_CONNECTED)); | |
| 185 } | 183 } |
| 186 | 184 |
| 187 if (!getGatt()->device()->isValidCharacteristic( | 185 if (!getGatt()->device()->isValidCharacteristic( |
| 188 m_characteristic->instance_id)) { | 186 m_characteristic->instance_id)) { |
| 189 return ScriptPromise::rejectWithDOMException( | 187 return ScriptPromise::rejectWithDOMException( |
| 190 scriptState, createInvalidCharacteristicError()); | 188 scriptState, createInvalidCharacteristicError()); |
| 191 } | 189 } |
| 192 | 190 |
| 193 // Partial implementation of writeValue algorithm: | 191 // Partial implementation of writeValue algorithm: |
| 194 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattchar
acteristic-writevalue | 192 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattchar
acteristic-writevalue |
| (...skipping 26 matching lines...) Expand all Loading... |
| 221 | 219 |
| 222 void BluetoothRemoteGATTCharacteristic::NotificationsCallback( | 220 void BluetoothRemoteGATTCharacteristic::NotificationsCallback( |
| 223 ScriptPromiseResolver* resolver, | 221 ScriptPromiseResolver* resolver, |
| 224 mojom::blink::WebBluetoothResult result) { | 222 mojom::blink::WebBluetoothResult result) { |
| 225 if (!resolver->getExecutionContext() || | 223 if (!resolver->getExecutionContext() || |
| 226 resolver->getExecutionContext()->isContextDestroyed()) | 224 resolver->getExecutionContext()->isContextDestroyed()) |
| 227 return; | 225 return; |
| 228 | 226 |
| 229 // If the device is disconnected, reject. | 227 // If the device is disconnected, reject. |
| 230 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { | 228 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { |
| 231 resolver->reject(BluetoothError::createDOMException( | 229 resolver->reject( |
| 232 blink::mojom::WebBluetoothResult::GATT_SERVER_DISCONNECTED)); | 230 BluetoothError::createNotConnectedException(BluetoothOperation::GATT)); |
| 233 return; | 231 return; |
| 234 } | 232 } |
| 235 | 233 |
| 236 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 234 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 237 resolver->resolve(this); | 235 resolver->resolve(this); |
| 238 } else { | 236 } else { |
| 239 resolver->reject(BluetoothError::createDOMException(result)); | 237 resolver->reject(BluetoothError::createDOMException(result)); |
| 240 } | 238 } |
| 241 } | 239 } |
| 242 | 240 |
| 243 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( | 241 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( |
| 244 ScriptState* scriptState) { | 242 ScriptState* scriptState) { |
| 245 if (!getGatt()->connected()) { | 243 if (!getGatt()->connected()) { |
| 246 return ScriptPromise::rejectWithDOMException( | 244 return ScriptPromise::rejectWithDOMException( |
| 247 scriptState, | 245 scriptState, |
| 248 BluetoothError::createDOMException( | 246 BluetoothError::createNotConnectedException(BluetoothOperation::GATT)); |
| 249 blink::mojom::WebBluetoothResult::GATT_SERVER_NOT_CONNECTED)); | |
| 250 } | 247 } |
| 251 | 248 |
| 252 if (!getGatt()->device()->isValidCharacteristic( | 249 if (!getGatt()->device()->isValidCharacteristic( |
| 253 m_characteristic->instance_id)) { | 250 m_characteristic->instance_id)) { |
| 254 return ScriptPromise::rejectWithDOMException( | 251 return ScriptPromise::rejectWithDOMException( |
| 255 scriptState, createInvalidCharacteristicError()); | 252 scriptState, createInvalidCharacteristicError()); |
| 256 } | 253 } |
| 257 | 254 |
| 258 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 255 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 259 ScriptPromise promise = resolver->promise(); | 256 ScriptPromise promise = resolver->promise(); |
| 260 getGatt()->AddToActiveAlgorithms(resolver); | 257 getGatt()->AddToActiveAlgorithms(resolver); |
| 261 | 258 |
| 262 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 259 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
| 263 service->RemoteCharacteristicStartNotifications( | 260 service->RemoteCharacteristicStartNotifications( |
| 264 m_characteristic->instance_id, | 261 m_characteristic->instance_id, |
| 265 convertToBaseCallback( | 262 convertToBaseCallback( |
| 266 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, | 263 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, |
| 267 wrapPersistent(this), wrapPersistent(resolver)))); | 264 wrapPersistent(this), wrapPersistent(resolver)))); |
| 268 | 265 |
| 269 return promise; | 266 return promise; |
| 270 } | 267 } |
| 271 | 268 |
| 272 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications( | 269 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications( |
| 273 ScriptState* scriptState) { | 270 ScriptState* scriptState) { |
| 274 if (!getGatt()->connected()) { | 271 if (!getGatt()->connected()) { |
| 275 return ScriptPromise::rejectWithDOMException( | 272 return ScriptPromise::rejectWithDOMException( |
| 276 scriptState, | 273 scriptState, |
| 277 BluetoothError::createDOMException( | 274 BluetoothError::createNotConnectedException(BluetoothOperation::GATT)); |
| 278 blink::mojom::WebBluetoothResult::GATT_SERVER_NOT_CONNECTED)); | |
| 279 } | 275 } |
| 280 | 276 |
| 281 if (!getGatt()->device()->isValidCharacteristic( | 277 if (!getGatt()->device()->isValidCharacteristic( |
| 282 m_characteristic->instance_id)) { | 278 m_characteristic->instance_id)) { |
| 283 return ScriptPromise::rejectWithDOMException( | 279 return ScriptPromise::rejectWithDOMException( |
| 284 scriptState, createInvalidCharacteristicError()); | 280 scriptState, createInvalidCharacteristicError()); |
| 285 } | 281 } |
| 286 | 282 |
| 287 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 283 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 288 ScriptPromise promise = resolver->promise(); | 284 ScriptPromise promise = resolver->promise(); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 descriptor); | 329 descriptor); |
| 334 } | 330 } |
| 335 | 331 |
| 336 ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptorsImpl( | 332 ScriptPromise BluetoothRemoteGATTCharacteristic::getDescriptorsImpl( |
| 337 ScriptState* scriptState, | 333 ScriptState* scriptState, |
| 338 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 334 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
| 339 const String& descriptorsUUID) { | 335 const String& descriptorsUUID) { |
| 340 if (!getGatt()->connected()) { | 336 if (!getGatt()->connected()) { |
| 341 return ScriptPromise::rejectWithDOMException( | 337 return ScriptPromise::rejectWithDOMException( |
| 342 scriptState, | 338 scriptState, |
| 343 BluetoothError::createDOMException( | 339 // TODO(crbug.com/684445): Change to DescriptorsRetrieval. |
| 344 blink::mojom::WebBluetoothResult::GATT_SERVER_NOT_CONNECTED)); | 340 BluetoothError::createNotConnectedException(BluetoothOperation::GATT)); |
| 345 } | 341 } |
| 346 | 342 |
| 347 if (!getGatt()->device()->isValidCharacteristic( | 343 if (!getGatt()->device()->isValidCharacteristic( |
| 348 m_characteristic->instance_id)) { | 344 m_characteristic->instance_id)) { |
| 349 return ScriptPromise::rejectWithDOMException( | 345 return ScriptPromise::rejectWithDOMException( |
| 350 scriptState, createInvalidCharacteristicError()); | 346 scriptState, createInvalidCharacteristicError()); |
| 351 } | 347 } |
| 352 | 348 |
| 353 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 349 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 354 ScriptPromise promise = resolver->promise(); | 350 ScriptPromise promise = resolver->promise(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 374 ScriptPromiseResolver* resolver, | 370 ScriptPromiseResolver* resolver, |
| 375 mojom::blink::WebBluetoothResult result, | 371 mojom::blink::WebBluetoothResult result, |
| 376 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTDescriptorPtr>> | 372 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTDescriptorPtr>> |
| 377 descriptors) { | 373 descriptors) { |
| 378 if (!resolver->getExecutionContext() || | 374 if (!resolver->getExecutionContext() || |
| 379 resolver->getExecutionContext()->isContextDestroyed()) | 375 resolver->getExecutionContext()->isContextDestroyed()) |
| 380 return; | 376 return; |
| 381 | 377 |
| 382 // If the device is disconnected, reject. | 378 // If the device is disconnected, reject. |
| 383 if (!m_service->device()->gatt()->RemoveFromActiveAlgorithms(resolver)) { | 379 if (!m_service->device()->gatt()->RemoveFromActiveAlgorithms(resolver)) { |
| 384 resolver->reject(BluetoothError::createDOMException( | 380 // TODO(crbug.com/684445): Change to DescriptorsRetrieval. |
| 385 blink::mojom::WebBluetoothResult::GATT_SERVER_DISCONNECTED)); | 381 resolver->reject( |
| 382 BluetoothError::createNotConnectedException(BluetoothOperation::GATT)); |
| 386 return; | 383 return; |
| 387 } | 384 } |
| 388 | 385 |
| 389 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 386 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 390 DCHECK(descriptors); | 387 DCHECK(descriptors); |
| 391 | 388 |
| 392 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { | 389 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { |
| 393 DCHECK_EQ(1u, descriptors->size()); | 390 DCHECK_EQ(1u, descriptors->size()); |
| 394 resolver->resolve( | 391 resolver->resolve( |
| 395 m_service->device()->getOrCreateBluetoothRemoteGATTDescriptor( | 392 m_service->device()->getOrCreateBluetoothRemoteGATTDescriptor( |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { | 426 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { |
| 430 visitor->trace(m_service); | 427 visitor->trace(m_service); |
| 431 visitor->trace(m_properties); | 428 visitor->trace(m_properties); |
| 432 visitor->trace(m_value); | 429 visitor->trace(m_value); |
| 433 visitor->trace(m_device); | 430 visitor->trace(m_device); |
| 434 EventTargetWithInlineData::trace(visitor); | 431 EventTargetWithInlineData::trace(visitor); |
| 435 ContextLifecycleObserver::trace(visitor); | 432 ContextLifecycleObserver::trace(visitor); |
| 436 } | 433 } |
| 437 | 434 |
| 438 } // namespace blink | 435 } // namespace blink |
| OLD | NEW |