Debian Codegraph @

libgupnp/gupnp-resource-factory.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 Zeeshan Ali (Khattak) <[email protected]>
3 * Copyright (C) 2006, 2007 OpenedHand Ltd.
4 *
5 * Author: Zeeshan Ali (Khattak) <[email protected]>
6 * Jorn Baayen <[email protected]>
7 *
8 * SPDX-License-Identifier: LGPL-2.1-or-later
9 *
10 */
11
12 /**
13 * GUPnPResourceFactory:
14 *
15 * Associating custom Services, Devices, ServiceProxies and DeviceProxies with UPnP types.
16 *
17 * #GUPnPResourceFactory objects are used by [[email protected]],
18 * [[email protected]] and [[email protected]] to create resource proxy and resource
19 * objects.
20 *
21 * Register UPnP type - [[email protected]] pairs to have resource or resource proxy
22 * objects created with the specified #GType whenever an object for a resource
23 * of the specified UPnP type is requested. The #GType needs
24 * to be derived from the relevant resource or resource proxy type (e.g.
25 * a device proxy type needs to be derived from [[email protected]]).
26 */
27
28 #define G_LOG_DOMAIN "gupnp-resource-factory"
29
30 #include <config.h>
31 #include <string.h>
32
33 #include "gupnp-device-info-private.h"
34 #include "gupnp-resource-factory-private.h"
35 #include "gupnp-root-device.h"
36
37 struct _GUPnPResourceFactoryPrivate {
38 GHashTable *resource_type_hash;
39 GHashTable *proxy_type_hash;
40 };
41 typedef struct _GUPnPResourceFactoryPrivate GUPnPResourceFactoryPrivate;
42
43 G_DEFINE_TYPE_WITH_PRIVATE (GUPnPResourceFactory,
44 gupnp_resource_factory,
45 G_TYPE_OBJECT)
46
47 static void
48 gupnp_resource_factory_init (GUPnPResourceFactory *factory)
49 {
50 GUPnPResourceFactoryPrivate *priv;
51
52 priv = gupnp_resource_factory_get_instance_private (factory);
53 priv->resource_type_hash = g_hash_table_new_full (g_str_hash,
54 g_str_equal,
55 g_free,
56 NULL);
57 priv->proxy_type_hash = g_hash_table_new_full (g_str_hash,
58 g_str_equal,
59 g_free,
60 NULL);
61 }
62
63 static void
64 gupnp_resource_factory_finalize (GObject *object)
65 {
66 GUPnPResourceFactory *self;
67 GObjectClass *object_class;
68 GUPnPResourceFactoryPrivate *priv;
69
70 self = GUPNP_RESOURCE_FACTORY (object);
71 priv = gupnp_resource_factory_get_instance_private (self);
72
73 if (priv->resource_type_hash) {
74 g_hash_table_destroy (priv->resource_type_hash);
75 priv->resource_type_hash = NULL;
76 }
77
78 if (priv->proxy_type_hash) {
79 g_hash_table_destroy (priv->proxy_type_hash);
80 priv->proxy_type_hash = NULL;
81 }
82
83 object_class = G_OBJECT_CLASS (gupnp_resource_factory_parent_class);
84 object_class->finalize (object);
85 }
86
87 static void
88 gupnp_resource_factory_class_init (GUPnPResourceFactoryClass *klass)
89 {
90 GObjectClass *object_class;
91
92 object_class = G_OBJECT_CLASS (klass);
93
94 object_class->finalize = gupnp_resource_factory_finalize;
95 }
96
97 /**
98 * gupnp_resource_factory_new:
99 *
100 * Create a new #GUPnPResourceFactory object.
101 *
102 * Return value: A #GUPnPResourceFactory object.
103 **/
104 GUPnPResourceFactory *
105 gupnp_resource_factory_new (void)
106 {
107 return g_object_new (GUPNP_TYPE_RESOURCE_FACTORY, NULL);
108 }
109
110 /**
111 * gupnp_resource_factory_get_default:
112 *
113 * Get the default singleton #GUPnPResourceFactory object.
114 *
115 * Returns: (transfer none): A @GUPnPResourceFactory object.
116 **/
117 GUPnPResourceFactory *
118 gupnp_resource_factory_get_default (void)
119 {
120 static GUPnPResourceFactory *default_factory = NULL;
121
122 if (g_once_init_enter (&default_factory)) {
123 GUPnPResourceFactory *factory =
124 g_object_new (GUPNP_TYPE_RESOURCE_FACTORY, NULL);
125
126 g_once_init_leave (&default_factory, factory);
127 }
128
129 return default_factory;
130 }
131
132 static GType
133 lookup_type_with_fallback (GHashTable *resource_types,
134 const char *requested_type,
135 const char *child_node,
136 xmlNode *element,
137 GType fallback)
138 {
139 GType type = fallback;
140 char *upnp_type = NULL;
141
142 if (requested_type == NULL) {
143 g_debug ("Looking up type from XML");
144 upnp_type = xml_util_get_child_element_content_glib (element,
145 child_node);
146 } else {
147 g_debug ("Using passed type %s", requested_type);
148 upnp_type = g_strdup (requested_type);
149 }
150
151
152 if (upnp_type != NULL) {
153 g_debug ("Found type from XML: %s", upnp_type);
154 gpointer value;
155 char *needle = NULL;
156
157 value = g_hash_table_lookup (resource_types, upnp_type);
158
159 if (value == NULL) {
160 g_debug ("Trying to use version-less type...");
161 needle = g_strrstr (upnp_type, ":");
162 if (needle != NULL) {
163 *needle = '\0';
164 g_debug ("Version-less type is %s", upnp_type);
165
166 value = g_hash_table_lookup (resource_types, upnp_type);
167 }
168 }
169
170 if (value != NULL) {
171 type = GPOINTER_TO_SIZE (value);
172 }
173
174 g_debug ("Will return type %s for UPnP type %s", g_type_name (type), upnp_type);
175 g_free (upnp_type);
176 } else {
177 g_debug ("Will return fall-back type %s", upnp_type);
178 }
179
180
181 return type;
182 }
183
184
185 /**
186 * gupnp_resource_factory_create_device_proxy:
187 * @factory: A #GUPnPResourceFactory
188 * @context: A #GUPnPContext
189 * @doc: A #GUPnPXMLDoc
190 * @element: The #xmlNode ponting to the right device element
191 * @udn: The UDN of the device to create a proxy for
192 * @location: The location of the device description file
193 * @url_base: The URL base for this device, or %NULL if none
194 *
195 * Create a #GUPnPDeviceProxy for the device with element @element, as
196 * read from the device description file specified by @location.
197 *
198 * Return value:(nullable)(transfer full): A new #GUPnPDeviceProxy.
199 **/
200 GUPnPDeviceProxy *
201 gupnp_resource_factory_create_device_proxy (GUPnPResourceFactory *factory,
202 GUPnPContext *context,
203 GUPnPXMLDoc *doc,
204 xmlNode *element,
205 const char *udn,
206 const char *location,
207 const GUri *url_base)
208 {
209 GUPnPDeviceProxy *proxy;
210 GType proxy_type;
211 GUPnPResourceFactoryPrivate *priv;
212
213 g_return_val_if_fail (GUPNP_IS_RESOURCE_FACTORY (factory), NULL);
214 g_return_val_if_fail (GUPNP_IS_CONTEXT (context), NULL);
215 g_return_val_if_fail (GUPNP_IS_XML_DOC (doc), NULL);
216 g_return_val_if_fail (element != NULL, NULL);
217 g_return_val_if_fail (location != NULL, NULL);
218 g_return_val_if_fail (url_base != NULL, NULL);
219
220 priv = gupnp_resource_factory_get_instance_private (factory);
221
222 proxy_type = lookup_type_with_fallback (priv->proxy_type_hash,
223 NULL,
224 "deviceType",
225 element,
226 GUPNP_TYPE_DEVICE_PROXY);
227
228 proxy = g_object_new (proxy_type,
229 "resource-factory", factory,
230 "context", context,
231 "location", location,
232 "udn", udn,
233 "url-base", url_base,
234 "document", doc,
235 "element", element,
236 NULL);
237
238 return proxy;
239 }
240
241 /**
242 * gupnp_resource_factory_create_service_proxy:
243 * @factory: A #GUPnPResourceFactory
244 * @context: A #GUPnPContext
245 * @doc: A #GUPnPXMLDoc
246 * @element: The #xmlNode pointing to the right service element
247 * @location: The location of the service description file
248 * @udn: The UDN of the device the service is contained in
249 * @service_type: (nullable): The service type, or %NULL to use service
250 * type from @element
251 * @url_base: The URL base for this service, or %NULL if none
252 *
253 * Create a #GUPnPServiceProxy for the service with element @element, as
254 * read from the service description file specified by @location.
255 *
256 * Return value:(nullable)(transfer full): A new #GUPnPServiceProxy.
257 **/
258 GUPnPServiceProxy *
259 gupnp_resource_factory_create_service_proxy (GUPnPResourceFactory *factory,
260 GUPnPContext *context,
261 GUPnPXMLDoc *doc,
262 xmlNode *element,
263 const char *udn,
264 const char *service_type,
265 const char *location,
266 const GUri *url_base)
267 {
268 GUPnPServiceProxy *proxy;
269 GType proxy_type;
270 GUPnPResourceFactoryPrivate *priv;
271
272 g_return_val_if_fail (GUPNP_IS_RESOURCE_FACTORY (factory), NULL);
273 g_return_val_if_fail (GUPNP_IS_CONTEXT (context), NULL);
274 g_return_val_if_fail (GUPNP_IS_XML_DOC (doc), NULL);
275 g_return_val_if_fail (element != NULL, NULL);
276 g_return_val_if_fail (location != NULL, NULL);
277 g_return_val_if_fail (url_base != NULL, NULL);
278
279 priv = gupnp_resource_factory_get_instance_private (factory);
280
281 proxy_type = lookup_type_with_fallback (priv->proxy_type_hash,
282 service_type,
283 "serviceType",
284 element,
285 GUPNP_TYPE_SERVICE_PROXY);
286
287 proxy = g_object_new (proxy_type,
288 "context", context,
289 "location", location,
290 "udn", udn,
291 "service-type", service_type,
292 "url-base", url_base,
293 "document", doc,
294 "element", element,
295 NULL);
296
297 return proxy;
298 }
299
300 /**
301 * gupnp_resource_factory_create_device:
302 * @factory: A #GUPnPResourceFactory
303 * @context: A #GUPnPContext
304 * @root_device: The #GUPnPRootDevice
305 * @element: The #xmlNode ponting to the right device element
306 * @udn: The UDN of the device to create a device for
307 * @location: The location of the device description file
308 * @url_base: The URL base for this device
309 *
310 * Create a #GUPnPDevice for the device with element @element, as
311 * read from the device description file specified by @location.
312 *
313 * Return value: (nullable)(transfer full): A new #GUPnPDevice.
314 **/
315 GUPnPDevice *
316 gupnp_resource_factory_create_device (GUPnPResourceFactory *factory,
317 GUPnPContext *context,
318 GUPnPDevice *root_device,
319 xmlNode *element,
320 const char *udn,
321 const char *location,
322 const GUri *url_base)
323 {
324 GUPnPDevice *device;
325 GType device_type;
326 GUPnPResourceFactoryPrivate *priv;
327
328 g_return_val_if_fail (GUPNP_IS_RESOURCE_FACTORY (factory), NULL);
329 g_return_val_if_fail (GUPNP_IS_CONTEXT (context), NULL);
330 g_return_val_if_fail (GUPNP_IS_ROOT_DEVICE (root_device), NULL);
331 g_return_val_if_fail (element != NULL, NULL);
332 g_return_val_if_fail (url_base != NULL, NULL);
333
334 priv = gupnp_resource_factory_get_instance_private (factory);
335
336 device_type = lookup_type_with_fallback (priv->resource_type_hash,
337 NULL,
338 "deviceType",
339 element,
340 GUPNP_TYPE_DEVICE);
341
342 GUPnPXMLDoc *doc = _gupnp_device_info_get_document (
343 GUPNP_DEVICE_INFO (root_device));
344 device = g_object_new (device_type,
345 "resource-factory",
346 factory,
347 "context",
348 context,
349 "root-device",
350 root_device,
351 "location",
352 location,
353 "udn",
354 udn,
355 "url-base",
356 url_base,
357 "document",
358 doc,
359 "element",
360 element,
361 NULL);
362
363 return device;
364 }
365
366 /**
367 * gupnp_resource_factory_create_service:
368 * @factory: A #GUPnPResourceFactory
369 * @context: A #GUPnPContext
370 * @root_device: The #GUPnPRootDevice
371 * @element: The #xmlNode ponting to the right service element
372 * @udn: The UDN of the device the service is contained in
373 * @location: The location of the service description file
374 * @url_base: The URL base for this service
375 *
376 * Create a #GUPnPService for the service with element @element, as
377 * read from the service description file specified by @location.
378 *
379 * Return value: (nullable)(transfer full): A new #GUPnPService.
380 **/
381 GUPnPService *
382 gupnp_resource_factory_create_service (GUPnPResourceFactory *factory,
383 GUPnPContext *context,
384 GUPnPDevice *root_device,
385 xmlNode *element,
386 const char *udn,
387 const char *location,
388 const GUri *url_base)
389 {
390 GUPnPService *service;
391 GType service_type;
392 GUPnPResourceFactoryPrivate *priv;
393
394 g_return_val_if_fail (GUPNP_IS_RESOURCE_FACTORY (factory), NULL);
395 g_return_val_if_fail (GUPNP_IS_CONTEXT (context), NULL);
396 g_return_val_if_fail (GUPNP_IS_ROOT_DEVICE (root_device), NULL);
397 g_return_val_if_fail (element != NULL, NULL);
398 g_return_val_if_fail (location != NULL, NULL);
399 g_return_val_if_fail (url_base != NULL, NULL);
400
401 priv = gupnp_resource_factory_get_instance_private (factory);
402
403 service_type = lookup_type_with_fallback (priv->resource_type_hash,
404 NULL,
405 "serviceType",
406 element,
407 GUPNP_TYPE_SERVICE);
408
409 GUPnPXMLDoc *doc = _gupnp_device_info_get_document (
410 GUPNP_DEVICE_INFO (root_device));
411 service = g_object_new (service_type,
412 "context",
413 context,
414 "root-device",
415 root_device,
416 "location",
417 location,
418 "udn",
419 udn,
420 "url-base",
421 url_base,
422 "document",
423 doc,
424 "element",
425 element,
426 NULL);
427
428 return service;
429 }
430
431 /**
432 * gupnp_resource_factory_register_resource_type:
433 * @factory: A #GUPnPResourceFactory.
434 * @upnp_type: The UPnP type name of the resource.
435 * @type: The requested GType assignment for the resource.
436 *
437 * Registers the GType @type for the resource of UPnP type @upnp_type. After
438 * this call, the factory @factory will create object of GType @type each time
439 * it is asked to create a resource object for UPnP type @upnp_type.
440 *
441 * You can either register a type for a concrete version of a device or service
442 * such as urn:schemas-upnp-org:service:AVTransport:2 or version-independently,
443 * urn:schemas-upnp-org:service:AVTransport. If you register for an explicit
444 * version of a service, it will be an exact match.
445 *
446 * Note: GType @type must be a derived type of #GUPNP_TYPE_DEVICE if resource is
447 * a device or #GUPNP_TYPE_SERVICE if its a service.
448 **/
449 void
450 gupnp_resource_factory_register_resource_type (GUPnPResourceFactory *factory,
451 const char *upnp_type,
452 GType type)
453 {
454 GUPnPResourceFactoryPrivate *priv;
455
456 priv = gupnp_resource_factory_get_instance_private (factory);
457
458 g_hash_table_insert (priv->resource_type_hash,
459 g_strdup (upnp_type),
460 GSIZE_TO_POINTER (type));
461 }
462
463 /**
464 * gupnp_resource_factory_unregister_resource_type:
465 * @factory: A #GUPnPResourceFactory.
466 * @upnp_type: The UPnP type name of the resource.
467 *
468 * Unregisters the GType assignment for the resource of UPnP type @upnp_type.
469 *
470 * Return value: %TRUE if GType assignment was removed successfully, %FALSE
471 * otherwise.
472 **/
473 gboolean
474 gupnp_resource_factory_unregister_resource_type
475 (GUPnPResourceFactory *factory,
476 const char *upnp_type)
477 {
478 GUPnPResourceFactoryPrivate *priv;
479
480 priv = gupnp_resource_factory_get_instance_private (factory);
481
482 return g_hash_table_remove (priv->resource_type_hash,
483 upnp_type);
484 }
485
486 /**
487 * gupnp_resource_factory_register_resource_proxy_type:
488 * @factory: A #GUPnPResourceFactory.
489 * @upnp_type: The UPnP type name of the resource.
490 * @type: The requested GType assignment for the resource proxy.
491 *
492 * Registers the GType @type for the proxy of resource of UPnP type @upnp_type.
493 * After this call, the factory @factory will create object of GType @type each
494 * time it is asked to create a resource proxy object for UPnP type @upnp_type.
495 *
496 * Note: GType @type must be a derived type of #GUPNP_TYPE_DEVICE_PROXY if
497 * resource is a device or #GUPNP_TYPE_SERVICE_PROXY if its a service.
498 **/
499 void
500 gupnp_resource_factory_register_resource_proxy_type
501 (GUPnPResourceFactory *factory,
502 const char *upnp_type,
503 GType type)
504 {
505 GUPnPResourceFactoryPrivate *priv;
506
507 priv = gupnp_resource_factory_get_instance_private (factory);
508
509 g_hash_table_insert (priv->proxy_type_hash,
510 g_strdup (upnp_type),
511 GSIZE_TO_POINTER (type));
512 }
513
514 /**
515 * gupnp_resource_factory_unregister_resource_proxy_type:
516 * @factory: A #GUPnPResourceFactory.
517 * @upnp_type: The UPnP type name of the resource.
518 *
519 * Unregisters the GType assignment for the proxy of resource of UPnP type
520 * @upnp_type.
521 *
522 * Return value: %TRUE if GType assignment was removed successfully, %FALSE
523 * otherwise.
524 **/
525 gboolean
526 gupnp_resource_factory_unregister_resource_proxy_type
527 (GUPnPResourceFactory *factory,
528 const char *upnp_type)
529 {
530 GUPnPResourceFactoryPrivate *priv;
531
532 priv = gupnp_resource_factory_get_instance_private (factory);
533
534 return g_hash_table_remove (priv->proxy_type_hash, upnp_type);
535 }