From 7950ce50c6bdb6ca77561a00d1afb9bc689278cf Mon Sep 17 00:00:00 2001
From: Michael Gratton <mike@vee.net>
Date: Fri, 28 Aug 2020 11:59:11 +1000
Subject: [PATCH 078/124] GearyWebExtension: Untangle extension and JS
 interaction a bit

Move selection changed event listener into JS so it doesn't have to
cross the JS/native boundary twice.

Move sending remote load blocked from JS to the extension since we can
do that directly now and again so the JS/native boundary doesn't need
to be double-crossed again.
---
 .../web-process/web-process-extension.vala    | 60 ++-----------------
 ui/components-web-view.js                     |  8 +--
 2 files changed, 8 insertions(+), 60 deletions(-)

diff --git a/src/client/web-process/web-process-extension.vala b/src/client/web-process/web-process-extension.vala
index 31f2b0f0..6eed7746 100644
--- a/src/client/web-process/web-process-extension.vala
+++ b/src/client/web-process/web-process-extension.vala
@@ -77,7 +77,10 @@ public class GearyWebExtension : Object {
             if (should_load_remote_images(page)) {
                 should_load = true;
             } else {
-                remote_image_load_blocked(page);
+                page.send_message_to_view.begin(
+                    new WebKit.UserMessage("remote_image_load_blocked", null),
+                    null
+                );
             }
         }
 
@@ -99,54 +102,6 @@ public class GearyWebExtension : Object {
         return should_load;
     }
 
-    private void remote_image_load_blocked(WebKit.WebPage page) {
-        WebKit.Frame frame = page.get_main_frame();
-        JSC.Context context = frame.get_js_context();
-        try {
-            execute_script(
-                context,
-                "geary.remoteImageLoadBlocked();",
-                GLib.Log.FILE,
-                GLib.Log.METHOD,
-                GLib.Log.LINE
-            );
-        } catch (Error err) {
-            debug(
-                "Error calling PageState::remoteImageLoadBlocked: %s",
-                err.message
-            );
-        }
-    }
-
-    private void selection_changed(WebKit.WebPage page) {
-        WebKit.Frame frame = page.get_main_frame();
-        JSC.Context context = frame.get_js_context();
-        try {
-            execute_script(
-                context,
-                "geary.selectionChanged();",
-                GLib.Log.FILE,
-                GLib.Log.METHOD,
-                GLib.Log.LINE
-            );
-        } catch (Error err) {
-            debug("Error calling PageStates::selectionChanged: %s", err.message);
-        }
-    }
-
-    private JSC.Value execute_script(JSC.Context context,
-                                     string script,
-                                     string file_name,
-                                     string method_name,
-                                     int line_number)
-        throws Util.JS.Error {
-        JSC.Value ret = context.evaluate_with_source_uri(
-            script, -1, "geary:%s/%s".printf(file_name, method_name), line_number
-        );
-        Util.JS.check_exception(context);
-        return ret;
-    }
-
     private WebKit.UserMessage to_exception_message(string? name,
                                                     string? message,
                                                     string? backtrace = null,
@@ -208,13 +163,6 @@ public class GearyWebExtension : Object {
 
         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);
-            });
         page.user_message_received.connect(on_page_message_received);
     }
 
diff --git a/ui/components-web-view.js b/ui/components-web-view.js
index 29b6acd5..d0998a67 100644
--- a/ui/components-web-view.js
+++ b/ui/components-web-view.js
@@ -22,7 +22,6 @@ PageState.prototype = {
 
         this._selectionChanged = MessageSender("selection_changed");
         this._contentLoaded = MessageSender("content_loaded");
-        this._remoteImageLoadBlocked = MessageSender("remote_image_load_blocked");
         this._preferredHeightChanged = MessageSender("preferred_height_changed");
         this._commandStackChanged = MessageSender("command_stack_changed");
         this._documentModified = MessageSender("document_modified");
@@ -46,6 +45,10 @@ PageState.prototype = {
             state.loaded();
         });
 
+        document.addEventListener("selectionchange", function(e) {
+            state.selectionChanged();
+        });
+
         // Coalesce multiple calls to updatePreferredHeight using a
         // timeout to avoid the overhead of multiple JS messages sent
         // to the app and hence view multiple resizes being queued.
@@ -148,9 +151,6 @@ PageState.prototype = {
     stopBodyObserver: function() {
         this.bodyObserver.disconnect();
     },
-    remoteImageLoadBlocked: function() {
-        this._remoteImageLoadBlocked();
-    },
     /**
      * Sends "preferredHeightChanged" message if it has changed.
      */
-- 
2.29.2

