]> git.armaanb.net Git - chorizo.git/blob - browser.c
1856b5ba77d79cb46b9d971384b98155e78f850c
[chorizo.git] / browser.c
1 #include <limits.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <string.h>
8
9 #include <gtk/gtk.h>
10 #include <gtk/gtkx.h>
11 #include <gdk/gdkkeysyms.h>
12 #include <gio/gio.h>
13 #include <webkit2/webkit2.h>
14 #include <JavaScriptCore/JavaScript.h>
15
16
17 static void client_destroy(GtkWidget *, gpointer);
18 static gboolean client_destroy_request(WebKitWebView *, gpointer);
19 static WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean);
20 static WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
21                                          gpointer);
22 static void cooperation_setup(void);
23 static void changed_download_progress(GObject *, GParamSpec *, gpointer);
24 static void changed_load_progress(GObject *, GParamSpec *, gpointer);
25 static void changed_title(GObject *, GParamSpec *, gpointer);
26 static void changed_uri(GObject *, GParamSpec *, gpointer);
27 static gboolean crashed_web_view(WebKitWebView *, gpointer);
28 static gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
29                               WebKitPolicyDecisionType, gpointer);
30 static gboolean download_handle(WebKitDownload *, gchar *, gpointer);
31 static void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
32 static void downloadmanager_cancel(GtkToolButton *, gpointer);
33 static gboolean downloadmanager_delete(GtkWidget *, gpointer);
34 static void downloadmanager_setup(void);
35 static gchar *ensure_uri_scheme(const gchar *);
36 static void external_handler_run(GSimpleAction *, GVariant *, gpointer);
37 static void grab_environment_configuration(void);
38 static void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
39 static void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
40 static void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
41 static gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
42 static gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
43 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
44 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
45 static void keywords_load(void);
46 static gboolean keywords_try_search(WebKitWebView *, const gchar *);
47 static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
48                               WebKitHitTestResult *, gpointer);
49 static gboolean quit_if_nothing_active(void);
50 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
51 static void run_user_scripts(WebKitWebView *);
52 static void search(gpointer, gint);
53 static void show_web_view(WebKitWebView *, gpointer);
54 static Window tabbed_launch(void);
55 static void trust_user_certs(WebKitWebContext *);
56
57
58 struct Client
59 {
60     gchar *external_handler_uri;
61     gchar *hover_uri;
62     gchar *feed_html;
63     GtkWidget *location;
64     GtkWidget *vbox;
65     GtkWidget *web_view;
66     GtkWidget *win;
67 };
68
69 struct DownloadManager
70 {
71     GtkWidget *scroll;
72     GtkWidget *toolbar;
73     GtkWidget *win;
74 } dm;
75
76
77 static const gchar *accepted_language[2] = { NULL, NULL };
78 static gint clients = 0, downloads = 0;
79 static gboolean cooperative_alone = TRUE;
80 static gboolean cooperative_instances = TRUE;
81 static int cooperative_pipe_fp = 0;
82 static gchar *download_dir = "/var/tmp";
83 static gboolean enable_webgl = FALSE;
84 static gboolean enable_console_to_stdout = FALSE;
85 static Window embed = 0;
86 static gchar *fifo_suffix = "main";
87 static gdouble global_zoom = 1.0;
88 static gchar *history_file = NULL;
89 static gchar *home_uri = "about:blank";
90 static gboolean initial_wc_setup_done = FALSE;
91 static GHashTable *keywords = NULL;
92 static gchar *search_text = NULL;
93 static gboolean tabbed_automagic = TRUE;
94 static gchar *user_agent = NULL;
95
96
97 void
98 client_destroy(GtkWidget *widget, gpointer data)
99 {
100     struct Client *c = (struct Client *)data;
101
102     g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
103                                          changed_load_progress, c);
104
105     free(c);
106     clients--;
107
108     quit_if_nothing_active();
109 }
110
111 gboolean
112 client_destroy_request(WebKitWebView *web_view, gpointer data)
113 {
114     struct Client *c = (struct Client *)data;
115
116     gtk_widget_destroy(c->win);
117
118     return TRUE;
119 }
120
121 WebKitWebView *
122 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
123 {
124     struct Client *c;
125     WebKitWebContext *wc;
126     gchar *f;
127
128     if (uri != NULL && cooperative_instances && !cooperative_alone)
129     {
130         f = ensure_uri_scheme(uri);
131         write(cooperative_pipe_fp, f, strlen(f));
132         write(cooperative_pipe_fp, "\n", 1);
133         g_free(f);
134         return NULL;
135     }
136
137     c = calloc(1, sizeof(struct Client));
138     if (!c)
139     {
140         fprintf(stderr, __NAME__": fatal: calloc failed\n");
141         exit(EXIT_FAILURE);
142     }
143
144     if (embed != 0)
145     {
146         c->win = gtk_plug_new(embed);
147         if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
148         {
149             fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
150             gtk_widget_destroy(c->win);
151             c->win = NULL;
152             embed = 0;
153         }
154     }
155
156     if (c->win == NULL)
157         c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
158
159     gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
160
161     g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
162     gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
163
164     if (related_wv == NULL)
165         c->web_view = webkit_web_view_new();
166     else
167         c->web_view = webkit_web_view_new_with_related_view(related_wv);
168     wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
169
170     webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
171     g_signal_connect(G_OBJECT(c->web_view), "notify::title",
172                      G_CALLBACK(changed_title), c);
173     g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
174                      G_CALLBACK(changed_uri), c);
175     g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
176                      G_CALLBACK(changed_load_progress), c);
177     g_signal_connect(G_OBJECT(c->web_view), "create",
178                      G_CALLBACK(client_new_request), NULL);
179     g_signal_connect(G_OBJECT(c->web_view), "context-menu",
180                      G_CALLBACK(menu_web_view), c);
181     g_signal_connect(G_OBJECT(c->web_view), "close",
182                      G_CALLBACK(client_destroy_request), c);
183     g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
184                      G_CALLBACK(decide_policy), NULL);
185     g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
186                      G_CALLBACK(key_web_view), c);
187     g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
188                      G_CALLBACK(key_web_view), c);
189     g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
190                      G_CALLBACK(key_web_view), c);
191     g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
192                      G_CALLBACK(hover_web_view), c);
193     g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
194                      G_CALLBACK(crashed_web_view), c);
195
196     if (!initial_wc_setup_done)
197     {
198         if (accepted_language[0] != NULL)
199             webkit_web_context_set_preferred_languages(wc, accepted_language);
200
201         g_signal_connect(G_OBJECT(wc), "download-started",
202                          G_CALLBACK(download_handle_start), NULL);
203
204         trust_user_certs(wc);
205
206         initial_wc_setup_done = TRUE;
207     }
208
209     if (user_agent != NULL)
210         g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
211                      "user-agent", user_agent, NULL);
212
213     if (enable_console_to_stdout)
214         webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
215
216     if (enable_webgl)
217         webkit_settings_set_enable_webgl(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
218
219     webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
220
221     c->location = gtk_entry_new();
222     g_signal_connect(G_OBJECT(c->location), "key-press-event",
223                      G_CALLBACK(key_location), c);
224     g_signal_connect(G_OBJECT(c->location), "icon-release",
225                      G_CALLBACK(icon_location), c);
226     /* XXX This is a workaround. Setting this to NULL (which is done in
227      * grab_feeds_finished() if no feed has been detected) adds a little
228      * padding left of the text. Not sure why. The point of this call
229      * right here is to have that padding right from the start. This
230      * avoids a graphical artifact. */
231     gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
232                                       GTK_ENTRY_ICON_PRIMARY,
233                                       NULL);
234
235     c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
236     gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
237     gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
238
239     gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
240
241     if (show)
242         show_web_view(NULL, c);
243     else
244         g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
245                          G_CALLBACK(show_web_view), c);
246
247     if (uri != NULL)
248     {
249         f = ensure_uri_scheme(uri);
250         webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
251         g_free(f);
252     }
253
254     clients++;
255
256     return WEBKIT_WEB_VIEW(c->web_view);
257 }
258
259 WebKitWebView *
260 client_new_request(WebKitWebView *web_view,
261                    WebKitNavigationAction *navigation_action, gpointer data)
262 {
263     return client_new(NULL, web_view, FALSE);
264 }
265
266 void
267 cooperation_setup(void)
268 {
269     GIOChannel *towatch;
270     gchar *fifofilename, *fifopath;
271
272     fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
273     fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
274     g_free(fifofilename);
275
276     if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
277         mkfifo(fifopath, 0600);
278
279     cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
280     if (!cooperative_pipe_fp)
281     {
282         fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
283     }
284     else
285     {
286         if (write(cooperative_pipe_fp, "", 0) == -1)
287         {
288             /* Could not do an empty write to the FIFO which means there's
289              * no one listening. */
290             close(cooperative_pipe_fp);
291             towatch = g_io_channel_new_file(fifopath, "r+", NULL);
292             g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
293         }
294         else
295             cooperative_alone = FALSE;
296     }
297
298     g_free(fifopath);
299 }
300
301 void
302 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
303 {
304     WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
305     WebKitURIResponse *resp;
306     GtkToolItem *tb = GTK_TOOL_ITEM(data);
307     gdouble p, size_mb;
308     const gchar *uri;
309     gchar *t, *filename, *base;
310
311     p = webkit_download_get_estimated_progress(download);
312     p = p > 1 ? 1 : p;
313     p = p < 0 ? 0 : p;
314     p *= 100;
315     resp = webkit_download_get_response(download);
316     size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
317
318     uri = webkit_download_get_destination(download);
319     filename = g_filename_from_uri(uri, NULL, NULL);
320     if (filename == NULL)
321     {
322         /* This really should not happen because WebKit uses that URI to
323          * write to a file... */
324         fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
325         t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
326                             webkit_uri_response_get_uri(resp), p, size_mb);
327     }
328     else
329     {
330         base = g_path_get_basename(filename);
331         t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
332         g_free(filename);
333         g_free(base);
334     }
335     gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
336     g_free(t);
337 }
338
339 void
340 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
341 {
342     struct Client *c = (struct Client *)data;
343     gdouble p;
344     gchar *grab_feeds =
345         "a = document.querySelectorAll('"
346         "    html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
347         "    html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
348         "');"
349         "if (a.length == 0)"
350         "    null;"
351         "else"
352         "{"
353         "    out = '';"
354         "    for (i = 0; i < a.length; i++)"
355         "    {"
356         "        url = encodeURIComponent(a[i].href);"
357         "        if ('title' in a[i] && a[i].title != '')"
358         "            title = encodeURIComponent(a[i].title);"
359         "        else"
360         "            title = url;"
361         "        out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
362         "    }"
363         "    out;"
364         "}";
365
366     p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
367     if (p == 1)
368     {
369         p = 0;
370
371         /* The page has loaded fully. We now run the short JavaScript
372          * snippet above that operates on the DOM. It tries to grab all
373          * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
374          * references. */
375         webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
376                                        grab_feeds, NULL,
377                                        grab_feeds_finished, c);
378
379         run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
380     }
381     gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
382 }
383
384 void
385 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
386 {
387     const gchar *t, *u;
388     struct Client *c = (struct Client *)data;
389
390     u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
391     t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
392
393     u = u == NULL ? __NAME__ : u;
394     u = u[0] == 0 ? __NAME__ : u;
395
396     t = t == NULL ? u : t;
397     t = t[0] == 0 ? u : t;
398
399     gtk_window_set_title(GTK_WINDOW(c->win), t);
400 }
401
402 void
403 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
404 {
405     const gchar *t;
406     struct Client *c = (struct Client *)data;
407     FILE *fp;
408
409     t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
410
411     /* When a web process crashes, we get a "notify::uri" signal, but we
412      * can no longer read a meaningful URI. It's just an empty string
413      * now. Not updating the location bar in this scenario is important,
414      * because we would override the "WEB PROCESS CRASHED" message. */
415     if (t != NULL && strlen(t) > 0)
416     {
417         gtk_entry_set_text(GTK_ENTRY(c->location), t);
418
419         if (history_file != NULL)
420         {
421             fp = fopen(history_file, "a");
422             if (fp != NULL)
423             {
424                 fprintf(fp, "%s\n", t);
425                 fclose(fp);
426             }
427             else
428                 perror(__NAME__": Error opening history file");
429         }
430     }
431 }
432
433 gboolean
434 crashed_web_view(WebKitWebView *web_view, gpointer data)
435 {
436     gchar *t;
437     struct Client *c = (struct Client *)data;
438
439     t = g_strdup_printf("WEB PROCESS CRASHED: %s",
440                         webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
441     gtk_entry_set_text(GTK_ENTRY(c->location), t);
442     g_free(t);
443
444     return TRUE;
445 }
446
447 gboolean
448 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
449               WebKitPolicyDecisionType type, gpointer data)
450 {
451     WebKitResponsePolicyDecision *r;
452
453     switch (type)
454     {
455         case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
456             r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
457             if (!webkit_response_policy_decision_is_mime_type_supported(r))
458                 webkit_policy_decision_download(decision);
459             else
460                 webkit_policy_decision_use(decision);
461             break;
462         default:
463             /* Use whatever default there is. */
464             return FALSE;
465     }
466     return TRUE;
467 }
468
469 void
470 download_handle_finished(WebKitDownload *download, gpointer data)
471 {
472     downloads--;
473 }
474
475 void
476 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
477                       gpointer data)
478 {
479     g_signal_connect(G_OBJECT(download), "decide-destination",
480                      G_CALLBACK(download_handle), data);
481 }
482
483 gboolean
484 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
485 {
486     gchar *sug_clean, *path, *path2 = NULL, *uri;
487     GtkToolItem *tb;
488     int suffix = 1;
489     size_t i;
490
491     sug_clean = g_strdup(suggested_filename);
492     for (i = 0; i < strlen(sug_clean); i++)
493         if (sug_clean[i] == G_DIR_SEPARATOR)
494             sug_clean[i] = '_';
495
496     path = g_build_filename(download_dir, sug_clean, NULL);
497     path2 = g_strdup(path);
498     while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
499     {
500         g_free(path2);
501
502         path2 = g_strdup_printf("%s.%d", path, suffix);
503         suffix++;
504     }
505
506     if (suffix == 1000)
507     {
508         fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
509         webkit_download_cancel(download);
510     }
511     else
512     {
513         uri = g_filename_to_uri(path2, NULL, NULL);
514         webkit_download_set_destination(download, uri);
515         g_free(uri);
516
517         tb = gtk_tool_button_new(NULL, NULL);
518         gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
519         gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
520         gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
521         gtk_widget_show_all(dm.win);
522
523         g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
524                          G_CALLBACK(changed_download_progress), tb);
525
526         downloads++;
527         g_signal_connect(G_OBJECT(download), "finished",
528                          G_CALLBACK(download_handle_finished), NULL);
529
530         g_object_ref(download);
531         g_signal_connect(G_OBJECT(tb), "clicked",
532                          G_CALLBACK(downloadmanager_cancel), download);
533     }
534
535     g_free(sug_clean);
536     g_free(path);
537     g_free(path2);
538
539     /* Propagate -- to whom it may concern. */
540     return FALSE;
541 }
542
543 void
544 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
545 {
546     WebKitDownload *download = WEBKIT_DOWNLOAD(data);
547
548     webkit_download_cancel(download);
549     g_object_unref(download);
550
551     gtk_widget_destroy(GTK_WIDGET(tb));
552 }
553
554 gboolean
555 downloadmanager_delete(GtkWidget *obj, gpointer data)
556 {
557     if (!quit_if_nothing_active())
558         gtk_widget_hide(dm.win);
559
560     return TRUE;
561 }
562
563 void
564 downloadmanager_setup(void)
565 {
566     dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
567     gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
568     gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
569     gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
570     g_signal_connect(G_OBJECT(dm.win), "delete-event",
571                      G_CALLBACK(downloadmanager_delete), NULL);
572     g_signal_connect(G_OBJECT(dm.win), "key-press-event",
573                      G_CALLBACK(key_downloadmanager), NULL);
574
575     dm.toolbar = gtk_toolbar_new();
576     gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
577                                    GTK_ORIENTATION_VERTICAL);
578     gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
579     gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
580
581     dm.scroll = gtk_scrolled_window_new(NULL, NULL);
582     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
583                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
584     gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
585
586     gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
587 }
588
589 gchar *
590 ensure_uri_scheme(const gchar *t)
591 {
592     gchar *f, *fabs;
593
594     f = g_ascii_strdown(t, -1);
595     if (!g_str_has_prefix(f, "http:") &&
596         !g_str_has_prefix(f, "https:") &&
597         !g_str_has_prefix(f, "file:") &&
598         !g_str_has_prefix(f, "about:") &&
599         !g_str_has_prefix(f, "data:"))
600     {
601         g_free(f);
602         fabs = realpath(t, NULL);
603         if (fabs != NULL)
604         {
605             f = g_strdup_printf("file://%s", fabs);
606             free(fabs);
607         }
608         else
609             f = g_strdup_printf("http://%s", t);
610         return f;
611     }
612     else
613         return g_strdup(t);
614 }
615
616 void
617 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
618 {
619     struct Client *c = (struct Client *)data;
620     gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
621     GPid pid;
622     GError *err = NULL;
623
624     (void)simple;
625     (void)param;
626
627     argv[2] = c->external_handler_uri;
628     if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
629                        &pid, &err))
630     {
631         fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
632                 err->message);
633         g_error_free(err);
634     }
635     else
636         g_spawn_close_pid(pid);
637 }
638
639 void
640 grab_environment_configuration(void)
641 {
642     const gchar *e;
643
644     e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
645     if (e != NULL)
646         accepted_language[0] = g_strdup(e);
647
648     e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
649     if (e != NULL)
650         download_dir = g_strdup(e);
651
652     e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
653     if (e != NULL)
654         enable_console_to_stdout = TRUE;
655
656     e = g_getenv(__NAME_UPPERCASE__"_ENABLE_EXPERIMENTAL_WEBGL");
657     if (e != NULL)
658         enable_webgl = TRUE;
659
660     e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
661     if (e != NULL)
662         fifo_suffix = g_strdup(e);
663
664     e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
665     if (e != NULL)
666         history_file = g_strdup(e);
667
668     e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
669     if (e != NULL)
670         home_uri = g_strdup(e);
671
672     e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
673     if (e != NULL)
674         user_agent = g_strdup(e);
675
676     e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
677     if (e != NULL)
678         global_zoom = atof(e);
679 }
680
681 void
682 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
683 {
684     struct Client *c = (struct Client *)data;
685     WebKitJavascriptResult *js_result;
686     JSCValue *value;
687     JSCException *exception;
688     GError *err = NULL;
689     gchar *str_value;
690
691     g_free(c->feed_html);
692     c->feed_html = NULL;
693
694     /* This was taken almost verbatim from the example in WebKit's
695      * documentation:
696      *
697      * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
698
699     js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
700                                                       result, &err);
701     if (!js_result)
702     {
703         fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
704         g_error_free(err);
705         return;
706     }
707
708     value = webkit_javascript_result_get_js_value(js_result);
709     if (jsc_value_is_string(value))
710     {
711         str_value = jsc_value_to_string(value);
712         exception = jsc_context_get_exception(jsc_value_get_context(value));
713         if (exception != NULL)
714         {
715             fprintf(stderr, __NAME__": Error running javascript: %s\n",
716                     jsc_exception_get_message(exception));
717         }
718         else
719             c->feed_html = str_value;
720
721         gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
722                                           GTK_ENTRY_ICON_PRIMARY,
723                                           "application-rss+xml-symbolic");
724         gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
725                                        GTK_ENTRY_ICON_PRIMARY,
726                                        TRUE);
727     }
728     else
729     {
730         gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
731                                           GTK_ENTRY_ICON_PRIMARY,
732                                           NULL);
733     }
734
735     webkit_javascript_result_unref(js_result);
736 }
737
738 void
739 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
740                gpointer data)
741 {
742     struct Client *c = (struct Client *)data;
743
744     if (!gtk_widget_is_focus(c->location))
745     {
746         if (webkit_hit_test_result_context_is_link(ht))
747         {
748             gtk_entry_set_text(GTK_ENTRY(c->location),
749                                webkit_hit_test_result_get_link_uri(ht));
750
751             if (c->hover_uri != NULL)
752                 g_free(c->hover_uri);
753             c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
754         }
755         else
756         {
757             gtk_entry_set_text(GTK_ENTRY(c->location),
758                                webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
759
760             if (c->hover_uri != NULL)
761                 g_free(c->hover_uri);
762             c->hover_uri = NULL;
763         }
764     }
765 }
766
767 void
768 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
769               gpointer data)
770 {
771     struct Client *c = (struct Client *)data;
772     gchar *d;
773     gchar *data_template =
774         "data:text/html,"
775         "<!DOCTYPE html>"
776         "<html>"
777         "    <head>"
778         "        <meta charset=\"UTF-8\">"
779         "        <title>Feeds</title>"
780         "    </head>"
781         "    <body>"
782         "        <p>Feeds found on this page:</p>"
783         "        <ul>"
784         "        %s"
785         "        </ul>"
786         "    </body>"
787         "</html>";
788
789     if (c->feed_html != NULL)
790     {
791         /* What we're actually trying to do is show a simple HTML page
792          * that lists all the feeds on the current page. The function
793          * webkit_web_view_load_html() looks like the proper way to do
794          * that. Sad thing is, it doesn't create a history entry, but
795          * instead simply replaces the content of the current page. This
796          * is not what we want.
797          *
798          * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
799          * mechanism to show my custom HTML snippet *and* create a
800          * history entry.
801          *
802          * [0]: https://tools.ietf.org/html/rfc2397
803          * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
804         d = g_strdup_printf(data_template, c->feed_html);
805         webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
806         g_free(d);
807     }
808 }
809
810 gboolean
811 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
812 {
813     struct Client *c = (struct Client *)data;
814     WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
815     gchar *f;
816
817     if (event->type == GDK_KEY_PRESS)
818     {
819         if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
820         {
821             switch (((GdkEventKey *)event)->keyval)
822             {
823                 case GDK_KEY_q:  /* close window (left hand) */
824                     gtk_widget_destroy(c->win);
825                     return TRUE;
826                 case GDK_KEY_w:  /* home (left hand) */
827                     f = ensure_uri_scheme(home_uri);
828                     webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
829                     g_free(f);
830                     return TRUE;
831                 case GDK_KEY_e:  /* new tab (left hand) */
832                     f = ensure_uri_scheme(home_uri);
833                     client_new(f, NULL, TRUE);
834                     g_free(f);
835                     return TRUE;
836                 case GDK_KEY_r:  /* reload (left hand) */
837                     webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
838                                                         c->web_view));
839                     return TRUE;
840                 case GDK_KEY_d:  /* download manager (left hand) */
841                     gtk_widget_show_all(dm.win);
842                     return TRUE;
843                 case GDK_KEY_2:  /* search forward (left hand) */
844                 case GDK_KEY_n:  /* search forward (maybe both hands) */
845                     search(c, 1);
846                     return TRUE;
847                 case GDK_KEY_3:  /* search backward (left hand) */
848                     search(c, -1);
849                     return TRUE;
850                 case GDK_KEY_l:  /* location (BOTH hands) */
851                     gtk_widget_grab_focus(c->location);
852                     return TRUE;
853                 case GDK_KEY_k:  /* initiate search (BOTH hands) */
854                     gtk_widget_grab_focus(c->location);
855                     gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
856                     gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
857                     return TRUE;
858                 case GDK_KEY_c:  /* reload trusted certs (left hand) */
859                     trust_user_certs(wc);
860                     return TRUE;
861                 case GDK_KEY_x:  /* launch external handler (left hand) */
862                     if (c->external_handler_uri != NULL)
863                         g_free(c->external_handler_uri);
864                     c->external_handler_uri = g_strdup(
865                         webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
866                     external_handler_run(NULL, NULL, c);
867                     return TRUE;
868             }
869         }
870         /* navigate backward (left hand) */
871         else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
872         {
873             webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
874             return TRUE;
875         }
876         /* navigate forward (left hand) */
877         else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
878         {
879             webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
880             return TRUE;
881         }
882     }
883
884     return FALSE;
885 }
886
887 gboolean
888 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
889 {
890     if (event->type == GDK_KEY_PRESS)
891     {
892         if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
893         {
894             switch (((GdkEventKey *)event)->keyval)
895             {
896                 case GDK_KEY_d:  /* close window (left hand) */
897                 case GDK_KEY_q:
898                     downloadmanager_delete(dm.win, NULL);
899                     return TRUE;
900             }
901         }
902     }
903
904     return FALSE;
905 }
906
907 gboolean
908 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
909 {
910     struct Client *c = (struct Client *)data;
911     const gchar *t;
912     gchar *f;
913
914     if (key_common(widget, event, data))
915         return TRUE;
916
917     if (event->type == GDK_KEY_PRESS)
918     {
919         switch (((GdkEventKey *)event)->keyval)
920         {
921             case GDK_KEY_KP_Enter:
922             case GDK_KEY_Return:
923                 gtk_widget_grab_focus(c->web_view);
924                 t = gtk_entry_get_text(GTK_ENTRY(c->location));
925                 if (t != NULL && t[0] == ':' && t[1] == '/')
926                 {
927                     if (search_text != NULL)
928                         g_free(search_text);
929                     search_text = g_strdup(t + 2);
930                     search(c, 0);
931                 }
932                 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
933                 {
934                     f = ensure_uri_scheme(t);
935                     webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
936                     g_free(f);
937                 }
938                 return TRUE;
939             case GDK_KEY_Escape:
940                 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
941                 gtk_entry_set_text(GTK_ENTRY(c->location),
942                                    (t == NULL ? __NAME__ : t));
943                 return TRUE;
944         }
945     }
946
947     return FALSE;
948 }
949
950 gboolean
951 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
952 {
953     struct Client *c = (struct Client *)data;
954     gdouble dx, dy;
955     gfloat z;
956
957     if (key_common(widget, event, data))
958         return TRUE;
959
960     if (event->type == GDK_KEY_PRESS)
961     {
962         if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
963         {
964             webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
965             gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
966         }
967     }
968     else if (event->type == GDK_BUTTON_RELEASE)
969     {
970         switch (((GdkEventButton *)event)->button)
971         {
972             case 2:
973                 if (c->hover_uri != NULL)
974                 {
975                     client_new(c->hover_uri, NULL, TRUE);
976                     return TRUE;
977                 }
978                 break;
979             case 8:
980                 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
981                 return TRUE;
982             case 9:
983                 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
984                 return TRUE;
985         }
986     }
987     else if (event->type == GDK_SCROLL)
988     {
989         if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
990             ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
991         {
992             gdk_event_get_scroll_deltas(event, &dx, &dy);
993             z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
994             z += -dy * 0.1;
995             z = dx != 0 ? global_zoom : z;
996             webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
997             return TRUE;
998         }
999     }
1000
1001     return FALSE;
1002 }
1003
1004 void
1005 keywords_load(void)
1006 {
1007     GError *err = NULL;
1008     GIOChannel *channel = NULL;
1009     gchar *path = NULL, *buf = NULL;
1010     gchar **tokens = NULL;
1011
1012     keywords = g_hash_table_new(g_str_hash, g_str_equal);
1013
1014     path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
1015                             NULL);
1016     channel = g_io_channel_new_file(path, "r", &err);
1017     if (channel != NULL)
1018     {
1019         while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
1020                == G_IO_STATUS_NORMAL)
1021         {
1022             g_strstrip(buf);
1023             if (buf[0] != '#')
1024             {
1025                 tokens = g_strsplit(buf, " ", 2);
1026                 if (tokens[0] != NULL && tokens[1] != NULL)
1027                     g_hash_table_insert(keywords, g_strdup(tokens[0]),
1028                                         g_strdup(tokens[1]));
1029                 g_strfreev(tokens);
1030             }
1031             g_free(buf);
1032         }
1033         g_io_channel_shutdown(channel, FALSE, NULL);
1034     }
1035     g_free(path);
1036 }
1037
1038 gboolean
1039 keywords_try_search(WebKitWebView *web_view, const gchar *t)
1040 {
1041     gboolean ret = FALSE;
1042     gchar **tokens = NULL;
1043     gchar *val = NULL, *escaped = NULL, *uri = NULL;
1044
1045     tokens = g_strsplit(t, " ", 2);
1046     if (tokens[0] != NULL && tokens[1] != NULL)
1047     {
1048         val = g_hash_table_lookup(keywords, tokens[0]);
1049         if (val != NULL)
1050         {
1051             escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
1052             uri = g_strdup_printf((gchar *)val, escaped);
1053             webkit_web_view_load_uri(web_view, uri);
1054             g_free(uri);
1055             g_free(escaped);
1056             ret = TRUE;
1057         }
1058     }
1059     g_strfreev(tokens);
1060
1061     return ret;
1062 }
1063
1064 gboolean
1065 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1066               WebKitHitTestResult *ht, gpointer data)
1067 {
1068     struct Client *c = (struct Client *)data;
1069     GSimpleAction *action = NULL;
1070     WebKitContextMenuItem *mi = NULL;
1071     const gchar *uri = NULL;
1072
1073     (void)ev;
1074
1075     if (webkit_hit_test_result_context_is_link(ht))
1076         uri = webkit_hit_test_result_get_link_uri(ht);
1077     else if (webkit_hit_test_result_context_is_image(ht))
1078         uri = webkit_hit_test_result_get_image_uri(ht);
1079     else if (webkit_hit_test_result_context_is_media(ht))
1080         uri = webkit_hit_test_result_get_media_uri(ht);
1081
1082     if (uri != NULL)
1083     {
1084         webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1085
1086         if (c->external_handler_uri != NULL)
1087             g_free(c->external_handler_uri);
1088         c->external_handler_uri = g_strdup(uri);
1089         action = g_simple_action_new("external_handler", NULL);
1090         g_signal_connect(G_OBJECT(action), "activate",
1091                          G_CALLBACK(external_handler_run), data);
1092         mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1093                                                        "Open with external handler",
1094                                                        NULL);
1095         webkit_context_menu_append(menu, mi);
1096         g_object_unref(action);
1097     }
1098
1099     /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1100     return FALSE;
1101 }
1102
1103 gboolean
1104 quit_if_nothing_active(void)
1105 {
1106     if (clients == 0)
1107     {
1108         if (downloads == 0)
1109         {
1110             gtk_main_quit();
1111             return TRUE;
1112         }
1113         else
1114             gtk_widget_show_all(dm.win);
1115     }
1116
1117     return FALSE;
1118 }
1119
1120 gboolean
1121 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1122 {
1123     gchar *uri = NULL;
1124
1125     g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1126     if (uri)
1127     {
1128         g_strstrip(uri);
1129         client_new(uri, NULL, TRUE);
1130         g_free(uri);
1131     }
1132     return TRUE;
1133 }
1134
1135 void
1136 run_user_scripts(WebKitWebView *web_view)
1137 {
1138     gchar *base = NULL, *path = NULL, *contents = NULL;
1139     const gchar *entry = NULL;
1140     GDir *scriptdir = NULL;
1141
1142     base = g_build_filename(g_get_user_config_dir(), __NAME__, "scripts", NULL);
1143     scriptdir = g_dir_open(base, 0, NULL);
1144     if (scriptdir != NULL)
1145     {
1146         while ((entry = g_dir_read_name(scriptdir)) != NULL)
1147         {
1148             path = g_build_filename(base, entry, NULL);
1149             if (g_str_has_suffix(path, ".js"))
1150             {
1151                 if (g_file_get_contents(path, &contents, NULL, NULL))
1152                 {
1153                     webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1154                     g_free(contents);
1155                 }
1156             }
1157             g_free(path);
1158         }
1159         g_dir_close(scriptdir);
1160     }
1161
1162     g_free(base);
1163 }
1164
1165 void
1166 search(gpointer data, gint direction)
1167 {
1168     struct Client *c = (struct Client *)data;
1169     WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1170     WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1171
1172     if (search_text == NULL)
1173         return;
1174
1175     switch (direction)
1176     {
1177         case 0:
1178             webkit_find_controller_search(fc, search_text,
1179                                           WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1180                                           WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1181                                           G_MAXUINT);
1182             break;
1183         case 1:
1184             webkit_find_controller_search_next(fc);
1185             break;
1186         case -1:
1187             webkit_find_controller_search_previous(fc);
1188             break;
1189     }
1190 }
1191
1192 void
1193 show_web_view(WebKitWebView *web_view, gpointer data)
1194 {
1195     struct Client *c = (struct Client *)data;
1196
1197     (void)web_view;
1198
1199     gtk_widget_grab_focus(c->web_view);
1200     gtk_widget_show_all(c->win);
1201 }
1202
1203 Window
1204 tabbed_launch(void)
1205 {
1206     gint tabbed_stdout;
1207     GIOChannel *tabbed_stdout_channel;
1208     GError *err = NULL;
1209     gchar *output = NULL;
1210     char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
1211     Window plug_into;
1212
1213     if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
1214                                   NULL, NULL, NULL, &tabbed_stdout, NULL,
1215                                   &err))
1216     {
1217         fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
1218         g_error_free(err);
1219         return 0;
1220     }
1221
1222     tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
1223     if (tabbed_stdout_channel == NULL)
1224     {
1225         fprintf(stderr, __NAME__": Could open tabbed's stdout\n");
1226         return 0;
1227     }
1228     g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
1229     g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
1230     if (output == NULL)
1231     {
1232         fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
1233         return 0;
1234     }
1235     g_strstrip(output);
1236     plug_into = strtol(output, NULL, 16);
1237     g_free(output);
1238     if (plug_into == 0)
1239         fprintf(stderr, __NAME__": The XID from tabbed is 0\n");
1240     return plug_into;
1241 }
1242
1243 void
1244 trust_user_certs(WebKitWebContext *wc)
1245 {
1246     GTlsCertificate *cert;
1247     const gchar *basedir, *file, *absfile;
1248     GDir *dir;
1249
1250     basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1251     dir = g_dir_open(basedir, 0, NULL);
1252     if (dir != NULL)
1253     {
1254         file = g_dir_read_name(dir);
1255         while (file != NULL)
1256         {
1257             absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1258                                        file, NULL);
1259             cert = g_tls_certificate_new_from_file(absfile, NULL);
1260             if (cert == NULL)
1261                 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1262             else
1263                 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1264             file = g_dir_read_name(dir);
1265         }
1266         g_dir_close(dir);
1267     }
1268 }
1269
1270
1271 int
1272 main(int argc, char **argv)
1273 {
1274     gchar *c;
1275     int opt, i;
1276
1277     gtk_init(&argc, &argv);
1278     webkit_web_context_set_process_model(webkit_web_context_get_default(),
1279         WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1280
1281     grab_environment_configuration();
1282
1283     while ((opt = getopt(argc, argv, "e:CT")) != -1)
1284     {
1285         switch (opt)
1286         {
1287             case 'e':
1288                 embed = atol(optarg);
1289                 tabbed_automagic = FALSE;
1290                 break;
1291             case 'C':
1292                 cooperative_instances = FALSE;
1293                 break;
1294             case 'T':
1295                 tabbed_automagic = FALSE;
1296                 break;
1297             default:
1298                 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1299                 exit(EXIT_FAILURE);
1300         }
1301     }
1302
1303     keywords_load();
1304     if (cooperative_instances)
1305         cooperation_setup();
1306     downloadmanager_setup();
1307
1308     if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
1309         embed = tabbed_launch();
1310
1311     if (!cooperative_instances || cooperative_alone)
1312     {
1313         c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1314                              NULL);
1315         webkit_web_context_set_web_extensions_directory(
1316             webkit_web_context_get_default(), c
1317         );
1318     }
1319
1320     if (optind >= argc)
1321         client_new(home_uri, NULL, TRUE);
1322     else
1323     {
1324         for (i = optind; i < argc; i++)
1325             client_new(argv[i], NULL, TRUE);
1326     }
1327
1328     if (!cooperative_instances || cooperative_alone)
1329         gtk_main();
1330     exit(EXIT_SUCCESS);
1331 }