[gnome-online-accounts/wip/wk2: 2/2] Port to WebKit 2



commit a85ec181481c3fa1cd81f830093ee68e37112979
Author: Damián Nohales <damiannohales gmail com>
Date:   Fri Jan 9 15:41:54 2015 -0300

    Port to WebKit 2
    
    https://bugzilla.gnome.org/show_bug.cgi?id=742680

 configure.ac                         |    2 +-
 data/Makefile.am                     |    4 -
 data/goawebview.css                  |    3 -
 src/goabackend/Makefile.am           |   73 +++++++++----
 src/goabackend/goaoauth2provider.c   |   64 +++++++++---
 src/goabackend/goaoauth2provider.h   |    3 +-
 src/goabackend/goaoauthprovider.c    |   78 +++++++++++---
 src/goabackend/goaoauthprovider.h    |    7 +-
 src/goabackend/goawebextension.c     |  100 +++++++++++++++++
 src/goabackend/goawebextension.h     |   43 ++++++++
 src/goabackend/goawebextensionmain.c |   51 +++++++++
 src/goabackend/goawebview.c          |  194 ++++++++--------------------------
 12 files changed, 407 insertions(+), 215 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 807e253..cda6916 100644
--- a/configure.ac
+++ b/configure.ac
@@ -90,7 +90,7 @@ PKG_CHECK_MODULES(GTK, [gtk+-3.0 >= 3.11.1])
 AC_SUBST(GTK_CFLAGS)
 AC_SUBST(GTK_LIBS)
 
-PKG_CHECK_MODULES(WEBKIT_GTK, [webkitgtk-3.0 >= 2.1.90])
+PKG_CHECK_MODULES(WEBKIT_GTK, [webkit2gtk-4.0 >= 2.1.90])
 AC_SUBST(WEBKIT_GTK_CFLAGS)
 AC_SUBST(WEBKIT_GTK_LIBS)
 
diff --git a/data/Makefile.am b/data/Makefile.am
index cb30eb8..d5513ef 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -3,9 +3,6 @@ NULL =
 
 SUBDIRS = icons
 
-cssdir = $(pkgdatadir)
-css_DATA = goawebview.css
-
 gsettings_in_files = org.gnome.online-accounts.gschema.xml.in
 gsettings_SCHEMAS = $(gsettings_in_files:.xml.in=.xml)
 
@@ -20,7 +17,6 @@ $(service_DATA): $(service_in_files) Makefile
        @sed -e "s|\ libexecdir\@|$(libexecdir)|" $< > $@
 
 EXTRA_DIST =                                           \
-       $(css_DATA)                                     \
        $(gsettings_in_files)                           \
        $(service_in_files)                             \
        dbus-interfaces.xml                             \
diff --git a/src/goabackend/Makefile.am b/src/goabackend/Makefile.am
index 5ca8e5c..f988cbe 100644
--- a/src/goabackend/Makefile.am
+++ b/src/goabackend/Makefile.am
@@ -19,6 +19,7 @@ AM_CPPFLAGS =                                                         \
        -DPACKAGE_LOCALSTATE_DIR=\""$(localstatedir)"\"         \
        -DPACKAGE_LOCALE_DIR=\""$(localedir)"\"                 \
        -DPACKAGE_LIB_DIR=\""$(libdir)"\"                       \
+       -DPACKAGE_WEB_EXTENSIONS_DIR=\""$(libdir)/goa-1.0/web-extensions"\" \
        -D_POSIX_PTHREAD_SEMANTICS -D_REENTRANT                 \
        $(WARN_CFLAGS)                                          \
        $(NULL)
@@ -99,29 +100,6 @@ libgoa_backend_1_0_la_SOURCES =                                             \
        $(top_builddir)/src/goaidentity/org.freedesktop.realmd.c        \
        $(NULL)
 
-BUILT_SOURCES =                                                                \
-       $(libgoa_backend_1_0_la_built_sources)                          \
-       $(NULL)
-
-EXTRA_DIST +=                                                          \
-       goadleynaservermanager.xml                                      \
-       goadleynaservermediadevice.xml                                  \
-       $(NULL)
-
-goadleynaservermanager.h goadleynaservermanager.c: goadleynaservermanager.xml
-       $(AM_V_GEN)gdbus-codegen                                                \
-               --c-namespace DleynaServer                                      \
-               --generate-c-code goadleynaservermanager                        \
-               --interface-prefix com.intel.dLeynaServer.                      \
-               $<
-
-goadleynaservermediadevice.h goadleynaservermediadevice.c: goadleynaservermediadevice.xml
-       $(AM_V_GEN)gdbus-codegen                                                                \
-               --c-namespace DleynaServer                                                      \
-               --generate-c-code goadleynaservermediadevice                                    \
-               --interface-prefix com.intel.dLeynaServer.                                      \
-               $<
-
 if BUILD_KERBEROS
 libgoa_backend_1_0_la_SOURCES +=                                       \
        goakerberosprovider.h           goakerberosprovider.c
@@ -162,6 +140,55 @@ libgoa_backend_1_0_la_LDFLAGS =                            \
 
 # ----------------------------------------------------------------------------------------------------
 
