Chromium Code Reviews| 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 19 matching lines...) Expand all Loading... | |
| 30 ExecutionContext* context, | 30 ExecutionContext* context, |
| 31 BluetoothDevice* device) { | 31 BluetoothDevice* device) { |
| 32 return new BluetoothRemoteGATTServer(context, device); | 32 return new BluetoothRemoteGATTServer(context, device); |
| 33 } | 33 } |
| 34 | 34 |
| 35 void BluetoothRemoteGATTServer::contextDestroyed(ExecutionContext*) { | 35 void BluetoothRemoteGATTServer::contextDestroyed(ExecutionContext*) { |
| 36 Dispose(); | 36 Dispose(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 void BluetoothRemoteGATTServer::GATTServerDisconnected() { | 39 void BluetoothRemoteGATTServer::GATTServerDisconnected() { |
| 40 DispatchDisconnected(); | 40 // This function can only be called if |m_clientBinding| is bound and |
| 41 // |m_clientBinding| can only be bound if |m_connected| is true. | |
| 42 // Therefore we skip Step 2 of Responding to Disconnection. | |
| 43 // https://webbluetoothcg.github.io/web-bluetooth/#disconnection-events | |
| 44 DCHECK(m_clientBinding.is_bound()); | |
| 45 CleanupDisconnectedDevice(true /* dispatchEvent */); | |
| 41 } | 46 } |
| 42 | 47 |
| 43 void BluetoothRemoteGATTServer::AddToActiveAlgorithms( | 48 void BluetoothRemoteGATTServer::AddToActiveAlgorithms( |
| 44 ScriptPromiseResolver* resolver) { | 49 ScriptPromiseResolver* resolver) { |
| 45 auto result = m_activeAlgorithms.insert(resolver); | 50 auto result = m_activeAlgorithms.insert(resolver); |
| 46 CHECK(result.isNewEntry); | 51 CHECK(result.isNewEntry); |
| 47 } | 52 } |
| 48 | 53 |
| 49 bool BluetoothRemoteGATTServer::RemoveFromActiveAlgorithms( | 54 bool BluetoothRemoteGATTServer::RemoveFromActiveAlgorithms( |
| 50 ScriptPromiseResolver* resolver) { | 55 ScriptPromiseResolver* resolver) { |
| 51 if (!m_activeAlgorithms.contains(resolver)) { | 56 if (!m_activeAlgorithms.contains(resolver)) { |
| 52 return false; | 57 return false; |
| 53 } | 58 } |
| 54 m_activeAlgorithms.erase(resolver); | 59 m_activeAlgorithms.erase(resolver); |
| 55 return true; | 60 return true; |
| 56 } | 61 } |
| 57 | 62 |
| 58 void BluetoothRemoteGATTServer::DisconnectIfConnected() { | 63 void BluetoothRemoteGATTServer::CleanupDisconnectedDevice(bool dispatchEvent) { |
|
scheib
2017/03/22 01:11:08
ortuno reminds us that this implementation is inte
juncai
2017/03/27 20:44:46
Done.
| |
| 59 if (m_connected) { | 64 DCHECK(m_connected); |
| 60 SetConnected(false); | 65 SetConnected(false); |
| 61 ClearActiveAlgorithms(); | 66 ClearActiveAlgorithms(); |
| 62 mojom::blink::WebBluetoothService* service = | 67 m_device->ClearAttributeInstanceMap(); |
|
scheib
2017/03/22 01:11:08
Below this line there are steps in the specificati
juncai
2017/03/27 20:44:46
Done.
| |
| 63 m_device->bluetooth()->Service(); | 68 if (dispatchEvent) { |
| 64 service->RemoteServerDisconnect(m_device->id()); | 69 m_device->dispatchEvent( |
| 70 Event::createBubble(EventTypeNames::gattserverdisconnected)); | |
| 65 } | 71 } |
| 66 } | 72 } |
| 67 | 73 |
| 68 void BluetoothRemoteGATTServer::CleanupDisconnectedDeviceAndFireEvent() { | 74 void BluetoothRemoteGATTServer::HandleClientConnectionError() { |
| 69 DCHECK(m_connected); | |
| 70 SetConnected(false); | |
| 71 ClearActiveAlgorithms(); | |
| 72 m_device->ClearAttributeInstanceMapAndFireEvent(); | |
| 73 } | |
| 74 | |
| 75 void BluetoothRemoteGATTServer::DispatchDisconnected() { | |
| 76 if (!m_connected) { | 75 if (!m_connected) { |
| 77 return; | 76 return; |
| 78 } | 77 } |
| 79 CleanupDisconnectedDeviceAndFireEvent(); | 78 CleanupDisconnectedDevice(true /* dispatchEvent */); |
| 80 } | 79 } |
| 81 | 80 |
| 82 void BluetoothRemoteGATTServer::Dispose() { | 81 void BluetoothRemoteGATTServer::Dispose() { |
| 83 DisconnectIfConnected(); | 82 if (!m_connected) { |
| 84 // The pipe to this object must be closed when is marked unreachable to | 83 return; |
| 85 // prevent messages from being dispatched before lazy sweeping. | 84 } |
| 85 // The object is being garbage collected or the context is being destroyed, | |
| 86 // so no need to dispatch a gattserverdisconnected event. | |
| 87 CleanupDisconnectedDevice(false /* dispatchEvent */); | |
|
scheib
2017/03/22 01:11:08
Thanks! So much clearer.
| |
| 88 DCHECK(m_clientBinding.is_bound()); | |
| 86 m_clientBinding.Close(); | 89 m_clientBinding.Close(); |
| 87 } | 90 } |
| 88 | 91 |
| 89 DEFINE_TRACE(BluetoothRemoteGATTServer) { | 92 DEFINE_TRACE(BluetoothRemoteGATTServer) { |
| 90 visitor->trace(m_activeAlgorithms); | 93 visitor->trace(m_activeAlgorithms); |
| 91 visitor->trace(m_device); | 94 visitor->trace(m_device); |
| 92 ContextLifecycleObserver::trace(visitor); | 95 ContextLifecycleObserver::trace(visitor); |
| 93 } | 96 } |
| 94 | 97 |
| 95 void BluetoothRemoteGATTServer::ConnectCallback( | 98 void BluetoothRemoteGATTServer::ConnectCallback( |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 107 } | 110 } |
| 108 } | 111 } |
| 109 | 112 |
| 110 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { | 113 ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { |
| 111 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 114 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 112 ScriptPromise promise = resolver->promise(); | 115 ScriptPromise promise = resolver->promise(); |
| 113 | 116 |
| 114 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); | 117 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); |
| 115 mojom::blink::WebBluetoothServerClientAssociatedPtrInfo ptrInfo; | 118 mojom::blink::WebBluetoothServerClientAssociatedPtrInfo ptrInfo; |
| 116 m_clientBinding.Bind(&ptrInfo); | 119 m_clientBinding.Bind(&ptrInfo); |
| 120 m_clientBinding.set_connection_error_handler(convertToBaseCallback( | |
| 121 WTF::bind(&BluetoothRemoteGATTServer::HandleClientConnectionError, | |
| 122 wrapWeakPersistent(this)))); | |
| 117 service->RemoteServerConnect( | 123 service->RemoteServerConnect( |
| 118 m_device->id(), std::move(ptrInfo), | 124 m_device->id(), std::move(ptrInfo), |
| 119 convertToBaseCallback( | 125 convertToBaseCallback( |
| 120 WTF::bind(&BluetoothRemoteGATTServer::ConnectCallback, | 126 WTF::bind(&BluetoothRemoteGATTServer::ConnectCallback, |
| 121 wrapPersistent(this), wrapPersistent(resolver)))); | 127 wrapPersistent(this), wrapPersistent(resolver)))); |
| 122 | 128 |
| 123 return promise; | 129 return promise; |
| 124 } | 130 } |
| 125 | 131 |
| 126 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { | 132 void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { |
| 127 if (!m_connected) | 133 ClearActiveAlgorithms(); |
| 134 if (!m_connected) { | |
| 128 return; | 135 return; |
| 129 CleanupDisconnectedDeviceAndFireEvent(); | 136 } |
| 137 CleanupDisconnectedDevice(true /* dispatchEvent */); | |
| 138 DCHECK(m_clientBinding.is_bound()); | |
| 130 m_clientBinding.Close(); | 139 m_clientBinding.Close(); |
|
scheib
2017/03/22 01:11:08
It now becomes less clear how we are signaling to
juncai
2017/03/27 20:44:46
Done.
| |
| 131 mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); | |
| 132 service->RemoteServerDisconnect(m_device->id()); | |
| 133 } | 140 } |
| 134 | 141 |
| 135 // Callback that allows us to resolve the promise with a single service or | 142 // Callback that allows us to resolve the promise with a single service or |
| 136 // with a vector owning the services. | 143 // with a vector owning the services. |
| 137 void BluetoothRemoteGATTServer::GetPrimaryServicesCallback( | 144 void BluetoothRemoteGATTServer::GetPrimaryServicesCallback( |
| 138 const String& requestedServiceUUID, | 145 const String& requestedServiceUUID, |
| 139 mojom::blink::WebBluetoothGATTQueryQuantity quantity, | 146 mojom::blink::WebBluetoothGATTQueryQuantity quantity, |
| 140 ScriptPromiseResolver* resolver, | 147 ScriptPromiseResolver* resolver, |
| 141 mojom::blink::WebBluetoothResult result, | 148 mojom::blink::WebBluetoothResult result, |
| 142 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTServicePtr>> services) { | 149 Optional<Vector<mojom::blink::WebBluetoothRemoteGATTServicePtr>> services) { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 233 service->RemoteServerGetPrimaryServices( | 240 service->RemoteServerGetPrimaryServices( |
| 234 m_device->id(), quantity, servicesUUID, | 241 m_device->id(), quantity, servicesUUID, |
| 235 convertToBaseCallback( | 242 convertToBaseCallback( |
| 236 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, | 243 WTF::bind(&BluetoothRemoteGATTServer::GetPrimaryServicesCallback, |
| 237 wrapPersistent(this), servicesUUID, quantity, | 244 wrapPersistent(this), servicesUUID, quantity, |
| 238 wrapPersistent(resolver)))); | 245 wrapPersistent(resolver)))); |
| 239 return promise; | 246 return promise; |
| 240 } | 247 } |
| 241 | 248 |
| 242 } // namespace blink | 249 } // namespace blink |
| OLD | NEW |