Bug 1159209: Remove template parameters from |SocketIOEventRunnable|, r=kmachulis
authorThomas Zimmermann <tdz@users.sourceforge.net>
Wed, 29 Apr 2015 11:19:28 +0200
changeset 273035 27561f4e4910e5888a96791d0540a16d604384b0
parent 273034 4c438af160679af4b1c21be083f0f494e1ca7e6f
child 273036 65cd149078f9db420a8cb36c136524a97645b5a7
push id863
push userraliiev@mozilla.com
push dateMon, 03 Aug 2015 13:22:43 +0000
treeherdermozilla-release@f6321b14228d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmachulis
bugs1159209
milestone40.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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
dom/bluetooth/bluez/BluetoothSocket.cpp
ipc/bluetooth/BluetoothDaemonConnection.cpp
ipc/unixsocket/ListenSocket.cpp
ipc/unixsocket/SocketBase.cpp
ipc/unixsocket/SocketBase.h
ipc/unixsocket/StreamSocket.cpp
--- a/dom/bluetooth/bluedroid/BluetoothSocket.cpp
+++ b/dom/bluetooth/bluedroid/BluetoothSocket.cpp
@@ -86,23 +86,23 @@ public:
   }
 
   void Send(UnixSocketIOBuffer* aBuffer)
   {
     EnqueueData(aBuffer);
     AddWatchers(WRITE_WATCHER, false);
   }
 
-  bool IsShutdownOnMainThread()
+  bool IsShutdownOnMainThread() const override
   {
     MOZ_ASSERT(NS_IsMainThread());
     return mConsumer == nullptr;
   }
 
-  bool IsShutdownOnIOThread()
+  bool IsShutdownOnIOThread() const
   {
     return mShuttingDownOnIOThread;
   }
 
   void ShutdownOnMainThread()
   {
     MOZ_ASSERT(NS_IsMainThread());
     MOZ_ASSERT(!IsShutdownOnMainThread());
@@ -139,17 +139,17 @@ public:
     return mConsumer.get();
   }
 
   DataSocket* GetDataSocket()
   {
     return GetBluetoothSocket();
   }
 
-  SocketBase* GetSocketBase()
+  SocketBase* GetSocketBase() override
   {
     return GetDataSocket();
   }
 
   // Methods for |DataSocket|
   //
 
   nsresult QueryReceiveBuffer(UnixSocketIOBuffer** aBuffer);
@@ -305,20 +305,18 @@ DroidSocketImpl::Accept(int aFd)
   if (!(flags & O_NONBLOCK)) {
     int res = TEMP_FAILURE_RETRY(fcntl(aFd, F_SETFL, flags | O_NONBLOCK));
     NS_ENSURE_TRUE_VOID(!res);
   }
 
   SetFd(aFd);
   mConnectionStatus = SOCKET_IS_CONNECTED;
 
-  nsRefPtr<nsRunnable> r =
-    new SocketIOEventRunnable<DroidSocketImpl>(
-      this, SocketIOEventRunnable<DroidSocketImpl>::CONNECT_SUCCESS);
-  NS_DispatchToMainThread(r);
+  NS_DispatchToMainThread(
+    new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));
 
   AddWatchers(READ_WATCHER, true);
   if (HasPendingData()) {
     AddWatchers(WRITE_WATCHER, false);
   }
 }
 
 void
@@ -494,20 +492,18 @@ DroidSocketImpl::OnSocketCanConnectWitho
   MOZ_ASSERT(!mShuttingDownOnIOThread);
 
   /* We follow Posix behaviour here: Connect operations are
    * complete once we can write to the connecting socket.
    */
 
   mConnectionStatus = SOCKET_IS_CONNECTED;
 
-  nsRefPtr<nsRunnable> r =
-    new SocketIOEventRunnable<DroidSocketImpl>(
-      this, SocketIOEventRunnable<DroidSocketImpl>::CONNECT_SUCCESS);
-  NS_DispatchToMainThread(r);
+  NS_DispatchToMainThread(
+    new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));
 
   AddWatchers(READ_WATCHER, true);
   if (HasPendingData()) {
     AddWatchers(WRITE_WATCHER, false);
   }
 }
 
 nsresult
--- a/dom/bluetooth/bluez/BluetoothSocket.cpp
+++ b/dom/bluetooth/bluez/BluetoothSocket.cpp
@@ -33,22 +33,22 @@ public:
                     UnixSocketConnector* aConnector,
                     const nsACString& aAddress);
   ~BluetoothSocketIO();
 
   void        GetSocketAddr(nsAString& aAddrStr) const;
 
   BluetoothSocket* GetBluetoothSocket();
   DataSocket* GetDataSocket();
-  SocketBase* GetSocketBase();
+  SocketBase* GetSocketBase() override;
 
   // Shutdown state
   //
 