+webextension_LTLIBRARIES = libgoawebextension.la
+
+webextensiondir = $(libdir)/goa-1.0/web-extensions
+
+libgoawebextension_la_SOURCES =                                                \
+       goawebextensionmain.c                                           \
+       $(NULL)
+
+libgoawebextension_la_CFLAGS =                                         \
+       $(WEBKIT_GTK_CFLAGS)                                            \
+       $(NULL)
+
+libgoawebextension_la_LIBADD =                                         \
+       $(WEBKIT_GTK_LIBS)                                              \
+       $(NULL)
+
+libgoawebextension_la_LDFLAGS =                                                \
+       -avoid-version                                                  \
+       -module                                                         \
+       -no-undefined                                                   \
+       $(NULL)
+
+# ----------------------------------------------------------------------------------------------------
+
+BUILT_SOURCES =                                                                \
+       $(libgoa_backend_1_0_la_built_sources)                          \
+       $(NULL)
+
+EXTRA_DIST +=                                                          \
+       goadleynaservermanager.xml                                      \
+       goadleynaservermediadevice.xml                                  \
+       $(NULL)
+
+goadleynaservermanager.h goadleynaservermanager.c: goadleynaservermanager.xml
+       $(AM_V_GEN)gdbus-codegen                                                \
+               --c-namespace DleynaServer                                      \
+               --generate-c-code goadleynaservermanager                        \
+               --interface-prefix com.intel.dLeynaServer.                      \
+               $<
+
+goadleynaservermediadevice.h goadleynaservermediadevice.c: goadleynaservermediadevice.xml
+       $(AM_V_GEN)gdbus-codegen                                                                \
+               --c-namespace DleynaServer                                                      \
+               --generate-c-code goadleynaservermediadevice                                    \
+               --interface-prefix com.intel.dLeynaServer.                                      \
+               $<
+
+# ----------------------------------------------------------------------------------------------------
+
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = goa-backend-1.0.pc
 
diff --git a/src/goabackend/goaoauth2provider.c b/src/goabackend/goaoauth2provider.c
index 594c173..688ba33 100644
--- a/src/goabackend/goaoauth2provider.c
+++ b/src/goabackend/goaoauth2provider.c
@@ -77,6 +77,8 @@ struct _GoaOAuth2ProviderPrivate
   gchar *identity;
   gchar *presentation_identity;
   gchar *password;
+
+  gboolean load_failed;
 };
 
 #define GOA_OAUTH2_PROVIDER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOA_TYPE_OAUTH2_PROVIDER, 
GoaOAuth2ProviderPrivate))
@@ -793,16 +795,33 @@ on_form_submit (WebKitDOMNode *element, WebKitDOMEvent *event, gpointer user_dat
   priv->password_node = NULL;
 }
 
+static gboolean
+on_web_view_load_failed (WebKitWebView  *web_view,
+                         WebKitLoadEvent load_event,
+                         gchar          *failing_uri,
+                         gpointer        error,
+                         gpointer        user_data)
+{
+  GoaOAuth2Provider *provider = user_data;
+  provider->priv->load_failed = TRUE;
+}
+
 static void
-on_web_view_document_load_finished (WebKitWebView *web_view, WebKitWebFrame *frame, gpointer user_data)
+on_web_view_load_chaged (WebKitWebView *web_view, WebKitLoadEvent *load_event, gpointer user_data)
 {
   GoaOAuth2Provider *provider = GOA_OAUTH2_PROVIDER (user_data);
   GoaOAuth2ProviderPrivate *priv = provider->priv;
+
+  if (load_event != WEBKIT_LOAD_FINISHED || priv->load_failed)
+    return;
+
+#if 0 /* TODO: DOM Parsing */
   WebKitDOMDocument *document;
   WebKitDOMNodeList *elements;
   gulong element_count;
   gulong i;
 
+  // Find a way to replace webkit_web_view_get_dom_document
   document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (web_view));
   elements = webkit_dom_document_get_elements_by_tag_name (document, "*");
   element_count = webkit_dom_node_list_get_length (elements);
@@ -845,20 +864,21 @@ on_web_view_document_load_finished (WebKitWebView *web_view, WebKitWebFrame *fra
             }
         }
     }
+#endif
 }
 
 static gboolean
