[gnome-software/wip/ubuntu] Refactor snapd code to be cancellable
- From: Robert Ancell <rancell src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-software/wip/ubuntu] Refactor snapd code to be cancellable
- Date: Fri, 30 Sep 2016 01:34:55 +0000 (UTC)
commit 1f5b916a77898a5fe0a470f80956efcf5b239695
Author: Robert Ancell <robert ancell canonical com>
Date: Fri Sep 30 11:18:37 2016 +1300
Refactor snapd code to be cancellable
src/plugins/gs-plugin-snap.c | 26 ++++++++--------
src/plugins/gs-snapd.c | 62 ++++++++++++++++++++++++------------
src/plugins/gs-snapd.h | 1 +
src/plugins/gs-ubuntuone-dialog.c | 1 +
4 files changed, 56 insertions(+), 34 deletions(-)
---
diff --git a/src/plugins/gs-plugin-snap.c b/src/plugins/gs-plugin-snap.c
index 7e56027..6a24790 100644
--- a/src/plugins/gs-plugin-snap.c
+++ b/src/plugins/gs-plugin-snap.c
@@ -131,7 +131,7 @@ refine_app (GsPlugin *plugin, GsApp *app, JsonObject *package, gboolean from_sea
g_autofree gchar *icon_response = NULL;
gsize icon_response_length;
- if (send_snapd_request ("GET", icon_url, NULL, TRUE, NULL, TRUE, NULL, NULL, NULL, NULL,
&icon_response, &icon_response_length, NULL)) {
+ if (send_snapd_request ("GET", icon_url, NULL, TRUE, NULL, TRUE, NULL, NULL, NULL, NULL,
&icon_response, &icon_response_length, NULL, NULL)) {
g_autoptr(GdkPixbufLoader) loader = NULL;
loader = gdk_pixbuf_loader_new ();
@@ -182,7 +182,7 @@ refine_app (GsPlugin *plugin, GsApp *app, JsonObject *package, gboolean from_sea
}
static gboolean
-get_apps (GsPlugin *plugin, gchar **search_terms, GList **list, AppFilterFunc filter_func, gpointer
user_data, GError **error)
+get_apps (GsPlugin *plugin, gchar **search_terms, GList **list, AppFilterFunc filter_func, gpointer
user_data, GCancellable *cancellable, GError **error)
{
guint status_code;
GPtrArray *query_fields;
@@ -212,7 +212,7 @@ get_apps (GsPlugin *plugin, gchar **search_terms, GList **list, AppFilterFunc fi
g_string_append (path, fields);
}
g_ptr_array_free (query_fields, TRUE);
- if (!send_snapd_request ("GET", path->str, NULL, TRUE, NULL, TRUE, NULL, &status_code,
&reason_phrase, &response_type, &response, NULL, error))
+ if (!send_snapd_request ("GET", path->str, NULL, TRUE, NULL, TRUE, NULL, &status_code,
&reason_phrase, &response_type, &response, NULL, cancellable, error))
return FALSE;
if (status_code != SOUP_STATUS_OK) {
@@ -256,7 +256,7 @@ get_apps (GsPlugin *plugin, gchar **search_terms, GList **list, AppFilterFunc fi
}
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, *reason_phrase = NULL, *response_type = NULL, *response = NULL;
@@ -264,7 +264,7 @@ get_app (GsPlugin *plugin, GsApp *app, GError **error)
JsonObject *root, *result;
path = g_strdup_printf ("/v2/snaps/%s", gs_app_get_id (app));
- if (!send_snapd_request ("GET", path, NULL, TRUE, NULL, TRUE, NULL, &status_code, &reason_phrase,
&response_type, &response, NULL, error))
+ if (!send_snapd_request ("GET", path, NULL, TRUE, NULL, TRUE, NULL, &status_code, &reason_phrase,
&response_type, &response, NULL, cancellable, error))
return FALSE;
if (status_code == SOUP_STATUS_NOT_FOUND)
@@ -314,7 +314,7 @@ gs_plugin_add_installed (GsPlugin *plugin,
GCancellable *cancellable,
GError **error)
{
- return get_apps (plugin, NULL, list, is_active, NULL, error);
+ return get_apps (plugin, NULL, list, is_active, NULL, cancellable, error);
}
gboolean
@@ -324,7 +324,7 @@ gs_plugin_add_search (GsPlugin *plugin,
GCancellable *cancellable,
GError **error)
{
- return get_apps (plugin, values, list, NULL, values, error);
+ return get_apps (plugin, values, list, NULL, values, cancellable, error);
}
gboolean
@@ -343,7 +343,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;
}
@@ -351,7 +351,7 @@ gs_plugin_refine (GsPlugin *plugin,
}
static gboolean
-send_package_action (GsPlugin *plugin, GsApp *app, const char *id, const gchar *action, GError **error)
+send_package_action (GsPlugin *plugin, GsApp *app, const char *id, const gchar *action, GCancellable
*cancellable, GError **error)
{
g_autofree gchar *content = NULL, *path = NULL;
guint status_code;
@@ -368,7 +368,7 @@ send_package_action (GsPlugin *plugin, GsApp *app, const char *id, const gchar *
content = g_strdup_printf ("{\"action\": \"%s\"}", action);
path = g_strdup_printf ("/v2/snaps/%s", id);
- if (!send_snapd_request ("POST", path, content, TRUE, NULL, TRUE, &macaroon, &status_code,
&reason_phrase, &response_type, &response, NULL, error))
+ if (!send_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) {
@@ -399,7 +399,7 @@ send_package_action (GsPlugin *plugin, GsApp *app, const char *id, const gchar *
if (!send_snapd_request ("GET", resource_path, NULL, TRUE, macaroon, TRUE, NULL,
&status_code, &status_reason_phrase, &status_response_type,
- &status_response, NULL, error)) {
+ &status_response, NULL, cancellable, error)) {
return FALSE;
}
@@ -470,7 +470,7 @@ gs_plugin_app_install (GsPlugin *plugin,
return TRUE;
gs_app_set_state (app, AS_APP_STATE_INSTALLING);
- result = send_package_action (plugin, app, gs_app_get_id (app), "install", error);
+ result = send_package_action (plugin, app, gs_app_get_id (app), "install", cancellable, error);
if (result)
gs_app_set_state (app, AS_APP_STATE_INSTALLED);
else
@@ -525,7 +525,7 @@ gs_plugin_app_remove (GsPlugin *plugin,
return TRUE;
gs_app_set_state (app, AS_APP_STATE_REMOVING);
- result = send_package_action (plugin, app, gs_app_get_id (app), "remove", error);
+ result = send_package_action (plugin, app, gs_app_get_id (app), "remove", cancellable, error);
if (result)
gs_app_set_state (app, AS_APP_STATE_AVAILABLE);
else
diff --git a/src/plugins/gs-snapd.c b/src/plugins/gs-snapd.c
index 9658b2e..a0492c2 100644
--- a/src/plugins/gs-snapd.c
+++ b/src/plugins/gs-snapd.c
@@ -32,26 +32,31 @@
// snapd API documentation is at https://github.com/ubuntu-core/snappy/blob/master/docs/rest.md
static GSocket *
-open_snapd_socket (GError **error)
+open_snapd_socket (GCancellable *cancellable, GError **error)
{
GSocket *socket;
g_autoptr(GSocketAddress) address = NULL;
- g_autoptr(GError) sub_error = NULL;
+ g_autoptr(GError) error_local = NULL;
- socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT,
&sub_error);
+ socket = g_socket_new (G_SOCKET_FAMILY_UNIX,
+ G_SOCKET_TYPE_STREAM,
+ G_SOCKET_PROTOCOL_DEFAULT,
+ &error_local);
if (!socket) {
g_set_error (error,
GS_PLUGIN_ERROR,
GS_PLUGIN_ERROR_FAILED,
- "Unable to open snapd socket: %s", sub_error->message);
+ "Unable to open snapd socket: %s",
+ error_local->message);
return NULL;
}
address = g_unix_socket_address_new (SNAPD_SOCKET_PATH);
- if (!g_socket_connect (socket, address, NULL, &sub_error)) {
+ if (!g_socket_connect (socket, address, cancellable, &error_local)) {
g_set_error (error,
GS_PLUGIN_ERROR,
GS_PLUGIN_ERROR_FAILED,
- "Unable to connect snapd socket: %s", sub_error->message);
+ "Unable to connect snapd socket: %s",
+ error_local->message);
g_object_unref (socket);
return NULL;
}
@@ -60,13 +65,21 @@ open_snapd_socket (GError **error)
}
static gboolean
-read_from_snapd (GSocket *socket, gchar *buffer, gsize buffer_length, gsize *read_offset, GError **error)
+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, error);
+ n_read = g_socket_receive (socket,
+ buffer + *read_offset,
+ buffer_length - *read_offset,
+ cancellable,
+ error);
if (n_read < 0)
return FALSE;
- *read_offset += n_read;
+ *read_offset += (gsize) n_read;
buffer[*read_offset] = '\0';
return TRUE;
@@ -85,6 +98,7 @@ send_snapd_request (const gchar *method,
gchar **response_type,
gchar **response,
gsize *response_length,
+ GCancellable *cancellable,
GError **error)
{
g_autoptr (GSocket) socket = NULL;
@@ -110,8 +124,7 @@ send_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;
@@ -137,14 +150,19 @@ send_snapd_request (const gchar *method,
if (g_strcmp0 (g_getenv ("GNOME_SOFTWARE_SNAPPY"), "debug") == 0)
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);
+ /* send HTTP request */
+ n_written = g_socket_send (socket, request->str, request->len, cancellable, error);
if (n_written < 0)
return FALSE;
- /* Read HTTP headers */
+ /* read HTTP headers */
while (data_length < max_data_length && !body) {
- if (!read_from_snapd (socket, data, max_data_length, &data_length, error))
+ if (!read_from_snapd (socket,
+ data,
+ max_data_length,
+ &data_length,
+ cancellable,
+ error))
return FALSE;
body = strstr (data, "\r\n\r\n");
}
@@ -156,13 +174,14 @@ send_snapd_request (const gchar *method,
return FALSE;
}
- /* Body starts after header divider */
+ /* body starts after header divider */
body += 4;
- header_length = body - data;
+ header_length = (gsize) (body - data);
- /* Parse headers */
+ /* parse headers */
headers = soup_message_headers_new (SOUP_MESSAGE_HEADERS_RESPONSE);
- if (!soup_headers_parse_response (data, header_length, headers, NULL, &code, reason_phrase)) {
+ if (!soup_headers_parse_response (data, header_length, headers,
+ NULL, &code, reason_phrase)) {
g_set_error_literal (error,
GS_PLUGIN_ERROR,
GS_PLUGIN_ERROR_FAILED,
@@ -198,6 +217,7 @@ send_snapd_request (const gchar *method,
response_type,
response,
response_length,
+ cancellable,
error);
if (ret && out_macaroon != NULL) {
@@ -215,7 +235,7 @@ send_snapd_request (const gchar *method,
chunk_start = strstr (body, "\r\n");
if (chunk_start)
break;
- if (!read_from_snapd (socket, data, max_data_length, &data_length, error))
+ if (!read_from_snapd (socket, data, max_data_length, &data_length, cancellable,
error))
return FALSE;
}
if (!chunk_start) {
@@ -255,7 +275,7 @@ send_snapd_request (const gchar *method,
/* Read chunk content */
while (data_length < n_required)
- if (!read_from_snapd (socket, data, n_required - data_length, &data_length, error))
+ if (!read_from_snapd (socket, data, n_required - data_length, &data_length, cancellable,
error))
return FALSE;
if (out_macaroon != NULL)
diff --git a/src/plugins/gs-snapd.h b/src/plugins/gs-snapd.h
index d44d834..b02ddbd 100644
--- a/src/plugins/gs-snapd.h
+++ b/src/plugins/gs-snapd.h
@@ -36,6 +36,7 @@ gboolean send_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 6fc2a34..ca4dd32 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]