 Chromium Code Reviews
 Chromium Code Reviews Issue 2637343002:
  Implement WebBluetooth descriptor.readValue()  (Closed)
    
  
    Issue 2637343002:
  Implement WebBluetooth descriptor.readValue()  (Closed) 
  | 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" | 
| 9 #include <memory> | 13 #include <memory> | 
| 10 | 14 | 
| 11 namespace blink { | 15 namespace blink { | 
| 12 | 16 | 
| 17 namespace { | |
| 18 const char kGATTServerDisconnected[] = | |
| 19 "GATT Server disconnected while performing a GATT operation."; | |
| 20 const char kGATTServerNotConnected[] = | |
| 21 "GATT Server is disconnected. Cannot perform GATT operations."; | |
| 22 const char kInvalidDescriptor[] = | |
| 23 "Descriptor is no longer valid. Remember to retrieve the " | |
| 24 "Descriptor again after reconnecting."; | |
| 25 | |
| 26 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.
 | |
| 27 static_assert(sizeof(*wtfVector.data()) == 1, | |
| 28 "uint8_t should be a single byte"); | |
| 29 DOMArrayBuffer* domBuffer = | |
| 30 DOMArrayBuffer::create(wtfVector.data(), wtfVector.size()); | |
| 31 return DOMDataView::create(domBuffer, 0, wtfVector.size()); | |
| 32 } | |
| 33 } | |
| 34 | |
| 13 BluetoothRemoteGATTDescriptor::BluetoothRemoteGATTDescriptor( | 35 BluetoothRemoteGATTDescriptor::BluetoothRemoteGATTDescriptor( | 
| 14 mojom::blink::WebBluetoothRemoteGATTDescriptorPtr descriptor, | 36 mojom::blink::WebBluetoothRemoteGATTDescriptorPtr descriptor, | 
| 15 BluetoothRemoteGATTCharacteristic* characteristic) | 37 BluetoothRemoteGATTCharacteristic* characteristic) | 
| 16 : m_descriptor(std::move(descriptor)), m_characteristic(characteristic) {} | 38 : m_descriptor(std::move(descriptor)), m_characteristic(characteristic) {} | 
| 17 | 39 | 
| 18 BluetoothRemoteGATTDescriptor* BluetoothRemoteGATTDescriptor::create( | 40 BluetoothRemoteGATTDescriptor* BluetoothRemoteGATTDescriptor::create( | 
| 19 mojom::blink::WebBluetoothRemoteGATTDescriptorPtr descriptor, | 41 mojom::blink::WebBluetoothRemoteGATTDescriptorPtr descriptor, | 
| 20 | 42 | 
| 21 BluetoothRemoteGATTCharacteristic* characteristic) { | 43 BluetoothRemoteGATTCharacteristic* characteristic) { | 
| 22 BluetoothRemoteGATTDescriptor* result = | 44 BluetoothRemoteGATTDescriptor* result = | 
| 23 new BluetoothRemoteGATTDescriptor(std::move(descriptor), characteristic); | 45 new BluetoothRemoteGATTDescriptor(std::move(descriptor), characteristic); | 
| 24 return result; | 46 return result; | 
| 25 } | 47 } | 
| 26 | 48 | 
| 49 void BluetoothRemoteGATTDescriptor::setValue(DOMDataView* domDataView) { | |
| 50 m_value = domDataView; | |
| 51 } | |
| 52 | |
| 53 void BluetoothRemoteGATTDescriptor::ReadValueCallback( | |
| 54 ScriptPromiseResolver* resolver, | |
| 55 mojom::blink::WebBluetoothResult result, | |
| 56 const Optional<Vector<uint8_t>>& value) { | |
| 57 if (!resolver->getExecutionContext() || | |
| 58 resolver->getExecutionContext()->isContextDestroyed()) | |
| 59 return; | |
| 60 | |
| 61 // If the resolver is not in the set of ActiveAlgorithms then the frame | |
| 62 // disconnected so we reject. | |
| 63 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { | |
| 64 resolver->reject( | |
| 65 DOMException::create(NetworkError, kGATTServerDisconnected)); | |
| 66 return; | |
| 67 } | |
| 68 | |
| 69 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | |
| 70 DCHECK(value); | |
| 71 DOMDataView* domDataView = ConvertWTFVectorToDataView(value.value()); | |
| 72 setValue(domDataView); | |
| 73 resolver->resolve(domDataView); | |
| 74 } else { | |
| 75 resolver->reject(BluetoothError::take(resolver, result)); | |
| 76 } | |
| 77 } | |
| 78 | |
| 27 ScriptPromise BluetoothRemoteGATTDescriptor::readValue( | 79 ScriptPromise BluetoothRemoteGATTDescriptor::readValue( | 
| 28 ScriptState* scriptState) { | 80 ScriptState* scriptState) { | 
| 29 // TODO(668837): Implement WebBluetooth descriptor.readValue() | 81 // We always check that the device is connected. | 
| 30 return ScriptPromise::rejectWithDOMException( | 82 if (!getGatt()->connected()) { | 
| 31 scriptState, | 83 return ScriptPromise::rejectWithDOMException( | 
| 32 DOMException::create(NotSupportedError, | 84 scriptState, | 
| 33 "descriptor readValue is not implemented " | 85 DOMException::create(NetworkError, kGATTServerNotConnected)); | 
| 34 "yet. See https://goo.gl/J6ASzs")); | 86 } | 
| 87 | |
| 88 if (!getGatt()->device()->isValidDescriptor(m_descriptor->instance_id)) { | |
| 89 return ScriptPromise::rejectWithDOMException( | |
| 90 scriptState, | |
| 91 DOMException::create(InvalidStateError, kInvalidDescriptor)); | |
| 92 } | |
| 93 | |
| 94 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | |
| 95 ScriptPromise promise = resolver->promise(); | |
| 96 getGatt()->AddToActiveAlgorithms(resolver); | |
| 97 | |
| 98 mojom::blink::WebBluetoothService* service = | |
| 99 m_characteristic->m_device->bluetooth()->service(); | |
| 100 service->RemoteDescriptorReadValue( | |
| 101 m_descriptor->instance_id, | |
| 102 convertToBaseCallback( | |
| 103 WTF::bind(&BluetoothRemoteGATTDescriptor::ReadValueCallback, | |
| 104 wrapPersistent(this), wrapPersistent(resolver)))); | |
| 105 | |
| 106 return promise; | |
| 35 } | 107 } | 
| 36 | 108 | 
| 37 ScriptPromise BluetoothRemoteGATTDescriptor::writeValue( | 109 ScriptPromise BluetoothRemoteGATTDescriptor::writeValue( | 
| 38 ScriptState* scriptState, | 110 ScriptState* scriptState, | 
| 39 const DOMArrayPiece& value) { | 111 const DOMArrayPiece& value) { | 
| 40 // TODO(668838): Implement WebBluetooth descriptor.writeValue() | 112 // TODO(668838): Implement WebBluetooth descriptor.writeValue() | 
| 41 return ScriptPromise::rejectWithDOMException( | 113 return ScriptPromise::rejectWithDOMException( | 
| 42 scriptState, | 114 scriptState, | 
| 43 DOMException::create(NotSupportedError, | 115 DOMException::create(NotSupportedError, | 
| 44 "descriptor writeValue is not implemented " | 116 "descriptor writeValue is not implemented " | 
| 45 "yet. See https://goo.gl/J6ASzs")); | 117 "yet. See https://goo.gl/J6ASzs")); | 
| 46 } | 118 } | 
| 47 | 119 | 
| 48 DEFINE_TRACE(BluetoothRemoteGATTDescriptor) { | 120 DEFINE_TRACE(BluetoothRemoteGATTDescriptor) { | 
| 49 visitor->trace(m_characteristic); | 121 visitor->trace(m_characteristic); | 
| 50 visitor->trace(m_value); | 122 visitor->trace(m_value); | 
| 51 } | 123 } | 
| 52 | 124 | 
| 53 } // namespace blink | 125 } // namespace blink | 
| OLD | NEW |