[gnome-software: 1/4] gs-download-utils: Split out SoupSession construction




commit 85d25e769e05f51202ce72b90b0b225570bfb1b0
Author: Philip Withnall <pwithnall endlessos org>
Date:   Wed Feb 2 12:08:22 2022 +0000

    gs-download-utils: Split out SoupSession construction
    
    Centralising it in `GsPlugin` and `GsPluginLoader` is not great, since
    it means connection and authentication information get shared between
    different plugin contexts (which don’t need to share them), and thread
    contention on the session is increased.
    
    We probably want to aim for a model where, roughly, there is one
    `SoupSession` per plugin which needs one.
    
    Factor out a common construction method for `SoupSession` which sets the
    gnome-software user agent and default properties. Plugins can then call
    this.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>
    
    Helps: #1472

 lib/gnome-software.h    |  1 +
 lib/gs-download-utils.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 lib/gs-download-utils.h | 22 ++++++++++++++++++++++
 lib/meson.build         |  2 ++
 4 files changed, 72 insertions(+)
---
diff --git a/lib/gnome-software.h b/lib/gnome-software.h
index 724cc7e9f..7cf850203 100644
--- a/lib/gnome-software.h
+++ b/lib/gnome-software.h
@@ -18,6 +18,7 @@
 #include <gs-category.h>
 #include <gs-category-manager.h>
 #include <gs-desktop-data.h>
+#include <gs-download-utils.h>
 #include <gs-enums.h>
 #include <gs-icon.h>
 #include <gs-metered.h>
diff --git a/lib/gs-download-utils.c b/lib/gs-download-utils.c
new file mode 100644
index 000000000..a4e203755
--- /dev/null
+++ b/lib/gs-download-utils.c
@@ -0,0 +1,47 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ * vi:set noexpandtab tabstop=8 shiftwidth=8:
+ *
+ * Copyright (C) 2021, 2022 Endless OS Foundation LLC
+ *
+ * Author: Philip Withnall <pwithnall endlessos org>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+/**
+ * SECTION:gs-download-utils
+ * @short_description: Download and HTTP utilities
+ *
+ * A set of utilities for downloading things and doing HTTP requests.
+ *
+ * Since: 42
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <libsoup/soup.h>
+
+#include "gs-download-utils.h"
+#include "gs-utils.h"
+
+/**
+ * gs_build_soup_session:
+ *
+ * Build a new #SoupSession configured with the gnome-software user agent.
+ *
+ * A new #SoupSession should be used for each independent download context, such
+ * as in different plugins. Each #SoupSession caches HTTP connections and
+ * authentication information, and these likely needn’t be shared between
+ * plugins. Using separate sessions reduces thread contention.
+ *
+ * Returns: (transfer full): a new #SoupSession
+ * Since: 42
+ */
+SoupSession *
+gs_build_soup_session (void)
+{
+       return soup_session_new_with_options ("user-agent", gs_user_agent (),
+                                             "timeout", 10,
+                                             NULL);
+}
diff --git a/lib/gs-download-utils.h b/lib/gs-download-utils.h
new file mode 100644
index 000000000..e634ace78
--- /dev/null
+++ b/lib/gs-download-utils.h
@@ -0,0 +1,22 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ * vi:set noexpandtab tabstop=8 shiftwidth=8:
+ *
+ * Copyright (C) 2022 Endless OS Foundation LLC
+ *
+ * Author: Philip Withnall <pwithnall endlessos org>
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ */
+
+#pragma once
+
+#include <glib.h>
+#include <glib-object.h>
+#include <gio/gio.h>
+#include <libsoup/soup.h>
+
+G_BEGIN_DECLS
+
+SoupSession *gs_build_soup_session (void);
+
+G_END_DECLS
diff --git a/lib/meson.build b/lib/meson.build
index 4b42c1076..a87c9875b 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -10,6 +10,7 @@ libgnomesoftware_public_headers = [
   'gs-category.h',
   'gs-category-manager.h',
   'gs-desktop-data.h',
+  'gs-download-utils.h',
   'gs-external-appstream-utils.h',
   'gs-icon.h',
   'gs-ioprio.h',
@@ -84,6 +85,7 @@ libgnomesoftware = library(
     'gs-category-manager.c',
     'gs-debug.c',
     'gs-desktop-data.c',
+    'gs-download-utils.c',
     'gs-external-appstream-utils.c',
     'gs-fedora-third-party.c',
     'gs-icon.c',


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