-  bool IsShutdownOnMainThread() const;
+  bool IsShutdownOnMainThread() const override;
   void ShutdownOnMainThread();
 
   bool IsShutdownOnIOThread() const;
   void ShutdownOnIOThread();
 
   // Delayed-task handling
   //
 
@@ -347,20 +347,18 @@ BluetoothSocket::BluetoothSocketIO::OnAc
 
   RemoveWatchers(READ_WATCHER|WRITE_WATCHER);
   Close();
   if (!SetSocketFlags(aFd)) {
     return;
   }
   SetSocket(aFd, SOCKET_IS_CONNECTED);
 
-  nsRefPtr<nsRunnable> r =
-    new SocketIOEventRunnable<BluetoothSocketIO>(
-      this, SocketIOEventRunnable<BluetoothSocketIO>::CONNECT_SUCCESS);
-  NS_DispatchToMainThread(r);
+  NS_DispatchToMainThread(
+    new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));
 
   AddWatchers(READ_WATCHER, true);
   if (HasPendingData()) {
     AddWatchers(WRITE_WATCHER, false);
   }
 }
 
 void
@@ -376,20 +374,18 @@ BluetoothSocket::BluetoothSocketIO::OnCo
   }
 
   if (!mConnector->SetUp(GetFd())) {
     NS_WARNING("Could not set up socket!");
     FireSocketError();
     return;
   }
 
-  nsRefPtr<nsRunnable> r =
-    new SocketIOEventRunnable<BluetoothSocketIO>(
-      this, SocketIOEventRunnable<BluetoothSocketIO>::CONNECT_SUCCESS);
-  NS_DispatchToMainThread(r);
+  NS_DispatchToMainThread(
+    new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));
 
   AddWatchers(READ_WATCHER, true);
   if (HasPendingData()) {
     AddWatchers(WRITE_WATCHER, false);
   }
 }
 
 void
@@ -452,21 +448,19 @@ void
 BluetoothSocket::BluetoothSocketIO::FireSocketError()
 {
   MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
 
   // Clean up watchers, statuses, fds
   Close();
 
   // Tell the main thread we've errored
-  nsRefPtr<nsRunnable> r =
-    new SocketIOEventRunnable<BluetoothSocketIO>(
-      this, SocketIOEventRunnable<BluetoothSocketIO>::CONNECT_ERROR);
+  NS_DispatchToMainThread(
+    new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_ERROR));
 
-  NS_DispatchToMainThread(r);
 }
 
 bool
 BluetoothSocket::BluetoothSocketIO::SetSocketFlags(int aFd)
 {
   // Set socket addr to be reused even if kernel is still waiting to close
   int n = 1;
   if (setsockopt(aFd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) < 0) {
--- a/ipc/bluetooth/BluetoothDaemonConnection.cpp
+++ b/ipc/bluetooth/BluetoothDaemonConnection.cpp
@@ -187,16 +187,17 @@ BluetoothDaemonPDUConsumer::BluetoothDae
 BluetoothDaemonPDUConsumer::~BluetoothDaemonPDUConsumer()
 { }
 
 //
 // BluetoothDaemonConnectionIO
 //
 
 class BluetoothDaemonConnectionIO final : public UnixSocketWatcher
+                                        , public SocketIOBase
                                         , public ConnectionOrientedSocketIO
 {
 public:
   BluetoothDaemonConnectionIO(MessageLoop* aIOLoop, int aFd,
                               ConnectionStatus aConnectionStatus,
                               BluetoothDaemonConnection* aConnection,
                               BluetoothDaemonPDUConsumer* aConsumer);
 
@@ -473,21 +474,18 @@ BluetoothDaemonConnectionIO::OnSocketCan
 }
 
 void
 BluetoothDaemonConnectionIO::OnConnected()
 {
   MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
   MOZ_ASSERT(GetConnectionStatus() == SOCKET_IS_CONNECTED);
 
-  nsRefPtr<nsRunnable> r =
-    new SocketIOEventRunnable<BluetoothDaemonConnectionIO>(
-      this,
-      SocketIOEventRunnable<BluetoothDaemonConnectionIO>::CONNECT_SUCCESS);
-  NS_DispatchToMainThread(r);
+  NS_DispatchToMainThread(
+    new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));
 
   AddWatchers(READ_WATCHER, true);
   if (HasPendingData()) {
     AddWatchers(WRITE_WATCHER, false);
   }
 }
 
 void
@@ -496,21 +494,18 @@ BluetoothDaemonConnectionIO::OnError(con
   MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
 
   UnixFdWatcher::OnError(aFunction, aErrno);
 
   // Clean up watchers, status, fd
   Close();
 
   // Tell the main thread we've errored
-  nsRefPtr<nsRunnable> r =
-    new SocketIOEventRunnable<BluetoothDaemonConnectionIO>(
-      this,
-      SocketIOEventRunnable<BluetoothDaemonConnectionIO>::CONNECT_ERROR);
-  NS_DispatchToMainThread(r);
+  NS_DispatchToMainThread(
+    new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_ERROR));
 }
 
 //
 // I/O helper tasks
 //
 
 class BluetoothDaemonConnectTask final
   : public SocketIOTask<BluetoothDaemonConnectionIO>
