 Chromium Code Reviews
 Chromium Code Reviews Issue 2637343002:
  Implement WebBluetooth descriptor.readValue()  (Closed)
    
  
    Issue 2637343002:
  Implement WebBluetooth descriptor.readValue()  (Closed) 
  | 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..dc69ae552bfd182a40c15fb70758485e4c55c885 100644 | 
| --- a/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTDescriptor.cpp | 
| +++ b/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTDescriptor.cpp | 
| @@ -4,8 +4,13 @@ | 
| #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 "modules/bluetooth/BluetoothRemoteGATTUtils.h" | 
| #include <memory> | 
| namespace blink { | 
| @@ -24,14 +29,67 @@ 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(BluetoothRemoteGATTUtils::CreateDOMException( | 
| + BluetoothRemoteGATTUtils::ExceptionType::kGATTServerDisconnected)); | 
| + return; | 
| + } | 
| + | 
| + if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 
| + DCHECK(value); | 
| + DOMDataView* domDataView = | 
| + BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value()); | 
| + setValue(domDataView); | 
| 
ortuno
2017/01/20 04:30:53
Q: Why do we need the extra function?
 
dougt
2017/01/23 21:40:37
Removed the method and just setting the member dir
 | 
| + 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. | 
| 
ortuno
2017/01/20 04:30:53
Unsure why we need this comment.
 
dougt
2017/01/23 21:40:37
Done.
Also removed this comment in the 5 other pl
 | 
| + if (!getGatt()->connected()) { | 
| + return ScriptPromise::rejectWithDOMException( | 
| + scriptState, | 
| + BluetoothRemoteGATTUtils::CreateDOMException( | 
| + BluetoothRemoteGATTUtils::ExceptionType::kGATTServerNotConnected)); | 
| + } | 
| + | 
| + if (!getGatt()->device()->isValidDescriptor(m_descriptor->instance_id)) { | 
| + return ScriptPromise::rejectWithDOMException( | 
| + scriptState, | 
| + BluetoothRemoteGATTUtils::CreateDOMException( | 
| + BluetoothRemoteGATTUtils::ExceptionType::kInvalidDescriptor)); | 
| + } | 
| + | 
| + ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 
| + ScriptPromise promise = resolver->promise(); | 
| + getGatt()->AddToActiveAlgorithms(resolver); | 
| + | 
| + mojom::blink::WebBluetoothService* service = | 
| 
ortuno
2017/01/20 04:30:53
should we add a getBluetoothService() method?
 
dougt
2017/01/23 21:40:37
Maybe soon.  I suspect when we add ::writeValue()
 | 
| + 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( |