[gnome-software: 27/72] icons: Make refine asynchronous




commit e11c9b8e125e1d963dad8a8f92d4f49e7d9c7707
Author: Philip Withnall <pwithnall endlessos org>
Date:   Wed Nov 24 17:11:52 2021 +0000

    icons: Make refine asynchronous
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>
    
    Helps: #1472

 plugins/core/gs-plugin-icons.c | 183 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 165 insertions(+), 18 deletions(-)
---
diff --git a/plugins/core/gs-plugin-icons.c b/plugins/core/gs-plugin-icons.c
index 2daf9df42..b28596ed0 100644
--- a/plugins/core/gs-plugin-icons.c
+++ b/plugins/core/gs-plugin-icons.c
@@ -30,10 +30,15 @@
 struct _GsPluginIcons
 {
        GsPlugin        parent;
+
+       GsWorkerThread  *worker;  /* (owned) */
 };
 
 G_DEFINE_TYPE (GsPluginIcons, gs_plugin_icons, GS_TYPE_PLUGIN)
 
+#define assert_in_worker(self) \
+       g_assert (gs_worker_thread_is_in_worker_context (self->worker))
+
 static void
 gs_plugin_icons_init (GsPluginIcons *self)
 {
@@ -41,53 +46,195 @@ gs_plugin_icons_init (GsPluginIcons *self)
        gs_plugin_add_rule (GS_PLUGIN (self), GS_PLUGIN_RULE_RUN_AFTER, "appstream");
 }
 
+static void
+gs_plugin_icons_dispose (GObject *object)
+{
+       GsPluginIcons *self = GS_PLUGIN_ICONS (object);
+
+       g_clear_object (&self->worker);
+
+       G_OBJECT_CLASS (gs_plugin_icons_parent_class)->dispose (object);
+}
+
+static void
+gs_plugin_icons_setup_async (GsPlugin            *plugin,
+                             GCancellable        *cancellable,
+                             GAsyncReadyCallback  callback,
+                             gpointer             user_data)
+{
+       GsPluginIcons *self = GS_PLUGIN_ICONS (plugin);
+       g_autoptr(GTask) task = NULL;
+
+       task = g_task_new (plugin, cancellable, callback, user_data);
+       g_task_set_source_tag (task, gs_plugin_icons_setup_async);
+
+       /* Start up a worker thread to process all the plugin’s function calls. */
+       self->worker = gs_worker_thread_new ("gs-plugin-icons");
+
+       g_task_return_boolean (task, TRUE);
+}
+
+static gboolean
+gs_plugin_icons_setup_finish (GsPlugin      *plugin,
+                              GAsyncResult  *result,
+                              GError       **error)
+{
+       return g_task_propagate_boolean (G_TASK (result), error);
+}
+
+static void shutdown_cb (GObject      *source_object,
+                         GAsyncResult *result,
+                         gpointer      user_data);
+
+static void
+gs_plugin_icons_shutdown_async (GsPlugin            *plugin,
+                                GCancellable        *cancellable,
+                                GAsyncReadyCallback  callback,
+                                gpointer             user_data)
+{
+       GsPluginIcons *self = GS_PLUGIN_ICONS (plugin);
+       g_autoptr(GTask) task = NULL;
+
+       task = g_task_new (self, cancellable, callback, user_data);
+       g_task_set_source_tag (task, gs_plugin_icons_shutdown_async);
+
+       /* Stop the worker thread. */
+       gs_worker_thread_shutdown_async (self->worker, cancellable, shutdown_cb, g_steal_pointer (&task));
+}
+
+static void
+shutdown_cb (GObject      *source_object,
+             GAsyncResult *result,
+             gpointer      user_data)
+{
+       g_autoptr(GTask) task = G_TASK (user_data);
+       GsPluginIcons *self = g_task_get_source_object (task);
+       g_autoptr(GsWorkerThread) worker = NULL;
+       g_autoptr(GError) local_error = NULL;
+
+       worker = g_steal_pointer (&self->worker);
+
+       if (!gs_worker_thread_shutdown_finish (worker, result, &local_error)) {
+               g_task_return_error (task, g_steal_pointer (&local_error));
+               return;
+       }
+
+       g_task_return_boolean (task, TRUE);
+}
+
+static gboolean
+gs_plugin_icons_shutdown_finish (GsPlugin      *plugin,
+                                 GAsyncResult  *result,
+                                 GError       **error)
+{
+       return g_task_propagate_boolean (G_TASK (result), error);
+}
+
 static gboolean