-on_web_view_navigation_policy_decision_requested (WebKitWebView             *webView,
-                                                  WebKitWebFrame            *frame,
-                                                  WebKitNetworkRequest      *request,
-                                                  WebKitWebNavigationAction *navigation_action,
-                                                  WebKitWebPolicyDecision   *policy_decision,
-                                                  gpointer                   user_data)
+on_web_view_decide_policy (WebKitWebView           *web_view,
+                           WebKitPolicyDecision    *decision,
+                           WebKitPolicyDecisionType decision_type,
+                           gpointer                 user_data)
 {
   GoaOAuth2Provider *provider = GOA_OAUTH2_PROVIDER (user_data);
   GoaOAuth2ProviderPrivate *priv = provider->priv;
   GHashTable *key_value_pairs;
-  SoupMessage *message;
+  WebKitNavigationPolicyDecision *navigation_decision;
+  WebKitNavigationAction *action;
+  WebKitURIRequest *request;
   SoupURI *uri;
   const gchar *fragment;
   const gchar *oauth2_error;
@@ -867,15 +887,20 @@ on_web_view_navigation_policy_decision_requested (WebKitWebView             *web
   const gchar *requested_uri;
   gint response_id = GTK_RESPONSE_NONE;
 
+  if (decision_type != WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION)
+    goto default_behaviour;
+
   /* TODO: use oauth2_proxy_extract_access_token() */
 
-  requested_uri = webkit_network_request_get_uri (request);
+  navigation_decision = WEBKIT_NAVIGATION_POLICY_DECISION (decision);
+  action = webkit_navigation_policy_decision_get_navigation_action (navigation_decision);
+  request = webkit_navigation_action_get_request (action);
+  requested_uri = webkit_uri_request_get_uri (request);
   redirect_uri = goa_oauth2_provider_get_redirect_uri (provider);
   if (!g_str_has_prefix (requested_uri, redirect_uri))
     goto default_behaviour;
 
-  message = webkit_network_request_get_message (request);
-  uri = soup_message_get_uri (message);
+  uri = soup_uri_new (requested_uri);
   fragment = soup_uri_get_fragment (uri);
   query = soup_uri_get_query (uri);
 
@@ -973,7 +998,7 @@ on_web_view_navigation_policy_decision_requested (WebKitWebView             *web
  ignore_request:
   g_assert (response_id != GTK_RESPONSE_NONE);
   gtk_dialog_response (priv->dialog, response_id);
-  webkit_web_policy_decision_ignore (policy_decision);
+  webkit_policy_decision_ignore (decision);
   return TRUE;
 
  default_behaviour:
@@ -1061,10 +1086,18 @@ get_tokens_and_identity (GoaOAuth2Provider  *provider,
     goa_web_view_fake_mobile (GOA_WEB_VIEW (web_view));
 
   webkit_web_view_load_uri (WEBKIT_WEB_VIEW (embed), url);
-  g_signal_connect (embed, "document-load-finished", G_CALLBACK (on_web_view_document_load_finished), 
provider);
+  provider->priv->load_failed = FALSE;
+  g_signal_connect (embed,
+                    "load-failed",
+                    G_CALLBACK (on_web_view_load_failed),
+                    provider);
+  g_signal_connect (embed,
+                    "load-changed",
+                    G_CALLBACK (on_web_view_load_chaged),
+                    provider);
   g_signal_connect (embed,
-                    "navigation-policy-decision-requested",
-                    G_CALLBACK (on_web_view_navigation_policy_decision_requested),
+                    "decide-policy",
+                    G_CALLBACK (on_web_view_decide_policy),
                     provider);
 
   gtk_container_add (GTK_CONTAINER (grid), web_view);
@@ -1737,6 +1770,7 @@ static void
 goa_oauth2_provider_init (GoaOAuth2Provider *provider)
 {
   provider->priv = GOA_OAUTH2_PROVIDER_GET_PRIVATE (provider);
+  provider->priv->load_failed = FALSE;
 }
 
 static void
diff --git a/src/goabackend/goaoauth2provider.h b/src/goabackend/goaoauth2provider.h
index 65c46cf..6b90d3b 100644
--- a/src/goabackend/goaoauth2provider.h
+++ b/src/goabackend/goaoauth2provider.h
@@ -25,7 +25,8 @@
 
 #include <goabackend/goaprovider.h>
 #include <goabackend/goaprovider-priv.h>
-#include <webkit/webkit.h>
+#include <webkit2/webkit2.h>
+#include <webkitdom/webkitdom.h>
 
 G_BEGIN_DECLS
 
diff --git a/src/goabackend/goaoauthprovider.c b/src/goabackend/goaoauthprovider.c
index 9c95bae..e3c552f 100644
--- a/src/goabackend/goaoauthprovider.c
+++ b/src/goabackend/goaoauthprovider.c
@@ -63,6 +63,13 @@
  * need to be implemented - this type implements these methods.
  */
 
+struct _GoaOAuthProviderPrivate
+{
+  gboolean load_failed;
+};
+
+#define GOA_OAUTH_PROVIDER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GOA_TYPE_OAUTH_PROVIDER, 
GoaOAuthProviderPrivate))
+
 G_LOCK_DEFINE_STATIC (provider_lock);
 
 G_DEFINE_ABSTRACT_TYPE (GoaOAuthProvider, goa_oauth_provider, GOA_TYPE_PROVIDER);
@@ -656,16 +663,33 @@ on_form_submit (WebKitDOMNode *element, WebKitDOMEvent *event, gpointer user_dat
   data->password_node = NULL;
 }
 
+static gboolean
+on_web_view_load_failed (WebKitWebView  *web_view,
+                         WebKitLoadEvent load_event,
+                         gchar          *failing_uri,
+                         gpointer        error,
+                         gpointer        user_data)
+{
+  IdentifyData *data = user_data;
+  data->provider->priv->load_failed = TRUE;
+}
+
 static void
-on_web_view_document_load_finished (WebKitWebView *web_view, WebKitWebFrame *frame, gpointer user_data)
+on_web_view_load_chaged (WebKitWebView *web_view, WebKitLoadEvent *load_event, gpointer user_data)
 {
   IdentifyData *data = user_data;
   GoaOAuthProvider *provider = data->provider;
+
+  if (load_event != WEBKIT_LOAD_FINISHED || provider->priv->load_failed)
+    return;
+
+#if 0 /* TODO: DOM Parsing */
   WebKitDOMDocument *document;
   WebKitDOMNodeList *elements;
   gulong element_count;
   gulong i;
 
+  // Find a way to replace webkit_web_view_get_dom_document
   document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (web_view));
   elements = webkit_dom_document_get_elements_by_tag_name (document, "*");
   element_count = webkit_dom_node_list_get_length (elements);
@@ -708,32 +732,38 @@ on_web_view_document_load_finished (WebKitWebView *web_view, WebKitWebFrame *fra
             }
         }
     }
