Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/BluetoothRemoteGATTCharacteristic.h" | 5 #include "modules/bluetooth/BluetoothRemoteGATTCharacteristic.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/DOMDataView.h" | 9 #include "core/dom/DOMDataView.h" |
| 10 #include "core/dom/DOMException.h" | 10 #include "core/dom/DOMException.h" |
| 11 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
| 12 #include "core/events/Event.h" | 12 #include "core/events/Event.h" |
| 13 #include "core/inspector/ConsoleMessage.h" | 13 #include "core/inspector/ConsoleMessage.h" |
| 14 #include "modules/bluetooth/Bluetooth.h" | |
| 14 #include "modules/bluetooth/BluetoothCharacteristicProperties.h" | 15 #include "modules/bluetooth/BluetoothCharacteristicProperties.h" |
| 16 #include "modules/bluetooth/BluetoothDevice.h" | |
| 15 #include "modules/bluetooth/BluetoothError.h" | 17 #include "modules/bluetooth/BluetoothError.h" |
| 16 #include "modules/bluetooth/BluetoothRemoteGATTService.h" | 18 #include "modules/bluetooth/BluetoothRemoteGATTService.h" |
| 17 #include "modules/bluetooth/BluetoothSupplement.h" | |
| 18 #include "public/platform/modules/bluetooth/WebBluetooth.h" | |
| 19 #include <memory> | 19 #include <memory> |
| 20 | 20 |
| 21 namespace blink { | 21 namespace blink { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 const char kGATTServerDisconnected[] = | 25 const char kGATTServerDisconnected[] = |
| 26 "GATT Server disconnected while performing a GATT operation."; | 26 "GATT Server disconnected while performing a GATT operation."; |
| 27 const char kGATTServerNotConnected[] = | 27 const char kGATTServerNotConnected[] = |
| 28 "GATT Server is disconnected. Cannot perform GATT operations."; | 28 "GATT Server is disconnected. Cannot perform GATT operations."; |
| 29 const char kInvalidCharacteristic[] = | 29 const char kInvalidCharacteristic[] = |
| 30 "Characteristic is no longer valid. Remember to retrieve the " | 30 "Characteristic is no longer valid. Remember to retrieve the " |
| 31 "characteristic again after reconnecting."; | 31 "characteristic again after reconnecting."; |
| 32 | 32 |
| 33 DOMDataView* ConvertWebVectorToDataView(const WebVector<uint8_t>& webVector) { | 33 DOMDataView* ConvertWTFVectorToDataView(const Vector<uint8_t>& wtfVector) { |
| 34 static_assert(sizeof(*webVector.data()) == 1, | 34 static_assert(sizeof(*wtfVector.data()) == 1, |
| 35 "uint8_t should be a single byte"); | 35 "uint8_t should be a single byte"); |
| 36 DOMArrayBuffer* domBuffer = | 36 DOMArrayBuffer* domBuffer = |
| 37 DOMArrayBuffer::create(webVector.data(), webVector.size()); | 37 DOMArrayBuffer::create(wtfVector.data(), wtfVector.size()); |
| 38 return DOMDataView::create(domBuffer, 0, webVector.size()); | 38 return DOMDataView::create(domBuffer, 0, wtfVector.size()); |
| 39 } | 39 } |
| 40 | 40 |
| 41 } // anonymous namespace | 41 } // anonymous namespace |
| 42 | 42 |
| 43 BluetoothRemoteGATTCharacteristic::BluetoothRemoteGATTCharacteristic( | 43 BluetoothRemoteGATTCharacteristic::BluetoothRemoteGATTCharacteristic( |
| 44 ExecutionContext* context, | 44 ExecutionContext* context, |
| 45 std::unique_ptr<WebBluetoothRemoteGATTCharacteristicInit> webCharacteristic, | 45 const String& characteristicInstanceId, |
| 46 BluetoothRemoteGATTService* service) | 46 const String& serviceInstanceId, |
| 47 const String& uuid, | |
| 48 uint32_t characteristicProperties, | |
| 49 BluetoothRemoteGATTService* service, | |
| 50 BluetoothDevice* device) | |
| 47 : SuspendableObject(context), | 51 : SuspendableObject(context), |
| 48 m_webCharacteristic(std::move(webCharacteristic)), | 52 m_characteristicInstanceId(characteristicInstanceId), |
| 53 m_serviceInstanceId(serviceInstanceId), | |
| 54 m_uuid(uuid), | |
| 55 m_characteristicProperties(characteristicProperties), | |
| 49 m_service(service), | 56 m_service(service), |
| 50 m_stopped(false) { | 57 m_stopped(false), |
| 51 m_properties = BluetoothCharacteristicProperties::create( | 58 m_device(device) { |
| 52 m_webCharacteristic->characteristicProperties); | 59 m_properties = |
| 60 BluetoothCharacteristicProperties::create(m_characteristicProperties); | |
| 53 } | 61 } |
| 54 | 62 |
| 55 BluetoothRemoteGATTCharacteristic* BluetoothRemoteGATTCharacteristic::create( | 63 BluetoothRemoteGATTCharacteristic* BluetoothRemoteGATTCharacteristic::create( |
| 56 ExecutionContext* context, | 64 ExecutionContext* context, |
| 57 std::unique_ptr<WebBluetoothRemoteGATTCharacteristicInit> webCharacteristic, | 65 const String& characteristicInstanceId, |
| 58 BluetoothRemoteGATTService* service) { | 66 const String& serviceInstanceId, |
| 59 DCHECK(webCharacteristic); | 67 const String& uuid, |
| 60 | 68 uint32_t characteristicProperties, |
| 69 BluetoothRemoteGATTService* service, | |
| 70 BluetoothDevice* device) { | |
| 61 BluetoothRemoteGATTCharacteristic* characteristic = | 71 BluetoothRemoteGATTCharacteristic* characteristic = |
| 62 new BluetoothRemoteGATTCharacteristic( | 72 new BluetoothRemoteGATTCharacteristic( |
| 63 context, std::move(webCharacteristic), service); | 73 context, characteristicInstanceId, serviceInstanceId, uuid, |
| 74 characteristicProperties, service, device); | |
| 64 // See note in SuspendableObject about suspendIfNeeded. | 75 // See note in SuspendableObject about suspendIfNeeded. |
| 65 characteristic->suspendIfNeeded(); | 76 characteristic->suspendIfNeeded(); |
| 66 return characteristic; | 77 return characteristic; |
| 67 } | 78 } |
| 68 | 79 |
| 69 void BluetoothRemoteGATTCharacteristic::setValue(DOMDataView* domDataView) { | 80 void BluetoothRemoteGATTCharacteristic::setValue(DOMDataView* domDataView) { |
| 70 m_value = domDataView; | 81 m_value = domDataView; |
| 71 } | 82 } |
| 72 | 83 |
| 73 void BluetoothRemoteGATTCharacteristic::dispatchCharacteristicValueChanged( | 84 void BluetoothRemoteGATTCharacteristic::dispatchCharacteristicValueChanged( |
| 74 const WebVector<uint8_t>& value) { | 85 const Vector<uint8_t>& value) { |
| 75 this->setValue(ConvertWebVectorToDataView(value)); | 86 this->setValue(ConvertWTFVectorToDataView(value)); |
| 76 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); | 87 dispatchEvent(Event::create(EventTypeNames::characteristicvaluechanged)); |
| 77 } | 88 } |
| 78 | 89 |
| 79 void BluetoothRemoteGATTCharacteristic::contextDestroyed() { | 90 void BluetoothRemoteGATTCharacteristic::contextDestroyed() { |
| 80 notifyCharacteristicObjectRemoved(); | 91 notifyCharacteristicObjectRemoved(); |
| 81 } | 92 } |
| 82 | 93 |
| 83 void BluetoothRemoteGATTCharacteristic::dispose() { | 94 void BluetoothRemoteGATTCharacteristic::dispose() { |
| 84 notifyCharacteristicObjectRemoved(); | 95 notifyCharacteristicObjectRemoved(); |
| 85 } | 96 } |
| 86 | 97 |
| 87 void BluetoothRemoteGATTCharacteristic::notifyCharacteristicObjectRemoved() { | 98 void BluetoothRemoteGATTCharacteristic::notifyCharacteristicObjectRemoved() { |
| 88 if (!m_stopped) { | 99 if (!m_stopped) { |
| 89 m_stopped = true; | 100 m_stopped = true; |
| 90 WebBluetooth* webbluetooth = BluetoothSupplement::fromExecutionContext( | 101 m_device->bluetooth()->characteristicObjectRemoved( |
| 91 SuspendableObject::getExecutionContext()); | 102 m_characteristicInstanceId); |
| 92 webbluetooth->characteristicObjectRemoved( | |
| 93 m_webCharacteristic->characteristicInstanceID, this); | |
| 94 } | 103 } |
| 95 } | 104 } |
| 96 | 105 |
| 97 const WTF::AtomicString& BluetoothRemoteGATTCharacteristic::interfaceName() | 106 const WTF::AtomicString& BluetoothRemoteGATTCharacteristic::interfaceName() |
| 98 const { | 107 const { |
| 99 return EventTargetNames::BluetoothRemoteGATTCharacteristic; | 108 return EventTargetNames::BluetoothRemoteGATTCharacteristic; |
| 100 } | 109 } |
| 101 | 110 |
| 102 ExecutionContext* BluetoothRemoteGATTCharacteristic::getExecutionContext() | 111 ExecutionContext* BluetoothRemoteGATTCharacteristic::getExecutionContext() |
| 103 const { | 112 const { |
| 104 return SuspendableObject::getExecutionContext(); | 113 return SuspendableObject::getExecutionContext(); |
| 105 } | 114 } |
| 106 | 115 |
| 107 void BluetoothRemoteGATTCharacteristic::addedEventListener( | 116 void BluetoothRemoteGATTCharacteristic::addedEventListener( |
| 108 const AtomicString& eventType, | 117 const AtomicString& eventType, |
| 109 RegisteredEventListener& registeredListener) { | 118 RegisteredEventListener& registeredListener) { |
| 110 EventTargetWithInlineData::addedEventListener(eventType, registeredListener); | 119 EventTargetWithInlineData::addedEventListener(eventType, registeredListener); |
| 111 // We will also need to unregister a characteristic once all the event | 120 // We will also need to unregister a characteristic once all the event |
| 112 // listeners have been removed. See http://crbug.com/541390 | 121 // listeners have been removed. See http://crbug.com/541390 |
| 113 if (eventType == EventTypeNames::characteristicvaluechanged) { | 122 if (eventType == EventTypeNames::characteristicvaluechanged) { |
| 114 WebBluetooth* webbluetooth = | 123 m_device->bluetooth()->registerCharacteristicObject( |
| 115 BluetoothSupplement::fromExecutionContext(getExecutionContext()); | 124 m_characteristicInstanceId, this); |
| 116 webbluetooth->registerCharacteristicObject( | |
| 117 m_webCharacteristic->characteristicInstanceID, this); | |
| 118 } | 125 } |
| 119 } | 126 } |
| 120 | 127 |
| 121 class ReadValueCallback : public WebBluetoothReadValueCallbacks { | 128 void BluetoothRemoteGATTCharacteristic::ReadValueCallback( |
| 122 public: | 129 ScriptPromiseResolver* resolver, |
| 123 ReadValueCallback(BluetoothRemoteGATTCharacteristic* characteristic, | 130 mojom::blink::WebBluetoothResult result, |
| 124 ScriptPromiseResolver* resolver) | 131 const Optional<Vector<uint8_t>>& value) { |
| 125 : m_characteristic(characteristic), m_resolver(resolver) { | 132 if (!resolver->getExecutionContext() || |
| 126 // We always check that the device is connected before constructing this | 133 resolver->getExecutionContext()->isContextDestroyed()) |
| 127 // object. | 134 return; |
|
Reilly Grant (use Gerrit)
2016/12/16 01:41:29
This check is unnecessary. It is performed by
Scri
juncai
2016/12/17 01:18:52
Keep this check if the callbacks do more than Scri
| |
| 128 CHECK(m_characteristic->gatt()->connected()); | |
| 129 m_characteristic->gatt()->AddToActiveAlgorithms(m_resolver.get()); | |
| 130 } | |
| 131 | 135 |
| 132 void onSuccess(const WebVector<uint8_t>& value) override { | 136 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 133 if (!m_resolver->getExecutionContext() || | 137 DCHECK(value); |
| 134 m_resolver->getExecutionContext()->isContextDestroyed()) | 138 if (!gatt()->RemoveFromActiveAlgorithms(resolver)) { |
| 135 return; | 139 resolver->reject( |
| 136 | |
| 137 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms( | |
| 138 m_resolver.get())) { | |
| 139 m_resolver->reject( | |
| 140 DOMException::create(NetworkError, kGATTServerDisconnected)); | 140 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 141 return; | 141 return; |
| 142 } | 142 } |
| 143 | 143 |
| 144 DOMDataView* domDataView = ConvertWebVectorToDataView(value); | 144 DOMDataView* domDataView = ConvertWTFVectorToDataView(value.value()); |
| 145 if (m_characteristic) | 145 setValue(domDataView); |
| 146 m_characteristic->setValue(domDataView); | 146 resolver->resolve(domDataView); |
| 147 | 147 } else { |
| 148 m_resolver->resolve(domDataView); | 148 if (!gatt()->RemoveFromActiveAlgorithms(resolver)) { |
| 149 } | 149 resolver->reject( |
| 150 | |
| 151 void onError( | |
| 152 int32_t | |
| 153 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) | |
| 154 override { | |
| 155 if (!m_resolver->getExecutionContext() || | |
| 156 m_resolver->getExecutionContext()->isContextDestroyed()) | |
| 157 return; | |
| 158 | |
| 159 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms( | |
| 160 m_resolver.get())) { | |
| 161 m_resolver->reject( | |
| 162 DOMException::create(NetworkError, kGATTServerDisconnected)); | 150 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 163 return; | 151 return; |
| 164 } | 152 } |
| 165 | 153 |
| 166 m_resolver->reject(BluetoothError::take(m_resolver, error)); | 154 resolver->reject(BluetoothError::take(resolver, static_cast<int>(result))); |
|
dougt
2016/12/16 00:11:44
It would be great if BluetoothError::take would ac
juncai
2016/12/17 01:18:52
Done.
| |
| 167 } | 155 } |
| 168 | 156 } |
| 169 private: | |
| 170 Persistent<BluetoothRemoteGATTCharacteristic> m_characteristic; | |
| 171 Persistent<ScriptPromiseResolver> m_resolver; | |
| 172 }; | |
| 173 | 157 |
| 174 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( | 158 ScriptPromise BluetoothRemoteGATTCharacteristic::readValue( |
| 175 ScriptState* scriptState) { | 159 ScriptState* scriptState) { |
| 176 if (!gatt()->connected()) { | 160 if (!gatt()->connected()) { |
| 177 return ScriptPromise::rejectWithDOMException( | 161 return ScriptPromise::rejectWithDOMException( |
| 178 scriptState, | 162 scriptState, |
| 179 DOMException::create(NetworkError, kGATTServerNotConnected)); | 163 DOMException::create(NetworkError, kGATTServerNotConnected)); |
| 180 } | 164 } |
| 181 | 165 |
| 182 if (!gatt()->device()->isValidCharacteristic( | 166 if (!gatt()->device()->isValidCharacteristic(m_characteristicInstanceId)) { |
| 183 m_webCharacteristic->characteristicInstanceID)) { | |
| 184 return ScriptPromise::rejectWithDOMException( | 167 return ScriptPromise::rejectWithDOMException( |
| 185 scriptState, | 168 scriptState, |
| 186 DOMException::create(InvalidStateError, kInvalidCharacteristic)); | 169 DOMException::create(InvalidStateError, kInvalidCharacteristic)); |
| 187 } | 170 } |
| 188 | 171 |
| 189 WebBluetooth* webbluetooth = | |
| 190 BluetoothSupplement::fromScriptState(scriptState); | |
| 191 | |
| 192 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 172 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 193 ScriptPromise promise = resolver->promise(); | 173 ScriptPromise promise = resolver->promise(); |
| 194 webbluetooth->readValue(m_webCharacteristic->characteristicInstanceID, | 174 |
| 195 new ReadValueCallback(this, resolver)); | 175 // We always check that the device is connected. |
| 176 CHECK(gatt()->connected()); | |
| 177 gatt()->AddToActiveAlgorithms(resolver); | |
| 178 | |
| 179 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | |
| 180 service->RemoteCharacteristicReadValue( | |
| 181 m_characteristicInstanceId, | |
| 182 convertToBaseCallback( | |
| 183 WTF::bind(&BluetoothRemoteGATTCharacteristic::ReadValueCallback, | |
| 184 wrapPersistent(this), wrapPersistent(resolver)))); | |
| 196 | 185 |
| 197 return promise; | 186 return promise; |
| 198 } | 187 } |
| 199 | 188 |
| 200 class WriteValueCallback : public WebBluetoothWriteValueCallbacks { | 189 void BluetoothRemoteGATTCharacteristic::WriteValueCallback( |
| 201 public: | 190 ScriptPromiseResolver* resolver, |
| 202 WriteValueCallback(BluetoothRemoteGATTCharacteristic* characteristic, | 191 const Vector<uint8_t>& value, |
| 203 ScriptPromiseResolver* resolver) | 192 mojom::blink::WebBluetoothResult result) { |
| 204 : m_characteristic(characteristic), m_resolver(resolver) { | 193 if (!resolver->getExecutionContext() || |
| 205 // We always check that the device is connected before constructing this | 194 resolver->getExecutionContext()->isContextDestroyed()) |
| 206 // object. | 195 return; |
|
Reilly Grant (use Gerrit)
2016/12/16 01:41:29
This check is unnecessary. It is performed by
Scri
juncai
2016/12/17 01:18:52
ditto.
| |
| 207 CHECK(m_characteristic->gatt()->connected()); | |
| 208 m_characteristic->gatt()->AddToActiveAlgorithms(m_resolver.get()); | |
| 209 } | |
| 210 | 196 |
| 211 void onSuccess(const WebVector<uint8_t>& value) override { | 197 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 212 if (!m_resolver->getExecutionContext() || | 198 if (!gatt()->RemoveFromActiveAlgorithms(resolver)) { |
| 213 m_resolver->getExecutionContext()->isContextDestroyed()) | 199 resolver->reject( |
| 214 return; | |
| 215 | |
| 216 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms( | |
| 217 m_resolver.get())) { | |
| 218 m_resolver->reject( | |
| 219 DOMException::create(NetworkError, kGATTServerDisconnected)); | 200 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 220 return; | 201 return; |
| 221 } | 202 } |
| 222 | 203 |
| 223 if (m_characteristic) { | 204 setValue(ConvertWTFVectorToDataView(value)); |
| 224 m_characteristic->setValue(ConvertWebVectorToDataView(value)); | 205 resolver->resolve(); |
| 225 } | 206 } else { |
| 226 m_resolver->resolve(); | 207 if (!gatt()->RemoveFromActiveAlgorithms(resolver)) { |
| 227 } | 208 resolver->reject( |
| 228 | |
| 229 void onError( | |
| 230 int32_t | |
| 231 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) | |
| 232 override { | |
| 233 if (!m_resolver->getExecutionContext() || | |
| 234 m_resolver->getExecutionContext()->isContextDestroyed()) | |
| 235 return; | |
| 236 | |
| 237 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms( | |
| 238 m_resolver.get())) { | |
| 239 m_resolver->reject( | |
| 240 DOMException::create(NetworkError, kGATTServerDisconnected)); | 209 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 241 return; | 210 return; |
| 242 } | 211 } |
| 243 | 212 |
| 244 m_resolver->reject(BluetoothError::take(m_resolver, error)); | 213 resolver->reject(BluetoothError::take(resolver, static_cast<int>(result))); |
|
dougt
2016/12/16 00:11:44
same
juncai
2016/12/17 01:18:52
Done.
| |
| 245 } | 214 } |
| 246 | 215 } |
| 247 private: | |
| 248 Persistent<BluetoothRemoteGATTCharacteristic> m_characteristic; | |
| 249 Persistent<ScriptPromiseResolver> m_resolver; | |
| 250 }; | |
| 251 | 216 |
| 252 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue( | 217 ScriptPromise BluetoothRemoteGATTCharacteristic::writeValue( |
| 253 ScriptState* scriptState, | 218 ScriptState* scriptState, |
| 254 const DOMArrayPiece& value) { | 219 const DOMArrayPiece& value) { |
| 255 if (!gatt()->connected()) { | 220 if (!gatt()->connected()) { |
| 256 return ScriptPromise::rejectWithDOMException( | 221 return ScriptPromise::rejectWithDOMException( |
| 257 scriptState, | 222 scriptState, |
| 258 DOMException::create(NetworkError, kGATTServerNotConnected)); | 223 DOMException::create(NetworkError, kGATTServerNotConnected)); |
| 259 } | 224 } |
| 260 | 225 |
| 261 if (!gatt()->device()->isValidCharacteristic( | 226 if (!gatt()->device()->isValidCharacteristic(m_characteristicInstanceId)) { |
| 262 m_webCharacteristic->characteristicInstanceID)) { | |
| 263 return ScriptPromise::rejectWithDOMException( | 227 return ScriptPromise::rejectWithDOMException( |
| 264 scriptState, | 228 scriptState, |
| 265 DOMException::create(InvalidStateError, kInvalidCharacteristic)); | 229 DOMException::create(InvalidStateError, kInvalidCharacteristic)); |
| 266 } | 230 } |
| 267 | 231 |
| 268 WebBluetooth* webbluetooth = | |
| 269 BluetoothSupplement::fromScriptState(scriptState); | |
| 270 // Partial implementation of writeValue algorithm: | 232 // Partial implementation of writeValue algorithm: |
| 271 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattchar acteristic-writevalue | 233 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattchar acteristic-writevalue |
| 272 | 234 |
| 273 // If bytes is more than 512 bytes long (the maximum length of an attribute | 235 // If bytes is more than 512 bytes long (the maximum length of an attribute |
| 274 // value, per Long Attribute Values) return a promise rejected with an | 236 // value, per Long Attribute Values) return a promise rejected with an |
| 275 // InvalidModificationError and abort. | 237 // InvalidModificationError and abort. |
| 276 if (value.byteLength() > 512) | 238 if (value.byteLength() > 512) |
| 277 return ScriptPromise::rejectWithDOMException( | 239 return ScriptPromise::rejectWithDOMException( |
| 278 scriptState, DOMException::create(InvalidModificationError, | 240 scriptState, DOMException::create(InvalidModificationError, |
| 279 "Value can't exceed 512 bytes.")); | 241 "Value can't exceed 512 bytes.")); |
| 280 | 242 |
| 281 // Let valueVector be a copy of the bytes held by value. | 243 // Let valueVector be a copy of the bytes held by value. |
| 282 WebVector<uint8_t> valueVector(value.bytes(), value.byteLength()); | 244 Vector<uint8_t> valueVector; |
| 245 valueVector.append(value.bytes(), value.byteLength()); | |
|
dougt
2016/12/16 00:11:44
Do we need this change?
juncai
2016/12/17 01:18:52
I think so.
https://cs.chromium.org/chromium/src/o
| |
| 283 | 246 |
| 284 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 247 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 248 ScriptPromise promise = resolver->promise(); | |
| 285 | 249 |
| 286 ScriptPromise promise = resolver->promise(); | 250 // We always check that the device is connected. |
| 287 webbluetooth->writeValue(m_webCharacteristic->characteristicInstanceID, | 251 CHECK(gatt()->connected()); |
|
dougt
2016/12/16 00:11:44
this is a crash if we're not connected here. Shoul
juncai
2016/12/17 01:18:52
I think this line can just be removed since this c
| |
| 288 valueVector, new WriteValueCallback(this, resolver)); | 252 gatt()->AddToActiveAlgorithms(resolver); |
| 253 | |
| 254 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | |
| 255 service->RemoteCharacteristicWriteValue( | |
| 256 m_characteristicInstanceId, valueVector, | |
| 257 convertToBaseCallback(WTF::bind( | |
| 258 &BluetoothRemoteGATTCharacteristic::WriteValueCallback, | |
| 259 wrapPersistent(this), wrapPersistent(resolver), valueVector))); | |
| 289 | 260 |
| 290 return promise; | 261 return promise; |
| 291 } | 262 } |
| 292 | 263 |
| 293 class NotificationsCallback : public WebBluetoothNotificationsCallbacks { | 264 void BluetoothRemoteGATTCharacteristic::NotificationsCallback( |
| 294 public: | 265 ScriptPromiseResolver* resolver, |
| 295 NotificationsCallback(BluetoothRemoteGATTCharacteristic* characteristic, | 266 mojom::blink::WebBluetoothResult result) { |
| 296 ScriptPromiseResolver* resolver) | 267 if (!resolver->getExecutionContext() || |
| 297 : m_characteristic(characteristic), m_resolver(resolver) { | 268 resolver->getExecutionContext()->isContextDestroyed()) |
| 298 // We always check that the device is connected before constructing this | 269 return; |
|
Reilly Grant (use Gerrit)
2016/12/16 01:41:29
This check is unnecessary. It is performed by Scri
juncai
2016/12/17 01:18:52
ditto.
| |
| 299 // object. | |
| 300 CHECK(m_characteristic->gatt()->connected()); | |
| 301 m_characteristic->gatt()->AddToActiveAlgorithms(m_resolver.get()); | |
| 302 } | |
| 303 | 270 |
| 304 void onSuccess() override { | 271 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 305 if (!m_resolver->getExecutionContext() || | 272 if (!gatt()->RemoveFromActiveAlgorithms(resolver)) { |
| 306 m_resolver->getExecutionContext()->isContextDestroyed()) | 273 resolver->reject( |
| 307 return; | |
| 308 | |
| 309 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms( | |
| 310 m_resolver.get())) { | |
| 311 m_resolver->reject( | |
| 312 DOMException::create(NetworkError, kGATTServerDisconnected)); | 274 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 313 return; | 275 return; |
| 314 } | 276 } |
| 315 | 277 |
| 316 m_resolver->resolve(m_characteristic); | 278 resolver->resolve(this); |
| 317 } | 279 } else { |
| 318 | 280 if (!gatt()->RemoveFromActiveAlgorithms(resolver)) { |
| 319 void onError( | 281 resolver->reject( |
| 320 int32_t | |
| 321 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) | |
| 322 override { | |
| 323 if (!m_resolver->getExecutionContext() || | |
| 324 m_resolver->getExecutionContext()->isContextDestroyed()) | |
| 325 return; | |
| 326 | |
| 327 if (!m_characteristic->gatt()->RemoveFromActiveAlgorithms( | |
| 328 m_resolver.get())) { | |
| 329 m_resolver->reject( | |
| 330 DOMException::create(NetworkError, kGATTServerDisconnected)); | 282 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 331 return; | 283 return; |
| 332 } | 284 } |
| 333 | 285 |
| 334 m_resolver->reject(BluetoothError::take(m_resolver, error)); | 286 resolver->reject(BluetoothError::take(resolver, static_cast<int>(result))); |
|
dougt
2016/12/16 00:11:44
same as above. Would be nice if ::take() accepted
juncai
2016/12/17 01:18:52
Done.
| |
| 335 } | 287 } |
| 336 | 288 } |
| 337 private: | |
| 338 Persistent<BluetoothRemoteGATTCharacteristic> m_characteristic; | |
| 339 Persistent<ScriptPromiseResolver> m_resolver; | |
| 340 }; | |
| 341 | 289 |
| 342 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( | 290 ScriptPromise BluetoothRemoteGATTCharacteristic::startNotifications( |
| 343 ScriptState* scriptState) { | 291 ScriptState* scriptState) { |
| 344 if (!gatt()->connected()) { | 292 if (!gatt()->connected()) { |
| 345 return ScriptPromise::rejectWithDOMException( | 293 return ScriptPromise::rejectWithDOMException( |
| 346 scriptState, | 294 scriptState, |
| 347 DOMException::create(NetworkError, kGATTServerNotConnected)); | 295 DOMException::create(NetworkError, kGATTServerNotConnected)); |
| 348 } | 296 } |
| 349 | 297 |
| 350 if (!gatt()->device()->isValidCharacteristic( | 298 if (!gatt()->device()->isValidCharacteristic(m_characteristicInstanceId)) { |
| 351 m_webCharacteristic->characteristicInstanceID)) { | |
| 352 return ScriptPromise::rejectWithDOMException( | 299 return ScriptPromise::rejectWithDOMException( |
| 353 scriptState, | 300 scriptState, |
| 354 DOMException::create(InvalidStateError, kInvalidCharacteristic)); | 301 DOMException::create(InvalidStateError, kInvalidCharacteristic)); |
| 355 } | 302 } |
| 356 | 303 |
| 357 WebBluetooth* webbluetooth = | |
| 358 BluetoothSupplement::fromScriptState(scriptState); | |
| 359 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 304 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 360 ScriptPromise promise = resolver->promise(); | 305 ScriptPromise promise = resolver->promise(); |
| 361 webbluetooth->startNotifications( | 306 |
| 362 m_webCharacteristic->characteristicInstanceID, | 307 // We always check that the device is connected. |
| 363 new NotificationsCallback(this, resolver)); | 308 CHECK(gatt()->connected()); |
| 309 gatt()->AddToActiveAlgorithms(resolver); | |
| 310 | |
| 311 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | |
| 312 service->RemoteCharacteristicStartNotifications( | |
| 313 m_characteristicInstanceId, | |
| 314 convertToBaseCallback( | |
| 315 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, | |
| 316 wrapPersistent(this), wrapPersistent(resolver)))); | |
| 317 | |
| 364 return promise; | 318 return promise; |
| 365 } | 319 } |
| 366 | 320 |
| 367 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications( | 321 ScriptPromise BluetoothRemoteGATTCharacteristic::stopNotifications( |
| 368 ScriptState* scriptState) { | 322 ScriptState* scriptState) { |
| 369 if (!gatt()->connected()) { | 323 if (!gatt()->connected()) { |
| 370 return ScriptPromise::rejectWithDOMException( | 324 return ScriptPromise::rejectWithDOMException( |
| 371 scriptState, | 325 scriptState, |
| 372 DOMException::create(NetworkError, kGATTServerNotConnected)); | 326 DOMException::create(NetworkError, kGATTServerNotConnected)); |
| 373 } | 327 } |
| 374 | 328 |
| 375 if (!gatt()->device()->isValidCharacteristic( | 329 if (!gatt()->device()->isValidCharacteristic(m_characteristicInstanceId)) { |
| 376 m_webCharacteristic->characteristicInstanceID)) { | |
| 377 return ScriptPromise::rejectWithDOMException( | 330 return ScriptPromise::rejectWithDOMException( |
| 378 scriptState, | 331 scriptState, |
| 379 DOMException::create(InvalidStateError, kInvalidCharacteristic)); | 332 DOMException::create(InvalidStateError, kInvalidCharacteristic)); |
| 380 } | 333 } |
| 381 | 334 |
| 382 WebBluetooth* webbluetooth = | |
| 383 BluetoothSupplement::fromScriptState(scriptState); | |
| 384 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 335 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 385 ScriptPromise promise = resolver->promise(); | 336 ScriptPromise promise = resolver->promise(); |
| 386 webbluetooth->stopNotifications(m_webCharacteristic->characteristicInstanceID, | 337 |
| 387 new NotificationsCallback(this, resolver)); | 338 // We always check that the device is connected. |
| 339 CHECK(gatt()->connected()); | |
| 340 gatt()->AddToActiveAlgorithms(resolver); | |
| 341 | |
| 342 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | |
| 343 service->RemoteCharacteristicStopNotifications( | |
| 344 m_characteristicInstanceId, | |
| 345 convertToBaseCallback( | |
| 346 WTF::bind(&BluetoothRemoteGATTCharacteristic::NotificationsCallback, | |
| 347 wrapPersistent(this), wrapPersistent(resolver), | |
| 348 mojom::blink::WebBluetoothResult::SUCCESS))); | |
| 388 return promise; | 349 return promise; |
| 389 } | 350 } |
| 390 | 351 |
| 391 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { | 352 DEFINE_TRACE(BluetoothRemoteGATTCharacteristic) { |
| 392 visitor->trace(m_service); | 353 visitor->trace(m_service); |
| 393 visitor->trace(m_properties); | 354 visitor->trace(m_properties); |
| 394 visitor->trace(m_value); | 355 visitor->trace(m_value); |
| 356 visitor->trace(m_device); | |
| 395 EventTargetWithInlineData::trace(visitor); | 357 EventTargetWithInlineData::trace(visitor); |
| 396 SuspendableObject::trace(visitor); | 358 SuspendableObject::trace(visitor); |
| 397 } | 359 } |
| 398 | 360 |
| 399 } // namespace blink | 361 } // namespace blink |
| OLD | NEW |