-refine_app (GsPlugin             *plugin,
-           GsApp                *app,
-           GsPluginRefineFlags   flags,
-           GCancellable         *cancellable,
-           GError              **error)
+refine_app (GsPluginIcons        *self,
+            GsApp                *app,
+            GsPluginRefineFlags   flags,
+            GCancellable         *cancellable,
+            GError              **error)
 {
        SoupSession *soup_session;
        guint maximum_icon_size;
 
+       assert_in_worker (self);
+
        /* not required */
        if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_ICON) == 0)
                return TRUE;
 
-       soup_session = gs_plugin_get_soup_session (plugin);
+       soup_session = gs_plugin_get_soup_session (GS_PLUGIN (self));
 
        /* Currently a 160px icon is needed for #GsFeatureTile, at most. */
-       maximum_icon_size = 160 * gs_plugin_get_scale (plugin);
+       maximum_icon_size = 160 * gs_plugin_get_scale (GS_PLUGIN (self));
 
        gs_app_ensure_icons_downloaded (app, soup_session, maximum_icon_size, cancellable);
 
        return TRUE;
 }
 
-gboolean
-gs_plugin_refine (GsPlugin             *plugin,
-                 GsAppList            *list,
-                 GsPluginRefineFlags   flags,
-                 GCancellable         *cancellable,
-                 GError              **error)
+static void refine_thread_cb (GTask        *task,
+                              gpointer      source_object,
+                              gpointer      task_data,
+                              GCancellable *cancellable);
+
+static void
+gs_plugin_icons_refine_async (GsPlugin            *plugin,
+                              GsAppList           *list,
+                              GsPluginRefineFlags  flags,
+                              GCancellable        *cancellable,
+                              GAsyncReadyCallback  callback,
+                              gpointer             user_data)
 {
+       GsPluginIcons *self = GS_PLUGIN_ICONS (plugin);
+       g_autoptr(GTask) task = NULL;
+
+       task = gs_plugin_refine_data_new_task (plugin, list, flags, cancellable, callback, user_data);
+       g_task_set_source_tag (task, gs_plugin_icons_refine_async);
+
        /* nothing to do here */
-       if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_ICON) == 0)
-               return TRUE;
+       if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_ICON) == 0) {
+               g_task_return_boolean (task, TRUE);
+               return;
+       }
+
+       /* Queue a job for the refine. */
+       gs_worker_thread_queue (self->worker, G_PRIORITY_DEFAULT,
+                               refine_thread_cb, g_steal_pointer (&task));
+}
+
+/* Run in @worker. */
+static void
+refine_thread_cb (GTask        *task,
+                  gpointer      source_object,
+                  gpointer      task_data,
+                  GCancellable *cancellable)
+{
+       GsPluginIcons *self = GS_PLUGIN_ICONS (source_object);
+       GsPluginRefineData *data = task_data;
+       GsAppList *list = data->list;
+       GsPluginRefineFlags flags = data->flags;
+       g_autoptr(GError) local_error = NULL;
+
+       assert_in_worker (self);
 
        for (guint i = 0; i < gs_app_list_length (list); i++) {
                GsApp *app = gs_app_list_index (list, i);
-               if (!refine_app (plugin, app, flags, cancellable, error))
-                       return FALSE;
+
+               if (!refine_app (self, app, flags, cancellable, &local_error)) {
+                       g_task_return_error (task, g_steal_pointer (&local_error));
+                       return;
+               }
        }
 
-       return TRUE;
+       g_task_return_boolean (task, TRUE);
+}
+
+static gboolean
+gs_plugin_icons_refine_finish (GsPlugin      *plugin,
+                               GAsyncResult  *result,
+                               GError       **error)
+{
+       return g_task_propagate_boolean (G_TASK (result), error);
 }
 
 static void
 gs_plugin_icons_class_init (GsPluginIconsClass *klass)
 {
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       GsPluginClass *plugin_class = GS_PLUGIN_CLASS (klass);
+
+       object_class->dispose = gs_plugin_icons_dispose;
+
+       plugin_class->setup_async = gs_plugin_icons_setup_async;
+       plugin_class->setup_finish = gs_plugin_icons_setup_finish;
+       plugin_class->shutdown_async = gs_plugin_icons_shutdown_async;
+       plugin_class->shutdown_finish = gs_plugin_icons_shutdown_finish;
+       plugin_class->refine_async = gs_plugin_icons_refine_async;
+       plugin_class->refine_finish = gs_plugin_icons_refine_finish;
 }
 
 GType


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]