2012-02-01 12:34:08 +00:00
|
|
|
/* libsecret - GLib wrapper for Secret Prompt
|
2012-01-11 06:44:32 +00:00
|
|
|
*
|
|
|
|
* Copyright 2011 Collabora Ltd.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published
|
2012-03-31 13:32:43 +00:00
|
|
|
* by the Free Software Foundation; either version 2.1 of the licence or (at
|
2012-01-11 06:44:32 +00:00
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* See the included COPYING file for more information.
|
2012-03-31 13:32:43 +00:00
|
|
|
*
|
|
|
|
* Author: Stef Walter <stefw@gnome.org>
|
2012-01-11 06:44:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
#include "secret-dbus-generated.h"
|
|
|
|
#include "secret-private.h"
|
|
|
|
#include "secret-prompt.h"
|
2012-01-11 06:44:32 +00:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <glib/gi18n-lib.h>
|
|
|
|
|
2012-02-02 13:59:59 +00:00
|
|
|
/**
|
|
|
|
* SECTION:secret-prompt
|
|
|
|
* @title: SecretPrompt
|
|
|
|
* @short_description: a prompt in the Service
|
|
|
|
*
|
|
|
|
* A #SecretPrompt represents a prompt in the Secret Service.
|
|
|
|
*
|
|
|
|
* Certain actions on the Secret Service require user prompting to complete,
|
|
|
|
* such as creating a collection, or unlocking a collection. When such a prompt
|
|
|
|
* is necessary, then a #SecretPrompt object is created by this library, and
|
2012-03-17 13:25:50 +00:00
|
|
|
* passed to the secret_service_prompt() method. In this way it is handled
|
2012-02-02 13:59:59 +00:00
|
|
|
* automatically.
|
|
|
|
*
|
|
|
|
* In order to customize prompt handling, override the
|
|
|
|
* SecretServiceClass::prompt_async and SecretServiceClass::prompt_finish
|
|
|
|
* virtual methods of the #SecretService class.
|
2012-03-24 06:52:12 +00:00
|
|
|
*
|
2012-04-27 14:57:36 +00:00
|
|
|
* Stability: Unstable
|
2012-02-02 13:59:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SecretPrompt:
|
|
|
|
*
|
|
|
|
* A proxy object representing a prompt that the Secret Service will display
|
|
|
|
* to the user.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SecretPromptClass:
|
|
|
|
* @parent_class: the parent class
|
|
|
|
*
|
|
|
|
* The class for #SecretPrompt.
|
|
|
|
*/
|
2012-01-11 06:44:32 +00:00
|
|
|
|
2012-03-30 12:59:04 +00:00
|
|
|
struct _SecretPromptPrivate {
|
2012-01-11 06:44:32 +00:00
|
|
|
gint prompted;
|
2012-03-30 12:59:04 +00:00
|
|
|
};
|
2012-01-11 06:44:32 +00:00
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
G_DEFINE_TYPE (SecretPrompt, secret_prompt, G_TYPE_DBUS_PROXY);
|
2012-01-11 06:44:32 +00:00
|
|
|
|
|
|
|
static void
|
2012-02-01 12:34:08 +00:00
|
|
|
secret_prompt_init (SecretPrompt *self)
|
2012-01-11 06:44:32 +00:00
|
|
|
{
|
2012-02-01 12:34:08 +00:00
|
|
|
self->pv = G_TYPE_INSTANCE_GET_PRIVATE (self, SECRET_TYPE_PROMPT,
|
|
|
|
SecretPromptPrivate);
|
2012-01-11 06:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-02-01 12:34:08 +00:00
|
|
|
secret_prompt_class_init (SecretPromptClass *klass)
|
2012-01-11 06:44:32 +00:00
|
|
|
{
|
2012-02-01 12:34:08 +00:00
|
|
|
g_type_class_add_private (klass, sizeof (SecretPromptPrivate));
|
2012-01-11 06:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
GMainLoop *loop;
|
|
|
|
GAsyncResult *result;
|
|
|
|
} RunClosure;
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_prompt_run_complete (GObject *source,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
RunClosure *closure = user_data;
|
|
|
|
closure->result = g_object_ref (result);
|
|
|
|
g_main_loop_quit (closure->loop);
|
|
|
|
}
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
SecretPrompt *
|
2012-02-02 12:40:47 +00:00
|
|
|
_secret_prompt_instance (SecretService *service,
|
2012-01-11 06:44:32 +00:00
|
|
|
const gchar *prompt_path)
|
|
|
|
{
|
|
|
|
GDBusProxy *proxy;
|
2012-02-01 12:34:08 +00:00
|
|
|
SecretPrompt *prompt;
|
2012-01-11 06:44:32 +00:00
|
|
|
GError *error = NULL;
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
g_return_val_if_fail (SECRET_IS_SERVICE (service), NULL);
|
2012-01-11 06:44:32 +00:00
|
|
|
g_return_val_if_fail (prompt_path != NULL, NULL);
|
|
|
|
|
|
|
|
proxy = G_DBUS_PROXY (service);
|
2012-02-01 12:34:08 +00:00
|
|
|
prompt = g_initable_new (SECRET_TYPE_PROMPT, NULL, &error,
|
2012-01-11 06:44:32 +00:00
|
|
|
"g-flags", G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
|
2012-02-01 12:34:08 +00:00
|
|
|
"g-interface-info", _secret_gen_prompt_interface_info (),
|
2012-01-11 06:44:32 +00:00
|
|
|
"g-name", g_dbus_proxy_get_name (proxy),
|
|
|
|
"g-connection", g_dbus_proxy_get_connection (proxy),
|
|
|
|
"g-object-path", prompt_path,
|
2012-02-01 12:34:08 +00:00
|
|
|
"g-interface-name", SECRET_PROMPT_INTERFACE,
|
2012-01-11 06:44:32 +00:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (error != NULL) {
|
2012-02-01 12:34:08 +00:00
|
|
|
g_warning ("couldn't create SecretPrompt object: %s", error->message);
|
2012-01-11 06:44:32 +00:00
|
|
|
g_clear_error (&error);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return prompt;
|
|
|
|
}
|
|
|
|
|
2012-02-02 12:40:47 +00:00
|
|
|
/**
|
|
|
|
* secret_prompt_run:
|
|
|
|
* @self: a prompt
|
|
|
|
* @window_id: XWindow id for parent window to be transient for
|
|
|
|
* @cancellable: optional cancellation object
|
2012-04-29 19:34:40 +00:00
|
|
|
* @return_type: the variant type of the prompt result
|
2012-02-02 12:40:47 +00:00
|
|
|
* @error: location to place an error on failure
|
|
|
|
*
|
2012-04-29 19:34:40 +00:00
|
|
|
* Runs a prompt and performs the prompting. Returns a variant result if the
|
|
|
|
* prompt was completed and not dismissed. The type of result depends on the
|
|
|
|
* action the prompt is completing, and is defined in the Secret Service DBus
|
|
|
|
* API specification.
|
2012-02-02 12:40:47 +00:00
|
|
|
*
|
|
|
|
* If @window_id is non-zero then it is used as an XWindow id. The Secret
|
|
|
|
* Service can make its prompt transient for the window with this id. In some
|
|
|
|
* Secret Service implementations this is not possible, so the behavior
|
|
|
|
* depending on this should degrade gracefully.
|
|
|
|
*
|
|
|
|
* This runs the dialog in a recursive mainloop. When run from a user interface
|
|
|
|
* thread, this means the user interface will remain responsive. Care should be
|
|
|
|
* taken that appropriate user interface actions are disabled while running the
|
|
|
|
* prompt.
|
|
|
|
*
|
2012-04-29 19:34:40 +00:00
|
|
|
* Returns: (transfer full): %NULL if the prompt was dismissed or an error occurred
|
2012-02-02 12:40:47 +00:00
|
|
|
*/
|
2012-04-29 19:34:40 +00:00
|
|
|
GVariant *
|
2012-02-01 12:34:08 +00:00
|
|
|
secret_prompt_run (SecretPrompt *self,
|
2012-02-02 12:40:47 +00:00
|
|
|
gulong window_id,
|
|
|
|
GCancellable *cancellable,
|
2012-04-29 19:34:40 +00:00
|
|
|
const GVariantType *return_type,
|
2012-02-02 12:40:47 +00:00
|
|
|
GError **error)
|
2012-01-11 06:44:32 +00:00
|
|
|
{
|
|
|
|
GMainContext *context;
|
|
|
|
RunClosure *closure;
|
2012-04-29 19:34:40 +00:00
|
|
|
GVariant *retval;
|
2012-01-11 06:44:32 +00:00
|
|
|
|
2012-04-29 19:34:40 +00:00
|
|
|
g_return_val_if_fail (SECRET_IS_PROMPT (self), NULL);
|
|
|
|
g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
2012-01-11 06:44:32 +00:00
|
|
|
|
|
|
|
context = g_main_context_get_thread_default ();
|
|
|
|
|
|
|
|
closure = g_new0 (RunClosure, 1);
|
|
|
|
closure->loop = g_main_loop_new (context, FALSE);
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
secret_prompt_perform (self, window_id, cancellable,
|
2012-02-02 12:40:47 +00:00
|
|
|
on_prompt_run_complete, closure);
|
2012-01-11 06:44:32 +00:00
|
|
|
|
|
|
|
g_main_loop_run (closure->loop);
|
|
|
|
|
2012-04-29 19:34:40 +00:00
|
|
|
retval = secret_prompt_perform_finish (self, closure->result, return_type, error);
|
2012-01-11 06:44:32 +00:00
|
|
|
|
|
|
|
g_main_loop_unref (closure->loop);
|
|
|
|
g_object_unref (closure->result);
|
|
|
|
g_free (closure);
|
|
|
|
|
2012-04-29 19:34:40 +00:00
|
|
|
return retval;
|
2012-01-11 06:44:32 +00:00
|
|
|
}
|
|
|
|
|
2012-02-02 12:40:47 +00:00
|
|
|
/**
|
|
|
|
* secret_prompt_perform_sync:
|
|
|
|
* @self: a prompt
|
|
|
|
* @window_id: XWindow id for parent window to be transient for
|
|
|
|
* @cancellable: optional cancellation object
|
2012-04-29 19:34:40 +00:00
|
|
|
* @return_type: the variant type of the prompt result
|
2012-02-02 12:40:47 +00:00
|
|
|
* @error: location to place an error on failure
|
|
|
|
*
|
2012-04-29 19:34:40 +00:00
|
|
|
* Runs a prompt and performs the prompting. Returns a variant result if the
|
|
|
|
* prompt was completed and not dismissed. The type of result depends on the
|
|
|
|
* action the prompt is completing, and is defined in the Secret Service DBus
|
|
|
|
* API specification.
|
2012-02-02 12:40:47 +00:00
|
|
|
*
|
|
|
|
* If @window_id is non-zero then it is used as an XWindow id. The Secret
|
|
|
|
* Service can make its prompt transient for the window with this id. In some
|
|
|
|
* Secret Service implementations this is not possible, so the behavior
|
|
|
|
* depending on this should degrade gracefully.
|
|
|
|
*
|
|
|
|
* This method may block indefinitely and should not be used in user interface
|
|
|
|
* threads.
|
|
|
|
*
|
2012-04-29 19:34:40 +00:00
|
|
|
* Returns: (transfer full): %NULL if the prompt was dismissed or an error occurred
|
2012-02-02 12:40:47 +00:00
|
|
|
*/
|
2012-04-29 19:34:40 +00:00
|
|
|
GVariant *
|
2012-02-01 12:34:08 +00:00
|
|
|
secret_prompt_perform_sync (SecretPrompt *self,
|
2012-02-02 12:40:47 +00:00
|
|
|
gulong window_id,
|
|
|
|
GCancellable *cancellable,
|
2012-04-29 19:34:40 +00:00
|
|
|
const GVariantType *return_type,
|
2012-02-02 12:40:47 +00:00
|
|
|
GError **error)
|
2012-01-11 06:44:32 +00:00
|
|
|
{
|
|
|
|
GMainContext *context;
|
2012-04-29 19:34:40 +00:00
|
|
|
GVariant *retval;
|
2012-01-11 06:44:32 +00:00
|
|
|
|
2012-04-29 19:34:40 +00:00
|
|
|
g_return_val_if_fail (SECRET_IS_PROMPT (self), NULL);
|
|
|
|
g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
2012-01-11 06:44:32 +00:00
|
|
|
|
|
|
|
context = g_main_context_new ();
|
|
|
|
g_main_context_push_thread_default (context);
|
|
|
|
|
2012-04-29 19:34:40 +00:00
|
|
|
retval = secret_prompt_run (self, window_id, cancellable, return_type, error);
|
2012-01-11 06:44:32 +00:00
|
|
|
|
|
|
|
/* Needed to prevent memory leaks */
|
|
|
|
while (g_main_context_iteration (context, FALSE));
|
|
|
|
|
|
|
|
g_main_context_pop_thread_default (context);
|
|
|
|
g_main_context_unref (context);
|
|
|
|
|
2012-04-29 19:34:40 +00:00
|
|
|
return retval;
|
2012-01-11 06:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
GDBusConnection *connection;
|
|
|
|
GCancellable *call_cancellable;
|
|
|
|
GCancellable *async_cancellable;
|
|
|
|
gulong cancelled_sig;
|
|
|
|
gboolean prompting;
|
|
|
|
gboolean dismissed;
|
|
|
|
gboolean vanished;
|
|
|
|
gboolean completed;
|
2012-04-29 19:34:40 +00:00
|
|
|
GVariant *result;
|
2012-01-11 06:44:32 +00:00
|
|
|
guint signal;
|
|
|
|
guint watch;
|
|
|
|
} PerformClosure;
|
|
|
|
|
|
|
|
static void
|
|
|
|
perform_closure_free (gpointer data)
|
|
|
|
{
|
|
|
|
PerformClosure *closure = data;
|
|
|
|
g_object_unref (closure->call_cancellable);
|
|
|
|
g_clear_object (&closure->async_cancellable);
|
|
|
|
g_object_unref (closure->connection);
|
2012-04-29 19:34:40 +00:00
|
|
|
if (closure->result)
|
|
|
|
g_variant_unref (closure->result);
|
2012-01-11 06:44:32 +00:00
|
|
|
g_assert (closure->signal == 0);
|
|
|
|
g_assert (closure->watch == 0);
|
|
|
|
g_slice_free (PerformClosure, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
perform_prompt_complete (GSimpleAsyncResult *res,
|
|
|
|
gboolean dismissed)
|
|
|
|
{
|
|
|
|
PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
|
|
|
|
|
|
|
|
closure->dismissed = dismissed;
|
|
|
|
if (closure->completed)
|
|
|
|
return;
|
|
|
|
closure->completed = TRUE;
|
|
|
|
|
|
|
|
if (closure->signal)
|
|
|
|
g_dbus_connection_signal_unsubscribe (closure->connection, closure->signal);
|
|
|
|
closure->signal = 0;
|
|
|
|
|
|
|
|
if (closure->watch)
|
|
|
|
g_bus_unwatch_name (closure->watch);
|
|
|
|
closure->watch = 0;
|
|
|
|
|
|
|
|
if (closure->cancelled_sig)
|
|
|
|
g_signal_handler_disconnect (closure->async_cancellable, closure->cancelled_sig);
|
|
|
|
closure->cancelled_sig = 0;
|
|
|
|
|
|
|
|
g_simple_async_result_complete (res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_prompt_completed (GDBusConnection *connection,
|
|
|
|
const gchar *sender_name,
|
|
|
|
const gchar *object_path,
|
|
|
|
const gchar *interface_name,
|
|
|
|
const gchar *signal_name,
|
|
|
|
GVariant *parameters,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
|
2012-02-01 12:34:08 +00:00
|
|
|
SecretPrompt *self = SECRET_PROMPT (g_async_result_get_source_object (user_data));
|
2012-01-11 06:44:32 +00:00
|
|
|
PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
|
|
|
|
gboolean dismissed;
|
|
|
|
|
|
|
|
closure->prompting = FALSE;
|
|
|
|
|
|
|
|
if (!g_variant_is_of_type (parameters, G_VARIANT_TYPE ("(bv)"))) {
|
2012-02-01 12:34:08 +00:00
|
|
|
g_warning ("SecretPrompt received invalid %s signal of type %s",
|
2012-01-11 06:44:32 +00:00
|
|
|
signal_name, g_variant_get_type_string (parameters));
|
|
|
|
perform_prompt_complete (res, TRUE);
|
|
|
|
|
|
|
|
} else {
|
2012-04-29 19:34:40 +00:00
|
|
|
g_variant_get (parameters, "(bv)", &dismissed, &closure->result);
|
2012-01-11 06:44:32 +00:00
|
|
|
perform_prompt_complete (res, dismissed);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_prompt_prompted (GObject *source,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
|
|
|
|
PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
|
2012-02-01 12:34:08 +00:00
|
|
|
SecretPrompt *self = SECRET_PROMPT (source);
|
2012-01-11 06:44:32 +00:00
|
|
|
GError *error = NULL;
|
|
|
|
GVariant *retval;
|
|
|
|
|
|
|
|
retval = g_dbus_proxy_call_finish (G_DBUS_PROXY (self), result, &error);
|
|
|
|
|
|
|
|
if (retval)
|
|
|
|
g_variant_unref (retval);
|
|
|
|
if (closure->vanished)
|
|
|
|
g_clear_error (&error);
|
|
|
|
|
|
|
|
if (error != NULL) {
|
2012-07-09 06:44:50 +00:00
|
|
|
_secret_util_strip_remote_error (&error);
|
2012-01-11 06:44:32 +00:00
|
|
|
g_simple_async_result_take_error (res, error);
|
|
|
|
perform_prompt_complete (res, TRUE);
|
|
|
|
|
|
|
|
} else {
|
2012-01-20 14:10:35 +00:00
|
|
|
closure->prompting = TRUE;
|
2012-04-29 19:34:40 +00:00
|
|
|
g_atomic_int_set (&self->pv->prompted, 1);
|
2012-01-11 06:44:32 +00:00
|
|
|
|
|
|
|
/* And now we wait for the signal */
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref (res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_prompt_vanished (GDBusConnection *connection,
|
|
|
|
const gchar *name,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
|
|
|
|
PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
|
|
|
|
closure->vanished = TRUE;
|
|
|
|
g_cancellable_cancel (closure->call_cancellable);
|
|
|
|
perform_prompt_complete (res, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_prompt_dismissed (GObject *source,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
|
|
|
|
PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
|
2012-02-01 12:34:08 +00:00
|
|
|
SecretPrompt *self = SECRET_PROMPT (source);
|
2012-01-11 06:44:32 +00:00
|
|
|
GError *error = NULL;
|
|
|
|
GVariant *retval;
|
|
|
|
|
|
|
|
retval = g_dbus_proxy_call_finish (G_DBUS_PROXY (self), result, &error);
|
|
|
|
|
|
|
|
if (retval)
|
|
|
|
g_variant_unref (retval);
|
|
|
|
if (closure->vanished)
|
|
|
|
g_clear_error (&error);
|
2012-01-31 19:20:31 +00:00
|
|
|
if (g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD))
|
|
|
|
g_clear_error (&error);
|
2012-01-11 06:44:32 +00:00
|
|
|
|
|
|
|
if (error != NULL) {
|
2012-07-09 06:44:50 +00:00
|
|
|
_secret_util_strip_remote_error (&error);
|
2012-01-11 06:44:32 +00:00
|
|
|
g_simple_async_result_take_error (res, error);
|
|
|
|
perform_prompt_complete (res, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref (res);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_prompt_cancelled (GCancellable *cancellable,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
|
|
|
|
PerformClosure *closure = g_simple_async_result_get_op_res_gpointer (res);
|
2012-02-01 12:34:08 +00:00
|
|
|
SecretPrompt *self = SECRET_PROMPT (g_async_result_get_source_object (user_data));
|
2012-01-11 06:44:32 +00:00
|
|
|
|
|
|
|
/* Instead of cancelling our dbus calls, we cancel the prompt itself via this dbus call */
|
|
|
|
|
|
|
|
g_dbus_proxy_call (G_DBUS_PROXY (self), "Dismiss", g_variant_new ("()"),
|
|
|
|
G_DBUS_CALL_FLAGS_NO_AUTO_START, -1,
|
|
|
|
closure->call_cancellable,
|
|
|
|
on_prompt_dismissed, g_object_ref (res));
|
|
|
|
|
|
|
|
g_object_unref (self);
|
|
|
|
}
|
|
|
|
|
2012-02-02 12:40:47 +00:00
|
|
|
/**
|
|
|
|
* secret_prompt_perform:
|
|
|
|
* @self: a prompt
|
|
|
|
* @window_id: XWindow id for parent window to be transient for
|
|
|
|
* @cancellable: optional cancellation object
|
|
|
|
* @callback: called when the operation completes
|
|
|
|
* @user_data: data to be passed to the callback
|
|
|
|
*
|
|
|
|
* Runs a prompt and performs the prompting. Returns %TRUE if the prompt
|
|
|
|
* was completed and not dismissed.
|
|
|
|
*
|
|
|
|
* If @window_id is non-zero then it is used as an XWindow id. The Secret
|
|
|
|
* Service can make its prompt transient for the window with this id. In some
|
|
|
|
* Secret Service implementations this is not possible, so the behavior
|
|
|
|
* depending on this should degrade gracefully.
|
|
|
|
*
|
|
|
|
* This method will return immediately and complete asynchronously.
|
|
|
|
*/
|
2012-01-11 06:44:32 +00:00
|
|
|
void
|
2012-02-01 12:34:08 +00:00
|
|
|
secret_prompt_perform (SecretPrompt *self,
|
2012-02-02 12:40:47 +00:00
|
|
|
gulong window_id,
|
|
|
|
GCancellable *cancellable,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
2012-01-11 06:44:32 +00:00
|
|
|
{
|
|
|
|
GSimpleAsyncResult *res;
|
|
|
|
PerformClosure *closure;
|
|
|
|
const gchar *owner_name;
|
|
|
|
const gchar *object_path;
|
2012-01-20 14:10:35 +00:00
|
|
|
gboolean prompted;
|
2012-01-11 06:44:32 +00:00
|
|
|
GDBusProxy *proxy;
|
|
|
|
gchar *window;
|
|
|
|
|
2012-02-01 12:34:08 +00:00
|
|
|
g_return_if_fail (SECRET_IS_PROMPT (self));
|
2012-01-11 06:44:32 +00:00
|
|
|
g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
|
|
|
|
|
2012-04-29 19:34:40 +00:00
|
|
|
prompted = g_atomic_int_get (&self->pv->prompted);
|
2012-01-20 14:10:35 +00:00
|
|
|
if (prompted) {
|
2012-01-11 06:44:32 +00:00
|
|
|
g_warning ("The prompt object has already had its prompt called.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy = G_DBUS_PROXY (self);
|
|
|
|
|
|
|
|
res = g_simple_async_result_new (G_OBJECT (self), callback, user_data,
|
2012-02-01 12:34:08 +00:00
|
|
|
secret_prompt_perform);
|
2012-01-11 06:44:32 +00:00
|
|
|
closure = g_slice_new0 (PerformClosure);
|
|
|
|
closure->connection = g_object_ref (g_dbus_proxy_get_connection (proxy));
|
|
|
|
closure->call_cancellable = g_cancellable_new ();
|
|
|
|
closure->async_cancellable = cancellable ? g_object_ref (cancellable) : NULL;
|
|
|
|
g_simple_async_result_set_op_res_gpointer (res, closure, perform_closure_free);
|
|
|
|
|
|
|
|
if (window_id == 0)
|
|
|
|
window = g_strdup ("");
|
|
|
|
else
|
|
|
|
window = g_strdup_printf ("%lu", window_id);
|
|
|
|
|
|
|
|
owner_name = g_dbus_proxy_get_name_owner (proxy);
|
|
|
|
object_path = g_dbus_proxy_get_object_path (proxy);
|
|
|
|
|
|
|
|
closure->signal = g_dbus_connection_signal_subscribe (closure->connection, owner_name,
|
2012-02-01 12:34:08 +00:00
|
|
|
SECRET_PROMPT_INTERFACE,
|
|
|
|
SECRET_PROMPT_SIGNAL_COMPLETED,
|
2012-01-11 06:44:32 +00:00
|
|
|
object_path, NULL,
|
|
|
|
G_DBUS_SIGNAL_FLAGS_NONE,
|
|
|
|
on_prompt_completed,
|
|
|
|
g_object_ref (res),
|
|
|
|
g_object_unref);
|
|
|
|
|
|
|
|
closure->watch = g_bus_watch_name_on_connection (closure->connection, owner_name,
|
|
|
|
G_BUS_NAME_WATCHER_FLAGS_NONE, NULL,
|
|
|
|
on_prompt_vanished,
|
|
|
|
g_object_ref (res),
|
|
|
|
g_object_unref);
|
|
|
|
|
|
|
|
if (closure->async_cancellable) {
|
|
|
|
closure->cancelled_sig = g_cancellable_connect (closure->async_cancellable,
|
|
|
|
G_CALLBACK (on_prompt_cancelled),
|
|
|
|
res, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_dbus_proxy_call (proxy, "Prompt", g_variant_new ("(s)", window),
|
|
|
|
G_DBUS_CALL_FLAGS_NO_AUTO_START, -1,
|
|
|
|
closure->call_cancellable, on_prompt_prompted, g_object_ref (res));
|
|
|
|
|
|
|
|
g_free (window);
|
|
|
|
g_object_unref (res);
|
|
|
|
}
|
|
|
|
|
2012-02-02 12:40:47 +00:00
|
|
|
/**
|
|
|
|
* secret_prompt_perform_finish:
|
|
|
|
* @self: a prompt
|
|
|
|
* @result: the asynchronous result passed to the callback
|
2012-04-29 19:34:40 +00:00
|
|
|
* @return_type: the variant type of the prompt result
|
2012-02-02 12:40:47 +00:00
|
|
|
* @error: location to place an error on failure
|
|
|
|
*
|
|
|
|
* Complete asynchronous operation to run a prompt and perform the prompting.
|
|
|
|
*
|
2012-04-29 19:34:40 +00:00
|
|
|
* Returns a variant result if the prompt was completed and not dismissed. The
|
|
|
|
* type of result depends on the action the prompt is completing, and is
|
|
|
|
* defined in the Secret Service DBus API specification.
|
|
|
|
*
|
|
|
|
* Returns: (transfer full): %NULL if the prompt was dismissed or an error occurred,
|
|
|
|
* a variant result if the prompt was successful
|
2012-02-02 12:40:47 +00:00
|
|
|
*/
|
2012-04-29 19:34:40 +00:00
|
|
|
GVariant *
|
2012-02-01 12:34:08 +00:00
|
|
|
secret_prompt_perform_finish (SecretPrompt *self,
|
2012-02-02 12:40:47 +00:00
|
|
|
GAsyncResult *result,
|
2012-04-29 19:34:40 +00:00
|
|
|
const GVariantType *return_type,
|
2012-02-02 12:40:47 +00:00
|
|
|
GError **error)
|
2012-01-11 06:44:32 +00:00
|
|
|
{
|
|
|
|
PerformClosure *closure;
|
|
|
|
GSimpleAsyncResult *res;
|
2012-04-29 19:34:40 +00:00
|
|
|
gchar *string;
|
2012-01-11 06:44:32 +00:00
|
|
|
|
2012-04-29 19:34:40 +00:00
|
|
|
g_return_val_if_fail (SECRET_IS_PROMPT (self), NULL);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
2012-01-11 06:44:32 +00:00
|
|
|
g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (self),
|
2012-04-29 19:34:40 +00:00
|
|
|
secret_prompt_perform), NULL);
|
2012-01-11 06:44:32 +00:00
|
|
|
|
|
|
|
res = G_SIMPLE_ASYNC_RESULT (result);
|
|
|
|
|
|
|
|
if (g_simple_async_result_propagate_error (res, error))
|
2012-04-29 19:34:40 +00:00
|
|
|
return NULL;
|
2012-01-11 06:44:32 +00:00
|
|
|
|
|
|
|
closure = g_simple_async_result_get_op_res_gpointer (res);
|
2012-04-29 19:34:40 +00:00
|
|
|
if (closure->result == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (return_type != NULL && !g_variant_is_of_type (closure->result, return_type)) {
|
|
|
|
string = g_variant_type_dup_string (return_type);
|
2012-01-16 14:08:29 +00:00
|
|
|
g_warning ("received unexpected result type %s from Completed signal instead of expected %s",
|
2012-04-29 19:34:40 +00:00
|
|
|
g_variant_get_type_string (closure->result), string);
|
2012-01-16 14:08:29 +00:00
|
|
|
g_free (string);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-04-29 19:34:40 +00:00
|
|
|
return g_variant_ref (closure->result);
|
2012-01-11 06:44:32 +00:00
|
|
|
}
|