From 41432cb375eb04729c8fe506c40ce79fc803028b Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Sat, 13 Aug 2011 14:03:53 +0200 Subject: [PATCH] Make GTlsInteraction virtual methods cancellable * Add cancellable argument to g_tls_interaction_ask_password and g_tls_interaction_ask_password_async. * This is API breakage, but this API has not yet been released in a stable release (and very unlikely used yet). * Since we're breaking unreleased API, expand amount of padding on GTlsInteractionClass because we're going to need it. https://bugzilla.gnome.org/show_bug.cgi?id=656443 --- gio/gioenums.h | 12 ++++---- gio/gtlsinteraction.c | 57 ++++++++++++++++++++++++++++++++------ gio/gtlsinteraction.h | 18 ++++++++---- gio/tests/gtlsconsoleinteraction.c | 25 +++++++++++++---- 4 files changed, 87 insertions(+), 25 deletions(-) diff --git a/gio/gioenums.h b/gio/gioenums.h index 4130fe1..c0ba757 100644 --- a/gio/gioenums.h +++ b/gio/gioenums.h @@ -1416,12 +1416,12 @@ typedef enum _GTlsPasswordFlags /** * GTlsInteractionResult: - * @G_TLS_INTERACTION_HANDLED: The interaction completed, and resulting data - * is available. - * @G_TLS_INTERACTION_ABORTED: The user cancelled the interaction, and requested - * the operation to be aborted. * @G_TLS_INTERACTION_UNHANDLED: The interaction was unhandled (i.e. not * implemented). + * @G_TLS_INTERACTION_HANDLED: The interaction completed, and resulting data + * is available. + * @G_TLS_INTERACTION_FAILED: The interaction has failed, or was cancelled. + * and the operation should be aborted. * * #GTlsInteractionResult is returned by various functions in #GTlsInteraction * when finishing an interaction request. @@ -1429,9 +1429,9 @@ typedef enum _GTlsPasswordFlags * Since: 2.30 */ typedef enum { + G_TLS_INTERACTION_UNHANDLED, G_TLS_INTERACTION_HANDLED, - G_TLS_INTERACTION_ABORTED, - G_TLS_INTERACTION_UNHANDLED + G_TLS_INTERACTION_FAILED } GTlsInteractionResult; /** diff --git a/gio/gtlsinteraction.c b/gio/gtlsinteraction.c index 12aaab3..3c8be53 100644 --- a/gio/gtlsinteraction.c +++ b/gio/gtlsinteraction.c @@ -27,6 +27,7 @@ #include "gtlsinteraction.h" #include "gtlspassword.h" #include "gasyncresult.h" +#include "gcancellable.h" #include "gsimpleasyncresult.h" #include "gioenumtypes.h" #include "glibintl.h" @@ -70,31 +71,45 @@ G_DEFINE_TYPE (GTlsInteraction, g_tls_interaction, G_TYPE_OBJECT); static GTlsInteractionResult g_tls_interaction_default_ask_password (GTlsInteraction *interaction, - GTlsPassword *password) + GTlsPassword *password, + GCancellable *cancellable, + GError **error) { - return G_TLS_INTERACTION_UNHANDLED; + if (g_cancellable_set_error_if_cancelled (cancellable, error)) + return G_TLS_INTERACTION_FAILED; + else + return G_TLS_INTERACTION_UNHANDLED; } static void g_tls_interaction_default_ask_password_async (GTlsInteraction *interaction, GTlsPassword *password, + GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) { GSimpleAsyncResult *res; + GError *error = NULL; res = g_simple_async_result_new (G_OBJECT (interaction), callback, user_data, g_tls_interaction_default_ask_password); + if (g_cancellable_set_error_if_cancelled (cancellable, &error)) + g_simple_async_result_take_error (res, error); g_simple_async_result_complete_in_idle (res); g_object_unref (res); } static GTlsInteractionResult g_tls_interaction_default_ask_password_finish (GTlsInteraction *interaction, - GAsyncResult *result) + GAsyncResult *result, + GError **error) { g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (interaction), g_tls_interaction_default_ask_password), G_TLS_INTERACTION_UNHANDLED); + + if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error)) + return G_TLS_INTERACTION_FAILED; + return G_TLS_INTERACTION_UNHANDLED; } @@ -115,6 +130,8 @@ g_tls_interaction_class_init (GTlsInteractionClass *klass) * g_tls_interaction_ask_password: * @interaction: a #GTlsInteraction object * @password: a #GTlsPassword object + * @cancellable: an optional #GCancellable cancellation object + * @error: an optional location to place an error on failure * * This function is normally called by #GTlsConnection or #GTlsDatabase to * ask the user for a password. @@ -124,24 +141,35 @@ g_tls_interaction_class_init (GTlsInteractionClass *klass) * be filled in and then @callback will be called. Alternatively the user may * abort this password request, which will usually abort the TLS connection. * + * If the interaction is cancelled by the cancellation object, or by the + * user then %G_TLS_INTERACTION_ABORTED will be returned. Certain + * implementations may not support immediate cancellation. + * * Returns: The status of the ask password interaction. * * Since: 2.30 */ GTlsInteractionResult g_tls_interaction_ask_password (GTlsInteraction *interaction, - GTlsPassword *password) + GTlsPassword *password, + GCancellable *cancellable, + GError **error) { g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED); g_return_val_if_fail (G_IS_TLS_PASSWORD (password), G_TLS_INTERACTION_UNHANDLED); - return G_TLS_INTERACTION_GET_CLASS (interaction)->ask_password (interaction, password); + g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), G_TLS_INTERACTION_UNHANDLED); + return G_TLS_INTERACTION_GET_CLASS (interaction)->ask_password (interaction, + password, + cancellable, + error); } /** * g_tls_interaction_ask_password_async: * @interaction: a #GTlsInteraction object * @password: a #GTlsPassword object - * @callback: will be called when the interaction completes + * @cancellable: an optional #GCancellable cancellation object + * @callback: (allow-none): will be called when the interaction completes * @user_data: (allow-none): data to pass to the @callback * * This function is normally called by #GTlsConnection or #GTlsDatabase to @@ -157,18 +185,22 @@ g_tls_interaction_ask_password (GTlsInteraction *interaction, * g_tls_interaction_ask_password_finish() to get the status of the user * interaction. * + * Certain implementations may not support immediate cancellation. + * * Since: 2.30 */ void g_tls_interaction_ask_password_async (GTlsInteraction *interaction, GTlsPassword *password, + GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) { g_return_if_fail (G_IS_TLS_INTERACTION (interaction)); g_return_if_fail (G_IS_TLS_PASSWORD (password)); - g_return_if_fail (callback != NULL); + g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable)); G_TLS_INTERACTION_GET_CLASS (interaction)->ask_password_async (interaction, password, + cancellable, callback, user_data); } @@ -176,6 +208,7 @@ g_tls_interaction_ask_password_async (GTlsInteraction *interaction, * g_tls_interaction_ask_password_finish: * @interaction: a #GTlsInteraction object * @result: the result passed to the callback + * @error: an optional location to place an error on failure * * Complete an ask password user interaction request. This should be once * the g_tls_interaction_ask_password() completion callback is called. @@ -183,15 +216,21 @@ g_tls_interaction_ask_password_async (GTlsInteraction *interaction, * If %G_TLS_INTERACTION_HANDLED is returned, then the #GTlsPassword passed * to g_tls_interaction_ask_password() will have its password filled in. * + * If the interaction is cancelled by the cancellation object, or by the + * user then %G_TLS_INTERACTION_ABORTED will be returned. + * * Returns: The status of the ask password interaction. * * Since: 2.30 */ GTlsInteractionResult g_tls_interaction_ask_password_finish (GTlsInteraction *interaction, - GAsyncResult *result) + GAsyncResult *result, + GError **error) { g_return_val_if_fail (G_IS_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED); g_return_val_if_fail (G_IS_ASYNC_RESULT (result), G_TLS_INTERACTION_UNHANDLED); - return G_TLS_INTERACTION_GET_CLASS (interaction)->ask_password_finish (interaction, result); + return G_TLS_INTERACTION_GET_CLASS (interaction)->ask_password_finish (interaction, + result, + error); } diff --git a/gio/gtlsinteraction.h b/gio/gtlsinteraction.h index 19e234e..06fb52e 100644 --- a/gio/gtlsinteraction.h +++ b/gio/gtlsinteraction.h @@ -55,33 +55,41 @@ struct _GTlsInteractionClass /* virtual methods: */ GTlsInteractionResult (* ask_password) (GTlsInteraction *interaction, - GTlsPassword *password); + GTlsPassword *password, + GCancellable *cancellable, + GError **error); void (* ask_password_async) (GTlsInteraction *interaction, GTlsPassword *password, + GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); GTlsInteractionResult (* ask_password_finish) (GTlsInteraction *interaction, - GAsyncResult *result); + GAsyncResult *result, + GError **error); /*< private >*/ /* Padding for future expansion */ - gpointer padding[16]; + gpointer padding[24]; }; GType g_tls_interaction_get_type (void) G_GNUC_CONST; GTlsInteractionResult g_tls_interaction_ask_password (GTlsInteraction *interaction, - GTlsPassword *password); + GTlsPassword *password, + GCancellable *cancellable, + GError **error); void g_tls_interaction_ask_password_async (GTlsInteraction *interaction, GTlsPassword *password, + GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); GTlsInteractionResult g_tls_interaction_ask_password_finish (GTlsInteraction *interaction, - GAsyncResult *result); + GAsyncResult *result, + GError **error); G_END_DECLS diff --git a/gio/tests/gtlsconsoleinteraction.c b/gio/tests/gtlsconsoleinteraction.c index 470e6db..c56ca6a 100644 --- a/gio/tests/gtlsconsoleinteraction.c +++ b/gio/tests/gtlsconsoleinteraction.c @@ -36,7 +36,9 @@ G_DEFINE_TYPE (GTlsConsoleInteraction, g_tls_console_interaction, G_TYPE_TLS_INT static GTlsInteractionResult g_tls_console_interaction_ask_password (GTlsInteraction *interaction, - GTlsPassword *password) + GTlsPassword *password, + GCancellable *cancellable, + GError **error) { const gchar *value; gchar *prompt; @@ -45,6 +47,9 @@ g_tls_console_interaction_ask_password (GTlsInteraction *interaction, value = getpass (prompt); g_free (prompt); + if (g_cancellable_set_error_if_cancelled (cancellable, error)) + return G_TLS_INTERACTION_FAILED; + g_tls_password_set_value (password, (guchar *)value, -1); return G_TLS_INTERACTION_HANDLED; } @@ -55,14 +60,19 @@ ask_password_with_getpass (GSimpleAsyncResult *res, GCancellable *cancellable) { GTlsPassword *password; + GError *error = NULL; password = g_simple_async_result_get_op_res_gpointer (res); - g_tls_console_interaction_ask_password (G_TLS_INTERACTION (object), password); + g_tls_console_interaction_ask_password (G_TLS_INTERACTION (object), password, + cancellable, &error); + if (error != NULL) + g_simple_async_result_take_error (res, error); } void g_tls_console_interaction_ask_password_async (GTlsInteraction *interaction, GTlsPassword *password, + GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) { @@ -72,16 +82,21 @@ g_tls_console_interaction_ask_password_async (GTlsInteraction *interaction, g_tls_console_interaction_ask_password); g_simple_async_result_set_op_res_gpointer (res, g_object_ref (password), g_object_unref); g_simple_async_result_run_in_thread (res, ask_password_with_getpass, - G_PRIORITY_DEFAULT, NULL); + G_PRIORITY_DEFAULT, cancellable); g_object_unref (res); } GTlsInteractionResult g_tls_console_interaction_ask_password_finish (GTlsInteraction *interaction, - GAsyncResult *result) + GAsyncResult *result, + GError **error) { g_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (interaction), - g_tls_console_interaction_ask_password), G_TLS_INTERACTION_ABORTED); + g_tls_console_interaction_ask_password), G_TLS_INTERACTION_FAILED); + + if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result), error)) + return G_TLS_INTERACTION_FAILED; + return G_TLS_INTERACTION_HANDLED; } -- 2.7.4