Debian Codegraph @

modules/module-portal-permissionstore.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2021 Collabora Ltd.
4 * @author Julian Bouzas <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include <wp/wp.h>
10 #include "dbus-connection-state.h"
11
12 WP_DEFINE_LOCAL_LOG_TOPIC ("m-portal-permissionstore")
13
14 #define DBUS_INTERFACE_NAME "org.freedesktop.impl.portal.PermissionStore"
15 #define DBUS_OBJECT_PATH "/org/freedesktop/impl/portal/PermissionStore"
16
17 enum
18 {
19 ACTION_GET_DBUS,
20 ACTION_LOOKUP,
21 ACTION_SET,
22 SIGNAL_CHANGED,
23 LAST_SIGNAL
24 };
25
26 static guint signals[LAST_SIGNAL] = { 0 };
27
28 struct _WpPortalPermissionStorePlugin
29 {
30 WpPlugin parent;
31
32 WpPlugin *dbus;
33 guint signal_id;
34 };
35
36 G_DECLARE_FINAL_TYPE (WpPortalPermissionStorePlugin,
37 wp_portal_permissionstore_plugin, WP, PORTAL_PERMISSIONSTORE_PLUGIN,
38 WpPlugin)
39 G_DEFINE_TYPE (WpPortalPermissionStorePlugin, wp_portal_permissionstore_plugin,
40 WP_TYPE_PLUGIN)
41
42 static gpointer
43 wp_portal_permissionstore_plugin_get_dbus (WpPortalPermissionStorePlugin *self)
44 {
45 return self->dbus ? g_object_ref (self->dbus) : NULL;
46 }
47
48 static GVariant *
49 wp_portal_permissionstore_plugin_lookup (WpPortalPermissionStorePlugin *self,
50 const gchar *table, const gchar *id)
51 {
52 g_autoptr (GDBusConnection) conn = NULL;
53 g_autoptr (GError) error = NULL;
54 g_autoptr (GVariant) res = NULL;
55 GVariant *permissions = NULL, *data = NULL;
56
57 g_object_get (self->dbus, "connection", &conn, NULL);
58 g_return_val_if_fail (conn, NULL);
59
60 /* Lookup */
61 res = g_dbus_connection_call_sync (conn, DBUS_INTERFACE_NAME,
62 DBUS_OBJECT_PATH, DBUS_INTERFACE_NAME, "Lookup",
63 g_variant_new ("(ss)", table, id), NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL,
64 &error);
65 if (error) {
66 g_autofree gchar *remote_error = g_dbus_error_get_remote_error (error);
67 g_dbus_error_strip_remote_error (error);
68
69 /* NotFound is neither unexpected nor important, so log it as INFO */
70 if (!g_strcmp0 (remote_error, "org.freedesktop.portal.Error.NotFound")) {
71 wp_info_object (self, "Lookup: %s (%s)", error->message, remote_error);
72 return NULL;
73 }
74
75 wp_warning_object (self, "Lookup: %s (%s)", error->message, remote_error);
76 return NULL;
77 }
78
79 /* Get the permissions */
80 g_variant_get (res, "(@a{sas}@v)", &permissions, &data);
81
82 return permissions ? g_variant_ref (permissions) : NULL;
83 }
84
85 static void
86 wp_portal_permissionstore_plugin_set (WpPortalPermissionStorePlugin *self,
87 const gchar *table, gboolean create, const gchar *id, GVariant *permissions)
88 {
89 g_autoptr (GDBusConnection) conn = NULL;
90 g_autoptr (GError) error = NULL;
91 g_autoptr (GVariant) res = NULL;
92 GVariant *data = NULL;
93
94 g_object_get (self->dbus, "connection", &conn, NULL);
95 g_return_if_fail (conn);
96
97 /* Set */
98 res = g_dbus_connection_call_sync (conn, DBUS_INTERFACE_NAME,
99 DBUS_OBJECT_PATH, DBUS_INTERFACE_NAME, "Set",
100 g_variant_new ("(sbs@a{sas}@v)", table, id, permissions, data), NULL,
101 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
102 if (error) {
103 g_autofree gchar *remote_error = g_dbus_error_get_remote_error (error);
104 g_dbus_error_strip_remote_error (error);
105 wp_warning_object (self, "Set: %s (%s)", error->message, remote_error);
106 }
107 }
108
109 static void
110 wp_portal_permissionstore_plugin_changed (GDBusConnection *connection,
111 const gchar *sender_name, const gchar *object_path,
112 const gchar *interface_name, const gchar *signal_name,
113 GVariant *parameters, gpointer user_data)
114 {
115 WpPortalPermissionStorePlugin *self =
116 WP_PORTAL_PERMISSIONSTORE_PLUGIN (user_data);
117 const char *table = NULL, *id = NULL;
118 gboolean deleted = FALSE;
119 GVariant *permissions = NULL, *data = NULL;
120
121 g_return_if_fail (parameters);
122 g_variant_get (parameters, "(ssb@v@a{sas})", &table, &id, &deleted, &data,
123 &permissions);
124
125 g_signal_emit (self, signals[SIGNAL_CHANGED], 0, table, id, deleted,
126 permissions);
127 }
128
129 static void
130 clear_signal (WpPortalPermissionStorePlugin *self)
131 {
132 g_autoptr (GDBusConnection) conn = NULL;
133
134 g_object_get (self->dbus, "connection", &conn, NULL);
135 if (conn && self->signal_id > 0) {
136 g_dbus_connection_signal_unsubscribe (conn, self->signal_id);
137 self->signal_id = 0;
138 }
139 }
140
141 static void
142 on_dbus_state_changed (GObject * dbus, GParamSpec * spec,
143 WpPortalPermissionStorePlugin *self)
144 {
145 WpDBusConnectionState state = -1;
146 g_object_get (dbus, "state", &state, NULL);
147
148 switch (state) {
149 case WP_DBUS_CONNECTION_STATE_CONNECTED: {
150 g_autoptr (GDBusConnection) conn = NULL;
151
152 g_object_get (dbus, "connection", &conn, NULL);
153 g_return_if_fail (conn);
154
155 self->signal_id = g_dbus_connection_signal_subscribe (conn,
156 DBUS_INTERFACE_NAME, DBUS_INTERFACE_NAME, "Changed", NULL, NULL,
157 G_DBUS_SIGNAL_FLAGS_NONE, wp_portal_permissionstore_plugin_changed,
158 self, NULL);
159 break;
160 }
161
162 case WP_DBUS_CONNECTION_STATE_CONNECTING:
163 case WP_DBUS_CONNECTION_STATE_CLOSED:
164 clear_signal (self);
165 break;
166
167 default:
168 break;
169 }
170 }
171
172 static void
173 wp_portal_permissionstore_plugin_init (WpPortalPermissionStorePlugin * self)
174 {
175 }
176
177 static void
178 wp_portal_permissionstore_plugin_enable (WpPlugin * plugin,
179 WpTransition * transition)
180 {
181 WpPortalPermissionStorePlugin *self =
182 WP_PORTAL_PERMISSIONSTORE_PLUGIN (plugin);
183 g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (self));
184
185 self->dbus = wp_plugin_find (core, "dbus-connection");
186 if (!self->dbus) {
187 wp_transition_return_error (transition, g_error_new (WP_DOMAIN_LIBRARY,
188 WP_LIBRARY_ERROR_INVARIANT,
189 "dbus-connection module must be loaded before portal-permissionstore"));
190 return;
191 }
192
193 g_signal_connect_object (self->dbus, "notify::state",
194 G_CALLBACK (on_dbus_state_changed), self, 0);
195 on_dbus_state_changed (G_OBJECT (self->dbus), NULL, self);
196
197 wp_object_update_features (WP_OBJECT (self), WP_PLUGIN_FEATURE_ENABLED, 0);
198 }
199
200 static void
201 wp_portal_permissionstore_plugin_disable (WpPlugin * plugin)
202 {
203 WpPortalPermissionStorePlugin *self =
204 WP_PORTAL_PERMISSIONSTORE_PLUGIN (plugin);
205
206 clear_signal (self);
207 if (self->dbus)
208 g_signal_handlers_disconnect_by_data (self->dbus, self);
209 g_clear_object (&self->dbus);
210
211 wp_object_update_features (WP_OBJECT (self), 0, WP_PLUGIN_FEATURE_ENABLED);
212 }
213
214 static void
215 wp_portal_permissionstore_plugin_class_init (
216 WpPortalPermissionStorePluginClass * klass)
217 {
218 WpPluginClass *plugin_class = (WpPluginClass *) klass;
219
220 plugin_class->enable = wp_portal_permissionstore_plugin_enable;
221 plugin_class->disable = wp_portal_permissionstore_plugin_disable;
222
223 /**
224 * WpPortalPermissionStorePlugin::get-dbus:
225 *
226 * Returns: (transfer full): the dbus object
227 */
228 signals[ACTION_GET_DBUS] = g_signal_new_class_handler (
229 "get-dbus", G_TYPE_FROM_CLASS (klass),
230 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
231 (GCallback) wp_portal_permissionstore_plugin_get_dbus,
232 NULL, NULL, NULL,
233 G_TYPE_OBJECT, 0);
234
235 /**
236 * WpPortalPermissionStorePlugin::lookup:
237 *
238 * @brief
239 * @em table: the table name
240 * @em id: the Id name
241 *
242 * Returns: (transfer full): the GVariant with permissions
243 */
244 signals[ACTION_LOOKUP] = g_signal_new_class_handler (
245 "lookup", G_TYPE_FROM_CLASS (klass),
246 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
247 (GCallback) wp_portal_permissionstore_plugin_lookup,
248 NULL, NULL, NULL, G_TYPE_VARIANT,
249 2, G_TYPE_STRING, G_TYPE_STRING);
250
251 /**
252 * WpPortalPermissionStorePlugin::set:
253 *
254 * @brief
255 * @em table: the table name
256 * @em create: whether to create the table if it does not exist
257 * @em id: the Id name
258 * @em permissions: the permissions
259 *
260 * Sets the permissions in the permission store
261 */
262 signals[ACTION_SET] = g_signal_new_class_handler (
263 "set", G_TYPE_FROM_CLASS (klass),
264 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
265 (GCallback) wp_portal_permissionstore_plugin_set,
266 NULL, NULL, NULL, G_TYPE_NONE,
267 4, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_VARIANT);
268
269 /**
270 * WpPortalPermissionStorePlugin::changed:
271 *
272 * @brief
273 * @em table: the table name
274 * @em id: the Id name
275 * @em deleted: whether the permission was deleted or not
276 * @em permissions: the GVariant with permissions
277 *
278 * Signaled when the permissions changed
279 */
280 signals[SIGNAL_CHANGED] = g_signal_new (
281 "changed", G_TYPE_FROM_CLASS (klass),
282 G_SIGNAL_RUN_LAST, 0,
283 NULL, NULL, NULL, G_TYPE_NONE, 4,
284 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_VARIANT);
285 }
286
287 WP_PLUGIN_EXPORT GObject *
288 wireplumber__module_init (WpCore * core, WpSpaJson * args, GError ** error)
289 {
290 return G_OBJECT (g_object_new (
291 wp_portal_permissionstore_plugin_get_type(),
292 "name", "portal-permissionstore",
293 "core", core,
294 NULL));
295 }