[geary/wip/728002-webkit2: 10/15] Implement loading cid: scheme resources in ClientWebView.



commit 867fc37830059ba872df98742b349c30734e74bd
Author: Michael James Gratton <mike vee net>
Date:   Mon Oct 10 18:15:30 2016 +1100

    Implement loading cid: scheme resources in ClientWebView.
    
    * src/client/application/geary-controller.vala
      (GearyController::open_async): Register a handler for the 'cid' scheme.
    
    * src/client/components/client-web-view.vala
      (ClientWebView::handle_cid_request): Hook up requests for cid URIs
      using attachment files.

 src/client/application/geary-controller.vala |    6 ++++++
 src/client/components/client-web-view.vala   |   18 ++++++++++++++++++
 2 files changed, 24 insertions(+), 0 deletions(-)
---
diff --git a/src/client/application/geary-controller.vala b/src/client/application/geary-controller.vala
index 0181034..8d84c67 100644
--- a/src/client/application/geary-controller.vala
+++ b/src/client/application/geary-controller.vala
@@ -191,6 +191,12 @@ public class GearyController : Geary.BaseObject {
         WebKit.WebContext context = WebKit.WebContext.get_default();
         context.set_process_model(WebKit.ProcessModel.SHARED_SECONDARY_PROCESS);
         context.set_cache_model(WebKit.CacheModel.DOCUMENT_BROWSER);
+        context.register_uri_scheme("cid", (req) => {
+                ClientWebView? view = req.get_web_view() as ClientWebView;
+                if (view != null) {
+                    view.handle_cid_request(req);
+                }
+            });
         context.initialize_web_extensions.connect((context) => {
                 context.set_web_extensions_directory(
                     this.application.get_web_extensions_dir().get_path()
diff --git a/src/client/components/client-web-view.vala b/src/client/components/client-web-view.vala
index 547c9bf..e0dfae0 100644
--- a/src/client/components/client-web-view.vala
+++ b/src/client/components/client-web-view.vala
@@ -130,6 +130,24 @@ public class ClientWebView : WebKit.WebView {
         this.zoom_level -= (this.zoom_level * ZOOM_FACTOR);
     }
 
+    internal void handle_cid_request(WebKit.URISchemeRequest request) {
+        const string CID_PREFIX = "cid:";
+
+        string cid = request.get_uri().substring(CID_PREFIX.length);
+        File? file = this.cid_resources[cid];
+        if (file != null) {
+            try {
+                request.finish(file.read(), -1, null);
+            } catch (Error err) {
+                request.finish_error(err);
+            }
+        } else {
+            request.finish_error(
+                new FileError.NOENT("Unknown CID: %s".printf(cid))
+            );
+        }
+    }
+
     // Only allow string-based page loads, and notify but ignore if
     // the user attempts to click on a link. Deny everything else.
     private bool on_decide_policy(WebKit.WebView view,


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