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.
This commit is contained in:
Michael Gratton 2020-08-28 11:59:11 +10:00 committed by Michael James Gratton
parent 7b0146274c
commit 7950ce50c6
2 changed files with 8 additions and 60 deletions

View file

@ -77,7 +77,10 @@ public class GearyWebExtension : Object {
if (should_load_remote_images(page)) { if (should_load_remote_images(page)) {
should_load = true; should_load = true;
} else { } 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; 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, private WebKit.UserMessage to_exception_message(string? name,
string? message, string? message,
string? backtrace = null, string? backtrace = null,
@ -208,13 +163,6 @@ public class GearyWebExtension : Object {
page.console_message_sent.connect(on_console_message); page.console_message_sent.connect(on_console_message);
page.send_request.connect(on_send_request); 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); page.user_message_received.connect(on_page_message_received);
} }

View file

@ -22,7 +22,6 @@ PageState.prototype = {
this._selectionChanged = MessageSender("selection_changed"); this._selectionChanged = MessageSender("selection_changed");
this._contentLoaded = MessageSender("content_loaded"); this._contentLoaded = MessageSender("content_loaded");
this._remoteImageLoadBlocked = MessageSender("remote_image_load_blocked");
this._preferredHeightChanged = MessageSender("preferred_height_changed"); this._preferredHeightChanged = MessageSender("preferred_height_changed");
this._commandStackChanged = MessageSender("command_stack_changed"); this._commandStackChanged = MessageSender("command_stack_changed");
this._documentModified = MessageSender("document_modified"); this._documentModified = MessageSender("document_modified");
@ -46,6 +45,10 @@ PageState.prototype = {
state.loaded(); state.loaded();
}); });
document.addEventListener("selectionchange", function(e) {
state.selectionChanged();
});
// Coalesce multiple calls to updatePreferredHeight using a // Coalesce multiple calls to updatePreferredHeight using a
// timeout to avoid the overhead of multiple JS messages sent // timeout to avoid the overhead of multiple JS messages sent
// to the app and hence view multiple resizes being queued. // to the app and hence view multiple resizes being queued.
@ -148,9 +151,6 @@ PageState.prototype = {
stopBodyObserver: function() { stopBodyObserver: function() {
this.bodyObserver.disconnect(); this.bodyObserver.disconnect();
}, },
remoteImageLoadBlocked: function() {
this._remoteImageLoadBlocked();
},
/** /**
* Sends "preferredHeightChanged" message if it has changed. * Sends "preferredHeightChanged" message if it has changed.
*/ */