| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2021 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "component-loader.h" |
| 10 |
#include "log.h" |
| 11 |
#include "error.h" |
| 12 |
#include "private/registry.h" |
| 13 |
|
| 14 |
WP_DEFINE_LOCAL_LOG_TOPIC ("wp-comp-loader") |
| 15 |
|
| 16 |
/*! \defgroup wpcomponentloader WpComponentLoader */ |
| 17 |
/*! |
| 18 |
* \struct WpComponentLoader |
| 19 |
* |
| 20 |
* An interface that provides the ability to load components. |
| 21 |
* |
| 22 |
* Components can be: |
| 23 |
* - WirePlumber modules (libraries that provide WpPlugin and WpSiFactory objects) |
| 24 |
* - Scripts (ex. lua scripts) |
| 25 |
* |
| 26 |
* The WirePlumber library provides built-in support for loading WirePlumber |
| 27 |
* modules, without a component loader. For other kinds of components, |
| 28 |
* a component loader is meant to be provided in by some WirePlumber module. |
| 29 |
* For Lua scripts specifically, a component loader is provided by the lua |
| 30 |
* scripting module. |
| 31 |
*/ |
| 32 |
|
| 33 |
G_DEFINE_INTERFACE (WpComponentLoader, wp_component_loader, G_TYPE_OBJECT) |
| 34 |
|
| 35 |
static void |
| 36 |
wp_component_loader_default_init (WpComponentLoaderInterface * iface) |
| 37 |
{ |
| 38 |
} |
| 39 |
|
| 40 |
static gboolean |
| 41 |
find_component_loader_func (gpointer cl, gpointer type) |
| 42 |
{ |
| 43 |
if (WP_IS_COMPONENT_LOADER (cl) && |
| 44 |
(WP_COMPONENT_LOADER_GET_IFACE (cl)->supports_type ( |
| 45 |
WP_COMPONENT_LOADER (cl), (const gchar *) type))) |
| 46 |
return TRUE; |
| 47 |
|
| 48 |
return FALSE; |
| 49 |
} |
| 50 |
|
| 51 |
static WpComponentLoader * |
| 52 |
wp_component_loader_find (WpCore * core, const gchar * type) |
| 53 |
{ |
| 54 |
g_return_val_if_fail (WP_IS_CORE (core), NULL); |
| 55 |
GObject *c = wp_core_find_object (core, |
| 56 |
(GEqualFunc) find_component_loader_func, type); |
| 57 |
return c ? WP_COMPONENT_LOADER (c) : NULL; |
| 58 |
} |
| 59 |
|
| 60 |
static void |
| 61 |
wp_component_loader_load (WpComponentLoader * self, WpCore * core, |
| 62 |
const gchar * component, const gchar * type, WpSpaJson * args, |
| 63 |
GCancellable * cancellable, GAsyncReadyCallback callback, gpointer data) |
| 64 |
{ |
| 65 |
WP_COMPONENT_LOADER_GET_IFACE (self)->load (self, core, component, type, |
| 66 |
args, cancellable, callback, data); |
| 67 |
} |
| 68 |
|
| 69 |
static GObject * |
| 70 |
wp_component_loader_load_finish (WpComponentLoader * self, GAsyncResult * res, |
| 71 |
GError ** error) |
| 72 |
{ |
| 73 |
return WP_COMPONENT_LOADER_GET_IFACE (self)->load_finish (self, res, error); |
| 74 |
} |
| 75 |
|
| 76 |
static void |
| 77 |
wp_component_loader_load_task_return (GTask * task, gpointer object) |
| 78 |
{ |
| 79 |
WpCore *core = g_task_get_source_object (task); |
| 80 |
WpRegistry *reg = wp_core_get_registry (core); |
| 81 |
gchar *provides = g_task_get_task_data (task); |
| 82 |
|
| 83 |
/* store object in the registry */ |
| 84 |
if (object) |
| 85 |
wp_core_register_object (core, g_object_ref (object)); |
| 86 |
|
| 87 |
if (provides) |
| 88 |
wp_registry_mark_feature_provided (reg, provides); |
| 89 |
|
| 90 |
g_task_return_boolean (task, TRUE); |
| 91 |
} |
| 92 |
|
| 93 |
static void |
| 94 |
on_object_activated (WpObject * object, GAsyncResult * res, gpointer data) |
| 95 |
{ |
| 96 |
g_autoptr (GTask) task = G_TASK (data); |
| 97 |
g_autoptr (GError) error = NULL; |
| 98 |
|
| 99 |
if (!wp_object_activate_finish (object, res, &error)) { |
| 100 |
g_task_return_error (task, g_steal_pointer (&error)); |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
wp_component_loader_load_task_return (task, object); |
| 105 |
} |
| 106 |
|
| 107 |
static void |
| 108 |
on_component_loader_load_done (WpComponentLoader * cl, GAsyncResult * res, |
| 109 |
gpointer data) |
| 110 |
{ |
| 111 |
g_autoptr (GTask) task = G_TASK (data); |
| 112 |
g_autoptr (GError) error = NULL; |
| 113 |
g_autoptr (GObject) object = NULL; |
| 114 |
|
| 115 |
object = wp_component_loader_load_finish (cl, res, &error); |
| 116 |
if (error) { |
| 117 |
g_task_return_error (task, g_steal_pointer (&error)); |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
if (object) { |
| 122 |
wp_trace_object (cl, "loaded object " WP_OBJECT_FORMAT, WP_OBJECT_ARGS (object)); |
| 123 |
|
| 124 |
if (WP_IS_OBJECT (object)) { |
| 125 |
/* WpObject needs to be activated */ |
| 126 |
wp_object_activate (WP_OBJECT (object), WP_OBJECT_FEATURES_ALL, NULL, |
| 127 |
(GAsyncReadyCallback) on_object_activated, g_steal_pointer (&task)); |
| 128 |
return; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
wp_component_loader_load_task_return (task, object); |
| 133 |
} |
| 134 |
|
| 135 |
/*! |
| 136 |
* \brief Loads the specified \a component on \a self |
| 137 |
* |
| 138 |
* The \a type will determine which component loader to use. The following types |
| 139 |
* are built-in and will always work without a component loader: |
| 140 |
* - "module" - Loads a WirePlumber module |
| 141 |
* - "array" - Loads multiple components interpreting the \a args as a JSON |
| 142 |
* array with component definitions, as they would appear in the |
| 143 |
* configuration file. When this type is used, \a component is ignored and |
| 144 |
* can be NULL |
| 145 |
* |
| 146 |
* \ingroup wpcomponentloader |
| 147 |
* \param self the core |
| 148 |
* \param component (nullable): the module name or file name |
| 149 |
* \param type the type of the component |
| 150 |
* \param args (transfer none)(nullable): additional arguments for the component, |
| 151 |
* expected to be a JSON object |
| 152 |
* \param provides (nullable): the name of the feature that this component will |
| 153 |
* provide if it loads successfully; this can be queried later with |
| 154 |
* wp_core_test_feature() |
| 155 |
* \param cancellable (nullable): optional GCancellable |
| 156 |
* \param callback (scope async)(closure data): the callback to call when the operation is done |
| 157 |
* \param data data to pass to \a callback |
| 158 |
*/ |
| 159 |
void |
| 160 |
wp_core_load_component (WpCore * self, const gchar * component, |
| 161 |
const gchar * type, WpSpaJson * args, const gchar * provides, |
| 162 |
GCancellable * cancellable, GAsyncReadyCallback callback, gpointer data) |
| 163 |
{ |
| 164 |
g_autoptr (GTask) task = NULL; |
| 165 |
g_autoptr (WpComponentLoader) cl = NULL; |
| 166 |
|
| 167 |
task = g_task_new (self, cancellable, callback, data); |
| 168 |
g_task_set_source_tag (task, wp_core_load_component); |
| 169 |
|
| 170 |
if (provides) |
| 171 |
g_task_set_task_data (task, g_strdup (provides), g_free); |
| 172 |
|
| 173 |
/* find a component loader for that type and load the component */ |
| 174 |
cl = wp_component_loader_find (self, type); |
| 175 |
if (!cl) { |
| 176 |
g_task_return_new_error (task, WP_DOMAIN_LIBRARY, |
| 177 |
WP_LIBRARY_ERROR_INVALID_ARGUMENT, |
| 178 |
"No component loader was found for components of type '%s'", type); |
| 179 |
return; |
| 180 |
} |
| 181 |
|
| 182 |
wp_debug_object (self, "load '%s', type '%s', loader " WP_OBJECT_FORMAT, |
| 183 |
component, type, WP_OBJECT_ARGS (cl)); |
| 184 |
|
| 185 |
wp_component_loader_load (cl, self, component, type, args, cancellable, |
| 186 |
(GAsyncReadyCallback) on_component_loader_load_done, g_object_ref (task)); |
| 187 |
} |
| 188 |
|
| 189 |
/*! |
| 190 |
* \brief Finishes the operation started by wp_core_load_component(). |
| 191 |
* This is meant to be called in the callback that was passed to that method. |
| 192 |
* |
| 193 |
* \ingroup wpcomponentloader |
| 194 |
* \param self the component loader object |
| 195 |
* \param res the async result |
| 196 |
* \param error (out) (optional): the operation's error, if it occurred |
| 197 |
* \returns TRUE if the requested component was loaded, FALSE otherwise |
| 198 |
*/ |
| 199 |
gboolean |
| 200 |
wp_core_load_component_finish (WpCore * self, GAsyncResult * res, |
| 201 |
GError ** error) |
| 202 |
{ |
| 203 |
g_return_val_if_fail ( |
| 204 |
g_async_result_is_tagged (res, wp_core_load_component), FALSE); |
| 205 |
|
| 206 |
return g_task_propagate_boolean (G_TASK (res), error); |
| 207 |
} |