Separate the rendering engine process
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index d36053e..70d4eb8 100644 (file)
--- a/surf.c
+++ b/surf.c
@@ -5,6 +5,7 @@
 #include <signal.h>
 #include <X11/X.h>
 #include <X11/Xatom.h>
+#include <gtk/gtkx.h>
 #include <gtk/gtk.h>
 #include <gdk/gdkx.h>
 #include <gdk/gdk.h>
@@ -16,7 +17,7 @@
 #include <limits.h>
 #include <stdlib.h>
 #include <stdio.h>
-#include <webkit/webkit.h>
+#include <webkit2/webkit2.h>
 #include <glib/gstdio.h>
 #include <JavaScriptCore/JavaScript.h>
 #include <sys/file.h>
@@ -32,8 +33,6 @@ char *argv0;
 
 #define LENGTH(x)               (sizeof(x) / sizeof(x[0]))
 #define CLEANMASK(mask)         (mask & (MODKEY|GDK_SHIFT_MASK))
-#define COOKIEJAR_TYPE          (cookiejar_get_type ())
-#define COOKIEJAR(obj)          (G_TYPE_CHECK_INSTANCE_CAST ((obj), COOKIEJAR_TYPE, CookieJar))
 
 enum { AtomFind, AtomGo, AtomUri, AtomLast };
 enum {
@@ -54,7 +53,8 @@ union Arg {
 };
 
 typedef struct Client {
-       GtkWidget *win, *scroll, *vbox, *pane;
+       GtkWidget *win;
+       Window xid;
        WebKitWebView *view;
        WebKitWebInspector *inspector;
        char *title, *linkhover;
@@ -79,17 +79,6 @@ typedef struct {
        const Arg arg;
 } Button;
 
-typedef struct {
-       SoupCookieJarText parent_instance;
-       int lock;
-} CookieJar;
-
-typedef struct {
-       SoupCookieJarTextClass parent_class;
-} CookieJarClass;
-
-G_DEFINE_TYPE(CookieJar, cookiejar, SOUP_TYPE_COOKIE_JAR_TEXT)
-
 typedef struct {
        char *regex;
        char *style;
@@ -99,16 +88,14 @@ typedef struct {
 static Display *dpy;
 static Atom atoms[AtomLast];
 static Client *clients = NULL;
-static GdkNativeWindow embed = 0;
+static Window embed = 0;
 static gboolean showxid = FALSE;
 static char winid[64];
-static gboolean usingproxy = 0;
 static char togglestat[9];
 static char pagestat[3];
 static GTlsDatabase *tlsdb;
-static int policysel = 0;
+static int cookiepolicy;
 static char *stylefile = NULL;
-static SoupCache *diskcache = NULL;
 
 static void addaccelgroup(Client *c);
 static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
@@ -120,16 +107,8 @@ static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c);
 static void cleanup(void);
 static void clipboard(Client *c, const Arg *arg);
 
-/* Cookiejar implementation */
-static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
-                              SoupCookie *new_cookie);
-static void cookiejar_finalize(GObject *self);
-static SoupCookieJarAcceptPolicy cookiepolicy_get(void);
-static SoupCookieJar *cookiejar_new(const char *filename, gboolean read_only,
-                                    SoupCookieJarAcceptPolicy policy);
-static void cookiejar_set_property(GObject *self, guint prop_id,
-    const GValue *value, GParamSpec *pspec);
-static char cookiepolicy_set(const SoupCookieJarAcceptPolicy p);
+static WebKitCookieAcceptPolicy cookiepolicy_get(void);
+static char cookiepolicy_set(const WebKitCookieAcceptPolicy p);
 
 static char *copystr(char **str, const char *src);
 static WebKitWebView *createwindow(WebKitWebView *v, WebKitWebFrame *f,
@@ -196,7 +175,6 @@ static void scroll(GtkAdjustment *a, const Arg *arg);
 static void setatom(Client *c, int a, const char *v);
 static void setup(void);
 static void sigchld(int unused);
-static void source(Client *c, const Arg *arg);
 static void spawn(Client *c, const Arg *arg);
 static void stop(Client *c, const Arg *arg);
 static void titlechange(WebKitWebView *view, GParamSpec *pspec, Client *c);
@@ -349,10 +327,6 @@ buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c)
 void
 cleanup(void)
 {
-       if (diskcache) {
-               soup_cache_flush(diskcache);
-               soup_cache_dump(diskcache);
-       }
        while (clients)
                destroyclient(clients);
        g_free(cookiefile);
@@ -360,90 +334,31 @@ cleanup(void)
        g_free(stylefile);
 }
 
-void
-cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie,
-                  SoupCookie *new_cookie)
-{
-       flock(COOKIEJAR(self)->lock, LOCK_EX);
-       if (new_cookie && !new_cookie->expires && sessiontime) {
-               soup_cookie_set_expires(new_cookie,
-                                       soup_date_new_from_now(sessiontime));
-       }
-       SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self,
-                                                              old_cookie,
-                                                              new_cookie);
-       flock(COOKIEJAR(self)->lock, LOCK_UN);
-}
-
-void
-cookiejar_class_init(CookieJarClass *klass)
-{
-       SOUP_COOKIE_JAR_CLASS(klass)->changed = cookiejar_changed;
-       G_OBJECT_CLASS(klass)->get_property =
-           G_OBJECT_CLASS(cookiejar_parent_class)->get_property;
-       G_OBJECT_CLASS(klass)->set_property = cookiejar_set_property;
-       G_OBJECT_CLASS(klass)->finalize = cookiejar_finalize;
-       g_object_class_override_property(G_OBJECT_CLASS(klass), 1, "filename");
-}
-
-void
-cookiejar_finalize(GObject *self)
-{
-       close(COOKIEJAR(self)->lock);
-       G_OBJECT_CLASS(cookiejar_parent_class)->finalize(self);
-}
-
-void
-cookiejar_init(CookieJar *self)
-{
-       self->lock = open(cookiefile, 0);
-}
-
-SoupCookieJar *
-cookiejar_new(const char *filename, gboolean read_only,
-              SoupCookieJarAcceptPolicy policy)
-{
-       return g_object_new(COOKIEJAR_TYPE,
-                           SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
-                           SOUP_COOKIE_JAR_READ_ONLY, read_only,
-                           SOUP_COOKIE_JAR_ACCEPT_POLICY, policy, NULL);
-}
-
-void
-cookiejar_set_property(GObject *self, guint prop_id, const GValue *value,
-                       GParamSpec *pspec)
-{
-       flock(COOKIEJAR(self)->lock, LOCK_SH);
-       G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id,
-                                                            value, pspec);
-       flock(COOKIEJAR(self)->lock, LOCK_UN);
-}
-
-SoupCookieJarAcceptPolicy
+WebKitCookieAcceptPolicy
 cookiepolicy_get(void)
 {
-       switch (cookiepolicies[policysel]) {
+       switch (cookiepolicies[cookiepolicy]) {
        case 'a':
-               return SOUP_COOKIE_JAR_ACCEPT_NEVER;
+               return WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
        case '@':
-               return SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY;
+               return WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
        case 'A':
        default:
                break;
        }
 
-       return SOUP_COOKIE_JAR_ACCEPT_ALWAYS;
+       return WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
 }
 
 char
-cookiepolicy_set(const SoupCookieJarAcceptPolicy ep)
+cookiepolicy_set(const WebKitCookieAcceptPolicy ep)
 {
        switch (ep) {
-       case SOUP_COOKIE_JAR_ACCEPT_NEVER:
+       case WEBKIT_COOKIE_POLICY_ACCEPT_NEVER:
                return 'a';
-       case SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY:
+       case WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY:
                return '@';
-       case SOUP_COOKIE_JAR_ACCEPT_ALWAYS:
+       case WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS:
        default:
                break;
        }
@@ -625,9 +540,9 @@ getatom(Client *c, int a)
        unsigned long ldummy;
        unsigned char *p = NULL;
 
-       XGetWindowProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
-                          atoms[a], 0L, BUFSIZ, False, XA_STRING,
-                          &adummy, &idummy, &ldummy, &ldummy, &p);
+       XGetWindowProperty(dpy, c->xid,
+                          atoms[a], 0L, BUFSIZ, False, XA_STRING,
+                          &adummy, &idummy, &ldummy, &ldummy, &p);
        if (p)
                strncpy(buf, (char *)p, LENGTH(buf)-1);
        else
@@ -810,10 +725,6 @@ loadstatuschange(WebKitWebView *view, GParamSpec *pspec, Client *c)
        case WEBKIT_LOAD_FINISHED:
                c->progress = 100;
                updatetitle(c);
-               if (diskcache) {
-                       soup_cache_flush(diskcache);
-                       soup_cache_dump(diskcache);
-               }
                break;
        default:
                break;
@@ -867,9 +778,9 @@ newclient(void)
 {
        Client *c;
        WebKitWebSettings *settings;
-       WebKitWebFrame *frame;
        GdkGeometry hints = { 1, 1 };
        GdkScreen *screen;
+       GdkWindow *gwin;
        gdouble dpi;
        char *ua;
 
@@ -910,13 +821,6 @@ newclient(void)
        if (!kioskmode)
                addaccelgroup(c);
 
-       /* Pane */
-       c->pane = gtk_vpaned_new();
-
-       /* VBox */
-       c->vbox = gtk_vbox_new(FALSE, 0);
-       gtk_paned_pack1(GTK_PANED(c->pane), c->vbox, TRUE, TRUE);
-
        /* Webview */
        c->view = WEBKIT_WEB_VIEW(webkit_web_view_new());
 
@@ -963,41 +867,19 @@ newclient(void)
                         "should-show-delete-interface-for-element",
                         G_CALLBACK(deletion_interface), c);
 
-       /* Scrolled Window */
-       c->scroll = gtk_scrolled_window_new(NULL, NULL);
-
-       frame = webkit_web_view_get_main_frame(WEBKIT_WEB_VIEW(c->view));
-       g_signal_connect(G_OBJECT(frame), "scrollbars-policy-changed",
-                        G_CALLBACK(gtk_true), NULL);
-
-       if (!enablescrollbars) {
-               gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
-                                              GTK_POLICY_NEVER,
-                                              GTK_POLICY_NEVER);
-       } else {
-               gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c->scroll),
-                                              GTK_POLICY_AUTOMATIC,
-                                              GTK_POLICY_AUTOMATIC);
-       }
-
        /* Arranging */
