Bug 922617: Don't inherit BluetoothDBusService from RawDBusConnection, r=echou
authorThomas Zimmermann <tdz@users.sourceforge.net>
Tue, 01 Oct 2013 12:29:02 +0200
changeset 150202 32b6fc360a0b6884879b305419f38d4ed367f29a
parent 150201 3e484a4396a22f97b1ad7af8218c578700ef805d
child 150203 22f47bc6979f8c87ee3d64430b1ee0929751c4b4
push id2976
push userryanvm@gmail.com
push dateWed, 09 Oct 2013 19:32:52 +0000
treeherderfx-team@a141e39bf6da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersechou
bugs922617
milestone27.0a1
Bug 922617: Don't inherit BluetoothDBusService from RawDBusConnection, r=echou BluetoothDBusService uses RawDBusConnection, but isn't one. With this patch, the connection is instead maintained as a field of the class. This change also allows to delete the class instance when the connection gets closed, which will allow for further cleanups of the DBus connection handling within Bluetooth.
dom/bluetooth/linux/BluetoothDBusService.cpp
dom/bluetooth/linux/BluetoothDBusService.h
--- a/dom/bluetooth/linux/BluetoothDBusService.cpp
+++ b/dom/bluetooth/linux/BluetoothDBusService.cpp
@@ -1621,17 +1621,19 @@ BluetoothDBusService::StartInternal()
     BT_WARNING("Cannot start DBus thread!");
     return NS_ERROR_FAILURE;
   }
 
   if (mConnection) {
     return NS_OK;
   }
 
