sbDBus.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2010 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
27 //------------------------------------------------------------------------------
28 //------------------------------------------------------------------------------
29 //
30 // Songbird D-Bus services.
31 //
32 //------------------------------------------------------------------------------
33 //------------------------------------------------------------------------------
34 
40 //------------------------------------------------------------------------------
41 //
42 // Songbird D-Bus imported services.
43 //
44 //------------------------------------------------------------------------------
45 
46 // Self imports.
47 #include "sbDBus.h"
48 
49 // Mozilla imports.
50 #include <nsAutoPtr.h>
51 
52 
53 //------------------------------------------------------------------------------
54 //
55 // Songbird D-Bus message services.
56 //
57 //------------------------------------------------------------------------------
58 
59 //-------------------------------------
60 //
61 // NewMethodCall
62 //
63 
64 /* static */ nsresult
66  const char* aDestination,
67  const char* aPath,
68  const char* aInterface,
69  const char* aMethod)
70 {
71  // Validate arguments.
72  NS_ENSURE_ARG_POINTER(aMessage);
73 
74  // Create a Songbird D-Bus message.
75  nsAutoPtr<sbDBusMessage> message(new sbDBusMessage());
76  NS_ENSURE_TRUE(message, NS_ERROR_OUT_OF_MEMORY);
77 
78  // Create a D-Bus method call message.
79  DBusMessage* baseMessage = dbus_message_new_method_call(aDestination,
80  aPath,
81  aInterface,
82  aMethod);
83  NS_ENSURE_TRUE(baseMessage, NS_ERROR_OUT_OF_MEMORY);
84  message->Assign(baseMessage);
85 
86  // Return results.
87  *aMessage = message.forget();
88 
89  return NS_OK;
90 }
91 
92 
93 //-------------------------------------
94 //
95 // Assign
96 //
97 
98 void
99 sbDBusMessage::Assign(DBusMessage* aMessage)
100 {
101  // Unreference current base message.
102  if (mBaseMessage)
103  dbus_message_unref(mBaseMessage);
104 
105  // Assign new, already ref'ed base message.
106  mBaseMessage = aMessage;
107 }
108 
109 
110 //-------------------------------------
111 //
112 // GetArgs
113 //
114 
115 nsresult
116 sbDBusMessage::GetArgs(int aFirstArgType,
117  ...)
118 {
119  sbDBusError error;
120 
121  // Get the message arguments.
122  va_list varArgs;
123  va_start(varArgs, aFirstArgType);
124  dbus_message_get_args_valist(mBaseMessage, &error, aFirstArgType, varArgs);
125  va_end(varArgs);
126  SB_DBUS_ENSURE_SUCCESS(error, NS_ERROR_FAILURE);
127 
128  return NS_OK;
129 }
130 
131 
132 //-------------------------------------
133 //
134 // ~sbDBusMessage
135 //
136 
138 {
139  // Unreference base message.
140  if (mBaseMessage)
141  dbus_message_unref(mBaseMessage);
142 }
143 
144 
145 //------------------------------------------------------------------------------
146 //
147 // Songbird D-Bus connection services.
148 //
149 //------------------------------------------------------------------------------
150 
151 //-------------------------------------
152 //
153 // New
154 //
155 
156 /* static */ nsresult
158  DBusBusType aBusType,
159  const char* aDestination,
160  const char* aPath,
161  const char* aInterface)
162 {
163  // Validate arguments.
164  NS_ENSURE_ARG_POINTER(aConnection);
165 
166  // Function variables.
167  nsresult rv;
168 
169  // Create and initialize a new connection.
170  nsAutoPtr<sbDBusConnection> connection(new sbDBusConnection());
171  NS_ENSURE_TRUE(connection, NS_ERROR_OUT_OF_MEMORY);
172  rv = connection->Initialize(aBusType, aDestination, aPath, aInterface);
173  NS_ENSURE_SUCCESS(rv, rv);
174 
175  // Return results.
176  *aConnection = connection.forget();
177 
178  return NS_OK;
179 }
180 
181 
182 //-------------------------------------
183 //
184 // Initialize
185 //
186 
187 nsresult
188 sbDBusConnection::Initialize(DBusBusType aBusType,
189  const char* aDestination,
190  const char* aPath,
191  const char* aInterface)
192 {
193  sbDBusError error;
194 
195  // Get a bus connection.
196  mBaseConnection = dbus_bus_get(aBusType, &error);
197  SB_DBUS_ENSURE_SUCCESS(error, NS_ERROR_FAILURE);
198 
199  // Set connection info.
200  if (aDestination)
201  mDestination.Assign(aDestination);
202  if (aPath)
203  mPath.Assign(aPath);
204  if (aInterface)
205  mInterface.Assign(aInterface);
206 
207  return NS_OK;
208 }
209 
210 
211 //-------------------------------------
212 //
213 // InvokeMethod
214 //
215 
216 nsresult
217 sbDBusConnection::InvokeMethod(const char* aMethod,
218  sbDBusMessage** aReply,
219  int aFirstArgType,
220  ...)
221 {
222  // Validate arguments.
223  NS_ENSURE_ARG_POINTER(aMethod);
224 
225  // Function variables.
226  dbus_bool_t success;
227  sbDBusError error;
228  nsresult rv;
229 
230  // Create a new D-Bus method call message.
231  nsAutoPtr<sbDBusMessage> dBusMessage;
232  rv = sbDBusMessage::NewMethodCall(getter_Transfers(dBusMessage),
233  mDestination.get(),
234  mPath.get(),
235  mInterface.get(),
236  aMethod);
237  NS_ENSURE_SUCCESS(rv, rv);
238 
239  // Automatically start message destination.
240  dbus_message_set_auto_start(dBusMessage->get(), TRUE);
241 
242  // Add arguments to message.
243  if (aFirstArgType != DBUS_TYPE_INVALID) {
244  va_list varArgs;
245  va_start(varArgs, aFirstArgType);
246  success = dbus_message_append_args_valist(dBusMessage->get(),
247  aFirstArgType,
248  varArgs);
249  va_end(varArgs);
250  NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
251  }
252 
253  // Send message and get reply if requested.
254  if (aReply) {
255  // Create a Songbird D-Bus message for the reply.
256  nsAutoPtr<sbDBusMessage> reply(new sbDBusMessage());
257  NS_ENSURE_TRUE(reply, NS_ERROR_OUT_OF_MEMORY);
258 
259  // Send the message and wait for the reply.
260  DBusMessage* baseReply;
261  baseReply = dbus_connection_send_with_reply_and_block(mBaseConnection,
262  dBusMessage->get(),
263  sMethodCallTimeoutMS,
264  &error);
265  SB_DBUS_ENSURE_SUCCESS(error, NS_ERROR_FAILURE);
266  reply->Assign(baseReply);
267 
268  // Return results.
269  *aReply = reply.forget();
270  }
271  else {
272  // Send the message.
273  success = dbus_connection_send(mBaseConnection, dBusMessage->get(), NULL);
274  NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
275  }
276 
277  return NS_OK;
278 }
279 
280 
281 //-------------------------------------
282 //
283 // sbDBusConnection
284 //
285 
286 sbDBusConnection::sbDBusConnection() :
287  mBaseConnection(NULL)
288 {
289 }
290 
291 
292 //-------------------------------------
293 //
294 // ~sbDBusConnection
295 //
296 
298 {
299  // Unreference the base D-Bus connection.
300  if (mBaseConnection)
301  dbus_connection_unref(mBaseConnection);
302 }
303 
304 
305 //------------------------------------------------------------------------------
306 //
307 // Songbird D-Bus error services.
308 //
309 //------------------------------------------------------------------------------
310 
311 //-------------------------------------
312 //
313 // sbDBusError
314 //
315 
317 {
318  // Initialize the D-Bus error.
319  dbus_error_init(this);
320 }
321 
322 
323 //-------------------------------------
324 //
325 // ~sbDBusError
326 //
327 
329 {
330  // Free the D-Bus error.
331  dbus_error_free(this);
332 }
333 
nsresult GetArgs(int aFirstArgType,...)
Definition: sbDBus.cpp:116
static nsresult New(sbDBusConnection **aConnection, DBusBusType aBusType, const char *aDestination, const char *aPath, const char *aInterface)
Definition: sbDBus.cpp:157
return NS_OK
_updateCookies aPath
nsresult InvokeMethod(const char *aMethod, sbDBusMessage **aReply, int aFirstArgType,...)
Definition: sbDBus.cpp:217
static nsresult NewMethodCall(sbDBusMessage **aMessage, const char *aDestination, const char *aPath, const char *aInterface, const char *aMethod)
Definition: sbDBus.cpp:65
virtual ~sbDBusError()
Definition: sbDBus.cpp:328
sbDBusMessage(DBusMessage *aMessage=NULL)
Definition: sbDBus.h:127
GstMessage * message
Songbird D-Bus Service Definitions.
#define SB_DBUS_ENSURE_SUCCESS(aError, aReturnValue)
Definition: sbDBus.h:326
void Assign(DBusMessage *aMessage)
Definition: sbDBus.cpp:99
virtual ~sbDBusMessage()
Definition: sbDBus.cpp:137
virtual ~sbDBusConnection()
Definition: sbDBus.cpp:297