[geary/wip/728002-webkit2: 37/43] Load a default app script into ClientWebViews at construction time.



commit 19f27205d65b2ff8561a5ee0819e05863a5cbb1d
Author: Michael James Gratton <mike vee net>
Date:   Wed Nov 23 23:48:47 2016 +1100

    Load a default app script into ClientWebViews at construction time.
    
    * src/client/application/geary-controller.vala
      (GearyController::open_async): Load the app script for ClientWebView at
      startup.
    
    * src/client/components/client-web-view.vala
      (ClientWebView::ClientWebView): Ensure we actually have a
      UserContentManager instance to work with, add the app script to the
      manager for the instance.
      (ClientWebView::load_scripts): Actually load client-web-view.js.
      (ClientWebView::load_user_stylesheet): Helper method for this class and
      subsclasses for doing the actual script load from the app.
    
    * ui/client-web-view.js: Add boilerplaye for new file.
    
    * ui/CMakeLists.txt: Include client-web-view.js.

 src/client/application/geary-controller.vala |    5 ++-
 src/client/components/client-web-view.vala   |   30 ++++++++++++++++++++++++-
 ui/CMakeLists.txt                            |    1 +
 ui/client-web-view.js                        |    7 ++++++
 4 files changed, 39 insertions(+), 4 deletions(-)
---
diff --git a/src/client/application/geary-controller.vala b/src/client/application/geary-controller.vala
index 77bbe7e..bb636d0 100644
--- a/src/client/application/geary-controller.vala
+++ b/src/client/application/geary-controller.vala
@@ -208,11 +208,12 @@ public class GearyController : Geary.BaseObject {
                 );
             });
 
-        // Load web view stylesheets
+        // Load web view resources
         try {
+            ClientWebView.load_scripts(this.application);
             ConversationWebView.load_stylehseets(this.application);
         } catch (Error err) {
-            error("Error loading application CSS: %s", err.message);
+            error("Error loading web resources: %s", err.message);
         }
 
         // Use a global avatar session because a cache must be used
diff --git a/src/client/components/client-web-view.vala b/src/client/components/client-web-view.vala
index b1fd0f3..99911ae 100644
--- a/src/client/components/client-web-view.vala
+++ b/src/client/components/client-web-view.vala
@@ -1,5 +1,6 @@
 /*
  * Copyright 2016 Software Freedom Conservancy Inc.
+ * Copyright 2016 Michael Gratton <mike vee net>
  *
  * This software is licensed under the GNU Lesser General Public License
  * (version 2.1 or later). See the COPYING file in this distribution.
@@ -13,6 +14,14 @@ public class ClientWebView : WebKit.WebView {
     private const double ZOOM_DEFAULT = 1.0;
     private const double ZOOM_FACTOR = 0.1;
 
+    private static WebKit.UserScript? script = null;
+
+    public static void load_scripts(GearyApplication app)
+        throws Error {
+        ClientWebView.script = load_app_script(app, "client-web-view.js");
+    }
+
+    /** Loads an application-specific WebKit stylesheet. */
     protected static WebKit.UserStyleSheet load_app_stylesheet(GearyApplication app,
                                                                string name)
         throws Error {
@@ -25,6 +34,7 @@ public class ClientWebView : WebKit.WebView {
         );
     }
 
+    /** Loads a user stylesheet, if any. */
     protected static WebKit.UserStyleSheet? load_user_stylesheet(GearyApplication app,
                                                                  string name) {
         File stylesheet = app.get_user_config_directory().get_child(name);
@@ -47,6 +57,19 @@ public class ClientWebView : WebKit.WebView {
         return user_stylesheet;
     }
 
+    /** Loads an application-specific WebKit JavaScript script. */
+    protected static WebKit.UserScript load_app_script(GearyApplication app,
+                                                       string name)
+        throws Error {
+        return new WebKit.UserScript(
+            app.read_resource(name),
+            WebKit.UserContentInjectedFrames.TOP_FRAME,
+            WebKit.UserScriptInjectionTime.END,
+            null,
+            null
+        );
+    }
+
     private static inline uint to_wk2_font_size(Pango.FontDescription font) {
         Gdk.Screen? screen = Gdk.Screen.get_default();
         double dpi = screen != null ? screen.get_resolution() : 96.0;
@@ -102,7 +125,7 @@ public class ClientWebView : WebKit.WebView {
     public signal void inline_resource_loaded(string cid);
 
 
-    public ClientWebView(WebKit.UserContentManager? content_manager = null) {
+    public ClientWebView(WebKit.UserContentManager? custom_manager = null) {
         WebKit.Settings setts = new WebKit.Settings();
         setts.allow_modal_dialogs = false;
         setts.default_charset = "UTF-8";
@@ -118,6 +141,10 @@ public class ClientWebView : WebKit.WebView {
         setts.enable_plugins = false;
         setts.javascript_can_access_clipboard = true;
 
+        WebKit.UserContentManager content_manager =
+             custom_manager ?? new WebKit.UserContentManager();
+        content_manager.add_script(ClientWebView.script);
+        
         Object(user_content_manager: content_manager, settings: setts);
 
         // XXX get the allow prefix from the extension somehow
@@ -253,4 +280,3 @@ public class ClientWebView : WebKit.WebView {
     }
 
 }
-
diff --git a/ui/CMakeLists.txt b/ui/CMakeLists.txt
index e2ac935..a435559 100644
--- a/ui/CMakeLists.txt
+++ b/ui/CMakeLists.txt
@@ -5,6 +5,7 @@ set(RESOURCE_LIST
   STRIPBLANKS "account_list.glade"
   STRIPBLANKS "account_spinner.glade"
   STRIPBLANKS "certificate_warning_dialog.glade"
+              "client-web-view.js"
   STRIPBLANKS "composer-headerbar.ui"
   STRIPBLANKS "composer-menus.ui"
   STRIPBLANKS "composer-widget.ui"
diff --git a/ui/client-web-view.js b/ui/client-web-view.js
new file mode 100644
index 0000000..e417aeb
--- /dev/null
+++ b/ui/client-web-view.js
@@ -0,0 +1,7 @@
+/*
+ * Copyright 2016 Michael Gratton <mike vee net>
+ *
+ * This software is licensed under the GNU Lesser General Public License
+ * (version 2.1 or later). See the COPYING file in this distribution.
+ */
+


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