[gnome-software/wip/ubuntu-xenial] snapd: Allow snapd operations to be cancelled
- From: Robert Ancell <rancell src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-software/wip/ubuntu-xenial] snapd: Allow snapd operations to be cancelled
- Date: Mon, 4 Jul 2016 04:16:51 +0000 (UTC)
commit 090e0c196337e6a40dac8fb75e41cb3864ab6c56
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 | 55 +++++++++++++++++++++----------------
src/plugins/gs-snapd.c | 19 +++++++++----
src/plugins/gs-snapd.h | 1 +
src/plugins/gs-ubuntuone-dialog.c | 1 +
4 files changed, 46 insertions(+), 30 deletions(-)
---
diff --git a/src/plugins/gs-plugin-snap.c b/src/plugins/gs-plugin-snap.c
index 61758ba..16b871a 100644
--- a/src/plugins/gs-plugin-snap.c
+++ b/src/plugins/gs-plugin-snap.c
@@ -91,7 +91,7 @@ parse_result (const gchar *response, const gchar *response_type, GError **error)
}
static void
-refine_app (GsPlugin *plugin, GsApp *app, JsonObject *package, gboolean from_search)
+refine_app (GsPlugin *plugin, GsApp *app, JsonObject *package, gboolean from_search, GCancellable
*cancellable)
{
const gchar *status, *icon_url, *launch_name = NULL;
g_autoptr(GdkPixbuf) icon_pixbuf = NULL;
@@ -132,10 +132,11 @@ refine_app (GsPlugin *plugin, GsApp *app, JsonObject *package, gboolean from_sea
g_autofree gchar *icon_response = NULL;
gsize icon_response_length;
- if (gs_snapd_request ("GET", icon_url, NULL, TRUE, NULL, TRUE, NULL,
+ if (gs_snapd_request ("GET", icon_url, NULL,
+ TRUE, NULL, TRUE, NULL,
NULL, NULL, NULL,
&icon_response, &icon_response_length,
- NULL)) {
+ cancellable, NULL)) {
g_autoptr(GdkPixbufLoader) loader = NULL;
loader = gdk_pixbuf_loader_new ();
@@ -198,6 +199,7 @@ get_apps (GsPlugin *plugin,
GList **list,
AppFilterFunc filter_func,
gpointer user_data,
+ GCancellable *cancellable,
GError **error)
{
guint status_code;
@@ -230,10 +232,11 @@ get_apps (GsPlugin *plugin,
g_string_append (path, fields);
}
g_ptr_array_free (query_fields, TRUE);
- if (!gs_snapd_request ("GET", path->str, NULL, TRUE, NULL, TRUE, NULL,
+ if (!gs_snapd_request ("GET", path->str, NULL,
+ TRUE, NULL, TRUE, NULL,
&status_code, &reason_phrase,
- &response_type, &response,
- NULL, error))
+ &response_type, &response, NULL,
+ cancellable, error))
return FALSE;
if (status_code != SOUP_STATUS_OK) {
@@ -268,7 +271,7 @@ get_apps (GsPlugin *plugin,
gs_app_set_origin (app, _("Snap Store"));
gs_app_set_kind (app, AS_APP_KIND_DESKTOP);
gs_app_add_quirk (app, AS_APP_QUIRK_NOT_REVIEWABLE);
- refine_app (plugin, app, package, TRUE);
+ refine_app (plugin, app, package, TRUE, cancellable);
gs_plugin_add_app (list, app);
}
@@ -278,7 +281,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;
@@ -289,10 +292,11 @@ get_app (GsPlugin *plugin, GsApp *app, GError **error)
JsonObject *root, *result;
path = g_strdup_printf ("/v2/snaps/%s", gs_app_get_id (app));
- if (!gs_snapd_request ("GET", path, NULL, TRUE, NULL, TRUE, NULL,
+ if (!gs_snapd_request ("GET", path, NULL,
+ TRUE, NULL, TRUE, NULL,
&status_code, &reason_phrase,
- &response_type, &response,
- NULL, error))
+ &response_type, &response, NULL,
+ cancellable, error))
return FALSE;
if (status_code != SOUP_STATUS_OK) {
@@ -317,7 +321,7 @@ get_app (GsPlugin *plugin, GsApp *app, GError **error)
return FALSE;
}
- refine_app (plugin, app, result, FALSE);
+ refine_app (plugin, app, result, FALSE, cancellable);
return TRUE;
}
@@ -340,7 +344,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
@@ -350,7 +354,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
@@ -369,7 +373,7 @@ gs_plugin_refine (GsPlugin *plugin,
continue;
// Get info from snapd
- if (!get_app (plugin, app, error))
+ if (!get_app (plugin, app, cancellable, error))
return FALSE;
}
@@ -381,6 +385,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;
@@ -401,10 +406,11 @@ send_package_action (GsPlugin *plugin,
content = g_strdup_printf ("{\"action\": \"%s\"}", action);
path = g_strdup_printf ("/v2/snaps/%s", id);
- if (!gs_snapd_request ("POST", path, content, TRUE, NULL, TRUE,
- &macaroon, &status_code,
- &reason_phrase, &response_type,
- &response, NULL, error))
+ if (!gs_snapd_request ("POST", path, content,
+ TRUE, NULL, TRUE, &macaroon,
+ &status_code, &reason_phrase,
+ &response_type, &response, NULL,
+ cancellable, error))
return FALSE;
if (status_code != SOUP_STATUS_ACCEPTED) {
@@ -436,10 +442,11 @@ send_package_action (GsPlugin *plugin,
/* Wait for a little bit before polling */
g_usleep (100 * 1000);
- if (!gs_snapd_request ("GET", resource_path, NULL, TRUE, macaroon, TRUE, NULL,
+ if (!gs_snapd_request ("GET", resource_path, NULL,
+ TRUE, macaroon, TRUE, NULL,
&status_code, &status_reason_phrase,
- &status_response_type, &status_response,
- NULL, error)) {
+ &status_response_type, &status_response, NULL,
+ cancellable, error)) {
return FALSE;
}
@@ -511,7 +518,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 (app, AS_APP_STATE_AVAILABLE);
return FALSE;
@@ -566,7 +573,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 (app, AS_APP_STATE_INSTALLED);
return FALSE;
diff --git a/src/plugins/gs-snapd.c b/src/plugins/gs-snapd.c
index 229555f..4f1d39a 100644
--- a/src/plugins/gs-snapd.c
+++ b/src/plugins/gs-snapd.c
@@ -39,7 +39,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;
@@ -58,7 +58,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,
@@ -75,13 +75,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;
@@ -104,6 +105,7 @@ gs_snapd_request (const gchar *method,
gchar **response_type,
gchar **response,
gsize *response_length,
+ GCancellable *cancellable,
GError **error)
{
g_autoptr (GSocket) socket = NULL;
@@ -129,7 +131,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;
@@ -156,7 +158,7 @@ gs_snapd_request (const gchar *method,
g_print ("===== begin snapd request =====\n%s\n===== end snapd request =====\n",
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;
@@ -166,6 +168,7 @@ gs_snapd_request (const gchar *method,
data,
max_data_length,
&data_length,
+ cancellable,
error))
return FALSE;
body = strstr (data, "\r\n\r\n");
@@ -223,6 +226,7 @@ gs_snapd_request (const gchar *method,
response_type,
response,
response_length,
+ cancellable,
error);
if (ret && out_macaroon != NULL) {
@@ -249,9 +253,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;
@@ -267,6 +271,7 @@ gs_snapd_request (const gchar *method,
data,
max_data_length,
&data_length,
+ cancellable,
error))
return FALSE;
}
@@ -297,6 +302,7 @@ gs_snapd_request (const gchar *method,
if (!read_from_snapd (socket, data,
n_required - data_length,
&data_length,
+ cancellable,
error))
return FALSE;
break;
@@ -320,6 +326,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 b6de429..27540d0 100644
--- a/src/plugins/gs-snapd.h
+++ b/src/plugins/gs-snapd.h
@@ -38,6 +38,7 @@ gboolean gs_snapd_request (const gchar *method,
gchar **response_type,
gchar **response,
gsize *response_length,
+ GCancellable *cancellable,
GError **error);
#endif /* __GS_SNAPD_H__ */
diff --git a/src/plugins/gs-ubuntuone-dialog.c b/src/plugins/gs-ubuntuone-dialog.c
index aca0925..3516d1c 100644
--- a/src/plugins/gs-ubuntuone-dialog.c
+++ b/src/plugins/gs-ubuntuone-dialog.c
@@ -440,6 +440,7 @@ send_login_request (GsUbuntuoneDialog *self)
&response_type,
&response,
&response_length,
+ NULL,
&error)) {
reenable_widgets (self);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]