toolkit/system/dbus/nsDBusService.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/system/dbus/nsDBusService.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,142 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* vim:expandtab:shiftwidth=4:tabstop=4:
     1.6 + */
     1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    1.10 +
    1.11 +#include "nsDBusService.h"
    1.12 +#include "nsComponentManagerUtils.h"
    1.13 +#include "nsAutoPtr.h"
    1.14 +
    1.15 +#include <glib.h>
    1.16 +#include <dbus/dbus-glib-lowlevel.h>
    1.17 +#include <dbus/dbus-glib.h>
    1.18 +
    1.19 +nsDBusService::nsDBusService() {
    1.20 +  mConnection = nullptr;
    1.21 +  mSingleClient = nullptr;
    1.22 +}
    1.23 +
    1.24 +nsDBusService::~nsDBusService() {
    1.25 +  NS_ASSERTION(!mSingleClient, "Client failed to unregister");
    1.26 +  DropConnection();
    1.27 +  if (mReconnectTimer) {
    1.28 +    mReconnectTimer->Cancel();
    1.29 +  }
    1.30 +  gSingleton = nullptr;
    1.31 +}
    1.32 +
    1.33 +NS_IMPL_ISUPPORTS(nsDBusService, nsDBusService)
    1.34 +
    1.35 +nsDBusService* nsDBusService::gSingleton = nullptr;
    1.36 +
    1.37 +already_AddRefed<nsDBusService>
    1.38 +nsDBusService::Get() {
    1.39 +  if (!gSingleton) {
    1.40 +    gSingleton = new nsDBusService();
    1.41 +  }
    1.42 +  nsRefPtr<nsDBusService> ret = gSingleton;
    1.43 +  return ret.forget();
    1.44 +}
    1.45 +  
    1.46 +nsresult
    1.47 +nsDBusService::AddClient(DBusClient* client) {
    1.48 +  NS_ASSERTION(!mSingleClient, "Only one client supported right now");
    1.49 +  mSingleClient = client;
    1.50 +  nsresult rv = CreateConnection();
    1.51 +  if (NS_FAILED(rv)) {
    1.52 +    mSingleClient = nullptr;
    1.53 +  }
    1.54 +  return rv;
    1.55 +}
    1.56 +
    1.57 +void
    1.58 +nsDBusService::RemoveClient(DBusClient* client) {
    1.59 +  NS_ASSERTION(mSingleClient == client, "Removing wrong client");
    1.60 +  mSingleClient = nullptr;
    1.61 +}
    1.62 +  
    1.63 +DBusPendingCall*
    1.64 +nsDBusService::SendWithReply(DBusClient* client, DBusMessage* message) {
    1.65 +  DBusPendingCall* reply = nullptr;
    1.66 +  if (mConnection) {
    1.67 +    if (!dbus_connection_send_with_reply(mConnection, message, &reply, -1)) {
    1.68 +      reply = nullptr;
    1.69 +    }
    1.70 +  }
    1.71 +  dbus_message_unref(message);
    1.72 +  return reply;
    1.73 +}
    1.74 +
    1.75 +bool nsDBusService::HandleMessage(DBusMessage* message) {
    1.76 +  if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL,
    1.77 +                            "Disconnected")) {
    1.78 +    HandleDBusDisconnect();
    1.79 +    return false;
    1.80 +  }
    1.81 +  
    1.82 +  return mSingleClient && mSingleClient->HandleMessage(message);
    1.83 +}
    1.84 +
    1.85 +static DBusHandlerResult dbus_filter(DBusConnection* connection,
    1.86 +                                     DBusMessage* message,
    1.87 +                                     void* user_data) {
    1.88 +  return static_cast<nsDBusService*>(user_data)->HandleMessage(message)
    1.89 +    ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
    1.90 +}
    1.91 +
    1.92 +void nsDBusService::DoTimerCallback(nsITimer *aTimer) {
    1.93 +  if (aTimer == mReconnectTimer.get()) {
    1.94 +    nsresult rv = CreateConnection();
    1.95 +    if (NS_SUCCEEDED(rv)) {
    1.96 +      mReconnectTimer->Cancel();
    1.97 +      mReconnectTimer = nullptr;
    1.98 +    }
    1.99 +  }
   1.100 +}
   1.101 +
   1.102 +static void TimerCallback(nsITimer *aTimer, void *aClosure) {
   1.103 +  static_cast<nsDBusService*>(aClosure)->DoTimerCallback(aTimer);
   1.104 +}
   1.105 +
   1.106 +void nsDBusService::DropConnection() {
   1.107 +  if (mConnection) {
   1.108 +    dbus_connection_remove_filter(mConnection, dbus_filter, this);
   1.109 +    if (mSingleClient) {
   1.110 +      mSingleClient->UnregisterWithConnection(mConnection);
   1.111 +    }
   1.112 +    dbus_connection_unref(mConnection);
   1.113 +    mConnection = nullptr;
   1.114 +  }
   1.115 +}
   1.116 +
   1.117 +void nsDBusService::HandleDBusDisconnect() {
   1.118 +  DropConnection();
   1.119 +
   1.120 +  nsresult rv;
   1.121 +  mReconnectTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
   1.122 +  if (NS_FAILED(rv))
   1.123 +    return;
   1.124 +  rv = mReconnectTimer->InitWithFuncCallback(TimerCallback, this,
   1.125 +                                             5000, nsITimer::TYPE_REPEATING_SLACK);
   1.126 +  if (NS_FAILED(rv)) {
   1.127 +    mReconnectTimer = nullptr;
   1.128 +    return;
   1.129 +  }
   1.130 +}
   1.131 +
   1.132 +nsresult nsDBusService::CreateConnection() {
   1.133 +  mConnection = dbus_bus_get(DBUS_BUS_SYSTEM, nullptr);
   1.134 +  if (!mConnection)
   1.135 +    return NS_ERROR_FAILURE;
   1.136 +
   1.137 +  dbus_connection_set_exit_on_disconnect(mConnection, false);
   1.138 +  dbus_connection_setup_with_g_main(mConnection, nullptr);
   1.139 +
   1.140 +  if (!dbus_connection_add_filter(mConnection, dbus_filter, this, nullptr))
   1.141 +    return NS_ERROR_FAILURE;
   1.142 +
   1.143 +  mSingleClient->RegisterWithConnection(mConnection);
   1.144 +  return NS_OK;
   1.145 +}

mercurial