| 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/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/BluetoothCharacteristicProperties.h" | 14 #include "modules/bluetooth/BluetoothCharacteristicProperties.h" |
| 15 #include "modules/bluetooth/BluetoothError.h" | 15 #include "modules/bluetooth/BluetoothError.h" |
| 16 #include "modules/bluetooth/BluetoothRemoteGATTService.h" | 16 #include "modules/bluetooth/BluetoothRemoteGATTService.h" |
| 17 #include "modules/bluetooth/BluetoothSupplement.h" | 17 #include "modules/bluetooth/BluetoothSupplement.h" |
| 18 #include "public/platform/modules/bluetooth/WebBluetooth.h" | 18 #include "public/platform/modules/bluetooth/WebBluetooth.h" |
| 19 #include <memory> | 19 #include <memory> |
| 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[] = |
| 30 "Characteristic is no longer valid. Remember to retrieve the " |
| 31 "characteristic again after reconnecting."; |
| 29 | 32 |
| 30 DOMDataView* ConvertWebVectorToDataView(const WebVector<uint8_t>& webVector) { | 33 DOMDataView* ConvertWebVectorToDataView(const WebVector<uint8_t>& webVector) { |
| 31 static_assert(sizeof(*webVector.data()) == 1, | 34 static_assert(sizeof(*webVector.data()) == 1, |
| 32 "uint8_t should be a single byte"); | 35 "uint8_t should be a single byte"); |
| 33 DOMArrayBuffer* domBuffer = | 36 DOMArrayBuffer* domBuffer = |
| 34 DOMArrayBuffer::create(webVector.data(), webVector.size()); | 37 DOMArrayBuffer::create(webVector.data(), webVector.size()); |
| 35 return DOMDataView::create(domBuffer, 0, webVector.size()); | 38 return DOMDataView::create(domBuffer, 0, webVector.size()); |
| 36 } | 39 } |
| 37 | 40 |
| 38 } // anonymous namespace | 41 } // anonymous namespace |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 }; | 174 }; |
| 172 | 175 |
| 173 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( | 176 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( |
| 174 ScriptState* scriptState) { | 177 ScriptState* scriptState) { |
| 175 if (!gatt()->connected()) { | 178 if (!gatt()->connected()) { |
| 176 return ScriptPromise::rejectWithDOMException( | 179 return ScriptPromise::rejectWithDOMException( |
| 177 scriptState, | 180 scriptState, |
| 178 DOMException::create(NetworkError, kGATTServerNotConnected)); | 181 DOMException::create(NetworkError, kGATTServerNotConnected)); |
| 179 } | 182 } |
| 180 | 183 |
| 184 if (!gatt()->device()->isValidCharacteristic( |
| 185 m_webCharacteristic->characteristicInstanceID)) { |
| 186 return ScriptPromise::rejectWithDOMException( |
| 187 scriptState, |
| 188 DOMException::create(InvalidStateError, kInvalidCharacteristic)); |
| 189 } |
| 190 |
| 181 WebBluetooth* webbluetooth = | 191 WebBluetooth* webbluetooth = |
| 182 BluetoothSupplement::fromScriptState(scriptState); | 192 BluetoothSupplement::fromScriptState(scriptState); |
| 183 | 193 |
| 184 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 194 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 185 ScriptPromise promise = resolver->promise(); | 195 ScriptPromise promise = resolver->promise(); |
| 186 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, | 196 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, |
| 187 new ReadValueCallback(this, resolver)); | 197 new ReadValueCallback(this, resolver)); |
| 188 | 198 |
| 189 return promise; | 199 return promise; |
| 190 } | 200 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 | 253 |
| 244 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue( | 254 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue( |
| 245 ScriptState* scriptState, | 255 ScriptState* scriptState, |
| 246 const DOMArrayPiece& value) { | 256 const DOMArrayPiece& value) { |
| 247 if (!gatt()->connected()) { | 257 if (!gatt()->connected()) { |
| 248 return ScriptPromise::rejectWithDOMException( | 258 return ScriptPromise::rejectWithDOMException( |
| 249 scriptState, | 259 scriptState, |
| 250 DOMException::create(NetworkError, kGATTServerNotConnected)); | 260 DOMException::create(NetworkError, kGATTServerNotConnected)); |
| 251 } | 261 } |
| 252 | 262 |
| 263 if (!gatt()->device()->isValidCharacteristic( |
| 264 m_webCharacteristic->characteristicInstanceID)) { |
| 265 return ScriptPromise::rejectWithDOMException( |
| 266 scriptState, |
| 267 DOMException::create(InvalidStateError, kInvalidCharacteristic)); |
| 268 } |
| 269 |
| 253 WebBluetooth* webbluetooth = | 270 WebBluetooth* webbluetooth = |
| 254 BluetoothSupplement::fromScriptState(scriptState); | 271 BluetoothSupplement::fromScriptState(scriptState); |
| 255 // Partial implementation of writeValue algorithm: | 272 // Partial implementation of writeValue algorithm: |
| 256 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothgattcharac
teristic-writevalue | 273 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothgattcharac
teristic-writevalue |
| 257 | 274 |
| 258 // If bytes is more than 512 bytes long (the maximum length of an attribute | 275 // If bytes is more than 512 bytes long (the maximum length of an attribute |
| 259 // value, per Long Attribute Values) return a promise rejected with an | 276 // value, per Long Attribute Values) return a promise rejected with an |
| 260 // InvalidModificationError and abort. | 277 // InvalidModificationError and abort. |
| 261 if (value.byteLength() > 512) | 278 if (value.byteLength() > 512) |
| 262 return ScriptPromise::rejectWithDOMException( | 279 return ScriptPromise::rejectWithDOMException( |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 }; | 342 }; |
| 326 | 343 |
| 327 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( | 344 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( |
| 328 ScriptState* scriptState) { | 345 ScriptState* scriptState) { |
| 329 if (!gatt()->connected()) { | 346 if (!gatt()->connected()) { |
| 330 return ScriptPromise::rejectWithDOMException( | 347 return ScriptPromise::rejectWithDOMException( |
| 331 scriptState, | 348 scriptState, |
| 332 DOMException::create(NetworkError, kGATTServerNotConnected)); | 349 DOMException::create(NetworkError, kGATTServerNotConnected)); |
| 333 } | 350 } |
| 334 | 351 |
| 352 if (!gatt()->device()->isValidCharacteristic( |
| 353 m_webCharacteristic->characteristicInstanceID)) { |
| 354 return ScriptPromise::rejectWithDOMException( |
| 355 scriptState, |
| 356 DOMException::create(InvalidStateError, kInvalidCharacteristic)); |
| 357 } |
| 358 |
| 335 WebBluetooth* webbluetooth = | 359 WebBluetooth* webbluetooth = |
| 336 BluetoothSupplement::fromScriptState(scriptState); | 360 BluetoothSupplement::fromScriptState(scriptState); |
| 337 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 361 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 338 ScriptPromise promise = resolver->promise(); | 362 ScriptPromise promise = resolver->promise(); |
| 339 webbluetooth->startNotifications( | 363 webbluetooth->startNotifications( |
| 340 m_webCharacteristic->characteristicInstanceID, | 364 m_webCharacteristic->characteristicInstanceID, |
| 341 new NotificationsCallback(this, resolver)); | 365 new NotificationsCallback(this, resolver)); |
| 342 return promise; | 366 return promise; |
| 343 } | 367 } |
| 344 | 368 |
| 345 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications( | 369 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications( |
| 346 ScriptState* scriptState) { | 370 ScriptState* scriptState) { |
| 347 #if OS(MACOSX) | 371 #if OS(MACOSX) |
| 348 // TODO(jlebel): Remove when stopNotifications is implemented. | 372 // TODO(jlebel): Remove when stopNotifications is implemented. |
| 349 return ScriptPromise::rejectWithDOMException( | 373 return ScriptPromise::rejectWithDOMException( |
| 350 scriptState, DOMException::create(NotSupportedError, | 374 scriptState, DOMException::create(NotSupportedError, |
| 351 "stopNotifications is not implemented " | 375 "stopNotifications is not implemented " |
| 352 "yet. See https://goo.gl/J6ASzs")); | 376 "yet. See https://goo.gl/J6ASzs")); |
| 353 #endif // OS(MACOSX) | 377 #endif // OS(MACOSX) |
| 354 | 378 |
| 355 if (!gatt()->connected()) { | 379 if (!gatt()->connected()) { |
| 356 return ScriptPromise::rejectWithDOMException( | 380 return ScriptPromise::rejectWithDOMException( |
| 357 scriptState, | 381 scriptState, |
| 358 DOMException::create(NetworkError, kGATTServerNotConnected)); | 382 DOMException::create(NetworkError, kGATTServerNotConnected)); |
| 359 } | 383 } |
| 360 | 384 |
| 385 if (!gatt()->device()->isValidCharacteristic( |
| 386 m_webCharacteristic->characteristicInstanceID)) { |
| 387 return ScriptPromise::rejectWithDOMException( |
| 388 scriptState, |
| 389 DOMException::create(InvalidStateError, kInvalidCharacteristic)); |
| 390 } |
| 391 |
| 361 WebBluetooth* webbluetooth = | 392 WebBluetooth* webbluetooth = |
| 362 BluetoothSupplement::fromScriptState(scriptState); | 393 BluetoothSupplement::fromScriptState(scriptState); |
| 363 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 394 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 364 ScriptPromise promise = resolver->promise(); | 395 ScriptPromise promise = resolver->promise(); |
| 365 webbluetooth->stopNotifications(m_webCharacteristic->characteristicInstanceID, | 396 webbluetooth->stopNotifications(m_webCharacteristic->characteristicInstanceID, |
| 366 new NotificationsCallback(this, resolver)); | 397 new NotificationsCallback(this, resolver)); |
| 367 return promise; | 398 return promise; |
| 368 } | 399 } |
| 369 | 400 |
| 370 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { | 401 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { |
| 371 visitor->trace(m_service); | 402 visitor->trace(m_service); |
| 372 visitor->trace(m_properties); | 403 visitor->trace(m_properties); |
| 373 visitor->trace(m_value); | 404 visitor->trace(m_value); |
| 374 EventTargetWithInlineData::trace(visitor); | 405 EventTargetWithInlineData::trace(visitor); |
| 375 ActiveDOMObject::trace(visitor); | 406 ActiveDOMObject::trace(visitor); |
| 376 } | 407 } |
| 377 | 408 |
| 378 } // namespace blink | 409 } // namespace blink |
| OLD | NEW |