[geary/bug/728002-webkit2: 54/140] Use an internal URL scheme for displaying message bodies.



commit 57f10446a949655e0826200342021d4eeefa9ac5
Author: Michael James Gratton <mike vee net>
Date:   Sun Jan 1 18:14:50 2017 +1100

    Use an internal URL scheme for displaying message bodies.
    
    This makes debugging easier, since the URL will show up in the console,
    Inspector and elsewhere, and the HTML message source, etc will appear in
    the Inspector.
    
    * src/client/components/client-web-view.vala
      (ClientWebView::init_web_context): Rehitser new internal scheme, hook
      it up to the new ::handle_internal_request handler for it.
      (ClientWebView::load_html): Keep a reference to the HTML body so it can
      be loaded via the URL scheme handler, make the second param optional
      and if null, use the new internal URL for the body part. Update call
      sites to avoid using the second param.
    
    * src/client/web-process/web-process-extension.vala
      (GearyWebExtension::on_send_request): Allow requests for the
      internal URL scheme.

 src/client/components/client-web-view.vala         |   31 ++++++++++++++++++-
 src/client/composer/composer-web-view.vala         |    2 +-
 .../conversation-viewer/conversation-message.vala  |    4 +-
 src/client/web-process/web-process-extension.vala  |   13 +++++---
 4 files changed, 40 insertions(+), 10 deletions(-)
---
diff --git a/src/client/components/client-web-view.vala b/src/client/components/client-web-view.vala
index 6ad3cb4..6723578 100644
--- a/src/client/components/client-web-view.vala
+++ b/src/client/components/client-web-view.vala
@@ -18,7 +18,9 @@ public class ClientWebView : WebKit.WebView {
 
 
     /** URI Scheme and delimiter for images loaded by Content-ID. */
-    public const string CID_PREFIX = "cid:";
+    public const string CID_URL_PREFIX = "cid:";
+
+    private const string INTERNAL_URL_BODY = "geary:body";
 
     private const string PREFERRED_HEIGHT_MESSAGE = "preferredHeightChanged";
     private const string REMOTE_IMAGE_LOAD_BLOCKED_MESSAGE = "remoteImageLoadBlocked";
@@ -44,6 +46,12 @@ public class ClientWebView : WebKit.WebView {
                     view.handle_cid_request(req);
                 }
             });