-  if (NS_FAILED(EstablishDBusConnection())) {
+  mConnection = new RawDBusConnection();
+
+  if (NS_FAILED(mConnection->EstablishDBusConnection())) {
     BT_WARNING("Cannot start Main Thread DBus connection!");
     StopDBus();
     return NS_ERROR_FAILURE;
   }
 
   gThreadConnection = new RawDBusConnection();
 
   if (NS_FAILED(gThreadConnection->EstablishDBusConnection())) {
@@ -1643,27 +1645,27 @@ BluetoothDBusService::StartInternal()
   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,
+    dbus_bus_add_match(mConnection->GetConnection(),
                        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)) {
+  if (!dbus_connection_add_filter(mConnection->GetConnection(),
+                                  EventFilter, NULL, NULL)) {
     BT_WARNING("Cannot create DBus Event Filter for DBus Thread!");
     return NS_ERROR_FAILURE;
   }
 
   if (!sPairingReqTable) {
     sPairingReqTable = new nsDataHashtable<nsStringHashKey, DBusMessage* >;
   }
 
@@ -1707,25 +1709,26 @@ BluetoothDBusService::StopInternal()
   if (!mConnection) {
     StopDBus();
     return NS_OK;
   }
 
   DBusError err;
   dbus_error_init(&err);
   for (uint32_t i = 0; i < ArrayLength(sBluetoothDBusSignals); ++i) {
-    dbus_bus_remove_match(mConnection,
+    dbus_bus_remove_match(mConnection->GetConnection(),
                           sBluetoothDBusSignals[i],
                           &err);
     if (dbus_error_is_set(&err)) {
       LOG_AND_FREE_DBUS_ERROR(&err);
     }
   }
 
-  dbus_connection_remove_filter(mConnection, EventFilter, nullptr);
+  dbus_connection_remove_filter(mConnection->GetConnection(),
+                                EventFilter, nullptr);
 
   if (!dbus_connection_unregister_object_path(gThreadConnection->GetConnection(),
                                               KEY_LOCAL_AGENT)) {
     BT_WARNING("%s: Can't unregister object path %s for agent!",
         __FUNCTION__, KEY_LOCAL_AGENT);
   }
 
   if (!dbus_connection_unregister_object_path(gThreadConnection->GetConnection(),
@@ -1880,20 +1883,21 @@ BluetoothDBusService::GetDefaultAdapterP
     NS_NAMED_LITERAL_STRING(errorStr, "Bluetooth service is not ready yet!");
     DispatchBluetoothReply(aRunnable, BluetoothValue(), errorStr);
     return NS_OK;
   }
 
   nsRefPtr<DefaultAdapterPathReplyHandler> handler =
     new DefaultAdapterPathReplyHandler(aRunnable);
 
-  bool success = SendWithReply(DefaultAdapterPathReplyHandler::Callback,
-                               handler.get(), 1000,
-                               "/", DBUS_MANAGER_IFACE, "DefaultAdapter",
-                               DBUS_TYPE_INVALID);
+  bool success = mConnection->SendWithReply(
+    DefaultAdapterPathReplyHandler::Callback,
+    handler.get(), 1000,
+    "/", DBUS_MANAGER_IFACE, "DefaultAdapter",
+    DBUS_TYPE_INVALID);
 
   NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
 
   handler.forget();
 
   return NS_OK;
 }
 
@@ -1923,21 +1927,22 @@ BluetoothDBusService::SendDiscoveryMessa
   if (!IsReady()) {
     NS_NAMED_LITERAL_STRING(errorStr, "Bluetooth service is not ready yet!");
     DispatchBluetoothReply(aRunnable, BluetoothValue(), errorStr);
     return NS_OK;
   }
 
   nsRefPtr<BluetoothReplyRunnable> runnable(aRunnable);
 
-  bool success = SendWithReply(OnSendDiscoveryMessageReply,
-                               static_cast<void*>(aRunnable), -1,
-                               NS_ConvertUTF16toUTF8(sAdapterPath).get(),
-                               DBUS_ADAPTER_IFACE, aMessageName,
-                               DBUS_TYPE_INVALID);
+  bool success = mConnection->SendWithReply(
+    OnSendDiscoveryMessageReply,
+    static_cast<void*>(aRunnable), -1,
+    NS_ConvertUTF16toUTF8(sAdapterPath).get(),
+    DBUS_ADAPTER_IFACE, aMessageName,
+    DBUS_TYPE_INVALID);
 
   NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
 
   runnable.forget();
 
   return NS_OK;
 }
 
@@ -1977,21 +1982,21 @@ BluetoothDBusService::SendAsyncDBusMessa
     *serviceClass = BluetoothServiceClass::A2DP;
   } else if (!strcmp(aInterface, DBUS_INPUT_IFACE)) {
     *serviceClass = BluetoothServiceClass::HID;
   } else {
     MOZ_ASSERT(false);
     return NS_ERROR_FAILURE;
   }
 
-  bool ret = SendWithReply(aCallback,
-                           static_cast<void*>(serviceClass.forget()), -1,
-                           NS_ConvertUTF16toUTF8(aObjectPath).get(),
-                           aInterface, NS_ConvertUTF16toUTF8(aMessage).get(),
-                           DBUS_TYPE_INVALID);
+  bool ret = mConnection->SendWithReply(
+    aCallback, static_cast<void*>(serviceClass.forget()), -1,
+    NS_ConvertUTF16toUTF8(aObjectPath).get(),
+    aInterface, NS_ConvertUTF16toUTF8(aMessage).get(),
+    DBUS_TYPE_INVALID);
 
   NS_ENSURE_TRUE(ret, NS_ERROR_FAILURE);
 
   return NS_OK;
 }
 
 nsresult
 BluetoothDBusService::SendSinkMessage(const nsAString& aDeviceAddress,
@@ -2289,17 +2294,20 @@ BluetoothDBusService::SetProperty(Blueto
     BT_WARNING("Could not append argument to method call!");
     dbus_message_unref(msg);
     return NS_ERROR_FAILURE;
   }
 
   nsRefPtr<BluetoothReplyRunnable> runnable = aRunnable;
 
   // msg is unref'd as part of SendWithReply
-  if (!SendWithReply(GetVoidCallback, (void*)aRunnable, 1000, msg)) {
+  bool success = mConnection->SendWithReply(GetVoidCallback,
+                                            (void*)aRunnable,
+                                            1000, msg);
+  if (!success) {
     BT_WARNING("SendWithReply failed");
     return NS_ERROR_FAILURE;
   }
   runnable.forget();
   return NS_OK;
 }
 
 bool
@@ -2336,24 +2344,25 @@ BluetoothDBusService::CreatePairedDevice
    * Please see Bug 818696 for more information.
    */
   sIsPairing++;
 
   nsRefPtr<BluetoothReplyRunnable> runnable = aRunnable;
 
   // Then send CreatePairedDevice, it will register a temp device agent then
   // unregister it after pairing process is over
-  bool ret = SendWithReply(GetObjectPathCallback, (void*)runnable, aTimeout,
-                           NS_ConvertUTF16toUTF8(sAdapterPath).get(),
-                           DBUS_ADAPTER_IFACE,
-                           "CreatePairedDevice",
-                           DBUS_TYPE_STRING, &deviceAddress,
-                           DBUS_TYPE_OBJECT_PATH, &deviceAgentPath,
-                           DBUS_TYPE_STRING, &capabilities,
-                           DBUS_TYPE_INVALID);
+  bool ret = mConnection->SendWithReply(
+    GetObjectPathCallback, (void*)runnable, aTimeout,
+    NS_ConvertUTF16toUTF8(sAdapterPath).get(),
+    DBUS_ADAPTER_IFACE,
+    "CreatePairedDevice",
+    DBUS_TYPE_STRING, &deviceAddress,
+    DBUS_TYPE_OBJECT_PATH, &deviceAgentPath,
+    DBUS_TYPE_STRING, &capabilities,
+    DBUS_TYPE_INVALID);
   if (!ret) {
     BT_WARNING("Could not start async function!");
     return NS_ERROR_FAILURE;
   }
 
   runnable.forget();
   return NS_OK;
 }
