From f453f69762fca2920fc0c4c46b8523c593ef4da5 Mon Sep 17 00:00:00 2001 From: Philippe Normand Date: Mon, 28 Aug 2023 11:41:25 +0100 Subject: [PATCH] launcher: Add command-line option for controlling autoplay behavior Fixes #597 --- launcher/cog-launcher.c | 49 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/launcher/cog-launcher.c b/launcher/cog-launcher.c index 3907490b..ca7a5c01 100644 --- a/launcher/cog-launcher.c +++ b/launcher/cog-launcher.c @@ -13,6 +13,7 @@ #include #define HAVE_WEBKIT_NETWORK_PROXY_API WEBKIT_CHECK_VERSION(2, 32, 0) +#define HAVE_WEBKIT_AUTOPLAY WEBKIT_CHECK_VERSION(2, 30, 0) enum webprocess_fail_action { WEBPROCESS_FAIL_UNKNOWN = 0, @@ -62,15 +63,21 @@ static struct { gchar **ignore_hosts; #endif /* HAVE_WEBKIT_NETWORK_PROXY_API */ gboolean disable_key_bindings; +#if HAVE_WEBKIT_AUTOPLAY + WebKitAutoplayPolicy autoplay_policy; +#endif } s_options = { .scale_factor = 1.0, .device_scale_factor = 1.0, +#if HAVE_WEBKIT_AUTOPLAY + .autoplay_policy = WEBKIT_AUTOPLAY_ALLOW_WITHOUT_SOUND, +#endif }; #if !GLIB_CHECK_VERSION(2, 56, 0) typedef void (*GClearHandleFunc)(guint handle_id); -# undef g_clear_handle_id +#undef g_clear_handle_id void g_clear_handle_id(guint *tag_ptr, GClearHandleFunc clear_func) { @@ -236,15 +243,25 @@ cog_launcher_create_view(CogLauncher *self, CogShell *shell) if (!platform) g_error("Cannot instantiate a platform: %s", error->message); +#if HAVE_WEBKIT_AUTOPLAY + WebKitWebsitePolicies *website_policies = + webkit_website_policies_new_with_policies("autoplay", s_options.autoplay_policy, NULL); +#endif + g_autoptr(WebKitWebView) web_view = WEBKIT_WEB_VIEW(cog_view_new("settings", cog_shell_get_web_settings(shell), "web-context", web_context, "zoom-level", s_options.scale_factor, "is-controlled-by-automation", cog_shell_is_automated(shell), "use-key-bindings", !s_options.disable_key_bindings, #if COG_USE_WPE2 "network-session", self->network_session, +#endif +#if HAVE_WEBKIT_AUTOPLAY + "website-policies", website_policies, #endif NULL)); + g_clear_object(&website_policies); + if (s_options.filter) { WebKitUserContentManager *manager = webkit_web_view_get_user_content_manager(web_view); webkit_user_content_manager_add_filter(manager, s_options.filter); @@ -1028,6 +1045,32 @@ option_entry_parse_gamepad(const char *option_name, const char *value, void *dat return TRUE; } +#if HAVE_WEBKIT_AUTOPLAY +static gboolean +option_entry_parse_autoplay(const char *option_name, const char *value, void *data, GError **error) +{ + if (!g_strcmp0(value, "allow")) { + s_options.autoplay_policy = WEBKIT_AUTOPLAY_ALLOW; + return TRUE; + } + + if (!g_strcmp0(value, "allow-without-sound")) { + s_options.autoplay_policy = WEBKIT_AUTOPLAY_ALLOW_WITHOUT_SOUND; + return TRUE; + } + + if (!g_strcmp0(value, "deny")) { + s_options.autoplay_policy = WEBKIT_AUTOPLAY_DENY; + return TRUE; + } + + g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, + "Failed to parse '%s' as an autoplay policy, valid options are allow, allow-without-sound, and deny", + value); + return FALSE; +} +#endif + static GOptionEntry s_cli_options[] = { {"version", '\0', 0, G_OPTION_ARG_NONE, &s_options.version, "Print version and exit", NULL}, {"print-appid", '\0', 0, G_OPTION_ARG_NONE, &s_options.print_appid, "Print application ID and exit", NULL}, @@ -1069,6 +1112,10 @@ static GOptionEntry s_cli_options[] = { {"gamepad", '\0', 0, G_OPTION_ARG_CALLBACK, option_entry_parse_gamepad, "Set gamepad implementation", NULL}, {"no-key-bindings", '\0', 0, G_OPTION_ARG_NONE, &s_options.disable_key_bindings, "Disable handing of key bindings (default: enabled).", NULL}, +#if HAVE_WEBKIT_AUTOPLAY + {"autoplay-policy", 0, 0, G_OPTION_ARG_CALLBACK, option_entry_parse_autoplay, + "Autoplay policy. Valid options are: allow, allow-without-sound, and deny", NULL}, +#endif {G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &s_options.arguments, "", "[URL]"}, {NULL}};