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..32ed934f31d80aec7ba3e3033f86e555849f44d7 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*) { | 
| + DisconnectIfConnected(); | 
| 
 
haraken
2017/03/16 08:39:43
Do we want to call Dispose() as well?
 
juncai
2017/03/16 18:06:24
Done.
 
 | 
| +} | 
| + | 
| +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()) | 
| + 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()) | 
| + m_clientBinding.Close(); | 
| mojom::blink::WebBluetoothService* service = m_device->bluetooth()->Service(); | 
| service->RemoteServerDisconnect(m_device->id()); | 
| } |