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, return_type, cancellable,
171 on_prompt_run_complete, closure);
173 g_main_loop_run (closure->loop);
175 retval = secret_prompt_perform_finish (self, closure->result, 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;
247 GVariantType *return_type;
251 perform_closure_free (gpointer data)
253 PerformClosure *closure = data;
254 g_object_unref (closure->call_cancellable);
255 g_clear_object (&closure->async_cancellable);
256 g_object_unref (closure->connection);
258 g_variant_unref (closure->result);
259 if (closure->return_type)
260 g_variant_type_free (closure->return_type);
261 g_assert (closure->signal == 0);
262 g_assert (closure->watch == 0);
263 g_slice_free (PerformClosure, closure);
267 perform_prompt_complete (GSimpleAsyncResult *res,
270 PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
272 closure->dismissed = dismissed;
273 if (closure->completed)
275 closure->completed = TRUE;
278 g_dbus_connection_signal_unsubscribe (closure->connection, closure->signal);
282 g_bus_unwatch_name (closure->watch);
285 if (closure->cancelled_sig)
286 g_signal_handler_disconnect (closure->async_cancellable, closure->cancelled_sig);
287 closure->cancelled_sig = 0;
289 g_simple_async_result_complete (res);
293 on_prompt_completed (GDBusConnection *connection,
294 const gchar *sender_name,
295 const gchar *object_path,
296 const gchar *interface_name,
297 const gchar *signal_name,
298 GVariant *parameters,
301 GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
302 SecretPrompt *self = SECRET_PROMPT (g_async_result_get_source_object (user_data));
303 PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
306 closure->prompting = FALSE;
308 if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(bv)"))) {
309 g_warning ("SecretPrompt received invalid %s signal of type %s",
310 signal_name, g_variant_get_type_string (parameters));
311 perform_prompt_complete (res, TRUE);
314 g_variant_get (parameters, "(bv)", &dismissed, &closure->result);
315 perform_prompt_complete (res, dismissed);
318 g_object_unref (self);
322 on_prompt_prompted (GObject *source,
323 GAsyncResult *result,
326 GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
327 PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
328 SecretPrompt *self = SECRET_PROMPT (source);
329 GError *error = NULL;
332 retval = g_dbus_proxy_call_finish (G_DBUS_PROXY (self), result, &error);
335 g_variant_unref (retval);
336 if (closure->vanished)
337 g_clear_error (&error);
340 g_simple_async_result_take_error (res, error);
341 perform_prompt_complete (res, TRUE);
344 closure->prompting = TRUE;
345 g_atomic_int_set (&self->pv->prompted, 1);
347 /* And now we wait for the signal */
350 g_object_unref (res);
354 on_prompt_vanished (GDBusConnection *connection,
358 GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
359 PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
360 closure->vanished = TRUE;
361 g_cancellable_cancel (closure->call_cancellable);
362 perform_prompt_complete (res, TRUE);
366 on_prompt_dismissed (GObject *source,
367 GAsyncResult *result,
370 GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
371 PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
372 SecretPrompt *self = SECRET_PROMPT (source);
373 GError *error = NULL;
376 retval = g_dbus_proxy_call_finish (G_DBUS_PROXY (self), result, &error);
379 g_variant_unref (retval);
380 if (closure->vanished)
381 g_clear_error (&error);
382 if (g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD))
383 g_clear_error (&error);
386 g_simple_async_result_take_error (res, error);
387 perform_prompt_complete (res, TRUE);
390 g_object_unref (res);
394 on_prompt_cancelled (GCancellable *cancellable,
397 GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
398 PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
399 SecretPrompt *self = SECRET_PROMPT (g_async_result_get_source_object (user_data));
401 /* Instead of cancelling our dbus calls, we cancel the prompt itself via this dbus call */
403 g_dbus_proxy_call (G_DBUS_PROXY (self), "Dismiss", g_variant_new ("()"),
404 G_DBUS_CALL_FLAGS_NO_AUTO_START, -1,
405 closure->call_cancellable,
406 on_prompt_dismissed, g_object_ref (res));
408 g_object_unref (self);
412 * secret_prompt_perform:
414 * @window_id: XWindow id for parent window to be transient for
415 * @return_type: the variant type of the prompt result
416 * @cancellable: optional cancellation object
417 * @callback: called when the operation completes
418 * @user_data: data to be passed to the callback
420 * Runs a prompt and performs the prompting. Returns %TRUE if the prompt
421 * was completed and not dismissed.
423 * If @window_id is non-zero then it is used as an XWindow id. The Secret
424 * Service can make its prompt transient for the window with this id. In some
425 * Secret Service implementations this is not possible, so the behavior
426 * depending on this should degrade gracefully.
428 * This method will return immediately and complete asynchronously.
431 secret_prompt_perform (SecretPrompt *self,
433 const GVariantType *return_type,
434 GCancellable *cancellable,
435 GAsyncReadyCallback callback,
438 GSimpleAsyncResult *res;
439 PerformClosure *closure;
440 const gchar *owner_name;
441 const gchar *object_path;
446 g_return_if_fail (SECRET_IS_PROMPT (self));
447 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
449 prompted = g_atomic_int_get (&self->pv->prompted);
451 g_warning ("The prompt object has already had its prompt called.");
455 proxy = G_DBUS_PROXY (self);
457 res = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
458 secret_prompt_perform);
459 closure = g_slice_new0 (PerformClosure);
460 closure->connection = g_object_ref (g_dbus_proxy_get_connection (proxy));
461 closure->call_cancellable = g_cancellable_new ();
462 closure->async_cancellable = cancellable ? g_object_ref (cancellable) : NULL;
463 closure->return_type = return_type ? g_variant_type_copy (return_type) : NULL;
464 g_simple_async_result_set_op_res_gpointer (res, closure, perform_closure_free);
467 window = g_strdup ("");
469 window = g_strdup_printf ("%lu", window_id);
471 owner_name = g_dbus_proxy_get_name_owner (proxy);
472 object_path = g_dbus_proxy_get_object_path (proxy);
474 closure->signal = g_dbus_connection_signal_subscribe (closure->connection, owner_name,
475 SECRET_PROMPT_INTERFACE,
476 SECRET_PROMPT_SIGNAL_COMPLETED,
478 G_DBUS_SIGNAL_FLAGS_NONE,
483 closure->watch = g_bus_watch_name_on_connection (closure->connection, owner_name,
484 G_BUS_NAME_WATCHER_FLAGS_NONE, NULL,
489 if (closure->async_cancellable) {
490 closure->cancelled_sig = g_cancellable_connect (closure->async_cancellable,
491 G_CALLBACK (on_prompt_cancelled),
495 g_dbus_proxy_call (proxy, "Prompt", g_variant_new ("(s)", window),
496 G_DBUS_CALL_FLAGS_NO_AUTO_START, -1,
497 closure->call_cancellable, on_prompt_prompted, g_object_ref (res));
500 g_object_unref (res);
504 * secret_prompt_perform_finish:
506 * @result: the asynchronous result passed to the callback
507 * @error: location to place an error on failure
509 * Complete asynchronous operation to run a prompt and perform the prompting.
511 * Returns a variant result if the prompt was completed and not dismissed. The
512 * type of result depends on the action the prompt is completing, and is
513 * defined in the Secret Service DBus API specification.
515 * Returns: (transfer full): %NULL if the prompt was dismissed or an error occurred,
516 * a variant result if the prompt was successful
519 secret_prompt_perform_finish (SecretPrompt *self,
520 GAsyncResult *result,
523 PerformClosure *closure;
524 GSimpleAsyncResult *res;
527 g_return_val_if_fail (SECRET_IS_PROMPT (self), NULL);
528 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
529 g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self),
530 secret_prompt_perform), NULL);
532 res = G_SIMPLE_ASYNC_RESULT (result);
534 if (_secret_util_propagate_error (res, error))
537 closure = g_simple_async_result_get_op_res_gpointer (res);
538 if (closure->result == NULL)
540 if (closure->return_type != NULL && !g_variant_is_of_type (closure->result, closure->return_type)) {
541 string = g_variant_type_dup_string (closure->return_type);
542 g_warning ("received unexpected result type %s from Completed signal instead of expected %s",
543 g_variant_get_type_string (closure->result), string);
547 return g_variant_ref (closure->result);