Bug 452317 - FeedConverter.js: QueryInterface should throw NS_ERROR_NO_INTERFACE...
[wine-gecko.git] / toolkit / system / dbus / nsDBusService.cpp
blob200400bb88a087198113b78055c9f3dfcc95f039
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
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)
26 * Contributor(s):
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"
45 #include <glib.h>
46 #include <dbus/dbus-glib-lowlevel.h>
47 #include <dbus/dbus-glib.h>
49 nsDBusService::nsDBusService() {
50 mConnection = nsnull;
51 mSingleClient = nsnull;
54 nsDBusService::~nsDBusService() {
55 NS_ASSERTION(!mSingleClient, "Client failed to unregister");
56 DropConnection();
57 if (mReconnectTimer) {
58 mReconnectTimer->Cancel();
60 gSingleton = nsnull;
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() {
70 if (!gSingleton) {
71 gSingleton = new nsDBusService();
73 NS_IF_ADDREF(gSingleton);
74 return gSingleton;
77 nsresult
78 nsDBusService::AddClient(DBusClient* client) {
79 NS_ASSERTION(!mSingleClient, "Only one client supported right now");
80 mSingleClient = client;
81 nsresult rv = CreateConnection();
82 if (NS_FAILED(rv)) {
83 mSingleClient = nsnull;
85 return rv;
88 void
89 nsDBusService::RemoveClient(DBusClient* client) {
90 NS_ASSERTION(mSingleClient == client, "Removing wrong client");
91 mSingleClient = nsnull;
94 DBusPendingCall*
95 nsDBusService::SendWithReply(DBusClient* client, DBusMessage* message) {
96 DBusPendingCall* reply = nsnull;
97 if (mConnection) {
98 if (!dbus_connection_send_with_reply(mConnection, message, &reply, -1)) {
99 reply = nsnull;
102 dbus_message_unref(message);
103 return reply;
106 PRBool nsDBusService::HandleMessage(DBusMessage* message) {
107 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL,
108 "Disconnected")) {
109 HandleDBusDisconnect();
110 return PR_FALSE;
113 return mSingleClient && mSingleClient->HandleMessage(message);
116 static DBusHandlerResult dbus_filter(DBusConnection* connection,
117 DBusMessage* message,
118 void* user_data) {
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() {
138 if (mConnection) {
139 if (mSingleClient) {
140 mSingleClient->UnregisterWithConnection(mConnection);
142 dbus_connection_unref(mConnection);
143 mConnection = nsnull;
147 void nsDBusService::HandleDBusDisconnect() {
148 DropConnection();
150 nsresult rv;
151 mReconnectTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
152 if (NS_FAILED(rv))
153 return;
154 rv = mReconnectTimer->InitWithFuncCallback(TimerCallback, this,
155 5000, nsITimer::TYPE_REPEATING_SLACK);
156 if (NS_FAILED(rv)) {
157 mReconnectTimer = nsnull;
158 return;
162 nsresult nsDBusService::CreateConnection() {
163 mConnection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
164 if (!mConnection)
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);
174 return NS_OK;