[gnome-software] snapd: Allow snapd operations to be cancelled



commit 66acadc9e4876fedf8a6234d65a51af6983be57b
Author: Robert Ancell <robert ancell canonical com>
Date:   Mon Jul 4 14:56:06 2016 +1200

    snapd: Allow snapd operations to be cancelled

 src/plugins/gs-plugin-snap.c |   34 ++++++++++++++++++----------------
 src/plugins/gs-snapd.c       |   18 ++++++++++++------
 src/plugins/gs-snapd.h       |    1 +
 3 files changed, 31 insertions(+), 22 deletions(-)
---
diff --git a/src/plugins/gs-plugin-snap.c b/src/plugins/gs-plugin-snap.c
index 52df0ba..ade9b9e 100644
--- a/src/plugins/gs-plugin-snap.c
+++ b/src/plugins/gs-plugin-snap.c
@@ -84,7 +84,7 @@ parse_result (const gchar *response, const gchar *response_type, GError **error)
 }
 
 static void
-refine_app (GsPlugin *plugin, GsApp *app, JsonObject *package)
+refine_app (GsPlugin *plugin, GsApp *app, JsonObject *package, GCancellable *cancellable)
 {
        const gchar *status, *icon_url;
        g_autoptr(GdkPixbuf) icon_pixbuf = NULL;
@@ -127,7 +127,7 @@ refine_app (GsPlugin *plugin, GsApp *app, JsonObject *package)
                if (gs_snapd_request ("GET", icon_url, NULL,
                                      NULL, NULL,
                                      NULL, &icon_response, &icon_response_length,
-                                     NULL)) {
+                                     cancellable, NULL)) {
                        g_autoptr(GdkPixbufLoader) loader = NULL;
 
                        loader = gdk_pixbuf_loader_new ();
@@ -175,6 +175,7 @@ get_apps (GsPlugin *plugin,
          GsAppList *list,
          AppFilterFunc filter_func,
          gpointer user_data,
+         GCancellable *cancellable,
          GError **error)
 {
        guint status_code;
@@ -210,7 +211,7 @@ get_apps (GsPlugin *plugin,
        if (!gs_snapd_request ("GET", path->str, NULL,
                               &status_code, &reason_phrase,
                               &response_type, &response, NULL,
-                              error))
+                              cancellable, error))
                return FALSE;
 
        if (status_code != SOUP_STATUS_OK) {
@@ -245,7 +246,7 @@ get_apps (GsPlugin *plugin,
                gs_app_set_kind (app, AS_APP_KIND_DESKTOP);
                gs_app_add_quirk (app, AS_APP_QUIRK_NOT_REVIEWABLE);
                gs_app_add_quirk (app, AS_APP_QUIRK_NOT_LAUNCHABLE);
-               refine_app (plugin, app, package);
+               refine_app (plugin, app, package, cancellable);
                gs_app_list_add (list, app);
        }
 
@@ -255,7 +256,7 @@ get_apps (GsPlugin *plugin,
 }
 
 static gboolean
-get_app (GsPlugin *plugin, GsApp *app, GError **error)
+get_app (GsPlugin *plugin, GsApp *app, GCancellable *cancellable, GError **error)
 {
        guint status_code;
        g_autofree gchar *path = NULL;
@@ -274,7 +275,7 @@ get_app (GsPlugin *plugin, GsApp *app, GError **error)
        if (!gs_snapd_request ("GET", path, NULL,
                               &status_code, &reason_phrase,
                               &response_type, &response, NULL,
-                              error))
+                              cancellable, error))
                return FALSE;
 
        if (status_code == SOUP_STATUS_NOT_FOUND) {
@@ -287,7 +288,7 @@ get_app (GsPlugin *plugin, GsApp *app, GError **error)
                if (!gs_snapd_request ("GET", path, NULL,
                                       &status_code, &reason_phrase,
                                       &response_type, &response, NULL,
-                                      error))
+                                      cancellable, error))
                        return FALSE;
        }
 
