[geary/wip/728002-webkit2: 21/43] Implement web view resource loading policy for CID, data, & remote URLs.



commit 3d60325ddf33da04bbd7f70ca2a62cf5df78369b
Author: Michael James Gratton <mike vee net>
Date:   Mon Oct 10 18:09:55 2016 +1100

    Implement web view resource loading policy for CID, data, & remote URLs.
    
    * src/client/web-process/web-process-extension.vala
      (GearyWebExtension::GearyWebExtension): Set the allow_prefix here,
      since this is where we need to make the decision. Hook up to the
      page-created and subsequently the send-request signals.
      (GearyWebExtension::on_send_request): Always allow data and cid
      requests through, only allow remote requests through if they have the
      allow prefix.
      (random_string): Moved here from util-random so we can call it.
    
    * src/client/components/client-web-view.vala
      (ClientWebView::allow_prefix): Keep this here for now, but its value
      still needs to be fetched from the extension.
      (ClientWebView::on_resource_load_started): Removed, the policy needs to
      be set in the extension to actually work.

 src/client/components/client-web-view.vala        |   35 ++++----------------
 src/client/util/util-random.vala                  |   10 ------
 src/client/web-process/web-process-extension.vala |   36 +++++++++++++++++++++
 3 files changed, 43 insertions(+), 38 deletions(-)
---
diff --git a/src/client/components/client-web-view.vala b/src/client/components/client-web-view.vala
index 21117db..547c9bf 100644
--- a/src/client/components/client-web-view.vala
+++ b/src/client/components/client-web-view.vala
@@ -13,6 +13,7 @@ public class ClientWebView : WebKit.WebView {
 
 
     public bool is_loaded { get; private set; default = false; }
+    public string allow_prefix { get; private set; default = ""; }
 
     private string _document_font;
     public string document_font {
@@ -51,8 +52,6 @@ public class ClientWebView : WebKit.WebView {
         set { if (zoom_level != (float)value) zoom_level = (float)value; }
     }
 
-    public string allow_prefix { get; private set; }
-
     private Gee.Map<string,File> cid_resources = new Gee.HashMap<string,File>();
 
 
@@ -70,9 +69,8 @@ public class ClientWebView : WebKit.WebView {
 
         Object(user_content_manager: content_manager, settings: setts);
 
-        this.allow_prefix = random_string(10) + ":";
+        // XXX get the allow prefix from the extension somehow
 
-        this.resource_load_started.connect(on_resource_load_started);
         this.decide_policy.connect(on_decide_policy);
         this.load_changed.connect((web_view, event) => {
                 if (event == WebKit.LoadEvent.FINISHED) {
@@ -89,8 +87,11 @@ public class ClientWebView : WebKit.WebView {
         system_settings.bind("monospace-font-name", this, "monospace-font", SettingsBindFlags.DEFAULT);
     }
 
-    public void add_cid_resource(string cid, File file) {
-        this.cid_resources[cid] = file;
+    /**
+     * Adds a resource that may be accessed via a cid:id url.
+     */
+    public void add_cid_resource(string id, File file) {
+        this.cid_resources[id] = file;
     }
 
     /**
@@ -160,28 +161,6 @@ public class ClientWebView : WebKit.WebView {
         return Gdk.EVENT_STOP;
     }
 
-    private void on_resource_load_started(WebKit.WebView view,
-                                          WebKit.WebResource resource,
-                                          WebKit.URIRequest request) {
-        const string ABOUT_BLANK = "about:blank";
-        const string CID_PREFIX = "cid:";
-        const string DATA_PREFIX = "data:";
-
-        string? req_uri = request.get_uri();
-        string resp_uri = ABOUT_BLANK;
-        if (req_uri.has_prefix(CID_PREFIX)) {
-            File? file = this.cid_resources[req_uri.substring(CID_PREFIX.length)];
-            if (file != null) {
-                resp_uri = file.get_uri();
-            }
-        } else if (req_uri.has_prefix(this.allow_prefix)) {
-            resp_uri = req_uri.substring(this.allow_prefix.length);
-        } else if (req_uri.has_prefix(DATA_PREFIX)) {
-            resp_uri = req_uri;
-        }
-        request.set_uri(resp_uri);
-    }
-
     private bool on_scroll_event(Gdk.EventScroll event) {
         if ((event.state & Gdk.ModifierType.CONTROL_MASK) != 0) {
             double dir = 0;
diff --git a/src/client/util/util-random.vala b/src/client/util/util-random.vala
index f34ba20..879d637 100644
--- a/src/client/util/util-random.vala
+++ b/src/client/util/util-random.vala
@@ -4,13 +4,3 @@
  * (version 2.1 or later).  See the COPYING file in this distribution.
  */
 
-private string random_string(int length) {
-    // No upper case letters, since request gets lower-cased.
-    string chars = "abcdefghijklmnopqrstuvwxyz";
-    char[] random = new char[length+1]; //leave room for terminating null
-    for (int i = 0; i < length; i++)
-        random[i] = chars[Random.int_range(0, chars.length)];
-    random[length] = '\0'; //make sure the string is null-terminated
-    return (string) random;
-}
-
diff --git a/src/client/web-process/web-process-extension.vala 
b/src/client/web-process/web-process-extension.vala
index 948735e..227932e 100644
--- a/src/client/web-process/web-process-extension.vala
+++ b/src/client/web-process/web-process-extension.vala
@@ -31,9 +31,45 @@ public class GearyWebExtension : Object {
 
     private WebKit.WebExtension extension;
 
+    private string allow_prefix;
+
 
     public GearyWebExtension(WebKit.WebExtension extension) {
         this.extension = extension;
+        this.allow_prefix = random_string(10) + ":";
+
+        extension.page_created.connect((extension, web_page) => {
+                web_page.send_request.connect(on_send_request);
+            });
+    }
+
+    private bool on_send_request(WebKit.WebPage page,
+                                 WebKit.URIRequest request,
+                                 WebKit.URIResponse? response) {
+        const string CID_PREFIX = "cid:";
+        const string DATA_PREFIX = "data:";
+
+        bool should_load = false;
+        string req_uri = request.get_uri();
+        if (req_uri.has_prefix(CID_PREFIX) |
+            req_uri.has_prefix(DATA_PREFIX)) {
+            should_load = true;
+        } else if (req_uri.has_prefix(this.allow_prefix)) {
+            should_load = true;
+            request.set_uri(req_uri.substring(this.allow_prefix.length));
+        }
+
+        return should_load ? Gdk.EVENT_PROPAGATE : Gdk.EVENT_STOP; // LOL
     }
 
 }
+
+private string random_string(int length) {
+    // No upper case letters, since request gets lower-cased.
+    string chars = "abcdefghijklmnopqrstuvwxyz";
+    char[] random = new char[length+1]; //leave room for terminating null
+    for (int i = 0; i < length; i++)
+        random[i] = chars[Random.int_range(0, chars.length)];
+    random[length] = '\0'; //make sure the string is null-terminated
+    return (string) random;
+}


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