1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* vim:expandtab:shiftwidth=4:tabstop=4:
3 : */
4 : /* ***** BEGIN LICENSE BLOCK *****
5 : * Version: NPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : *
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/
12 : *
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.
17 : *
18 : * The Original Code is Novell code.
19 : *
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.
23 : *
24 : * Original Author: Robert O'Callahan (rocallahan@novell.com)
25 : *
26 : * Contributor(s):
27 : *
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.
39 : *
40 : * ***** END LICENSE BLOCK ***** */
41 :
42 : #include "nsDBusService.h"
43 : #include "nsComponentManagerUtils.h"
44 :
45 : #include <glib.h>
46 : #include <dbus/dbus-glib-lowlevel.h>
47 : #include <dbus/dbus-glib.h>
48 :
49 1386 : nsDBusService::nsDBusService() {
50 1386 : mConnection = nsnull;
51 1386 : mSingleClient = nsnull;
52 1386 : }
53 :
54 4149 : nsDBusService::~nsDBusService() {
55 1383 : NS_ASSERTION(!mSingleClient, "Client failed to unregister");
56 1383 : DropConnection();
57 1383 : if (mReconnectTimer) {
58 0 : mReconnectTimer->Cancel();
59 : }
60 1383 : gSingleton = nsnull;
61 5532 : }
62 :
63 6927 : NS_IMPL_ISUPPORTS1(nsDBusService, nsDBusService)
64 : NS_DEFINE_STATIC_IID_ACCESSOR(nsDBusService, NS_DBUS_IID)
65 :
66 : nsDBusService* nsDBusService::gSingleton = nsnull;
67 :
68 : already_AddRefed<nsDBusService>
69 1386 : nsDBusService::Get() {
70 1386 : if (!gSingleton) {
71 1386 : gSingleton = new nsDBusService();
72 : }
73 1386 : NS_IF_ADDREF(gSingleton);
74 1386 : return gSingleton;
75 : }
76 :
77 : nsresult
78 1386 : nsDBusService::AddClient(DBusClient* client) {
79 1386 : NS_ASSERTION(!mSingleClient, "Only one client supported right now");
80 1386 : mSingleClient = client;
81 1386 : nsresult rv = CreateConnection();
82 1386 : if (NS_FAILED(rv)) {
83 0 : mSingleClient = nsnull;
84 : }
85 1386 : return rv;
86 : }
87 :
88 : void
89 1383 : nsDBusService::RemoveClient(DBusClient* client) {
90 1383 : NS_ASSERTION(mSingleClient == client, "Removing wrong client");
91 1383 : mSingleClient = nsnull;
92 1383 : }
93 :
94 : DBusPendingCall*
95 1386 : nsDBusService::SendWithReply(DBusClient* client, DBusMessage* message) {
96 1386 : DBusPendingCall* reply = nsnull;
97 1386 : if (mConnection) {
98 1386 : if (!dbus_connection_send_with_reply(mConnection, message, &reply, -1)) {
99 0 : reply = nsnull;
100 : }
101 : }
102 1386 : dbus_message_unref(message);
103 1386 : return reply;
104 : }
105 :
106 1386 : bool nsDBusService::HandleMessage(DBusMessage* message) {
107 1386 : if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL,
108 1386 : "Disconnected")) {
109 0 : HandleDBusDisconnect();
110 0 : return false;
111 : }
112 :
113 1386 : return mSingleClient && mSingleClient->HandleMessage(message);
114 : }
115 :
116 1386 : static DBusHandlerResult dbus_filter(DBusConnection* connection,
117 : DBusMessage* message,
118 : void* user_data) {
119 1386 : return static_cast<nsDBusService*>(user_data)->HandleMessage(message)
120 1386 : ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
121 : }
122 :
123 0 : void nsDBusService::DoTimerCallback(nsITimer *aTimer) {
124 0 : if (aTimer == mReconnectTimer.get()) {
125 0 : nsresult rv = CreateConnection();
126 0 : if (NS_SUCCEEDED(rv)) {
127 0 : mReconnectTimer->Cancel();
128 0 : mReconnectTimer = nsnull;
129 : }
130 : }
131 0 : }
132 :
133 0 : static void TimerCallback(nsITimer *aTimer, void *aClosure) {
134 0 : static_cast<nsDBusService*>(aClosure)->DoTimerCallback(aTimer);
135 0 : }
136 :
137 1383 : void nsDBusService::DropConnection() {
138 1383 : if (mConnection) {
139 1383 : dbus_connection_remove_filter(mConnection, dbus_filter, this);
140 1383 : if (mSingleClient) {
141 0 : mSingleClient->UnregisterWithConnection(mConnection);
142 : }
143 1383 : dbus_connection_unref(mConnection);
144 1383 : mConnection = nsnull;
145 : }
146 1383 : }
147 :
148 0 : void nsDBusService::HandleDBusDisconnect() {
149 0 : DropConnection();
150 :
151 : nsresult rv;
152 0 : mReconnectTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
153 0 : if (NS_FAILED(rv))
154 0 : return;
155 0 : rv = mReconnectTimer->InitWithFuncCallback(TimerCallback, this,
156 0 : 5000, nsITimer::TYPE_REPEATING_SLACK);
157 0 : if (NS_FAILED(rv)) {
158 0 : mReconnectTimer = nsnull;
159 0 : return;
160 : }
161 : }
162 :
163 1386 : nsresult nsDBusService::CreateConnection() {
164 1386 : mConnection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
165 1386 : if (!mConnection)
166 0 : return NS_ERROR_FAILURE;
167 :
168 1386 : dbus_connection_set_exit_on_disconnect(mConnection, false);
169 1386 : dbus_connection_setup_with_g_main(mConnection, NULL);
170 :
171 1386 : if (!dbus_connection_add_filter(mConnection, dbus_filter, this, NULL))
172 0 : return NS_ERROR_FAILURE;
173 :
174 1386 : mSingleClient->RegisterWithConnection(mConnection);
175 1386 : return NS_OK;
176 : }
|