From 3655f4896f2b33f47a94c8e7dc4f9623ba42fc2e Mon Sep 17 00:00:00 2001 From: Michael Gratton Date: Fri, 28 Aug 2020 11:38:06 +1000 Subject: [PATCH 076/124] =?UTF-8?q?Composer.WebView:=20Convert=20to=20usin?= =?UTF-8?q?g=20messages=20for=20JS=20=E2=86=92=20client=20comms?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/composer/composer-web-view.vala | 75 ++++++++++------------ ui/composer-web-view.js | 18 ++++-- 2 files changed, 45 insertions(+), 48 deletions(-) diff --git a/src/client/composer/composer-web-view.vala b/src/client/composer/composer-web-view.vala index 24a2740c..57b7e1e4 100644 --- a/src/client/composer/composer-web-view.vala +++ b/src/client/composer/composer-web-view.vala @@ -33,8 +33,8 @@ public class Composer.WebView : Components.WebView { private const string SPACER = "

"; // WebKit message handler names - private const string CURSOR_CONTEXT_CHANGED = "cursorContextChanged"; - private const string DRAG_DROP_RECEIVED = "dragDropReceived"; + private const string CURSOR_CONTEXT_CHANGED = "cursor_context_changed"; + private const string DRAG_DROP_RECEIVED = "drag_drop_received"; /** * Encapsulates editing-related state for a specific DOM node. @@ -152,8 +152,8 @@ public class Composer.WebView : Components.WebView { this.user_content_manager.add_style_sheet(WebView.app_style); this.user_content_manager.add_script(WebView.app_script); - register_message_handler(CURSOR_CONTEXT_CHANGED, on_cursor_context_changed); - register_message_handler(DRAG_DROP_RECEIVED, on_drag_drop_received); + register_message_callback(CURSOR_CONTEXT_CHANGED, on_cursor_context_changed); + register_message_callback(DRAG_DROP_RECEIVED, on_drag_drop_received); // XXX this is a bit of a hack given the docs for is_empty, // above @@ -530,50 +530,43 @@ public class Composer.WebView : Components.WebView { return ret; } - private void on_cursor_context_changed(WebKit.JavascriptResult result) { - try { - cursor_context_changed( - new EditContext(Util.JS.to_string(result.get_js_value())) - ); - } catch (Util.JS.Error err) { - debug("Could not get text cursor style: %s", err.message); + private void on_cursor_context_changed(GLib.Variant? parameters) { + if (parameters != null && parameters.classify() == STRING) { + cursor_context_changed(new EditContext(parameters as string)); + } else { + warning("Could not get text cursor style"); } } /** * Handle a dropped image */ - private void on_drag_drop_received(WebKit.JavascriptResult result) { - - try { - JSC.Value object = result.get_js_value(); - string filename = Util.JS.to_string( - Util.JS.get_property(object, "fileName") - ); - string filename_unescaped = GLib.Uri.unescape_string(filename); - - string file_type = Util.JS.to_string( - Util.JS.get_property(object, "fileType") - ); - - string content_base64 = Util.JS.to_string( - Util.JS.get_property(object, "content") - ); - uint8[] image = GLib.Base64.decode(content_base64); - - if (image.length == 0) { - warning("%s is empty", filename); - return; - } + private void on_drag_drop_received(GLib.Variant? parameters) { + var dict = new GLib.VariantDict(parameters); + string file_name = dict.lookup_value( + "fileName", GLib.VariantType.STRING + ).get_string(); + string file_name_unescaped = GLib.Uri.unescape_string(file_name); + + string file_type = dict.lookup_value( + "fileType", GLib.VariantType.STRING + ).get_string(); + + string content_base64 = dict.lookup_value( + "content", GLib.VariantType.STRING + ).get_string(); + uint8[] image = GLib.Base64.decode(content_base64); + + if (image.length == 0) { + warning("%s is empty", file_name); + return; + } - // A simple check to see if the file looks like an image. A problem here - // will be this accepting types which won't be supported by WebKit - // or recipients. - if (file_type.index_of("image/") == 0) { - image_file_dropped(filename_unescaped, file_type, image); - } - } catch (Util.JS.Error err) { - debug("Could not get deceptive link param: %s", err.message); + // A simple check to see if the file looks like an image. A problem here + // will be this accepting types which won't be supported by WebKit + // or recipients. + if (file_type.index_of("image/") == 0) { + image_file_dropped(file_name_unescaped, file_type, image); } } } diff --git a/ui/composer-web-view.js b/ui/composer-web-view.js index ca918990..4fe34ad0 100644 --- a/ui/composer-web-view.js +++ b/ui/composer-web-view.js @@ -1,6 +1,6 @@ /* - * Copyright 2016 Software Freedom Conservancy Inc. - * Copyright 2016 Michael Gratton + * Copyright © 2016 Software Freedom Conservancy Inc. + * Copyright © 2016-2020 Michael Gratton * * This software is licensed under the GNU Lesser General Public License * (version 2.1 or later). See the COPYING file in this distribution. @@ -35,6 +35,9 @@ ComposerPageState.prototype = { this.nextSelectionId = 0; this.cursorContext = null; + this._cursorContextChanged = MessageSender("cursor_context_changed"); + this._dragDropReceived = MessageSender("drag_drop_received"); + document.addEventListener("click", function(e) { if (e.target.tagName == "A") { e.preventDefault(); @@ -99,7 +102,9 @@ ComposerPageState.prototype = { }, true); // Handle file drag & drop - document.body.addEventListener("drop", state.handleFileDrop, true); + document.body.addEventListener("drop", function(e) { + state.handleFileDrop(e); + }, true); document.body.addEventListener("allowDrop", function(e) { ev.preventDefault(); }, true); @@ -346,9 +351,7 @@ ComposerPageState.prototype = { let newContext = new EditContext(cursor); if (!newContext.equals(this.cursorContext)) { this.cursorContext = newContext; - window.webkit.messageHandlers.cursorContextChanged.postMessage( - newContext.encode() - ); + this._cursorContextChanged(newContext.encode()); } } @@ -396,13 +399,14 @@ ComposerPageState.prototype = { continue; const reader = new FileReader(); + const state = this; reader.onload = (function(filename, imageType) { return function(loadEvent) { // Remove prefixed file type and encoding type var parts = loadEvent.target.result.split(","); if (parts.length < 2) return; - window.webkit.messageHandlers.dragDropReceived.postMessage({ + state._dragDropReceived({ fileName: encodeURIComponent(filename), fileType: imageType, content: parts[1] -- 2.29.2