| 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/BluetoothRemoteGATTService.h" | 5 #include "modules/bluetooth/BluetoothRemoteGATTService.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 "core/dom/ExceptionCode.h" | 10 #include "core/dom/ExceptionCode.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 visitor->trace(m_device); | 43 visitor->trace(m_device); |
| 44 } | 44 } |
| 45 | 45 |
| 46 // Class that allows us to resolve the promise with a single Characteristic or | 46 // Class that allows us to resolve the promise with a single Characteristic or |
| 47 // with a vector owning the characteristics. | 47 // with a vector owning the characteristics. |
| 48 class GetCharacteristicsCallback | 48 class GetCharacteristicsCallback |
| 49 : public WebBluetoothGetCharacteristicsCallbacks { | 49 : public WebBluetoothGetCharacteristicsCallbacks { |
| 50 public: | 50 public: |
| 51 GetCharacteristicsCallback( | 51 GetCharacteristicsCallback( |
| 52 BluetoothRemoteGATTService* service, | 52 BluetoothRemoteGATTService* service, |
| 53 const String characteristicsUUID, |
| 53 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 54 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
| 54 ScriptPromiseResolver* resolver) | 55 ScriptPromiseResolver* resolver) |
| 55 : m_service(service), m_quantity(quantity), m_resolver(resolver) { | 56 : m_service(service), |
| 57 m_characteristicsUUID(characteristicsUUID), |
| 58 m_quantity(quantity), |
| 59 m_resolver(resolver) { |
| 56 // We always check that the device is connected before constructing this | 60 // We always check that the device is connected before constructing this |
| 57 // object. | 61 // object. |
| 58 CHECK(m_service->device()->gatt()->connected()); | 62 CHECK(m_service->device()->gatt()->connected()); |
| 59 m_service->device()->gatt()->AddToActiveAlgorithms(m_resolver.get()); | 63 m_service->device()->gatt()->AddToActiveAlgorithms(m_resolver.get()); |
| 60 } | 64 } |
| 61 | 65 |
| 62 void onSuccess(const WebVector<WebBluetoothRemoteGATTCharacteristicInit*>& | 66 void onSuccess(const WebVector<WebBluetoothRemoteGATTCharacteristicInit*>& |
| 63 webCharacteristics) override { | 67 webCharacteristics) override { |
| 64 if (!m_resolver->getExecutionContext() || | 68 if (!m_resolver->getExecutionContext() || |
| 65 m_resolver->getExecutionContext()->isContextDestroyed()) | 69 m_resolver->getExecutionContext()->isContextDestroyed()) |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 return; | 108 return; |
| 105 | 109 |
| 106 // If the resolver is not in the set of ActiveAlgorithms then the frame | 110 // If the resolver is not in the set of ActiveAlgorithms then the frame |
| 107 // disconnected so we reject. | 111 // disconnected so we reject. |
| 108 if (!m_service->device()->gatt()->RemoveFromActiveAlgorithms( | 112 if (!m_service->device()->gatt()->RemoveFromActiveAlgorithms( |
| 109 m_resolver.get())) { | 113 m_resolver.get())) { |
| 110 m_resolver->reject( | 114 m_resolver->reject( |
| 111 DOMException::create(NetworkError, kGATTServerDisconnected)); | 115 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 112 return; | 116 return; |
| 113 } | 117 } |
| 114 | 118 if (!m_characteristicsUUID.isEmpty() && |
| 115 m_resolver->reject(BluetoothError::take(m_resolver, error)); | 119 BluetoothError::isSameError( |
| 120 error, |
| 121 mojom::blink::WebBluetoothResult::CHARACTERISTIC_NOT_FOUND)) { |
| 122 m_resolver->reject(BluetoothError::take( |
| 123 m_resolver, error, |
| 124 "No Characteristics matching UUID " + m_characteristicsUUID + |
| 125 " found in Service with UUID " + m_service->uuid() + ".")); |
| 126 } else { |
| 127 m_resolver->reject(BluetoothError::take(m_resolver, error)); |
| 128 } |
| 116 } | 129 } |
| 117 | 130 |
| 118 private: | 131 private: |
| 119 Persistent<BluetoothRemoteGATTService> m_service; | 132 Persistent<BluetoothRemoteGATTService> m_service; |
| 133 const String m_characteristicsUUID; |
| 120 mojom::blink::WebBluetoothGATTQueryQuantity m_quantity; | 134 mojom::blink::WebBluetoothGATTQueryQuantity m_quantity; |
| 121 const Persistent<ScriptPromiseResolver> m_resolver; | 135 const Persistent<ScriptPromiseResolver> m_resolver; |
| 122 }; | 136 }; |
| 123 | 137 |
| 124 ScriptPromise BluetoothRemoteGATTService::getCharacteristic( | 138 ScriptPromise BluetoothRemoteGATTService::getCharacteristic( |
| 125 ScriptState* scriptState, | 139 ScriptState* scriptState, |
| 126 const StringOrUnsignedLong& characteristic, | 140 const StringOrUnsignedLong& characteristic, |
| 127 ExceptionState& exceptionState) { | 141 ExceptionState& exceptionState) { |
| 128 String characteristicUUID = | 142 String characteristicUUID = |
| 129 BluetoothUUID::getCharacteristic(characteristic, exceptionState); | 143 BluetoothUUID::getCharacteristic(characteristic, exceptionState); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 scriptState, DOMException::create(InvalidStateError, kInvalidService)); | 185 scriptState, DOMException::create(InvalidStateError, kInvalidService)); |
| 172 } | 186 } |
| 173 | 187 |
| 174 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 188 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 175 ScriptPromise promise = resolver->promise(); | 189 ScriptPromise promise = resolver->promise(); |
| 176 | 190 |
| 177 WebBluetooth* webbluetooth = | 191 WebBluetooth* webbluetooth = |
| 178 BluetoothSupplement::fromScriptState(scriptState); | 192 BluetoothSupplement::fromScriptState(scriptState); |
| 179 webbluetooth->getCharacteristics( | 193 webbluetooth->getCharacteristics( |
| 180 m_webService->serviceInstanceID, static_cast<int32_t>(quantity), | 194 m_webService->serviceInstanceID, static_cast<int32_t>(quantity), |
| 181 characteristicsUUID, | 195 characteristicsUUID, new GetCharacteristicsCallback( |
| 182 new GetCharacteristicsCallback(this, quantity, resolver)); | 196 this, characteristicsUUID, quantity, resolver)); |
| 183 | 197 |
| 184 return promise; | 198 return promise; |
| 185 } | 199 } |
| 186 | 200 |
| 187 } // namespace blink | 201 } // namespace blink |
| OLD | NEW |