--- a/ipc/unixsocket/ListenSocket.cpp
+++ b/ipc/unixsocket/ListenSocket.cpp
@@ -28,22 +28,22 @@ public:
   ListenSocketIO(MessageLoop* mIOLoop,
                  ListenSocket* aListenSocket,
                  UnixSocketConnector* aConnector,
                  const nsACString& aAddress);
   ~ListenSocketIO();
 
   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;
   void ShutdownOnIOThread();
 
   // Task callback methods
   //
 
@@ -253,20 +253,18 @@ ListenSocketIO::OnListening()
     NS_WARNING("Could not set up listen socket!");
     FireSocketError();
     return;
   }
 
   AddWatchers(READ_WATCHER, true);
 
   /* We signal a successful 'connection' to a local address for listening. */
-  nsRefPtr<nsRunnable> runnable =
-      new SocketIOEventRunnable<ListenSocketIO>(
-        this, SocketIOEventRunnable<ListenSocketIO>::CONNECT_SUCCESS);
-  NS_DispatchToMainThread(runnable);
+  NS_DispatchToMainThread(
+    new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));
 }
 
 void
 ListenSocketIO::OnError(const char* aFunction, int aErrno)
 {
   MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
 
   UnixFdWatcher::OnError(aFunction, aErrno);
@@ -277,21 +275,18 @@ void
 ListenSocketIO::FireSocketError()
 {
   MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
 
   // Clean up watchers, statuses, fds
   Close();
 
   // Tell the main thread we've errored
-  nsRefPtr<nsRunnable> r =
-    new SocketIOEventRunnable<ListenSocketIO>(
-      this, SocketIOEventRunnable<ListenSocketIO>::CONNECT_ERROR);
-
-  NS_DispatchToMainThread(r);
+  NS_DispatchToMainThread(
+    new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_ERROR));
 }
 
 bool
 ListenSocketIO::SetSocketFlags(int aFd)
 {
   static const int reuseaddr = 1;
 
   // Set socket addr to be reused even if kernel is still waiting to close
--- a/ipc/unixsocket/SocketBase.cpp
+++ b/ipc/unixsocket/SocketBase.cpp
@@ -273,10 +273,47 @@ SocketBase::SetConnectionStatus(SocketCo
 //
 
 SocketIOBase::SocketIOBase()
 { }
 
 SocketIOBase::~SocketIOBase()
 { }
 
+//
+// SocketIOEventRunnable
+//
+
+SocketIOEventRunnable::SocketIOEventRunnable(SocketIOBase* aIO,
+                                             SocketEvent aEvent)
+  : SocketIORunnable<SocketIOBase>(aIO)
+  , mEvent(aEvent)
+{ }
+
+NS_METHOD
+SocketIOEventRunnable::Run()
+{
+  MOZ_ASSERT(NS_IsMainThread());
+
+  SocketIOBase* io = SocketIORunnable<SocketIOBase>::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;
+}
+
 }
 }
--- a/ipc/unixsocket/SocketBase.h
+++ b/ipc/unixsocket/SocketBase.h
@@ -306,16 +306,49 @@ private:
   uint32_t CalculateConnectDelayMs() const;
 
   SocketConnectionStatus mConnectionStatus;
   PRIntervalTime mConnectTimestamp;
   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
 //
 
 /* |SocketIORunnable| is a runnable for sending a message from
  * the I/O thread to the main thread.
  */
 template <typename T>
 class SocketIORunnable : public nsRunnable
@@ -335,60 +368,32 @@ protected:
   {
     MOZ_ASSERT(aIO);
   }
 
 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 <typename T>