@@ -2387,22 +2396,22 @@ BluetoothDBusService::RemoveDeviceIntern
 
   nsCString deviceObjectPath =
     NS_ConvertUTF16toUTF8(GetObjectPathFromAddress(sAdapterPath,
                                                    aDeviceAddress));
   const char* cstrDeviceObjectPath = deviceObjectPath.get();
 
   nsRefPtr<BluetoothReplyRunnable> runnable(aRunnable);
 
-  bool success = SendWithReply(OnRemoveDeviceReply,
-                               static_cast<void*>(runnable.get()), -1,
-                               NS_ConvertUTF16toUTF8(sAdapterPath).get(),
-                               DBUS_ADAPTER_IFACE, "RemoveDevice",
-                               DBUS_TYPE_OBJECT_PATH, &cstrDeviceObjectPath,
-                               DBUS_TYPE_INVALID);
+  bool success = mConnection->SendWithReply(
+    OnRemoveDeviceReply, static_cast<void*>(runnable.get()), -1,
+    NS_ConvertUTF16toUTF8(sAdapterPath).get(),
+    DBUS_ADAPTER_IFACE, "RemoveDevice",
+    DBUS_TYPE_OBJECT_PATH, &cstrDeviceObjectPath,
+    DBUS_TYPE_INVALID);
 
   NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
 
   runnable.forget();
 
   return NS_OK;
 }
 
@@ -2438,17 +2447,17 @@ BluetoothDBusService::SetPinCodeInternal
 
   if (!dbus_message_append_args(reply,
                                 DBUS_TYPE_STRING, &pinCode,
                                 DBUS_TYPE_INVALID)) {
     BT_WARNING("%s: Couldn't append arguments to dbus message.", __FUNCTION__);
     errorStr.AssignLiteral("Couldn't append arguments to dbus message.");
     result = false;
   } else {
-    result = Send(reply);
+    result = mConnection->Send(reply);
   }
 
   dbus_message_unref(msg);
   dbus_message_unref(reply);
 
   sPairingReqTable->Remove(aDeviceAddress);
   DispatchBluetoothReply(aRunnable, v, errorStr);
   return result;