-       gtk_container_add(GTK_CONTAINER(c->scroll), GTK_WIDGET(c->view));
-       gtk_container_add(GTK_CONTAINER(c->win), c->pane);
-       gtk_container_add(GTK_CONTAINER(c->vbox), c->scroll);
+       gtk_container_add(GTK_CONTAINER(c->win), GTK_WIDGET(c->view));
 
        /* Setup */
-       gtk_box_set_child_packing(GTK_BOX(c->vbox), c->scroll, TRUE, TRUE, 0,
-                                 GTK_PACK_START);
        gtk_widget_grab_focus(GTK_WIDGET(c->view));
-       gtk_widget_show(c->pane);
-       gtk_widget_show(c->vbox);
-       gtk_widget_show(c->scroll);
        gtk_widget_show(GTK_WIDGET(c->view));
        gtk_widget_show(c->win);
+       gwin = gtk_widget_get_window(GTK_WIDGET(c->win));
+       c->xid = gdk_x11_window_get_xid(gwin);
        gtk_window_set_geometry_hints(GTK_WINDOW(c->win), NULL, &hints,
-                                     GDK_HINT_MIN_SIZE);
-       gdk_window_set_events(GTK_WIDGET(c->win)->window, GDK_ALL_EVENTS_MASK);
-       gdk_window_add_filter(GTK_WIDGET(c->win)->window, processx, c);
+                                     GDK_HINT_MIN_SIZE);
+       gdk_window_set_events(gwin, GDK_ALL_EVENTS_MASK);
+       gdk_window_add_filter(gwin, processx, c);
        webkit_web_view_set_full_content_zoom(c->view, TRUE);
 
        runscript(frame);