+#endif
 }
 
 static gboolean
-on_web_view_navigation_policy_decision_requested (WebKitWebView             *webView,
-                                                  WebKitWebFrame            *frame,
-                                                  WebKitNetworkRequest      *request,
-                                                  WebKitWebNavigationAction *navigation_action,
-                                                  WebKitWebPolicyDecision   *policy_decision,
-                                                  gpointer                   user_data)
+on_web_view_decide_policy (WebKitWebView           *web_view,
+                           WebKitPolicyDecision    *decision,
+                           WebKitPolicyDecisionType decision_type,
+                           gpointer                 user_data)
 {
   IdentifyData *data = user_data;
+  WebKitNavigationPolicyDecision *navigation_decision;
+  WebKitNavigationAction *action;
+  WebKitURIRequest *request;
   const gchar *redirect_uri;
   const gchar *requested_uri;
 
+  if (decision_type != WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION)
+    return FALSE;
+
   /* TODO: use oauth_proxy_extract_access_token() */
 
-  requested_uri = webkit_network_request_get_uri (request);
+  navigation_decision = WEBKIT_NAVIGATION_POLICY_DECISION (decision);
+  action = webkit_navigation_policy_decision_get_navigation_action (navigation_decision);
+  request = webkit_navigation_action_get_request (action);
+  requested_uri = webkit_uri_request_get_uri (request);
   redirect_uri = goa_oauth_provider_get_callback_uri (data->provider);
   if (g_str_has_prefix (requested_uri, redirect_uri))
     {
-      SoupMessage *message;
       SoupURI *uri;
       GHashTable *key_value_pairs;
 
-      message = webkit_network_request_get_message (request);
-      uri = soup_message_get_uri (message);
+      uri = soup_uri_new (requested_uri);
       key_value_pairs = soup_form_decode (uri->query);
 
       /* TODO: error handling? */
@@ -743,12 +773,15 @@ on_web_view_navigation_policy_decision_requested (WebKitWebView             *web
           gtk_dialog_response (data->dialog, GTK_RESPONSE_OK);
         }
       g_hash_table_unref (key_value_pairs);
-      webkit_web_policy_decision_ignore (policy_decision);
-      return TRUE; /* ignore the request */
+
+       /* ignore the request */
+      webkit_policy_decision_ignore (decision);
+      return TRUE;
     }
   else
     {
-      return FALSE; /* make default behavior apply */
+      /* make default behavior apply */
+      return FALSE;
     }
 }
 
@@ -895,10 +928,18 @@ get_tokens_and_identity (GoaOAuthProvider *provider,
     goa_web_view_fake_mobile (GOA_WEB_VIEW (web_view));
 
   webkit_web_view_load_uri (WEBKIT_WEB_VIEW (embed), url);
-  g_signal_connect (embed, "document-load-finished", G_CALLBACK (on_web_view_document_load_finished), &data);
+  provider->priv->load_failed = FALSE;
+  g_signal_connect (embed,
+                    "load-failed",
+                    G_CALLBACK (on_web_view_load_failed),
+                    &data);
+  g_signal_connect (embed,
+                    "load-changed",
+                    G_CALLBACK (on_web_view_load_chaged),
+                    &data);
   g_signal_connect (embed,
-                    "navigation-policy-decision-requested",
-                    G_CALLBACK (on_web_view_navigation_policy_decision_requested),
+                    "decide-policy",
+                    G_CALLBACK (on_web_view_decide_policy),
                     &data);
 
   gtk_container_add (GTK_CONTAINER (grid), web_view);
@@ -1677,8 +1718,9 @@ goa_oauth_provider_ensure_credentials_sync (GoaProvider    *_provider,
 /* ---------------------------------------------------------------------------------------------------- */
 
 static void
-goa_oauth_provider_init (GoaOAuthProvider *client)
+goa_oauth_provider_init (GoaOAuthProvider *provider)
 {
+  provider->priv = GOA_OAUTH_PROVIDER_GET_PRIVATE (provider);
 }
 
 static void
diff --git a/src/goabackend/goaoauthprovider.h b/src/goabackend/goaoauthprovider.h
index f39d2b1..9fedb03 100644
--- a/src/goabackend/goaoauthprovider.h
+++ b/src/goabackend/goaoauthprovider.h
@@ -26,7 +26,8 @@
 #include <goabackend/goaprovider.h>
 #include <goabackend/goaprovider-priv.h>
 #include <rest/rest-proxy-call.h>
-#include <webkit/webkit.h>
+#include <webkit2/webkit2.h>
+#include <webkitdom/webkitdom.h>
 
 G_BEGIN_DECLS
 
@@ -120,8 +121,10 @@ struct _GoaOAuthProviderClass
                                                 WebKitDOMHTMLInputElement    *element);
 
   /*< private >*/
+  GoaOAuthProviderPrivate *priv;
+
   /* Padding for future expansion */
-  gpointer goa_reserved[29];
+  gpointer goa_reserved[28];
 };
 
 GType        goa_oauth_provider_get_type                     (void) G_GNUC_CONST;
