From 74910a62cf5f2954cad88e69d35a73f843211722 Mon Sep 17 00:00:00 2001 From: Thomas Zimmermann Date: Thu, 30 Apr 2015 12:55:37 +0200 Subject: [PATCH] Bug 1159709: Cleanup inherited methods of |StreamSocket|, r=kmachulis This patch cleans up the inherited methods of |StreamSocket|. Methods of the same base class are grouped within the file, and each method is labeled with 'override'. --- ipc/unixsocket/StreamSocket.cpp | 230 +++++++++++++++++--------------- ipc/unixsocket/StreamSocket.h | 25 ++-- 2 files changed, 134 insertions(+), 121 deletions(-) diff --git a/ipc/unixsocket/StreamSocket.cpp b/ipc/unixsocket/StreamSocket.cpp index 4267b7945c40..61ae1e46e9b3 100644 --- a/ipc/unixsocket/StreamSocket.cpp +++ b/ipc/unixsocket/StreamSocket.cpp @@ -43,22 +43,6 @@ public: StreamSocket* GetStreamSocket(); DataSocket* GetDataSocket(); - SocketBase* GetSocketBase() override; - - // StreamSocketIOBase - // - - nsresult Accept(int aFd, - const union sockaddr_any* aAddr, socklen_t aAddrLen); - - // Shutdown state - // - - bool IsShutdownOnMainThread() const override; - void ShutdownOnMainThread() override; - - bool IsShutdownOnIOThread() const override; - void ShutdownOnIOThread() override; // Delayed-task handling // @@ -88,12 +72,30 @@ public: void OnSocketCanReceiveWithoutBlocking() override; void OnSocketCanSendWithoutBlocking() override; + // Methods for |ConnectionOrientedSocketIO| + // + + nsresult Accept(int aFd, + const union sockaddr_any* aAddr, + socklen_t aAddrLen) override; + // Methods for |DataSocket| // - nsresult QueryReceiveBuffer(UnixSocketIOBuffer** aBuffer); - void ConsumeBuffer(); - void DiscardBuffer(); + nsresult QueryReceiveBuffer(UnixSocketIOBuffer** aBuffer) override; + void ConsumeBuffer() override; + void DiscardBuffer() override; + + // Methods for |SocketIOBase| + // + + SocketBase* GetSocketBase() override; + + bool IsShutdownOnMainThread() const override; + bool IsShutdownOnIOThread() const override; + + void ShutdownOnMainThread() override; + void ShutdownOnIOThread() override; private: void FireSocketError(); @@ -204,45 +206,6 @@ StreamSocketIO::GetDataSocket() return mStreamSocket.get(); } -SocketBase* -StreamSocketIO::GetSocketBase() -{ - return GetDataSocket(); -} - -bool -StreamSocketIO::IsShutdownOnMainThread() const -{ - MOZ_ASSERT(NS_IsMainThread()); - - return mStreamSocket == nullptr; -} - -void -StreamSocketIO::ShutdownOnMainThread() -{ - MOZ_ASSERT(NS_IsMainThread()); - MOZ_ASSERT(!IsShutdownOnMainThread()); - - mStreamSocket = nullptr; -} - -bool -StreamSocketIO::IsShutdownOnIOThread() const -{ - return mShuttingDownOnIOThread; -} - -void -StreamSocketIO::ShutdownOnIOThread() -{ - MOZ_ASSERT(!NS_IsMainThread()); - MOZ_ASSERT(!mShuttingDownOnIOThread); - - Close(); // will also remove fd from I/O loop - mShuttingDownOnIOThread = true; -} - void StreamSocketIO::SetDelayedConnectTask(CancelableTask* aTask) { @@ -272,42 +235,6 @@ StreamSocketIO::CancelDelayedConnectTask() ClearDelayedConnectTask(); } -nsresult -StreamSocketIO::Accept(int aFd, - const union sockaddr_any* aAddr, socklen_t aAddrLen) -{ - MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); - MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTING); - - // File-descriptor setup - - if (!mConnector->SetUp(aFd)) { - NS_WARNING("Could not set up socket!"); - return NS_ERROR_FAILURE; - } - - if (!SetSocketFlags(aFd)) { - return NS_ERROR_FAILURE; - } - SetSocket(aFd, SOCKET_IS_CONNECTED); - - AddWatchers(READ_WATCHER, true); - if (HasPendingData()) { - AddWatchers(WRITE_WATCHER, false); - } - - // Address setup - - memcpy(&mAddr, aAddr, aAddrLen); - mAddrSize = aAddrLen; - - // Signal success - NS_DispatchToMainThread( - new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS)); - - return NS_OK; -} - void StreamSocketIO::Connect() { @@ -506,6 +433,46 @@ StreamSocketIO::SetSocketFlags(int aFd) return true; } +// |ConnectionOrientedSocketIO| + +nsresult +StreamSocketIO::Accept(int aFd, + const union sockaddr_any* aAddr, socklen_t aAddrLen) +{ + MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop()); + MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTING); + + // File-descriptor setup + + if (!mConnector->SetUp(aFd)) { + NS_WARNING("Could not set up socket!"); + return NS_ERROR_FAILURE; + } + + if (!SetSocketFlags(aFd)) { + return NS_ERROR_FAILURE; + } + SetSocket(aFd, SOCKET_IS_CONNECTED); + + AddWatchers(READ_WATCHER, true); + if (HasPendingData()) { + AddWatchers(WRITE_WATCHER, false); + } + + // Address setup + + memcpy(&mAddr, aAddr, aAddrLen); + mAddrSize = aAddrLen; + + // Signal success + NS_DispatchToMainThread( + new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS)); + + return NS_OK; +} + +// |DataSocketIO| + nsresult StreamSocketIO::QueryReceiveBuffer(UnixSocketIOBuffer** aBuffer) { @@ -568,6 +535,47 @@ StreamSocketIO::DiscardBuffer() // Nothing to do. } +// |SocketIOBase| + +SocketBase* +StreamSocketIO::GetSocketBase() +{ + return GetDataSocket(); +} + +bool +StreamSocketIO::IsShutdownOnMainThread() const +{ + MOZ_ASSERT(NS_IsMainThread()); + + return mStreamSocket == nullptr; +} + +bool +StreamSocketIO::IsShutdownOnIOThread() const +{ + return mShuttingDownOnIOThread; +} + +void +StreamSocketIO::ShutdownOnMainThread() +{ + MOZ_ASSERT(NS_IsMainThread()); + MOZ_ASSERT(!IsShutdownOnMainThread()); + + mStreamSocket = nullptr; +} + +void +StreamSocketIO::ShutdownOnIOThread() +{ + MOZ_ASSERT(!NS_IsMainThread()); + MOZ_ASSERT(!mShuttingDownOnIOThread); + + Close(); // will also remove fd from I/O loop + mShuttingDownOnIOThread = true; +} + // // Socket tasks // @@ -628,18 +636,6 @@ StreamSocket::~StreamSocket() MOZ_ASSERT(!mIO); } -void -StreamSocket::SendSocketData(UnixSocketIOBuffer* aBuffer) -{ - MOZ_ASSERT(NS_IsMainThread()); - MOZ_ASSERT(mIO); - - MOZ_ASSERT(!mIO->IsShutdownOnMainThread()); - XRE_GetIOMessageLoop()->PostTask( - FROM_HERE, - new SocketIOSendTask(mIO, aBuffer)); -} - bool StreamSocket::SendSocketData(const nsACString& aStr) { @@ -734,5 +730,27 @@ StreamSocket::PrepareAccept(UnixSocketConnector* aConnector) return mIO; } +// |DataSocket| + +void +StreamSocket::SendSocketData(UnixSocketIOBuffer* aBuffer) +{ + MOZ_ASSERT(NS_IsMainThread()); + MOZ_ASSERT(mIO); + + MOZ_ASSERT(!mIO->IsShutdownOnMainThread()); + XRE_GetIOMessageLoop()->PostTask( + FROM_HERE, + new SocketIOSendTask(mIO, aBuffer)); +} + +// |SocketBase| + +void +StreamSocket::CloseSocket() +{ + Close(); +} + } // namespace ipc } // namespace mozilla diff --git a/ipc/unixsocket/StreamSocket.h b/ipc/unixsocket/StreamSocket.h index a4c73e11a133..e3dd403a20fe 100644 --- a/ipc/unixsocket/StreamSocket.h +++ b/ipc/unixsocket/StreamSocket.h @@ -28,14 +28,6 @@ public: */ virtual void ReceiveSocketData(nsAutoPtr& aBuffer) = 0; - /** - * Queue data to be sent to the socket on the IO thread. Can only be called on - * originating thread. - * - * @param aBuffer Data to be sent to socket - */ - void SendSocketData(UnixSocketIOBuffer* aBuffer); - /** * Convenience function for sending strings to the socket (common in bluetooth * profile usage). Converts to a UnixSocketRawData struct. Can only be called @@ -74,6 +66,16 @@ public: */ void GetSocketAddr(nsAString& aAddrStr); + // Methods for |DataSocket| + // + + void SendSocketData(UnixSocketIOBuffer* aBuffer) override; + + // Methods for |SocketBase| + // + + void CloseSocket() override; + protected: virtual ~StreamSocket(); @@ -83,13 +85,6 @@ protected: ConnectionOrientedSocketIO* PrepareAccept(UnixSocketConnector* aConnector); private: - - // Legacy interface from |SocketBase|; should be replaced by |Close|. - void CloseSocket() override - { - Close(); - } - StreamSocketIO* mIO; };