[geary/mjog/558-webkit-shared-process: 9/11] Fix remote resource blocking with shared processes



commit 6c57839ddf80af09e6e242574b5a3f36b9067254
Author: Michael Gratton <mike vee net>
Date:   Tue Nov 26 19:32:43 2019 +1100

    Fix remote resource blocking with shared processes
    
    Now that a shared WebKitUserContentManager is shared between web views,
    the old "load a JS file when remote resource loading is allowed" doesn't
    work any more. Instead, set a variable on the frame's window object
    in the web extension notified that a new page has been loaded, and
    use that instead.

 src/client/components/components-web-view.vala    | 12 +------
 src/client/web-process/web-process-extension.vala | 43 +++++++++++++----------
 ui/components-web-view-allow-remote-images.js     | 11 ------
 ui/components-web-view.js                         |  3 +-
 ui/org.gnome.Geary.gresource.xml                  |  1 -
 5 files changed, 26 insertions(+), 44 deletions(-)
---
diff --git a/src/client/components/components-web-view.vala b/src/client/components/components-web-view.vala
index c630505e..5a945c52 100644
--- a/src/client/components/components-web-view.vala
+++ b/src/client/components/components-web-view.vala
@@ -65,7 +65,6 @@ public abstract class Components.WebView : WebKit.WebView, Geary.BaseInterface {
     private static WebKit.UserStyleSheet? user_stylesheet = null;
 
     private static WebKit.UserScript? script = null;
-    private static WebKit.UserScript? allow_remote_images = null;
 
 
     /**
@@ -119,9 +118,6 @@ public abstract class Components.WebView : WebKit.WebView, Geary.BaseInterface {
         WebView.script = load_app_script(
             "components-web-view.js"
         );
-        WebView.allow_remote_images = load_app_script(
-            "components-web-view-allow-remote-images.js"
-        );
 
         foreach (string name in new string[] { USER_CSS, USER_CSS_LEGACY }) {
             GLib.File stylesheet = user_dir.get_child(name);
@@ -406,13 +402,7 @@ public abstract class Components.WebView : WebKit.WebView, Geary.BaseInterface {
      * effect.
      */
     public void allow_remote_image_loading() {
-        // Use a separate script here since we need to update the
-        // value of window.geary.allow_remote_image_loading after it
-        // was first created by components-web-view.js (which is loaded at
-        // the start of page load), but before the page load is
-        // started (so that any remote images present are actually
-        // loaded).
-        this.user_content_manager.add_script(WebView.allow_remote_images);
+        this.run_javascript.begin("_gearyAllowRemoteResourceLoads = true", null);
     }
 
     /**
diff --git a/src/client/web-process/web-process-extension.vala 
b/src/client/web-process/web-process-extension.vala
index b2b29bf9..4bba5154 100644
--- a/src/client/web-process/web-process-extension.vala
+++ b/src/client/web-process/web-process-extension.vala
@@ -32,22 +32,14 @@ public class GearyWebExtension : Object {
 
     private const string[] ALLOWED_SCHEMES = { "cid", "geary", "data", "blob" };
 
+    private const string REMOTE_LOAD_VAR = "_gearyAllowRemoteResourceLoads";
+
     private WebKit.WebExtension extension;
 
 
     public GearyWebExtension(WebKit.WebExtension extension) {
         this.extension = extension;
-        extension.page_created.connect((extension, web_page) => {
-                web_page.console_message_sent.connect(on_console_message);
-                web_page.send_request.connect(on_send_request);
-                // XXX investigate whether the earliest supported
-                // version of WK supports the DOM "selectionchanged"
-                // event, and if so use that rather that doing it in
-                // here in the extension
-                web_page.get_editor().selection_changed.connect(() => {
-                    selection_changed(web_page);
-                });
-            });
+        extension.page_created.connect(on_page_created);
     }
 
     // XXX Conditionally enable while we still depend on WK2 <2.12
@@ -89,14 +81,7 @@ public class GearyWebExtension : Object {
         WebKit.Frame frame = page.get_main_frame();
         JSC.Context context = frame.get_js_context();
         try {
-            JSC.Value ret = execute_script(
-                context,
-                "geary.allowRemoteImages",
-                GLib.Log.FILE,
-                GLib.Log.METHOD,
-                GLib.Log.LINE
-            );
-            should_load = Util.JS.to_bool(ret);
+            should_load = Util.JS.to_bool(context.get_value(REMOTE_LOAD_VAR));
         } catch (GLib.Error err) {
             debug(
                 "Error checking PageState::allowRemoteImages: %s",
@@ -154,4 +139,24 @@ public class GearyWebExtension : Object {
         return ret;
     }
 
+    private void on_page_created(WebKit.WebExtension extension,
+                                 WebKit.WebPage page) {
+        WebKit.Frame frame = page.get_main_frame();
+        JSC.Context context = frame.get_js_context();
+        context.set_value(
+            REMOTE_LOAD_VAR,
+            new JSC.Value.boolean(context, false)
+        );
+
+        page.console_message_sent.connect(on_console_message);
+        page.send_request.connect(on_send_request);
+        // XXX investigate whether the earliest supported
+        // version of WK supports the DOM "selectionchanged"
+        // event, and if so use that rather that doing it in
+        // here in the extension
+        page.get_editor().selection_changed.connect(() => {
+                selection_changed(page);
+            });
+    }
+
 }
diff --git a/ui/components-web-view.js b/ui/components-web-view.js
index 90f4cc14..80e86d7c 100644
--- a/ui/components-web-view.js
+++ b/ui/components-web-view.js
@@ -14,7 +14,6 @@ let PageState = function() {
 };
 PageState.prototype = {
     init: function() {
-        this.allowRemoteImages = false;
         this.isLoaded = false;
         this.undoEnabled = false;
         this.redoEnabled = false;
@@ -108,7 +107,7 @@ PageState.prototype = {
         window.webkit.messageHandlers.contentLoaded.postMessage(null);
     },
     loadRemoteImages: function() {
-        this.allowRemoteImages = true;
+        window._gearyAllowRemoteResourceLoads = true;
         let images = document.getElementsByTagName("IMG");
         for (let i = 0; i < images.length; i++) {
             let img = images.item(i);
diff --git a/ui/org.gnome.Geary.gresource.xml b/ui/org.gnome.Geary.gresource.xml
index fef3b445..4e78a446 100644
--- a/ui/org.gnome.Geary.gresource.xml
+++ b/ui/org.gnome.Geary.gresource.xml
@@ -10,7 +10,6 @@
     <file compressed="true" preprocess="xml-stripblanks">application-main-window.ui</file>
     <file compressed="true" preprocess="xml-stripblanks">certificate_warning_dialog.glade</file>
     <file compressed="true">components-web-view.js</file>
-    <file compressed="true">components-web-view-allow-remote-images.js</file>
     <file compressed="true" preprocess="xml-stripblanks">components-attachment-pane.ui</file>
     <file compressed="true" preprocess="xml-stripblanks">components-attachment-pane-menus.ui</file>
     <file compressed="true" preprocess="xml-stripblanks">components-attachment-view.ui</file>


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