diff --git a/src/goabackend/goawebextension.c b/src/goabackend/goawebextension.c
new file mode 100644
index 0000000..b60b550
--- /dev/null
+++ b/src/goabackend/goawebextension.c
@@ -0,0 +1,100 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (C) 2015 Damián Nohales
+ * Copyright (C) 2015 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "goawebextension.h"
+
+struct _GoaWebExtension
+{
+  GObject parent;
+  WebKitWebExtension *wk_extension;
+  gboolean initialized;
+};
+
+struct _GoaWebExtensionClass
+{
+  GObjectClass parent;
+};
+
+enum
+{
+  PROP_0,
+  PROP_WK_EXTENSION
+};
+
+G_DEFINE_TYPE (GoaWebExtension, goa_web_extension, G_TYPE_OBJECT)
+
+static void
+goa_web_extension_dispose (GObject *object)
+{
+  GoaWebExtension *self = GOA_WEB_EXTENSION (object);
+
+  g_clear_object (&self->wk_extension);
+
+  G_OBJECT_CLASS (goa_web_extension_parent_class)->dispose (object);
+}
+
+static void
+goa_web_extension_set_property (GObject      *object,
+                                guint         prop_id,
+                                const GValue *value,
+                                GParamSpec   *pspec)
+{
+  GoaWebExtension *self = GOA_WEB_EXTENSION (object);
+
+  switch (prop_id)
+    {
+    case PROP_WK_EXTENSION:
+      self->wk_extension = g_value_dup_object (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+static void
+goa_web_extension_class_init (GoaWebExtensionClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->dispose = goa_web_extension_dispose;
+  object_class->set_property = goa_web_extension_set_property;
+
+  g_object_class_install_property (gobject_class,
+                                   PROP_WK_EXTENSION,
+                                   g_param_spec_object ("wk-extension",
+                                                        "A WebKitWebExtension",
+                                                        "The associated WebKitWebExtension",
+                                                        WEBKIT_TYPE_WEB_EXTENSION,
+                                                        G_PARAM_WRITABLE |
+                                                        G_PARAM_CONSTRUCT_ONLY |
+                                                        G_PARAM_STATIC_STRINGS));
+}
+
+static void
+goa_web_extension_init (GoaWebExtension *self)
+{
+}
+
+GoaWebExtension *
+goa_web_extension_new (WebKitWebExtension *wk_extension)
+{
+  return g_object_new (EPHY_TYPE_WEB_EXTENSION, "wk-extension", wk_extension, NULL);
+}
diff --git a/src/goabackend/goawebextension.h b/src/goabackend/goawebextension.h
new file mode 100644
index 0000000..aadc002
--- /dev/null
+++ b/src/goabackend/goawebextension.h
@@ -0,0 +1,43 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (C) 2015 Damián Nohales
+ * Copyright (C) 2015 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GOA_WEB_EXTENSION_H__
+#define __GOA_WEB_EXTENSION_H__
+
+#include <glib-object.h>
+#include <webkit2/webkit-web-extension.h>
+
+G_BEGIN_DECLS
+
+#define GOA_TYPE_WEB_EXTENSION            (goa_web_extension_get_type())
+#define GOA_WEB_EXTENSION(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GOA_TYPE_WEB_EXTENSION, 
GoaWebExtension))
+#define GOA_WEB_EXTENSION_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass),  GOA_TYPE_WEB_EXTENSION, 
GoaWebExtensionClass))
+#define GOA_IS_WEB_EXTENSION(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GOA_TYPE_WEB_EXTENSION))
+#define GOA_IS_WEB_EXTENSION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),  GOA_TYPE_WEB_EXTENSION))
+#define GOA_WEB_EXTENSION_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),  GOA_TYPE_WEB_EXTENSION, 
GoaWebExtensionClass))
+
+typedef struct _GoaWebExtension      GoaWebExtension;
+typedef struct _GoaWebExtensionClass GoaWebExtensionClass;
+
+GType                goa_web_extension_get_type   (void);
+GoaWebExtension     *goa_web_extension_new        (WebKitWebExtension *wk_extension);
+
+G_END_DECLS
+
+#endif /* __GOA_WEB_EXTENSION_H__ */
diff --git a/src/goabackend/goawebextensionmain.c b/src/goabackend/goawebextensionmain.c
new file mode 100644
index 0000000..da03d86
--- /dev/null
+++ b/src/goabackend/goawebextensionmain.c
@@ -0,0 +1,51 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/*
+ * Copyright (C) 2015 Damián Nohales
+ * Copyright (C) 2015 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <webkit2/webkit-web-extension.h>
+
+#include "goawebextension.h"
+
+static GoaWebExtension *the_extension;
+
+static void
+on_name_acquired (GDBusConnection *connection,
+                  const gchar     *name,
+                  gpointer         user_data)
+{
+}
+
+G_MODULE_EXPORT void
+webkit_web_extension_initialize (WebKitWebExtension *wk_extension)
+{
+  the_extension = goa_web_extension_new (wk_extension);
+  g_bus_own_name (G_BUS_TYPE_SESSION,
+                  "org.gnome.OnlineAccounts.WebExtension",
+                  G_BUS_NAME_OWNER_FLAGS_NONE,
+                  NULL,
+                  (GBusNameAcquiredCallback) on_name_acquired,
+                  NULL,
+                  web_extension,
+                  NULL);
+}
+
+static void __attribute__((destructor))
+goa_web_extension_shutdown (void)
+{
+  g_clear_object (&the_extension);
+}
diff --git a/src/goabackend/goawebview.c b/src/goabackend/goawebview.c
index 0c7c75c..fa4d311 100644
--- a/src/goabackend/goawebview.c
+++ b/src/goabackend/goawebview.c
@@ -1,5 +1,6 @@
 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
  *
+ * Copyright (C) 2015 Damián Nohales
  * Copyright (C) 2012, 2013, 2014 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
@@ -24,7 +25,7 @@
 #include <glib.h>
 #include <glib/gi18n-lib.h>
 #include <libsoup/soup.h>
-#include <webkit/webkit.h>
+#include <webkit2/webkit2.h>
 
 #include "goawebview.h"
 #include "nautilus-floating-bar.h"
@@ -34,7 +35,7 @@ struct _GoaWebViewPrivate
   GtkWidget *floating_bar;
   GtkWidget *progress_bar;
   GtkWidget *web_view;
-  gboolean status;
+  SoupCookieJar *cookie_jar;
   gulong clear_notify_progress_id;
   gulong notify_load_status_id;
   gulong notify_progress_id;
@@ -89,58 +90,22 @@ web_view_floating_bar_update (GoaWebView *self, const gchar *text)
     gtk_widget_show (priv->floating_bar);
 }
 
-static gboolean
-web_view_is_loading (GoaWebView *self)
-{
-  GoaWebViewPrivate *priv = self->priv;
-  WebKitLoadStatus status;
-
-  status = webkit_web_view_get_load_status (WEBKIT_WEB_VIEW (priv->web_view));
-
-  if ((priv->status == WEBKIT_LOAD_FINISHED || priv->status == WEBKIT_LOAD_FAILED)
-      && status != WEBKIT_LOAD_PROVISIONAL)
-    return FALSE;
-
-  priv->status = status;
-  return status != WEBKIT_LOAD_FINISHED && status != WEBKIT_LOAD_FAILED;
-}
-
 static void
-web_view_log_printer (SoupLogger         *logger,
-                      SoupLoggerLogLevel  level,
-                      gchar               direction,
-                      const gchar        *data,
-                      gpointer            user_data)
-{
-  gchar *message;
-
-  message = g_strdup_printf ("%c %s", direction, data);
-  g_log_default_handler ("goa", G_LOG_LEVEL_DEBUG, message, NULL);
-  g_free (message);
-}
-
-static void
-web_view_notify_load_status_cb (GObject *object, GParamSpec *pspec, gpointer user_data)
+web_view_load_changed_cb (WebKitWebView  *web_view,
+                          WebKitLoadEvent load_event,
+                          gpointer        user_data)
 {
   GoaWebView *self = GOA_WEB_VIEW (user_data);
-  WebKitWebView *web_view = WEBKIT_WEB_VIEW (object);
-  WebKitLoadStatus status;
 
-  status = webkit_web_view_get_load_status (web_view);
-  switch (status)
+  switch (load_event)
     {
-    case WEBKIT_LOAD_PROVISIONAL:
+    case WEBKIT_LOAD_STARTED:
+    case WEBKIT_LOAD_COMMITTED:
       {
-        WebKitNetworkRequest *request;
-        WebKitWebDataSource *source;
-        WebKitWebFrame *frame;
         const gchar *uri;
         gchar *title;
 
-        frame = webkit_web_view_get_main_frame (web_view);
-        source = webkit_web_frame_get_provisional_data_source (frame);
-        request = webkit_web_data_source_get_initial_request (source);
-        uri = webkit_network_request_get_uri (request);
+        uri = webkit_web_view_get_uri (web_view);
         title = web_view_create_loading_title (uri);
 
         web_view_floating_bar_update (self, title);
@@ -148,7 +113,10 @@ web_view_notify_load_status_cb (GObject *object, GParamSpec *pspec, gpointer use
         break;
       }
 
-    case WEBKIT_LOAD_FAILED:
+    case WEBKIT_LOAD_REDIRECTED:
+      /* TODO: Update the loading uri */
+      break;
+
     case WEBKIT_LOAD_FINISHED:
       web_view_floating_bar_update (self, NULL);
       break;
