Bug 968868: Move DBusWatcher to RawDBusConnection, r=qdot
authorThomas Zimmermann <tdz@users.sourceforge.net>
Fri, 07 Feb 2014 12:01:27 +0100
changeset 167519 dbd02a1aae202724c0417a4d0f8c8008f4487b3e
parent 167518 793f060868f1aaf88ca0afea3967a9905bb72aba
child 167520 c511e66336864ea9829d2bb781c642eb63554e84
push id4936
push userryanvm@gmail.com
push dateFri, 07 Feb 2014 19:43:49 +0000
treeherderfx-team@949b41f324da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersqdot
bugs968868
milestone30.0a1
Bug 968868: Move DBusWatcher to RawDBusConnection, r=qdot DBusWatcher is a helper for RawDBusConnection. This patch moves both classes into the same file. Calling RawDBusConnection::Watch installs a DBusWatcher for the connection.
ipc/dbus/DBusThread.cpp
ipc/dbus/RawDBusConnection.cpp
ipc/dbus/RawDBusConnection.h
--- a/ipc/dbus/DBusThread.cpp
+++ b/ipc/dbus/DBusThread.cpp
@@ -18,234 +18,35 @@
  * NOTE: Due to being based on the dbus compatibility layer for
  * android's bluetooth implementation, this file is licensed under the
  * apache license instead of MPL.
  *
  */
 
 #include "DBusThread.h"
 #include "RawDBusConnection.h"
-#include "DBusUtils.h"
-
-#include <dbus/dbus.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <sys/select.h>
-#include <sys/types.h>
-
-#include <string.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <string>
-#include <stdlib.h>
-#include <errno.h>
-#include <unistd.h>
-#include <poll.h>
-
-#include <list>
-
-#include "base/eintr_wrapper.h"
 #include "base/message_loop.h"
-#include "nsTArray.h"
-#include "nsDataHashtable.h"
-#include "mozilla/SyncRunnable.h"
-#include "mozilla/NullPtr.h"
-#include "mozilla/StaticPtr.h"
-#include "mozilla/Monitor.h"
-#include "mozilla/FileUtils.h"
 #include "nsThreadUtils.h"
-#include "nsIThread.h"
 #include "nsXULAppAPI.h"
