|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* vim:expandtab:shiftwidth=4:tabstop=4: |
|
3 */ |
|
4 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
5 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
7 |
|
8 #include "nsDBusService.h" |
|
9 #include "nsComponentManagerUtils.h" |
|
10 #include "nsAutoPtr.h" |
|
11 |
|
12 #include <glib.h> |
|
13 #include <dbus/dbus-glib-lowlevel.h> |
|
14 #include <dbus/dbus-glib.h> |
|
15 |
|
16 nsDBusService::nsDBusService() { |
|
17 mConnection = nullptr; |
|
18 mSingleClient = nullptr; |
|
19 } |
|
20 |
|
21 nsDBusService::~nsDBusService() { |
|
22 NS_ASSERTION(!mSingleClient, "Client failed to unregister"); |
|
23 DropConnection(); |
|
24 if (mReconnectTimer) { |
|
25 mReconnectTimer->Cancel(); |
|
26 } |
|
27 gSingleton = nullptr; |
|
28 } |
|
29 |
|
30 NS_IMPL_ISUPPORTS(nsDBusService, nsDBusService) |
|
31 |
|
32 nsDBusService* nsDBusService::gSingleton = nullptr; |
|
33 |
|
34 already_AddRefed<nsDBusService> |
|
35 nsDBusService::Get() { |
|
36 if (!gSingleton) { |
|
37 gSingleton = new nsDBusService(); |
|
38 } |
|
39 nsRefPtr<nsDBusService> ret = gSingleton; |
|
40 return ret.forget(); |
|
41 } |
|
42 |
|
43 nsresult |
|
44 nsDBusService::AddClient(DBusClient* client) { |
|
45 NS_ASSERTION(!mSingleClient, "Only one client supported right now"); |
|
46 mSingleClient = client; |
|
47 nsresult rv = CreateConnection(); |
|
48 if (NS_FAILED(rv)) { |
|
49 mSingleClient = nullptr; |
|
50 } |
|
51 return rv; |
|
52 } |
|
53 |
|
54 void |
|
55 nsDBusService::RemoveClient(DBusClient* client) { |
|
56 NS_ASSERTION(mSingleClient == client, "Removing wrong client"); |
|
57 mSingleClient = nullptr; |
|
58 } |
|
59 |
|
60 DBusPendingCall* |
|
61 nsDBusService::SendWithReply(DBusClient* client, DBusMessage* message) { |
|
62 DBusPendingCall* reply = nullptr; |
|
63 if (mConnection) { |
|
64 if (!dbus_connection_send_with_reply(mConnection, message, &reply, -1)) { |
|
65 reply = nullptr; |
|
66 } |
|
67 } |
|
68 dbus_message_unref(message); |
|
69 return reply; |
|
70 } |
|
71 |
|
72 bool nsDBusService::HandleMessage(DBusMessage* message) { |
|
73 if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, |
|
74 "Disconnected")) { |
|
75 HandleDBusDisconnect(); |
|
76 return false; |
|
77 } |
|
78 |
|
79 return mSingleClient && mSingleClient->HandleMessage(message); |
|
80 } |
|
81 |
|
82 static DBusHandlerResult dbus_filter(DBusConnection* connection, |
|
83 DBusMessage* message, |
|
84 void* user_data) { |
|
85 return static_cast<nsDBusService*>(user_data)->HandleMessage(message) |
|
86 ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
|
87 } |
|
88 |
|
89 void nsDBusService::DoTimerCallback(nsITimer *aTimer) { |
|
90 if (aTimer == mReconnectTimer.get()) { |
|
91 nsresult rv = CreateConnection(); |
|
92 if (NS_SUCCEEDED(rv)) { |
|
93 mReconnectTimer->Cancel(); |
|
94 mReconnectTimer = nullptr; |
|
95 } |
|
96 } |
|
97 } |
|
98 |
|
99 static void TimerCallback(nsITimer *aTimer, void *aClosure) { |
|
100 static_cast<nsDBusService*>(aClosure)->DoTimerCallback(aTimer); |
|
101 } |
|
102 |
|
103 void nsDBusService::DropConnection() { |
|
104 if (mConnection) { |
|
105 dbus_connection_remove_filter(mConnection, dbus_filter, this); |
|
106 if (mSingleClient) { |
|
107 mSingleClient->UnregisterWithConnection(mConnection); |
|
108 } |
|
109 dbus_connection_unref(mConnection); |
|
110 mConnection = nullptr; |
|
111 } |
|
112 } |
|
113 |
|
114 void nsDBusService::HandleDBusDisconnect() { |
|
115 DropConnection(); |
|
116 |
|
117 nsresult rv; |
|
118 mReconnectTimer = do_CreateInstance("@mozilla.org/timer;1", &rv); |
|
119 if (NS_FAILED(rv)) |
|
120 return; |
|
121 rv = mReconnectTimer->InitWithFuncCallback(TimerCallback, this, |
|
122 5000, nsITimer::TYPE_REPEATING_SLACK); |
|
123 if (NS_FAILED(rv)) { |
|
124 mReconnectTimer = nullptr; |
|
125 return; |
|
126 } |
|
127 } |
|
128 |
|
129 nsresult nsDBusService::CreateConnection() { |
|
130 mConnection = dbus_bus_get(DBUS_BUS_SYSTEM, nullptr); |
|
131 if (!mConnection) |
|
132 return NS_ERROR_FAILURE; |
|
133 |
|
134 dbus_connection_set_exit_on_disconnect(mConnection, false); |
|
135 dbus_connection_setup_with_g_main(mConnection, nullptr); |
|
136 |
|
137 if (!dbus_connection_add_filter(mConnection, dbus_filter, this, nullptr)) |
|
138 return NS_ERROR_FAILURE; |
|
139 |
|
140 mSingleClient->RegisterWithConnection(mConnection); |
|
141 return NS_OK; |
|
142 } |