@@ -159,7 +127,9 @@ web_view_notify_load_status_cb (GObject *object, GParamSpec *pspec, gpointer use
 }
 
 static void
-web_view_notify_progress_cb (GObject *object, GParamSpec *pspec, gpointer user_data)
+web_view_notify_estimated_load_progress_cb (GObject    *object,
+                                            GParamSpec *pspec,
+                                            gpointer    user_data)
 {
   GoaWebView *self = GOA_WEB_VIEW (user_data);
   GoaWebViewPrivate *priv = self->priv;
@@ -178,8 +148,8 @@ web_view_notify_progress_cb (GObject *object, GParamSpec *pspec, gpointer user_d
   if (!uri || g_str_equal (uri, "about:blank"))
     return;
 
-  progress = webkit_web_view_get_progress (WEBKIT_WEB_VIEW (priv->web_view));
-  loading = web_view_is_loading (self);
+  progress = webkit_web_view_get_estimated_load_progress (web_view);
+  loading = webkit_web_view_is_loading (web_view);
 
   if (progress == 1.0 || !loading)
     priv->clear_notify_progress_id = g_timeout_add (500, web_view_clear_notify_progress_cb, self);
@@ -196,6 +166,8 @@ goa_web_view_dispose (GObject *object)
   GoaWebView *self = GOA_WEB_VIEW (object);
   GoaWebViewPrivate *priv = self->priv;
 
+  g_clear_object (&priv->cookie_jar);
+
   if (priv->clear_notify_progress_id != 0)
     {
       g_source_remove (priv->clear_notify_progress_id);
@@ -217,108 +189,40 @@ goa_web_view_dispose (GObject *object)
   G_OBJECT_CLASS (goa_web_view_parent_class)->dispose (object);
 }
 
-#ifdef GOA_INSPECTOR_ENABLED
-static WebKitWebView *
-web_inspector_inspect_web_view_cb (WebKitWebInspector *inspector,
-                                   WebKitWebView      *web_view,
-                                   gpointer            user_data)
-{
-  GtkWidget *inspector_web_view;
-  GtkWidget *scrolled_window;
-  GtkWidget *window;
-
-  inspector_web_view = webkit_web_view_new ();
-
-  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
-  gtk_window_resize (GTK_WINDOW (window), 800, 600);
-
-  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
-  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
-                                  GTK_POLICY_AUTOMATIC,
-                                  GTK_POLICY_AUTOMATIC);
-
-  gtk_container_add (GTK_CONTAINER (window), scrolled_window);
-  gtk_container_add (GTK_CONTAINER (scrolled_window), inspector_web_view);
-
-  g_object_set_data (G_OBJECT (inspector), "window", window);
-
-  return WEBKIT_WEB_VIEW (inspector_web_view);
-}
-
-static gboolean
-web_inspector_show_window_cb (WebKitWebInspector *inspector,
-                              gpointer            user_data)
-{
-  GtkWidget *window;
-  GtkWindowGroup *group;
-
-  group = gtk_window_group_new ();
-
-  window = g_object_get_data (G_OBJECT (inspector), "window");
-  gtk_window_group_add_window (group, GTK_WINDOW (window));
-  gtk_widget_show_all (window);
-  gtk_window_present (GTK_WINDOW (window));
-
-  g_object_unref (group);
-  return GDK_EVENT_STOP;
-}
-#endif /* GOA_INSPECTOR_ENABLED */
-
 static void
 goa_web_view_init (GoaWebView *self)
 {
   GoaWebViewPrivate *priv;
-  GtkWidget *scrolled_window;
-  SoupCookieJar *cookie_jar;
-  SoupLogger *logger;
-  SoupSession *session;
-  WebKitWebSettings *settings;
+  WebKitCookieManager *cookie_manager;
+  WebKitWebContext *context;
+  gchar *jar_file;
 
   self->priv = GOA_WEB_VIEW_GET_PRIVATE (self);
   priv = self->priv;
 
-  session = webkit_get_default_session ();
-  g_object_set (session, SOUP_SESSION_SSL_USE_SYSTEM_CA_FILE, TRUE, SOUP_SESSION_SSL_STRICT, TRUE, NULL);
-
-  soup_session_add_feature_by_type (session, SOUP_TYPE_PROXY_RESOLVER_DEFAULT);
-  g_object_set (session, "accept-language-auto", TRUE, NULL);
-
-  soup_session_remove_feature_by_type (session, SOUP_TYPE_COOKIE_JAR);
-  cookie_jar = soup_cookie_jar_new ();
-  soup_session_add_feature (session, SOUP_SESSION_FEATURE (cookie_jar));
-  g_object_unref (cookie_jar);
-
-  logger = soup_logger_new (SOUP_LOGGER_LOG_BODY, -1);
-  soup_logger_set_printer (logger, web_view_log_printer, NULL, NULL);
-  soup_session_add_feature (session, SOUP_SESSION_FEATURE (logger));
-  g_object_unref (logger);
-
-  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
-  gtk_widget_set_size_request (scrolled_window, 500, 400);
-  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window), GTK_SHADOW_IN);
-  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
-                                  GTK_POLICY_AUTOMATIC,
-                                  GTK_POLICY_AUTOMATIC);
-  gtk_container_add (GTK_CONTAINER (self), scrolled_window);
+  context = webkit_web_context_get_default ();
+  cookie_manager = webkit_web_context_get_cookie_manager (context);
+  jar_file = g_build_filename (g_get_user_cache_dir (), "goa-1.0", "cookies.sqlite", NULL);
+  priv->cookie_jar = soup_cookie_jar_db_new (jar_file, FALSE);
+  webkit_cookie_manager_set_persistent_storage (cookie_manager, jar_file, 
WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE);
+  webkit_cookie_manager_delete_all_cookies (cookie_manager);
+  g_free (jar_file);
 
   priv->web_view = webkit_web_view_new ();
