2012-05-07 14:50:25 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2012-08-20 20:21:24 -07:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-07 14:50:25 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include <dbus/dbus.h>
|
2013-10-29 08:03:40 -07:00
|
|
|
#include "base/message_loop.h"
|
2013-10-01 01:20:21 -07:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "RawDBusConnection.h"
|
|
|
|
|
2013-12-17 10:26:45 -08:00
|
|
|
#ifdef CHROMIUM_LOG
|
|
|
|
#undef CHROMIUM_LOG
|
2013-10-01 01:20:21 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
|
|
|
#include <android/log.h>
|
2013-12-17 10:26:45 -08:00
|
|
|
#define CHROMIUM_LOG(args...) __android_log_print(ANDROID_LOG_INFO, "Gonk", args);
|
2013-10-01 01:20:21 -07:00
|
|
|
#else
|
2013-12-17 10:26:45 -08:00
|
|
|
#define CHROMIUM_LOG(args...) printf(args);
|
2013-10-01 01:20:21 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* TODO: Remove BlueZ constant */
|
|
|
|
#define BLUEZ_DBUS_BASE_IFC "org.bluez"
|
2012-05-07 14:50:25 -07:00
|
|
|
|
2014-02-07 03:01:27 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2013-10-01 01:20:21 -07:00
|
|
|
//
|
2014-02-07 03:01:27 -08:00
|
|
|
// DBusWatcher
|
2013-10-01 01:20:21 -07:00
|
|
|
//
|
|
|
|
|
2014-02-07 03:01:27 -08:00
|
|
|
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
|
|
|
|
//
|
2013-10-01 01:20:21 -07:00
|
|
|
|
2014-02-06 08:28:44 -08:00
|
|
|
class Notification
|
2013-10-01 01:20:21 -07:00
|
|
|
{
|
2013-10-29 08:03:40 -07:00
|
|
|
public:
|
2014-02-06 08:28:44 -08:00
|
|
|
Notification(DBusReplyCallback aCallback, void* aData)
|
|
|
|
: mCallback(aCallback),
|
|
|
|
mData(aData)
|
2013-10-29 08:03:40 -07:00
|
|
|
{ }
|
|
|
|
|
2014-02-06 08:28:44 -08:00
|
|
|
// Callback function for DBus replies. Only run it on I/O thread.
|
2013-10-01 01:20:21 -07:00
|
|
|
//
|
2014-02-06 08:28:44 -08:00
|
|
|
static void Handle(DBusPendingCall* aCall, void* aData)
|
2013-10-01 01:20:21 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2014-02-06 08:28:44 -08:00
|
|
|
MOZ_ASSERT(MessageLoop::current());
|
2013-10-01 01:20:21 -07:00
|
|
|
|
2014-02-06 08:28:44 -08:00
|
|
|
nsAutoPtr<Notification> ntfn(static_cast<Notification*>(aData));
|
2013-10-01 01:20:21 -07:00
|
|
|
|
2014-02-06 08:28:44 -08:00
|
|
|
// The reply can be non-null if the timeout has been reached.
|
2013-10-01 01:20:21 -07:00
|
|
|
DBusMessage* reply = dbus_pending_call_steal_reply(aCall);
|
|
|
|
|
|
|
|
if (reply) {
|
2014-02-06 08:28:44 -08:00
|
|
|
ntfn->RunCallback(reply);
|
2013-10-01 01:20:21 -07:00
|
|
|
dbus_message_unref(reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
dbus_pending_call_cancel(aCall);
|
|
|
|
dbus_pending_call_unref(aCall);
|
|
|
|
}
|
|
|
|
|
2014-02-06 08:28:44 -08:00
|
|
|
private:
|
|
|
|
void RunCallback(DBusMessage* aMessage)
|
2013-10-01 01:20:21 -07:00
|
|
|
{
|
2014-02-06 08:28:44 -08:00
|
|
|
if (mCallback) {
|
|
|
|
mCallback(aMessage, mData);
|
|
|
|
}
|
|
|
|
}
|
2013-10-01 01:20:21 -07:00
|
|
|
|
2013-10-01 01:20:55 -07:00
|
|
|
DBusReplyCallback mCallback;
|
|
|
|
void* mData;
|
2013-10-01 01:20:21 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// RawDBusConnection
|
|
|
|
//
|
|
|
|
|
2013-01-14 02:50:18 -08:00
|
|
|
bool RawDBusConnection::sDBusIsInit(false);
|
|
|
|
|
2013-07-22 04:30:22 -07:00
|
|
|
RawDBusConnection::RawDBusConnection()
|
|
|
|
{
|
2012-05-07 14:50:25 -07:00
|
|
|
}
|
|
|
|
|
2013-07-22 04:30:22 -07:00
|
|
|
RawDBusConnection::~RawDBusConnection()
|
|
|
|
{
|
2012-05-07 14:50:25 -07:00
|
|
|
}
|
|
|
|
|
2013-01-14 02:50:18 -08:00
|
|
|
nsresult RawDBusConnection::EstablishDBusConnection()
|
|
|
|
{
|
|
|
|
if (!sDBusIsInit) {
|
|
|
|
dbus_bool_t success = dbus_threads_init_default();
|
|
|
|
NS_ENSURE_TRUE(success == TRUE, NS_ERROR_FAILURE);
|
|
|
|
sDBusIsInit = true;
|
|
|
|
}
|
2012-05-07 14:50:25 -07:00
|
|
|
DBusError err;
|
|
|
|
dbus_error_init(&err);
|
2013-10-29 08:03:40 -07:00
|
|
|
mConnection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err);
|
2012-05-07 14:50:25 -07:00
|
|
|
if (dbus_error_is_set(&err)) {
|
|
|
|
dbus_error_free(&err);
|
2012-06-02 11:23:16 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-05-07 14:50:25 -07:00
|
|
|
}
|
|
|
|
dbus_connection_set_exit_on_disconnect(mConnection, FALSE);
|
2012-06-02 11:23:16 -07:00
|
|
|
return NS_OK;
|
2012-05-07 14:50:25 -07:00
|
|
|
}
|
|
|
|
|
2014-02-07 03:01:27 -08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-06-02 11:23:16 -07:00
|
|
|
void RawDBusConnection::ScopedDBusConnectionPtrTraits::release(DBusConnection* ptr)
|
|
|
|
{
|
2013-01-14 02:50:18 -08:00
|
|
|
if (ptr) {
|
2013-10-29 08:03:40 -07:00
|
|
|
dbus_connection_close(ptr);
|
2013-01-14 02:50:18 -08:00
|
|
|
dbus_connection_unref(ptr);
|
|
|
|
}
|
2012-06-02 11:23:16 -07:00
|
|
|
}
|
2013-10-01 01:20:21 -07:00
|
|
|
|
|
|
|
bool RawDBusConnection::Send(DBusMessage* aMessage)
|
|
|
|
{
|
2014-02-06 08:28:44 -08:00
|
|
|
MOZ_ASSERT(aMessage);
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(MessageLoop::current());
|
|
|
|
|
|
|
|
dbus_bool_t success = dbus_connection_send(mConnection,
|
|
|
|
aMessage,
|
|
|
|
nullptr);
|
|
|
|
if (success != TRUE) {
|
|
|
|
dbus_message_unref(aMessage);
|
2013-10-01 01:20:21 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-01 01:20:55 -07:00
|
|
|
bool RawDBusConnection::SendWithReply(DBusReplyCallback aCallback,
|
2013-10-01 01:20:21 -07:00
|
|
|
void* aData,
|
|
|
|
int aTimeout,
|
|
|
|
DBusMessage* aMessage)
|
|
|
|
{
|
2014-02-06 08:28:44 -08:00
|
|
|
MOZ_ASSERT(aMessage);
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(MessageLoop::current());
|
2013-10-01 01:20:21 -07:00
|
|
|
|
2014-02-06 08:28:44 -08:00
|
|
|
nsAutoPtr<Notification> ntfn(new Notification(aCallback, aData));
|
|
|
|
NS_ENSURE_TRUE(ntfn, false);
|
2013-10-01 01:20:21 -07:00
|
|
|
|
2014-02-06 08:28:44 -08:00
|
|
|
DBusPendingCall* call;
|
|
|
|
|
|
|
|
dbus_bool_t success = dbus_connection_send_with_reply(mConnection,
|
|
|
|
aMessage,
|
|
|
|
&call,
|
|
|
|
aTimeout);
|
|
|
|
NS_ENSURE_TRUE(success == TRUE, false);
|
|
|
|
|
|
|
|
success = dbus_pending_call_set_notify(call, Notification::Handle,
|
|
|
|
ntfn, nullptr);
|
|
|
|
NS_ENSURE_TRUE(success == TRUE, false);
|
|
|
|
|
|
|
|
ntfn.forget();
|
|
|
|
dbus_message_unref(aMessage);
|
2013-10-01 01:20:21 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-01 01:20:55 -07:00
|
|
|
bool RawDBusConnection::SendWithReply(DBusReplyCallback aCallback,
|
2013-10-01 01:20:21 -07:00
|
|
|
void* aData,
|
|
|
|
int aTimeout,
|
|
|
|
const char* aPath,
|
|
|
|
const char* aIntf,
|
|
|
|
const char* aFunc,
|
|
|
|
int aFirstArgType,
|
|
|
|
...)
|
|
|
|
{
|
2014-02-06 08:28:44 -08:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2013-10-01 01:20:21 -07:00
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, aFirstArgType);
|
|
|
|
DBusMessage* msg = BuildDBusMessage(aPath, aIntf, aFunc,
|
|
|
|
aFirstArgType, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (!msg) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SendWithReply(aCallback, aData, aTimeout, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
DBusMessage* RawDBusConnection::BuildDBusMessage(const char* aPath,
|
|
|
|
const char* aIntf,
|
|
|
|
const char* aFunc,
|
|
|
|
int aFirstArgType,
|
|
|
|
va_list aArgs)
|
|
|
|
{
|
|
|
|
DBusMessage* msg = dbus_message_new_method_call(BLUEZ_DBUS_BASE_IFC,
|
|
|
|
aPath, aIntf, aFunc);
|
|
|
|
if (!msg) {
|
2013-12-17 10:26:45 -08:00
|
|
|
CHROMIUM_LOG("Could not allocate D-Bus message object!");
|
2013-10-01 01:20:21 -07:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* append arguments */
|
|
|
|
if (!dbus_message_append_args_valist(msg, aFirstArgType, aArgs)) {
|
2013-12-17 10:26:45 -08:00
|
|
|
CHROMIUM_LOG("Could not append argument to method call!");
|
2013-10-01 01:20:21 -07:00
|
|
|
dbus_message_unref(msg);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
2014-02-06 08:28:44 -08:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|