Chromium Code Reviews| Index: third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTServer.cpp |
| diff --git a/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTServer.cpp b/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTServer.cpp |
| index 8aca52efb5b14381215b3b9e844913c92bb47e59..22231cc657f9da087917ab57910e48979a1b8282 100644 |
| --- a/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTServer.cpp |
| +++ b/third_party/WebKit/Source/modules/bluetooth/BluetoothRemoteGATTServer.cpp |
| @@ -19,12 +19,25 @@ |
| namespace blink { |
| -BluetoothRemoteGATTServer::BluetoothRemoteGATTServer(BluetoothDevice* device) |
| - : m_device(device), m_connected(false) {} |
| +BluetoothRemoteGATTServer::BluetoothRemoteGATTServer(ExecutionContext* context, |
| + BluetoothDevice* device) |
| + : ContextLifecycleObserver(context), |
| + m_clientBinding(this), |
| + m_device(device), |
| + m_connected(false) {} |
| BluetoothRemoteGATTServer* BluetoothRemoteGATTServer::Create( |
| + ExecutionContext* context, |
| BluetoothDevice* device) { |
| - return new BluetoothRemoteGATTServer(device); |
| + return new BluetoothRemoteGATTServer(context, device); |
| +} |
| + |
| +void BluetoothRemoteGATTServer::contextDestroyed(ExecutionContext*) { |
| + Dispose(); |
| +} |
| + |
| +void BluetoothRemoteGATTServer::GATTServerDisconnected() { |
| + DispatchDisconnected(); |
| } |
| void BluetoothRemoteGATTServer::AddToActiveAlgorithms( |
| @@ -42,9 +55,42 @@ bool BluetoothRemoteGATTServer::RemoveFromActiveAlgorithms( |
| return true; |
| } |
| +void BluetoothRemoteGATTServer::DisconnectIfConnected() { |
| + if (m_connected) { |
| + SetConnected(false); |
| + ClearActiveAlgorithms(); |
| + mojom::blink::WebBluetoothService* service = |
| + m_device->bluetooth()->Service(); |
| + service->RemoteServerDisconnect(m_device->id()); |
| + } |
| +} |
| + |
| +void BluetoothRemoteGATTServer::CleanupDisconnectedDeviceAndFireEvent() { |
| + DCHECK(m_connected); |
| + SetConnected(false); |
| + ClearActiveAlgorithms(); |
| + m_device->ClearAttributeInstanceMapAndFireEvent(); |
| +} |
| + |
| +void BluetoothRemoteGATTServer::DispatchDisconnected() { |
| + if (!m_connected) { |
| + return; |
| + } |
| + CleanupDisconnectedDeviceAndFireEvent(); |
| +} |
| + |
| +void BluetoothRemoteGATTServer::Dispose() { |
| + DisconnectIfConnected(); |
| + // The pipe to this object must be closed when is marked unreachable to |
| + // prevent messages from being dispatched before lazy sweeping. |
| + if (m_clientBinding.is_bound()) |
|
dcheng
2017/03/16 22:20:29
Nit: no need to check is_bound()
juncai
2017/03/17 00:53:56
Done.
|
| + m_clientBinding.Close(); |
| +} |
| + |
| DEFINE_TRACE(BluetoothRemoteGATTServer) { |
| visitor->trace(m_activeAlgorithms); |
| visitor->trace(m_device); |
| + ContextLifecycleObserver::trace(visitor); |
| } |
| void BluetoothRemoteGATTServer::ConnectCallback( |
| @@ -55,7 +101,6 @@ void BluetoothRemoteGATTServer::ConnectCallback( |
| return; |
| if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| - m_device->bluetooth()->AddToConnectedDevicesMap(m_device->id(), m_device); |
| SetConnected(true); |
| resolver->resolve(this); |
| } else { |
| @@ -68,10 +113,13 @@ ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { |
| ScriptPromise promise = resolver->promise(); |
| mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); |
| + mojom::blink::WebBluetoothServerClientAssociatedPtrInfo ptrInfo; |
| + m_clientBinding.Bind(&ptrInfo); |
| service->RemoteServerConnect( |
| - m_device->id(), convertToBaseCallback(WTF::bind( |
| - &BluetoothRemoteGATTServer::ConnectCallback, |
| - wrapPersistent(this), wrapPersistent(resolver)))); |
| + m_device->id(), std::move(ptrInfo), |
| + convertToBaseCallback( |
| + WTF::bind(&BluetoothRemoteGATTServer::ConnectCallback, |
| + wrapPersistent(this), wrapPersistent(resolver)))); |
| return promise; |
| } |
| @@ -79,8 +127,9 @@ ScriptPromise BluetoothRemoteGATTServer::connect(ScriptState* scriptState) { |
| void BluetoothRemoteGATTServer::disconnect(ScriptState* scriptState) { |
| if (!m_connected) |
| return; |
| - m_device->CleanupDisconnectedDeviceAndFireEvent(); |
| - m_device->bluetooth()->RemoveFromConnectedDevicesMap(m_device->id()); |
| + CleanupDisconnectedDeviceAndFireEvent(); |
| + if (m_clientBinding.is_bound()) |
|
dcheng
2017/03/16 22:20:29
Nit: it's unnecessary to do this check.
juncai
2017/03/17 00:53:57
Done.
|
| + m_clientBinding.Close(); |
| mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); |
| service->RemoteServerDisconnect(m_device->id()); |
| } |