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