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
This commit is contained in:
Stef Walter 2011-08-13 14:03:53 +02:00
parent 7d679717f0
commit 41432cb375
4 changed files with 87 additions and 25 deletions

View File

@ -1416,12 +1416,12 @@ typedef enum _GTlsPasswordFlags
/** /**
* GTlsInteractionResult: * 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 * @G_TLS_INTERACTION_UNHANDLED: The interaction was unhandled (i.e. not
* implemented). * 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 * #GTlsInteractionResult is returned by various functions in #GTlsInteraction
* when finishing an interaction request. * when finishing an interaction request.
@ -1429,9 +1429,9 @@ typedef enum _GTlsPasswordFlags
* Since: 2.30 * Since: 2.30
*/ */
typedef enum { typedef enum {
G_TLS_INTERACTION_UNHANDLED,
G_TLS_INTERACTION_HANDLED, G_TLS_INTERACTION_HANDLED,
G_TLS_INTERACTION_ABORTED, G_TLS_INTERACTION_FAILED
G_TLS_INTERACTION_UNHANDLED
} GTlsInteractionResult; } GTlsInteractionResult;
/** /**

View File

@ -27,6 +27,7 @@
#include "gtlsinteraction.h" #include "gtlsinteraction.h"
#include "gtlspassword.h" #include "gtlspassword.h"
#include "gasyncresult.h" #include "gasyncresult.h"
#include "gcancellable.h"
#include "gsimpleasyncresult.h" #include "gsimpleasyncresult.h"
#include "gioenumtypes.h" #include "gioenumtypes.h"
#include "glibintl.h" #include "glibintl.h"
@ -70,31 +71,45 @@ G_DEFINE_TYPE (GTlsInteraction, g_tls_interaction, G_TYPE_OBJECT);
static GTlsInteractionResult static GTlsInteractionResult
g_tls_interaction_default_ask_password (GTlsInteraction *interaction, g_tls_interaction_default_ask_password (GTlsInteraction *interaction,
GTlsPassword *password) GTlsPassword *password,
GCancellable *cancellable,
GError **error)
{ {
if (g_cancellable_set_error_if_cancelled (cancellable, error))
return G_TLS_INTERACTION_FAILED;
else
return G_TLS_INTERACTION_UNHANDLED; return G_TLS_INTERACTION_UNHANDLED;
} }
static void static void
g_tls_interaction_default_ask_password_async (GTlsInteraction *interaction, g_tls_interaction_default_ask_password_async (GTlsInteraction *interaction,
GTlsPassword *password, GTlsPassword *password,
GCancellable *cancellable,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data) gpointer user_data)
{ {
GSimpleAsyncResult *res; GSimpleAsyncResult *res;
GError *error = NULL;
res = g_simple_async_result_new (G_OBJECT (interaction), callback, user_data, res = g_simple_async_result_new (G_OBJECT (interaction), callback, user_data,
g_tls_interaction_default_ask_password); 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_simple_async_result_complete_in_idle (res);
g_object_unref (res); g_object_unref (res);
} }
static GTlsInteractionResult static GTlsInteractionResult
g_tls_interaction_default_ask_password_finish (GTlsInteraction *interaction, 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_return_val_if_fail (g_simple_async_result_is_valid (result, G_OBJECT (interaction),
g_tls_interaction_default_ask_password), G_TLS_INTERACTION_UNHANDLED); 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; return G_TLS_INTERACTION_UNHANDLED;
} }
@ -115,6 +130,8 @@ g_tls_interaction_class_init (GTlsInteractionClass *klass)
* g_tls_interaction_ask_password: * g_tls_interaction_ask_password:
* @interaction: a #GTlsInteraction object * @interaction: a #GTlsInteraction object
* @password: a #GTlsPassword 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 * This function is normally called by #GTlsConnection or #GTlsDatabase to
* ask the user for a password. * 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 * be filled in and then @callback will be called. Alternatively the user may
* abort this password request, which will usually abort the TLS connection. * 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. * Returns: The status of the ask password interaction.
* *
* Since: 2.30 * Since: 2.30
*/ */
GTlsInteractionResult GTlsInteractionResult
g_tls_interaction_ask_password (GTlsInteraction *interaction, 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_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
g_return_val_if_fail (G_IS_TLS_PASSWORD (password), 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: * g_tls_interaction_ask_password_async:
* @interaction: a #GTlsInteraction object * @interaction: a #GTlsInteraction object
* @password: a #GTlsPassword 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 * @user_data: (allow-none): data to pass to the @callback
* *
* This function is normally called by #GTlsConnection or #GTlsDatabase to * 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 * g_tls_interaction_ask_password_finish() to get the status of the user
* interaction. * interaction.
* *
* Certain implementations may not support immediate cancellation.
*
* Since: 2.30 * Since: 2.30
*/ */
void void
g_tls_interaction_ask_password_async (GTlsInteraction *interaction, g_tls_interaction_ask_password_async (GTlsInteraction *interaction,
GTlsPassword *password, GTlsPassword *password,
GCancellable *cancellable,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data) gpointer user_data)
{ {
g_return_if_fail (G_IS_TLS_INTERACTION (interaction)); g_return_if_fail (G_IS_TLS_INTERACTION (interaction));
g_return_if_fail (G_IS_TLS_PASSWORD (password)); 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, G_TLS_INTERACTION_GET_CLASS (interaction)->ask_password_async (interaction, password,
cancellable,
callback, user_data); callback, user_data);
} }
@ -176,6 +208,7 @@ g_tls_interaction_ask_password_async (GTlsInteraction *interaction,
* g_tls_interaction_ask_password_finish: * g_tls_interaction_ask_password_finish:
* @interaction: a #GTlsInteraction object * @interaction: a #GTlsInteraction object
* @result: the result passed to the callback * @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 * Complete an ask password user interaction request. This should be once
* the g_tls_interaction_ask_password() completion callback is called. * 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 * If %G_TLS_INTERACTION_HANDLED is returned, then the #GTlsPassword passed
* to g_tls_interaction_ask_password() will have its password filled in. * 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. * Returns: The status of the ask password interaction.
* *
* Since: 2.30 * Since: 2.30
*/ */
GTlsInteractionResult GTlsInteractionResult
g_tls_interaction_ask_password_finish (GTlsInteraction *interaction, 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_TLS_INTERACTION (interaction), G_TLS_INTERACTION_UNHANDLED);
g_return_val_if_fail (G_IS_ASYNC_RESULT (result), 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);
} }

