| 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" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 } | 56 } |
| 57 | 57 |
| 58 void BluetoothRemoteGATTServer::ConnectCallback( | 58 void BluetoothRemoteGATTServer::ConnectCallback( |
| 59 ScriptPromiseResolver* resolver, | 59 ScriptPromiseResolver* resolver, |
| 60 mojom::blink::WebBluetoothResult result) { | 60 mojom::blink::WebBluetoothResult result) { |
| 61 if (!resolver->getExecutionContext() || | 61 if (!resolver->getExecutionContext() || |
| 62 resolver->getExecutionContext()->isContextDestroyed()) | 62 resolver->getExecutionContext()->isContextDestroyed()) |
| 63 return; | 63 return; |
| 64 | 64 |
| 65 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 65 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 66 m_device->bluetooth()->addToConnectedDevicesMap(device()->id(), device()); | 66 m_device->bluetooth()->addToConnectedDevicesMap(m_device->id(), m_device); |
| 67 setConnected(true); | 67 setConnected(true); |
| 68 resolver->resolve(this); | 68 resolver->resolve(this); |
| 69 } else { | 69 } else { |
| 70 resolver->reject(BluetoothError::createDOMException(result)); | 70 resolver->reject(BluetoothError::createDOMException(result)); |
| 71 } | 71 } |
| 72 } | 72 } |
| 73 | 73 |
| 74 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { | 74 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { |
| 75 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 75 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 76 ScriptPromise promise = resolver->promise(); | 76 ScriptPromise promise = resolver->promise(); |
| 77 | 77 |
| 78 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 78 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
| 79 service->RemoteServerConnect( | 79 service->RemoteServerConnect( |
| 80 device()->id(), convertToBaseCallback(WTF::bind( | 80 m_device->id(), convertToBaseCallback(WTF::bind( |
| 81 &BluetoothRemoteGATTServer::ConnectCallback, | 81 &BluetoothRemoteGATTServer::ConnectCallback, |
| 82 wrapPersistent(this), wrapPersistent(resolver)))); | 82 wrapPersistent(this), wrapPersistent(resolver)))); |
| 83 | 83 |
| 84 return promise; | 84 return promise; |
| 85 } | 85 } |
| 86 | 86 |
| 87 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { | 87 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { |
| 88 if (!m_connected) | 88 if (!m_connected) |
| 89 return; | 89 return; |
| 90 device()->cleanupDisconnectedDeviceAndFireEvent(); | 90 m_device->cleanupDisconnectedDeviceAndFireEvent(); |
| 91 m_device->bluetooth()->removeFromConnectedDevicesMap(device()->id()); | 91 m_device->bluetooth()->removeFromConnectedDevicesMap(m_device->id()); |
| 92 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 92 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
| 93 service->RemoteServerDisconnect(device()->id()); | 93 service->RemoteServerDisconnect(m_device->id()); |
| 94 } | 94 } |
| 95 | 95 |
| 96 // Callback that allows us to resolve the promise with a single service or | 96 // Callback that allows us to resolve the promise with a single service or |
| 97 // with a vector owning the services. | 97 // with a vector owning the services. |
| 98 void BluetoothRemoteGATTServer::GetPrimaryServicesCallback( | 98 void BluetoothRemoteGATTServer::GetPrimaryServicesCallback( |
| 99 const String& requestedServiceUUID, | 99 const String& requestedServiceUUID, |
| 100 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 100 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
| 101 ScriptPromiseResolver* resolver, | 101 ScriptPromiseResolver* resolver, |
| 102 mojom::blink::WebBluetoothResult result, | 102 mojom::blink::WebBluetoothResult result, |
| 103 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTServicePtr>> services) { | 103 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTServicePtr>> services) { |
| 104 if (!resolver->getExecutionContext() || | 104 if (!resolver->getExecutionContext() || |
| 105 resolver->getExecutionContext()->isContextDestroyed()) | 105 resolver->getExecutionContext()->isContextDestroyed()) |
| 106 return; | 106 return; |
| 107 | 107 |
| 108 // If the device is disconnected, reject. | 108 // If the device is disconnected, reject. |
| 109 if (!RemoveFromActiveAlgorithms(resolver)) { | 109 if (!RemoveFromActiveAlgorithms(resolver)) { |
| 110 resolver->reject( | 110 resolver->reject( |
| 111 DOMException::create(NetworkError, kGATTServerNotConnected)); | 111 DOMException::create(NetworkError, kGATTServerNotConnected)); |
| 112 return; | 112 return; |
| 113 } | 113 } |
| 114 | 114 |
| 115 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 115 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 116 DCHECK(services); | 116 DCHECK(services); |
| 117 | 117 |
| 118 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { | 118 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { |
| 119 DCHECK_EQ(1u, services->size()); | 119 DCHECK_EQ(1u, services->size()); |
| 120 resolver->resolve(m_device->getOrCreateRemoteGATTService( | 120 resolver->resolve(m_device->getOrCreateRemoteGATTService( |
| 121 std::move(services.value()[0]), true /* isPrimary */, | 121 std::move(services.value()[0]), true /* isPrimary */, |
| 122 device()->id())); | 122 m_device->id())); |
| 123 return; | 123 return; |
| 124 } | 124 } |
| 125 | 125 |
| 126 HeapVector<Member<BluetoothRemoteGATTService>> gattServices; | 126 HeapVector<Member<BluetoothRemoteGATTService>> gattServices; |
| 127 gattServices.reserveInitialCapacity(services->size()); | 127 gattServices.reserveInitialCapacity(services->size()); |
| 128 | 128 |
| 129 for (auto& service : services.value()) { | 129 for (auto& service : services.value()) { |
| 130 gattServices.push_back(m_device->getOrCreateRemoteGATTService( | 130 gattServices.push_back(m_device->getOrCreateRemoteGATTService( |
| 131 std::move(service), true /* isPrimary */, device()->id())); | 131 std::move(service), true /* isPrimary */, m_device->id())); |
| 132 } | 132 } |
| 133 resolver->resolve(gattServices); | 133 resolver->resolve(gattServices); |
| 134 } else { | 134 } else { |
| 135 if (result == mojom::blink::WebBluetoothResult::SERVICE_NOT_FOUND) { | 135 if (result == mojom::blink::WebBluetoothResult::SERVICE_NOT_FOUND) { |
| 136 resolver->reject(BluetoothError::createDOMException( | 136 resolver->reject(BluetoothError::createDOMException( |
| 137 BluetoothErrorCode::ServiceNotFound, "No Services matching UUID " + | 137 BluetoothErrorCode::ServiceNotFound, "No Services matching UUID " + |
| 138 requestedServiceUUID + | 138 requestedServiceUUID + |
| 139 " found in Device.")); | 139 " found in Device.")); |
| 140 } else { | 140 } else { |
| 141 resolver->reject(BluetoothError::createDOMException(result)); | 141 resolver->reject(BluetoothError::createDOMException(result)); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 ScriptState* scriptState, | 173 ScriptState* scriptState, |
| 174 ExceptionState&) { | 174 ExceptionState&) { |
| 175 return getPrimaryServicesImpl( | 175 return getPrimaryServicesImpl( |
| 176 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE); | 176 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE); |
| 177 } | 177 } |
| 178 | 178 |
| 179 ScriptPromise BluetoothRemoteGATTServer::getPrimaryServicesImpl( | 179 ScriptPromise BluetoothRemoteGATTServer::getPrimaryServicesImpl( |
| 180 ScriptState* scriptState, | 180 ScriptState* scriptState, |
| 181 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 181 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
| 182 String servicesUUID) { | 182 String servicesUUID) { |
| 183 if (!connected()) { | 183 if (!m_connected) { |
| 184 return ScriptPromise::rejectWithDOMException( | 184 return ScriptPromise::rejectWithDOMException( |
| 185 scriptState, | 185 scriptState, |
| 186 DOMException::create(NetworkError, kGATTServerNotConnected)); | 186 DOMException::create(NetworkError, kGATTServerNotConnected)); |
| 187 } | 187 } |
| 188 | 188 |
| 189 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 189 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 190 ScriptPromise promise = resolver->promise(); | 190 ScriptPromise promise = resolver->promise(); |
| 191 AddToActiveAlgorithms(resolver); | 191 AddToActiveAlgorithms(resolver); |
| 192 | 192 |
| 193 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 193 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
| 194 service->RemoteServerGetPrimaryServices( | 194 service->RemoteServerGetPrimaryServices( |
| 195 device()->id(), quantity, servicesUUID, | 195 m_device->id(), quantity, servicesUUID, |
| 196 convertToBaseCallback( | 196 convertToBaseCallback( |
| 197 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, | 197 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, |
| 198 wrapPersistent(this), servicesUUID, quantity, | 198 wrapPersistent(this), servicesUUID, quantity, |
| 199 wrapPersistent(resolver)))); | 199 wrapPersistent(resolver)))); |
| 200 return promise; | 200 return promise; |
| 201 } | 201 } |
| 202 | 202 |
| 203 } // namespace blink | 203 } // namespace blink |
| OLD | NEW |