@@ -2484,17 +2493,17 @@ BluetoothDBusService::SetPasskeyInternal
 
   if (!dbus_message_append_args(reply,
                                 DBUS_TYPE_UINT32, &passkey,
                                 DBUS_TYPE_INVALID)) {
     BT_WARNING("%s: Couldn't append arguments to dbus message.", __FUNCTION__);
     errorStr.AssignLiteral("Couldn't append arguments to dbus message.");
     result = false;
   } else {
-    result = Send(reply);
+    result = mConnection->Send(reply);
   }
 
   dbus_message_unref(msg);
   dbus_message_unref(reply);
 
   sPairingReqTable->Remove(aDeviceAddress);
   DispatchBluetoothReply(aRunnable, v, errorStr);
   return result;
@@ -2528,17 +2537,17 @@ BluetoothDBusService::SetPairingConfirma
   if (!reply) {
     BT_WARNING("%s: Memory can't be allocated for the message.", __FUNCTION__);
     dbus_message_unref(msg);
     errorStr.AssignLiteral("Memory can't be allocated for the message.");
     DispatchBluetoothReply(aRunnable, v, errorStr);
     return false;
   }
 
-  bool result = Send(reply);
+  bool result = mConnection->Send(reply);
   if (!result) {
     errorStr.AssignLiteral("Can't send message!");
   }
   dbus_message_unref(msg);
   dbus_message_unref(reply);
 
   sPairingReqTable->Remove(aDeviceAddress);
   DispatchBluetoothReply(aRunnable, v, errorStr);
