| 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" | 7 #include "bindings/core/v8/ScriptPromise.h" |
| 8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 9 #include "core/dom/DOMException.h" | 9 #include "core/dom/DOMException.h" |
| 10 #include "modules/bluetooth/Bluetooth.h" | |
| 11 #include "modules/bluetooth/BluetoothError.h" | 10 #include "modules/bluetooth/BluetoothError.h" |
| 12 #include "modules/bluetooth/BluetoothRemoteGATTService.h" | 11 #include "modules/bluetooth/BluetoothRemoteGATTService.h" |
| 13 #include "modules/bluetooth/BluetoothRemoteGATTUtils.h" | 12 #include "modules/bluetooth/BluetoothRemoteGATTUtils.h" |
| 14 #include <memory> | 13 #include <memory> |
| 15 | 14 |
| 16 namespace blink { | 15 namespace blink { |
| 17 | 16 |
| 18 BluetoothRemoteGATTDescriptor::BluetoothRemoteGATTDescriptor( | 17 BluetoothRemoteGATTDescriptor::BluetoothRemoteGATTDescriptor( |
| 19 mojom::blink::WebBluetoothRemoteGATTDescriptorPtr descriptor, | 18 mojom::blink::WebBluetoothRemoteGATTDescriptorPtr descriptor, |
| 20 BluetoothRemoteGATTCharacteristic* characteristic) | 19 BluetoothRemoteGATTCharacteristic* characteristic) |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 if (!getGatt()->device()->isValidDescriptor(m_descriptor->instance_id)) { | 66 if (!getGatt()->device()->isValidDescriptor(m_descriptor->instance_id)) { |
| 68 return ScriptPromise::rejectWithDOMException( | 67 return ScriptPromise::rejectWithDOMException( |
| 69 scriptState, | 68 scriptState, |
| 70 BluetoothRemoteGATTUtils::CreateDOMException( | 69 BluetoothRemoteGATTUtils::CreateDOMException( |
| 71 BluetoothRemoteGATTUtils::ExceptionType::kInvalidDescriptor)); | 70 BluetoothRemoteGATTUtils::ExceptionType::kInvalidDescriptor)); |
| 72 } | 71 } |
| 73 | 72 |
| 74 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 73 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 75 ScriptPromise promise = resolver->promise(); | 74 ScriptPromise promise = resolver->promise(); |
| 76 getGatt()->AddToActiveAlgorithms(resolver); | 75 getGatt()->AddToActiveAlgorithms(resolver); |
| 77 | 76 getService()->RemoteDescriptorReadValue( |
| 78 mojom::blink::WebBluetoothService* service = | |
| 79 m_characteristic->m_device->bluetooth()->service(); | |
| 80 service->RemoteDescriptorReadValue( | |
| 81 m_descriptor->instance_id, | 77 m_descriptor->instance_id, |
| 82 convertToBaseCallback( | 78 convertToBaseCallback( |
| 83 WTF::bind(&BluetoothRemoteGATTDescriptor::ReadValueCallback, | 79 WTF::bind(&BluetoothRemoteGATTDescriptor::ReadValueCallback, |
| 84 wrapPersistent(this), wrapPersistent(resolver)))); | 80 wrapPersistent(this), wrapPersistent(resolver)))); |
| 85 | 81 |
| 86 return promise; | 82 return promise; |
| 87 } | 83 } |
| 88 | 84 |
| 85 void BluetoothRemoteGATTDescriptor::WriteValueCallback( |
| 86 ScriptPromiseResolver* resolver, |
| 87 const Vector<uint8_t>& value, |
| 88 mojom::blink::WebBluetoothResult result) { |
| 89 if (!resolver->getExecutionContext() || |
| 90 resolver->getExecutionContext()->isContextDestroyed()) |
| 91 return; |
| 92 |
| 93 // If the resolver is not in the set of ActiveAlgorithms then the frame |
| 94 // disconnected so we reject. |
| 95 if (!getGatt()->RemoveFromActiveAlgorithms(resolver)) { |
| 96 resolver->reject(BluetoothRemoteGATTUtils::CreateDOMException( |
| 97 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerDisconnected)); |
| 98 return; |
| 99 } |
| 100 |
| 101 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 102 m_value = BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(value); |
| 103 resolver->resolve(); |
| 104 } else { |
| 105 resolver->reject(BluetoothError::take(resolver, result)); |
| 106 } |
| 107 } |
| 108 |
| 89 ScriptPromise BluetoothRemoteGATTDescriptor::writeValue( | 109 ScriptPromise BluetoothRemoteGATTDescriptor::writeValue( |
| 90 ScriptState* scriptState, | 110 ScriptState* scriptState, |
| 91 const DOMArrayPiece& value) { | 111 const DOMArrayPiece& value) { |
| 92 // TODO(668838): Implement WebBluetooth descriptor.writeValue() | 112 if (!getGatt()->connected()) { |
| 93 return ScriptPromise::rejectWithDOMException( | 113 return ScriptPromise::rejectWithDOMException( |
| 94 scriptState, | 114 scriptState, |
| 95 DOMException::create(NotSupportedError, | 115 BluetoothRemoteGATTUtils::CreateDOMException( |
| 96 "descriptor writeValue is not implemented " | 116 BluetoothRemoteGATTUtils::ExceptionType::kGATTServerNotConnected)); |
| 97 "yet. See https://goo.gl/J6ASzs")); | 117 } |
| 118 |
| 119 if (!getGatt()->device()->isValidDescriptor(m_descriptor->instance_id)) { |
| 120 return ScriptPromise::rejectWithDOMException( |
| 121 scriptState, |
| 122 BluetoothRemoteGATTUtils::CreateDOMException( |
| 123 BluetoothRemoteGATTUtils::ExceptionType::kInvalidDescriptor)); |
| 124 } |
| 125 |
| 126 // Partial implementation of writeValue algorithm: |
| 127 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattdesc
riptor-writevalue |
| 128 |
| 129 // If bytes is more than 512 bytes long (the maximum length of an attribute |
| 130 // value, per Long Attribute Values) return a promise rejected with an |
| 131 // InvalidModificationError and abort. |
| 132 if (value.byteLength() > 512) { |
| 133 return ScriptPromise::rejectWithDOMException( |
| 134 scriptState, DOMException::create(InvalidModificationError, |
| 135 "Value can't exceed 512 bytes.")); |
| 136 } |
| 137 |
| 138 // Let valueVector be a copy of the bytes held by value. |
| 139 Vector<uint8_t> valueVector; |
| 140 valueVector.append(value.bytes(), value.byteLength()); |
| 141 |
| 142 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 143 ScriptPromise promise = resolver->promise(); |
| 144 getGatt()->AddToActiveAlgorithms(resolver); |
| 145 getService()->RemoteDescriptorWriteValue( |
| 146 m_descriptor->instance_id, valueVector, |
| 147 convertToBaseCallback(WTF::bind( |
| 148 &BluetoothRemoteGATTDescriptor::WriteValueCallback, |
| 149 wrapPersistent(this), wrapPersistent(resolver), valueVector))); |
| 150 |
| 151 return promise; |
| 98 } | 152 } |
| 99 | 153 |
| 100 DEFINE_TRACE(BluetoothRemoteGATTDescriptor) { | 154 DEFINE_TRACE(BluetoothRemoteGATTDescriptor) { |
| 101 visitor->trace(m_characteristic); | 155 visitor->trace(m_characteristic); |
| 102 visitor->trace(m_value); | 156 visitor->trace(m_value); |
| 103 } | 157 } |
| 104 | 158 |
| 105 } // namespace blink | 159 } // namespace blink |
| OLD | NEW |