-  priv->status = WEBKIT_LOAD_PROVISIONAL;
-  gtk_container_add (GTK_CONTAINER (scrolled_window), priv->web_view);
-
-  settings = webkit_web_view_get_settings (WEBKIT_WEB_VIEW (priv->web_view));
-  g_object_set (settings, "user-stylesheet-uri", "file://" PACKAGE_DATA_DIR "/goawebview.css", NULL);
+  gtk_widget_set_size_request (priv->web_view, 500, 400);
+  gtk_container_add (GTK_CONTAINER (self), priv->web_view);
 
 #ifdef GOA_INSPECTOR_ENABLED
   {
+    WebKitSettings *settings;
     WebKitWebInspector *inspector;
 
     /* Setup the inspector */
+    settings = webkit_web_view_get_settings (WEBKIT_WEB_VIEW (priv->web_view));
     g_object_set (settings, "enable-developer-extras", TRUE, NULL);
-    inspector = webkit_web_view_get_inspector (WEBKIT_WEB_VIEW (priv->web_view));
-    webkit_web_inspector_show (WEBKIT_WEB_INSPECTOR (inspector));
 
-    g_signal_connect (inspector, "inspect-web-view", G_CALLBACK (web_inspector_inspect_web_view_cb), NULL);
-    g_signal_connect (inspector, "show-window", G_CALLBACK (web_inspector_show_window_cb), NULL);
+    inspector = webkit_web_view_get_inspector (WEBKIT_WEB_VIEW (priv->web_view));
+    webkit_web_inspector_show (inspector);
   }
 #endif /* GOA_INSPECTOR_ENABLED */
 
