| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/BluetoothRemoteGATTDescriptor.h" | 5 #include "modules/bluetooth/BluetoothRemoteGATTDescriptor.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromise.h" |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 7 #include "core/dom/DOMException.h" | 9 #include "core/dom/DOMException.h" |
| 10 #include "modules/bluetooth/Bluetooth.h" |
| 11 #include "modules/bluetooth/BluetoothError.h" |
| 8 #include "modules/bluetooth/BluetoothRemoteGATTService.h" | 12 #include "modules/bluetooth/BluetoothRemoteGATTService.h" |
| 13 #include "modules/bluetooth/BluetoothRemoteGATTUtils.h" |
| 9 #include <memory> | 14 #include <memory> |
| 10 | 15 |
| 11 namespace blink { | 16 namespace blink { |
| 12 | 17 |
| 13 BluetoothRemoteGATTDescriptor::BluetoothRemoteGATTDescriptor( | 18 BluetoothRemoteGATTDescriptor::BluetoothRemoteGATTDescriptor( |
| 14 mojom::blink::WebBluetoothRemoteGATTDescriptorPtr descriptor, | 19 mojom::blink::WebBluetoothRemoteGATTDescriptorPtr descriptor, |
| 15 BluetoothRemoteGATTCharacteristic* characteristic) | 20 BluetoothRemoteGATTCharacteristic* characteristic) |
| 16 : m_descriptor(std::move(descriptor)), m_characteristic(characteristic) {} | 21 : m_descriptor(std::move(descriptor)), m_characteristic(characteristic) {} |
| 17 | 22 |
| 18 BluetoothRemoteGATTDescriptor* BluetoothRemoteGATTDescriptor::create( | 23 BluetoothRemoteGATTDescriptor* BluetoothRemoteGATTDescriptor::create( |
| 19 mojom::blink::WebBluetoothRemoteGATTDescriptorPtr descriptor, | 24 mojom::blink::WebBluetoothRemoteGATTDescriptorPtr descriptor, |
| 20 | 25 |
| 21 BluetoothRemoteGATTCharacteristic* characteristic) { | 26 BluetoothRemoteGATTCharacteristic* characteristic) { |
| 22 BluetoothRemoteGATTDescriptor* result = | 27 BluetoothRemoteGATTDescriptor* result = |
| 23 new BluetoothRemoteGATTDescriptor(std::move(descriptor), characteristic); | 28 new BluetoothRemoteGATTDescriptor(std::move(descriptor), characteristic); |
| 24 return result; | 29 return result; |
| 25 } | 30 } |
| 26 | 31 |
| 32 void BluetoothRemoteGATTDescriptor::ReadValueCallback( |
| 33 ScriptPromiseResolver* resolver, |
| 34 mojom::blink::WebBluetoothResult result, |
| 35 const Optional<Vector<uint8_t>>& value) { |
| 36 if (!resolver->getExecutionContext() || |
| 37 resolver->getExecutionContext()->isContextDestroyed()) |
| 38 return; |
| 39 |
| 40 // If the device is disconnected, reject. |
| 41 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { |
| 42 resolver->reject(BluetoothRemoteGATTUtils::CreateDOMException( |
| 43 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerDisconnected)); |
| 44 return; |
| 45 } |
| 46 |
| 47 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 48 DCHECK(value); |
| 49 DOMDataView* domDataView = |
| 50 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value()); |
| 51 m_value = domDataView; |
| 52 resolver->resolve(domDataView); |
| 53 } else { |
| 54 resolver->reject(BluetoothError::take(resolver, result)); |
| 55 } |
| 56 } |
| 57 |
| 27 ScriptPromise BluetoothRemoteGATTDescriptor::readValue( | 58 ScriptPromise BluetoothRemoteGATTDescriptor::readValue( |
| 28 ScriptState* scriptState) { | 59 ScriptState* scriptState) { |
| 29 // TODO(668837): Implement WebBluetooth descriptor.readValue() | 60 if (!getGatt()->connected()) { |
| 30 return ScriptPromise::rejectWithDOMException( | 61 return ScriptPromise::rejectWithDOMException( |
| 31 scriptState, | 62 scriptState, |
| 32 DOMException::create(NotSupportedError, | 63 BluetoothRemoteGATTUtils::CreateDOMException( |
| 33 "descriptor readValue is not implemented " | 64 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerNotConnected)); |
| 34 "yet. See https://goo.gl/J6ASzs")); | 65 } |
| 66 |
| 67 if (!getGatt()->device()->isValidDescriptor(m_descriptor->instance_id)) { |
| 68 return ScriptPromise::rejectWithDOMException( |
| 69 scriptState, |
| 70 BluetoothRemoteGATTUtils::CreateDOMException( |
| 71 BluetoothRemoteGATTUtils::ExceptionType::kInvalidDescriptor)); |
| 72 } |
| 73 |
| 74 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 75 ScriptPromise promise = resolver->promise(); |
| 76 getGatt()->AddToActiveAlgorithms(resolver); |
| 77 |
| 78 mojom::blink::WebBluetoothService* service = |
| 79 m_characteristic->m_device->bluetooth()->service(); |
| 80 service->RemoteDescriptorReadValue( |
| 81 m_descriptor->instance_id, |
| 82 convertToBaseCallback( |
| 83 WTF::bind(&BluetoothRemoteGATTDescriptor::ReadValueCallback, |
| 84 wrapPersistent(this), wrapPersistent(resolver)))); |
| 85 |
| 86 return promise; |
| 35 } | 87 } |
| 36 | 88 |
| 37 ScriptPromise BluetoothRemoteGATTDescriptor::writeValue( | 89 ScriptPromise BluetoothRemoteGATTDescriptor::writeValue( |
| 38 ScriptState* scriptState, | 90 ScriptState* scriptState, |
| 39 const DOMArrayPiece& value) { | 91 const DOMArrayPiece& value) { |
| 40 // TODO(668838): Implement WebBluetooth descriptor.writeValue() | 92 // TODO(668838): Implement WebBluetooth descriptor.writeValue() |
| 41 return ScriptPromise::rejectWithDOMException( | 93 return ScriptPromise::rejectWithDOMException( |
| 42 scriptState, | 94 scriptState, |
| 43 DOMException::create(NotSupportedError, | 95 DOMException::create(NotSupportedError, |
| 44 "descriptor writeValue is not implemented " | 96 "descriptor writeValue is not implemented " |
| 45 "yet. See https://goo.gl/J6ASzs")); | 97 "yet. See https://goo.gl/J6ASzs")); |
| 46 } | 98 } |
| 47 | 99 |
| 48 DEFINE_TRACE(BluetoothRemoteGATTDescriptor) { | 100 DEFINE_TRACE(BluetoothRemoteGATTDescriptor) { |
| 49 visitor->trace(m_characteristic); | 101 visitor->trace(m_characteristic); |
| 50 visitor->trace(m_value); | 102 visitor->trace(m_value); |
| 51 } | 103 } |
| 52 | 104 |
| 53 } // namespace blink | 105 } // namespace blink |
| OLD | NEW |