From 6999dd655b3677466c0dc5f38d07a2382eab8750 Mon Sep 17 00:00:00 2001 From: Thomas Zimmermann Date: Wed, 29 Apr 2015 11:19:28 +0200 Subject: [PATCH] Bug 1159209: Remove template parameters from |SocketIOEventRunnable|, r=kmachulis This patch removes the template parameters from |SocketIOEventRunnable| and moves its methods into the C++ source file. All users have been adapted. --- dom/bluetooth/bluedroid/BluetoothSocket.cpp | 18 ++--- dom/bluetooth/bluez/BluetoothSocket.cpp | 22 ++--- ipc/bluetooth/BluetoothDaemonConnection.cpp | 15 ++-- ipc/unixsocket/ListenSocket.cpp | 17 ++-- ipc/unixsocket/SocketBase.cpp | 37 +++++++++ ipc/unixsocket/SocketBase.h | 90 +++++++++------------ ipc/unixsocket/StreamSocket.cpp | 30 +++---- 7 files changed, 112 insertions(+), 117 deletions(-) diff --git a/dom/bluetooth/bluedroid/BluetoothSocket.cpp b/dom/bluetooth/bluedroid/BluetoothSocket.cpp index 683ae5bae58b..339d98d08785 100644 --- a/dom/bluetooth/bluedroid/BluetoothSocket.cpp +++ b/dom/bluetooth/bluedroid/BluetoothSocket.cpp @@ -91,13 +91,13 @@ public: AddWatchers(WRITE_WATCHER, false); } - bool IsShutdownOnMainThread() + bool IsShutdownOnMainThread() const override { MOZ_ASSERT(NS_IsMainThread()); return mConsumer == nullptr; } - bool IsShutdownOnIOThread() + bool IsShutdownOnIOThread() const { return mShuttingDownOnIOThread; } @@ -144,7 +144,7 @@ public: return GetBluetoothSocket(); } - SocketBase* GetSocketBase() + SocketBase* GetSocketBase() override { return GetDataSocket(); } @@ -310,10 +310,8 @@ DroidSocketImpl::Accept(int aFd) SetFd(aFd); mConnectionStatus = SOCKET_IS_CONNECTED; - nsRefPtr r = - new SocketIOEventRunnable( - this, SocketIOEventRunnable::CONNECT_SUCCESS); - NS_DispatchToMainThread(r); + NS_DispatchToMainThread( + new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS)); AddWatchers(READ_WATCHER, true); if (HasPendingData()) { @@ -499,10 +497,8 @@ DroidSocketImpl::OnSocketCanConnectWithoutBlocking(int aFd) mConnectionStatus = SOCKET_IS_CONNECTED; - nsRefPtr r = - new SocketIOEventRunnable( - this, SocketIOEventRunnable::CONNECT_SUCCESS); - NS_DispatchToMainThread(r); + NS_DispatchToMainThread( + new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS)); AddWatchers(READ_WATCHER, true); if (HasPendingData()) { diff --git a/dom/bluetooth/bluez/BluetoothSocket.cpp b/dom/bluetooth/bluez/BluetoothSocket.cpp index f01a29333f66..2ccdff7b274c 100644 --- a/dom/bluetooth/bluez/BluetoothSocket.cpp +++ b/dom/bluetooth/bluez/BluetoothSocket.cpp @@ -38,12 +38,12 @@ public: BluetoothSocket* GetBluetoothSocket(); DataSocket* GetDataSocket(); - SocketBase* GetSocketBase(); + SocketBase* GetSocketBase() override; // Shutdown state // - bool IsShutdownOnMainThread() const; + bool IsShutdownOnMainThread() const override; void ShutdownOnMainThread(); bool IsShutdownOnIOThread() const; @@ -352,10 +352,8 @@ BluetoothSocket::BluetoothSocketIO::OnAccepted( } SetSocket(aFd, SOCKET_IS_CONNECTED); - nsRefPtr r = - new SocketIOEventRunnable( - this, SocketIOEventRunnable::CONNECT_SUCCESS); - NS_DispatchToMainThread(r); + NS_DispatchToMainThread( + new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS)); AddWatchers(READ_WATCHER, true); if (HasPendingData()) { @@ -381,10 +379,8 @@ BluetoothSocket::BluetoothSocketIO::OnConnected() return; } - nsRefPtr r = - new SocketIOEventRunnable( - this, SocketIOEventRunnable::CONNECT_SUCCESS); - NS_DispatchToMainThread(r); + NS_DispatchToMainThread( + new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS)); AddWatchers(READ_WATCHER, true); if (HasPendingData()) { @@ -457,11 +453,9 @@ BluetoothSocket::BluetoothSocketIO::FireSocketError() Close(); // Tell the main thread we've errored - nsRefPtr r = - new SocketIOEventRunnable( - this, SocketIOEventRunnable::CONNECT_ERROR); + NS_DispatchToMainThread( + new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_ERROR)); - NS_DispatchToMainThread(r); } bool diff --git a/ipc/bluetooth/BluetoothDaemonConnection.cpp b/ipc/bluetooth/BluetoothDaemonConnection.cpp index 432c89d428f0..3151383aa7b8 100644 --- a/ipc/bluetooth/BluetoothDaemonConnection.cpp +++ b/ipc/bluetooth/BluetoothDaemonConnection.cpp @@ -192,6 +192,7 @@ BluetoothDaemonPDUConsumer::~BluetoothDaemonPDUConsumer() // class BluetoothDaemonConnectionIO final : public UnixSocketWatcher + , public SocketIOBase , public ConnectionOrientedSocketIO { public: @@ -478,11 +479,8 @@ BluetoothDaemonConnectionIO::OnConnected() MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED); - nsRefPtr r = - new SocketIOEventRunnable( - this, - SocketIOEventRunnable::CONNECT_SUCCESS); - NS_DispatchToMainThread(r); + NS_DispatchToMainThread( + new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS)); AddWatchers(READ_WATCHER, true); if (HasPendingData()) { @@ -501,11 +499,8 @@ BluetoothDaemonConnectionIO::OnError(const char* aFunction, int aErrno) Close(); // Tell the main thread we've errored - nsRefPtr r = - new SocketIOEventRunnable( - this, - SocketIOEventRunnable::CONNECT_ERROR); - NS_DispatchToMainThread(r); + NS_DispatchToMainThread( + new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_ERROR)); } // diff --git a/ipc/unixsocket/ListenSocket.cpp b/ipc/unixsocket/ListenSocket.cpp index 19b92a22cc91..2a65db9cc5e1 100644 --- a/ipc/unixsocket/ListenSocket.cpp +++ b/ipc/unixsocket/ListenSocket.cpp @@ -33,12 +33,12 @@ public: void GetSocketAddr(nsAString& aAddrStr) const; DataSocket* GetDataSocket(); - SocketBase* GetSocketBase(); + SocketBase* GetSocketBase() override; // Shutdown state // - bool IsShutdownOnMainThread() const; + bool IsShutdownOnMainThread() const override; void ShutdownOnMainThread(); bool IsShutdownOnIOThread() const; @@ -258,10 +258,8 @@ ListenSocketIO::OnListening() AddWatchers(READ_WATCHER, true); /* We signal a successful 'connection' to a local address for listening. */ - nsRefPtr runnable = - new SocketIOEventRunnable( - this, SocketIOEventRunnable::CONNECT_SUCCESS); - NS_DispatchToMainThread(runnable); + NS_DispatchToMainThread( + new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS)); } void @@ -282,11 +280,8 @@ ListenSocketIO::FireSocketError() Close(); // Tell the main thread we've errored - nsRefPtr r = - new SocketIOEventRunnable( - this, SocketIOEventRunnable::CONNECT_ERROR); - - NS_DispatchToMainThread(r); + NS_DispatchToMainThread( + new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_ERROR)); } bool diff --git a/ipc/unixsocket/SocketBase.cpp b/ipc/unixsocket/SocketBase.cpp index a145e2d83609..0859fcfdd0dc 100644 --- a/ipc/unixsocket/SocketBase.cpp +++ b/ipc/unixsocket/SocketBase.cpp @@ -278,5 +278,42 @@ SocketIOBase::SocketIOBase() SocketIOBase::~SocketIOBase() { } +// +// SocketIOEventRunnable +// + +SocketIOEventRunnable::SocketIOEventRunnable(SocketIOBase* aIO, + SocketEvent aEvent) + : SocketIORunnable(aIO) + , mEvent(aEvent) +{ } + +NS_METHOD +SocketIOEventRunnable::Run() +{ + MOZ_ASSERT(NS_IsMainThread()); + + SocketIOBase* io = SocketIORunnable::GetIO(); + + if (NS_WARN_IF(io->IsShutdownOnMainThread())) { + // Since we've already explicitly closed and the close + // happened before this, this isn't really an error. + return NS_OK; + } + + SocketBase* socketBase = io->GetSocketBase(); + MOZ_ASSERT(socketBase); + + if (mEvent == CONNECT_SUCCESS) { + socketBase->NotifySuccess(); + } else if (mEvent == CONNECT_ERROR) { + socketBase->NotifyError(); + } else if (mEvent == DISCONNECT) { + socketBase->NotifyDisconnect(); + } + + return NS_OK; +} + } } diff --git a/ipc/unixsocket/SocketBase.h b/ipc/unixsocket/SocketBase.h index 4192d4d1a034..ec234d809812 100644 --- a/ipc/unixsocket/SocketBase.h +++ b/ipc/unixsocket/SocketBase.h @@ -310,6 +310,39 @@ private: uint32_t mConnectDelayMs; }; +// +// SocketIOBase +// + +/** + * |SocketIOBase| is a base class for Socket I/O classes that + * perform operations on the I/O thread. + */ +class SocketIOBase +{ +public: + virtual ~SocketIOBase(); + + /** + * Implemented by socket I/O classes to return the current instance of + * |SocketBase|. + * + * @return The current instance of |SocketBase| + */ + virtual SocketBase* GetSocketBase() = 0; + + /** + * Implemented by socket I/O classes to signal that socket class has + * been shut down. + * + * @return True if the socket class has been shut down, false otherwise. + */ + virtual bool IsShutdownOnMainThread() const = 0; + +protected: + SocketIOBase(); +}; + // // Socket I/O runnables // @@ -340,11 +373,11 @@ private: T* mIO; }; -/* |SocketIOEventRunnable| reports the connection state on the - * I/O thrad back to the main thread. +/** + * |SocketIOEventRunnable| reports the connection state on the + * I/O thread back to the main thread. */ -template -class SocketIOEventRunnable final : public SocketIORunnable +class SocketIOEventRunnable final : public SocketIORunnable { public: enum SocketEvent { @@ -353,37 +386,9 @@ public: DISCONNECT }; - SocketIOEventRunnable(T* aIO, SocketEvent e) - : SocketIORunnable(aIO) - , mEvent(e) - { } + SocketIOEventRunnable(SocketIOBase* aIO, SocketEvent aEvent); - NS_IMETHOD Run() override - { - MOZ_ASSERT(NS_IsMainThread()); - - T* io = SocketIORunnable::GetIO(); - - if (io->IsShutdownOnMainThread()) { - NS_WARNING("I/O consumer has already been closed!"); - // Since we've already explicitly closed and the close happened before - // this, this isn't really an error. Since we've warned, return OK. - return NS_OK; - } - - SocketBase* base = io->GetSocketBase(); - MOZ_ASSERT(base); - - if (mEvent == CONNECT_SUCCESS) { - base->NotifySuccess(); - } else if (mEvent == CONNECT_ERROR) { - base->NotifyError(); - } else if (mEvent == DISCONNECT) { - base->NotifyDisconnect(); - } - - return NS_OK; - } + NS_IMETHOD Run() override; private: SocketEvent mEvent; @@ -440,23 +445,6 @@ private: nsAutoPtr mInstance; }; -// -// SocketIOBase -// - -/** - * |SocketIOBase| is a base class for Socket I/O classes that - * perform operations on the I/O thread. - */ -class SocketIOBase -{ -public: - virtual ~SocketIOBase(); - -protected: - SocketIOBase(); -}; - // // Socket I/O tasks // diff --git a/ipc/unixsocket/StreamSocket.cpp b/ipc/unixsocket/StreamSocket.cpp index 00b5b335ca7f..c8284f4c6da7 100644 --- a/ipc/unixsocket/StreamSocket.cpp +++ b/ipc/unixsocket/StreamSocket.cpp @@ -44,7 +44,7 @@ public: StreamSocket* GetStreamSocket(); DataSocket* GetDataSocket(); - SocketBase* GetSocketBase(); + SocketBase* GetSocketBase() override; // StreamSocketIOBase // @@ -55,7 +55,7 @@ public: // Shutdown state // - bool IsShutdownOnMainThread() const; + bool IsShutdownOnMainThread() const override; void ShutdownOnMainThread(); bool IsShutdownOnIOThread() const; @@ -303,11 +303,8 @@ StreamSocketIO::Accept(int aFd, mAddrSize = aAddrLen; // Signal success - - nsRefPtr r = - new SocketIOEventRunnable( - this, SocketIOEventRunnable::CONNECT_SUCCESS); - NS_DispatchToMainThread(r); + NS_DispatchToMainThread( + new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS)); return NS_OK; } @@ -377,10 +374,8 @@ StreamSocketIO::OnAccepted(int aFd, } SetSocket(aFd, SOCKET_IS_CONNECTED); - nsRefPtr r = - new SocketIOEventRunnable( - this, SocketIOEventRunnable::CONNECT_SUCCESS); - NS_DispatchToMainThread(r); + NS_DispatchToMainThread( + new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS)); AddWatchers(READ_WATCHER, true); if (HasPendingData()) { @@ -406,10 +401,8 @@ StreamSocketIO::OnConnected() return; } - nsRefPtr r = - new SocketIOEventRunnable( - this, SocketIOEventRunnable::CONNECT_SUCCESS); - NS_DispatchToMainThread(r); + NS_DispatchToMainThread( + new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS)); AddWatchers(READ_WATCHER, true); if (HasPendingData()) { @@ -475,11 +468,8 @@ StreamSocketIO::FireSocketError() Close(); // Tell the main thread we've errored - nsRefPtr r = - new SocketIOEventRunnable( - this, SocketIOEventRunnable::CONNECT_ERROR); - - NS_DispatchToMainThread(r); + NS_DispatchToMainThread( + new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_ERROR)); } bool