Bug 759883 - [b2g-bluetooth] Divide out bluetooth specifics from DBusThread object; r=echou
authorKyle Machulis <kyle@nonpolynomial.com>
Tue, 07 Aug 2012 21:16:03 -0700
changeset 104891 9215610e05a1123175ea49dab834ba7d8e9ffd3f
parent 104890 8f7045786fbf27ace25c28e653313340420f43e2
child 104892 9e64393fa8c0f8284e08aebd5d89da2be35c7984
push idunknown
push userunknown
push dateunknown
reviewersechou
bugs759883
milestone17.0a1
Bug 759883 - [b2g-bluetooth] Divide out bluetooth specifics from DBusThread object; r=echou
dom/bluetooth/linux/BluetoothDBusService.cpp
ipc/dbus/DBusThread.cpp
--- a/dom/bluetooth/linux/BluetoothDBusService.cpp
+++ b/dom/bluetooth/linux/BluetoothDBusService.cpp
@@ -333,17 +333,19 @@ GetProperty(DBusMessageIter aIter, Prope
           s = NS_ConvertUTF8toUTF16(tmp);
           arr.AppendElement(s);
         } while (dbus_message_iter_next(&array_val_iter));
         propertyValue = arr;
       } else {
         // This happens when the array is 0-length. Apparently we get a
         // DBUS_TYPE_INVALID type.
         propertyValue = InfallibleTArray<nsString>();
+#ifdef DEBUG
         NS_WARNING("Received array type that's not a string array!");
+#endif
       }
       break;
     default:
       NS_NOTREACHED("Cannot find dbus message type!");
   }
   aProperties.AppendElement(BluetoothNamedValue(propertyName, propertyValue));
   return true;
 }
@@ -600,25 +602,41 @@ BluetoothDBusService::StartInternal()
   MOZ_ASSERT(!NS_IsMainThread());
   
   if (!StartDBus()) {
     NS_WARNING("Cannot start DBus thread!");
     return NS_ERROR_FAILURE;
   }
   
   if (mConnection) {
-    return NS_OK;    
+    return NS_OK;
   }
 
   if (NS_FAILED(EstablishDBusConnection())) {
     NS_WARNING("Cannot start DBus connection!");
     StopDBus();
     return NS_ERROR_FAILURE;
   }
-	
+
+  DBusError err;
+  dbus_error_init(&err);
+
+  // Set which messages will be processed by this dbus connection.
+  // Since we are maintaining a single thread for all the DBus bluez
+  // signals we want, register all of them in this thread at startup.
+  // The event handler will sort the destinations out as needed.
+  for (uint32_t i = 0; i < ArrayLength(sBluetoothDBusSignals); ++i) {
+    dbus_bus_add_match(mConnection,
+                       sBluetoothDBusSignals[i],
+                       &err);
+    if (dbus_error_is_set(&err)) {
+      LOG_AND_FREE_DBUS_ERROR(&err);
+    }
+  }
+
   // Add a filter for all incoming messages_base
   if (!dbus_connection_add_filter(mConnection, EventFilter,
                                   NULL, NULL)) {
     NS_WARNING("Cannot create DBus Event Filter for DBus Thread!");
     return NS_ERROR_FAILURE;
   }
 
   return NS_OK;
@@ -629,17 +647,30 @@ BluetoothDBusService::StopInternal()
 {
   // This could block. It should never be run on the main thread.
   MOZ_ASSERT(!NS_IsMainThread());
   
   if (!mConnection) {
     StopDBus();
     return NS_OK;
   }
-  dbus_connection_remove_filter(mConnection, EventFilter, NULL);
+
+  DBusError err;
+  dbus_error_init(&err);
+  for (uint32_t i = 0; i < ArrayLength(sBluetoothDBusSignals); ++i) {
+    dbus_bus_remove_match(mConnection,
+                          sBluetoothDBusSignals[i],
+                          &err);
+    if (dbus_error_is_set(&err)) {
+      LOG_AND_FREE_DBUS_ERROR(&err);
+    }
+  }
+
+  dbus_connection_remove_filter(mConnection, EventFilter, nullptr);
+  
   mConnection = nullptr;
   mBluetoothSignalObserverTable.Clear();
   StopDBus();
   return NS_OK;
 }
 
 nsresult
 BluetoothDBusService::GetDefaultAdapterPathInternal(BluetoothReplyRunnable* aRunnable)
--- a/ipc/dbus/DBusThread.cpp
+++ b/ipc/dbus/DBusThread.cpp
@@ -72,31 +72,16 @@
 // TODO Add Wakeup to this list once we've moved to ics
 
 enum {
   DBUS_EVENT_LOOP_EXIT = 1,
   DBUS_EVENT_LOOP_ADD = 2,
   DBUS_EVENT_LOOP_REMOVE = 3,
 } DBusEventTypes;
 
