| 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/BluetoothRemoteGATTServer.h" | 5 #include "modules/bluetooth/BluetoothRemoteGATTServer.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 8 #include "bindings/core/v8/ScriptPromise.h" | 8 #include "bindings/core/v8/ScriptPromise.h" |
| 9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.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 "modules/bluetooth/Bluetooth.h" | 13 #include "modules/bluetooth/Bluetooth.h" |
| 14 #include "modules/bluetooth/BluetoothDevice.h" | 14 #include "modules/bluetooth/BluetoothDevice.h" |
| 15 #include "modules/bluetooth/BluetoothError.h" | 15 #include "modules/bluetooth/BluetoothError.h" |
| 16 #include "modules/bluetooth/BluetoothRemoteGATTService.h" | 16 #include "modules/bluetooth/BluetoothRemoteGATTService.h" |
| 17 #include "modules/bluetooth/BluetoothUUID.h" | 17 #include "modules/bluetooth/BluetoothUUID.h" |
| 18 #include <utility> |
| 18 | 19 |
| 19 namespace blink { | 20 namespace blink { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 const char kGATTServerDisconnected[] = | 24 const char kGATTServerDisconnected[] = |
| 24 "GATT Server disconnected while retrieving services."; | 25 "GATT Server disconnected while retrieving services."; |
| 25 const char kGATTServerNotConnected[] = | 26 const char kGATTServerNotConnected[] = |
| 26 "GATT Server is disconnected. Cannot retrieve services."; | 27 "GATT Server is disconnected. Cannot retrieve services."; |
| 27 | 28 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 } | 71 } |
| 71 } | 72 } |
| 72 | 73 |
| 73 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { | 74 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { |
| 74 m_device->bluetooth()->addDevice(device()->id(), device()); | 75 m_device->bluetooth()->addDevice(device()->id(), device()); |
| 75 | 76 |
| 76 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 77 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 77 ScriptPromise promise = resolver->promise(); | 78 ScriptPromise promise = resolver->promise(); |
| 78 | 79 |
| 79 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 80 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
| 80 mojom::blink::WebBluetoothDeviceIdPtr deviceId = | |
| 81 BluetoothDevice::createMojoDeviceId(device()->id()); | |
| 82 service->RemoteServerConnect( | 81 service->RemoteServerConnect( |
| 83 std::move(deviceId), | 82 device()->id(), convertToBaseCallback(WTF::bind( |
| 84 convertToBaseCallback( | 83 &BluetoothRemoteGATTServer::ConnectCallback, |
| 85 WTF::bind(&BluetoothRemoteGATTServer::ConnectCallback, | 84 wrapPersistent(this), wrapPersistent(resolver)))); |
| 86 wrapPersistent(this), wrapPersistent(resolver)))); | |
| 87 | 85 |
| 88 return promise; | 86 return promise; |
| 89 } | 87 } |
| 90 | 88 |
| 91 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { | 89 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { |
| 92 if (!m_connected) | 90 if (!m_connected) |
| 93 return; | 91 return; |
| 94 device()->cleanupDisconnectedDeviceAndFireEvent(); | 92 device()->cleanupDisconnectedDeviceAndFireEvent(); |
| 95 m_device->bluetooth()->removeDevice(device()->id()); | 93 m_device->bluetooth()->removeDevice(device()->id()); |
| 96 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 94 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
| 97 mojom::blink::WebBluetoothDeviceIdPtr deviceId = | 95 service->RemoteServerDisconnect(device()->id()); |
| 98 BluetoothDevice::createMojoDeviceId(device()->id()); | |
| 99 service->RemoteServerDisconnect(std::move(deviceId)); | |
| 100 } | 96 } |
| 101 | 97 |
| 102 // Callback that allows us to resolve the promise with a single service or | 98 // Callback that allows us to resolve the promise with a single service or |
| 103 // with a vector owning the services. | 99 // with a vector owning the services. |
| 104 void BluetoothRemoteGATTServer::GetPrimaryServicesCallback( | 100 void BluetoothRemoteGATTServer::GetPrimaryServicesCallback( |
| 105 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 101 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
| 106 ScriptPromiseResolver* resolver, | 102 ScriptPromiseResolver* resolver, |
| 107 mojom::blink::WebBluetoothResult result, | 103 mojom::blink::WebBluetoothResult result, |
| 108 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTServicePtr>> services) { | 104 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTServicePtr>> services) { |
| 109 if (!resolver->getExecutionContext() || | 105 if (!resolver->getExecutionContext() || |
| 110 resolver->getExecutionContext()->isContextDestroyed()) | 106 resolver->getExecutionContext()->isContextDestroyed()) |
| 111 return; | 107 return; |
| 112 | 108 |
| 113 // If the resolver is not in the set of ActiveAlgorithms then the frame | 109 // If the resolver is not in the set of ActiveAlgorithms then the frame |
| 114 // disconnected so we reject. | 110 // disconnected so we reject. |
| 115 if (!RemoveFromActiveAlgorithms(resolver)) { | 111 if (!RemoveFromActiveAlgorithms(resolver)) { |
| 116 resolver->reject( | 112 resolver->reject( |
| 117 DOMException::create(NetworkError, kGATTServerDisconnected)); | 113 DOMException::create(NetworkError, kGATTServerDisconnected)); |
| 118 return; | 114 return; |
| 119 } | 115 } |
| 120 | 116 |
| 121 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 117 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 122 DCHECK(services); | 118 DCHECK(services); |
| 123 | 119 |
| 124 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { | 120 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { |
| 125 DCHECK_EQ(1u, services->size()); | 121 DCHECK_EQ(1u, services->size()); |
| 126 resolver->resolve(m_device->getOrCreateRemoteGATTService( | 122 resolver->resolve(m_device->getOrCreateRemoteGATTService( |
| 127 services.value()[0]->instance_id, services.value()[0]->uuid, | 123 std::move(services.value()[0]), true /* isPrimary */, |
| 128 true /* isPrimary */, device()->id())); | 124 device()->id())); |
| 129 return; | 125 return; |
| 130 } | 126 } |
| 131 | 127 |
| 132 HeapVector<Member<BluetoothRemoteGATTService>> gattServices; | 128 HeapVector<Member<BluetoothRemoteGATTService>> gattServices; |
| 133 gattServices.reserveInitialCapacity(services->size()); | 129 gattServices.reserveInitialCapacity(services->size()); |
| 134 | 130 |
| 135 for (const auto& service : services.value()) { | 131 for (auto& service : services.value()) { |
| 136 gattServices.push_back(m_device->getOrCreateRemoteGATTService( | 132 gattServices.push_back(m_device->getOrCreateRemoteGATTService( |
| 137 service->instance_id, service->uuid, true /* isPrimary */, | 133 std::move(service), true /* isPrimary */, device()->id())); |
| 138 device()->id())); | |
| 139 } | 134 } |
| 140 resolver->resolve(gattServices); | 135 resolver->resolve(gattServices); |
| 141 } else { | 136 } else { |
| 142 resolver->reject(BluetoothError::take(resolver, result)); | 137 resolver->reject(BluetoothError::take(resolver, result)); |
| 143 } | 138 } |
| 144 } | 139 } |
| 145 | 140 |
| 146 ScriptPromise BluetoothRemoteGATTServer::getPrimaryService( | 141 ScriptPromise BluetoothRemoteGATTServer::getPrimaryService( |
| 147 ScriptState* scriptState, | 142 ScriptState* scriptState, |
| 148 const StringOrUnsignedLong& service, | 143 const StringOrUnsignedLong& service, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 return ScriptPromise::rejectWithDOMException( | 180 return ScriptPromise::rejectWithDOMException( |
| 186 scriptState, | 181 scriptState, |
| 187 DOMException::create(NetworkError, kGATTServerNotConnected)); | 182 DOMException::create(NetworkError, kGATTServerNotConnected)); |
| 188 } | 183 } |
| 189 | 184 |
| 190 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 185 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 191 ScriptPromise promise = resolver->promise(); | 186 ScriptPromise promise = resolver->promise(); |
| 192 AddToActiveAlgorithms(resolver); | 187 AddToActiveAlgorithms(resolver); |
| 193 | 188 |
| 194 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 189 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
| 195 mojom::blink::WebBluetoothDeviceIdPtr deviceId = | |
| 196 BluetoothDevice::createMojoDeviceId(device()->id()); | |
| 197 WTF::Optional<String> uuid = WTF::nullopt; | 190 WTF::Optional<String> uuid = WTF::nullopt; |
| 198 if (!servicesUUID.isEmpty()) | 191 if (!servicesUUID.isEmpty()) |
| 199 uuid = servicesUUID; | 192 uuid = servicesUUID; |
| 200 | 193 |
| 201 service->RemoteServerGetPrimaryServices( | 194 service->RemoteServerGetPrimaryServices( |
| 202 std::move(deviceId), quantity, uuid, | 195 device()->id(), quantity, uuid, |
| 203 convertToBaseCallback( | 196 convertToBaseCallback( |
| 204 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, | 197 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, |
| 205 wrapPersistent(this), quantity, wrapPersistent(resolver)))); | 198 wrapPersistent(this), quantity, wrapPersistent(resolver)))); |
| 206 return promise; | 199 return promise; |
| 207 } | 200 } |
| 208 | 201 |
| 209 } // namespace blink | 202 } // namespace blink |
| OLD | NEW |