| 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 <utility> |
| 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 8 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
| 9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 10 #include "core/dom/DOMException.h" | 11 #include "core/dom/DOMException.h" |
| 11 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
| 12 #include "core/events/Event.h" | 13 #include "core/events/Event.h" |
| 13 #include "modules/bluetooth/Bluetooth.h" | 14 #include "modules/bluetooth/Bluetooth.h" |
| 14 #include "modules/bluetooth/BluetoothDevice.h" | 15 #include "modules/bluetooth/BluetoothDevice.h" |
| 15 #include "modules/bluetooth/BluetoothError.h" | 16 #include "modules/bluetooth/BluetoothError.h" |
| 17 #include "modules/bluetooth/BluetoothMetrics.h" |
| 16 #include "modules/bluetooth/BluetoothRemoteGATTService.h" | 18 #include "modules/bluetooth/BluetoothRemoteGATTService.h" |
| 17 #include "modules/bluetooth/BluetoothUUID.h" | 19 #include "modules/bluetooth/BluetoothUUID.h" |
| 18 #include <utility> | |
| 19 | 20 |
| 20 namespace blink { | 21 namespace blink { |
| 21 | 22 |
| 22 BluetoothRemoteGATTServer::BluetoothRemoteGATTServer(BluetoothDevice* device) | 23 BluetoothRemoteGATTServer::BluetoothRemoteGATTServer(BluetoothDevice* device) |
| 23 : m_device(device), m_connected(false) {} | 24 : m_device(device), m_connected(false) {} |
| 24 | 25 |
| 25 BluetoothRemoteGATTServer* BluetoothRemoteGATTServer::Create( | 26 BluetoothRemoteGATTServer* BluetoothRemoteGATTServer::Create( |
| 26 BluetoothDevice* device) { | 27 BluetoothDevice* device) { |
| 27 return new BluetoothRemoteGATTServer(device); | 28 return new BluetoothRemoteGATTServer(device); |
| 28 } | 29 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 57 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 58 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 58 m_device->bluetooth()->AddToConnectedDevicesMap(m_device->id(), m_device); | 59 m_device->bluetooth()->AddToConnectedDevicesMap(m_device->id(), m_device); |
| 59 SetConnected(true); | 60 SetConnected(true); |
| 60 resolver->resolve(this); | 61 resolver->resolve(this); |
| 61 } else { | 62 } else { |
| 62 resolver->reject(BluetoothError::CreateDOMException(result)); | 63 resolver->reject(BluetoothError::CreateDOMException(result)); |
| 63 } | 64 } |
| 64 } | 65 } |
| 65 | 66 |
| 66 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { | 67 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { |
| 68 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::CONNECT_GATT); |
| 69 |
| 67 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 70 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 68 ScriptPromise promise = resolver->promise(); | 71 ScriptPromise promise = resolver->promise(); |
| 69 | 72 |
| 70 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); | 73 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); |
| 71 service->RemoteServerConnect( | 74 service->RemoteServerConnect( |
| 72 m_device->id(), convertToBaseCallback(WTF::bind( | 75 m_device->id(), convertToBaseCallback(WTF::bind( |
| 73 &BluetoothRemoteGATTServer::ConnectCallback, | 76 &BluetoothRemoteGATTServer::ConnectCallback, |
| 74 wrapPersistent(this), wrapPersistent(resolver)))); | 77 wrapPersistent(this), wrapPersistent(resolver)))); |
| 75 | 78 |
| 76 return promise; | 79 return promise; |
| 77 } | 80 } |
| 78 | 81 |
| 79 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { | 82 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { |
| 83 RecordWebBluetoothFunctionCall( |
| 84 UMAWebBluetoothFunction::REMOTE_GATT_SERVER_DISCONNECT); |
| 85 |
| 80 if (!m_connected) | 86 if (!m_connected) |
| 81 return; | 87 return; |
| 82 m_device->CleanupDisconnectedDeviceAndFireEvent(); | 88 m_device->CleanupDisconnectedDeviceAndFireEvent(); |
| 83 m_device->bluetooth()->RemoveFromConnectedDevicesMap(m_device->id()); | 89 m_device->bluetooth()->RemoveFromConnectedDevicesMap(m_device->id()); |
| 84 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); | 90 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); |
| 85 service->RemoteServerDisconnect(m_device->id()); | 91 service->RemoteServerDisconnect(m_device->id()); |
| 86 } | 92 } |
| 87 | 93 |
| 88 // Callback that allows us to resolve the promise with a single service or | 94 // Callback that allows us to resolve the promise with a single service or |
| 89 // with a vector owning the services. | 95 // with a vector owning the services. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 } else { | 138 } else { |
| 133 resolver->reject(BluetoothError::CreateDOMException(result)); | 139 resolver->reject(BluetoothError::CreateDOMException(result)); |
| 134 } | 140 } |
| 135 } | 141 } |
| 136 } | 142 } |
| 137 | 143 |
| 138 ScriptPromise BluetoothRemoteGATTServer::getPrimaryService( | 144 ScriptPromise BluetoothRemoteGATTServer::getPrimaryService( |
| 139 ScriptState* scriptState, | 145 ScriptState* scriptState, |
| 140 const StringOrUnsignedLong& service, | 146 const StringOrUnsignedLong& service, |
| 141 ExceptionState& exceptionState) { | 147 ExceptionState& exceptionState) { |
| 148 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::GET_PRIMARY_SERVICE); |
| 149 |
| 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); |
| 145 | 153 |
| 146 return GetPrimaryServicesImpl( | 154 return GetPrimaryServicesImpl( |
| 147 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE, | 155 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE, |
| 148 serviceUUID); | 156 serviceUUID); |
| 149 } | 157 } |
| 150 | 158 |
| 151 ScriptPromise BluetoothRemoteGATTServer::getPrimaryServices( | 159 ScriptPromise BluetoothRemoteGATTServer::getPrimaryServices( |
| 152 ScriptState* scriptState, | 160 ScriptState* scriptState, |
| 153 const StringOrUnsignedLong& service, | 161 const StringOrUnsignedLong& service, |
| 154 ExceptionState& exceptionState) { | 162 ExceptionState& exceptionState) { |
| 163 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::GET_PRIMARY_SERVICES); |
| 164 |
| 155 String serviceUUID = BluetoothUUID::getService(service, exceptionState); | 165 String serviceUUID = BluetoothUUID::getService(service, exceptionState); |
| 156 if (exceptionState.hadException()) | 166 if (exceptionState.hadException()) |
| 157 return exceptionState.reject(scriptState); | 167 return exceptionState.reject(scriptState); |
| 158 | 168 |
| 159 return GetPrimaryServicesImpl( | 169 return GetPrimaryServicesImpl( |
| 160 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE, | 170 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE, |
| 161 serviceUUID); | 171 serviceUUID); |
| 162 } | 172 } |
| 163 | 173 |
| 164 ScriptPromise BluetoothRemoteGATTServer::getPrimaryServices( | 174 ScriptPromise BluetoothRemoteGATTServer::getPrimaryServices( |
| 165 ScriptState* scriptState, | 175 ScriptState* scriptState, |
| 166 ExceptionState&) { | 176 ExceptionState&) { |
| 177 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::GET_PRIMARY_SERVICES); |
| 178 |
| 167 return GetPrimaryServicesImpl( | 179 return GetPrimaryServicesImpl( |
| 168 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE); | 180 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE); |
| 169 } | 181 } |
| 170 | 182 |
| 171 ScriptPromise BluetoothRemoteGATTServer::GetPrimaryServicesImpl( | 183 ScriptPromise BluetoothRemoteGATTServer::GetPrimaryServicesImpl( |
| 172 ScriptState* scriptState, | 184 ScriptState* scriptState, |
| 173 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 185 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
| 174 String servicesUUID) { | 186 String servicesUUID) { |
| 175 if (!m_connected) { | 187 if (!m_connected) { |
| 176 return ScriptPromise::rejectWithDOMException( | 188 return ScriptPromise::rejectWithDOMException( |
| 177 scriptState, BluetoothError::CreateNotConnectedException( | 189 scriptState, BluetoothError::CreateNotConnectedException( |
| 178 BluetoothOperation::ServicesRetrieval)); | 190 BluetoothOperation::ServicesRetrieval)); |
| 179 } | 191 } |
| 180 | 192 |
| 181 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 193 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 182 ScriptPromise promise = resolver->promise(); | 194 ScriptPromise promise = resolver->promise(); |
| 183 AddToActiveAlgorithms(resolver); | 195 AddToActiveAlgorithms(resolver); |
| 184 | 196 |
| 185 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); | 197 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); |
| 186 service->RemoteServerGetPrimaryServices( | 198 service->RemoteServerGetPrimaryServices( |
| 187 m_device->id(), quantity, servicesUUID, | 199 m_device->id(), quantity, servicesUUID, |
| 188 convertToBaseCallback( | 200 convertToBaseCallback( |
| 189 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, | 201 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, |
| 190 wrapPersistent(this), servicesUUID, quantity, | 202 wrapPersistent(this), servicesUUID, quantity, |
| 191 wrapPersistent(resolver)))); | 203 wrapPersistent(resolver)))); |
| 192 return promise; | 204 return promise; |
| 193 } | 205 } |
| 194 | 206 |
| 195 } // namespace blink | 207 } // namespace blink |
| OLD | NEW |