@@ -322,13 +323,13 @@ get_app (GsPlugin *plugin, GsApp *app, GError **error)
                        result_object = json_node_get_object (result_element);
                        if (g_strcmp0 (json_object_get_string_member (result_object, "name"),
                                       gs_app_get_id (app)) == 0) {
-                               refine_app (plugin, app, result_object);
+                               refine_app (plugin, app, result_object, cancellable);
                                break;
                        }
                }
        } else if (JSON_NODE_HOLDS_OBJECT (result)) {
                result_object = json_node_get_object (result);
-               refine_app (plugin, app, result_object);
+               refine_app (plugin, app, result_object, cancellable);
        }
 
        return TRUE;
@@ -352,7 +353,7 @@ gs_plugin_add_installed (GsPlugin *plugin,
                         GCancellable *cancellable,
                         GError **error)
 {
-       return get_apps (plugin, "local", NULL, list, is_active, NULL, error);
+       return get_apps (plugin, "local", NULL, list, is_active, NULL, cancellable, error);
 }
 
 gboolean
@@ -362,7 +363,7 @@ gs_plugin_add_search (GsPlugin *plugin,
                      GCancellable *cancellable,
                      GError **error)
 {
-       return get_apps (plugin, NULL, values, list, NULL, values, error);
+       return get_apps (plugin, NULL, values, list, NULL, values, cancellable, error);
 }
 
 gboolean
@@ -377,7 +378,7 @@ gs_plugin_refine_app (GsPlugin *plugin,
                return TRUE;
 
        // Get info from snapd
-       return get_app (plugin, app, error);
+       return get_app (plugin, app, cancellable, error);
 }
 
 static gboolean
