| 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 <memory> | |
| 18 #include <utility> | 17 #include <utility> |
| 19 | 18 |
| 20 namespace blink { | 19 namespace blink { |
| 21 | 20 |
| 22 namespace { | 21 namespace { |
| 23 | 22 |
| 24 const char kGATTServerDisconnected[] = | 23 const char kGATTServerDisconnected[] = |
| 25 "GATT Server disconnected while retrieving characteristics."; | 24 "GATT Server disconnected while retrieving characteristics."; |
| 26 const char kGATTServerNotConnected[] = | 25 const char kGATTServerNotConnected[] = |
| 27 "GATT Server is disconnected. Cannot retrieve characteristics."; | 26 "GATT Server is disconnected. Cannot retrieve characteristics."; |
| 28 const char kInvalidService[] = | 27 const char kInvalidService[] = |
| 29 "Service is no longer valid. Remember to retrieve the service again after " | 28 "Service is no longer valid. Remember to retrieve the service again after " |
| 30 "reconnecting."; | 29 "reconnecting."; |
| 31 | 30 |
| 32 } // namespace | 31 } // namespace |
| 33 | 32 |
| 34 BluetoothRemoteGATTService::BluetoothRemoteGATTService( | 33 BluetoothRemoteGATTService::BluetoothRemoteGATTService( |
| 35 const String& serviceInstanceId, | 34 mojom::blink::WebBluetoothRemoteGATTServicePtr service, |
| 36 const String& uuid, | |
| 37 bool isPrimary, | 35 bool isPrimary, |
| 38 const String& deviceInstanceId, | 36 const String& deviceInstanceId, |
| 39 BluetoothDevice* device) | 37 BluetoothDevice* device) |
| 40 : m_serviceInstanceId(serviceInstanceId), | 38 : m_service(std::move(service)), |
| 41 m_uuid(uuid), | |
| 42 m_isPrimary(isPrimary), | 39 m_isPrimary(isPrimary), |
| 43 m_deviceInstanceId(deviceInstanceId), | 40 m_deviceInstanceId(deviceInstanceId), |
| 44 m_device(device) {} | 41 m_device(device) {} |
| 45 | 42 |
| 46 DEFINE_TRACE(BluetoothRemoteGATTService) { | 43 DEFINE_TRACE(BluetoothRemoteGATTService) { |
| 47 visitor->trace(m_device); | 44 visitor->trace(m_device); |
| 48 } | 45 } |
| 49 | 46 |
| 50 // Callback that allows us to resolve the promise with a single Characteristic | 47 // Callback that allows us to resolve the promise with a single Characteristic |
| 51 // or with a vector owning the characteristics. | 48 // or with a vector owning the characteristics. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 67 DOMException::create(NetworkError, kGATTServerDisconnected)); | 64 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 68 return; | 65 return; |
| 69 } | 66 } |
| 70 | 67 |
| 71 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 68 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 72 DCHECK(characteristics); | 69 DCHECK(characteristics); |
| 73 | 70 |
| 74 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { | 71 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { |
| 75 DCHECK_EQ(1u, characteristics->size()); | 72 DCHECK_EQ(1u, characteristics->size()); |
| 76 resolver->resolve(device()->getOrCreateRemoteGATTCharacteristic( | 73 resolver->resolve(device()->getOrCreateRemoteGATTCharacteristic( |
| 77 resolver->getExecutionContext(), | 74 resolver->getExecutionContext(), serviceInstanceId, |
| 78 characteristics.value()[0]->instance_id, serviceInstanceId, | 75 std::move(characteristics.value()[0]), this)); |
| 79 characteristics.value()[0]->uuid, | |
| 80 characteristics.value()[0]->properties, this)); | |
| 81 return; | 76 return; |
| 82 } | 77 } |
| 83 | 78 |
| 84 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> gattCharacteristics; | 79 HeapVector<Member<BluetoothRemoteGATTCharacteristic>> gattCharacteristics; |
| 85 gattCharacteristics.reserveInitialCapacity(characteristics->size()); | 80 gattCharacteristics.reserveInitialCapacity(characteristics->size()); |
| 86 for (const auto& characteristic : characteristics.value()) { | 81 for (auto& characteristic : characteristics.value()) { |
| 87 gattCharacteristics.push_back( | 82 gattCharacteristics.push_back( |
| 88 device()->getOrCreateRemoteGATTCharacteristic( | 83 device()->getOrCreateRemoteGATTCharacteristic( |
| 89 resolver->getExecutionContext(), characteristic->instance_id, | 84 resolver->getExecutionContext(), serviceInstanceId, |
| 90 serviceInstanceId, characteristic->uuid, | 85 std::move(characteristic), this)); |
| 91 characteristic->properties, this)); | |
| 92 } | 86 } |
| 93 resolver->resolve(gattCharacteristics); | 87 resolver->resolve(gattCharacteristics); |
| 94 } else { | 88 } else { |
| 95 resolver->reject(BluetoothError::take(resolver, result)); | 89 resolver->reject(BluetoothError::take(resolver, result)); |
| 96 } | 90 } |
| 97 } | 91 } |
| 98 | 92 |
| 99 ScriptPromise BluetoothRemoteGATTService::getCharacteristic( | 93 ScriptPromise BluetoothRemoteGATTService::getCharacteristic( |
| 100 ScriptState* scriptState, | 94 ScriptState* scriptState, |
| 101 const StringOrUnsignedLong& characteristic, | 95 const StringOrUnsignedLong& characteristic, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 ScriptState* scriptState, | 129 ScriptState* scriptState, |
| 136 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 130 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
| 137 const String& characteristicsUUID) { | 131 const String& characteristicsUUID) { |
| 138 // We always check that the device is connected. | 132 // We always check that the device is connected. |
| 139 if (!device()->gatt()->connected()) { | 133 if (!device()->gatt()->connected()) { |
| 140 return ScriptPromise::rejectWithDOMException( | 134 return ScriptPromise::rejectWithDOMException( |
| 141 scriptState, | 135 scriptState, |
| 142 DOMException::create(NetworkError, kGATTServerNotConnected)); | 136 DOMException::create(NetworkError, kGATTServerNotConnected)); |
| 143 } | 137 } |
| 144 | 138 |
| 145 if (!device()->isValidService(m_serviceInstanceId)) { | 139 if (!device()->isValidService(m_service->instance_id)) { |
| 146 return ScriptPromise::rejectWithDOMException( | 140 return ScriptPromise::rejectWithDOMException( |
| 147 scriptState, DOMException::create(InvalidStateError, kInvalidService)); | 141 scriptState, DOMException::create(InvalidStateError, kInvalidService)); |
| 148 } | 142 } |
| 149 | 143 |
| 150 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 144 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 151 ScriptPromise promise = resolver->promise(); | 145 ScriptPromise promise = resolver->promise(); |
| 152 device()->gatt()->AddToActiveAlgorithms(resolver); | 146 device()->gatt()->AddToActiveAlgorithms(resolver); |
| 153 | 147 |
| 154 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 148 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
| 155 | 149 |
| 156 WTF::Optional<String> uuid = WTF::nullopt; | 150 WTF::Optional<String> uuid = WTF::nullopt; |
| 157 if (!characteristicsUUID.isEmpty()) | 151 if (!characteristicsUUID.isEmpty()) |
| 158 uuid = characteristicsUUID; | 152 uuid = characteristicsUUID; |
| 159 service->RemoteServiceGetCharacteristics( | 153 service->RemoteServiceGetCharacteristics( |
| 160 m_serviceInstanceId, quantity, uuid, | 154 m_service->instance_id, quantity, uuid, |
| 161 convertToBaseCallback( | 155 convertToBaseCallback( |
| 162 WTF::bind(&BluetoothRemoteGATTService::GetCharacteristicsCallback, | 156 WTF::bind(&BluetoothRemoteGATTService::GetCharacteristicsCallback, |
| 163 wrapPersistent(this), m_serviceInstanceId, quantity, | 157 wrapPersistent(this), m_service->instance_id, quantity, |
| 164 wrapPersistent(resolver)))); | 158 wrapPersistent(resolver)))); |
| 165 | 159 |
| 166 return promise; | 160 return promise; |
| 167 } | 161 } |
| 168 | 162 |
| 169 } // namespace blink | 163 } // namespace blink |
| OLD | NEW |