6 #include <webkit2/webkit2.h>
11 WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
13 gboolean crashed_web_view(WebKitWebView *, gpointer);
14 gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
15 WebKitPolicyDecisionType, gpointer);
16 gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
17 gboolean key_tablabel(GtkWidget *, GdkEvent *, gpointer);
18 gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
19 gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
20 gchar *ensure_uri_scheme(const gchar *);
21 void changed_favicon(GObject *, GParamSpec *, gpointer);
22 void changed_load_progress(GObject *, GParamSpec *, gpointer);
23 void changed_title(GObject *, GParamSpec *, gpointer);
24 void changed_uri(GObject *, GParamSpec *, gpointer);
25 void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
26 void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
27 void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
28 void mainwindow_title(gint);
29 void notebook_switch_page(GtkNotebook *, GtkWidget *, guint, gpointer);
30 void show_web_view(WebKitWebView *, gpointer);
31 void trust_user_certs(WebKitWebContext *);
40 WebKitSettings *settings;
41 gboolean focus_new_tab;
42 gchar *external_handler_uri;
52 struct Configuration {
53 gboolean cooperative_alone;
54 gboolean noncooperative_instances;
60 struct Client **client_arr;
62 int cooperative_pipe_fp = 0;
66 size_t num_closed = 0;
71 fprintf(stderr, "chorizo: fatal: alloc failed\n");
76 togglejs(GtkButton *jsbutton, gpointer data)
78 struct Client *c = (struct Client *)data;
79 webkit_settings_set_enable_javascript(
81 !webkit_settings_get_enable_javascript(c->settings));
82 webkit_web_view_set_settings(WEBKIT_WEB_VIEW(c->web_view), c->settings);
86 client_destroy(GtkWidget *widget, gpointer data)
88 struct Client *c = (struct Client *)data;
90 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
91 changed_load_progress, c);
93 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
95 fprintf(stderr, "chorizo: warning: tab index was -1\n");
97 gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx);
99 if (!cfg.private && WEBKIT_IS_WEB_VIEW(c->web_view)) {
101 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
103 // TODO: Shift everything left if over certain amount
105 if (num_closed > cfg_max_tabs_closed) {
106 memmove(closed_tabs, closed_tabs,
107 cfg_max_tabs_closed - 1);
108 num_closed = cfg_max_tabs_closed;
112 num_closed * sizeof(closed_tabs[0]));
113 if (!closed_tabs) allocfail();
115 closed_tabs[num_closed - 1] = strdup(uri);
121 quit_if_nothing_active();
125 set_uri(const char *uri, struct Client *c)
127 if (!gtk_widget_is_focus(c->location))
128 gtk_entry_set_text(GTK_ENTRY(c->location),
129 (uri != NULL) ? uri : "");
133 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
138 GtkWidget *evbox, *tabbox;
139 if (uri != NULL && !cfg.noncooperative_instances &&
140 !cfg.cooperative_alone) {
141 f = ensure_uri_scheme(uri);
142 write(cooperative_pipe_fp, f, strlen(f));
143 write(cooperative_pipe_fp, "\n", 1);
147 c = calloc(1, sizeof(struct Client));
149 c->focus_new_tab = focus_tab;
151 if (related_wv == NULL) {
152 WebKitUserContentManager *ucm =
153 webkit_user_content_manager_new();
154 WebKitUserScript *wkscript;
155 WebKitUserStyleSheet *wkstyle;
156 gchar *path = NULL, *source, *base;
157 const gchar *entry = NULL;
159 base = g_build_filename(g_get_user_data_dir(), "chorizo",
160 "user-scripts", NULL);
161 dir = g_dir_open(base, 0, NULL);
163 while ((entry = g_dir_read_name(dir)) != NULL) {
164 path = g_build_filename(base, entry, NULL);
165 if (g_str_has_suffix(path, ".js")) {
166 g_file_get_contents(path, &source, NULL,
168 wkscript = webkit_user_script_new(
170 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
171 WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START,
173 webkit_user_content_manager_add_script(
175 webkit_user_script_unref(wkscript);
178 if (source) g_free(source);
182 base = g_build_filename(g_get_user_data_dir(), "chorizo",
183 "user-styles", NULL);
184 dir = g_dir_open(base, 0, NULL);
186 while ((entry = g_dir_read_name(dir)) != NULL) {
187 path = g_build_filename(base, entry, NULL);
188 if (g_str_has_suffix(path, ".css")) {
189 g_file_get_contents(path, &source, NULL,
191 wkstyle = webkit_user_style_sheet_new(
193 WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES,
194 WEBKIT_USER_STYLE_LEVEL_USER,
196 webkit_user_content_manager_add_style_sheet(
198 webkit_user_style_sheet_unref(wkstyle);
208 webkit_web_view_new_with_user_content_manager(ucm);
210 c->web_view = webkit_web_view_new_with_related_view(related_wv);
214 webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view));
215 webkit_settings_set_enable_javascript(c->settings, cfg_js_default);
217 webkit_settings_set_enable_write_console_messages_to_stdout(
219 webkit_settings_set_enable_developer_extras(c->settings, TRUE);
221 g_signal_connect(G_OBJECT(c->web_view), "notify::favicon",
222 G_CALLBACK(changed_favicon), c);
223 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
224 G_CALLBACK(changed_title), c);
225 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
226 G_CALLBACK(changed_uri), c);
227 g_signal_connect(G_OBJECT(c->web_view),
228 "notify::estimated-load-progress",
229 G_CALLBACK(changed_load_progress), c);
230 g_signal_connect(G_OBJECT(c->web_view), "create",
231 G_CALLBACK(client_new_request), NULL);
232 g_signal_connect(G_OBJECT(c->web_view), "close",
233 G_CALLBACK(client_destroy), c);
234 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
235 G_CALLBACK(decide_policy), NULL);
236 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
237 G_CALLBACK(key_web_view), c);
238 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
239 G_CALLBACK(key_web_view), c);
240 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
241 G_CALLBACK(key_web_view), c);
242 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
243 G_CALLBACK(hover_web_view), c);
244 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
245 G_CALLBACK(crashed_web_view), c);
247 GtkWidget *locbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
248 c->jsbutton = gtk_toggle_button_new_with_label("JS");
249 gtk_widget_set_tooltip_text(c->jsbutton, "Toggle JavaScript execution");
250 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(c->jsbutton),
252 g_signal_connect(G_OBJECT(c->jsbutton), "toggled", G_CALLBACK(togglejs),
255 c->location = gtk_entry_new();
256 gtk_box_pack_start(GTK_BOX(locbox), c->location, TRUE, TRUE, 0);
259 GtkWidget *privindicator = gtk_label_new("Private mode");
260 gtk_widget_set_tooltip_text(
262 "You are in private mode. No history, caches, or "
263 "cookies will be saved beyond this session.");
264 gtk_box_pack_end(GTK_BOX(locbox), privindicator, FALSE, FALSE,
267 gtk_box_pack_start(GTK_BOX(locbox), c->jsbutton, FALSE, FALSE, 5);
269 g_signal_connect(G_OBJECT(c->location), "key-press-event",
270 G_CALLBACK(key_location), c);
271 g_signal_connect(G_OBJECT(c->location), "icon-release",
272 G_CALLBACK(icon_location), c);
274 * XXX This is a workaround. Setting this to NULL (which is done in
275 * grab_feeds_finished() if no feed has been detected) adds a little
276 * padding left of the text. Not sure why. The point of this call
277 * right here is to have that padding right from the start. This
278 * avoids a graphical artifact.
281 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
282 GTK_ENTRY_ICON_SECONDARY, NULL);
284 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
285 gtk_box_pack_start(GTK_BOX(c->vbox), locbox, FALSE, FALSE, 0);
286 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
287 gtk_container_set_focus_child(GTK_CONTAINER(c->vbox), c->web_view);
289 c->tabicon = gtk_image_new_from_icon_name("text-html",
290 GTK_ICON_SIZE_SMALL_TOOLBAR);
292 c->tablabel = gtk_label_new("chorizo");
293 gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
294 gtk_label_set_width_chars(GTK_LABEL(c->tablabel), 20);
295 gtk_widget_set_has_tooltip(c->tablabel, TRUE);
298 * XXX I don't own a HiDPI screen, so I don't know if scale_factor
299 * does the right thing.
301 tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
302 5 * gtk_widget_get_scale_factor(mw.win));
303 gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0);
304 gtk_box_pack_start(GTK_BOX(tabbox), c->tablabel, TRUE, TRUE, 0);
306 evbox = gtk_event_box_new();
307 gtk_container_add(GTK_CONTAINER(evbox), tabbox);
308 g_signal_connect(G_OBJECT(evbox), "button-release-event",
309 G_CALLBACK(key_tablabel), c);
311 gtk_widget_add_events(evbox, GDK_SCROLL_MASK);
312 g_signal_connect(G_OBJECT(evbox), "scroll-event",
313 G_CALLBACK(key_tablabel), c);
315 //For easy access, store a reference to our label.
316 g_object_set_data(G_OBJECT(evbox), "chorizo-tab-label", c->tablabel);
319 * This only shows the event box and the label inside, nothing else.
320 * Needed because the evbox/label is "internal" to the notebook and
321 * not part of the normal "widget tree" (IIUC).
323 gtk_widget_show_all(evbox);
325 int page = gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)) + 1;
326 gtk_notebook_insert_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox,
328 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox,
332 show_web_view(NULL, c);
334 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
335 G_CALLBACK(show_web_view), c);
338 f = ensure_uri_scheme(uri);
339 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
345 client_arr = realloc(client_arr, (clients + 1) * sizeof(client_arr[0]));
346 if (!client_arr) allocfail();
348 client_arr[clients] = c;
350 if (clients == 1 || uri == NULL) gtk_widget_grab_focus(c->location);
352 return WEBKIT_WEB_VIEW(c->web_view);
356 client_new_request(WebKitWebView *web_view,
357 WebKitNavigationAction *navigation_action, gpointer data)
359 return client_new(NULL, web_view, FALSE, FALSE);
363 mkdirp(const char *dir, mode_t mode)
368 snprintf(tmp, sizeof(tmp), "%s", dir);
370 if (tmp[len - 1] == '/') tmp[len - 1] = 0;
371 for (p = tmp + 1; *p; p++)
381 cooperation_setup(void)
386 gchar *priv = (cfg.private) ? "-private" : "";
387 const gchar *fifo_suffix_env = g_getenv("CHORIZO_FIFO_SUFFIX");
388 const gchar *fifo_suffix = (fifo_suffix_env) ? fifo_suffix_env : "";
389 fifofilename = g_strdup_printf("%s%s%s%s", "chorizo", priv, ".fifo",
391 fifopath = g_build_filename(g_get_user_runtime_dir(), "chorizo",
393 mkdirp(dirname(fifopath), 0600);
394 g_free(fifofilename);
396 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS)) mkfifo(fifopath, 0600);
398 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
399 if (!cooperative_pipe_fp) {
400 fprintf(stderr, "chorizo: error: can't open FIFO\n");
402 if (write(cooperative_pipe_fp, "", 0) == -1) {
404 * Could not do an empty write to the FIFO which
405 * means there's no one listening.
407 close(cooperative_pipe_fp);
408 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
409 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg,
412 cfg.cooperative_alone = FALSE;
418 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
420 struct Client *c = (struct Client *)data;
423 "a = document.querySelectorAll('"
425 "link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
427 "link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
433 " for (i = 0; i < a.length; i++) {"
434 " url = encodeURIComponent(a[i].href);"
435 " if ('title' in a[i] && a[i].title != '')"
436 " title = encodeURIComponent(a[i].title);"
439 " out += '<li><a href=\"' + url + '\">' + title + "
444 p = webkit_web_view_get_estimated_load_progress(
445 WEBKIT_WEB_VIEW(c->web_view));
450 * The page has loaded fully. We now run the short JavaScript
451 * snippet above that operates on the DOM. It tries to grab
452 * all occurences of <link rel="alternate" ...>, i.e.
453 * RSS/Atom feed references.
455 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
457 grab_feeds_finished, c);
459 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
463 changed_favicon(GObject *obj, GParamSpec *pspec, gpointer data)
465 struct Client *c = (struct Client *)data;
467 int w, h, w_should, h_should;
468 GdkPixbuf *pb, *pb_scaled;
469 f = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(c->web_view));
471 gtk_image_set_from_icon_name(GTK_IMAGE(c->tabicon), "text-html",
472 GTK_ICON_SIZE_SMALL_TOOLBAR);
474 w = cairo_image_surface_get_width(f);
475 h = cairo_image_surface_get_height(f);
476 pb = gdk_pixbuf_get_from_surface(f, 0, 0, w, h);
478 w_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
479 h_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
480 pb_scaled = gdk_pixbuf_scale_simple(
481 pb, w_should, h_should, GDK_INTERP_BILINEAR);
482 gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon),
485 g_object_unref(pb_scaled);
492 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
495 struct Client *c = (struct Client *)data;
496 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
497 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
499 u = u == NULL ? "chorizo" : u;
500 u = u[0] == 0 ? "chorizo" : u;
502 t = t == NULL ? u : t;
503 t = t[0] == 0 ? u : t;
505 gchar *name = malloc(strlen(t) + 4);
506 if (!name) allocfail();
509 webkit_web_view_get_is_muted(WEBKIT_WEB_VIEW(c->web_view));
510 gchar *muted = (mute) ? "[m] " : "";
511 sprintf(name, "%s%s", muted, t);
512 gtk_label_set_text(GTK_LABEL(c->tablabel), name);
515 gtk_widget_set_tooltip_text(c->tablabel, t);
517 gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)));
521 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
524 struct Client *c = (struct Client *)data;
526 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
529 * When a web process crashes, we get a "notify::uri" signal, but we
530 * can no longer read a meaningful URI. It's just an empty string
531 * now. Not updating the location bar in this scenario is important,
532 * because we would override the "WEB PROCESS CRASHED" message.
534 if (t != NULL && strlen(t) > 0) {
537 //No g_get_user_state_dir unfortunately
538 gchar *state_env = getenv("XDG_STATE_DIR");
539 gchar *state_dir = (state_env) ?
541 g_build_filename(g_get_home_dir(),
545 gchar *history_file =
546 g_build_filename(state_dir, "history", NULL);
548 mkdirp(state_dir, 0700);
549 fp = fopen(history_file, "a");
551 fprintf(fp, "%s\n", t);
554 perror("chorizo: error: could not open history file");
557 g_free(history_file);
562 crashed_web_view(WebKitWebView *web_view, gpointer data)
565 struct Client *c = (struct Client *)data;
566 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
567 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
568 gtk_entry_set_text(GTK_ENTRY(c->location), t);
574 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
575 WebKitPolicyDecisionType type, gpointer data)
577 WebKitResponsePolicyDecision *r;
579 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
580 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
581 if (!webkit_response_policy_decision_is_mime_type_supported(r))
582 webkit_policy_decision_download(decision);
584 webkit_policy_decision_use(decision);
587 //Use whatever default there is.
594 ensure_uri_scheme(const gchar *t)
597 f = g_ascii_strdown(t, -1);
598 if (!g_str_has_prefix(f, "http:") && !g_str_has_prefix(f, "https:") &&
599 !g_str_has_prefix(f, "file:") && !g_str_has_prefix(f, "about:") &&
600 !g_str_has_prefix(f, "data:") && !g_str_has_prefix(f, "webkit:")) {
602 fabs = realpath(t, NULL);
604 f = g_strdup_printf("file://%s", fabs);
607 f = g_strdup_printf("http://%s", t);
615 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
617 struct Client *c = (struct Client *)data;
618 WebKitJavascriptResult *js_result;
620 JSCException *exception;
623 g_free(c->feed_html);
627 * This was taken almost verbatim from the example in WebKit's
630 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html
633 js_result = webkit_web_view_run_javascript_finish(
634 WEBKIT_WEB_VIEW(object), result, &err);
637 "chorizo: error: error running javascript: %s\n",
642 value = webkit_javascript_result_get_js_value(js_result);
643 if (jsc_value_is_string(value)) {
644 str_value = jsc_value_to_string(value);
646 jsc_context_get_exception(jsc_value_get_context(value));
647 if (exception != NULL) {
649 "chorizo: warning: error running javascript: %s\n",
650 jsc_exception_get_message(exception));
652 c->feed_html = str_value;
655 gtk_entry_set_icon_from_icon_name(
656 GTK_ENTRY(c->location), GTK_ENTRY_ICON_SECONDARY,
657 "application-rss+xml-symbolic");
658 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
659 GTK_ENTRY_ICON_SECONDARY, TRUE);
661 gtk_entry_set_icon_from_icon_name(
662 GTK_ENTRY(c->location), GTK_ENTRY_ICON_SECONDARY, NULL);
665 webkit_javascript_result_unref(js_result);
669 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht,
670 guint modifiers, gpointer data)
672 struct Client *c = (struct Client *)data;
674 g_free(c->hover_uri);
676 if (webkit_hit_test_result_context_is_link(ht)) {
677 to_show = webkit_hit_test_result_get_link_uri(ht);
678 c->hover_uri = g_strdup(to_show);
680 to_show = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
684 if (!gtk_widget_is_focus(c->location)) set_uri(to_show, c);
688 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
691 struct Client *c = (struct Client *)data;
693 gchar *data_template = "data:text/html,"
697 " <meta charset=\"UTF-8\">"
698 " <title>Feeds</title>"
701 " <p>Feeds found on this page:</p>"
707 if (c->feed_html != NULL) {
709 * What we're actually trying to do is show a simple HTML
710 * page that lists all the feeds on the current page. The
711 * function webkit_web_view_load_html() looks like the proper
712 * way to do that. Sad thing is, it doesn't create a history
713 * entry, but instead simply replaces the content of the
714 * current page. This is not what we want.
716 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
717 * mechanism to show my custom HTML snippet* and*create a
720 * [0]: https://tools.ietf.org/html/rfc2397 [1]:
721 * https://en.wikipedia.org/wiki/Data_URI_scheme
724 d = g_strdup_printf(data_template, c->feed_html);
725 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
731 init_default_web_context(void)
734 WebKitWebContext *wc;
735 WebKitCookieManager *cm;
736 wc = (cfg.private) ? webkit_web_context_new_ephemeral() :
737 webkit_web_context_get_default();
739 p = g_build_filename(g_get_user_config_dir(), "chorizo", "adblock",
741 webkit_web_context_set_sandbox_enabled(wc, TRUE);
742 webkit_web_context_add_path_to_sandbox(wc, p, TRUE);
745 WebKitProcessModel model =
746 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES;
747 webkit_web_context_set_process_model(wc, model);
749 p = g_build_filename(g_get_user_data_dir(), "chorizo", "web-extensions",
751 webkit_web_context_set_web_extensions_directory(wc, p);
754 char *xdg_down = getenv("XDG_DOWNLOAD_DIR");
755 g_signal_connect(G_OBJECT(wc), "download-started",
756 G_CALLBACK(download_start),
757 (xdg_down) ? xdg_down : "/var/tmp");
759 trust_user_certs(wc);
761 cm = webkit_web_context_get_cookie_manager(wc);
762 webkit_cookie_manager_set_accept_policy(cm, cfg_cookie_policy);
765 webkit_web_context_set_favicon_database_directory(wc, NULL);
767 gchar *fname = g_build_filename("/", g_get_user_data_dir(),
768 "chorizo", "cookies.db", NULL);
769 mkdirp(dirname(fname), 0700);
770 WebKitCookiePersistentStorage type =
771 WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE;
772 webkit_cookie_manager_set_persistent_storage(cm, fname, type);
775 webkit_web_context_set_spell_checking_enabled(wc, TRUE);
779 search(gpointer data, gint direction)
781 struct Client *c = (struct Client *)data;
782 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
783 WebKitFindController *fc =
784 webkit_web_view_get_find_controller(web_view);
785 if (search_text == NULL) return;
789 webkit_find_controller_search(
791 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
792 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
796 webkit_find_controller_search_next(fc);
799 webkit_find_controller_search_previous(fc);
802 webkit_find_controller_search_finish(fc);
808 search_init(struct Client *c, int direction)
810 gtk_widget_grab_focus(c->location);
811 const gchar *contents = gtk_entry_get_text(GTK_ENTRY(c->location));
812 if (strcspn(contents, "s/")) {
813 gtk_entry_set_text(GTK_ENTRY(c->location), "s/");
814 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
818 search(c, direction);
822 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
824 struct Client *c = (struct Client *)data;
826 if (event->type == GDK_KEY_PRESS) {
827 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK) {
828 const char *uri = webkit_web_view_get_uri(
829 WEBKIT_WEB_VIEW(c->web_view));
830 int key = ((GdkEventKey *)event)->keyval;
831 if (GDK_KEY_y == key) {
832 downloadmanager_show();
834 } else if (GDK_KEY_h == key) {
835 webkit_web_view_go_back(
836 WEBKIT_WEB_VIEW(c->web_view));
838 } else if (GDK_KEY_l == key) {
839 webkit_web_view_go_forward(
840 WEBKIT_WEB_VIEW(c->web_view));
842 } else if (GDK_KEY_o == key) {
843 gtk_widget_grab_focus(c->location);
845 } else if (GDK_KEY_Print == key) {
846 WebKitPrintOperation *operation =
847 webkit_print_operation_new(
848 WEBKIT_WEB_VIEW(c->web_view));
849 GtkWidget *toplevel =
850 gtk_widget_get_toplevel(mw.win);
851 webkit_print_operation_run_dialog(
852 operation, GTK_WINDOW(toplevel));
854 } else if (GDK_KEY_g == key) {
856 gtk_widget_grab_focus(c->web_view);
857 gtk_editable_set_position(
858 GTK_EDITABLE(c->location), -1);
861 GTK_ENTRY(c->location), uri);
862 webkit_web_view_run_javascript(
863 WEBKIT_WEB_VIEW(c->web_view),
864 "window.getSelection().removeAllRanges();"
865 "document.activeElement.blur();",
868 } else if (GDK_KEY_r == key) {
869 webkit_web_view_reload_bypass_cache(
870 WEBKIT_WEB_VIEW(c->web_view));
872 } else if (GDK_KEY_j == key) {
873 for (int i = 0; i <= cfg_scroll_lines - 1;
875 event->key.keyval = GDK_KEY_Down;
876 gdk_event_put(event);
879 } else if (GDK_KEY_k == key) {
880 for (int i = 0; i <= cfg_scroll_lines - 1;
882 event->key.keyval = GDK_KEY_Up;
883 gdk_event_put(event);
886 } else if (GDK_KEY_f == key) {
887 event->key.keyval = GDK_KEY_Page_Down;
888 gdk_event_put(event);
890 } else if (GDK_KEY_b == key) {
891 event->key.keyval = GDK_KEY_Page_Up;
892 gdk_event_put(event);
894 } else if (GDK_KEY_s == key) {
897 } else if (GDK_KEY_r == key) {
900 } else if (GDK_KEY_q == key) {
901 client_destroy(NULL, c);
903 } else if (GDK_KEY_1 == key) {
904 gtk_notebook_set_current_page(
905 GTK_NOTEBOOK(mw.notebook), 0);
907 } else if (GDK_KEY_2 == key) {
908 gtk_notebook_set_current_page(
909 GTK_NOTEBOOK(mw.notebook), 1);
911 } else if (GDK_KEY_3 == key) {
912 gtk_notebook_set_current_page(
913 GTK_NOTEBOOK(mw.notebook), 2);
915 } else if (GDK_KEY_4 == key) {
916 gtk_notebook_set_current_page(
917 GTK_NOTEBOOK(mw.notebook), 3);
919 } else if (GDK_KEY_5 == key) {
920 gtk_notebook_set_current_page(
921 GTK_NOTEBOOK(mw.notebook), 4);
923 } else if (GDK_KEY_6 == key) {
924 gtk_notebook_set_current_page(
925 GTK_NOTEBOOK(mw.notebook), 5);
927 } else if (GDK_KEY_7 == key) {
928 gtk_notebook_set_current_page(
929 GTK_NOTEBOOK(mw.notebook), 6);
931 } else if (GDK_KEY_8 == key) {
932 gtk_notebook_set_current_page(
933 GTK_NOTEBOOK(mw.notebook), 7);
935 } else if (GDK_KEY_9 == key) {
936 gtk_notebook_set_current_page(
937 GTK_NOTEBOOK(mw.notebook), 8);
939 } else if (GDK_KEY_u == key) {
940 gtk_notebook_prev_page(
941 GTK_NOTEBOOK(mw.notebook));
943 } else if (GDK_KEY_m == key) {
944 gboolean muted = webkit_web_view_get_is_muted(
945 WEBKIT_WEB_VIEW(c->web_view));
946 webkit_web_view_set_is_muted(
947 WEBKIT_WEB_VIEW(c->web_view), !muted);
948 changed_title(G_OBJECT(c->web_view), NULL, c);
950 } else if (GDK_KEY_t == key) {
951 client_new(cfg_home_uri, NULL, TRUE, TRUE);
953 } else if (GDK_KEY_bracketleft == key) {
954 if (num_closed == 0) return TRUE;
955 client_new(closed_tabs[num_closed - 1], NULL,
958 closed_tabs = realloc(
960 num_closed * sizeof(closed_tabs[0]));
961 if (!closed_tabs) allocfail();
963 } else if (GDK_KEY_i == key) {
964 gtk_notebook_next_page(
965 GTK_NOTEBOOK(mw.notebook));
967 } else if (GDK_KEY_p == key) {
969 webkit_settings_get_enable_javascript(
971 webkit_settings_set_enable_javascript(
973 webkit_web_view_set_settings(
974 WEBKIT_WEB_VIEW(c->web_view),
976 gtk_toggle_button_set_active(
977 GTK_TOGGLE_BUTTON(c->jsbutton), !on);
978 } else if (GDK_KEY_d == key) {
979 gtk_widget_grab_focus(c->location);
980 gtk_entry_set_text(GTK_ENTRY(c->location),
982 gtk_editable_set_position(
983 GTK_EDITABLE(c->location), -1);
985 } else if (GDK_KEY_equal == key) {
986 now = webkit_web_view_get_zoom_level(
987 WEBKIT_WEB_VIEW(c->web_view));
988 webkit_web_view_set_zoom_level(
989 WEBKIT_WEB_VIEW(c->web_view),
992 } else if (GDK_KEY_minus == key) {
993 now = webkit_web_view_get_zoom_level(
994 WEBKIT_WEB_VIEW(c->web_view));
995 webkit_web_view_set_zoom_level(
996 WEBKIT_WEB_VIEW(c->web_view),
999 } else if (GDK_KEY_0 == key) {
1000 webkit_web_view_set_zoom_level(
1001 WEBKIT_WEB_VIEW(c->web_view), 1);
1009 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
1011 struct Client *c = (struct Client *)data;
1013 if (key_common(widget, event, data)) return TRUE;
1015 if (event->type == GDK_KEY_PRESS) {
1016 int key = ((GdkEventKey *)event)->keyval;
1017 if ((GDK_KEY_KP_Enter == key) || (GDK_KEY_Return == key)) {
1018 gtk_widget_grab_focus(c->web_view);
1019 t = gtk_entry_get_text(GTK_ENTRY(c->location));
1020 if (t != NULL && t[0] == 's' && t[1] == '/') {
1021 if (search_text != NULL) g_free(search_text);
1022 search_text = g_strdup(t + 2);
1024 } else if (t != NULL && t[0] == 'w' && t[1] == '/') {
1025 int len = strlen(cfg_search_engine) +
1027 gchar *f = malloc(len + 1);
1028 if (!f) allocfail();
1029 snprintf(f, len + 1, "%s%s", cfg_search_engine,
1031 webkit_web_view_load_uri(
1032 WEBKIT_WEB_VIEW(c->web_view), f);
1035 webkit_web_view_load_uri(
1036 WEBKIT_WEB_VIEW(c->web_view),
1037 ensure_uri_scheme(t));
1040 } else if (GDK_KEY_Escape == key) {
1041 t = webkit_web_view_get_uri(
1042 WEBKIT_WEB_VIEW(c->web_view));
1043 gtk_entry_set_text(GTK_ENTRY(c->location),
1044 (t == NULL) ? "" : t);
1051 key_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data)
1053 GdkScrollDirection direction;
1054 if (event->type == GDK_BUTTON_RELEASE) {
1055 switch (((GdkEventButton *)event)->button) {
1057 client_destroy(NULL, data);
1060 } else if (event->type == GDK_SCROLL) {
1061 gdk_event_get_scroll_direction(event, &direction);
1062 switch (direction) {
1064 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
1066 case GDK_SCROLL_DOWN:
1067 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
1077 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
1079 struct Client *c = (struct Client *)data;
1082 if (key_common(widget, event, data)) return TRUE;
1084 if (event->type == GDK_KEY_PRESS) {
1085 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape) {
1086 webkit_web_view_stop_loading(
1087 WEBKIT_WEB_VIEW(c->web_view));
1088 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location),
1091 } else if (event->type == GDK_BUTTON_RELEASE) {
1092 GdkModifierType modifiers =
1093 gtk_accelerator_get_default_mod_mask();
1094 switch (((GdkEventButton *)event)->button) {
1096 if ((((GdkEventButton *)event)->state & modifiers) ==
1098 c->hover_uri != NULL) {
1099 client_new(c->hover_uri, NULL, TRUE, FALSE);
1104 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
1107 webkit_web_view_go_forward(
1108 WEBKIT_WEB_VIEW(c->web_view));
1111 } else if (event->type == GDK_SCROLL) {
1112 event->scroll.delta_y *= cfg_scroll_lines;
1113 if (((GdkEventScroll *)event)->state & GDK_CONTROL_MASK) {
1114 gdk_event_get_scroll_deltas(event, &dx, &dy);
1115 z = webkit_web_view_get_zoom_level(
1116 WEBKIT_WEB_VIEW(c->web_view));
1118 z = dx != 0 ? 1 : z;
1119 webkit_web_view_set_zoom_level(
1120 WEBKIT_WEB_VIEW(c->web_view), z);
1128 mainwindow_setup(void)
1130 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1131 gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
1132 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
1134 gchar *priv = (cfg.private) ? "-private" : "";
1135 gchar *title = malloc(strlen(priv) + 7);
1136 if (!title) allocfail();
1137 sprintf(title, "%s%s", "chorizo", priv);
1138 gtk_window_set_title(GTK_WINDOW(mw.win), title);
1141 mw.notebook = gtk_notebook_new();
1142 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
1143 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
1144 g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
1145 G_CALLBACK(notebook_switch_page), NULL);
1149 mainwindow_title(gint idx)
1151 GtkWidget *child, *widg, *tablabel;
1153 child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx);
1154 if (child == NULL) return;
1156 widg = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child);
1157 tablabel = (GtkWidget *)g_object_get_data(G_OBJECT(widg),
1158 "chorizo-tab-label");
1159 text = gtk_label_get_text(GTK_LABEL(tablabel));
1160 gtk_window_set_title(GTK_WINDOW(mw.win), text);
1164 notebook_switch_page(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data)
1166 mainwindow_title(idx);
1169 quit_if_nothing_active(void)
1172 if (downloads == 0) {
1176 downloadmanager_show();
1182 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1185 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1188 client_new(uri, NULL, TRUE, TRUE);
1195 show_web_view(WebKitWebView *web_view, gpointer data)
1197 struct Client *c = (struct Client *)data;
1201 gtk_widget_show_all(mw.win);
1203 if (c->focus_new_tab) {
1204 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
1206 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook),
1209 gtk_widget_grab_focus(c->web_view);
1214 trust_user_certs(WebKitWebContext *wc)
1216 GTlsCertificate *cert;
1217 gchar *basedir, *absfile;
1220 basedir = g_build_filename(g_get_user_data_dir(), "chorizo", "certs",
1222 dir = g_dir_open(basedir, 0, NULL);
1225 file = g_dir_read_name(dir);
1226 while (file != NULL) {
1227 absfile = g_build_filename(g_get_user_data_dir(),
1228 "chorizo", "certs", file,
1230 cert = g_tls_certificate_new_from_file(absfile, NULL);
1234 "chorizo: warning: could not load trusted cert: %s\n",
1237 webkit_web_context_allow_tls_certificate_for_host(
1239 file = g_dir_read_name(dir);
1248 printf("%s %s\n", "chorizo", VERSION);
1252 main(int argc, char **argv)
1257 cfg.noncooperative_instances = FALSE;
1258 cfg.cooperative_alone = TRUE;
1259 closed_tabs = malloc(0);
1260 if (!closed_tabs) allocfail();
1262 while ((opt = getopt(argc, argv, "cpvV")) != -1) {
1265 cfg.noncooperative_instances = TRUE;
1278 "usage: chorizo [OPTION]... [URI]...\n");
1283 if (cfg.verbose) version();
1285 gtk_init(&argc, &argv);
1287 //Keep clipboard contents after program closes
1288 gtk_clipboard_store(gtk_clipboard_get_for_display(
1289 gdk_display_get_default(), GDK_SELECTION_CLIPBOARD));
1291 if (!cfg.noncooperative_instances) cooperation_setup();
1293 if (cfg.noncooperative_instances || cfg.cooperative_alone)
1294 init_default_web_context();
1296 downloadmanager_setup();
1299 client_arr = malloc(sizeof(struct Client *));
1300 if (!client_arr) allocfail();
1302 if (optind >= argc) {
1303 client_new(cfg_home_uri, NULL, TRUE, TRUE);
1305 for (i = optind; i < argc; i++)
1306 client_new(argv[i], NULL, TRUE, TRUE);
1309 if (cfg.noncooperative_instances || cfg.cooperative_alone) {
1313 for (int i = 0; i < clients; i++) { free(&(client_arr[i])); }