@@ -385,6 +386,7 @@ send_package_action (GsPlugin *plugin,
                     GsApp *app,
                     const gchar *id,
                     const gchar *action,
+                    GCancellable *cancellable,
                     GError **error)
 {
        g_autofree gchar *content = NULL, *path = NULL;
@@ -407,7 +409,7 @@ send_package_action (GsPlugin *plugin,
        if (!gs_snapd_request ("POST", path, content,
                               &status_code, &reason_phrase,
                               &response_type, &response, NULL,
-                              error))
+                              cancellable, error))
                return FALSE;
 
        if (status_code != SOUP_STATUS_ACCEPTED) {
@@ -442,7 +444,7 @@ send_package_action (GsPlugin *plugin,
                        if (!gs_snapd_request ("GET", resource_path, NULL,
                                               &status_code, &status_reason_phrase,
                                               &status_response_type, &status_response, NULL,
-                                              error)) {
+                                              cancellable, error)) {
                                return FALSE;
                        }
 
@@ -514,7 +516,7 @@ gs_plugin_app_install (GsPlugin *plugin,
                return TRUE;
 
        gs_app_set_state (app, AS_APP_STATE_INSTALLING);
-       ret = send_package_action (plugin, app, gs_app_get_id (app), "install", error);
+       ret = send_package_action (plugin, app, gs_app_get_id (app), "install", cancellable, error);
        if (!ret) {
                gs_app_set_state_recover (app);
                return FALSE;
@@ -536,7 +538,7 @@ gs_plugin_app_remove (GsPlugin *plugin,
                return TRUE;
 
        gs_app_set_state (app, AS_APP_STATE_REMOVING);
-       ret = send_package_action (plugin, app, gs_app_get_id (app), "remove", error);
+       ret = send_package_action (plugin, app, gs_app_get_id (app), "remove", cancellable, error);
        if (!ret) {
                gs_app_set_state_recover (app);
                return FALSE;
diff --git a/src/plugins/gs-snapd.c b/src/plugins/gs-snapd.c
index 272925a..3a6a959 100644
--- a/src/plugins/gs-snapd.c
+++ b/src/plugins/gs-snapd.c
@@ -38,7 +38,7 @@ gs_snapd_exists (void)
 }
 
 static GSocket *
-open_snapd_socket (GError **error)
+open_snapd_socket (GCancellable *cancellable, GError **error)
 {
        GSocket *socket;
        g_autoptr(GSocketAddress) address = NULL;
@@ -57,7 +57,7 @@ open_snapd_socket (GError **error)
                return NULL;
        }
        address = g_unix_socket_address_new (SNAPD_SOCKET);
-       if (!g_socket_connect (socket, address, NULL, &error_local)) {
+       if (!g_socket_connect (socket, address, cancellable, &error_local)) {
                g_set_error (error,
                             GS_PLUGIN_ERROR,
                             GS_PLUGIN_ERROR_FAILED,
@@ -74,13 +74,14 @@ static gboolean
 read_from_snapd (GSocket *socket,
                 gchar *buffer, gsize buffer_length,
                 gsize *read_offset,
+                GCancellable *cancellable,
                 GError **error)
 {
        gssize n_read;
        n_read = g_socket_receive (socket,
                                   buffer + *read_offset,
                                   buffer_length - *read_offset,
-                                  NULL,
+                                  cancellable,
                                   error);
        if (n_read < 0)
                return FALSE;
@@ -99,6 +100,7 @@ gs_snapd_request (const gchar  *method,
                  gchar       **response_type,
                  gchar       **response,
                  gsize        *response_length,
+                 GCancellable *cancellable,
                  GError      **error)
 {
        g_autoptr (GSocket) socket = NULL;
@@ -114,7 +116,7 @@ gs_snapd_request (const gchar  *method,
        // NOTE: Would love to use libsoup but it doesn't support unix sockets
        // https://bugzilla.gnome.org/show_bug.cgi?id=727563
 
-       socket = open_snapd_socket (error);
+       socket = open_snapd_socket (cancellable, error);
        if (socket == NULL)
                return FALSE;
 
@@ -130,7 +132,7 @@ gs_snapd_request (const gchar  *method,
        g_debug ("begin snapd request: %s", request->str);
 
        /* send HTTP request */
-       n_written = g_socket_send (socket, request->str, request->len, NULL, error);
+       n_written = g_socket_send (socket, request->str, request->len, cancellable, error);
        if (n_written < 0)
                return FALSE;
 
@@ -140,6 +142,7 @@ gs_snapd_request (const gchar  *method,
                                      data,
                                      max_data_length,
                                      &data_length,
+                                     cancellable,
                                      error))
                        return FALSE;
                body = strstr (data, "\r\n\r\n");
@@ -185,9 +188,9 @@ gs_snapd_request (const gchar  *method,
                        if (!read_from_snapd (socket, data,
                                              max_data_length - data_length,
                                              &data_length,
+                                             cancellable,
                                              error))
                                return FALSE;
-                       g_printerr ("%zi -> %zi\n", n_read, data_length);
                        if (n_read == data_length)
                                break;
                        chunk_length += data_length - n_read;
@@ -203,6 +206,7 @@ gs_snapd_request (const gchar  *method,
                                              data,
                                              max_data_length,
                                              &data_length,
+                                             cancellable,
                                              error))
                                return FALSE;
                }
@@ -233,6 +237,7 @@ gs_snapd_request (const gchar  *method,
                        if (!read_from_snapd (socket, data,
                                              n_required - data_length,
                                              &data_length,
+                                             cancellable,
                                              error))
                                return FALSE;
                break;
@@ -256,6 +261,7 @@ gs_snapd_request (const gchar  *method,
                        if (!read_from_snapd (socket, data,
                                              n_required - data_length,
                                              &data_length,
+                                             cancellable,
                                              error))
                                return FALSE;
                }
diff --git a/src/plugins/gs-snapd.h b/src/plugins/gs-snapd.h
index fadfaa4..e84d36b 100644
--- a/src/plugins/gs-snapd.h
+++ b/src/plugins/gs-snapd.h
@@ -34,6 +34,7 @@ gboolean gs_snapd_request     (const gchar    *method,
                                 gchar          **response_type,
                                 gchar          **response,
                                 gsize          *response_length,
+                                GCancellable   *cancellable,
                                 GError         **error);
 
 #endif /* __GS_SNAPD_H__ */


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