From 93c0b48bd9b1bffb2630cbf3e12688023c17541a Mon Sep 17 00:00:00 2001 From: Julian Bouzas Date: Tue, 23 Nov 2021 14:48:33 -0500 Subject: [PATCH] object: add new _abort_activation API Allows aborting an object activation by returning a transition error for all pending transitions. --- lib/wp/object.c | 40 ++++++++++++++++++++++++++++++++++++++++ lib/wp/object.h | 3 +++ 2 files changed, 43 insertions(+) diff --git a/lib/wp/object.c b/lib/wp/object.c index 3b56d6bf..c0ba8b2a 100644 --- a/lib/wp/object.c +++ b/lib/wp/object.c @@ -11,6 +11,7 @@ #include "object.h" #include "log.h" #include "core.h" +#include "error.h" /*! \defgroup wpfeatureactivationtransition WpFeatureActivationTransition */ /*! @@ -466,6 +467,45 @@ wp_object_deactivate (WpObject * self, WpObjectFeatures features) WP_OBJECT_GET_CLASS (self)->deactivate (self, features & priv->ft_active); } +/*! + * \brief Aborts the current object activation by returning a transition error + * if any transitions are pending. + * + * This is usually used to stop any pending activation if an error happened. + * + * \ingroup wpobject + * \param self the object + * \param msg the message used in the transition error + * \since 0.4.6 + */ +void +wp_object_abort_activation (WpObject * self, const gchar *msg) +{ + WpObjectPrivate *priv; + g_autoptr (WpTransition) t = NULL; + + g_return_if_fail (WP_IS_OBJECT (self)); + + priv = wp_object_get_instance_private (self); + + g_clear_pointer (&priv->idle_advnc_source, g_source_unref); + + /* abort ongoing transition if any */ + t = g_weak_ref_get (&priv->ongoing_transition); + if (t) + wp_transition_return_error (t, g_error_new (WP_DOMAIN_LIBRARY, + WP_LIBRARY_ERROR_OPERATION_FAILED, + "Object activation aborted: %s", msg)); + + /* abort queued transitions */ + while (!g_queue_is_empty (priv->transitions)) { + WpTransition *next = g_queue_pop_head (priv->transitions); + wp_transition_return_error (next, g_error_new (WP_DOMAIN_LIBRARY, + WP_LIBRARY_ERROR_OPERATION_FAILED, + "Object activation aborted: %s", msg)); + } +} + /*! * \brief Allows subclasses to update the currently active features. * diff --git a/lib/wp/object.h b/lib/wp/object.h index 18173f0e..21d60660 100644 --- a/lib/wp/object.h +++ b/lib/wp/object.h @@ -106,6 +106,9 @@ void wp_object_deactivate (WpObject * self, WpObjectFeatures features); /* for subclasses only */ +WP_API +void wp_object_abort_activation (WpObject * self, const gchar *msg); + WP_API void wp_object_update_features (WpObject * self, WpObjectFeatures activated, WpObjectFeatures deactivated);