Request cookiemanager only once at creation
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index f01a91c..6950dc5 100644 (file)
--- a/surf.c
+++ b/surf.c
@@ -146,6 +146,7 @@ static void sigchld(int unused);
 static void sighup(int unused);
 static char *buildfile(const char *path);
 static char *buildpath(const char *path);
+static char *untildepath(const char *path);
 static const char *getuserhomedir(const char *user);
 static const char *getcurrentuserhomedir(void);
 static Client *newclient(Client *c);
@@ -205,6 +206,9 @@ static void downloadstarted(WebKitWebContext *wc, WebKitDownload *d,
                             Client *c);
 static void responsereceived(WebKitDownload *d, GParamSpec *ps, Client *c);
 static void download(Client *c, WebKitURIResponse *r);
+static void webprocessterminated(WebKitWebView *v,
+                                 WebKitWebProcessTerminationReason r,
+                                 Client *c);
 static void closeview(WebKitWebView *v, Client *c);
 static void destroywin(GtkWidget* w, Client *c);
 
@@ -470,26 +474,12 @@ getcurrentuserhomedir(void)
 char *
 buildpath(const char *path)
 {
-       char *apath, *name, *p, *fpath;
-       const char *homedir;
-
-       if (path[0] == '~') {
-               if (path[1] == '/' || path[1] == '\0') {
-                       p = (char *)&path[1];
-                       homedir = getcurrentuserhomedir();
-               } else {
-                       if ((p = strchr(path, '/')))
-                               name = g_strndup(&path[1], --p - path);
-                       else
-                               name = g_strdup(&path[1]);
+       char *apath, *fpath;
 
-                       homedir = getuserhomedir(name);
-                       g_free(name);
-               }
-               apath = g_build_filename(homedir, p, NULL);
-       } else {
+       if (path[0] == '~')
+               apath = untildepath(path);
+       else
                apath = g_strdup(path);
-       }
 
        /* creating directory */
        if (g_mkdir_with_parents(apath, 0700) < 0)
@@ -501,6 +491,28 @@ buildpath(const char *path)
        return fpath;
 }
 
+char *
+untildepath(const char *path)
+{
+       char *apath, *name, *p;
+       const char *homedir;
+
+       if (path[1] == '/' || path[1] == '\0') {
+               p = (char *)&path[1];
+               homedir = getcurrentuserhomedir();
+       } else {
+               if ((p = strchr(path, '/')))
+                       name = g_strndup(&path[1], p - (path + 1));
+               else
+                       name = g_strdup(&path[1]);
+
+               homedir = getuserhomedir(name);
+               g_free(name);
+       }
+       apath = g_build_filename(homedir, p, NULL);
+       return apath;
+}
+
 Client *
 newclient(Client *rc)
 {
@@ -522,7 +534,7 @@ void
 loaduri(Client *c, const Arg *a)
 {
        struct stat st;
-       char *url, *path;
+       char *url, *path, *apath;
        const char *uri = a->v;
 
        if (g_strcmp0(uri, "") == 0)
@@ -533,11 +545,19 @@ loaduri(Client *c, const Arg *a)
            g_str_has_prefix(uri, "file://")  ||
            g_str_has_prefix(uri, "about:")) {
                url = g_strdup(uri);
-       } else if (!stat(uri, &st) && (path = realpath(uri, NULL))) {
-               url = g_strdup_printf("file://%s", path);
-               free(path);
        } else {
-               url = g_strdup_printf("http://%s", uri);
+               if (uri[0] == '~')
+                       apath = untildepath(uri);
+               else
+                       apath = (char *)uri;
+               if (!stat(apath, &st) && (path = realpath(apath, NULL))) {
+                       url = g_strdup_printf("file://%s", path);
+                       free(path);
+               } else {
+                       url = g_strdup_printf("http://%s", uri);
+               }
+               if (apath != uri)
+                       free(apath);
        }
 
        setatom(c, AtomUri, url);
@@ -1059,6 +1079,7 @@ newview(Client *c, WebKitWebView *rv)
        WebKitSettings *settings;
        WebKitUserContentManager *contentmanager;
        WebKitWebContext *context;
+       WebKitCookieManager *cookiemanager;
 
        /* Webview */
        if (rv) {
@@ -1105,6 +1126,8 @@ newview(Client *c, WebKitWebView *rv)
                          "base-data-directory", cachedir,
                          NULL));
 
+               cookiemanager = webkit_web_context_get_cookie_manager(context);
+
                /* rendering process model, can be a shared unique one
                 * or one for each view */
                webkit_web_context_set_process_model(context,
@@ -1119,12 +1142,10 @@ newview(Client *c, WebKitWebView *rv)
                    WEBKIT_CACHE_MODEL_DOCUMENT_VIEWER);
 
                /* Currently only works with text file to be compatible with curl */
-               webkit_cookie_manager_set_persistent_storage(
-                   webkit_web_context_get_cookie_manager(context), cookiefile,
-                   WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
+               webkit_cookie_manager_set_persistent_storage(cookiemanager,
+                   cookiefile, WEBKIT_COOKIE_PERSISTENT_STORAGE_TEXT);
                /* cookie policy */
-               webkit_cookie_manager_set_accept_policy(
-                   webkit_web_context_get_cookie_manager(context),
+               webkit_cookie_manager_set_accept_policy(cookiemanager,
                    cookiepolicy_get());
                /* languages */
                webkit_web_context_set_preferred_languages(context,
@@ -1170,6 +1191,8 @@ newview(Client *c, WebKitWebView *rv)
                         G_CALLBACK(permissionrequested), c);
        g_signal_connect(G_OBJECT(v), "ready-to-show",
                         G_CALLBACK(showview), c);
+       g_signal_connect(G_OBJECT(v), "web-process-terminated",
+                        G_CALLBACK(webprocessterminated), c);
 
        return v;
 }
@@ -1315,6 +1338,7 @@ showview(WebKitWebView *v, Client *c)
        if (showxid) {
                gdk_display_sync(gtk_widget_get_display(c->win));
                puts(winid);
+               fflush(stdout);
        }
 
        if (curconfig[HideBackground].val.i)
@@ -1673,6 +1697,15 @@ download(Client *c, WebKitURIResponse *r)
        spawn(c, &a);
 }
 
+void
+webprocessterminated(WebKitWebView *v, WebKitWebProcessTerminationReason r,
+                     Client *c)
+{
+       fprintf(stderr, "web process terminated: %s\n",
+               r == WEBKIT_WEB_PROCESS_CRASHED ? "crashed" : "no memory");
+       closeview(v, c);
+}
+
 void
 closeview(WebKitWebView *v, Client *c)
 {