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 #include <utility> |
19 | 19 |
20 namespace blink { | 20 namespace blink { |
21 | 21 |
22 namespace { | |
23 | |
24 const char kGATTServerNotConnected[] = | |
25 "GATT Server is disconnected. Cannot retrieve services. (Re)connect first " | |
26 "with `device.gatt.connect`."; | |
27 | |
28 } // namespace | |
29 | |
30 BluetoothRemoteGATTServer::BluetoothRemoteGATTServer(BluetoothDevice* device) | 22 BluetoothRemoteGATTServer::BluetoothRemoteGATTServer(BluetoothDevice* device) |
31 : m_device(device), m_connected(false) {} | 23 : m_device(device), m_connected(false) {} |
32 | 24 |
33 BluetoothRemoteGATTServer* BluetoothRemoteGATTServer::create( | 25 BluetoothRemoteGATTServer* BluetoothRemoteGATTServer::create( |
34 BluetoothDevice* device) { | 26 BluetoothDevice* device) { |
35 return new BluetoothRemoteGATTServer(device); | 27 return new BluetoothRemoteGATTServer(device); |
36 } | 28 } |
37 | 29 |
38 void BluetoothRemoteGATTServer::AddToActiveAlgorithms( | 30 void BluetoothRemoteGATTServer::AddToActiveAlgorithms( |
39 ScriptPromiseResolver* resolver) { | 31 ScriptPromiseResolver* resolver) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 92 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
101 ScriptPromiseResolver* resolver, | 93 ScriptPromiseResolver* resolver, |
102 mojom::blink::WebBluetoothResult result, | 94 mojom::blink::WebBluetoothResult result, |
103 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTServicePtr>> services) { | 95 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTServicePtr>> services) { |
104 if (!resolver->getExecutionContext() || | 96 if (!resolver->getExecutionContext() || |
105 resolver->getExecutionContext()->isContextDestroyed()) | 97 resolver->getExecutionContext()->isContextDestroyed()) |
106 return; | 98 return; |
107 | 99 |
108 // If the device is disconnected, reject. | 100 // If the device is disconnected, reject. |
109 if (!RemoveFromActiveAlgorithms(resolver)) { | 101 if (!RemoveFromActiveAlgorithms(resolver)) { |
110 resolver->reject( | 102 resolver->reject(BluetoothError::createNotConnectedException( |
111 DOMException::create(NetworkError, kGATTServerNotConnected)); | 103 BluetoothOperation::ServicesRetrieval)); |
112 return; | 104 return; |
113 } | 105 } |
114 | 106 |
115 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 107 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
116 DCHECK(services); | 108 DCHECK(services); |
117 | 109 |
118 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { | 110 if (quantity == mojom::blink::WebBluetoothGATTQueryQuantity::SINGLE) { |
119 DCHECK_EQ(1u, services->size()); | 111 DCHECK_EQ(1u, services->size()); |
120 resolver->resolve(m_device->getOrCreateRemoteGATTService( | 112 resolver->resolve(m_device->getOrCreateRemoteGATTService( |
121 std::move(services.value()[0]), true /* isPrimary */, | 113 std::move(services.value()[0]), true /* isPrimary */, |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 return getPrimaryServicesImpl( | 167 return getPrimaryServicesImpl( |
176 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE); | 168 scriptState, mojom::blink::WebBluetoothGATTQueryQuantity::MULTIPLE); |
177 } | 169 } |
178 | 170 |
179 ScriptPromise BluetoothRemoteGATTServer::getPrimaryServicesImpl( | 171 ScriptPromise BluetoothRemoteGATTServer::getPrimaryServicesImpl( |
180 ScriptState* scriptState, | 172 ScriptState* scriptState, |
181 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 173 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
182 String servicesUUID) { | 174 String servicesUUID) { |
183 if (!m_connected) { | 175 if (!m_connected) { |
184 return ScriptPromise::rejectWithDOMException( | 176 return ScriptPromise::rejectWithDOMException( |
185 scriptState, | 177 scriptState, BluetoothError::createNotConnectedException( |
186 DOMException::create(NetworkError, kGATTServerNotConnected)); | 178 BluetoothOperation::ServicesRetrieval)); |
187 } | 179 } |
188 | 180 |
189 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 181 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
190 ScriptPromise promise = resolver->promise(); | 182 ScriptPromise promise = resolver->promise(); |
191 AddToActiveAlgorithms(resolver); | 183 AddToActiveAlgorithms(resolver); |
192 | 184 |
193 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); | 185 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->service(); |
194 service->RemoteServerGetPrimaryServices( | 186 service->RemoteServerGetPrimaryServices( |
195 m_device->id(), quantity, servicesUUID, | 187 m_device->id(), quantity, servicesUUID, |
196 convertToBaseCallback( | 188 convertToBaseCallback( |
197 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, | 189 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, |
198 wrapPersistent(this), servicesUUID, quantity, | 190 wrapPersistent(this), servicesUUID, quantity, |
199 wrapPersistent(resolver)))); | 191 wrapPersistent(resolver)))); |
200 return promise; | 192 return promise; |
201 } | 193 } |
202 | 194 |
203 } // namespace blink | 195 } // namespace blink |
OLD | NEW |