[libsoup/pgriffis/duplicate-soup-features] session: Forbid multiple features of the same type to be added




commit 502da930ad00ad683c68a0f2e7b1343afec009dc
Author: Patrick Griffis <pgriffis igalia com>
Date:   Tue May 25 09:50:02 2021 -0500

    session: Forbid multiple features of the same type to be added
    
    This can cause accidental conflicts and doesn't seem to be valuable
    with the current features.

 libsoup/soup-session.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
---
diff --git a/libsoup/soup-session.c b/libsoup/soup-session.c
index 68d7a026..b8b95e11 100644
--- a/libsoup/soup-session.c
+++ b/libsoup/soup-session.c
@@ -2211,12 +2211,28 @@ soup_session_abort (SoupSession *session)
        g_slist_free (conns);
 }
 
+static gboolean
+feature_already_added (GSList *features, GType feature_type)
+{
+        for (GSList *l = features; l; l = g_slist_next (l)) {
+                SoupSessionFeature *feature = l->data;
+                if (G_TYPE_FROM_INSTANCE (feature) == feature_type) {
+                        g_warning ("SoupSession already has a %s, ignoring new feature",
+                                   g_type_name (feature_type));
+                        return TRUE;
+                }
+        }
+
+        return FALSE;
+}
+
 /**
  * soup_session_add_feature:
  * @session: a #SoupSession
  * @feature: an object that implements #SoupSessionFeature
  *
- * Adds @feature's functionality to @session.
+ * Adds @feature's functionality to @session. You cannot add multiple
+ * features of the same #GType to a session.
  *
  * See the main #SoupSession documentation for information on what
  * features are present in sessions by default.
@@ -2231,6 +2247,10 @@ soup_session_add_feature (SoupSession *session, SoupSessionFeature *feature)
        g_return_if_fail (SOUP_IS_SESSION_FEATURE (feature));
 
        priv = soup_session_get_instance_private (session);
+
+        if (feature_already_added (priv->features, G_TYPE_FROM_INSTANCE (feature)))
+                return;
+
        priv->features = g_slist_prepend (priv->features, g_object_ref (feature));
        g_hash_table_remove_all (priv->features_cache);
        soup_session_feature_attach (feature, session);
@@ -2245,6 +2265,7 @@ soup_session_add_feature (SoupSession *session, SoupSessionFeature *feature)
  * #SoupSessionFeature, this creates a new feature of that type and
  * adds it to @session as with soup_session_add_feature(). You can use
  * this when you don't need to customize the new feature in any way.
+ * Adding multiple features of the same @feature_type is not allowed.
  *
  * If @feature_type is not a #SoupSessionFeature type, this gives each
  * existing feature on @session the chance to accept @feature_type as
@@ -2266,6 +2287,9 @@ soup_session_add_feature_by_type (SoupSession *session, GType feature_type)
        if (g_type_is_a (feature_type, SOUP_TYPE_SESSION_FEATURE)) {
                SoupSessionFeature *feature;
 
+                if (feature_already_added (priv->features, feature_type))
+                        return;
+
                feature = g_object_new (feature_type, NULL);
                soup_session_add_feature (session, feature);
                g_object_unref (feature);


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