[gnome-software/wip/attente/ubuntu-xenial: 4/7] Connect to socket every request



commit fb7f5ee762b4b114678af4836548fc19bebc1b0b
Author: William Hua <william hua canonical com>
Date:   Sun Apr 17 16:21:55 2016 -0400

    Connect to socket every request

 src/plugins/gs-plugin-snappy.c    |   27 +++++----------------------
 src/plugins/gs-ubuntu-snapd.c     |   18 +++++++++++-------
 src/plugins/gs-ubuntu-snapd.h     |    5 +----
 src/plugins/gs-ubuntuone-dialog.c |   16 +---------------
 4 files changed, 18 insertions(+), 48 deletions(-)
---
diff --git a/src/plugins/gs-plugin-snappy.c b/src/plugins/gs-plugin-snappy.c
index d7fd73f..a81a246 100644
--- a/src/plugins/gs-plugin-snappy.c
+++ b/src/plugins/gs-plugin-snappy.c
@@ -119,12 +119,10 @@ refine_app (GsPlugin *plugin, GsApp *app, JsonObject *package)
        gs_app_add_quirk (app, AS_APP_QUIRK_PROVENANCE);
        icon_url = json_object_get_string_member (package, "icon");
        if (g_str_has_prefix (icon_url, "/")) {
-               g_autoptr(GSocket) icon_socket = NULL;
                g_autofree gchar *icon_response = NULL;
                gsize icon_response_length;
 
-               icon_socket = open_snapd_socket (NULL);
-               if (icon_socket && send_snapd_request (icon_socket, TRUE, TRUE, "GET", icon_url, NULL, NULL, 
NULL, NULL, &icon_response, &icon_response_length, NULL)) {
+               if (send_snapd_request (TRUE, TRUE, "GET", icon_url, NULL, NULL, NULL, NULL, &icon_response, 
&icon_response_length, NULL)) {
                        g_autoptr(GdkPixbufLoader) loader = NULL;
 
                        loader = gdk_pixbuf_loader_new ();
@@ -164,7 +162,6 @@ refine_app (GsPlugin *plugin, GsApp *app, JsonObject *package)
 static gboolean
 get_apps (GsPlugin *plugin, const gchar *sources, gchar **search_terms, GList **list, AppFilterFunc 
filter_func, gpointer user_data, GError **error)
 {
-       g_autoptr(GSocket) socket = NULL;
        guint status_code;
        GPtrArray *query_fields;
        g_autoptr (GString) path = NULL;
@@ -175,10 +172,6 @@ get_apps (GsPlugin *plugin, const gchar *sources, gchar **search_terms, GList **
        GList *snaps;
        GList *i;
 
-       socket = open_snapd_socket (error);
-       if (!socket)
-               return FALSE;
-
        /* Get all the apps */
        query_fields = g_ptr_array_new_with_free_func (g_free);
        if (sources != NULL)
@@ -197,7 +190,7 @@ get_apps (GsPlugin *plugin, const gchar *sources, gchar **search_terms, GList **
                g_string_append (path, fields);
        }
        g_ptr_array_free (query_fields, TRUE);
-       if (!send_snapd_request (socket, TRUE, TRUE, "GET", path->str, NULL, &status_code, &reason_phrase, 
&response_type, &response, NULL, error))
+       if (!send_snapd_request (TRUE, TRUE, "GET", path->str, NULL, &status_code, &reason_phrase, 
&response_type, &response, NULL, error))
                return FALSE;
 
        if (status_code != SOUP_STATUS_OK) {
@@ -242,18 +235,13 @@ get_apps (GsPlugin *plugin, const gchar *sources, gchar **search_terms, GList **
 static gboolean
 get_app (GsPlugin *plugin, GsApp *app, GError **error)
 {
-       g_autoptr(GSocket) socket = NULL;
        guint status_code;
        g_autofree gchar *path = NULL, *reason_phrase = NULL, *response_type = NULL, *response = NULL;
        g_autoptr(JsonParser) parser = NULL;
        JsonObject *root, *result;
 
-       socket = open_snapd_socket (error);
-       if (!socket)
-               return FALSE;
-
        path = g_strdup_printf ("/v2/snaps/%s", gs_app_get_id (app));
-       if (!send_snapd_request (socket, TRUE, TRUE, "GET", path, NULL, &status_code, &reason_phrase, 
&response_type, &response, NULL, error))
+       if (!send_snapd_request (TRUE, TRUE, "GET", path, NULL, &status_code, &reason_phrase, &response_type, 
&response, NULL, error))
                return FALSE;
 
        if (status_code != SOUP_STATUS_OK) {
@@ -340,20 +328,15 @@ static gboolean
 send_package_action (GsPlugin *plugin, const char *id, const gchar *action, GError **error)
 {
        g_autofree gchar *content = NULL, *path = NULL;
-       g_autoptr(GSocket) socket = NULL;
        guint status_code;
        g_autofree gchar *reason_phrase = NULL, *response_type = NULL, *response = NULL, *status = NULL;
        g_autoptr(JsonParser) parser = NULL;
        JsonObject *root, *result;
         const gchar *resource_path;
 
-       socket = open_snapd_socket (error);
-       if (!socket)
-               return FALSE;
-
        content = g_strdup_printf ("{\"action\": \"%s\"}", action);
        path = g_strdup_printf ("/v2/snaps/%s", id);
-       if (!send_snapd_request (socket, TRUE, TRUE, "POST", path, content, &status_code, &reason_phrase, 
&response_type, &response, NULL, error))
+       if (!send_snapd_request (TRUE, TRUE, "POST", path, content, &status_code, &reason_phrase, 
&response_type, &response, NULL, error))
                return FALSE;
 
        if (status_code != SOUP_STATUS_ACCEPTED) {
@@ -378,7 +361,7 @@ send_package_action (GsPlugin *plugin, const char *id, const gchar *action, GErr
                /* Wait for a little bit before polling */
                g_usleep (100 * 1000);
 
-               if (!send_snapd_request (socket, TRUE, TRUE, "GET", resource_path, NULL, &status_code, 
&status_reason_phrase, &status_response_type, &status_response, NULL, error))
+               if (!send_snapd_request (TRUE, TRUE, "GET", resource_path, NULL, &status_code, 
&status_reason_phrase, &status_response_type, &status_response, NULL, error))
                        return FALSE;
                if (status_code != SOUP_STATUS_OK) {
                        g_set_error (error,
diff --git a/src/plugins/gs-ubuntu-snapd.c b/src/plugins/gs-ubuntu-snapd.c
index e90d865..b3873ab 100644
--- a/src/plugins/gs-ubuntu-snapd.c
+++ b/src/plugins/gs-ubuntu-snapd.c
@@ -31,7 +31,7 @@
 
 // snapd API documentation is at https://github.com/ubuntu-core/snappy/blob/master/docs/rest.md
 
-GSocket *
+static GSocket *
 open_snapd_socket (GError **error)
 {
        GSocket *socket;
@@ -73,8 +73,7 @@ read_from_snapd (GSocket *socket, gchar *buffer, gsize buffer_length, gsize *rea
 }
 
 gboolean
-send_snapd_request (GSocket      *socket,
-                   gboolean      authenticate,
+send_snapd_request (gboolean      authenticate,
                    gboolean      retry_after_login,
                    const gchar  *method,
                    const gchar  *path,
@@ -86,6 +85,7 @@ send_snapd_request (GSocket      *socket,
                    gsize        *response_length,
                    GError      **error)
 {
+       g_autoptr (GSocket) socket = NULL;
        g_autoptr (GString) request = NULL;
        gssize n_written;
        gsize max_data_length = 65535, data_length = 0, header_length;
@@ -99,11 +99,16 @@ send_snapd_request (GSocket      *socket,
        GVariantIter *iter;
        guint code;
 
+       // NOTE: Would love to use libsoup but it doesn't support unix sockets
+       // https://bugzilla.gnome.org/show_bug.cgi?id=727563
+
        if (authenticate)
                macaroon = gs_ubuntuone_get_macaroon (TRUE, FALSE, NULL);
 
-       // 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);
+
+       if (socket == NULL)
+               return FALSE;
 
        request = g_string_new ("");
        g_string_append_printf (request, "%s %s HTTP/1.1\r\n", method, path);
@@ -166,8 +171,7 @@ send_snapd_request (GSocket      *socket,
                if (macaroon == NULL)
                        return FALSE;
 
-               return send_snapd_request (socket,
-                                          TRUE,
+               return send_snapd_request (TRUE,
                                           FALSE,
                                           method,
                                           path,
diff --git a/src/plugins/gs-ubuntu-snapd.h b/src/plugins/gs-ubuntu-snapd.h
index bfb4992..29d8d9e 100644
--- a/src/plugins/gs-ubuntu-snapd.h
+++ b/src/plugins/gs-ubuntu-snapd.h
@@ -24,10 +24,7 @@
 
 #include <gio/gio.h>
 
-GSocket * open_snapd_socket  (GError      **error);
-
-gboolean  send_snapd_request (GSocket      *socket,
-                             gboolean      authenticate,
+gboolean  send_snapd_request (gboolean      authenticate,
                              gboolean      retry_after_login,
                              const gchar  *method,
                              const gchar  *path,
diff --git a/src/plugins/gs-ubuntuone-dialog.c b/src/plugins/gs-ubuntuone-dialog.c
index 2eceb56..c941545 100644
--- a/src/plugins/gs-ubuntuone-dialog.c
+++ b/src/plugins/gs-ubuntuone-dialog.c
@@ -359,7 +359,6 @@ err:
 static void
 send_login_request (GsUbuntuoneDialog *self)
 {
-       g_autoptr(GSocket) socket = NULL;
        g_autofree gchar *content = NULL;
        g_autofree gchar *username = NULL;
        g_autofree gchar *password = NULL;
@@ -385,18 +384,6 @@ send_login_request (GsUbuntuoneDialog *self)
        show_status (self, _("Signing in…"), FALSE);
 
        if (self->get_macaroon) {
-               socket = open_snapd_socket (&error);
-
-               if (socket == NULL) {
-                       g_warning ("could not open snapd socket: %s", error->message);
-
-                       reenable_widgets (self);
-                       show_status (self, _("An error occurred"), TRUE);
-                       gtk_widget_grab_focus (self->password_entry);
-
-                       return;
-               }
-
                username = g_strescape (gtk_entry_get_text (GTK_ENTRY (self->email_entry)), NULL);
                password = g_strescape (gtk_entry_get_text (GTK_ENTRY (self->password_entry)), NULL);
 
@@ -420,8 +407,7 @@ send_login_request (GsUbuntuoneDialog *self)
                                                   password);
                }
 
-               if (send_snapd_request (socket,
-                                       FALSE,
+               if (send_snapd_request (FALSE,
                                        FALSE,
                                        SOUP_METHOD_POST,
                                        "/v2/login",


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