From c2af4fec1a3880e043735c588715b9732dd38f89 Mon Sep 17 00:00:00 2001 From: Thomas Zimmermann Date: Fri, 25 Oct 2013 09:50:07 +0200 Subject: [PATCH] Bug 923647: Replace GetDefaultAdapterAdapter by non-blocking implementation, r=echou GetDefaultAdapter blocks while querying the default Bluetooth adapter. This patch replaces the function with a non-blocking call to DBus. The reply is handled in a callback function. As a side effect, this change also removes an invalid warning in cases where no default adapter is available, and enabling Bluetooth seems a bit faster. --HG-- extra : rebase_source : ad3cbb2ccc8ac98b3c8e6d56e47822da0afb07f1 --- dom/bluetooth/linux/BluetoothDBusService.cpp | 52 +++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/dom/bluetooth/linux/BluetoothDBusService.cpp b/dom/bluetooth/linux/BluetoothDBusService.cpp index efcaf5e05a30..0abc47a61b75 100644 --- a/dom/bluetooth/linux/BluetoothDBusService.cpp +++ b/dom/bluetooth/linux/BluetoothDBusService.cpp @@ -1578,27 +1578,31 @@ EventFilter(DBusConnection* aConn, DBusMessage* aMsg, void* aData) return DBUS_HANDLER_RESULT_HANDLED; } -static bool -GetDefaultAdapterPath(BluetoothValue& aValue, nsString& aError) +static void +OnDefaultAdapterReply(DBusMessage* aReply, void* aData) { - // This could block. It should never be run on the main thread. - MOZ_ASSERT(!NS_IsMainThread()); + MOZ_ASSERT(!NS_IsMainThread()); // DBus thread + + if (!aReply || dbus_message_is_error(aReply, DBUS_ERROR_TIMEOUT)) { + return; + } DBusError err; dbus_error_init(&err); - DBusMessage* msg; - bool success = gThreadConnection->SendWithError(&msg, &err, 1000, "/", - DBUS_MANAGER_IFACE, - "DefaultAdapter", - DBUS_TYPE_INVALID); - NS_ENSURE_TRUE(success, false); + BluetoothValue v; + nsAutoString errorString; - UnpackObjectPathMessage(msg, &err, aValue, aError); + UnpackObjectPathMessage(aReply, &err, v, errorString); - dbus_message_unref(msg); + if (!errorString.IsEmpty()) { + return; + } - return aError.IsEmpty(); + nsRefPtr b = new PrepareAdapterRunnable(v.get_nsString()); + if (NS_FAILED(NS_DispatchToMainThread(b))) { + BT_WARNING("Failed to dispatch to main thread!"); + } } bool @@ -1669,17 +1673,17 @@ BluetoothDBusService::StartInternal() sPairingReqTable = new nsDataHashtable; } - BluetoothValue v; - nsAutoString replyError; - if (!GetDefaultAdapterPath(v, replyError)) { - // Adapter path is not ready yet - // Let's do PrepareAdapterRunnable when we receive signal 'AdapterAdded' - } else { - // Adapter path has been ready. let's do PrepareAdapterRunnable now - nsRefPtr b = new PrepareAdapterRunnable(v.get_nsString()); - if (NS_FAILED(NS_DispatchToMainThread(b))) { - BT_WARNING("Failed to dispatch to main thread!"); - } + // Normally we'll receive the signal 'AdapterAdded' for the default + // adapter from the DBus daemon during start up. If we restart after + // a crash, the default adapter might already be available, so we ask + // the daemon explicitly here. + bool success = mConnection->SendWithReply(OnDefaultAdapterReply, nullptr, + 1000, "/", + DBUS_ADAPTER_IFACE, + "DefaultAdapter", + DBUS_TYPE_INVALID); + if (!success) { + BT_WARNING("Failed to query default adapter!"); } return NS_OK;