[gnome-software: 9/20] gs-application: Move plugin setup to the end of the activate vfunc




commit 319b3151eb6a220b2c54793c023fac0fc07c417d
Author: Philip Withnall <pwithnall endlessos org>
Date:   Mon Mar 7 15:36:58 2022 +0000

    gs-application: Move plugin setup to the end of the activate vfunc
    
    This means that all the private objects in `GsApplication` will have
    been created before the async call starts, which means that other vfunc
    calls on the `GsApplication` in the meantime will be able to use them.
    
    In particular, this means that `app->shell` will exist by the time
    `activate()` is called (and it’s typically called right after
    `startup()`).
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>
    
    Fixes: #1670

 src/gs-application.c | 73 ++++++++++++++++++++++------------------------------
 1 file changed, 31 insertions(+), 42 deletions(-)
---
diff --git a/src/gs-application.c b/src/gs-application.c
index efeed5353..db1d4e2d1 100644
--- a/src/gs-application.c
+++ b/src/gs-application.c
@@ -152,19 +152,6 @@ gs_application_init (GsApplication *application)
        g_application_add_main_option_entries (G_APPLICATION (application), options);
 }
 
-static void
-async_result_cb (GObject      *source_object,
-                 GAsyncResult *result,
-                 gpointer      user_data)
-{
-       GAsyncResult **result_out = user_data;
-
-       g_assert (*result_out == NULL);
-       *result_out = g_object_ref (result);
-
-       g_main_context_wakeup (g_main_context_get_thread_default ());
-}
-
 static gboolean
 gs_application_dbus_register (GApplication    *application,
                               GDBusConnection *connection,
@@ -928,6 +915,10 @@ gs_application_add_wrapper_actions (GApplication *application)
        }
 }
 
+static void startup_cb (GObject      *source_object,
+                        GAsyncResult *result,
+                        gpointer      user_data);
+
 static void
 gs_application_startup (GApplication *application)
 {
@@ -935,7 +926,6 @@ gs_application_startup (GApplication *application)
        GsApplication *app = GS_APPLICATION (application);
        g_auto(GStrv) plugin_blocklist = NULL;
        g_auto(GStrv) plugin_allowlist = NULL;
-       g_autoptr(GError) error = NULL;
        const gchar *tmp;
        g_autoptr(GAsyncResult) setup_result = NULL;
 
@@ -959,34 +949,6 @@ gs_application_startup (GApplication *application)
        if (g_file_test (LOCALPLUGINDIR, G_FILE_TEST_EXISTS))
                gs_plugin_loader_add_location (app->plugin_loader, LOCALPLUGINDIR);
 
-       /* Set up the plugins. Manually iterate the thread-default #GMainContext
-        * at this point to save refactoring all this code to be async (FIXME:
-        * we should do that in future).
-        *
-        * We can’t use gs_plugin_loader_setup() from gs-plugin-loader-sync.c
-        * here because that uses a custom #GMainContext, which means that a lot
-        * of objects in plugins are initialised with the wrong #GMainContext
-        * for subsequent callbacks. */
-       gs_plugin_loader_setup_async (app->plugin_loader,
-                                     (const gchar * const *) plugin_allowlist,
-                                     (const gchar * const *) plugin_blocklist,
-                                     NULL,
-                                     async_result_cb,
-                                     &setup_result);
-
-       while (setup_result == NULL)
-               g_main_context_iteration (g_main_context_get_thread_default (), TRUE);
-
-       if (!gs_plugin_loader_setup_finish (app->plugin_loader,
-                                           setup_result,
-                                           &error)) {
-               g_warning ("Failed to setup plugins: %s", error->message);
-               exit (1);
-       }
-
-       /* show the priority of each plugin */
-       gs_plugin_loader_dump_state (app->plugin_loader);
-
        gs_shell_search_provider_setup (app->search_provider, app->plugin_loader);
 
 #ifdef HAVE_PACKAGEKIT
@@ -1013,6 +975,33 @@ gs_application_startup (GApplication *application)
        app->update_monitor = gs_update_monitor_new (app, app->plugin_loader);
 
        gs_application_update_software_sources_presence (application);
+
+       /* Set up the plugins. */
+       gs_plugin_loader_setup_async (app->plugin_loader,
+                                     (const gchar * const *) plugin_allowlist,
+                                     (const gchar * const *) plugin_blocklist,
+                                     NULL,
+                                     startup_cb,
+                                     app);
+}
+
+static void
+startup_cb (GObject      *source_object,
+            GAsyncResult *result,
+            gpointer      user_data)
+{
+       GsPluginLoader *plugin_loader = GS_PLUGIN_LOADER (source_object);
+       g_autoptr(GError) local_error = NULL;
+
+       if (!gs_plugin_loader_setup_finish (plugin_loader,
+                                           result,
+                                           &local_error)) {
+               g_warning ("Failed to setup plugins: %s", local_error->message);
+               exit (1);
+       }
+
+       /* show the priority of each plugin */
+       gs_plugin_loader_dump_state (plugin_loader);
 }
 
 static void


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