Chromium Code Reviews| Index: third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTDescriptor.cpp |
| diff --git a/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTDescriptor.cpp b/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTDescriptor.cpp |
| index 4184c2d18be5394ab47af74c4de4ccfcbd44199b..d20efc2ed4bde148aac024073583a4a4b4152607 100644 |
| --- a/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTDescriptor.cpp |
| +++ b/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTDescriptor.cpp |
| @@ -4,12 +4,34 @@ |
| #include "modules/bluetooth/BluetoothRemoteGATTDescriptor.h" |
| +#include "bindings/core/v8/ScriptPromise.h" |
| +#include "bindings/core/v8/ScriptPromiseResolver.h" |
| #include "core/dom/DOMException.h" |
| +#include "modules/bluetooth/Bluetooth.h" |
| +#include "modules/bluetooth/BluetoothError.h" |
| #include "modules/bluetooth/BluetoothRemoteGATTService.h" |
| #include <memory> |
| namespace blink { |
| +namespace { |
| +const char kGATTServerDisconnected[] = |
| + "GATT Server disconnected while performing a GATT operation."; |
| +const char kGATTServerNotConnected[] = |
| + "GATT Server is disconnected. Cannot perform GATT operations."; |
| +const char kInvalidDescriptor[] = |
| + "Descriptor is no longer valid. Remember to retrieve the " |
| + "Descriptor again after reconnecting."; |
| + |
| +DOMDataView* ConvertWTFVectorToDataView(const Vector<uint8_t>& wtfVector) { |
|
scheib
2017/01/19 06:16:18
Copy paste... hmm... but when you asked earlier I'
dougt
2017/01/20 03:17:24
Done.
|
| + static_assert(sizeof(*wtfVector.data()) == 1, |
| + "uint8_t should be a single byte"); |
| + DOMArrayBuffer* domBuffer = |
| + DOMArrayBuffer::create(wtfVector.data(), wtfVector.size()); |
| + return DOMDataView::create(domBuffer, 0, wtfVector.size()); |
| +} |
| +} |
| + |
| BluetoothRemoteGATTDescriptor::BluetoothRemoteGATTDescriptor( |
| mojom::blink::WebBluetoothRemoteGATTDescriptorPtr descriptor, |
| BluetoothRemoteGATTCharacteristic* characteristic) |
| @@ -24,14 +46,64 @@ BluetoothRemoteGATTDescriptor* BluetoothRemoteGATTDescriptor::create( |
| return result; |
| } |
| +void BluetoothRemoteGATTDescriptor::setValue(DOMDataView* domDataView) { |
| + m_value = domDataView; |
| +} |
| + |
| +void BluetoothRemoteGATTDescriptor::ReadValueCallback( |
| + ScriptPromiseResolver* resolver, |
| + mojom::blink::WebBluetoothResult result, |
| + const Optional<Vector<uint8_t>>& value) { |
| + if (!resolver->getExecutionContext() || |
| + resolver->getExecutionContext()->isContextDestroyed()) |
| + return; |
| + |
| + // If the resolver is not in the set of ActiveAlgorithms then the frame |
| + // disconnected so we reject. |
| + if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { |
| + resolver->reject( |
| + DOMException::create(NetworkError, kGATTServerDisconnected)); |
| + return; |
| + } |
| + |
| + if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| + DCHECK(value); |
| + DOMDataView* domDataView = ConvertWTFVectorToDataView(value.value()); |
| + setValue(domDataView); |
| + resolver->resolve(domDataView); |
| + } else { |
| + resolver->reject(BluetoothError::take(resolver, result)); |
| + } |
| +} |
| + |
| ScriptPromise BluetoothRemoteGATTDescriptor::readValue( |
| ScriptState* scriptState) { |
| - // TODO(668837): Implement WebBluetooth descriptor.readValue() |
| - return ScriptPromise::rejectWithDOMException( |
| - scriptState, |
| - DOMException::create(NotSupportedError, |
| - "descriptor readValue is not implemented " |
| - "yet. See https://goo.gl/J6ASzs")); |
| + // We always check that the device is connected. |
| + if (!getGatt()->connected()) { |
| + return ScriptPromise::rejectWithDOMException( |
| + scriptState, |
| + DOMException::create(NetworkError, kGATTServerNotConnected)); |
| + } |
| + |
| + if (!getGatt()->device()->isValidDescriptor(m_descriptor->instance_id)) { |
| + return ScriptPromise::rejectWithDOMException( |
| + scriptState, |
| + DOMException::create(InvalidStateError, kInvalidDescriptor)); |
| + } |
| + |
| + ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| + ScriptPromise promise = resolver->promise(); |
| + getGatt()->AddToActiveAlgorithms(resolver); |
| + |
| + mojom::blink::WebBluetoothService* service = |
| + m_characteristic->m_device->bluetooth()->service(); |
| + service->RemoteDescriptorReadValue( |
| + m_descriptor->instance_id, |
| + convertToBaseCallback( |
| + WTF::bind(&BluetoothRemoteGATTDescriptor::ReadValueCallback, |
| + wrapPersistent(this), wrapPersistent(resolver)))); |
| + |
| + return promise; |
| } |
| ScriptPromise BluetoothRemoteGATTDescriptor::writeValue( |