-#include "nsServiceManagerUtils.h"
-#include "nsCOMPtr.h"
-
-#undef CHROMIUM_LOG
-#if defined(MOZ_WIDGET_GONK)
-#include <android/log.h>
-#define CHROMIUM_LOG(args...)  __android_log_print(ANDROID_LOG_INFO, "GonkDBus", args);
-#else
-#define BTDEBUG true
-#define CHROMIUM_LOG(args...) if (BTDEBUG) printf(args);
-#endif
 
 namespace mozilla {
 namespace ipc {
 
-class DBusWatcher : public MessageLoopForIO::Watcher
-{
-public:
-  DBusWatcher(RawDBusConnection* aConnection, DBusWatch* aWatch)
-  : mConnection(aConnection),
-    mWatch(aWatch)
-  {
-    MOZ_ASSERT(mConnection);
-    MOZ_ASSERT(mWatch);
-  }
-
-  ~DBusWatcher()
-  { }
-
-  void StartWatching();
-  void StopWatching();
-
-  static void        FreeFunction(void* aData);
-  static dbus_bool_t AddWatchFunction(DBusWatch* aWatch, void* aData);
-  static void        RemoveWatchFunction(DBusWatch* aWatch, void* aData);
-  static void        ToggleWatchFunction(DBusWatch* aWatch, void* aData);
-
-  RawDBusConnection* GetConnection();
-
-private:
-  void OnFileCanReadWithoutBlocking(int aFd);
-  void OnFileCanWriteWithoutBlocking(int aFd);
-
-  // Read watcher for libevent. Only to be accessed on IO Thread.
-  MessageLoopForIO::FileDescriptorWatcher mReadWatcher;
-
-  // Write watcher for libevent. Only to be accessed on IO Thread.
-  MessageLoopForIO::FileDescriptorWatcher mWriteWatcher;
-
-  // DBus structures
-  RawDBusConnection* mConnection;
-  DBusWatch* mWatch;
-};
-
-RawDBusConnection*
-DBusWatcher::GetConnection()
-{
-  return mConnection;
-}
-
-void DBusWatcher::StartWatching()
-{
-  MOZ_ASSERT(!NS_IsMainThread());
-  MOZ_ASSERT(mWatch);
-
-  int fd = dbus_watch_get_unix_fd(mWatch);
-
-  MessageLoopForIO* ioLoop = MessageLoopForIO::current();
-
-  unsigned int flags = dbus_watch_get_flags(mWatch);
-
-  if (flags & DBUS_WATCH_READABLE) {
-    ioLoop->WatchFileDescriptor(fd, true, MessageLoopForIO::WATCH_READ,
-                                &mReadWatcher, this);
-  }
-  if (flags & DBUS_WATCH_WRITABLE) {
-    ioLoop->WatchFileDescriptor(fd, true, MessageLoopForIO::WATCH_WRITE,
-                                &mWriteWatcher, this);
-  }
-}
-
-void DBusWatcher::StopWatching()
-{
-  MOZ_ASSERT(!NS_IsMainThread());
-
-  unsigned int flags = dbus_watch_get_flags(mWatch);
-
-  if (flags & DBUS_WATCH_READABLE) {
-    mReadWatcher.StopWatchingFileDescriptor();
-  }
-  if (flags & DBUS_WATCH_WRITABLE) {
-    mWriteWatcher.StopWatchingFileDescriptor();
-  }
-}
-
-// DBus utility functions, used as function pointers in DBus setup
-
-void
-DBusWatcher::FreeFunction(void* aData)
-{
-  delete static_cast<DBusWatcher*>(aData);
-}
-
-dbus_bool_t
-DBusWatcher::AddWatchFunction(DBusWatch* aWatch, void* aData)
-{
-  MOZ_ASSERT(!NS_IsMainThread());
-
-  RawDBusConnection* connection = static_cast<RawDBusConnection*>(aData);
-
-  DBusWatcher* dbusWatcher = new DBusWatcher(connection, aWatch);
-  dbus_watch_set_data(aWatch, dbusWatcher, DBusWatcher::FreeFunction);
-
-  if (dbus_watch_get_enabled(aWatch)) {
-    dbusWatcher->StartWatching();
-  }
-
-  return TRUE;
-}
-
-void
-DBusWatcher::RemoveWatchFunction(DBusWatch* aWatch, void* aData)
-{
-  MOZ_ASSERT(!NS_IsMainThread());
-
-  DBusWatcher* dbusWatcher =
-    static_cast<DBusWatcher*>(dbus_watch_get_data(aWatch));
-  dbusWatcher->StopWatching();
-}
-
-void
-DBusWatcher::ToggleWatchFunction(DBusWatch* aWatch, void* aData)
-{
-  MOZ_ASSERT(!NS_IsMainThread());
-
-  DBusWatcher* dbusWatcher =
-    static_cast<DBusWatcher*>(dbus_watch_get_data(aWatch));
-
-  if (dbus_watch_get_enabled(aWatch)) {
-    dbusWatcher->StartWatching();
-  } else {
-    dbusWatcher->StopWatching();
-  }
-}
-
-void
-DBusWatcher::OnFileCanReadWithoutBlocking(int aFd)
-{
-  MOZ_ASSERT(!NS_IsMainThread());
-
-  dbus_watch_handle(mWatch, DBUS_WATCH_READABLE);
-
-  DBusDispatchStatus dbusDispatchStatus;
-  do {
-    dbusDispatchStatus =
-      dbus_connection_dispatch(mConnection->GetConnection());
-  } while (dbusDispatchStatus == DBUS_DISPATCH_DATA_REMAINS);
-}
-
-void
-DBusWatcher::OnFileCanWriteWithoutBlocking(int aFd)
-{
-  MOZ_ASSERT(!NS_IsMainThread());
-
-  dbus_watch_handle(mWatch, DBUS_WATCH_WRITABLE);
-}
-
 class WatchDBusConnectionTask : public Task
 {
 public:
   WatchDBusConnectionTask(RawDBusConnection* aConnection)
   : mConnection(aConnection)
   {
     MOZ_ASSERT(mConnection);
   }
 
   void Run()
   {
-    MOZ_ASSERT(!NS_IsMainThread());
-
-    dbus_bool_t success =
-      dbus_connection_set_watch_functions(mConnection->GetConnection(),
-                                          DBusWatcher::AddWatchFunction,
-                                          DBusWatcher::RemoveWatchFunction,
-                                          DBusWatcher::ToggleWatchFunction,
-                                          mConnection, nullptr);
-    NS_ENSURE_TRUE_VOID(success == TRUE);
+    mConnection->Watch();
   }
 
 private:
   RawDBusConnection* mConnection;
 };
 
 class DeleteDBusConnectionTask : public Task
 {
--- a/ipc/dbus/RawDBusConnection.cpp
+++ b/ipc/dbus/RawDBusConnection.cpp
@@ -18,22 +18,180 @@
 #define CHROMIUM_LOG(args...)  __android_log_print(ANDROID_LOG_INFO, "Gonk", args);
 #else
 #define CHROMIUM_LOG(args...)  printf(args);
 #endif
 
 /* TODO: Remove BlueZ constant */
 #define BLUEZ_DBUS_BASE_IFC "org.bluez"
 
+namespace mozilla {
+namespace ipc {
+
 //
-// Runnables
+// DBusWatcher
 //
 
-namespace mozilla {
-namespace ipc {
+class DBusWatcher : public MessageLoopForIO::Watcher
+{
+public:
+  DBusWatcher(RawDBusConnection* aConnection, DBusWatch* aWatch)
+  : mConnection(aConnection),
+    mWatch(aWatch)
+  {
+    MOZ_ASSERT(mConnection);
+    MOZ_ASSERT(mWatch);
+  }
+
+  ~DBusWatcher()
+  { }
+
+  void StartWatching();
+  void StopWatching();
+
+  static void        FreeFunction(void* aData);
+  static dbus_bool_t AddWatchFunction(DBusWatch* aWatch, void* aData);
+  static void        RemoveWatchFunction(DBusWatch* aWatch, void* aData);
+  static void        ToggleWatchFunction(DBusWatch* aWatch, void* aData);
+
+  RawDBusConnection* GetConnection();
+
+private:
+  void OnFileCanReadWithoutBlocking(int aFd);
+  void OnFileCanWriteWithoutBlocking(int aFd);
+
+  // Read watcher for libevent. Only to be accessed on IO Thread.
+  MessageLoopForIO::FileDescriptorWatcher mReadWatcher;
+
+  // Write watcher for libevent. Only to be accessed on IO Thread.
+  MessageLoopForIO::FileDescriptorWatcher mWriteWatcher;
+
+  // DBus structures
+  RawDBusConnection* mConnection;
+  DBusWatch* mWatch;
+};
+
+RawDBusConnection*
+DBusWatcher::GetConnection()
+{
+  return mConnection;
+}
+
+void DBusWatcher::StartWatching()
+{
+  MOZ_ASSERT(!NS_IsMainThread());
+  MOZ_ASSERT(mWatch);
+
+  int fd = dbus_watch_get_unix_fd(mWatch);
+
+  MessageLoopForIO* ioLoop = MessageLoopForIO::current();
+
+  unsigned int flags = dbus_watch_get_flags(mWatch);
+
+  if (flags & DBUS_WATCH_READABLE) {
+    ioLoop->WatchFileDescriptor(fd, true, MessageLoopForIO::WATCH_READ,
+                                &mReadWatcher, this);
+  }
+  if (flags & DBUS_WATCH_WRITABLE) {
+    ioLoop->WatchFileDescriptor(fd, true, MessageLoopForIO::WATCH_WRITE,
+                                &mWriteWatcher, this);
+  }
+}
+
+void DBusWatcher::StopWatching()
+{
+  MOZ_ASSERT(!NS_IsMainThread());
+
+  unsigned int flags = dbus_watch_get_flags(mWatch);
+
+  if (flags & DBUS_WATCH_READABLE) {
+    mReadWatcher.StopWatchingFileDescriptor();
+  }
+  if (flags & DBUS_WATCH_WRITABLE) {
+    mWriteWatcher.StopWatchingFileDescriptor();
+  }
+}
+
+// DBus utility functions, used as function pointers in DBus setup
+
+void
+DBusWatcher::FreeFunction(void* aData)
+{
+  delete static_cast<DBusWatcher*>(aData);
+}
+
+dbus_bool_t
+DBusWatcher::AddWatchFunction(DBusWatch* aWatch, void* aData)
+{
+  MOZ_ASSERT(!NS_IsMainThread());
+
+  RawDBusConnection* connection = static_cast<RawDBusConnection*>(aData);
+
+  DBusWatcher* dbusWatcher = new DBusWatcher(connection, aWatch);
+  dbus_watch_set_data(aWatch, dbusWatcher, DBusWatcher::FreeFunction);
+
+  if (dbus_watch_get_enabled(aWatch)) {
+    dbusWatcher->StartWatching();
+  }
+
+  return TRUE;
+}
+
+void
+DBusWatcher::RemoveWatchFunction(DBusWatch* aWatch, void* aData)
+{
+  MOZ_ASSERT(!NS_IsMainThread());
+
+  DBusWatcher* dbusWatcher =
+    static_cast<DBusWatcher*>(dbus_watch_get_data(aWatch));
+  dbusWatcher->StopWatching();
+}
+
+void
+DBusWatcher::ToggleWatchFunction(DBusWatch* aWatch, void* aData)
+{
+  MOZ_ASSERT(!NS_IsMainThread());
+
+  DBusWatcher* dbusWatcher =
+    static_cast<DBusWatcher*>(dbus_watch_get_data(aWatch));
+
+  if (dbus_watch_get_enabled(aWatch)) {
+    dbusWatcher->StartWatching();
+  } else {
+    dbusWatcher->StopWatching();
+  }
+}
+
+// I/O-loop callbacks
+
+void
+DBusWatcher::OnFileCanReadWithoutBlocking(int aFd)
+{
+  MOZ_ASSERT(!NS_IsMainThread());
+
+  dbus_watch_handle(mWatch, DBUS_WATCH_READABLE);
+
+  DBusDispatchStatus dbusDispatchStatus;
+  do {
+    dbusDispatchStatus =
+      dbus_connection_dispatch(mConnection->GetConnection());
+  } while (dbusDispatchStatus == DBUS_DISPATCH_DATA_REMAINS);
+}
+
+void
+DBusWatcher::OnFileCanWriteWithoutBlocking(int aFd)
+{
+  MOZ_ASSERT(!NS_IsMainThread());
+
+  dbus_watch_handle(mWatch, DBUS_WATCH_WRITABLE);
+}
+
+//
+// Notification
+//
 
 class Notification
 {
 public:
   Notification(DBusReplyCallback aCallback, void* aData)
   : mCallback(aCallback),
     mData(aData)
   { }
@@ -98,16 +256,32 @@ nsresult RawDBusConnection::EstablishDBu
   if (dbus_error_is_set(&err)) {
     dbus_error_free(&err);
     return NS_ERROR_FAILURE;
   }
   dbus_connection_set_exit_on_disconnect(mConnection, FALSE);
   return NS_OK;
 }
 
+bool RawDBusConnection::Watch()
+{
+  MOZ_ASSERT(!NS_IsMainThread());
+  MOZ_ASSERT(MessageLoop::current());
+
+  dbus_bool_t success =
+    dbus_connection_set_watch_functions(mConnection,
+                                        DBusWatcher::AddWatchFunction,
+                                        DBusWatcher::RemoveWatchFunction,
+                                        DBusWatcher::ToggleWatchFunction,
+                                        this, nullptr);
+  NS_ENSURE_TRUE(success == TRUE, false);
+
+  return true;
+}
+
 void RawDBusConnection::ScopedDBusConnectionPtrTraits::release(DBusConnection* ptr)
 {
   if (ptr) {
     dbus_connection_close(ptr);
     dbus_connection_unref(ptr);
   }
 }
 
--- a/ipc/dbus/RawDBusConnection.h
+++ b/ipc/dbus/RawDBusConnection.h
@@ -26,16 +26,18 @@ class RawDBusConnection
   };
 
 public:
   RawDBusConnection();
   virtual ~RawDBusConnection();
 
   nsresult EstablishDBusConnection();
 
+  bool Watch();
+
   DBusConnection* GetConnection()
   {
     return mConnection;
   }
 
   bool Send(DBusMessage* aMessage);
 
   bool SendWithReply(DBusReplyCallback aCallback, void* aData,