1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:expandtab:shiftwidth=4:tabstop=4:
4 /* ***** BEGIN LICENSE BLOCK *****
5 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
8 * The contents of this file are subject to the Mozilla Public
9 * License Version 1.1 (the "License"); you may not use this file
10 * except in compliance with the License. You may obtain a copy of
11 * the License at http://www.mozilla.org/MPL/
13 * Software distributed under the License is distributed on an "AS
14 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
15 * implied. See the License for the specific language governing
16 * rights and limitations under the License.
18 * The Original Code is Novell code.
20 * The Initial Developer of the Original Code is Novell, Inc.
21 * Portions created by the Initial Developer are Copyright (C) 2006
22 * the Initial Developer. All Rights Reserved.
24 * Original Author: Robert O'Callahan (rocallahan@novell.com)
28 * Alternatively, the contents of this file may be used under the terms of
29 * either the GNU General Public License Version 2 or later (the "GPL"), or
30 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the NPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the NPL, the GPL or the LGPL.
40 * ***** END LICENSE BLOCK ***** */
42 #include "nsDBusService.h"
43 #include "nsComponentManagerUtils.h"
46 #include <dbus/dbus-glib-lowlevel.h>
47 #include <dbus/dbus-glib.h>
49 nsDBusService::nsDBusService() {
51 mSingleClient
= nsnull
;
54 nsDBusService::~nsDBusService() {
55 NS_ASSERTION(!mSingleClient
, "Client failed to unregister");
57 if (mReconnectTimer
) {
58 mReconnectTimer
->Cancel();
63 NS_IMPL_ISUPPORTS1(nsDBusService
, nsDBusService
)
64 NS_DEFINE_STATIC_IID_ACCESSOR(nsDBusService
, NS_DBUS_IID
)
66 nsDBusService
* nsDBusService::gSingleton
= nsnull
;
68 already_AddRefed
<nsDBusService
>
69 nsDBusService::Get() {
71 gSingleton
= new nsDBusService();
73 NS_IF_ADDREF(gSingleton
);
78 nsDBusService::AddClient(DBusClient
* client
) {
79 NS_ASSERTION(!mSingleClient
, "Only one client supported right now");
80 mSingleClient
= client
;
81 nsresult rv
= CreateConnection();
83 mSingleClient
= nsnull
;
89 nsDBusService::RemoveClient(DBusClient
* client
) {
90 NS_ASSERTION(mSingleClient
== client
, "Removing wrong client");
91 mSingleClient
= nsnull
;
95 nsDBusService::SendWithReply(DBusClient
* client
, DBusMessage
* message
) {
96 DBusPendingCall
* reply
= nsnull
;
98 if (!dbus_connection_send_with_reply(mConnection
, message
, &reply
, -1)) {
102 dbus_message_unref(message
);
106 PRBool
nsDBusService::HandleMessage(DBusMessage
* message
) {
107 if (dbus_message_is_signal(message
, DBUS_INTERFACE_LOCAL
,
109 HandleDBusDisconnect();
113 return mSingleClient
&& mSingleClient
->HandleMessage(message
);
116 static DBusHandlerResult
dbus_filter(DBusConnection
* connection
,
117 DBusMessage
* message
,
119 return static_cast<nsDBusService
*>(user_data
)->HandleMessage(message
)
120 ? DBUS_HANDLER_RESULT_HANDLED
: DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
123 void nsDBusService::DoTimerCallback(nsITimer
*aTimer
) {
124 if (aTimer
== mReconnectTimer
.get()) {
125 nsresult rv
= CreateConnection();
126 if (NS_SUCCEEDED(rv
)) {
127 mReconnectTimer
->Cancel();
128 mReconnectTimer
= nsnull
;
133 static void TimerCallback(nsITimer
*aTimer
, void *aClosure
) {
134 static_cast<nsDBusService
*>(aClosure
)->DoTimerCallback(aTimer
);
137 void nsDBusService::DropConnection() {
140 mSingleClient
->UnregisterWithConnection(mConnection
);
142 dbus_connection_unref(mConnection
);
143 mConnection
= nsnull
;
147 void nsDBusService::HandleDBusDisconnect() {
151 mReconnectTimer
= do_CreateInstance("@mozilla.org/timer;1", &rv
);
154 rv
= mReconnectTimer
->InitWithFuncCallback(TimerCallback
, this,
155 5000, nsITimer::TYPE_REPEATING_SLACK
);
157 mReconnectTimer
= nsnull
;
162 nsresult
nsDBusService::CreateConnection() {
163 mConnection
= dbus_bus_get(DBUS_BUS_SYSTEM
, NULL
);
165 return NS_ERROR_FAILURE
;
167 dbus_connection_set_exit_on_disconnect(mConnection
, PR_FALSE
);
168 dbus_connection_setup_with_g_main(mConnection
, NULL
);
170 if (!dbus_connection_add_filter(mConnection
, dbus_filter
, this, NULL
))
171 return NS_ERROR_FAILURE
;
173 mSingleClient
->RegisterWithConnection(mConnection
);