@@ -1025,20 +907,6 @@ newclient(void)
        if (enablestyle)
                setstyle(c, getstyle("about:blank"));
 
-       /*
-        * While stupid, CSS specifies that a pixel represents 1/96 of an inch.
-        * This ensures websites are not unusably small with a high DPI screen.
-        * It is equivalent to firefox's "layout.css.devPixelsPerPx" setting.
-        */
-       if (zoomto96dpi) {
-               screen = gdk_window_get_screen(GTK_WIDGET(c->win)->window);
-               dpi = gdk_screen_get_resolution(screen);
-               if (dpi != -1) {
-                       g_object_set(G_OBJECT(settings),
-                                    "enforce-96-dpi", true, NULL);
-                       webkit_web_view_set_zoom_level(c->view, dpi/96);
-               }
-       }
        /* This might conflict with _zoomto96dpi_. */
        if (zoomlevel != 1.0)
                webkit_web_view_set_zoom_level(c->view, zoomlevel);
@@ -1069,8 +937,7 @@ newclient(void)
 
        if (showxid) {
                gdk_display_sync(gtk_widget_get_display(c->win));
-               printf("%u\n",
-                      (guint)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
+               printf("%lu\n", c->xid);
                fflush(NULL);
                 if (fclose(stdout) != 0) {
                        die("Error closing stdout");
@@ -1150,15 +1017,13 @@ menuactivate(GtkMenuItem *item, Client *c)
         * context-menu-action-12   stop
         */
 
-       GtkAction *a = NULL;
-       const char *name, *uri;
+       const gchar *name, *uri;
        GtkClipboard *prisel, *clpbrd;
 
-       a = gtk_activatable_get_related_action(GTK_ACTIVATABLE(item));
-       if (a == NULL)
+       name = gtk_actionable_get_action_name(GTK_ACTIONABLE(item));
+       if (name == NULL)
                return;
 
-       name = gtk_action_get_name(a);
        if (!g_strcmp0(name, "context-menu-action-3")) {
                prisel = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
                gtk_clipboard_set_text(prisel, c->linkhover, -1);
@@ -1281,26 +1146,24 @@ void
 setatom(Client *c, int a, const char *v)
 {
        XSync(dpy, False);
-       XChangeProperty(dpy, GDK_WINDOW_XID(GTK_WIDGET(c->win)->window),
-                       atoms[a], XA_STRING, 8, PropModeReplace,
-                       (unsigned char *)v, strlen(v) + 1);
+       XChangeProperty(dpy, c->xid,
+                       atoms[a], XA_STRING, 8, PropModeReplace,
+                       (unsigned char *)v, strlen(v) + 1);
 }
 
 void
 setup(void)
 {
        int i;
-       char *proxy, *new_proxy;
        char *styledirfile, *stylepath;
-       SoupURI *puri;
-       SoupSession *s;
+       WebKitWebContext *context;
        GError *error = NULL;
 
        /* clean up any zombies immediately */
        sigchld(0);
        gtk_init(NULL, NULL);
 
-       dpy = GDK_DISPLAY();
+       dpy = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
 
        /* atoms */
        atoms[AtomFind] = XInternAtom(dpy, "_SURF_FIND", False);
@@ -1336,48 +1199,28 @@ setup(void)
                g_free(stylepath);
        }
 
-       /* request handler */
-       s = webkit_get_default_session();
+       /* cookie policy */
+       webkit_cookie_manager_set_persistent_storage(
+           webkit_web_context_get_cookie_manager(context), cookiefile,
+           WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
+       webkit_cookie_manager_set_accept_policy(
+           webkit_web_context_get_cookie_manager(context),
+           cookiepolicy_get());
 
-       /* cookie jar */
-       soup_session_add_feature(s,
-                                SOUP_SESSION_FEATURE(cookiejar_new(cookiefile,
-                                FALSE, cookiepolicy_get())));
+       /* rendering process model, can be a shared unique one or one for each
+        * view */
+       webkit_web_context_set_process_model(context,
+           WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
 
        /* disk cache */
-       if (enablediskcache) {
-               diskcache = soup_cache_new(cachefolder,
-                                          SOUP_CACHE_SINGLE_USER);
-               soup_cache_set_max_size(diskcache, diskcachebytes);
-               soup_cache_load(diskcache);
-               soup_session_add_feature(s, SOUP_SESSION_FEATURE(diskcache));
-       }
+       webkit_web_context_set_cache_model(context, enablecache ?
+           WEBKIT_CACHE_MODEL_WEB_BROWSER :
+           WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
 
        /* ssl */
-       tlsdb = g_tls_file_database_new(cafile, &error);
-
-       if (error) {
-               g_warning("Error loading SSL database %s: %s", cafile,
-                         error->message);
-               g_error_free(error);
-       }
-       g_object_set(G_OBJECT(s), "tls-database", tlsdb, NULL);
-       g_object_set(G_OBJECT(s), "ssl-strict", strictssl, NULL);
-
-       /* proxy */
-       if ((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
-               new_proxy = g_strrstr(proxy, "http://")
-                           || g_strrstr(proxy, "socks://")
-                           || g_strrstr(proxy, "socks4://")
-                           || g_strrstr(proxy, "socks5://")
-                           ? g_strdup(proxy)
-                           : g_strdup_printf("http://%s", proxy);
-               puri = soup_uri_new(new_proxy);
-               g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
-               soup_uri_free(puri);
-               g_free(new_proxy);
-               usingproxy = 1;
-       }
+       webkit_web_context_set_tls_errors_policy(context, strictssl ?
+           WEBKIT_TLS_ERRORS_POLICY_FAIL :
+           WEBKIT_TLS_ERRORS_POLICY_IGNORE);
 }
 
 void
@@ -1388,17 +1231,6 @@ sigchld(int unused)
        while (0 < waitpid(-1, NULL, WNOHANG));
 }
 
-void
-source(Client *c, const Arg *arg)
-{
-       Arg a = { .b = FALSE };
-       gboolean s;
-
-       s = webkit_web_view_get_view_source_mode(c->view);
-       webkit_web_view_set_view_source_mode(c->view, !s);
-       reload(c, &a);
-}
-
 void
 spawn(Client *c, const Arg *arg)
 {
@@ -1462,19 +1294,13 @@ toggle(Client *c, const Arg *arg)
 void
 togglecookiepolicy(Client *c, const Arg *arg)
 {
-       SoupCookieJar *jar;
-       SoupCookieJarAcceptPolicy policy;
-
-       jar = SOUP_COOKIE_JAR(soup_session_get_feature(
-                             webkit_get_default_session(),
-                             SOUP_TYPE_COOKIE_JAR));
-       g_object_get(G_OBJECT(jar), "accept-policy", &policy, NULL);
-
-       policysel++;
-       if (policysel >= strlen(cookiepolicies))
-               policysel = 0;
+       ++cookiepolicy;
+       cookiepolicy %= strlen(cookiepolicies);
 
-       g_object_set(G_OBJECT(jar), "accept-policy", cookiepolicy_get(), NULL);
+       webkit_cookie_manager_set_accept_policy(
+           webkit_web_context_get_cookie_manager(
+           webkit_web_view_get_context(c->view)),
+           cookiepolicy_get());
 
        updatetitle(c);
        /* Do not reload. */
@@ -1582,8 +1408,7 @@ getpagestat(Client *c)
        else
                pagestat[0] = '-';
 
-       pagestat[1] = usingproxy ? 'P' : '-';
-       pagestat[2] = '\0';
+       pagestat[1] = '\0';
 }
 
 void
@@ -1618,8 +1443,7 @@ updatetitle(Client *c)
 void
 updatewinid(Client *c)
 {
-       snprintf(winid, LENGTH(winid), "%u",
-                (int)GDK_WINDOW_XID(GTK_WIDGET(c->win)->window));
+       snprintf(winid, LENGTH(winid), "%lu", c->xid);
 }
 
 void