[epiphany/pgriffis/web-extension/navigation-policy] WebExtensions: Handle the navigation policy of extension views




commit d1561193d20b9facef136cf55b1a32cdad097c7b
Author: Patrick Griffis <pgriffis igalia com>
Date:   Fri Jun 3 17:29:27 2022 -0500

    WebExtensions: Handle the navigation policy of extension views
    
    We don't want extensions views to navigate outside of their host
    and we also want new window requests to open in the main browser.

 src/webextension/ephy-web-extension-manager.c | 47 +++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
---
diff --git a/src/webextension/ephy-web-extension-manager.c b/src/webextension/ephy-web-extension-manager.c
index a61ec724d..6161aa508 100644
--- a/src/webextension/ephy-web-extension-manager.c
+++ b/src/webextension/ephy-web-extension-manager.c
@@ -741,6 +741,51 @@ init_web_extension_api (WebKitWebContext *web_context,
   webkit_web_context_set_web_extensions_initialization_user_data (web_context, g_steal_pointer (&user_data));
 }
 
+static gboolean
+decide_policy_cb (WebKitWebView            *web_view,
+                  WebKitPolicyDecision     *decision,
+                  WebKitPolicyDecisionType  decision_type,
+                  EphyWebExtension         *web_extension)
+{
+  WebKitNavigationPolicyDecision *navigation_decision;
+  WebKitNavigationAction *navigation_action;
+  WebKitURIRequest *request;
+  const char *request_uri;
+  const char *request_scheme;
+  EphyEmbed *embed;
+  EphyWebView *new_view;
+
+  if (decision_type != WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION &&
+      decision_type != WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION)
+    return FALSE;
+
+  navigation_decision = WEBKIT_NAVIGATION_POLICY_DECISION (decision);
+  navigation_action = webkit_navigation_policy_decision_get_navigation_action (navigation_decision);
+  request = webkit_navigation_action_get_request (navigation_action);
+  request_uri = webkit_uri_request_get_uri (request);
+
+  if (decision_type == WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION) {
+    g_autofree char *allowed_prefix = g_strdup_printf ("ephy-webextension://%s/", 
ephy_web_extension_get_guid (web_extension));
+    if (g_str_has_prefix (request_uri, allowed_prefix))
+      webkit_policy_decision_use (decision);
+    else {
+      g_warning ("Extension '%s' tried to navigate to %s", ephy_web_extension_get_name (web_extension), 
request_uri);
+      webkit_policy_decision_ignore (decision);
+    }
+    return TRUE;
+  }
+
+  request_scheme = g_uri_peek_scheme (request_uri);
+  if (g_strcmp0 (request_scheme, "https") == 0 || g_strcmp0 (request_scheme, "http") == 0) {
+    embed = ephy_shell_new_tab (ephy_shell_get_default (), NULL, NULL, 0);
+    new_view = ephy_embed_get_web_view (embed);
+    ephy_web_view_load_url (new_view, request_uri);
+  }
+
+  webkit_policy_decision_ignore (decision);
+  return TRUE;
+}
+
 static GtkWidget *
 create_web_extensions_webview (EphyWebExtension *web_extension)
 {
@@ -772,6 +817,8 @@ create_web_extensions_webview (EphyWebExtension *web_extension)
   settings = webkit_web_view_get_settings (WEBKIT_WEB_VIEW (web_view));
   webkit_settings_set_enable_write_console_messages_to_stdout (settings, TRUE);
 
+  g_signal_connect (web_view, "decide-policy", G_CALLBACK (decide_policy_cb), web_extension);
+
   return web_view;
 }
 


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