@@ -2741,23 +2750,23 @@ BluetoothDBusService::GetServiceChannel(
   // GetServiceAttributeValue only exists in android's bluez dbus binding
   // implementation
   nsCString serviceUUID = NS_ConvertUTF16toUTF8(aServiceUUID);
   const char* cstrServiceUUID = serviceUUID.get();
 
   nsRefPtr<OnGetServiceChannelReplyHandler> handler =
     new OnGetServiceChannelReplyHandler(objectPath, aServiceUUID, aManager);
 
-  bool success = SendWithReply(OnGetServiceChannelReplyHandler::Callback,
-                               handler, -1,
-                               NS_ConvertUTF16toUTF8(objectPath).get(),
-                               DBUS_DEVICE_IFACE, "GetServiceAttributeValue",
-                               DBUS_TYPE_STRING, &cstrServiceUUID,
-                               DBUS_TYPE_UINT16, &sProtocolDescriptorList,
-                               DBUS_TYPE_INVALID);
+  bool success = mConnection->SendWithReply(
+    OnGetServiceChannelReplyHandler::Callback, handler, -1,
+    NS_ConvertUTF16toUTF8(objectPath).get(),
+    DBUS_DEVICE_IFACE, "GetServiceAttributeValue",
+    DBUS_TYPE_STRING, &cstrServiceUUID,
+    DBUS_TYPE_UINT16, &sProtocolDescriptorList,
+    DBUS_TYPE_INVALID);
   NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
 
   handler.forget();
 #else
   // FIXME/Bug 793977 qdot: Just set something for desktop, until we have a
   // parser for the GetServiceAttributes xml block
   //
   // Even though we are on the main thread already, we need to dispatch a
@@ -2794,23 +2803,23 @@ BluetoothDBusService::UpdateSdpRecords(c
 
   nsString objectPath(GetObjectPathFromAddress(sAdapterPath, aDeviceAddress));
 
   // I choose to use raw pointer here because this is going to be passed as an
   // argument into SendWithReply() at once.
   OnUpdateSdpRecordsRunnable* callbackRunnable =
     new OnUpdateSdpRecordsRunnable(objectPath, aManager);
 
-  return SendWithReply(DiscoverServicesCallback,
-                       (void*)callbackRunnable, -1,
-                       NS_ConvertUTF16toUTF8(objectPath).get(),
-                       DBUS_DEVICE_IFACE,
-                       "DiscoverServices",
-                       DBUS_TYPE_STRING, &EmptyCString(),
-                       DBUS_TYPE_INVALID);
+  return mConnection->SendWithReply(DiscoverServicesCallback,
+                                    (void*)callbackRunnable, -1,
+                                    NS_ConvertUTF16toUTF8(objectPath).get(),
+                                    DBUS_DEVICE_IFACE,
+                                    "DiscoverServices",
+                                    DBUS_TYPE_STRING, &EmptyCString(),
+                                    DBUS_TYPE_INVALID);
 }
 
 nsresult
 BluetoothDBusService::GetScoSocket(const nsAString& aAddress,
                                    bool aAuth,
                                    bool aEncrypt,
                                    mozilla::ipc::UnixSocketConsumer* aConsumer)
 {
@@ -3009,26 +3018,27 @@ BluetoothDBusService::SendMetaData(const
   if (aMediaNumber != a2dp->GetMediaNumber() ||
       !aTitle.Equals(prevTitle) ||
       !aAlbum.Equals(prevAlbum)) {
     UpdateNotification(ControlEventId::EVENT_TRACK_CHANGED, aMediaNumber);
   }
 
   nsRefPtr<BluetoothReplyRunnable> runnable(aRunnable);
 
-  bool ret = SendWithReply(GetVoidCallback, (void*)runnable.get(), -1,
-                           NS_ConvertUTF16toUTF8(objectPath).get(),
-                           DBUS_CTL_IFACE, "UpdateMetaData",
-                           DBUS_TYPE_STRING, &title,
-                           DBUS_TYPE_STRING, &artist,
-                           DBUS_TYPE_STRING, &album,
-                           DBUS_TYPE_STRING, &mediaNumber,
-                           DBUS_TYPE_STRING, &totalMediaCount,
-                           DBUS_TYPE_STRING, &duration,
-                           DBUS_TYPE_INVALID);
+  bool ret = mConnection->SendWithReply(
+    GetVoidCallback, (void*)runnable.get(), -1,
+    NS_ConvertUTF16toUTF8(objectPath).get(),
+    DBUS_CTL_IFACE, "UpdateMetaData",
+    DBUS_TYPE_STRING, &title,
+    DBUS_TYPE_STRING, &artist,
+    DBUS_TYPE_STRING, &album,
+    DBUS_TYPE_STRING, &mediaNumber,
+    DBUS_TYPE_STRING, &totalMediaCount,
+    DBUS_TYPE_STRING, &duration,
+    DBUS_TYPE_INVALID);
   NS_ENSURE_TRUE_VOID(ret);
 
   runnable.forget();
 
   a2dp->UpdateMetaData(aTitle, aArtist, aAlbum,
                        aMediaNumber, aTotalMediaCount, aDuration);
 }
 
@@ -3106,23 +3116,24 @@ BluetoothDBusService::SendPlayStatus(int
 
   nsAutoString address;
   a2dp->GetAddress(address);
   nsString objectPath =
     GetObjectPathFromAddress(sAdapterPath, address);
 
   nsRefPtr<BluetoothReplyRunnable> runnable(aRunnable);
 
-  bool ret = SendWithReply(GetVoidCallback, (void*)runnable.get(), -1,
-                           NS_ConvertUTF16toUTF8(objectPath).get(),
-                           DBUS_CTL_IFACE, "UpdatePlayStatus",
-                           DBUS_TYPE_UINT32, &aDuration,
-                           DBUS_TYPE_UINT32, &aPosition,
-                           DBUS_TYPE_UINT32, &tempPlayStatus,
-                           DBUS_TYPE_INVALID);
+  bool ret = mConnection->SendWithReply(
+    GetVoidCallback, (void*)runnable.get(), -1,
+    NS_ConvertUTF16toUTF8(objectPath).get(),
+    DBUS_CTL_IFACE, "UpdatePlayStatus",
+    DBUS_TYPE_UINT32, &aDuration,
+    DBUS_TYPE_UINT32, &aPosition,
+    DBUS_TYPE_UINT32, &tempPlayStatus,
+    DBUS_TYPE_INVALID);
   NS_ENSURE_TRUE_VOID(ret);
 
   runnable.forget();
 
   a2dp->UpdatePlayStatus(aDuration, aPosition, playStatus);
 }
 
 static void
@@ -3153,23 +3164,24 @@ BluetoothDBusService::UpdatePlayStatus(u
 
   nsAutoString address;
   a2dp->GetAddress(address);
   nsString objectPath =
     GetObjectPathFromAddress(sAdapterPath, address);
 
   uint32_t tempPlayStatus = aPlayStatus;
 
-  bool ret = SendWithReply(ControlCallback, nullptr, -1,
-                           NS_ConvertUTF16toUTF8(objectPath).get(),
-                           DBUS_CTL_IFACE, "UpdatePlayStatus",
-                           DBUS_TYPE_UINT32, &aDuration,
-                           DBUS_TYPE_UINT32, &aPosition,
-                           DBUS_TYPE_UINT32, &tempPlayStatus,
-                           DBUS_TYPE_INVALID);
+  bool ret = mConnection->SendWithReply(
+    ControlCallback, nullptr, -1,
+    NS_ConvertUTF16toUTF8(objectPath).get(),
+    DBUS_CTL_IFACE, "UpdatePlayStatus",
+    DBUS_TYPE_UINT32, &aDuration,
+    DBUS_TYPE_UINT32, &aPosition,
+    DBUS_TYPE_UINT32, &tempPlayStatus,
+    DBUS_TYPE_INVALID);
   NS_ENSURE_TRUE_VOID(ret);
 }
 
 void
 BluetoothDBusService::UpdateNotification(ControlEventId aEventId,
                                          uint64_t aData)
 {
   MOZ_ASSERT(NS_IsMainThread());
@@ -3181,17 +3193,17 @@ BluetoothDBusService::UpdateNotification
   MOZ_ASSERT(a2dp->IsAvrcpConnected());
 
   nsAutoString address;
   a2dp->GetAddress(address);
   nsString objectPath =
     GetObjectPathFromAddress(sAdapterPath, address);
   uint16_t eventId = aEventId;
 
-  bool ret = SendWithReply(ControlCallback, nullptr, -1,
-                           NS_ConvertUTF16toUTF8(objectPath).get(),
-                           DBUS_CTL_IFACE,
-                           "UpdateNotification",
-                           DBUS_TYPE_UINT16, &eventId,
-                           DBUS_TYPE_UINT64, &aData,
-                           DBUS_TYPE_INVALID);
+  bool ret = mConnection->SendWithReply(
+    ControlCallback, nullptr, -1,
+    NS_ConvertUTF16toUTF8(objectPath).get(),
+    DBUS_CTL_IFACE, "UpdateNotification",
+    DBUS_TYPE_UINT16, &eventId,
+    DBUS_TYPE_UINT64, &aData,
+    DBUS_TYPE_INVALID);
   NS_ENSURE_TRUE_VOID(ret);
 }
--- a/dom/bluetooth/linux/BluetoothDBusService.h
+++ b/dom/bluetooth/linux/BluetoothDBusService.h
@@ -17,17 +17,16 @@ class DBusMessage;
 BEGIN_BLUETOOTH_NAMESPACE
 
 /**
  * BluetoothDBusService is the implementation of BluetoothService for DBus on
  * linux/android/B2G. Function comments are in BluetoothService.h
  */
 
 class BluetoothDBusService : public BluetoothService
-                           , private mozilla::ipc::RawDBusConnection
 {
 public:
   bool IsReady();
 
   virtual nsresult StartInternal() MOZ_OVERRIDE;
 
   virtual nsresult StopInternal() MOZ_OVERRIDE;
 
@@ -196,13 +195,15 @@ private:
                                   BluetoothReplyRunnable* aRunnable);
 
   void UpdateNotification(ControlEventId aEventId, uint64_t aData);
 
   nsresult SendAsyncDBusMessage(const nsAString& aObjectPath,
                                 const char* aInterface,
                                 const nsAString& aMessage,
                                 mozilla::ipc::DBusReplyCallback aCallback);
+
+  nsRefPtr<mozilla::ipc::RawDBusConnection> mConnection;
 };
 
 END_BLUETOOTH_NAMESPACE
 
 #endif