Jan Synacek 1116d1
commit d781662873f228b110a128f7a2b6583a4d5e0a3a
Jan Synacek 1116d1
Author: Ricardo Wurmus <rekado@elephly.net>
Jan Synacek 1116d1
Date:   Tue Oct 25 23:00:35 2016 -0700
Jan Synacek 1116d1
Jan Synacek 1116d1
    xwidget: Use WebKit2 API
Jan Synacek 1116d1
    
Jan Synacek 1116d1
    * configure.ac: Check for webkit2gtk-4.0.
Jan Synacek 1116d1
    * src/xwidget.c: Adjust to use WebKit2 API.
Jan Synacek 1116d1
    * lisp/xwidget.el (xwidget-webkit-callback): Adjust matches for
Jan Synacek 1116d1
    `xwidget-event-type'.
Jan Synacek 1116d1
Jan Synacek 1116d1
diff --git a/configure.ac b/configure.ac
Jan Synacek 1116d1
index 998ff52..46fd434 100644
Jan Synacek 1116d1
--- a/configure.ac
Jan Synacek 1116d1
+++ b/configure.ac
Jan Synacek 1116d1
@@ -2630,8 +2630,8 @@ if test "$with_xwidgets" != "no"; then
Jan Synacek 1116d1
   test "$USE_GTK_TOOLKIT" = "GTK3" && test "$window_system" != "none" ||
Jan Synacek 1116d1
     AC_MSG_ERROR([xwidgets requested but gtk3 not used.])
Jan Synacek 1116d1
 
Jan Synacek 1116d1
-  WEBKIT_REQUIRED=1.4.0
Jan Synacek 1116d1
-  WEBKIT_MODULES="webkitgtk-3.0 >= $WEBKIT_REQUIRED"
Jan Synacek 1116d1
+  WEBKIT_REQUIRED=2.12
Jan Synacek 1116d1
+  WEBKIT_MODULES="webkit2gtk-4.0 >= $WEBKIT_REQUIRED"
Jan Synacek 1116d1
   EMACS_CHECK_MODULES([WEBKIT], [$WEBKIT_MODULES])
Jan Synacek 1116d1
   HAVE_XWIDGETS=$HAVE_WEBKIT
Jan Synacek 1116d1
   test $HAVE_XWIDGETS = yes ||
Jan Synacek 1116d1
diff --git a/lisp/xwidget.el b/lisp/xwidget.el
Jan Synacek 1116d1
index 7a0ca8b..1bae6bb 100644
Jan Synacek 1116d1
--- a/lisp/xwidget.el
Jan Synacek 1116d1
+++ b/lisp/xwidget.el
Jan Synacek 1116d1
@@ -187,7 +187,7 @@ XWIDGET instance, XWIDGET-EVENT-TYPE depends on the originating xwidget."
Jan Synacek 1116d1
        "error: callback called for xwidget with dead buffer")
Jan Synacek 1116d1
     (with-current-buffer (xwidget-buffer xwidget)
Jan Synacek 1116d1
       (let* ((strarg  (nth 3 last-input-event)))
Jan Synacek 1116d1
-        (cond ((eq xwidget-event-type 'document-load-finished)
Jan Synacek 1116d1
+        (cond ((eq xwidget-event-type 'load-changed)
Jan Synacek 1116d1
                (xwidget-log "webkit finished loading: '%s'"
Jan Synacek 1116d1
                             (xwidget-webkit-get-title xwidget))
Jan Synacek 1116d1
                ;;TODO - check the native/internal scroll
Jan Synacek 1116d1
@@ -196,8 +196,7 @@ XWIDGET instance, XWIDGET-EVENT-TYPE depends on the originating xwidget."
Jan Synacek 1116d1
                (rename-buffer (format "*xwidget webkit: %s *"
Jan Synacek 1116d1
                                       (xwidget-webkit-get-title xwidget)))
Jan Synacek 1116d1
                (pop-to-buffer (current-buffer)))
Jan Synacek 1116d1
-              ((eq xwidget-event-type
Jan Synacek 1116d1
-                   'navigation-policy-decision-requested)
Jan Synacek 1116d1
+              ((eq xwidget-event-type 'decide-policy)
Jan Synacek 1116d1
                (if (string-match ".*#\\(.*\\)" strarg)
Jan Synacek 1116d1
                    (xwidget-webkit-show-id-or-named-element
Jan Synacek 1116d1
                     xwidget
Jan Synacek 1116d1
diff --git a/src/xwidget.c b/src/xwidget.c
Jan Synacek 1116d1
index f5f4da0..78349a8 100644
Jan Synacek 1116d1
--- a/src/xwidget.c
Jan Synacek 1116d1
+++ b/src/xwidget.c
Jan Synacek 1116d1
@@ -98,13 +98,7 @@ along with GNU Emacs.  If not, see 
Jan Synacek 1116d1
 
Jan Synacek 1116d1
 #include <wchar.h>
Jan Synacek 1116d1
 
Jan Synacek 1116d1
-#include <webkit/webkitwebview.h>
Jan Synacek 1116d1
-#include <webkit/webkitwebplugindatabase.h>
Jan Synacek 1116d1
-#include <webkit/webkitwebplugin.h>
Jan Synacek 1116d1
-#include <webkit/webkitglobals.h>
Jan Synacek 1116d1
-#include <webkit/webkitwebnavigationaction.h>
Jan Synacek 1116d1
-#include <webkit/webkitdownload.h>
Jan Synacek 1116d1
-#include <webkit/webkitwebpolicydecision.h>
Jan Synacek 1116d1
+#include <webkit2/webkit2.h>
Jan Synacek 1116d1
 
Jan Synacek 1116d1
 static struct xwidget *
Jan Synacek 1116d1
 allocate_xwidget (void)
Jan Synacek 1116d1
@@ -50,34 +47,16 @@ allocate_xwidget_view (void)
Jan Synacek 1116d1
 
Jan Synacek 1116d1
 static struct xwidget_view *xwidget_view_lookup (struct xwidget *,
Jan Synacek 1116d1
 						 struct window *);
Jan Synacek 1116d1
-static void webkit_document_load_finished_cb (WebKitWebView *, WebKitWebFrame *,
Jan Synacek 1116d1
-					      gpointer);
Jan Synacek 1116d1
-static gboolean webkit_download_cb (WebKitWebView *, WebKitDownload *, gpointer);
Jan Synacek 1116d1
+static void webkit_view_load_changed_cb (WebKitWebView *,
Jan Synacek 1116d1
+                                         WebKitLoadEvent,
Jan Synacek 1116d1
+                                         gpointer);
Jan Synacek 1116d1
+static gboolean webkit_download_cb (WebKitWebContext *, WebKitDownload *, gpointer);
Jan Synacek 1116d1
 
Jan Synacek 1116d1
 static gboolean
Jan Synacek 1116d1
-webkit_mime_type_policy_typedecision_requested_cb (WebKitWebView *,
Jan Synacek 1116d1
-                                                   WebKitWebFrame *,
Jan Synacek 1116d1
-                                                   WebKitNetworkRequest *,
Jan Synacek 1116d1
-                                                   gchar *,
Jan Synacek 1116d1
-                                                   WebKitWebPolicyDecision *,
Jan Synacek 1116d1
-                                                   gpointer);
Jan Synacek 1116d1
-
Jan Synacek 1116d1
-static gboolean
Jan Synacek 1116d1
-webkit_new_window_policy_decision_requested_cb (WebKitWebView *,
Jan Synacek 1116d1
-                                                WebKitWebFrame *,
Jan Synacek 1116d1
-                                                WebKitNetworkRequest *,
Jan Synacek 1116d1
-                                                WebKitWebNavigationAction *,
Jan Synacek 1116d1
-                                                WebKitWebPolicyDecision *,
Jan Synacek 1116d1
-                                                gpointer);
Jan Synacek 1116d1
-
Jan Synacek 1116d1
-static gboolean
Jan Synacek 1116d1
-webkit_navigation_policy_decision_requested_cb (WebKitWebView *,
Jan Synacek 1116d1
-                                                WebKitWebFrame *,
Jan Synacek 1116d1
-                                                WebKitNetworkRequest *,
Jan Synacek 1116d1
-                                                WebKitWebNavigationAction *,
Jan Synacek 1116d1
-                                                WebKitWebPolicyDecision *,
Jan Synacek 1116d1
-                                                gpointer);
Jan Synacek 1116d1
-
Jan Synacek 1116d1
+webkit_decide_policy_cb (WebKitWebView *,
Jan Synacek 1116d1
+                         WebKitPolicyDecision *,
Jan Synacek 1116d1
+                         WebKitPolicyDecisionType,
Jan Synacek 1116d1
+                         gpointer);
Jan Synacek 1116d1
 
Jan Synacek 1116d1
 
Jan Synacek 1116d1
 DEFUN ("make-xwidget",
Jan Synacek 1116d1
@@ -168,29 +147,17 @@ Returns the newly constructed xwidget, or nil if construction fails.  */)
Jan Synacek 1116d1
       if (EQ (xw->type, Qwebkit))
Jan Synacek 1116d1
         {
Jan Synacek 1116d1
           g_signal_connect (G_OBJECT (xw->widget_osr),
Jan Synacek 1116d1
-                            "document-load-finished",
Jan Synacek 1116d1
-                            G_CALLBACK (webkit_document_load_finished_cb), xw);
Jan Synacek 1116d1
+                            "load-changed",
Jan Synacek 1116d1
+                            G_CALLBACK (webkit_view_load_changed_cb), xw);
Jan Synacek 1116d1
 
Jan Synacek 1116d1
-          g_signal_connect (G_OBJECT (xw->widget_osr),
Jan Synacek 1116d1
-                            "download-requested",
Jan Synacek 1116d1
+          g_signal_connect (G_OBJECT (webkit_web_context_get_default ()),
Jan Synacek 1116d1
+                            "download-started",
Jan Synacek 1116d1
                             G_CALLBACK (webkit_download_cb), xw);
Jan Synacek 1116d1
 
Jan Synacek 1116d1
           g_signal_connect (G_OBJECT (xw->widget_osr),
Jan Synacek 1116d1
-                            "mime-type-policy-decision-requested",
Jan Synacek 1116d1
-                            G_CALLBACK
Jan Synacek 1116d1
-                            (webkit_mime_type_policy_typedecision_requested_cb),
Jan Synacek 1116d1
-                            xw);
Jan Synacek 1116d1
-
Jan Synacek 1116d1
-          g_signal_connect (G_OBJECT (xw->widget_osr),
Jan Synacek 1116d1
-                            "new-window-policy-decision-requested",
Jan Synacek 1116d1
-                            G_CALLBACK
Jan Synacek 1116d1
-                            (webkit_new_window_policy_decision_requested_cb),
Jan Synacek 1116d1
-                            xw);
Jan Synacek 1116d1
-
Jan Synacek 1116d1
-          g_signal_connect (G_OBJECT (xw->widget_osr),
Jan Synacek 1116d1
-                            "navigation-policy-decision-requested",
Jan Synacek 1116d1
+                            "decide-policy",
Jan Synacek 1116d1
                             G_CALLBACK
Jan Synacek 1116d1
-                            (webkit_navigation_policy_decision_requested_cb),
Jan Synacek 1116d1
+                            (webkit_decide_policy_cb),
Jan Synacek 1116d1
                             xw);
Jan Synacek 1116d1
         }
Jan Synacek 1116d1
 
Jan Synacek 1116d1
@@ -284,81 +251,83 @@ store_xwidget_event_string (struct xwidget *xw, const char *eventname,
Jan Synacek 1116d1
   kbd_buffer_store_event (&event);
Jan Synacek 1116d1
 }
Jan Synacek 1116d1
 
Jan Synacek 1116d1
-/* TODO deprecated, use load-status.  */
Jan Synacek 1116d1
 void
Jan Synacek 1116d1
-webkit_document_load_finished_cb (WebKitWebView *webkitwebview,
Jan Synacek 1116d1
-                                  WebKitWebFrame *arg1,
Jan Synacek 1116d1
-                                  gpointer data)
Jan Synacek 1116d1
+webkit_view_load_changed_cb (WebKitWebView *webkitwebview,
Jan Synacek 1116d1
+                             WebKitLoadEvent load_event,
Jan Synacek 1116d1
+                             gpointer data)
Jan Synacek 1116d1
 {
Jan Synacek 1116d1
-  struct xwidget *xw = g_object_get_data (G_OBJECT (webkitwebview),
Jan Synacek 1116d1
-                                          XG_XWIDGET);
Jan Synacek 1116d1
-
Jan Synacek 1116d1
-  store_xwidget_event_string (xw, "document-load-finished", "");
Jan Synacek 1116d1
+  switch (load_event) {
Jan Synacek 1116d1
+  case WEBKIT_LOAD_FINISHED:
Jan Synacek 1116d1
+    {
Jan Synacek 1116d1
+      struct xwidget *xw = g_object_get_data (G_OBJECT (webkitwebview),
Jan Synacek 1116d1
+                                              XG_XWIDGET);
Jan Synacek 1116d1
+      store_xwidget_event_string (xw, "load-changed", "");
Jan Synacek 1116d1
+      break;
Jan Synacek 1116d1
+    }
Jan Synacek 1116d1
+  default:
Jan Synacek 1116d1
+    break;
Jan Synacek 1116d1
+  }
Jan Synacek 1116d1
 }
Jan Synacek 1116d1
 
Jan Synacek 1116d1
 gboolean
Jan Synacek 1116d1
-webkit_download_cb (WebKitWebView *webkitwebview,
Jan Synacek 1116d1
+webkit_download_cb (WebKitWebContext *webkitwebcontext,
Jan Synacek 1116d1
                     WebKitDownload *arg1,
Jan Synacek 1116d1
                     gpointer data)
Jan Synacek 1116d1
 {
Jan Synacek 1116d1
-  struct xwidget *xw = g_object_get_data (G_OBJECT (webkitwebview),
Jan Synacek 1116d1
+  WebKitWebView *view = webkit_download_get_web_view(arg1);
Jan Synacek 1116d1
+  WebKitURIRequest *request = webkit_download_get_request(arg1);
Jan Synacek 1116d1
+  struct xwidget *xw = g_object_get_data (G_OBJECT (view),
Jan Synacek 1116d1
                                           XG_XWIDGET);
Jan Synacek 1116d1
-  store_xwidget_event_string (xw, "download-requested",
Jan Synacek 1116d1
-                              webkit_download_get_uri (arg1));
Jan Synacek 1116d1
+
Jan Synacek 1116d1
+  store_xwidget_event_string (xw, "download-started",
Jan Synacek 1116d1
+                              webkit_uri_request_get_uri(request));
Jan Synacek 1116d1
   return FALSE;
Jan Synacek 1116d1
 }
Jan Synacek 1116d1
 
Jan Synacek 1116d1
 static gboolean
Jan Synacek 1116d1
-webkit_mime_type_policy_typedecision_requested_cb (WebKitWebView *webView,
Jan Synacek 1116d1
-						   WebKitWebFrame *frame,
Jan Synacek 1116d1
-						   WebKitNetworkRequest *request,
Jan Synacek 1116d1
-						   gchar *mimetype,
Jan Synacek 1116d1
-						   WebKitWebPolicyDecision *policy_decision,
Jan Synacek 1116d1
-						   gpointer user_data)
Jan Synacek 1116d1
+webkit_decide_policy_cb (WebKitWebView *webView,
Jan Synacek 1116d1
+                         WebKitPolicyDecision *decision,
Jan Synacek 1116d1
+                         WebKitPolicyDecisionType type,
Jan Synacek 1116d1
+                         gpointer user_data)
Jan Synacek 1116d1
 {
Jan Synacek 1116d1
-  /* This function makes webkit send a download signal for all unknown
Jan Synacek 1116d1
-     mime types.  TODO: Defer the decision to Lisp, so that it's
Jan Synacek 1116d1
-     possible to make Emacs handle mime text for instance.  */
Jan Synacek 1116d1
-  if (!webkit_web_view_can_show_mime_type (webView, mimetype))
Jan Synacek 1116d1
+  switch (type) {
Jan Synacek 1116d1
+  case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
Jan Synacek 1116d1
+    /* This function makes webkit send a download signal for all unknown
Jan Synacek 1116d1
+       mime types.  TODO: Defer the decision to Lisp, so that it's
Jan Synacek 1116d1
+       possible to make Emacs handle mime text for instance.  */
Jan Synacek 1116d1
     {
Jan Synacek 1116d1
-      webkit_web_policy_decision_download (policy_decision);
Jan Synacek 1116d1
-      return TRUE;
Jan Synacek 1116d1
+      WebKitResponsePolicyDecision *response =
Jan Synacek 1116d1
+        WEBKIT_RESPONSE_POLICY_DECISION (decision);
Jan Synacek 1116d1
+      if (!webkit_response_policy_decision_is_mime_type_supported (response))
Jan Synacek 1116d1
+        {
Jan Synacek 1116d1
+          webkit_policy_decision_download (decision);
Jan Synacek 1116d1
+          return TRUE;
Jan Synacek 1116d1
+        }
Jan Synacek 1116d1
+      else
Jan Synacek 1116d1
+        return FALSE;
Jan Synacek 1116d1
+      break;
Jan Synacek 1116d1
     }
Jan Synacek 1116d1
-  else
Jan Synacek 1116d1
+  case WEBKIT_POLICY_DECISION_TYPE_NEW_WINDOW_ACTION:
Jan Synacek 1116d1
+  case WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION:
Jan Synacek 1116d1
+    {
Jan Synacek 1116d1
+      WebKitNavigationPolicyDecision *navigation_decision =
Jan Synacek 1116d1
+        WEBKIT_NAVIGATION_POLICY_DECISION (decision);
Jan Synacek 1116d1
+      WebKitNavigationAction *navigation_action =
Jan Synacek 1116d1
+        webkit_navigation_policy_decision_get_navigation_action (navigation_decision);
Jan Synacek 1116d1
+      WebKitURIRequest *request =
Jan Synacek 1116d1
+        webkit_navigation_action_get_request (navigation_action);
Jan Synacek 1116d1
+
Jan Synacek 1116d1
+      struct xwidget *xw = g_object_get_data (G_OBJECT (webView), XG_XWIDGET);
Jan Synacek 1116d1
+      store_xwidget_event_string (xw, "decide-policy",
Jan Synacek 1116d1
+                                  webkit_uri_request_get_uri (request));
Jan Synacek 1116d1
+      return FALSE;
Jan Synacek 1116d1
+      break;
Jan Synacek 1116d1
+    }
Jan Synacek 1116d1
+  default:
Jan Synacek 1116d1
     return FALSE;
Jan Synacek 1116d1
+  }
Jan Synacek 1116d1
 }
Jan Synacek 1116d1
 
Jan Synacek 1116d1
-static gboolean
Jan Synacek 1116d1
-webkit_new_window_policy_decision_requested_cb (WebKitWebView *webView,
Jan Synacek 1116d1
-						WebKitWebFrame *frame,
Jan Synacek 1116d1
-						WebKitNetworkRequest *request,
Jan Synacek 1116d1
-						WebKitWebNavigationAction *navigation_action,
Jan Synacek 1116d1
-						WebKitWebPolicyDecision *policy_decision,
Jan Synacek 1116d1
-						gpointer user_data)
Jan Synacek 1116d1
-{
Jan Synacek 1116d1
-  struct xwidget *xw = g_object_get_data (G_OBJECT (webView), XG_XWIDGET);
Jan Synacek 1116d1
-  webkit_web_navigation_action_get_original_uri (navigation_action);
Jan Synacek 1116d1
-
Jan Synacek 1116d1
-  store_xwidget_event_string (xw, "new-window-policy-decision-requested",
Jan Synacek 1116d1
-                              webkit_web_navigation_action_get_original_uri
Jan Synacek 1116d1
-                              (navigation_action));
Jan Synacek 1116d1
-  return FALSE;
Jan Synacek 1116d1
-}
Jan Synacek 1116d1
-
Jan Synacek 1116d1
-static gboolean
Jan Synacek 1116d1
-webkit_navigation_policy_decision_requested_cb (WebKitWebView *webView,
Jan Synacek 1116d1
-						WebKitWebFrame *frame,
Jan Synacek 1116d1
-						WebKitNetworkRequest *request,
Jan Synacek 1116d1
-						WebKitWebNavigationAction *navigation_action,
Jan Synacek 1116d1
-						WebKitWebPolicyDecision *policy_decision,
Jan Synacek 1116d1
-						gpointer user_data)
Jan Synacek 1116d1
-{
Jan Synacek 1116d1
-  struct xwidget *xw = g_object_get_data (G_OBJECT (webView), XG_XWIDGET);
Jan Synacek 1116d1
-  store_xwidget_event_string (xw, "navigation-policy-decision-requested",
Jan Synacek 1116d1
-                              webkit_web_navigation_action_get_original_uri
Jan Synacek 1116d1
-                              (navigation_action));
Jan Synacek 1116d1
-  return FALSE;
Jan Synacek 1116d1
-}
Jan Synacek 1116d1
 
Jan Synacek 1116d1
 /* For gtk3 offscreen rendered widgets.  */
Jan Synacek 1116d1
 static gboolean
Jan Synacek 1116d1
@@ -599,8 +568,13 @@ DEFUN ("xwidget-webkit-execute-script",
Jan Synacek 1116d1
 {
Jan Synacek 1116d1
   WEBKIT_FN_INIT ();
Jan Synacek 1116d1
   CHECK_STRING (script);
Jan Synacek 1116d1
-  webkit_web_view_execute_script (WEBKIT_WEB_VIEW (xw->widget_osr),
Jan Synacek 1116d1
-                                  SSDATA (script));
Jan Synacek 1116d1
+  // TODO: provide callback function to do something with the return
Jan Synacek 1116d1
+  // value!  This allows us to get rid of the title hack.
Jan Synacek 1116d1
+  webkit_web_view_run_javascript (WEBKIT_WEB_VIEW (xw->widget_osr),
Jan Synacek 1116d1
+                                  SSDATA (script),
Jan Synacek 1116d1
+                                  NULL, /*cancellable*/
Jan Synacek 1116d1
+                                  NULL, /*callback*/
Jan Synacek 1116d1
+                                  NULL /*user data*/);
Jan Synacek 1116d1
   return Qnil;
Jan Synacek 1116d1
 }
Jan Synacek 1116d1