-class SocketIOEventRunnable final : public SocketIORunnable<T>
+class SocketIOEventRunnable final : public SocketIORunnable<SocketIOBase>
 {
 public:
   enum SocketEvent {
     CONNECT_SUCCESS,
     CONNECT_ERROR,
     DISCONNECT
   };
 
-  SocketIOEventRunnable(T* aIO, SocketEvent e)
-  : SocketIORunnable<T>(aIO)
-  , mEvent(e)
-  { }
-
-  NS_IMETHOD Run() override
-  {
-    MOZ_ASSERT(NS_IsMainThread());
-
-    T* io = SocketIORunnable<T>::GetIO();
+  SocketIOEventRunnable(SocketIOBase* aIO, SocketEvent aEvent);
 
-    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;
 };
 
 template <typename T>
 class SocketIORequestClosingRunnable final : public SocketIORunnable<T>
 {
@@ -436,33 +441,16 @@ public:
     return NS_OK;
   }
 
 private:
   nsAutoPtr<T> 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
 //
 
 /* |SocketIOTask| holds a reference to a Socket I/O object. It's
  * supposed to run on the I/O thread.
  */
 template<typename Tio>
 class SocketIOTask : public CancelableTask
--- a/ipc/unixsocket/StreamSocket.cpp
+++ b/ipc/unixsocket/StreamSocket.cpp
@@ -39,28 +39,28 @@ public:
                  UnixSocketConnector* aConnector,
                  const nsACString& aAddress);
   ~StreamSocketIO();
 
   void GetSocketAddr(nsAString& aAddrStr) const;
 
   StreamSocket* GetStreamSocket();
   DataSocket* GetDataSocket();
-  SocketBase* GetSocketBase();
+  SocketBase* GetSocketBase() override;
 
   // StreamSocketIOBase
   //
 
   nsresult Accept(int aFd,
                   const union sockaddr_any* aAddr, socklen_t aAddrLen);
 
   // Shutdown state
   //
 
-  bool IsShutdownOnMainThread() const;
+  bool IsShutdownOnMainThread() const override;
   void ShutdownOnMainThread();
 
   bool IsShutdownOnIOThread() const;
   void ShutdownOnIOThread();
 
   // Delayed-task handling
   //
 
@@ -298,21 +298,18 @@ StreamSocketIO::Accept(int aFd,
   }
 
   // Address setup
 
   memcpy(&mAddr, aAddr, aAddrLen);
   mAddrSize = aAddrLen;
 
   // Signal success
-
-  nsRefPtr<nsRunnable> r =
-    new SocketIOEventRunnable<StreamSocketIO>(
-      this, SocketIOEventRunnable<StreamSocketIO>::CONNECT_SUCCESS);
-  NS_DispatchToMainThread(r);
+  NS_DispatchToMainThread(
+    new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));
 
   return NS_OK;
 }
 
 void
 StreamSocketIO::Connect()
 {
   MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
@@ -372,20 +369,18 @@ StreamSocketIO::OnAccepted(int aFd,
 
   RemoveWatchers(READ_WATCHER|WRITE_WATCHER);
   Close();
   if (!SetSocketFlags(aFd)) {
     return;
   }
   SetSocket(aFd, SOCKET_IS_CONNECTED);
 
-  nsRefPtr<nsRunnable> r =
-    new SocketIOEventRunnable<StreamSocketIO>(
-      this, SocketIOEventRunnable<StreamSocketIO>::CONNECT_SUCCESS);
-  NS_DispatchToMainThread(r);
+  NS_DispatchToMainThread(
+    new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));
 
   AddWatchers(READ_WATCHER, true);
   if (HasPendingData()) {
     AddWatchers(WRITE_WATCHER, false);
   }
 }
 
 void
@@ -401,20 +396,18 @@ StreamSocketIO::OnConnected()
   }
 
   if (!mConnector->SetUp(GetFd())) {
     NS_WARNING("Could not set up socket!");
     FireSocketError();
     return;
   }
 
-  nsRefPtr<nsRunnable> r =
-    new SocketIOEventRunnable<StreamSocketIO>(
-      this, SocketIOEventRunnable<StreamSocketIO>::CONNECT_SUCCESS);
-  NS_DispatchToMainThread(r);
+  NS_DispatchToMainThread(
+    new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_SUCCESS));
 
   AddWatchers(READ_WATCHER, true);
   if (HasPendingData()) {
     AddWatchers(WRITE_WATCHER, false);
   }
 }
 
 void
@@ -470,21 +463,18 @@ void
 StreamSocketIO::FireSocketError()
 {
   MOZ_ASSERT(MessageLoopForIO::current() == GetIOLoop());
 
   // Clean up watchers, statuses, fds
   Close();
 
   // Tell the main thread we've errored
-  nsRefPtr<nsRunnable> r =
-    new SocketIOEventRunnable<StreamSocketIO>(
-      this, SocketIOEventRunnable<StreamSocketIO>::CONNECT_ERROR);
-
-  NS_DispatchToMainThread(r);
+  NS_DispatchToMainThread(
+    new SocketIOEventRunnable(this, SocketIOEventRunnable::CONNECT_ERROR));
 }
 
 bool
 StreamSocketIO::SetSocketFlags(int aFd)
 {
   static const int reuseaddr = 1;
 
   // Set socket addr to be reused even if kernel is still waiting to close