[geary/wip/728002-webkit2: 12/46] Properly build and load the web extension for ClientWebView.



commit de215fbaf92f0011dc87ff134808054c4ce4a8af
Author: Michael James Gratton <mike vee net>
Date:   Mon Oct 10 17:54:35 2016 +1100

    Properly build and load the web extension for ClientWebView.
    
    * src/CMakeLists.txt: Pass the build dir through to the application so it
      can work out where to find the extension when running from the source
      tree. Build geary-static as reloacatable so we can link it with the
      extension. Actually link geary-static into the extension and install it.
    
    * src/client/application/geary-application.vala
      (GearyApplication::get_web_extensions_dir): New method that determines
      where the web extension lib is to be found.
    
    * src/client/application/geary-controller.vala
      (GearyController::open_async): Set the extension dir on the WebContext,
      and pass through logging config to the extension.
    
    * src/client/web-process/web-process-extension.vala: Add a
      GearyWebExtension extension object, create it on init and init logging.

 src/CMakeLists.txt                                |   19 +++++++-----
 src/client/application/geary-application.vala     |   27 ++++++++++++++---
 src/client/application/geary-controller.vala      |    8 +++++
 src/client/web-process/web-process-extension.vala |   32 +++++++++++++++++++-
 4 files changed, 71 insertions(+), 15 deletions(-)
---
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ce94d0f..c989514 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -542,6 +542,7 @@ set(CFLAGS
     ${DEPS_CFLAGS}
     ${DEPS_CFLAGS_OTHER}
     -D_INSTALL_PREFIX=\"${CMAKE_INSTALL_PREFIX}\"
+    -D_BUILD_ROOT_DIR=\"${CMAKE_BINARY_DIR}\"
     -D_SOURCE_ROOT_DIR=\"${CMAKE_SOURCE_DIR}\"
     -D_GSETTINGS_DIR=\"${CMAKE_BINARY_DIR}/gsettings\"
     -DGETTEXT_PACKAGE=\"${GETTEXT_PACKAGE}\"
@@ -608,7 +609,13 @@ OPTIONS
     ${VALAC_OPTIONS}
 )
 
+# Build the library statically so we can it statically, but make it
+# relocatable so we can link it to the web extension dynamic lib.
 add_library(geary-static STATIC ${ENGINE_VALA_C})
+set_property(
+  TARGET geary-static
+  PROPERTY POSITION_INDEPENDENT_CODE TRUE
+)
 add_dependencies(geary-static git-version)
 target_link_libraries(geary-static ${DEPS_LIBRARIES} sqlite3-unicodesn gthread-2.0)
 
@@ -660,15 +667,11 @@ OPTIONS
     ${VALAC_OPTIONS}
 )
 
-add_library(geary-web-process ${WEB_PROCESS_VALA_C})
-target_link_libraries(geary-web-process ${DEPS_LIBRARIES} gthread-2.0)
-add_custom_command(
-    TARGET
-        geary-web-process
-    POST_BUILD
-    COMMAND
-        ${CMAKE_COMMAND} -E copy geary-console ${CMAKE_BINARY_DIR}/
+add_library(geary-web-process MODULE ${WEB_PROCESS_VALA_C})
+target_link_libraries(geary-web-process
+  PRIVATE ${DEPS_LIBRARIES} gthread-2.0 geary-static
 )
+install(TARGETS geary-web-process LIBRARY DESTINATION lib/geary/web-extensions)
 
 # Console app
 #################################################
diff --git a/src/client/application/geary-application.vala b/src/client/application/geary-application.vala
index 2e93c21..e2bbeb7 100644
--- a/src/client/application/geary-application.vala
+++ b/src/client/application/geary-application.vala
@@ -8,6 +8,7 @@
 extern const string _INSTALL_PREFIX;
 extern const string _GSETTINGS_DIR;
 extern const string _SOURCE_ROOT_DIR;
+extern const string _BUILD_ROOT_DIR;
 extern const string GETTEXT_PACKAGE;
 
 /**
@@ -27,7 +28,8 @@ public class GearyApplication : Gtk.Application {
     public const string INSTALL_PREFIX = _INSTALL_PREFIX;
     public const string GSETTINGS_DIR = _GSETTINGS_DIR;
     public const string SOURCE_ROOT_DIR = _SOURCE_ROOT_DIR;
-    
+    public const string BUILD_ROOT_DIR = _BUILD_ROOT_DIR;
+
     public const string[] AUTHORS = {
         "Jim Nelson <jim yorba org>",
         "Eric Gregory <eric yorba org>",
@@ -252,12 +254,27 @@ public class GearyApplication : Gtk.Application {
         else
             return File.new_for_path(SOURCE_ROOT_DIR);
     }
-    
-    // Returns the directory the application is currently executing from.
+
+    /** Returns the directory the application is currently executing from. */
     public File get_exec_dir() {
-        return exec_dir;
+        return this.exec_dir;
     }
-    
+
+    /**
+     * Returns the directory containing the application's WebExtension libs.
+     *
+     * If the application is installed, this will be
+     * `$INSTALL_PREFIX/lib/geary/web-extension`, else it will be
+     */
+    public File get_web_extensions_dir() {
+        File? dir = get_install_dir();
+        if (dir != null)
+            dir = dir.get_child("lib").get_child("geary").get_child("web-extensions");
+        else
+            dir = File.new_for_path(BUILD_ROOT_DIR).get_child("src");
+        return dir;
+    }
+
     public File? get_desktop_file() {
         File? install_dir = get_install_dir();
         File desktop_file = (install_dir != null)
diff --git a/src/client/application/geary-controller.vala b/src/client/application/geary-controller.vala
index 150e4fb..ea2d530 100644
--- a/src/client/application/geary-controller.vala
+++ b/src/client/application/geary-controller.vala
@@ -192,6 +192,14 @@ 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.initialize_web_extensions.connect((context) => {
+                context.set_web_extensions_directory(
+                    this.application.get_web_extensions_dir().get_path()
+                );
+                context.set_web_extensions_initialization_user_data(
+                    new Variant.boolean(Args.log_debug)
+                );
+            });
 
         // Use a global avatar session because a cache must be used
         // per-session, and we don't want to have to load the cache
diff --git a/src/client/web-process/web-process-extension.vala 
b/src/client/web-process/web-process-extension.vala
index b063878..948735e 100644
--- a/src/client/web-process/web-process-extension.vala
+++ b/src/client/web-process/web-process-extension.vala
@@ -5,7 +5,35 @@
  * (version 2.1 or later). See the COPYING file in this distribution.
  */
 
+/**
+ * Initialises GearyWebExtension for WebKit web processes.
+ */
+public void webkit_web_extension_initialize_with_user_data(WebKit.WebExtension extension,
+                                                           Variant data) {
+    bool logging_enabled = data.get_boolean();
+
+    Geary.Logging.init();
+    if (logging_enabled)
+        Geary.Logging.log_to(stdout);
+
+    debug("Initialising...");
+
+    // Ref it so it doesn't get free'ed right away
+    GearyWebExtension instance = new GearyWebExtension(extension);
+    instance.ref();
+}
+
+/**
+ * A WebExtension that manages Geary-specific behaviours in web processes.
+ */
+public class GearyWebExtension : Object {
+
+
+    private WebKit.WebExtension extension;
+
+
+    public GearyWebExtension(WebKit.WebExtension extension) {
+        this.extension = extension;
+    }
 
-public static void webkit_web_extension_initialize (WebKit.WebExtension extension) {
-    // noop for now
 }


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