| 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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(device()->id(), device()); |
| 67 setConnected(true); | 67 setConnected(true); |
| 68 resolver->resolve(this); | 68 resolver->resolve(this); |
| 69 } else { | 69 } else { |
| 70 resolver->reject(BluetoothError::take(resolver, 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 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 device()->cleanupDisconnectedDeviceAndFireEvent(); |
| 91 m_device->bluetooth()->removeFromConnectedDevicesMap(device()->id()); | 91 m_device->bluetooth()->removeFromConnectedDevicesMap(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(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 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 100 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
| 100 ScriptPromiseResolver* resolver, | 101 ScriptPromiseResolver* resolver, |
| 101 mojom::blink::WebBluetoothResult result, | 102 mojom::blink::WebBluetoothResult result, |
| 102 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTServicePtr>> services) { | 103 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTServicePtr>> services) { |
| 103 if (!resolver->getExecutionContext() || | 104 if (!resolver->getExecutionContext() || |
| 104 resolver->getExecutionContext()->isContextDestroyed()) | 105 resolver->getExecutionContext()->isContextDestroyed()) |
| 105 return; | 106 return; |
| 106 | 107 |
| 107 // If the device is disconnected, reject. | 108 // If the device is disconnected, reject. |
| 108 if (!RemoveFromActiveAlgorithms(resolver)) { | 109 if (!RemoveFromActiveAlgorithms(resolver)) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 124 | 125 |
| 125 HeapVector<Member<BluetoothRemoteGATTService>> gattServices; | 126 HeapVector<Member<BluetoothRemoteGATTService>> gattServices; |
| 126 gattServices.reserveInitialCapacity(services->size()); | 127 gattServices.reserveInitialCapacity(services->size()); |
| 127 | 128 |
| 128 for (auto& service : services.value()) { | 129 for (auto& service : services.value()) { |
| 129 gattServices.push_back(m_device->getOrCreateRemoteGATTService( | 130 gattServices.push_back(m_device->getOrCreateRemoteGATTService( |
| 130 std::move(service), true /* isPrimary */, device()->id())); | 131 std::move(service), true /* isPrimary */, device()->id())); |
| 131 } | 132 } |
| 132 resolver->resolve(gattServices); | 133 resolver->resolve(gattServices); |
| 133 } else { | 134 } else { |
| 134 resolver->reject(BluetoothError::take(resolver, result)); | 135 if (result == mojom::blink::WebBluetoothResult::SERVICE_NOT_FOUND) { |
| 136 resolver->reject(BluetoothError::createDOMException( |
| 137 BluetoothErrorCode::ServiceNotFound, |
| 138 "No Services matching UUID " + requestedServiceUUID + |
| 139 " found in Device.")); |
| 140 } else { |
| 141 resolver->reject(BluetoothError::createDOMException(result)); |
| 142 } |
| 135 } | 143 } |
| 136 } | 144 } |
| 137 | 145 |
| 138 ScriptPromise BluetoothRemoteGATTServer::getPrimaryService( | 146 ScriptPromise BluetoothRemoteGATTServer::getPrimaryService( |
| 139 ScriptState* scriptState, | 147 ScriptState* scriptState, |
| 140 const StringOrUnsignedLong& service, | 148 const StringOrUnsignedLong& service, |
| 141 ExceptionState& exceptionState) { | 149 ExceptionState& exceptionState) { |
| 142 String serviceUUID = BluetoothUUID::getService(service, exceptionState); | 150 String serviceUUID = BluetoothUUID::getService(service, exceptionState); |
| 143 if (exceptionState.hadException()) | 151 if (exceptionState.hadException()) |
| 144 return exceptionState.reject(scriptState); | 152 return exceptionState.reject(scriptState); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 | 188 |
| 181 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 189 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 182 ScriptPromise promise = resolver->promise(); | 190 ScriptPromise promise = resolver->promise(); |
| 183 AddToActiveAlgorithms(resolver); | 191 AddToActiveAlgorithms(resolver); |
| 184 | 192 |
| 185 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 193 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
| 186 service->RemoteServerGetPrimaryServices( | 194 service->RemoteServerGetPrimaryServices( |
| 187 device()->id(), quantity, servicesUUID, | 195 device()->id(), quantity, servicesUUID, |
| 188 convertToBaseCallback( | 196 convertToBaseCallback( |
| 189 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, | 197 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, |
| 190 wrapPersistent(this), quantity, wrapPersistent(resolver)))); | 198 wrapPersistent(this), servicesUUID, quantity, |
| 199 wrapPersistent(resolver)))); |
| 191 return promise; | 200 return promise; |
| 192 } | 201 } |
| 193 | 202 |
| 194 } // namespace blink | 203 } // namespace blink |
| OLD | NEW |