 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" | 
| 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::setValue(DOMDataView* domDataView) { | |
| 33 m_value = domDataView; | |
| 34 } | |
| 35 | |
| 36 void BluetoothRemoteGATTDescriptor::ReadValueCallback( | |
| 37 ScriptPromiseResolver* resolver, | |
| 38 mojom::blink::WebBluetoothResult result, | |
| 39 const Optional<Vector<uint8_t>>& value) { | |
| 40 if (!resolver->getExecutionContext() || | |
| 41 resolver->getExecutionContext()->isContextDestroyed()) | |
| 42 return; | |
| 43 | |
| 44 // If the resolver is not in the set of ActiveAlgorithms then the frame | |
| 45 // disconnected so we reject. | |
| 46 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { | |
| 47 resolver->reject(BluetoothRemoteGATTUtils::CreateDOMException( | |
| 48 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerDisconnected)); | |
| 49 return; | |
| 50 } | |
| 51 | |
| 52 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | |
| 53 DCHECK(value); | |
| 54 DOMDataView* domDataView = | |
| 55 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value.value()); | |
| 56 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
 | |
| 57 resolver->resolve(domDataView); | |
| 58 } else { | |
| 59 resolver->reject(BluetoothError::take(resolver, result)); | |
| 60 } | |
| 61 } | |
| 62 | |
| 27 ScriptPromise BluetoothRemoteGATTDescriptor::readValue( | 63 ScriptPromise BluetoothRemoteGATTDescriptor::readValue( | 
| 28 ScriptState* scriptState) { | 64 ScriptState* scriptState) { | 
| 29 // TODO(668837): Implement WebBluetooth descriptor.readValue() | 65 // 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
 | |
| 30 return ScriptPromise::rejectWithDOMException( | 66 if (!getGatt()->connected()) { | 
| 31 scriptState, | 67 return ScriptPromise::rejectWithDOMException( | 
| 32 DOMException::create(NotSupportedError, | 68 scriptState, | 
| 33 "descriptor readValue is not implemented " | 69 BluetoothRemoteGATTUtils::CreateDOMException( | 
| 34 "yet. See https://goo.gl/J6ASzs")); | 70 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerNotConnected)); | 
| 71 } | |
| 72 | |
| 73 if (!getGatt()->device()->isValidDescriptor(m_descriptor->instance_id)) { | |
| 74 return ScriptPromise::rejectWithDOMException( | |
| 75 scriptState, | |
| 76 BluetoothRemoteGATTUtils::CreateDOMException( | |
| 77 BluetoothRemoteGATTUtils::ExceptionType::kInvalidDescriptor)); | |
| 78 } | |
| 79 | |
| 80 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | |
| 81 ScriptPromise promise = resolver->promise(); | |
| 82 getGatt()->AddToActiveAlgorithms(resolver); | |
| 83 | |
| 84 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()
 | |
| 85 m_characteristic->m_device->bluetooth()->service(); | |
| 86 service->RemoteDescriptorReadValue( | |
| 87 m_descriptor->instance_id, | |
| 88 convertToBaseCallback( | |
| 89 WTF::bind(&BluetoothRemoteGATTDescriptor::ReadValueCallback, | |
| 90 wrapPersistent(this), wrapPersistent(resolver)))); | |
| 91 | |
| 92 return promise; | |
| 35 } | 93 } | 
| 36 | 94 | 
| 37 ScriptPromise BluetoothRemoteGATTDescriptor::writeValue( | 95 ScriptPromise BluetoothRemoteGATTDescriptor::writeValue( | 
| 38 ScriptState* scriptState, | 96 ScriptState* scriptState, | 
| 39 const DOMArrayPiece& value) { | 97 const DOMArrayPiece& value) { | 
| 40 // TODO(668838): Implement WebBluetooth descriptor.writeValue() | 98 // TODO(668838): Implement WebBluetooth descriptor.writeValue() | 
| 41 return ScriptPromise::rejectWithDOMException( | 99 return ScriptPromise::rejectWithDOMException( | 
| 42 scriptState, | 100 scriptState, | 
| 43 DOMException::create(NotSupportedError, | 101 DOMException::create(NotSupportedError, | 
| 44 "descriptor writeValue is not implemented " | 102 "descriptor writeValue is not implemented " | 
| 45 "yet. See https://goo.gl/J6ASzs")); | 103 "yet. See https://goo.gl/J6ASzs")); | 
| 46 } | 104 } | 
| 47 | 105 | 
| 48 DEFINE_TRACE(BluetoothRemoteGATTDescriptor) { | 106 DEFINE_TRACE(BluetoothRemoteGATTDescriptor) { | 
| 49 visitor->trace(m_characteristic); | 107 visitor->trace(m_characteristic); | 
| 50 visitor->trace(m_value); | 108 visitor->trace(m_value); | 
| 51 } | 109 } | 
| 52 | 110 | 
| 53 } // namespace blink | 111 } // namespace blink | 
| OLD | NEW |