| 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/Bluetooth.h" |
| 13 #include "modules/bluetooth/BluetoothError.h" | 13 #include "modules/bluetooth/BluetoothError.h" |
| 14 #include "modules/bluetooth/BluetoothRemoteGATTCharacteristic.h" | 14 #include "modules/bluetooth/BluetoothRemoteGATTCharacteristic.h" |
| 15 #include "modules/bluetooth/BluetoothUUID.h" | 15 #include "modules/bluetooth/BluetoothUUID.h" |
| 16 #include "wtf/PtrUtil.h" | 16 #include "wtf/PtrUtil.h" |
| 17 #include <utility> | 17 #include <utility> |
| 18 | 18 |
| 19 namespace blink { | 19 namespace blink { |
| 20 | 20 |
| 21 namespace { | |
| 22 | |
| 23 const char kGATTServerDisconnected[] = | |
| 24 "GATT Server disconnected while retrieving characteristics."; | |
| 25 const char kGATTServerNotConnected[] = | |
| 26 "GATT Server is disconnected. Cannot retrieve characteristics."; | |
| 27 const char kInvalidService[] = | |
| 28 "Service is no longer valid. Remember to retrieve the service again after " | |
| 29 "reconnecting."; | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 BluetoothRemoteGATTService::BluetoothRemoteGATTService( | 21 BluetoothRemoteGATTService::BluetoothRemoteGATTService( |
| 34 mojom::blink::WebBluetoothRemoteGATTServicePtr service, | 22 mojom::blink::WebBluetoothRemoteGATTServicePtr service, |
| 35 bool isPrimary, | 23 bool isPrimary, |
| 36 const String& deviceInstanceId, | 24 const String& deviceInstanceId, |
| 37 BluetoothDevice* device) | 25 BluetoothDevice* device) |
| 38 : m_service(std::move(service)), | 26 : m_service(std::move(service)), |
| 39 m_isPrimary(isPrimary), | 27 m_isPrimary(isPrimary), |
| 40 m_deviceInstanceId(deviceInstanceId), | 28 m_deviceInstanceId(deviceInstanceId), |
| 41 m_device(device) {} | 29 m_device(device) {} |
| 42 | 30 |
| 43 DEFINE_TRACE(BluetoothRemoteGATTService) { | 31 DEFINE_TRACE(BluetoothRemoteGATTService) { |
| 44 visitor->trace(m_device); | 32 visitor->trace(m_device); |
| 45 } | 33 } |
| 46 | 34 |
| 47 // Callback that allows us to resolve the promise with a single characteristic | 35 // Callback that allows us to resolve the promise with a single characteristic |
| 48 // or with a vector owning the characteristics. | 36 // or with a vector owning the characteristics. |
| 49 void BluetoothRemoteGATTService::GetCharacteristicsCallback( | 37 void BluetoothRemoteGATTService::GetCharacteristicsCallback( |
| 50 const String& serviceInstanceId, | 38 const String& serviceInstanceId, |
| 39 const String& requestedCharacteristicUUID, |
| 51 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 40 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
| 52 ScriptPromiseResolver* resolver, | 41 ScriptPromiseResolver* resolver, |
| 53 mojom::blink::WebBluetoothResult result, | 42 mojom::blink::WebBluetoothResult result, |
| 54 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr>> | 43 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTCharacteristicPtr>> |
| 55 characteristics) { | 44 characteristics) { |
| 56 if (!resolver->getExecutionContext() || | 45 if (!resolver->getExecutionContext() || |
| 57 resolver->getExecutionContext()->isContextDestroyed()) | 46 resolver->getExecutionContext()->isContextDestroyed()) |
| 58 return; | 47 return; |
| 59 | 48 |
| 60 // If the device is disconnected, reject. | 49 // If the device is disconnected, reject. |
| 61 if (!device()->gatt()->RemoveFromActiveAlgorithms(resolver)) { | 50 if (!device()->gatt()->RemoveFromActiveAlgorithms(resolver)) { |
| 62 resolver->reject( | 51 resolver->reject(BluetoothError::createDOMException( |
| 63 DOMException::create(NetworkError, kGATTServerDisconnected)); | 52 mojom::blink::WebBluetoothResult:: |
| 53 GATT_SERVER_DISCONNECTED_WHILE_RETRIEVING_CHARACTERISTICS)); |
| 64 return; | 54 return; |
| 65 } | 55 } |
| 66 | 56 |
| 67 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 57 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 68 DCHECK(characteristics); | 58 DCHECK(characteristics); |
| 69 | 59 |
| 70 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { | 60 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { |
| 71 DCHECK_EQ(1u, characteristics->size()); | 61 DCHECK_EQ(1u, characteristics->size()); |
| 72 resolver->resolve(device()->getOrCreateRemoteGATTCharacteristic( | 62 resolver->resolve(device()->getOrCreateRemoteGATTCharacteristic( |
| 73 resolver->getExecutionContext(), | 63 resolver->getExecutionContext(), |
| 74 std::move(characteristics.value()[0]), this)); | 64 std::move(characteristics.value()[0]), this)); |
| 75 return; | 65 return; |
| 76 } | 66 } |
| 77 | 67 |
| 78 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> gattCharacteristics; | 68 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> gattCharacteristics; |
| 79 gattCharacteristics.reserveInitialCapacity(characteristics->size()); | 69 gattCharacteristics.reserveInitialCapacity(characteristics->size()); |
| 80 for (auto& characteristic : characteristics.value()) { | 70 for (auto& characteristic : characteristics.value()) { |
| 81 gattCharacteristics.push_back( | 71 gattCharacteristics.push_back( |
| 82 device()->getOrCreateRemoteGATTCharacteristic( | 72 device()->getOrCreateRemoteGATTCharacteristic( |
| 83 resolver->getExecutionContext(), std::move(characteristic), | 73 resolver->getExecutionContext(), std::move(characteristic), |
| 84 this)); | 74 this)); |
| 85 } | 75 } |
| 86 resolver->resolve(gattCharacteristics); | 76 resolver->resolve(gattCharacteristics); |
| 87 } else { | 77 } else { |
| 88 resolver->reject(BluetoothError::take(resolver, result)); | 78 if (result == mojom::blink::WebBluetoothResult::CHARACTERISTIC_NOT_FOUND) { |
| 79 resolver->reject(BluetoothError::createDOMException( |
| 80 result, "No Characteristics matching UUID " + |
| 81 requestedCharacteristicUUID + |
| 82 " found in Service with UUID " + uuid() + ".")); |
| 83 } else { |
| 84 resolver->reject(BluetoothError::createDOMException(result)); |
| 85 } |
| 89 } | 86 } |
| 90 } | 87 } |
| 91 | 88 |
| 92 ScriptPromise BluetoothRemoteGATTService::getCharacteristic( | 89 ScriptPromise BluetoothRemoteGATTService::getCharacteristic( |
| 93 ScriptState* scriptState, | 90 ScriptState* scriptState, |
| 94 const StringOrUnsignedLong& characteristic, | 91 const StringOrUnsignedLong& characteristic, |
| 95 ExceptionState& exceptionState) { | 92 ExceptionState& exceptionState) { |
| 96 String characteristicUUID = | 93 String characteristicUUID = |
| 97 BluetoothUUID::getCharacteristic(characteristic, exceptionState); | 94 BluetoothUUID::getCharacteristic(characteristic, exceptionState); |
| 98 if (exceptionState.hadException()) | 95 if (exceptionState.hadException()) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 124 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE); | 121 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE); |
| 125 } | 122 } |
| 126 | 123 |
| 127 ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl( | 124 ScriptPromise BluetoothRemoteGATTService::getCharacteristicsImpl( |
| 128 ScriptState* scriptState, | 125 ScriptState* scriptState, |
| 129 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 126 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
| 130 const String& characteristicsUUID) { | 127 const String& characteristicsUUID) { |
| 131 if (!device()->gatt()->connected()) { | 128 if (!device()->gatt()->connected()) { |
| 132 return ScriptPromise::rejectWithDOMException( | 129 return ScriptPromise::rejectWithDOMException( |
| 133 scriptState, | 130 scriptState, |
| 134 DOMException::create(NetworkError, kGATTServerNotConnected)); | 131 BluetoothError::createDOMException( |
| 132 mojom::blink::WebBluetoothResult:: |
| 133 GATT_SERVER_NOT_CONNECTED_CANNOT_RETRIEVE_CHARACTERISTICS)); |
| 135 } | 134 } |
| 136 | 135 |
| 137 if (!device()->isValidService(m_service->instance_id)) { | 136 if (!device()->isValidService(m_service->instance_id)) { |
| 138 return ScriptPromise::rejectWithDOMException( | 137 return ScriptPromise::rejectWithDOMException( |
| 139 scriptState, DOMException::create(InvalidStateError, kInvalidService)); | 138 scriptState, BluetoothError::createDOMException( |
| 139 mojom::blink::WebBluetoothResult::INVALID_SERVICE, |
| 140 "Service with UUID " + m_service->uuid + |
| 141 " is no longer valid. Remember to retrieve " |
| 142 "the service again after reconnecting.")); |
| 140 } | 143 } |
| 141 | 144 |
| 142 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 145 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 143 ScriptPromise promise = resolver->promise(); | 146 ScriptPromise promise = resolver->promise(); |
| 144 device()->gatt()->AddToActiveAlgorithms(resolver); | 147 device()->gatt()->AddToActiveAlgorithms(resolver); |
| 145 | 148 |
| 146 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 149 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
| 147 service->RemoteServiceGetCharacteristics( | 150 service->RemoteServiceGetCharacteristics( |
| 148 m_service->instance_id, quantity, characteristicsUUID, | 151 m_service->instance_id, quantity, characteristicsUUID, |
| 149 convertToBaseCallback( | 152 convertToBaseCallback( |
| 150 WTF::bind(&BluetoothRemoteGATTService::GetCharacteristicsCallback, | 153 WTF::bind(&BluetoothRemoteGATTService::GetCharacteristicsCallback, |
| 151 wrapPersistent(this), m_service->instance_id, quantity, | 154 wrapPersistent(this), m_service->instance_id, |
| 152 wrapPersistent(resolver)))); | 155 characteristicsUUID, quantity, wrapPersistent(resolver)))); |
| 153 | 156 |
| 154 return promise; | 157 return promise; |
| 155 } | 158 } |
| 156 | 159 |
| 157 } // namespace blink | 160 } // namespace blink |
| OLD | NEW |