[epiphany/mcatanzaro/decide-policy] WIP: Fix decide policy callbacks



commit f3981166f04cbd434f8a76731242878bedaaa1e9
Author: Michael Catanzaro <mcatanzaro gnome org>
Date:   Tue Jun 30 21:11:33 2020 -0500

    WIP: Fix decide policy callbacks
    
    I'm too tired to write a commit message tonight. Also this is yucky and
    needs refactored. Also having decide-policy in two different places
    might be too confusing.

 embed/ephy-web-view.c | 66 ++++++---------------------------------------------
 src/ephy-window.c     | 40 ++++++++++++++++++++++++++-----
 2 files changed, 41 insertions(+), 65 deletions(-)
---
diff --git a/embed/ephy-web-view.c b/embed/ephy-web-view.c
index a0bf84d7d..e0b4eebe7 100644
--- a/embed/ephy-web-view.c
+++ b/embed/ephy-web-view.c
@@ -812,46 +812,10 @@ process_terminated_cb (EphyWebView                       *web_view,
 }
 
 static gboolean
-decide_navigation (WebKitWebView        *web_view,
-                   WebKitPolicyDecision *decision,
-                   gpointer              user_data)
-{
-  EphyEmbedShell *shell = ephy_embed_shell_get_default ();
-  WebKitWebsitePolicies *website_policies = NULL;
-  g_autofree gchar *origin = NULL;
-  WebKitNavigationPolicyDecision *navigation_policy_decision = WEBKIT_NAVIGATION_POLICY_DECISION (decision);
-  WebKitNavigationAction *navigation_action = webkit_navigation_policy_decision_get_navigation_action 
(navigation_policy_decision);
-  WebKitURIRequest *request = webkit_navigation_action_get_request (navigation_action);
-  const gchar *navigation_uri = webkit_uri_request_get_uri (request);
-  EphyPermission permission = EPHY_PERMISSION_UNDECIDED;
-
-  origin = ephy_uri_to_security_origin (navigation_uri);
-  if (origin)
-    permission = ephy_permissions_manager_get_permission (ephy_embed_shell_get_permissions_manager (shell),
-                                                          EPHY_PERMISSION_TYPE_AUTOPLAY_POLICY,
-                                                          origin);
-
-  switch (permission) {
-    case EPHY_PERMISSION_UNDECIDED:
-      website_policies = webkit_website_policies_new_with_policies ("autoplay", 
WEBKIT_AUTOPLAY_ALLOW_WITHOUT_SOUND, NULL);
-      break;
-    case EPHY_PERMISSION_PERMIT:
-      website_policies = webkit_website_policies_new_with_policies ("autoplay", WEBKIT_AUTOPLAY_ALLOW, NULL);
-      break;
-    case EPHY_PERMISSION_DENY:
-      website_policies = webkit_website_policies_new_with_policies ("autoplay", WEBKIT_AUTOPLAY_DENY, NULL);
-      break;
-  }
-
-  webkit_policy_decision_use_with_policies (decision, website_policies);
-  g_object_unref (website_policies);
-  return TRUE;
-}
-
-static gboolean
-decide_resource (WebKitWebView        *web_view,
-                 WebKitPolicyDecision *decision,
-                 gpointer              user_data)
+decide_policy_cb (WebKitWebView            *web_view,
+                  WebKitPolicyDecision     *decision,
+                  WebKitPolicyDecisionType  decision_type,
+                  gpointer                  user_data)
 {
   WebKitResponsePolicyDecision *response_decision;
   WebKitURIResponse *response;
@@ -862,6 +826,9 @@ decide_resource (WebKitWebView        *web_view,
   const char *request_uri;
   gboolean is_main_resource;
 
+  if (decision_type != WEBKIT_POLICY_DECISION_TYPE_RESPONSE)
+    return FALSE;
+
   /* If WebKit can handle the MIME type, let it.
    * Otherwise, we'll start a download.
    */
@@ -914,25 +881,6 @@ decide_resource (WebKitWebView        *web_view,
   return TRUE;
 }
 
-static gboolean
-decide_policy_cb (WebKitWebView            *web_view,
-                  WebKitPolicyDecision     *decision,
-                  WebKitPolicyDecisionType  decision_type,
-                  gpointer                  user_data)
-{
-  switch (decision_type) {
-    case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
-      return decide_navigation (web_view, decision, user_data);
-    case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
-      return decide_resource (web_view, decision, user_data);
-    case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
-      /* not handled */
-      break;
-  }
-
-  return TRUE;
-}
-
 typedef struct {
   EphyWebView *web_view;
   WebKitPermissionRequest *request;
diff --git a/src/ephy-window.c b/src/ephy-window.c
index 181b59653..c6ea2f9c3 100644
--- a/src/ephy-window.c
+++ b/src/ephy-window.c
@@ -45,6 +45,7 @@
 #include "ephy-mouse-gesture-controller.h"
 #include "ephy-notebook.h"
 #include "ephy-pages-view.h"
+#include "ephy-permissions-manager.h"
 #include "ephy-prefs.h"
 #include "ephy-security-popover.h"
 #include "ephy-session.h"
@@ -54,6 +55,7 @@
 #include "ephy-title-box.h"
 #include "ephy-title-widget.h"
 #include "ephy-type-builtins.h"
+#include "ephy-uri-helpers.h"
 #include "ephy-view-source-handler.h"
 #include "ephy-web-app-utils.h"
 #include "ephy-web-view.h"
@@ -2049,8 +2051,12 @@ decide_navigation_policy (WebKitWebView            *web_view,
   WebKitNavigationPolicyDecision *navigation_decision;
   WebKitNavigationAction *navigation_action;
   WebKitNavigationType navigation_type;
+  WebKitWebsitePolicies *website_policies;
   WebKitURIRequest *request;
   const char *uri;
+  const char *origin;
+  EphyPermission permission = EPHY_PERMISSION_UNDECIDED;
+  EphyEmbedShell *shell;
   EphyEmbed *embed;
 
   g_assert (WEBKIT_IS_WEB_VIEW (web_view));
@@ -2058,6 +2064,8 @@ decide_navigation_policy (WebKitWebView            *web_view,
   g_assert (decision_type != WEBKIT_POLICY_DECISION_TYPE_RESPONSE);
   g_assert (EPHY_IS_WINDOW (window));
 
+  shell = ephy_embed_shell_get_default ();
+
   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);
@@ -2077,8 +2085,7 @@ decide_navigation_policy (WebKitWebView            *web_view,
         if (error) {
           LOG ("failed to handle non web scheme: %s", error->message);
           g_error_free (error);
-
-          return FALSE;
+          goto accept;
         }
       }
     }
@@ -2119,7 +2126,7 @@ decide_navigation_policy (WebKitWebView            *web_view,
     if (navigation_type == WEBKIT_NAVIGATION_TYPE_LINK_CLICKED ||
         (navigation_type == WEBKIT_NAVIGATION_TYPE_OTHER && webkit_navigation_action_is_user_gesture 
(navigation_action))) {
       if (ephy_web_application_is_uri_allowed (uri))
-        return FALSE;
+        goto accept;
 
       /* We can't get here under flatpak because this code only
        * executes in web app mode.
@@ -2172,9 +2179,8 @@ decide_navigation_policy (WebKitWebView            *web_view,
         return TRUE;
       }
     }
-    /* Those were our special cases, we won't handle this */
     else {
-      return FALSE;
+      goto accept;
     }
 
     embed = ephy_embed_container_get_active_child
@@ -2205,7 +2211,29 @@ decide_navigation_policy (WebKitWebView            *web_view,
     return TRUE;
   }
 
-  return FALSE;
+accept:
+  origin = ephy_uri_to_security_origin (uri);
+  if (origin)
+    permission = ephy_permissions_manager_get_permission (ephy_embed_shell_get_permissions_manager (shell),
+                                                          EPHY_PERMISSION_TYPE_AUTOPLAY_POLICY,
+                                                          origin);
+
+  switch (permission) {
+    case EPHY_PERMISSION_UNDECIDED:
+      website_policies = webkit_website_policies_new_with_policies ("autoplay", 
WEBKIT_AUTOPLAY_ALLOW_WITHOUT_SOUND, NULL);
+      break;
+    case EPHY_PERMISSION_PERMIT:
+      website_policies = webkit_website_policies_new_with_policies ("autoplay", WEBKIT_AUTOPLAY_ALLOW, NULL);
+      break;
+    case EPHY_PERMISSION_DENY:
+      website_policies = webkit_website_policies_new_with_policies ("autoplay", WEBKIT_AUTOPLAY_DENY, NULL);
+      break;
+  }
+
+  webkit_policy_decision_use_with_policies (decision, website_policies);
+  g_object_unref (website_policies);
+
+  return TRUE;
 }
 
 static void


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