[gnome-software: 1/6] snap: Use top-level SnapdClient instances in all vfuncs




commit 3866191ded1eea4ebe65477d16f94eb2e79a6b18
Author: Philip Withnall <pwithnall endlessos org>
Date:   Thu Mar 3 21:35:59 2022 +0000

    snap: Use top-level SnapdClient instances in all vfuncs
    
    Move `get_client()` calls out of helper methods. This avoids multiple
    clients being created during a single call into the plugin, which should
    speed things up a bit.
    
    It also means that the client state and properties get preserved, which
    will help in an upcoming commit to improve handling of interactive state.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 plugins/snap/gs-plugin-snap.c | 82 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 61 insertions(+), 21 deletions(-)
---
diff --git a/plugins/snap/gs-plugin-snap.c b/plugins/snap/gs-plugin-snap.c
index b581e3497..d3a3e40a1 100644
--- a/plugins/snap/gs-plugin-snap.c
+++ b/plugins/snap/gs-plugin-snap.c
@@ -332,19 +332,15 @@ store_snap_cache_update (GsPluginSnap *self,
 
 static GPtrArray *
 find_snaps (GsPluginSnap    *self,
+            SnapdClient     *client,
             SnapdFindFlags   flags,
             const gchar     *section,
             const gchar     *query,
             GCancellable    *cancellable,
             GError         **error)
 {
-       g_autoptr(SnapdClient) client = NULL;
        g_autoptr(GPtrArray) snaps = NULL;
 
-       client = get_client (self, error);
-       if (client == NULL)
-               return NULL;
-
        snaps = snapd_client_find_section_sync (client, flags, section, query, NULL, cancellable, error);
        if (snaps == NULL) {
                snapd_error_convert (error);
@@ -424,6 +420,7 @@ gs_plugin_url_to_app (GsPlugin *plugin,
                      GError **error)
 {
        GsPluginSnap *self = GS_PLUGIN_SNAP (plugin);
+       g_autoptr(SnapdClient) client = NULL;
        g_autofree gchar *scheme = NULL;
        g_autofree gchar *path = NULL;
        g_autoptr(GPtrArray) snaps = NULL;
@@ -435,13 +432,22 @@ gs_plugin_url_to_app (GsPlugin *plugin,
            g_strcmp0 (scheme, "appstream") != 0)
                return TRUE;
 
+       /* Create client. */
+       client = get_client (self, error);
+       if (client == NULL)
+               return FALSE;
+
        /* create app */
        path = gs_utils_get_url_path (url);
-       snaps = find_snaps (self, SNAPD_FIND_FLAGS_SCOPE_WIDE | SNAPD_FIND_FLAGS_MATCH_NAME, NULL, path, 
cancellable, NULL);
+       snaps = find_snaps (self, client,
+                           SNAPD_FIND_FLAGS_SCOPE_WIDE | SNAPD_FIND_FLAGS_MATCH_NAME,
+                           NULL, path, cancellable, NULL);
        if (snaps == NULL || snaps->len < 1) {
                g_clear_pointer (&snaps, g_ptr_array_unref);
                /* This works for the appstream:// URL-s */
-               snaps = find_snaps (self, SNAPD_FIND_FLAGS_SCOPE_WIDE | SNAPD_FIND_FLAGS_MATCH_COMMON_ID, 
NULL, path, cancellable, NULL);
+               snaps = find_snaps (self, client,
+                                   SNAPD_FIND_FLAGS_SCOPE_WIDE | SNAPD_FIND_FLAGS_MATCH_COMMON_ID,
+                                   NULL, path, cancellable, NULL);
        }
        if (snaps == NULL || snaps->len < 1)
                return TRUE;
@@ -503,10 +509,17 @@ gs_plugin_add_popular (GsPlugin *plugin,
                       GError **error)
 {
        GsPluginSnap *self = GS_PLUGIN_SNAP (plugin);
+       g_autoptr(SnapdClient) client = NULL;
        g_autoptr(GPtrArray) snaps = NULL;
        guint i;
 
-       snaps = find_snaps (self, SNAPD_FIND_FLAGS_SCOPE_WIDE, "featured", NULL, cancellable, error);
+       /* Create client. */
+       client = get_client (self, error);
+       if (client == NULL)
+               return FALSE;
+
+       snaps = find_snaps (self, client, SNAPD_FIND_FLAGS_SCOPE_WIDE, "featured",
+                           NULL, cancellable, error);
        if (snaps == NULL)
                return FALSE;
 
@@ -526,10 +539,16 @@ gs_plugin_add_category_apps (GsPlugin *plugin,
                             GError **error)
 {
        GsPluginSnap *self = GS_PLUGIN_SNAP (plugin);
+       g_autoptr(SnapdClient) client = NULL;
        GsCategory *c;
        g_autoptr(GString) id = NULL;
        const gchar *sections = NULL;
 
+       /* Create client. */
+       client = get_client (self, error);
+       if (client == NULL)
+               return FALSE;
+
        id = g_string_new ("");
        for (c = category; c != NULL; c = gs_category_get_parent (c)) {
                if (c != category)
@@ -570,7 +589,8 @@ gs_plugin_add_category_apps (GsPlugin *plugin,
                        g_autoptr(GPtrArray) snaps = NULL;
                        guint j;
 
-                       snaps = find_snaps (self, SNAPD_FIND_FLAGS_SCOPE_WIDE, tokens[i], NULL, cancellable, 
error);
+                       snaps = find_snaps (self, client, SNAPD_FIND_FLAGS_SCOPE_WIDE,
+                                           tokens[i], NULL, cancellable, error);
                        if (snaps == NULL)
                                return FALSE;
                        for (j = 0; j < snaps->len; j++) {
@@ -655,12 +675,19 @@ gs_plugin_add_search (GsPlugin *plugin,
                      GError **error)
 {
        GsPluginSnap *self = GS_PLUGIN_SNAP (plugin);
+       g_autoptr(SnapdClient) client = NULL;
        g_autofree gchar *query = NULL;
        g_autoptr(GPtrArray) snaps = NULL;
        guint i;
 
+       /* Create client. */
+       client = get_client (self, error);
+       if (client == NULL)
+               return FALSE;
+
        query = g_strjoinv (" ", values);
-       snaps = find_snaps (self, SNAPD_FIND_FLAGS_SCOPE_WIDE, NULL, query, cancellable, error);
+       snaps = find_snaps (self, client, SNAPD_FIND_FLAGS_SCOPE_WIDE, NULL,
+                           query, cancellable, error);
        if (snaps == NULL)
                return FALSE;
 
@@ -674,6 +701,7 @@ gs_plugin_add_search (GsPlugin *plugin,
 
 static SnapdSnap *
 get_store_snap (GsPluginSnap  *self,
+                SnapdClient   *client,
                 const gchar   *name,
                 gboolean       need_details,
                 GCancellable  *cancellable,
@@ -687,7 +715,9 @@ get_store_snap (GsPluginSnap  *self,
        if (snap != NULL)
                return g_object_ref (snap);
 
-       snaps = find_snaps (self, SNAPD_FIND_FLAGS_SCOPE_WIDE | SNAPD_FIND_FLAGS_MATCH_NAME, NULL, name, 
cancellable, error);
+       snaps = find_snaps (self, client,
+                           SNAPD_FIND_FLAGS_SCOPE_WIDE | SNAPD_FIND_FLAGS_MATCH_NAME,
+                           NULL, name, cancellable, error);
        if (snaps == NULL || snaps->len < 1)
                return NULL;
 
@@ -797,6 +827,11 @@ gs_plugin_add_alternates (GsPlugin *plugin,
                          GError **error)
 {
        GsPluginSnap *self = GS_PLUGIN_SNAP (plugin);
+       g_autoptr(SnapdClient) client = NULL;
+
+       client = get_client (self, error);
+       if (client == NULL)
+               return FALSE;
 
        /* If it is a snap, find the channels that snap provides, otherwise find snaps that match on common 
id */
        if (gs_app_has_management_plugin (app, plugin)) {
@@ -805,7 +840,7 @@ gs_plugin_add_alternates (GsPlugin *plugin,
 
                snap_name = gs_app_get_metadata_item (app, "snap::name");
 
-               snap = get_store_snap (self, snap_name, TRUE, cancellable, NULL);
+               snap = get_store_snap (self, client, snap_name, TRUE, cancellable, NULL);
                if (snap == NULL) {
                        g_warning ("Failed to get store snap %s\n", snap_name);
                        return TRUE;
@@ -816,12 +851,15 @@ gs_plugin_add_alternates (GsPlugin *plugin,
                g_autoptr(GPtrArray) snaps = NULL;
                guint i;
 
-               snaps = find_snaps (self, SNAPD_FIND_FLAGS_SCOPE_WIDE | SNAPD_FIND_FLAGS_MATCH_COMMON_ID, 
NULL, gs_app_get_id (app), cancellable, NULL);
+               snaps = find_snaps (self, client,
+                                   SNAPD_FIND_FLAGS_SCOPE_WIDE | SNAPD_FIND_FLAGS_MATCH_COMMON_ID,
+                                   NULL, gs_app_get_id (app), cancellable, NULL);
                for (i = 0; snaps != NULL && i < snaps->len; i++) {
                        SnapdSnap *snap = g_ptr_array_index (snaps, i);
                        SnapdSnap *store_snap;
 
-                       store_snap = get_store_snap (self, snapd_snap_get_name (snap), TRUE, cancellable, 
NULL);
+                       store_snap = get_store_snap (self, client, snapd_snap_get_name (snap),
+                                                    TRUE, cancellable, NULL);
                        add_channels (self, store_snap, list);
                }
                return TRUE;
@@ -1173,7 +1211,8 @@ get_snaps_cb (GObject      *object,
                        need_details = TRUE;
                if (need_details) {
                        g_clear_object (&store_snap);
-                       store_snap = get_store_snap (self, snap_name, need_details, cancellable, NULL);
+                       store_snap = get_store_snap (self, client, snap_name, need_details,
+                                                    cancellable, NULL);
                }
 
                /* we don't know anything about this snap */
@@ -1433,18 +1472,14 @@ gs_plugin_app_install (GsPlugin *plugin,
 // https://bugs.launchpad.net/bugs/1595023
 static gboolean
 is_graphical (GsPluginSnap *self,
+              SnapdClient  *client,
               GsApp        *app,
               GCancellable *cancellable)
 {
-       g_autoptr(SnapdClient) client = NULL;
        g_autoptr(GPtrArray) plugs = NULL;
        guint i;
        g_autoptr(GError) error = NULL;
 
-       client = get_client (self, &error);
-       if (client == NULL)
-               return FALSE;
-
        if (!snapd_client_get_connections2_sync (client,
                                                 SNAPD_GET_CONNECTIONS_FLAGS_SELECT_ALL, NULL, NULL,
                                                 NULL, NULL, &plugs, NULL,
@@ -1496,6 +1531,7 @@ gs_plugin_launch (GsPlugin *plugin,
                info = (GAppInfo *)g_desktop_app_info_new_from_filename (launch_desktop);
        } else {
                g_autofree gchar *commandline = NULL;
+               g_autoptr(SnapdClient) client = NULL;
                GAppInfoCreateFlags flags = G_APP_INFO_CREATE_NONE;
 
                if (g_strcmp0 (launch_name, gs_app_get_metadata_item (app, "snap::name")) == 0)
@@ -1503,7 +1539,11 @@ gs_plugin_launch (GsPlugin *plugin,
                else
                        commandline = g_strdup_printf ("snap run %s.%s", gs_app_get_metadata_item (app, 
"snap::name"), launch_name);
 
-               if (!is_graphical (self, app, cancellable))
+               client = get_client (self, error);
+               if (client == NULL)
+                       return FALSE;
+
+               if (!is_graphical (self, client, app, cancellable))
                        flags |= G_APP_INFO_CREATE_NEEDS_TERMINAL;
                info = g_app_info_create_from_commandline (commandline, NULL, flags, error);
        }


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