+        context.register_uri_scheme("geary", (req) => {
+                ClientWebView? view = req.get_web_view() as ClientWebView;
+                if (view != null) {
+                    view.handle_internal_request(req);
+                }
+            });
         context.initialize_web_extensions.connect((context) => {
                 context.set_web_extensions_directory(
                     web_extension_dir.get_path()
@@ -156,6 +164,8 @@ public class ClientWebView : WebKit.WebView {
         }
     }
 
+    private weak string? body = null;
+
     private Gee.Map<string,Geary.Memory.Buffer> cid_resources =
         new Gee.HashMap<string,Geary.Memory.Buffer>();
 
@@ -258,6 +268,14 @@ public class ClientWebView : WebKit.WebView {
     }
 
     /**
+     * Loads a message HTML body into the view.
+     */
+    public new void load_html(string? body, string? base_uri=null) {
+        this.body = body;
+        base.load_html(body, base_uri ?? INTERNAL_URL_BODY);
+    }
+
+    /**
      * Adds an inline resource that may be accessed via a cid:id url.
      */
     public void add_inline_resource(string id, Geary.Memory.Buffer buf) {
@@ -347,7 +365,7 @@ public class ClientWebView : WebKit.WebView {
     }
 
     internal void handle_cid_request(WebKit.URISchemeRequest request) {
-        string cid = request.get_uri().substring(CID_PREFIX.length);
+        string cid = request.get_uri().substring(CID_URL_PREFIX.length);
         Geary.Memory.Buffer? buf = this.cid_resources[cid];
         if (buf != null) {
             request.finish(buf.get_input_stream(), buf.size, null);
@@ -359,6 +377,15 @@ public class ClientWebView : WebKit.WebView {
         }
     }
 
+    internal void handle_internal_request(WebKit.URISchemeRequest request) {
+        if (request.get_uri() == INTERNAL_URL_BODY) {
+            Geary.Memory.Buffer buf = new Geary.Memory.StringBuffer(this.body);
+            request.finish(buf.get_input_stream(), buf.size, null);
+        } else {
+            request.finish_error(new FileError.NOENT("Unknown internal URL"));
+        }
+    }
+
     protected inline void register_message_handler(string name) {
         if (!get_user_content_manager().register_script_message_handler(name)) {
             debug("Failed to register script message handler: %s", name);
diff --git a/src/client/composer/composer-web-view.vala b/src/client/composer/composer-web-view.vala
index 457fc5e..50abc1a 100644
--- a/src/client/composer/composer-web-view.vala
+++ b/src/client/composer/composer-web-view.vala
@@ -92,7 +92,7 @@ public class ComposerWebView : ClientWebView {
         else
             html = body + CURSOR + "<br /><br />" + signature;
 
-        base.load_html(HTML_BODY.printf(html), null);
+        base.load_html(HTML_BODY.printf(html));
     }
 
     public bool can_undo() {
diff --git a/src/client/conversation-viewer/conversation-message.vala 
b/src/client/conversation-viewer/conversation-message.vala
index cc95abe..ff7f236 100644
--- a/src/client/conversation-viewer/conversation-message.vala
+++ b/src/client/conversation-viewer/conversation-message.vala
@@ -451,7 +451,7 @@ public class ConversationMessage : Gtk.Grid {
         }
 
         load_cancelled.cancelled.connect(() => { web_view.stop_loading(); });
-        this.web_view.load_html(body_text ?? "", null);
+        this.web_view.load_html(body_text ?? "");
     }
 
     /**
@@ -690,7 +690,7 @@ public class ConversationMessage : Gtk.Grid {
         return "<img alt=\"%s\" class=\"%s\" src=\"%s%s\" />".printf(
             Geary.HTML.escape_markup(filename),
             REPLACED_IMAGE_CLASS,
-            ClientWebView.CID_PREFIX,
+            ClientWebView.CID_URL_PREFIX,
             Geary.HTML.escape_markup(id)
         );
     }
diff --git a/src/client/web-process/web-process-extension.vala 
b/src/client/web-process/web-process-extension.vala
index 23728e4..ac7efc4 100644
--- a/src/client/web-process/web-process-extension.vala
+++ b/src/client/web-process/web-process-extension.vala
@@ -29,8 +29,10 @@ public void webkit_web_extension_initialize_with_user_data(WebKit.WebExtension e
 public class GearyWebExtension : Object {
 
 
-    private const string CID_PREFIX = "cid:";
-    private const string DATA_PREFIX = "data:";
+    private const string CID_URL_PREFIX = "cid:";
+    private const string DATA_URL_PREFIX = "data:";
+    private const string INTERNAL_URL_PREFIX = "geary:";
+    private const string INTERNAL_URL_BODY = INTERNAL_URL_PREFIX + "body";
 
     private WebKit.WebExtension extension;
 
@@ -63,9 +65,10 @@ public class GearyWebExtension : Object {
                                  WebKit.URIResponse? response) {
         bool should_load = false;
         string req_uri = request.get_uri();
-        if (req_uri.has_prefix(CID_PREFIX) ||
-            req_uri.has_prefix(DATA_PREFIX)) {
-            // Always load images with these prefixes
+        if (req_uri.has_prefix(CID_URL_PREFIX) ||
+            req_uri.has_prefix(DATA_URL_PREFIX) ||
+            req_uri == INTERNAL_URL_BODY) {
+            // Always load images/resources with these prefixes
             should_load = true;
         } else {
             // Only load anything else if remote image loading is


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