| 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" |
| 11 #include "core/inspector/ConsoleMessage.h" | 11 #include "core/inspector/ConsoleMessage.h" |
| 12 #include "modules/bluetooth/Bluetooth.h" |
| 12 #include "modules/bluetooth/BluetoothError.h" | 13 #include "modules/bluetooth/BluetoothError.h" |
| 13 #include "modules/bluetooth/BluetoothRemoteGATTCharacteristic.h" | 14 #include "modules/bluetooth/BluetoothRemoteGATTCharacteristic.h" |
| 14 #include "modules/bluetooth/BluetoothSupplement.h" | |
| 15 #include "modules/bluetooth/BluetoothUUID.h" | 15 #include "modules/bluetooth/BluetoothUUID.h" |
| 16 #include "public/platform/modules/bluetooth/WebBluetooth.h" | |
| 17 #include "wtf/PtrUtil.h" | 16 #include "wtf/PtrUtil.h" |
| 18 #include <memory> | 17 #include <memory> |
| 19 #include <utility> | 18 #include <utility> |
| 20 | 19 |
| 21 namespace blink { | 20 namespace blink { |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| 25 const char kGATTServerDisconnected[] = | 24 const char kGATTServerDisconnected[] = |
| 26 "GATT Server disconnected while retrieving characteristics."; | 25 "GATT Server disconnected while retrieving characteristics."; |
| 27 const char kGATTServerNotConnected[] = | 26 const char kGATTServerNotConnected[] = |
| 28 "GATT Server is disconnected. Cannot retrieve characteristics."; | 27 "GATT Server is disconnected. Cannot retrieve characteristics."; |
| 29 const char kInvalidService[] = | 28 const char kInvalidService[] = |
| 30 "Service is no longer valid. Remember to retrieve the service again after " | 29 "Service is no longer valid. Remember to retrieve the service again after " |
| 31 "reconnecting."; | 30 "reconnecting."; |
| 32 | 31 |
| 33 } // namespace | 32 } // namespace |
| 34 | 33 |
| 35 BluetoothRemoteGATTService::BluetoothRemoteGATTService( | 34 BluetoothRemoteGATTService::BluetoothRemoteGATTService( |
| 36 std::unique_ptr<WebBluetoothRemoteGATTService> webService, | 35 const String& serviceInstanceId, |
| 36 const String& uuid, |
| 37 bool isPrimary, |
| 38 const String& deviceInstanceId, |
| 37 BluetoothDevice* device) | 39 BluetoothDevice* device) |
| 38 : m_webService(std::move(webService)), m_device(device) { | 40 : m_serviceInstanceId(serviceInstanceId), |
| 39 DCHECK(m_webService); | 41 m_uuid(uuid), |
| 40 } | 42 m_isPrimary(isPrimary), |
| 43 m_deviceInstanceId(deviceInstanceId), |
| 44 m_device(device) {} |
| 41 | 45 |
| 42 DEFINE_TRACE(BluetoothRemoteGATTService) { | 46 DEFINE_TRACE(BluetoothRemoteGATTService) { |
| 43 visitor->trace(m_device); | 47 visitor->trace(m_device); |
| 44 } | 48 } |
| 45 | 49 |
| 46 // Class that allows us to resolve the promise with a single Characteristic or | 50 // Callback that allows us to resolve the promise with a single Characteristic |
| 47 // with a vector owning the characteristics. | 51 // or with a vector owning the characteristics. |
| 48 class GetCharacteristicsCallback | 52 void BluetoothRemoteGATTService::GetCharacteristicsCallback( |
| 49 : public WebBluetoothGetCharacteristicsCallbacks { | 53 const String& serviceInstanceId, |
| 50 public: | 54 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
| 51 GetCharacteristicsCallback( | 55 ScriptPromiseResolver* resolver, |
| 52 BluetoothRemoteGATTService* service, | 56 mojom::blink::WebBluetoothResult result, |
| 53 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 57 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr>> |
| 54 ScriptPromiseResolver* resolver) | 58 characteristics) { |
| 55 : m_service(service), m_quantity(quantity), m_resolver(resolver) { | 59 if (!resolver->getExecutionContext() || |
| 56 // We always check that the device is connected before constructing this | 60 resolver->getExecutionContext()->isContextDestroyed()) |
| 57 // object. | 61 return; |
| 58 CHECK(m_service->device()->gatt()->connected()); | 62 |
| 59 m_service->device()->gatt()->AddToActiveAlgorithms(m_resolver.get()); | 63 // If the resolver is not in the set of ActiveAlgorithms then the frame |
| 64 // disconnected so we reject. |
| 65 if (!device()->gatt()->RemoveFromActiveAlgorithms(resolver)) { |
| 66 resolver->reject( |
| 67 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 68 return; |
| 60 } | 69 } |
| 61 | 70 |
| 62 void onSuccess(const WebVector<WebBluetoothRemoteGATTCharacteristicInit*>& | 71 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 63 webCharacteristics) override { | 72 DCHECK(characteristics); |
| 64 if (!m_resolver->getExecutionContext() || | |
| 65 m_resolver->getExecutionContext()->isContextDestroyed()) | |
| 66 return; | |
| 67 | 73 |
| 68 // If the resolver is not in the set of ActiveAlgorithms then the frame | 74 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { |
| 69 // disconnected so we reject. | 75 DCHECK_EQ(1u, characteristics->size()); |
| 70 if (!m_service->device()->gatt()->RemoveFromActiveAlgorithms( | 76 resolver->resolve(device()->getOrCreateBluetoothRemoteGATTCharacteristic( |
| 71 m_resolver.get())) { | 77 resolver->getExecutionContext(), |
| 72 m_resolver->reject( | 78 characteristics.value()[0]->instance_id, serviceInstanceId, |
| 73 DOMException::create(NetworkError, kGATTServerDisconnected)); | 79 characteristics.value()[0]->uuid, |
| 80 characteristics.value()[0]->properties, this)); |
| 74 return; | 81 return; |
| 75 } | 82 } |
| 76 | 83 |
| 77 if (m_quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { | 84 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> gattCharacteristics; |
| 78 DCHECK_EQ(1u, webCharacteristics.size()); | 85 gattCharacteristics.reserveInitialCapacity(characteristics->size()); |
| 79 m_resolver->resolve( | 86 for (const auto& characteristic : characteristics.value()) { |
| 80 m_service->device()->getOrCreateBluetoothRemoteGATTCharacteristic( | 87 gattCharacteristics.append( |
| 81 m_resolver->getExecutionContext(), | 88 device()->getOrCreateBluetoothRemoteGATTCharacteristic( |
| 82 WTF::wrapUnique(webCharacteristics[0]), m_service)); | 89 resolver->getExecutionContext(), characteristic->instance_id, |
| 83 return; | 90 serviceInstanceId, characteristic->uuid, |
| 91 characteristic->properties, this)); |
| 84 } | 92 } |
| 85 | 93 resolver->resolve(gattCharacteristics); |
| 86 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> characteristics; | 94 } else { |
| 87 characteristics.reserveInitialCapacity(webCharacteristics.size()); | 95 resolver->reject(BluetoothError::take(resolver, result)); |
| 88 for (WebBluetoothRemoteGATTCharacteristicInit* webCharacteristic : | |
| 89 webCharacteristics) { | |
| 90 characteristics.append( | |
| 91 m_service->device()->getOrCreateBluetoothRemoteGATTCharacteristic( | |
| 92 m_resolver->getExecutionContext(), | |
| 93 WTF::wrapUnique(webCharacteristic), m_service)); | |
| 94 } | |
| 95 m_resolver->resolve(characteristics); | |
| 96 } | 96 } |
| 97 | 97 } |
| 98 void onError( | |
| 99 int32_t | |
| 100 error /* Corresponds to WebBluetoothResult in web_bluetooth.mojom */) | |
| 101 override { | |
| 102 if (!m_resolver->getExecutionContext() || | |
| 103 m_resolver->getExecutionContext()->isContextDestroyed()) | |
| 104 return; | |
| 105 | |
| 106 // If the resolver is not in the set of ActiveAlgorithms then the frame | |
| 107 // disconnected so we reject. | |
| 108 if (!m_service->device()->gatt()->RemoveFromActiveAlgorithms( | |
| 109 m_resolver.get())) { | |
| 110 m_resolver->reject( | |
| 111 DOMException::create(NetworkError, kGATTServerDisconnected)); | |
| 112 return; | |
| 113 } | |
| 114 | |
| 115 m_resolver->reject(BluetoothError::take(m_resolver, error)); | |
| 116 } | |
| 117 | |
| 118 private: | |
| 119 Persistent<BluetoothRemoteGATTService> m_service; | |
| 120 mojom::blink::WebBluetoothGATTQueryQuantity m_quantity; | |
| 121 const Persistent<ScriptPromiseResolver> m_resolver; | |
| 122 }; | |
| 123 | 98 |
| 124 ScriptPromise BluetoothRemoteGATTService::getCharacteristic( | 99 ScriptPromise BluetoothRemoteGATTService::getCharacteristic( |
| 125 ScriptState* scriptState, | 100 ScriptState* scriptState, |
| 126 const StringOrUnsignedLong& characteristic, | 101 const StringOrUnsignedLong& characteristic, |
| 127 ExceptionState& exceptionState) { | 102 ExceptionState& exceptionState) { |
| 128 String characteristicUUID = | 103 String characteristicUUID = |
| 129 BluetoothUUID::getCharacteristic(characteristic, exceptionState); | 104 BluetoothUUID::getCharacteristic(characteristic, exceptionState); |
| 130 if (exceptionState.hadException()) | 105 if (exceptionState.hadException()) |
| 131 return exceptionState.reject(scriptState); | 106 return exceptionState.reject(scriptState); |
| 132 | 107 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 152 ScriptPromise BluetoothRemoteGATTService::getCharacteristics( | 127 ScriptPromise BluetoothRemoteGATTService::getCharacteristics( |
| 153 ScriptState* scriptState, | 128 ScriptState* scriptState, |
| 154 ExceptionState&) { | 129 ExceptionState&) { |
| 155 return getCharacteristicsImpl( | 130 return getCharacteristicsImpl( |
| 156 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE); | 131 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE); |
| 157 } | 132 } |
| 158 | 133 |
| 159 ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl( | 134 ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl( |
| 160 ScriptState* scriptState, | 135 ScriptState* scriptState, |
| 161 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 136 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
| 162 String characteristicsUUID) { | 137 const String& characteristicsUUID) { |
| 138 // We always check that the device is connected. |
| 163 if (!device()->gatt()->connected()) { | 139 if (!device()->gatt()->connected()) { |
| 164 return ScriptPromise::rejectWithDOMException( | 140 return ScriptPromise::rejectWithDOMException( |
| 165 scriptState, | 141 scriptState, |
| 166 DOMException::create(NetworkError, kGATTServerNotConnected)); | 142 DOMException::create(NetworkError, kGATTServerNotConnected)); |
| 167 } | 143 } |
| 168 | 144 |
| 169 if (!device()->isValidService(m_webService->serviceInstanceID)) { | 145 if (!device()->isValidService(m_serviceInstanceId)) { |
| 170 return ScriptPromise::rejectWithDOMException( | 146 return ScriptPromise::rejectWithDOMException( |
| 171 scriptState, DOMException::create(InvalidStateError, kInvalidService)); | 147 scriptState, DOMException::create(InvalidStateError, kInvalidService)); |
| 172 } | 148 } |
| 173 | 149 |
| 174 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 150 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 175 ScriptPromise promise = resolver->promise(); | 151 ScriptPromise promise = resolver->promise(); |
| 152 device()->gatt()->AddToActiveAlgorithms(resolver); |
| 176 | 153 |
| 177 WebBluetooth* webbluetooth = | 154 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
| 178 BluetoothSupplement::fromScriptState(scriptState); | 155 |
| 179 webbluetooth->getCharacteristics( | 156 WTF::Optional<String> uuid = WTF::nullopt; |
| 180 m_webService->serviceInstanceID, static_cast<int32_t>(quantity), | 157 if (!characteristicsUUID.isEmpty()) |
| 181 characteristicsUUID, | 158 uuid = characteristicsUUID; |
| 182 new GetCharacteristicsCallback(this, quantity, resolver)); | 159 service->RemoteServiceGetCharacteristics( |
| 160 m_serviceInstanceId, quantity, uuid, |
| 161 convertToBaseCallback( |
| 162 WTF::bind(&BluetoothRemoteGATTService::GetCharacteristicsCallback, |
| 163 wrapPersistent(this), m_serviceInstanceId, quantity, |
| 164 wrapPersistent(resolver)))); |
| 183 | 165 |
| 184 return promise; | 166 return promise; |
| 185 } | 167 } |
| 186 | 168 |
| 187 } // namespace blink | 169 } // namespace blink |
| OLD | NEW |