-// Signals that the DBus thread should listen for. Needs to include
-// all signals any DBus observer object may need.
-
-static const char* DBUS_SIGNALS[] =
-{
-  "type='signal',interface='org.bluez.Adapter'",
-  "type='signal',interface='org.bluez.Manager'",
-  "type='signal',interface='org.bluez.Device'",
-  "type='signal',interface='org.bluez.Input'",
-  "type='signal',interface='org.bluez.Network'",
-  "type='signal',interface='org.bluez.NetworkServer'",
-  "type='signal',interface='org.bluez.HealthDevice'",
-  "type='signal',interface='org.bluez.AudioSink'"
-};
-
 static unsigned int UnixEventsToDBusFlags(short events)
 {
   return (events & DBUS_WATCH_READABLE ? POLLIN : 0) |
     (events & DBUS_WATCH_WRITABLE ? POLLOUT : 0) |
     (events & DBUS_WATCH_ERROR ? POLLERR : 0) |
     (events & DBUS_WATCH_HANGUP ? POLLHUP : 0);
 }
 
@@ -145,17 +130,16 @@ struct DBusThread : public RawDBusConnec
   // Sockets for receiving dbus control information (watch
   // add/removes, loop shutdown, etc...)
   ScopedClose mControlFdR;
   ScopedClose mControlFdW;
 
 protected:
   bool SetUpEventLoop();
   bool TearDownData();
-  bool TearDownEventLoop();
 };
 
 static nsAutoPtr<DBusThread> sDBusThread;
 
 // DBus utility functions
 // Free statics, as they're used as function pointers in dbus setup
 
 static dbus_bool_t
@@ -317,29 +301,16 @@ DBusThread::SetUpEventLoop()
 
   // If we can't establish a connection to dbus, nothing else will work
   nsresult rv = EstablishDBusConnection();
   if (NS_FAILED(rv)) {
     NS_WARNING("Cannot create DBus Connection for DBus Thread!");
     return false;
   }
 
-  // Set which messages will be processed by this dbus connection.
-  // Since we are maintaining a single thread for all the DBus bluez
-  // signals we want, register all of them in this thread at startup.
-  // The event handler will sort the destinations out as needed.
-  for (uint32_t i = 0; i < ArrayLength(DBUS_SIGNALS); ++i) {
-    dbus_bus_add_match(mConnection,
-                       DBUS_SIGNALS[i],
-                       &err);
-    if (dbus_error_is_set(&err)) {
-      LOG_AND_FREE_DBUS_ERROR(&err);
-      return false;
-    }
-  }
   return true;
 }
 
 bool
 DBusThread::TearDownData()
 {
   LOG("Removing DBus Sockets\n");
   if (mControlFdW.get()) {
@@ -351,36 +322,16 @@ DBusThread::TearDownData()
   mPollData.Clear();
 
   // DBusWatch pointers are maintained by DBus, so we won't leak by
   // clearing.
   mWatchData.Clear();
   return true;
 }
 
-bool
-DBusThread::TearDownEventLoop()
-{
-  MOZ_ASSERT(mConnection);
-
-  DBusError err;
-  dbus_error_init(&err);
-
-  for (uint32_t i = 0; i < ArrayLength(DBUS_SIGNALS); ++i) {
-    dbus_bus_remove_match(mConnection,
-                          DBUS_SIGNALS[i],
-                          &err);
-    if (dbus_error_is_set(&err)) {
-      LOG_AND_FREE_DBUS_ERROR(&err);
-    }
-  }
-
-  return true;
-}
-
 void
 DBusThread::EventLoop()
 {
   dbus_connection_set_watch_functions(mConnection, AddWatch,
                                       RemoveWatch, ToggleWatch, this, NULL);
 
   LOG("DBus Event Loop Starting\n");
   while (1) {
@@ -395,17 +346,16 @@ DBusThread::EventLoop()
         char data;
         while (recv(mControlFdR.get(), &data, sizeof(char), MSG_DONTWAIT)
                != -1) {
           switch (data) {
             case DBUS_EVENT_LOOP_EXIT:
               LOG("DBus Event Loop Exiting\n");
               dbus_connection_set_watch_functions(mConnection,
                                                   NULL, NULL, NULL, NULL, NULL);
-              TearDownEventLoop();
               return;
             case DBUS_EVENT_LOOP_ADD:
               HandleWatchAdd(this);
               break;
             case DBUS_EVENT_LOOP_REMOVE:
               HandleWatchRemove(this);
               break;
           }