diff --git a/Source/WebKit/UIProcess/API/glib/WebKitFeature.cpp b/Source/WebKit/UIProcess/API/glib/WebKitFeature.cpp index 44142ebbcf39b..55851ff59529d 100644 --- a/Source/WebKit/UIProcess/API/glib/WebKitFeature.cpp +++ b/Source/WebKit/UIProcess/API/glib/WebKitFeature.cpp @@ -431,3 +431,27 @@ WebKitFeature* webkit_feature_list_get(WebKitFeatureList* featureList, gsize ind g_return_val_if_fail(index < featureList->items.size(), nullptr); return featureList->items[index]; } + +/** + * webkit_feature_list_find: + * @feature_list: a #WebKitFeatureList + * @identifier: a #WebKitFeature identifier + * + * Finds a feature given its identifier. + * + * Returns: (transfer none) (nullable): The feature with the given + * @identifier, or @NULL if it cannot be found. + * + * Since: 2.54 + */ +WebKitFeature* webkit_feature_list_find(WebKitFeatureList* featureList, const char* identifier) +{ + g_return_val_if_fail(featureList, nullptr); + g_return_val_if_fail(identifier, nullptr); + + auto it = std::ranges::find_if(featureList->items, [identifier](WebKitFeature* feature) -> bool { + return !g_ascii_strcasecmp(identifier, webkit_feature_get_identifier(feature)); + }); + + return (it != featureList->items.end()) ? *it : nullptr; +} diff --git a/Source/WebKit/UIProcess/API/glib/WebKitFeature.h.in b/Source/WebKit/UIProcess/API/glib/WebKitFeature.h.in index 05b4bbafd72d8..0e21ed3bd69ed 100644 --- a/Source/WebKit/UIProcess/API/glib/WebKitFeature.h.in +++ b/Source/WebKit/UIProcess/API/glib/WebKitFeature.h.in @@ -124,6 +124,10 @@ WEBKIT_API WebKitFeature * webkit_feature_list_get (WebKitFeatureList *feature_list, gsize index); +WEBKIT_API WebKitFeature * +webkit_feature_list_find (WebKitFeatureList *feature_list, + const gchar *identifier); + G_DEFINE_AUTOPTR_CLEANUP_FUNC(WebKitFeatureList, webkit_feature_list_unref) G_END_DECLS diff --git a/Tools/MiniBrowser/gtk/main.c b/Tools/MiniBrowser/gtk/main.c index 8433f5360dc4a..6e981a9161e3f 100644 --- a/Tools/MiniBrowser/gtk/main.c +++ b/Tools/MiniBrowser/gtk/main.c @@ -257,16 +257,6 @@ static gboolean isValidParameterType(GType gParamType) || gParamType == G_TYPE_FLOAT); } -static WebKitFeature* findFeature(WebKitFeatureList *featureList, const char *identifier) -{ - for (gsize i = 0; i < webkit_feature_list_get_length(featureList); i++) { - WebKitFeature *feature = webkit_feature_list_get(featureList, i); - if (!g_ascii_strcasecmp(identifier, webkit_feature_get_identifier(feature))) - return feature; - } - return NULL; -} - static gboolean parseFeaturesOptionCallback(const gchar *option, const gchar *value, WebKitSettings *webSettings, GError **error) { g_autoptr(WebKitFeatureList) featureList = webkit_settings_get_all_features(); @@ -311,7 +301,7 @@ static gboolean parseFeaturesOptionCallback(const gchar *option, const gchar *va return FALSE; } - WebKitFeature *feature = findFeature(featureList, item); + WebKitFeature *feature = webkit_feature_list_find(featureList, item); if (!feature) { g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, "Feature '%s' is not available", item); return FALSE; diff --git a/Tools/MiniBrowser/wpe/main.cpp b/Tools/MiniBrowser/wpe/main.cpp index 61760f3b78402..8e51eecafb324 100644 --- a/Tools/MiniBrowser/wpe/main.cpp +++ b/Tools/MiniBrowser/wpe/main.cpp @@ -373,16 +373,6 @@ static void automationStartedCallback(WebKitWebContext*, WebKitAutomationSession g_signal_connect(session, "create-web-view", G_CALLBACK(createWebViewForAutomationCallback), view); } -static WebKitFeature* findFeature(WebKitFeatureList* featureList, const char* identifier) -{ - for (gsize i = 0; i < webkit_feature_list_get_length(featureList); i++) { - WebKitFeature* feature = webkit_feature_list_get(featureList, i); - if (!g_ascii_strcasecmp(identifier, webkit_feature_get_identifier(feature))) - return feature; - } - return nullptr; -} - static void activate(GApplication* application, WPEToolingBackends::ViewBackend* backend) { g_application_hold(application); @@ -507,7 +497,7 @@ static void activate(GApplication* application, WPEToolingBackends::ViewBackend* continue; } - if (auto* feature = findFeature(features, item)) + if (auto* feature = webkit_feature_list_find(features, item)) webkit_settings_set_feature_enabled(settings, feature, enabled); else g_printerr("Feature '%s' is not available.", item); diff --git a/Tools/TestWebKitAPI/Tests/WebKitGLib/TestWebKitSettings.cpp b/Tools/TestWebKitAPI/Tests/WebKitGLib/TestWebKitSettings.cpp index 8592ebc6224f7..916bc11837364 100644 --- a/Tools/TestWebKitAPI/Tests/WebKitGLib/TestWebKitSettings.cpp +++ b/Tools/TestWebKitAPI/Tests/WebKitGLib/TestWebKitSettings.cpp @@ -493,6 +493,22 @@ void testWebKitFeatures(Test* test, gconstpointer) g_assert(webkit_settings_get_feature_enabled(settings.get(), feature) == webkit_feature_get_default_value(feature)); } + + // Check finding features given their identifier. + if (webkit_feature_list_get_length(allFeatures)) { + WebKitFeature* firstFeature = webkit_feature_list_get(allFeatures, 0); + WebKitFeature* foundFeature = webkit_feature_list_find(allFeatures, webkit_feature_get_identifier(firstFeature)); + g_assert_nonnull(foundFeature); + g_assert(firstFeature == foundFeature); + + g_autofree char* lowerCaseIdentifier = g_utf8_strdown(webkit_feature_get_identifier(firstFeature), -1); + foundFeature = webkit_feature_list_find(allFeatures, lowerCaseIdentifier); + g_assert_nonnull(foundFeature); + g_assert(firstFeature == foundFeature); + } + + WebKitFeature* foundFeature = webkit_feature_list_find(allFeatures, "ThisFeatureIdentifierCannotPossiblyExist"); + g_assert_null(foundFeature); } void testWebKitSettingsApplyFromConfigFile(Test* test, gconstpointer)