]> code.delx.au - pulseaudio/blob - src/pulsecore/protocol-dbus.c
dbus-protocol: Implement extension registration.
[pulseaudio] / src / pulsecore / protocol-dbus.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2009 Tanu Kaskinen
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <dbus/dbus.h>
27
28 #include <pulse/xmalloc.h>
29
30 #include <pulsecore/core-util.h>
31 #include <pulsecore/hashmap.h>
32 #include <pulsecore/idxset.h>
33 #include <pulsecore/shared.h>
34 #include <pulsecore/strbuf.h>
35
36 #include "protocol-dbus.h"
37
38 struct pa_dbus_protocol {
39 PA_REFCNT_DECLARE;
40
41 pa_core *core;
42 pa_hashmap *objects; /* Object path -> struct object_entry */
43 pa_hashmap *connections; /* DBusConnection -> struct connection_entry */
44 pa_idxset *extensions; /* Strings */
45
46 pa_hook hooks[PA_DBUS_PROTOCOL_HOOK_MAX];
47 };
48
49 struct object_entry {
50 char *path;
51 pa_hashmap *interfaces; /* Interface name -> struct interface_entry */
52 char *introspection;
53 };
54
55 struct connection_entry {
56 DBusConnection *connection;
57 pa_client *client;
58
59 pa_bool_t listening_for_all_signals;
60
61 /* Contains object paths. If this is empty, then signals from all objects
62 * are accepted. Only used when listening_for_all_signals == TRUE. */
63 pa_idxset *all_signals_objects;
64
65 /* Signal name -> idxset. The idxsets contain object paths. If an idxset is
66 * empty, then that signal is accepted from all objects. Only used when
67 * listening_for_all_signals == FALSE. */
68 pa_hashmap *listening_signals;
69 };
70
71 struct interface_entry {
72 char *name;
73 pa_hashmap *method_handlers;
74 pa_hashmap *property_handlers;
75 pa_dbus_receive_cb_t get_all_properties_cb;
76 pa_dbus_signal_info *signals;
77 unsigned n_signals;
78 void *userdata;
79 };
80
81 char *pa_get_dbus_address_from_server_type(pa_server_type_t server_type) {
82 char *address = NULL;
83 char *runtime_path = NULL;
84 char *escaped_path = NULL;
85
86 switch (server_type) {
87 case PA_SERVER_TYPE_USER:
88 pa_assert_se((runtime_path = pa_runtime_path(PA_DBUS_SOCKET_NAME)));
89 pa_assert_se((escaped_path = dbus_address_escape_value(runtime_path)));
90 address = pa_sprintf_malloc("unix:path=%s", escaped_path);
91 break;
92
93 case PA_SERVER_TYPE_SYSTEM:
94 pa_assert_se((escaped_path = dbus_address_escape_value(PA_DBUS_SYSTEM_SOCKET_PATH)));
95 address = pa_sprintf_malloc("unix:path=%s", escaped_path);
96 break;
97
98 case PA_SERVER_TYPE_NONE:
99 address = pa_xnew0(char, 1);
100 break;
101
102 default:
103 pa_assert_not_reached();
104 }
105
106 pa_xfree(runtime_path);
107 pa_xfree(escaped_path);
108
109 return address;
110 }
111
112 static pa_dbus_protocol *dbus_protocol_new(pa_core *c) {
113 pa_dbus_protocol *p;
114 unsigned i;
115
116 pa_assert(c);
117
118 p = pa_xnew(pa_dbus_protocol, 1);
119 PA_REFCNT_INIT(p);
120 p->core = c;
121 p->objects = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
122 p->connections = pa_hashmap_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
123 p->extensions = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
124
125 for (i = 0; i < PA_DBUS_PROTOCOL_HOOK_MAX; ++i)
126 pa_hook_init(&p->hooks[i], p);
127
128 pa_assert_se(pa_shared_set(c, "dbus-protocol", p) >= 0);
129
130 return p;
131 }
132
133 pa_dbus_protocol* pa_dbus_protocol_get(pa_core *c) {
134 pa_dbus_protocol *p;
135
136 if ((p = pa_shared_get(c, "dbus-protocol")))
137 return pa_dbus_protocol_ref(p);
138
139 return dbus_protocol_new(c);
140 }
141
142 pa_dbus_protocol* pa_dbus_protocol_ref(pa_dbus_protocol *p) {
143 pa_assert(p);
144 pa_assert(PA_REFCNT_VALUE(p) >= 1);
145
146 PA_REFCNT_INC(p);
147
148 return p;
149 }
150
151 void pa_dbus_protocol_unref(pa_dbus_protocol *p) {
152 unsigned i;
153
154 pa_assert(p);
155 pa_assert(PA_REFCNT_VALUE(p) >= 1);
156
157 if (PA_REFCNT_DEC(p) > 0)
158 return;
159
160 pa_assert(pa_hashmap_isempty(p->objects));
161 pa_assert(pa_hashmap_isempty(p->connections));
162 pa_assert(pa_idxset_isempty(p->extensions));
163
164 pa_hashmap_free(p->objects, NULL, NULL);
165 pa_hashmap_free(p->connections, NULL, NULL);
166 pa_idxset_free(p->extensions, NULL, NULL);
167
168 for (i = 0; i < PA_DBUS_PROTOCOL_HOOK_MAX; ++i)
169 pa_hook_done(&p->hooks[i]);
170
171 pa_assert_se(pa_shared_remove(p->core, "dbus-protocol") >= 0);
172
173 pa_xfree(p);
174 }
175
176 static void update_introspection(struct object_entry *oe) {
177 pa_strbuf *buf;
178 void *interfaces_state = NULL;
179 struct interface_entry *iface_entry = NULL;
180
181 pa_assert(oe);
182
183 buf = pa_strbuf_new();
184 pa_strbuf_puts(buf, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
185 pa_strbuf_puts(buf, "<node>\n");
186
187 while ((iface_entry = pa_hashmap_iterate(oe->interfaces, &interfaces_state, NULL))) {
188 pa_dbus_method_handler *method_handler;
189 pa_dbus_property_handler *property_handler;
190 void *handlers_state = NULL;
191 unsigned i;
192 unsigned j;
193
194 pa_strbuf_printf(buf, " <interface name=\"%s\">\n", iface_entry->name);
195
196 while ((method_handler = pa_hashmap_iterate(iface_entry->method_handlers, &handlers_state, NULL))) {
197 pa_strbuf_printf(buf, " <method name=\"%s\">\n", method_handler->method_name);
198
199 for (i = 0; i < method_handler->n_arguments; ++i)
200 pa_strbuf_printf(buf, " <arg name=\"%s\" type=\"%s\" direction=\"%s\"/>\n", method_handler->arguments[i].name,
201 method_handler->arguments[i].type,
202 method_handler->arguments[i].direction);
203
204 pa_strbuf_puts(buf, " </method>\n");
205 }
206
207 handlers_state = NULL;
208
209 while ((property_handler = pa_hashmap_iterate(iface_entry->property_handlers, &handlers_state, NULL)))
210 pa_strbuf_printf(buf, " <property name=\"%s\" type=\"%s\" access=\"%s\"/>\n", property_handler->property_name,
211 property_handler->type,
212 property_handler->get_cb ? (property_handler->set_cb ? "readwrite" : "read") : "write");
213
214 for (i = 0; i < iface_entry->n_signals; ++i) {
215 pa_strbuf_printf(buf, " <signal name=\"%s\">\n", iface_entry->signals[i].name);
216
217 for (j = 0; j < iface_entry->signals[i].n_arguments; ++j)
218 pa_strbuf_printf(buf, " <arg name=\"%s\" type=\"%s\"/>\n", iface_entry->signals[i].arguments[j].name,
219 iface_entry->signals[i].arguments[j].type);
220
221 pa_strbuf_puts(buf, " </signal>\n");
222 }
223
224 pa_strbuf_puts(buf, " </interface>\n");
225 }
226
227 pa_strbuf_puts(buf, " <interface name=\"" DBUS_INTERFACE_INTROSPECTABLE "\">\n"
228 " <method name=\"Introspect\">\n"
229 " <arg name=\"data\" type=\"s\" direction=\"out\"/>\n"
230 " </method>\n"
231 " </interface>\n"
232 " <interface name=\"" DBUS_INTERFACE_PROPERTIES "\">\n"
233 " <method name=\"Get\">\n"
234 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
235 " <arg name=\"property_name\" type=\"s\" direction=\"in\"/>\n"
236 " <arg name=\"value\" type=\"v\" direction=\"out\"/>\n"
237 " </method>\n"
238 " <method name=\"Set\">\n"
239 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
240 " <arg name=\"property_name\" type=\"s\" direction=\"in\"/>\n"
241 " <arg name=\"value\" type=\"v\" direction=\"in\"/>\n"
242 " </method>\n"
243 " <method name=\"GetAll\">\n"
244 " <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
245 " <arg name=\"props\" type=\"a{sv}\" direction=\"out\"/>\n"
246 " </method>\n"
247 " </interface>\n");
248
249 pa_strbuf_puts(buf, "</node>\n");
250
251 pa_xfree(oe->introspection);
252 oe->introspection = pa_strbuf_tostring_free(buf);
253 }
254
255 enum find_result_t {
256 FOUND_METHOD,
257 FOUND_GET_PROPERTY,
258 FOUND_SET_PROPERTY,
259 FOUND_GET_ALL,
260 PROPERTY_ACCESS_DENIED,
261 NO_SUCH_METHOD,
262 NO_SUCH_PROPERTY,
263 INVALID_MESSAGE_ARGUMENTS
264 };
265
266 static enum find_result_t find_handler_by_property(struct object_entry *obj_entry,
267 DBusMessage *msg,
268 const char *property,
269 struct interface_entry **iface_entry,
270 pa_dbus_property_handler **property_handler) {
271 void *state = NULL;
272
273 pa_assert(obj_entry);
274 pa_assert(msg);
275 pa_assert(property);
276 pa_assert(iface_entry);
277 pa_assert(property_handler);
278
279 while ((*iface_entry = pa_hashmap_iterate(obj_entry->interfaces, &state, NULL))) {
280 if ((*property_handler = pa_hashmap_get((*iface_entry)->property_handlers, property))) {
281 if (dbus_message_has_member(msg, "Get"))
282 return (*property_handler)->get_cb ? FOUND_GET_PROPERTY : PROPERTY_ACCESS_DENIED;
283 else if (dbus_message_has_member(msg, "Set"))
284 return (*property_handler)->set_cb ? FOUND_SET_PROPERTY : PROPERTY_ACCESS_DENIED;
285 else
286 pa_assert_not_reached();
287 }
288 }
289
290 return NO_SUCH_PROPERTY;
291 }
292
293 static enum find_result_t find_handler_by_method(struct object_entry *obj_entry,
294 const char *method,
295 struct interface_entry **iface_entry,
296 pa_dbus_method_handler **method_handler) {
297 void *state = NULL;
298
299 pa_assert(obj_entry);
300 pa_assert(method);
301 pa_assert(iface_entry);
302 pa_assert(method_handler);
303
304 while ((*iface_entry = pa_hashmap_iterate(obj_entry->interfaces, &state, NULL))) {
305 if ((*method_handler = pa_hashmap_get((*iface_entry)->method_handlers, method)))
306 return FOUND_METHOD;
307 }
308
309 return NO_SUCH_METHOD;
310 }
311
312 static enum find_result_t find_handler_from_properties_call(struct object_entry *obj_entry,
313 DBusMessage *msg,
314 struct interface_entry **iface_entry,
315 pa_dbus_property_handler **property_handler,
316 const char **attempted_property) {
317 const char *interface;
318
319 pa_assert(obj_entry);
320 pa_assert(msg);
321 pa_assert(iface_entry);
322
323 if (dbus_message_has_member(msg, "GetAll")) {
324 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &interface, DBUS_TYPE_INVALID))
325 return INVALID_MESSAGE_ARGUMENTS;
326
327 if (*interface) {
328 if ((*iface_entry = pa_hashmap_get(obj_entry->interfaces, interface)))
329 return FOUND_GET_ALL;
330 else
331 return NO_SUCH_METHOD; /* XXX: NO_SUCH_INTERFACE or something like that might be more accurate. */
332 } else {
333 pa_assert_se((*iface_entry = pa_hashmap_first(obj_entry->interfaces)));
334 return FOUND_GET_ALL;
335 }
336 } else {
337 if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING, attempted_property, DBUS_TYPE_INVALID))
338 return INVALID_MESSAGE_ARGUMENTS;
339
340 if (*interface) {
341 if ((*iface_entry = pa_hashmap_get(obj_entry->interfaces, interface)) &&
342 (*property_handler = pa_hashmap_get((*iface_entry)->property_handlers, *attempted_property))) {
343 if (dbus_message_has_member(msg, "Get"))
344 return (*property_handler)->get_cb ? FOUND_GET_PROPERTY : PROPERTY_ACCESS_DENIED;
345 else if (dbus_message_has_member(msg, "Set"))
346 return (*property_handler)->set_cb ? FOUND_SET_PROPERTY : PROPERTY_ACCESS_DENIED;
347 else
348 pa_assert_not_reached();
349 } else
350 return NO_SUCH_PROPERTY;
351 } else
352 return find_handler_by_property(obj_entry, msg, *attempted_property, iface_entry, property_handler);
353 }
354 }
355
356 static enum find_result_t find_handler(struct object_entry *obj_entry,
357 DBusMessage *msg,
358 struct interface_entry **iface_entry,
359 pa_dbus_method_handler **method_handler,
360 pa_dbus_property_handler **property_handler,
361 const char **attempted_property) {
362 const char *interface;
363
364 pa_assert(obj_entry);
365 pa_assert(msg);
366 pa_assert(iface_entry);
367 pa_assert(method_handler);
368 pa_assert(property_handler);
369 pa_assert(attempted_property);
370
371 *iface_entry = NULL;
372 *method_handler = NULL;
373
374 if (dbus_message_has_interface(msg, DBUS_INTERFACE_PROPERTIES))
375 return find_handler_from_properties_call(obj_entry, msg, iface_entry, property_handler, attempted_property);
376
377 else if ((interface = dbus_message_get_interface(msg))) {
378 if ((*iface_entry = pa_hashmap_get(obj_entry->interfaces, interface)) &&
379 (*method_handler = pa_hashmap_get((*iface_entry)->method_handlers, dbus_message_get_member(msg))))
380 return FOUND_METHOD;
381 else
382 return NO_SUCH_METHOD;
383
384 } else { /* The method call doesn't contain an interface. */
385 if (dbus_message_has_member(msg, "Get") || dbus_message_has_member(msg, "Set") || dbus_message_has_member(msg, "GetAll")) {
386 if (find_handler_by_method(obj_entry, dbus_message_get_member(msg), iface_entry, method_handler) == FOUND_METHOD)
387 return FOUND_METHOD; /* The object has a method named Get, Set or GetAll in some other interface than .Properties. */
388 else
389 /* Assume this is a .Properties call. */
390 return find_handler_from_properties_call(obj_entry, msg, iface_entry, property_handler, attempted_property);
391
392 } else /* This is not a .Properties call. */
393 return find_handler_by_method(obj_entry, dbus_message_get_member(msg), iface_entry, method_handler);
394 }
395 }
396
397 static DBusHandlerResult handle_message_cb(DBusConnection *connection, DBusMessage *message, void *user_data) {
398 pa_dbus_protocol *p = user_data;
399 struct object_entry *obj_entry = NULL;
400 struct interface_entry *iface_entry = NULL;
401 pa_dbus_method_handler *method_handler = NULL;
402 pa_dbus_property_handler *property_handler = NULL;
403 const char *attempted_property = NULL;
404 DBusMessage *reply = NULL;
405
406 pa_assert(connection);
407 pa_assert(message);
408 pa_assert(p);
409 pa_assert(p->objects);
410
411 if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
412 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
413
414 pa_assert_se((obj_entry = pa_hashmap_get(p->objects, dbus_message_get_path(message))));
415
416 if (dbus_message_is_method_call(message, "org.freedesktop.DBus.Introspectable", "Introspect") ||
417 (!dbus_message_get_interface(message) && dbus_message_has_member(message, "Introspect"))) {
418 pa_assert_se((reply = dbus_message_new_method_return(message)));
419 pa_assert_se(dbus_message_append_args(reply, DBUS_TYPE_STRING, &obj_entry->introspection, DBUS_TYPE_INVALID));
420 pa_assert_se(dbus_connection_send(connection, reply, NULL));
421
422 pa_log_debug("%s.Introspect handled.", obj_entry->path);
423
424 goto finish;
425 }
426
427 switch (find_handler(obj_entry, message, &iface_entry, &method_handler, &property_handler, &attempted_property)) {
428 case FOUND_METHOD:
429 method_handler->receive_cb(connection, message, iface_entry->userdata);
430 break;
431
432 case FOUND_GET_PROPERTY:
433 property_handler->get_cb(connection, message, iface_entry->userdata);
434 break;
435
436 case FOUND_SET_PROPERTY:
437 property_handler->set_cb(connection, message, iface_entry->userdata);
438 break;
439
440 case FOUND_GET_ALL:
441 if (iface_entry->get_all_properties_cb)
442 iface_entry->get_all_properties_cb(connection, message, iface_entry->userdata);
443 break;
444
445 case PROPERTY_ACCESS_DENIED:
446 pa_assert_se((reply = dbus_message_new_error_printf(message, DBUS_ERROR_ACCESS_DENIED, "%s access denied for property %s", dbus_message_get_member(message), attempted_property)));
447 pa_assert_se(dbus_connection_send(connection, reply, NULL));
448 break;
449
450 case NO_SUCH_METHOD:
451 pa_assert_se((reply = dbus_message_new_error_printf(message, DBUS_ERROR_UNKNOWN_METHOD, "%s: No such method", dbus_message_get_member(message))));
452 pa_assert_se(dbus_connection_send(connection, reply, NULL));
453 break;
454
455 case NO_SUCH_PROPERTY:
456 pa_assert_se((reply = dbus_message_new_error_printf(message, PA_DBUS_ERROR_NO_SUCH_PROPERTY, "%s: No such property", attempted_property)));
457 pa_assert_se(dbus_connection_send(connection, reply, NULL));
458 break;
459
460 case INVALID_MESSAGE_ARGUMENTS:
461 pa_assert_se((reply = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS, "Invalid arguments for %s", dbus_message_get_member(message))));
462 pa_assert_se(dbus_connection_send(connection, reply, NULL));
463 break;
464
465 default:
466 pa_assert_not_reached();
467 }
468
469 finish:
470 if (reply)
471 dbus_message_unref(reply);
472
473 return DBUS_HANDLER_RESULT_HANDLED;
474 }
475
476 static DBusObjectPathVTable vtable = {
477 .unregister_function = NULL,
478 .message_function = handle_message_cb,
479 .dbus_internal_pad1 = NULL,
480 .dbus_internal_pad2 = NULL,
481 .dbus_internal_pad3 = NULL,
482 .dbus_internal_pad4 = NULL
483 };
484
485 static void register_object(pa_dbus_protocol *p, struct object_entry *obj_entry) {
486 struct connection_entry *conn_entry;
487 void *state = NULL;
488
489 pa_assert(p);
490 pa_assert(obj_entry);
491
492 while ((conn_entry = pa_hashmap_iterate(p->connections, &state, NULL)))
493 pa_assert_se(dbus_connection_register_object_path(conn_entry->connection, obj_entry->path, &vtable, p));
494 }
495
496 static pa_dbus_arg_info *copy_args(const pa_dbus_arg_info *src, unsigned n) {
497 pa_dbus_arg_info *dst;
498 unsigned i;
499
500 if (n == 0)
501 return NULL;
502
503 pa_assert(src);
504
505 dst = pa_xnew0(pa_dbus_arg_info, n);
506
507 for (i = 0; i < n; ++i) {
508 dst[i].name = pa_xstrdup(src[i].name);
509 dst[i].type = pa_xstrdup(src[i].type);
510 dst[i].direction = pa_xstrdup(src[i].direction);
511 }
512
513 return dst;
514 }
515
516 static pa_hashmap *create_method_handlers(const pa_dbus_interface_info *info) {
517 pa_hashmap *handlers;
518 unsigned i;
519
520 pa_assert(info);
521 pa_assert(info->method_handlers || info->n_method_handlers == 0);
522
523 handlers = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
524
525 for (i = 0; i < info->n_method_handlers; ++i) {
526 pa_dbus_method_handler *h = pa_xnew(pa_dbus_method_handler, 1);
527 h->method_name = pa_xstrdup(info->method_handlers[i].method_name);
528 h->arguments = copy_args(info->method_handlers[i].arguments, info->method_handlers[i].n_arguments);
529 h->n_arguments = info->method_handlers[i].n_arguments;
530 h->receive_cb = info->method_handlers[i].receive_cb;
531
532 pa_hashmap_put(handlers, h->method_name, h);
533 }
534
535 return handlers;
536 }
537
538 static pa_hashmap *create_property_handlers(const pa_dbus_interface_info *info) {
539 pa_hashmap *handlers;
540 unsigned i = 0;
541
542 pa_assert(info);
543 pa_assert(info->property_handlers || info->n_property_handlers == 0);
544
545 handlers = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
546
547 for (i = 0; i < info->n_property_handlers; ++i) {
548 pa_dbus_property_handler *h = pa_xnew(pa_dbus_property_handler, 1);
549 h->property_name = pa_xstrdup(info->property_handlers[i].property_name);
550 h->type = pa_xstrdup(info->property_handlers[i].type);
551 h->get_cb = info->property_handlers[i].get_cb;
552 h->set_cb = info->property_handlers[i].set_cb;
553
554 pa_hashmap_put(handlers, h->property_name, h);
555 }
556
557 return handlers;
558 }
559
560 static pa_dbus_signal_info *copy_signals(const pa_dbus_interface_info *info) {
561 pa_dbus_signal_info *dst;
562 unsigned i;
563
564 pa_assert(info);
565
566 if (info->n_signals == 0)
567 return NULL;
568
569 pa_assert(info->signals);
570
571 dst = pa_xnew(pa_dbus_signal_info, info->n_signals);
572
573 for (i = 0; i < info->n_signals; ++i) {
574 dst[i].name = pa_xstrdup(info->signals[i].name);
575 dst[i].arguments = copy_args(info->signals[i].arguments, info->signals[i].n_arguments);
576 dst[i].n_arguments = info->signals[i].n_arguments;
577 }
578
579 return dst;
580 }
581
582 int pa_dbus_protocol_add_interface(pa_dbus_protocol *p,
583 const char *path,
584 const pa_dbus_interface_info *info,
585 void *userdata) {
586 struct object_entry *obj_entry;
587 struct interface_entry *iface_entry;
588 pa_bool_t obj_entry_created = FALSE;
589
590 pa_assert(p);
591 pa_assert(path);
592 pa_assert(info);
593 pa_assert(info->name);
594 pa_assert(info->method_handlers || info->n_method_handlers == 0);
595 pa_assert(info->property_handlers || info->n_property_handlers == 0);
596 pa_assert(info->get_all_properties_cb || info->n_property_handlers == 0);
597 pa_assert(info->signals || info->n_signals == 0);
598
599 if (!(obj_entry = pa_hashmap_get(p->objects, path))) {
600 obj_entry = pa_xnew(struct object_entry, 1);
601 obj_entry->path = pa_xstrdup(path);
602 obj_entry->interfaces = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
603 obj_entry->introspection = NULL;
604
605 pa_hashmap_put(p->objects, path, obj_entry);
606 obj_entry_created = TRUE;
607 }
608
609 if (pa_hashmap_get(obj_entry->interfaces, info->name) != NULL)
610 goto fail; /* The interface was already registered. */
611
612 iface_entry = pa_xnew(struct interface_entry, 1);
613 iface_entry->name = pa_xstrdup(info->name);
614 iface_entry->method_handlers = create_method_handlers(info);
615 iface_entry->property_handlers = create_property_handlers(info);
616 iface_entry->get_all_properties_cb = info->get_all_properties_cb;
617 iface_entry->signals = copy_signals(info);
618 iface_entry->n_signals = info->n_signals;
619 iface_entry->userdata = userdata;
620 pa_hashmap_put(obj_entry->interfaces, iface_entry->name, iface_entry);
621
622 update_introspection(obj_entry);
623
624 if (obj_entry_created)
625 register_object(p, obj_entry);
626
627 return 0;
628
629 fail:
630 if (obj_entry_created) {
631 pa_hashmap_remove(p->objects, path);
632 pa_dbus_protocol_unref(p);
633 pa_xfree(obj_entry);
634 }
635
636 return -1;
637 }
638
639 static void unregister_object(pa_dbus_protocol *p, struct object_entry *obj_entry) {
640 struct connection_entry *conn_entry;
641 void *state = NULL;
642
643 pa_assert(p);
644 pa_assert(obj_entry);
645
646 while ((conn_entry = pa_hashmap_iterate(p->connections, &state, NULL)))
647 pa_assert_se(dbus_connection_unregister_object_path(conn_entry->connection, obj_entry->path));
648 }
649
650 static void method_handler_free_cb(void *p, void *userdata) {
651 pa_dbus_method_handler *h = p;
652 unsigned i;
653
654 pa_assert(h);
655
656 pa_xfree((char *) h->method_name);
657
658 for (i = 0; i < h->n_arguments; ++i) {
659 pa_xfree((char *) h->arguments[i].name);
660 pa_xfree((char *) h->arguments[i].type);
661 pa_xfree((char *) h->arguments[i].direction);
662 }
663
664 pa_xfree((pa_dbus_arg_info *) h->arguments);
665 }
666
667 static void property_handler_free_cb(void *p, void *userdata) {
668 pa_dbus_property_handler *h = p;
669
670 pa_assert(h);
671
672 pa_xfree((char *) h->property_name);
673 pa_xfree((char *) h->type);
674
675 pa_xfree(h);
676 }
677
678 int pa_dbus_protocol_remove_interface(pa_dbus_protocol *p, const char* path, const char* interface) {
679 struct object_entry *obj_entry;
680 struct interface_entry *iface_entry;
681 unsigned i;
682
683 pa_assert(p);
684 pa_assert(path);
685 pa_assert(interface);
686
687 if (!(obj_entry = pa_hashmap_get(p->objects, path)))
688 return -1;
689
690 if (!(iface_entry = pa_hashmap_remove(obj_entry->interfaces, interface)))
691 return -1;
692
693 update_introspection(obj_entry);
694
695 pa_xfree(iface_entry->name);
696 pa_hashmap_free(iface_entry->method_handlers, method_handler_free_cb, NULL);
697 pa_hashmap_free(iface_entry->property_handlers, property_handler_free_cb, NULL);
698
699 for (i = 0; i < iface_entry->n_signals; ++i) {
700 unsigned j;
701
702 pa_xfree((char *) iface_entry->signals[i].name);
703
704 for (j = 0; j < iface_entry->signals[i].n_arguments; ++j) {
705 pa_xfree((char *) iface_entry->signals[i].arguments[j].name);
706 pa_xfree((char *) iface_entry->signals[i].arguments[j].type);
707 pa_assert(iface_entry->signals[i].arguments[j].direction == NULL);
708 }
709
710 pa_xfree((pa_dbus_arg_info *) iface_entry->signals[i].arguments);
711 }
712
713 pa_xfree(iface_entry->signals);
714 pa_xfree(iface_entry);
715
716 if (pa_hashmap_isempty(obj_entry->interfaces)) {
717 unregister_object(p, obj_entry);
718
719 pa_hashmap_remove(p->objects, path);
720 pa_xfree(obj_entry->path);
721 pa_hashmap_free(obj_entry->interfaces, NULL, NULL);
722 pa_xfree(obj_entry->introspection);
723 pa_xfree(obj_entry);
724 }
725
726 return 0;
727 }
728
729 static void register_all_objects(pa_dbus_protocol *p, DBusConnection *conn) {
730 struct object_entry *obj_entry;
731 void *state = NULL;
732
733 pa_assert(p);
734 pa_assert(conn);
735
736 while ((obj_entry = pa_hashmap_iterate(p->objects, &state, NULL)))
737 pa_assert_se(dbus_connection_register_object_path(conn, obj_entry->path, &vtable, p));
738 }
739
740 int pa_dbus_protocol_register_connection(pa_dbus_protocol *p, DBusConnection *conn, pa_client *client) {
741 struct connection_entry *conn_entry;
742
743 pa_assert(p);
744 pa_assert(conn);
745 pa_assert(client);
746
747 if (pa_hashmap_get(p->connections, conn))
748 return -1; /* The connection was already registered. */
749
750 register_all_objects(p, conn);
751
752 conn_entry = pa_xnew(struct connection_entry, 1);
753 conn_entry->connection = dbus_connection_ref(conn);
754 conn_entry->client = client;
755 conn_entry->listening_for_all_signals = FALSE;
756 conn_entry->all_signals_objects = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
757 conn_entry->listening_signals = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
758
759 pa_hashmap_put(p->connections, conn, conn_entry);
760
761 return 0;
762 }
763
764 static void unregister_all_objects(pa_dbus_protocol *p, DBusConnection *conn) {
765 struct object_entry *obj_entry;
766 void *state = NULL;
767
768 pa_assert(p);
769 pa_assert(conn);
770
771 while ((obj_entry = pa_hashmap_iterate(p->objects, &state, NULL)))
772 pa_assert_se(dbus_connection_unregister_object_path(conn, obj_entry->path));
773 }
774
775 static void free_listened_object_name_cb(void *p, void *userdata) {
776 pa_assert(p);
777
778 pa_xfree(p);
779 }
780
781 static void free_listening_signals_idxset_cb(void *p, void *userdata) {
782 pa_idxset *set = p;
783
784 pa_assert(set);
785
786 pa_idxset_free(set, free_listened_object_name_cb, NULL);
787 }
788
789 int pa_dbus_protocol_unregister_connection(pa_dbus_protocol *p, DBusConnection *conn) {
790 struct connection_entry *conn_entry;
791
792 pa_assert(p);
793 pa_assert(conn);
794
795 if (!(conn_entry = pa_hashmap_remove(p->connections, conn)))
796 return -1;
797
798 unregister_all_objects(p, conn);
799
800 dbus_connection_unref(conn_entry->connection);
801 pa_idxset_free(conn_entry->all_signals_objects, free_listened_object_name_cb, NULL);
802 pa_hashmap_free(conn_entry->listening_signals, free_listening_signals_idxset_cb, NULL);
803 pa_xfree(conn_entry);
804
805 return 0;
806 }
807
808 pa_client *pa_dbus_protocol_get_client(pa_dbus_protocol *p, DBusConnection *conn) {
809 struct connection_entry *conn_entry;
810
811 pa_assert(p);
812 pa_assert(conn);
813
814 if (!(conn_entry = pa_hashmap_get(p->connections, conn)))
815 return NULL;
816
817 return conn_entry->client;
818 }
819
820 void pa_dbus_protocol_add_signal_listener(pa_dbus_protocol *p, DBusConnection *conn, const char *signal, char **objects, unsigned n_objects) {
821 struct connection_entry *conn_entry;
822 pa_idxset *object_set;
823 char *object_path;
824 unsigned i;
825
826 pa_assert(p);
827 pa_assert(conn);
828 pa_assert(objects || n_objects == 0);
829
830 pa_assert_se((conn_entry = pa_hashmap_get(p->connections, conn)));
831
832 /* all_signals_objects will either be emptied or replaced with new objects,
833 * so we empty it here unconditionally. If listening_for_all_signals is
834 * currently FALSE, the idxset is empty already. */
835 while ((object_path = pa_idxset_steal_first(conn_entry->all_signals_objects, NULL)))
836 pa_xfree(object_path);
837
838 if (signal) {
839 conn_entry->listening_for_all_signals = FALSE;
840
841 /* Replace the old object list with a new one. */
842 if ((object_set = pa_hashmap_get(conn_entry->listening_signals, signal)))
843 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
844 object_set = pa_idxset_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
845
846 for (i = 0; i < n_objects; ++i)
847 pa_idxset_put(object_set, pa_xstrdup(objects[i]), NULL);
848
849 } else {
850 conn_entry->listening_for_all_signals = TRUE;
851
852 /* We're not interested in individual signals anymore, so let's empty
853 * listening_signals. */
854 while ((object_set = pa_hashmap_steal_first(conn_entry->listening_signals)))
855 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
856
857 for (i = 0; i < n_objects; ++i)
858 pa_idxset_put(conn_entry->all_signals_objects, pa_xstrdup(objects[i]), NULL);
859 }
860 }
861
862 void pa_dbus_protocol_remove_signal_listener(pa_dbus_protocol *p, DBusConnection *conn, const char *signal) {
863 struct connection_entry *conn_entry;
864 pa_idxset *object_set;
865
866 pa_assert(p);
867 pa_assert(conn);
868
869 pa_assert_se((conn_entry = pa_hashmap_get(p->connections, conn)));
870
871 if (signal) {
872 if ((object_set = pa_hashmap_get(conn_entry->listening_signals, signal)))
873 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
874
875 } else {
876 char *object_path;
877
878 conn_entry->listening_for_all_signals = FALSE;
879
880 while ((object_path = pa_idxset_steal_first(conn_entry->all_signals_objects, NULL)))
881 pa_xfree(object_path);
882
883 while ((object_set = pa_hashmap_steal_first(conn_entry->listening_signals)))
884 pa_idxset_free(object_set, free_listened_object_name_cb, NULL);
885 }
886 }
887
888 void pa_dbus_protocol_send_signal(pa_dbus_protocol *p, DBusMessage *signal) {
889 struct connection_entry *conn_entry;
890 void *state = NULL;
891 pa_idxset *object_set;
892 DBusMessage *signal_copy;
893
894 pa_assert(p);
895 pa_assert(signal);
896 pa_assert(dbus_message_get_type(signal) == DBUS_MESSAGE_TYPE_SIGNAL);
897
898 /* XXX: We have to do some linear searching to find connections that want
899 * to receive the signal. This shouldn't be very significant performance
900 * problem, and adding an (object path, signal name) -> connection mapping
901 * would be likely to create substantial complexity. */
902
903 while ((conn_entry = pa_hashmap_iterate(p->connections, &state, NULL))) {
904
905 if ((conn_entry->listening_for_all_signals /* Case 1: listening for all signals */
906 && (pa_idxset_get_by_data(conn_entry->all_signals_objects, dbus_message_get_path(signal), NULL)
907 || pa_idxset_isempty(conn_entry->all_signals_objects)))
908
909 || (!conn_entry->listening_for_all_signals /* Case 2: not listening for all signals */
910 && (object_set = pa_hashmap_get(conn_entry->listening_signals, signal))
911 && (pa_idxset_get_by_data(object_set, dbus_message_get_path(signal), NULL)
912 || pa_idxset_isempty(object_set)))) {
913
914 pa_assert_se(signal_copy = dbus_message_copy(signal));
915 pa_assert_se(dbus_connection_send(conn_entry->connection, signal_copy, NULL));
916 dbus_message_unref(signal_copy);
917 }
918 }
919 }
920
921 const char **pa_dbus_protocol_get_extensions(pa_dbus_protocol *p, unsigned *n) {
922 const char **extensions;
923 const char *ext_name;
924 void *state = NULL;
925 unsigned i = 0;
926
927 pa_assert(p);
928 pa_assert(n);
929
930 *n = pa_idxset_size(p->extensions);
931
932 if (*n <= 0)
933 return NULL;
934
935 extensions = pa_xnew(const char *, *n);
936
937 while ((ext_name = pa_idxset_iterate(p->extensions, &state, NULL))) {
938 extensions[i] = ext_name;
939 ++i;
940 }
941
942 return extensions;
943 }
944
945 int pa_dbus_protocol_register_extension(pa_dbus_protocol *p, const char *name) {
946 char *internal_name;
947
948 pa_assert(p);
949 pa_assert(name);
950
951 internal_name = pa_xstrdup(name);
952
953 if (pa_idxset_put(p->extensions, internal_name, NULL) < 0) {
954 pa_xfree(internal_name);
955 return -1;
956 }
957
958 pa_hook_fire(&p->hooks[PA_DBUS_PROTOCOL_HOOK_EXTENSION_REGISTERED], internal_name);
959
960 return 0;
961 }
962
963 int pa_dbus_protocol_unregister_extension(pa_dbus_protocol *p, const char *name) {
964 char *internal_name;
965
966 pa_assert(p);
967 pa_assert(name);
968
969 if (!(internal_name = pa_idxset_remove_by_data(p->extensions, name, NULL)))
970 return -1;
971
972 pa_hook_fire(&p->hooks[PA_DBUS_PROTOCOL_HOOK_EXTENSION_UNREGISTERED], internal_name);
973
974 pa_xfree(internal_name);
975
976 return 0;
977 }
978
979 pa_hook_slot *pa_dbus_protocol_hook_connect(pa_dbus_protocol *p, pa_dbus_protocol_hook_t hook, pa_hook_priority_t prio, pa_hook_cb_t cb, void *data) {
980 pa_assert(p);
981 pa_assert(hook < PA_DBUS_PROTOCOL_HOOK_MAX);
982 pa_assert(cb);
983
984 return pa_hook_connect(&p->hooks[hook], prio, cb, data);
985 }