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 "mojo/public/cpp/bindings/associated_interface_ptr.h" | |
19 #include <utility> | 18 #include <utility> |
20 | 19 |
21 namespace blink { | 20 namespace blink { |
22 | 21 |
23 BluetoothRemoteGATTServer::BluetoothRemoteGATTServer(ExecutionContext* context, | 22 BluetoothRemoteGATTServer::BluetoothRemoteGATTServer(ExecutionContext* context, |
24 BluetoothDevice* device) | 23 BluetoothDevice* device) |
25 : ContextLifecycleObserver(context), m_device(device), m_connected(false) {} | 24 : ContextLifecycleObserver(context), |
| 25 m_clientBinding(this), |
| 26 m_device(device), |
| 27 m_connected(false) {} |
26 | 28 |
27 BluetoothRemoteGATTServer* BluetoothRemoteGATTServer::Create( | 29 BluetoothRemoteGATTServer* BluetoothRemoteGATTServer::Create( |
28 ExecutionContext* context, | 30 ExecutionContext* context, |
29 BluetoothDevice* device) { | 31 BluetoothDevice* device) { |
30 return new BluetoothRemoteGATTServer(context, device); | 32 return new BluetoothRemoteGATTServer(context, device); |
31 } | 33 } |
32 | 34 |
33 void BluetoothRemoteGATTServer::contextDestroyed(ExecutionContext*) { | 35 void BluetoothRemoteGATTServer::contextDestroyed(ExecutionContext*) { |
34 Dispose(); | 36 Dispose(); |
35 } | 37 } |
36 | 38 |
37 void BluetoothRemoteGATTServer::GATTServerDisconnected() { | 39 void BluetoothRemoteGATTServer::GATTServerDisconnected() { |
38 DispatchDisconnected(); | 40 if (!m_connected) { |
| 41 return; |
| 42 } |
| 43 DCHECK(m_clientBinding.is_bound()); |
| 44 CleanupDisconnectedDevice(true /* dispatchEvent */); |
39 } | 45 } |
40 | 46 |
41 void BluetoothRemoteGATTServer::AddToActiveAlgorithms( | 47 void BluetoothRemoteGATTServer::AddToActiveAlgorithms( |
42 ScriptPromiseResolver* resolver) { | 48 ScriptPromiseResolver* resolver) { |
43 auto result = m_activeAlgorithms.insert(resolver); | 49 auto result = m_activeAlgorithms.insert(resolver); |
44 CHECK(result.isNewEntry); | 50 CHECK(result.isNewEntry); |
45 } | 51 } |
46 | 52 |
47 bool BluetoothRemoteGATTServer::RemoveFromActiveAlgorithms( | 53 bool BluetoothRemoteGATTServer::RemoveFromActiveAlgorithms( |
48 ScriptPromiseResolver* resolver) { | 54 ScriptPromiseResolver* resolver) { |
49 if (!m_activeAlgorithms.contains(resolver)) { | 55 if (!m_activeAlgorithms.contains(resolver)) { |
50 return false; | 56 return false; |
51 } | 57 } |
52 m_activeAlgorithms.erase(resolver); | 58 m_activeAlgorithms.erase(resolver); |
53 return true; | 59 return true; |
54 } | 60 } |
55 | 61 |
56 void BluetoothRemoteGATTServer::DisconnectIfConnected() { | 62 void BluetoothRemoteGATTServer::CleanupDisconnectedDevice(bool dispatchEvent) { |
57 if (m_connected) { | 63 // This implements the specification "clean up the disconnected device" |
58 SetConnected(false); | 64 // algorithm: |
59 ClearActiveAlgorithms(); | 65 // https://webbluetoothcg.github.io/web-bluetooth/#clean-up-the-disconnected-d
evice |
60 mojom::blink::WebBluetoothService* service = | 66 DCHECK(m_connected); |
61 m_device->bluetooth()->Service(); | 67 SetConnected(false); |
62 service->RemoteServerDisconnect(m_device->id()); | 68 ClearActiveAlgorithms(); |
| 69 m_device->ClearAttributeInstanceMap(); |
| 70 // Below this line there are steps in the above specification which we are |
| 71 // not explicitly handling here: clearing the representedService, |
| 72 // representedCharacteristic and representedDescriptors is done by |
| 73 // ClearAttributeInstanceMap(). The notification context is implemented in |
| 74 // the browser so it cleans up itself. |
| 75 if (dispatchEvent) { |
| 76 m_device->dispatchEvent( |
| 77 Event::createBubble(EventTypeNames::gattserverdisconnected)); |
63 } | 78 } |
64 } | 79 } |
65 | 80 |
66 void BluetoothRemoteGATTServer::CleanupDisconnectedDeviceAndFireEvent() { | 81 void BluetoothRemoteGATTServer::HandleClientConnectionError() { |
67 DCHECK(m_connected); | |
68 SetConnected(false); | |
69 ClearActiveAlgorithms(); | |
70 m_device->ClearAttributeInstanceMapAndFireEvent(); | |
71 } | |
72 | |
73 void BluetoothRemoteGATTServer::DispatchDisconnected() { | |
74 if (!m_connected) { | 82 if (!m_connected) { |
75 return; | 83 return; |
76 } | 84 } |
77 CleanupDisconnectedDeviceAndFireEvent(); | 85 CleanupDisconnectedDevice(true /* dispatchEvent */); |
78 } | 86 } |
79 | 87 |
80 void BluetoothRemoteGATTServer::Dispose() { | 88 void BluetoothRemoteGATTServer::Dispose() { |
81 DisconnectIfConnected(); | 89 if (!m_connected) { |
82 // The pipe to this object must be closed when is marked unreachable to | 90 return; |
83 // prevent messages from being dispatched before lazy sweeping. | 91 } |
84 m_clientBindings.CloseAllBindings(); | 92 // The object is being garbage collected or the context is being destroyed, |
| 93 // so no need to dispatch a gattserverdisconnected event. |
| 94 CleanupDisconnectedDevice(false /* dispatchEvent */); |
| 95 DCHECK(m_clientBinding.is_bound()); |
| 96 m_clientBinding.Close(); |
85 } | 97 } |
86 | 98 |
87 DEFINE_TRACE(BluetoothRemoteGATTServer) { | 99 DEFINE_TRACE(BluetoothRemoteGATTServer) { |
88 visitor->trace(m_activeAlgorithms); | 100 visitor->trace(m_activeAlgorithms); |
89 visitor->trace(m_device); | 101 visitor->trace(m_device); |
90 ContextLifecycleObserver::trace(visitor); | 102 ContextLifecycleObserver::trace(visitor); |
91 } | 103 } |
92 | 104 |
93 void BluetoothRemoteGATTServer::ConnectCallback( | 105 void BluetoothRemoteGATTServer::ConnectCallback( |
94 ScriptPromiseResolver* resolver, | 106 ScriptPromiseResolver* resolver, |
95 mojom::blink::WebBluetoothResult result) { | 107 mojom::blink::WebBluetoothResult result) { |
96 if (!resolver->getExecutionContext() || | 108 if (!resolver->getExecutionContext() || |
97 resolver->getExecutionContext()->isContextDestroyed()) | 109 resolver->getExecutionContext()->isContextDestroyed()) |
98 return; | 110 return; |
99 | 111 |
100 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 112 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
101 SetConnected(true); | 113 SetConnected(true); |
102 resolver->resolve(this); | 114 resolver->resolve(this); |
103 } else { | 115 } else { |
104 resolver->reject(BluetoothError::CreateDOMException(result)); | 116 resolver->reject(BluetoothError::CreateDOMException(result)); |
105 } | 117 } |
106 } | 118 } |
107 | 119 |
108 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { | 120 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { |
109 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 121 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
110 ScriptPromise promise = resolver->promise(); | 122 ScriptPromise promise = resolver->promise(); |
111 | 123 |
112 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); | 124 if (m_connected) { |
113 mojom::blink::WebBluetoothServerClientAssociatedPtrInfo ptrInfo; | 125 resolver->resolve(this); |
114 auto request = mojo::MakeRequest(&ptrInfo); | 126 } else { |
115 m_clientBindings.AddBinding(this, std::move(request)); | 127 mojom::blink::WebBluetoothService* service = |
| 128 m_device->bluetooth()->Service(); |
| 129 mojom::blink::WebBluetoothServerClientAssociatedPtrInfo ptrInfo; |
| 130 m_clientBinding.Bind(&ptrInfo); |
| 131 m_clientBinding.set_connection_error_handler(convertToBaseCallback( |
| 132 WTF::bind(&BluetoothRemoteGATTServer::HandleClientConnectionError, |
| 133 wrapWeakPersistent(this)))); |
116 | 134 |
117 service->RemoteServerConnect( | 135 service->RemoteServerConnect( |
118 m_device->id(), std::move(ptrInfo), | 136 m_device->id(), std::move(ptrInfo), |
119 convertToBaseCallback( | 137 convertToBaseCallback( |
120 WTF::bind(&BluetoothRemoteGATTServer::ConnectCallback, | 138 WTF::bind(&BluetoothRemoteGATTServer::ConnectCallback, |
121 wrapPersistent(this), wrapPersistent(resolver)))); | 139 wrapPersistent(this), wrapPersistent(resolver)))); |
| 140 } |
122 | 141 |
123 return promise; | 142 return promise; |
124 } | 143 } |
125 | 144 |
126 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { | 145 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { |
127 if (!m_connected) | 146 // ClearActiveAlgorithms() implements the specification |
| 147 // "1. Clear this.[[activeAlgorithms]] to abort any active connect() calls." |
| 148 // at: |
| 149 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothremotegattserv
er-disconnect |
| 150 // connect() calls will add a promise to this active algorithms map. When |
| 151 // connect's callback is run we check that the promise is still in this |
| 152 // map and if not we reject the promise. During this, |m_connected| stays |
| 153 // false so if we want to actually cancel the connection we need to do so |
| 154 // before the |m_connected| check. |
| 155 ClearActiveAlgorithms(); |
| 156 if (!m_connected) { |
128 return; | 157 return; |
129 CleanupDisconnectedDeviceAndFireEvent(); | 158 } |
130 m_clientBindings.CloseAllBindings(); | 159 CleanupDisconnectedDevice(true /* dispatchEvent */); |
131 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); | 160 DCHECK(m_clientBinding.is_bound()); |
132 service->RemoteServerDisconnect(m_device->id()); | 161 // Implicitly signals disconnect via the connection_error_handler. |
| 162 m_clientBinding.Close(); |
133 } | 163 } |
134 | 164 |
135 // Callback that allows us to resolve the promise with a single service or | 165 // Callback that allows us to resolve the promise with a single service or |
136 // with a vector owning the services. | 166 // with a vector owning the services. |
137 void BluetoothRemoteGATTServer::GetPrimaryServicesCallback( | 167 void BluetoothRemoteGATTServer::GetPrimaryServicesCallback( |
138 const String& requestedServiceUUID, | 168 const String& requestedServiceUUID, |
139 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 169 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
140 ScriptPromiseResolver* resolver, | 170 ScriptPromiseResolver* resolver, |
141 mojom::blink::WebBluetoothResult result, | 171 mojom::blink::WebBluetoothResult result, |
142 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTServicePtr>> services) { | 172 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTServicePtr>> services) { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 service->RemoteServerGetPrimaryServices( | 263 service->RemoteServerGetPrimaryServices( |
234 m_device->id(), quantity, servicesUUID, | 264 m_device->id(), quantity, servicesUUID, |
235 convertToBaseCallback( | 265 convertToBaseCallback( |
236 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, | 266 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, |
237 wrapPersistent(this), servicesUUID, quantity, | 267 wrapPersistent(this), servicesUUID, quantity, |
238 wrapPersistent(resolver)))); | 268 wrapPersistent(resolver)))); |
239 return promise; | 269 return promise; |
240 } | 270 } |
241 | 271 |
242 } // namespace blink | 272 } // namespace blink |
OLD | NEW |