@@ -337,12 +241,12 @@ goa_web_view_init (GoaWebView *self)
   gtk_overlay_add_overlay (GTK_OVERLAY (self), priv->progress_bar);
 
   priv->notify_progress_id = g_signal_connect (priv->web_view,
-                                               "notify::progress",
-                                               G_CALLBACK (web_view_notify_progress_cb),
+                                               "notify::estimated-load-progress",
+                                               G_CALLBACK (web_view_notify_estimated_load_progress_cb),
                                                self);
   priv->notify_load_status_id = g_signal_connect (priv->web_view,
-                                                  "notify::load-status",
-                                                  G_CALLBACK (web_view_notify_load_status_cb),
+                                                  "load_changed",
+                                                  G_CALLBACK (web_view_load_changed_cb),
                                                   self);
 }
 
@@ -372,7 +276,7 @@ goa_web_view_get_view (GoaWebView *self)
 void
 goa_web_view_fake_mobile (GoaWebView *self)
 {
-  WebKitWebSettings *settings;
+  WebKitSettings *settings;
 
   settings = webkit_web_view_get_settings (WEBKIT_WEB_VIEW (self->priv->web_view));
 
@@ -389,26 +293,20 @@ goa_web_view_fake_mobile (GoaWebView *self)
    * Also note that the user agents of some mobile browsers may
    * not work. eg., Nokia N9.
    */
-  g_object_set (G_OBJECT (settings),
-                "user-agent", "Mozilla/5.0 (GNOME; not Android) "
-                              "AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile",
-                NULL);
+  webkit_settings_set_user_agent (settings,
+                                  "Mozilla/5.0 (GNOME; not Android) "
+                                  "AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile");
 }
 
 void
 goa_web_view_add_cookies (GoaWebView *self,
                           GSList     *cookies)
 {
-  SoupCookieJar *cookie_jar;
-  SoupSession *session;
   GSList *l;
 
-  session = webkit_get_default_session ();
-  cookie_jar = SOUP_COOKIE_JAR (soup_session_get_feature (session, SOUP_TYPE_COOKIE_JAR));
-
   for (l = cookies; l != NULL; l = l->next)
     {
       SoupCookie *cookie = l->data;
-      soup_cookie_jar_add_cookie (cookie_jar, soup_cookie_copy (cookie));
+      soup_cookie_jar_add_cookie (self->priv->cookie_jar, soup_cookie_copy (cookie));
     }
 }



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