1 /* libsecret - GLib wrapper for Secret Prompt
3 * Copyright 2011 Collabora Ltd.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; either version 2.1 of the licence or (at
8 * your option) any later version.
10 * See the included COPYING file for more information.
12 * Author: Stef Walter <stefw@gnome.org>
17 #include "secret-dbus-generated.h"
18 #include "secret-private.h"
19 #include "secret-prompt.h"
22 #include <glib/gi18n-lib.h>
25 * SECTION:secret-prompt
26 * @title: SecretPrompt
27 * @short_description: a prompt in the Service
29 * A #SecretPrompt represents a prompt in the Secret Service.
31 * Certain actions on the Secret Service require user prompting to complete,
32 * such as creating a collection, or unlocking a collection. When such a prompt
33 * is necessary, then a #SecretPrompt object is created by this library, and
34 * passed to the secret_service_prompt() method. In this way it is handled
37 * In order to customize prompt handling, override the
38 * SecretServiceClass::prompt_async and SecretServiceClass::prompt_finish
39 * virtual methods of the #SecretService class.
41 * These functions have an unstable API and may change across versions. Use
42 * <literal>libsecret-unstable</literal> package to access them.
50 * A proxy object representing a prompt that the Secret Service will display
56 * @parent_class: the parent class
58 * The class for #SecretPrompt.
61 struct _SecretPromptPrivate {
65 G_DEFINE_TYPE (SecretPrompt, secret_prompt, G_TYPE_DBUS_PROXY);
68 secret_prompt_init (SecretPrompt *self)
70 self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, SECRET_TYPE_PROMPT,
75 secret_prompt_class_init (SecretPromptClass *klass)
77 g_type_class_add_private (klass, sizeof (SecretPromptPrivate));
86 on_prompt_run_complete (GObject *source,
90 RunClosure *closure = user_data;
91 closure->result = g_object_ref (result);
92 g_main_loop_quit (closure->loop);
96 _secret_prompt_instance (SecretService *service,
97 const gchar *prompt_path)
100 SecretPrompt *prompt;
101 GError *error = NULL;
103 g_return_val_if_fail (SECRET_IS_SERVICE (service), NULL);
104 g_return_val_if_fail (prompt_path != NULL, NULL);
106 proxy = G_DBUS_PROXY (service);
107 prompt = g_initable_new (SECRET_TYPE_PROMPT, NULL, &error,
108 "g-flags", G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
109 "g-interface-info", _secret_gen_prompt_interface_info (),
110 "g-name", g_dbus_proxy_get_name (proxy),
111 "g-connection", g_dbus_proxy_get_connection (proxy),
112 "g-object-path", prompt_path,
113 "g-interface-name", SECRET_PROMPT_INTERFACE,
117 g_warning ("couldn't create SecretPrompt object: %s", error->message);
118 g_clear_error (&error);
128 * @window_id: XWindow id for parent window to be transient for
129 * @cancellable: optional cancellation object
130 * @return_type: the variant type of the prompt result
131 * @error: location to place an error on failure
133 * Runs a prompt and performs the prompting. Returns a variant result if the
134 * prompt was completed and not dismissed. The type of result depends on the
135 * action the prompt is completing, and is defined in the Secret Service DBus
138 * If @window_id is non-zero then it is used as an XWindow id. The Secret
139 * Service can make its prompt transient for the window with this id. In some
140 * Secret Service implementations this is not possible, so the behavior
141 * depending on this should degrade gracefully.
143 * This runs the dialog in a recursive mainloop. When run from a user interface
144 * thread, this means the user interface will remain responsive. Care should be
145 * taken that appropriate user interface actions are disabled while running the
148 * Returns: (transfer full): %NULL if the prompt was dismissed or an error occurred
151 secret_prompt_run (SecretPrompt *self,
153 GCancellable *cancellable,
154 const GVariantType *return_type,
157 GMainContext *context;
161 g_return_val_if_fail (SECRET_IS_PROMPT (self), NULL);
162 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
163 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
165 context = g_main_context_get_thread_default ();
167 closure = g_new0 (RunClosure, 1);
168 closure->loop = g_main_loop_new (context, FALSE);
170 secret_prompt_perform (self, window_id, cancellable,
171 on_prompt_run_complete, closure);
173 g_main_loop_run (closure->loop);
175 retval = secret_prompt_perform_finish (self, closure->result, return_type, error);
177 g_main_loop_unref (closure->loop);
178 g_object_unref (closure->result);
185 * secret_prompt_perform_sync:
187 * @window_id: XWindow id for parent window to be transient for
188 * @cancellable: optional cancellation object
189 * @return_type: the variant type of the prompt result
190 * @error: location to place an error on failure
192 * Runs a prompt and performs the prompting. Returns a variant result if the
193 * prompt was completed and not dismissed. The type of result depends on the
194 * action the prompt is completing, and is defined in the Secret Service DBus
197 * If @window_id is non-zero then it is used as an XWindow id. The Secret
198 * Service can make its prompt transient for the window with this id. In some
199 * Secret Service implementations this is not possible, so the behavior
200 * depending on this should degrade gracefully.
202 * This method may block indefinitely and should not be used in user interface
205 * Returns: (transfer full): %NULL if the prompt was dismissed or an error occurred
208 secret_prompt_perform_sync (SecretPrompt *self,
210 GCancellable *cancellable,
211 const GVariantType *return_type,
214 GMainContext *context;
217 g_return_val_if_fail (SECRET_IS_PROMPT (self), NULL);
218 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
219 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
221 context = g_main_context_new ();
222 g_main_context_push_thread_default (context);
224 retval = secret_prompt_run (self, window_id, cancellable, return_type, error);
226 /* Needed to prevent memory leaks */
227 while (g_main_context_iteration (context, FALSE));
229 g_main_context_pop_thread_default (context);
230 g_main_context_unref (context);
236 GDBusConnection *connection;
237 GCancellable *call_cancellable;
238 GCancellable *async_cancellable;
239 gulong cancelled_sig;
250 perform_closure_free (gpointer data)
252 PerformClosure *closure = data;
253 g_object_unref (closure->call_cancellable);
254 g_clear_object (&closure->async_cancellable);
255 g_object_unref (closure->connection);
257 g_variant_unref (closure->result);
258 g_assert (closure->signal == 0);
259 g_assert (closure->watch == 0);
260 g_slice_free (PerformClosure, closure);
264 perform_prompt_complete (GSimpleAsyncResult *res,
267 PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
269 closure->dismissed = dismissed;
270 if (closure->completed)
272 closure->completed = TRUE;
275 g_dbus_connection_signal_unsubscribe (closure->connection, closure->signal);
279 g_bus_unwatch_name (closure->watch);
282 if (closure->cancelled_sig)
283 g_signal_handler_disconnect (closure->async_cancellable, closure->cancelled_sig);
284 closure->cancelled_sig = 0;
286 g_simple_async_result_complete (res);
290 on_prompt_completed (GDBusConnection *connection,
291 const gchar *sender_name,
292 const gchar *object_path,
293 const gchar *interface_name,
294 const gchar *signal_name,
295 GVariant *parameters,
298 GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
299 SecretPrompt *self = SECRET_PROMPT (g_async_result_get_source_object (user_data));
300 PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
303 closure->prompting = FALSE;
305 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(bv)"))) {
306 g_warning ("SecretPrompt received invalid %s signal of type %s",
307 signal_name, g_variant_get_type_string (parameters));
308 perform_prompt_complete (res, TRUE);
311 g_variant_get (parameters, "(bv)", &dismissed, &closure->result);
312 perform_prompt_complete (res, dismissed);
315 g_object_unref (self);
319 on_prompt_prompted (GObject *source,
320 GAsyncResult *result,
323 GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
324 PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
325 SecretPrompt *self = SECRET_PROMPT (source);
326 GError *error = NULL;
329 retval = g_dbus_proxy_call_finish (G_DBUS_PROXY (self), result, &error);
332 g_variant_unref (retval);
333 if (closure->vanished)
334 g_clear_error (&error);
337 _secret_util_strip_remote_error (&error);
338 g_simple_async_result_take_error (res, error);
339 perform_prompt_complete (res, TRUE);
342 closure->prompting = TRUE;
343 g_atomic_int_set (&self->pv->prompted, 1);
345 /* And now we wait for the signal */
348 g_object_unref (res);
352 on_prompt_vanished (GDBusConnection *connection,
356 GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
357 PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
358 closure->vanished = TRUE;
359 g_cancellable_cancel (closure->call_cancellable);
360 perform_prompt_complete (res, TRUE);
364 on_prompt_dismissed (GObject *source,
365 GAsyncResult *result,
368 GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
369 PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
370 SecretPrompt *self = SECRET_PROMPT (source);
371 GError *error = NULL;
374 retval = g_dbus_proxy_call_finish (G_DBUS_PROXY (self), result, &error);
377 g_variant_unref (retval);
378 if (closure->vanished)
379 g_clear_error (&error);
380 if (g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD))
381 g_clear_error (&error);
384 _secret_util_strip_remote_error (&error);
385 g_simple_async_result_take_error (res, error);
386 perform_prompt_complete (res, TRUE);
389 g_object_unref (res);
393 on_prompt_cancelled (GCancellable *cancellable,
396 GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
397 PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
398 SecretPrompt *self = SECRET_PROMPT (g_async_result_get_source_object (user_data));
400 /* Instead of cancelling our dbus calls, we cancel the prompt itself via this dbus call */
402 g_dbus_proxy_call (G_DBUS_PROXY (self), "Dismiss", g_variant_new ("()"),
403 G_DBUS_CALL_FLAGS_NO_AUTO_START, -1,
404 closure->call_cancellable,
405 on_prompt_dismissed, g_object_ref (res));
407 g_object_unref (self);
411 * secret_prompt_perform:
413 * @window_id: XWindow id for parent window to be transient for
414 * @cancellable: optional cancellation object
415 * @callback: called when the operation completes
416 * @user_data: data to be passed to the callback
418 * Runs a prompt and performs the prompting. Returns %TRUE if the prompt
419 * was completed and not dismissed.
421 * If @window_id is non-zero then it is used as an XWindow id. The Secret
422 * Service can make its prompt transient for the window with this id. In some
423 * Secret Service implementations this is not possible, so the behavior
424 * depending on this should degrade gracefully.
426 * This method will return immediately and complete asynchronously.
429 secret_prompt_perform (SecretPrompt *self,
431 GCancellable *cancellable,
432 GAsyncReadyCallback callback,
435 GSimpleAsyncResult *res;
436 PerformClosure *closure;
437 const gchar *owner_name;
438 const gchar *object_path;
443 g_return_if_fail (SECRET_IS_PROMPT (self));
444 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
446 prompted = g_atomic_int_get (&self->pv->prompted);
448 g_warning ("The prompt object has already had its prompt called.");
452 proxy = G_DBUS_PROXY (self);
454 res = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
455 secret_prompt_perform);
456 closure = g_slice_new0 (PerformClosure);
457 closure->connection = g_object_ref (g_dbus_proxy_get_connection (proxy));
458 closure->call_cancellable = g_cancellable_new ();
459 closure->async_cancellable = cancellable ? g_object_ref (cancellable) : NULL;
460 g_simple_async_result_set_op_res_gpointer (res, closure, perform_closure_free);
463 window = g_strdup ("");
465 window = g_strdup_printf ("%lu", window_id);
467 owner_name = g_dbus_proxy_get_name_owner (proxy);
468 object_path = g_dbus_proxy_get_object_path (proxy);
470 closure->signal = g_dbus_connection_signal_subscribe (closure->connection, owner_name,
471 SECRET_PROMPT_INTERFACE,
472 SECRET_PROMPT_SIGNAL_COMPLETED,
474 G_DBUS_SIGNAL_FLAGS_NONE,
479 closure->watch = g_bus_watch_name_on_connection (closure->connection, owner_name,
480 G_BUS_NAME_WATCHER_FLAGS_NONE, NULL,
485 if (closure->async_cancellable) {
486 closure->cancelled_sig = g_cancellable_connect (closure->async_cancellable,
487 G_CALLBACK (on_prompt_cancelled),
491 g_dbus_proxy_call (proxy, "Prompt", g_variant_new ("(s)", window),
492 G_DBUS_CALL_FLAGS_NO_AUTO_START, -1,
493 closure->call_cancellable, on_prompt_prompted, g_object_ref (res));
496 g_object_unref (res);
500 * secret_prompt_perform_finish:
502 * @result: the asynchronous result passed to the callback
503 * @return_type: the variant type of the prompt result
504 * @error: location to place an error on failure
506 * Complete asynchronous operation to run a prompt and perform the prompting.
508 * Returns a variant result if the prompt was completed and not dismissed. The
509 * type of result depends on the action the prompt is completing, and is
510 * defined in the Secret Service DBus API specification.
512 * Returns: (transfer full): %NULL if the prompt was dismissed or an error occurred,
513 * a variant result if the prompt was successful
516 secret_prompt_perform_finish (SecretPrompt *self,
517 GAsyncResult *result,
518 const GVariantType *return_type,
521 PerformClosure *closure;
522 GSimpleAsyncResult *res;
525 g_return_val_if_fail (SECRET_IS_PROMPT (self), NULL);
526 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
527 g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self),
528 secret_prompt_perform), NULL);
530 res = G_SIMPLE_ASYNC_RESULT (result);
532 if (g_simple_async_result_propagate_error (res, error))
535 closure = g_simple_async_result_get_op_res_gpointer (res);
536 if (closure->result == NULL)
538 if (return_type != NULL && !g_variant_is_of_type (closure->result, return_type)) {
539 string = g_variant_type_dup_string (return_type);
540 g_warning ("received unexpected result type %s from Completed signal instead of expected %s",
541 g_variant_get_type_string (closure->result), string);
545 return g_variant_ref (closure->result);