View File

@ -55,33 +55,41 @@ struct _GTlsInteractionClass
/* virtual methods: */ /* virtual methods: */
GTlsInteractionResult (* ask_password) (GTlsInteraction *interaction, GTlsInteractionResult (* ask_password) (GTlsInteraction *interaction,
GTlsPassword *password); GTlsPassword *password,
GCancellable *cancellable,
GError **error);
void (* ask_password_async) (GTlsInteraction *interaction, void (* ask_password_async) (GTlsInteraction *interaction,
GTlsPassword *password, GTlsPassword *password,
GCancellable *cancellable,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data); gpointer user_data);
GTlsInteractionResult (* ask_password_finish) (GTlsInteraction *interaction, GTlsInteractionResult (* ask_password_finish) (GTlsInteraction *interaction,
GAsyncResult *result); GAsyncResult *result,
GError **error);
/*< private >*/ /*< private >*/
/* Padding for future expansion */ /* Padding for future expansion */
gpointer padding[16]; gpointer padding[24];
}; };
GType g_tls_interaction_get_type (void) G_GNUC_CONST; GType g_tls_interaction_get_type (void) G_GNUC_CONST;
GTlsInteractionResult g_tls_interaction_ask_password (GTlsInteraction *interaction, 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, void g_tls_interaction_ask_password_async (GTlsInteraction *interaction,
GTlsPassword *password, GTlsPassword *password,
GCancellable *cancellable,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data); gpointer user_data);
GTlsInteractionResult g_tls_interaction_ask_password_finish (GTlsInteraction *interaction, GTlsInteractionResult g_tls_interaction_ask_password_finish (GTlsInteraction *interaction,
GAsyncResult *result); GAsyncResult *result,
GError **error);
G_END_DECLS G_END_DECLS

View File

@ -36,7 +36,9 @@ G_DEFINE_TYPE (GTlsConsoleInteraction, g_tls_console_interaction, G_TYPE_TLS_INT
static GTlsInteractionResult static GTlsInteractionResult
g_tls_console_interaction_ask_password (GTlsInteraction *interaction, g_tls_console_interaction_ask_password (GTlsInteraction *interaction,
GTlsPassword *password) GTlsPassword *password,
GCancellable *cancellable,
GError **error)
{ {
const gchar *value; const gchar *value;
gchar *prompt; gchar *prompt;
@ -45,6 +47,9 @@ g_tls_console_interaction_ask_password (GTlsInteraction *interaction,
value = getpass (prompt); value = getpass (prompt);
g_free (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); g_tls_password_set_value (password, (guchar *)value, -1);
return G_TLS_INTERACTION_HANDLED; return G_TLS_INTERACTION_HANDLED;
} }
@ -55,14 +60,19 @@ ask_password_with_getpass (GSimpleAsyncResult *res,
GCancellable *cancellable) GCancellable *cancellable)
{ {
GTlsPassword *password; GTlsPassword *password;
GError *error = NULL;
password = g_simple_async_result_get_op_res_gpointer (res); 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 void
g_tls_console_interaction_ask_password_async (GTlsInteraction *interaction, g_tls_console_interaction_ask_password_async (GTlsInteraction *interaction,
GTlsPassword *password, GTlsPassword *password,
GCancellable *cancellable,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data) gpointer user_data)
{ {
@ -72,16 +82,21 @@ g_tls_console_interaction_ask_password_async (GTlsInteraction *interaction,
g_tls_console_interaction_ask_password); 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_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_simple_async_result_run_in_thread (res, ask_password_with_getpass,
G_PRIORITY_DEFAULT, NULL); G_PRIORITY_DEFAULT, cancellable);
g_object_unref (res); g_object_unref (res);
} }
GTlsInteractionResult GTlsInteractionResult
g_tls_console_interaction_ask_password_finish (GTlsInteraction *interaction, 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_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; return G_TLS_INTERACTION_HANDLED;
} }