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, 0);
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));
528 if (t != NULL && strlen(t) > 0) {
531 //No g_get_user_state_dir unfortunately
532 gchar *state_env = getenv("XDG_STATE_DIR");
533 gchar *state_dir = (state_env) ?
535 g_build_filename(g_get_home_dir(),
539 gchar *history_file =
540 g_build_filename(state_dir, "history", NULL);
542 mkdirp(state_dir, 0700);
543 fp = fopen(history_file, "a");
545 fprintf(fp, "%s\n", t);
548 perror("chorizo: error: could not open history file");
551 g_free(history_file);
557 crashed_web_view(WebKitWebView *web_view, gpointer data)
559 struct Client *c = (struct Client *)data;
560 GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT;
561 GtkWidget *dialog = gtk_message_dialog_new(
562 GTK_WINDOW(c->vbox), flags, GTK_MESSAGE_ERROR,
563 GTK_BUTTONS_CLOSE, "ERROR: Web process %s crashed.",
564 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)),
566 gtk_dialog_run(GTK_DIALOG(dialog));
567 gtk_widget_destroy(dialog);
572 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
573 WebKitPolicyDecisionType type, gpointer data)
575 WebKitResponsePolicyDecision *r;
577 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
578 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
579 if (!webkit_response_policy_decision_is_mime_type_supported(r))
580 webkit_policy_decision_download(decision);
582 webkit_policy_decision_use(decision);
585 //Use whatever default there is.
592 ensure_uri_scheme(const gchar *t)
595 f = g_ascii_strdown(t, -1);
596 if (!g_str_has_prefix(f, "http:") && !g_str_has_prefix(f, "https:") &&
597 !g_str_has_prefix(f, "file:") && !g_str_has_prefix(f, "about:") &&
598 !g_str_has_prefix(f, "data:") && !g_str_has_prefix(f, "webkit:")) {
600 fabs = realpath(t, NULL);
602 f = g_strdup_printf("file://%s", fabs);
605 f = g_strdup_printf("http://%s", t);
613 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
615 struct Client *c = (struct Client *)data;
616 WebKitJavascriptResult *js_result;
618 JSCException *exception;
621 g_free(c->feed_html);
625 * This was taken almost verbatim from the example in WebKit's
628 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html
631 js_result = webkit_web_view_run_javascript_finish(
632 WEBKIT_WEB_VIEW(object), result, &err);
635 "chorizo: error: error running javascript: %s\n",
640 value = webkit_javascript_result_get_js_value(js_result);
641 if (jsc_value_is_string(value)) {
642 str_value = jsc_value_to_string(value);
644 jsc_context_get_exception(jsc_value_get_context(value));
645 if (exception != NULL) {
647 "chorizo: warning: error running javascript: %s\n",
648 jsc_exception_get_message(exception));
650 c->feed_html = str_value;
653 gtk_entry_set_icon_from_icon_name(
654 GTK_ENTRY(c->location), GTK_ENTRY_ICON_SECONDARY,
655 "application-rss+xml-symbolic");
656 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
657 GTK_ENTRY_ICON_SECONDARY, TRUE);
659 gtk_entry_set_icon_from_icon_name(
660 GTK_ENTRY(c->location), GTK_ENTRY_ICON_SECONDARY, NULL);
663 webkit_javascript_result_unref(js_result);
667 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht,
668 guint modifiers, gpointer data)
670 struct Client *c = (struct Client *)data;
672 g_free(c->hover_uri);
674 if (webkit_hit_test_result_context_is_link(ht)) {
675 to_show = webkit_hit_test_result_get_link_uri(ht);
676 c->hover_uri = g_strdup(to_show);
678 to_show = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
682 if (!gtk_widget_is_focus(c->location)) set_uri(to_show, c);
686 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
689 struct Client *c = (struct Client *)data;
691 gchar *data_template = "data:text/html,"
695 " <meta charset=\"UTF-8\">"
696 " <title>Feeds</title>"
699 " <p>Feeds found on this page:</p>"
705 if (c->feed_html != NULL) {
707 * What we're actually trying to do is show a simple HTML
708 * page that lists all the feeds on the current page. The
709 * function webkit_web_view_load_html() looks like the proper
710 * way to do that. Sad thing is, it doesn't create a history
711 * entry, but instead simply replaces the content of the
712 * current page. This is not what we want.
714 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
715 * mechanism to show my custom HTML snippet* and*create a
718 * [0]: https://tools.ietf.org/html/rfc2397 [1]:
719 * https://en.wikipedia.org/wiki/Data_URI_scheme
722 d = g_strdup_printf(data_template, c->feed_html);
723 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
729 init_default_web_context(void)
732 WebKitWebContext *wc;
733 WebKitCookieManager *cm;
734 wc = (cfg.private) ? webkit_web_context_new_ephemeral() :
735 webkit_web_context_get_default();
737 p = g_build_filename(g_get_user_config_dir(), "chorizo", "adblock",
739 webkit_web_context_set_sandbox_enabled(wc, TRUE);
740 webkit_web_context_add_path_to_sandbox(wc, p, TRUE);
743 WebKitProcessModel model =
744 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES;
745 webkit_web_context_set_process_model(wc, model);
747 p = g_build_filename(g_get_user_data_dir(), "chorizo", "web-extensions",
749 webkit_web_context_set_web_extensions_directory(wc, p);
752 char *xdg_down = getenv("XDG_DOWNLOAD_DIR");
753 g_signal_connect(G_OBJECT(wc), "download-started",
754 G_CALLBACK(download_start),
755 (xdg_down) ? xdg_down : "/var/tmp");
757 trust_user_certs(wc);
759 cm = webkit_web_context_get_cookie_manager(wc);
760 webkit_cookie_manager_set_accept_policy(cm, cfg_cookie_policy);
763 webkit_web_context_set_favicon_database_directory(wc, NULL);
765 gchar *fname = g_build_filename("/", g_get_user_data_dir(),
766 "chorizo", "cookies.db", NULL);
767 mkdirp(dirname(fname), 0700);
768 WebKitCookiePersistentStorage type =
769 WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE;
770 webkit_cookie_manager_set_persistent_storage(cm, fname, type);
773 webkit_web_context_set_spell_checking_enabled(wc, TRUE);
777 search(gpointer data, gint direction)
779 struct Client *c = (struct Client *)data;
780 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
781 WebKitFindController *fc =
782 webkit_web_view_get_find_controller(web_view);
783 if (search_text == NULL) return;
787 webkit_find_controller_search(
789 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
790 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
794 webkit_find_controller_search_next(fc);
797 webkit_find_controller_search_previous(fc);
800 webkit_find_controller_search_finish(fc);
806 search_init(struct Client *c, int direction)
808 gtk_widget_grab_focus(c->location);
809 const gchar *contents = gtk_entry_get_text(GTK_ENTRY(c->location));
810 if (strcspn(contents, "s/")) {
811 gtk_entry_set_text(GTK_ENTRY(c->location), "s/");
812 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
816 search(c, direction);
820 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
822 struct Client *c = (struct Client *)data;
824 if (event->type == GDK_KEY_PRESS) {
825 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK) {
826 const char *uri = webkit_web_view_get_uri(
827 WEBKIT_WEB_VIEW(c->web_view));
828 int key = ((GdkEventKey *)event)->keyval;
829 if (GDK_KEY_y == key) {
830 downloadmanager_show();
832 } else if (GDK_KEY_h == key) {
833 webkit_web_view_go_back(
834 WEBKIT_WEB_VIEW(c->web_view));
836 } else if (GDK_KEY_l == key) {
837 webkit_web_view_go_forward(
838 WEBKIT_WEB_VIEW(c->web_view));
840 } else if (GDK_KEY_o == key) {
841 gtk_widget_grab_focus(c->location);
843 } else if (GDK_KEY_Print == key) {
844 WebKitPrintOperation *operation =
845 webkit_print_operation_new(
846 WEBKIT_WEB_VIEW(c->web_view));
847 GtkWidget *toplevel =
848 gtk_widget_get_toplevel(mw.win);
849 webkit_print_operation_run_dialog(
850 operation, GTK_WINDOW(toplevel));
852 } else if (GDK_KEY_g == key) {
854 gtk_widget_grab_focus(c->web_view);
855 gtk_editable_set_position(
856 GTK_EDITABLE(c->location), -1);
859 GTK_ENTRY(c->location), uri);
860 webkit_web_view_run_javascript(
861 WEBKIT_WEB_VIEW(c->web_view),
862 "window.getSelection().removeAllRanges();"
863 "document.activeElement.blur();",
866 } else if (GDK_KEY_semicolon == key) {
867 webkit_web_view_reload_bypass_cache(
868 WEBKIT_WEB_VIEW(c->web_view));
870 } else if (GDK_KEY_j == key) {
871 for (int i = 0; i <= cfg_scroll_lines - 1;
873 event->key.keyval = GDK_KEY_Down;
874 gdk_event_put(event);
877 } else if (GDK_KEY_k == key) {
878 for (int i = 0; i <= cfg_scroll_lines - 1;
880 event->key.keyval = GDK_KEY_Up;
881 gdk_event_put(event);
884 } else if (GDK_KEY_f == key) {
885 event->key.keyval = GDK_KEY_Page_Down;
886 gdk_event_put(event);
888 } else if (GDK_KEY_b == key) {
889 event->key.keyval = GDK_KEY_Page_Up;
890 gdk_event_put(event);
892 } else if (GDK_KEY_s == key) {
895 } else if (GDK_KEY_r == key) {
898 } else if (GDK_KEY_q == key) {
899 client_destroy(NULL, c);
901 } else if (GDK_KEY_1 == key) {
902 gtk_notebook_set_current_page(
903 GTK_NOTEBOOK(mw.notebook), 0);
905 } else if (GDK_KEY_2 == key) {
906 gtk_notebook_set_current_page(
907 GTK_NOTEBOOK(mw.notebook), 1);
909 } else if (GDK_KEY_3 == key) {
910 gtk_notebook_set_current_page(
911 GTK_NOTEBOOK(mw.notebook), 2);
913 } else if (GDK_KEY_4 == key) {
914 gtk_notebook_set_current_page(
915 GTK_NOTEBOOK(mw.notebook), 3);
917 } else if (GDK_KEY_5 == key) {
918 gtk_notebook_set_current_page(
919 GTK_NOTEBOOK(mw.notebook), 4);
921 } else if (GDK_KEY_6 == key) {
922 gtk_notebook_set_current_page(
923 GTK_NOTEBOOK(mw.notebook), 5);
925 } else if (GDK_KEY_7 == key) {
926 gtk_notebook_set_current_page(
927 GTK_NOTEBOOK(mw.notebook), 6);
929 } else if (GDK_KEY_8 == key) {
930 gtk_notebook_set_current_page(
931 GTK_NOTEBOOK(mw.notebook), 7);
933 } else if (GDK_KEY_9 == key) {
934 gtk_notebook_set_current_page(
935 GTK_NOTEBOOK(mw.notebook), 8);
937 } else if (GDK_KEY_u == key) {
938 gtk_notebook_prev_page(
939 GTK_NOTEBOOK(mw.notebook));
941 } else if (GDK_KEY_m == key) {
942 gboolean muted = webkit_web_view_get_is_muted(
943 WEBKIT_WEB_VIEW(c->web_view));
944 webkit_web_view_set_is_muted(
945 WEBKIT_WEB_VIEW(c->web_view), !muted);
946 changed_title(G_OBJECT(c->web_view), NULL, c);
948 } else if (GDK_KEY_t == key) {
949 client_new(cfg_home_uri, NULL, TRUE, TRUE);
951 } else if (GDK_KEY_bracketleft == key) {
952 if (num_closed == 0) return TRUE;
953 client_new(closed_tabs[num_closed - 1], NULL,
956 closed_tabs = realloc(
958 num_closed * sizeof(closed_tabs[0]));
959 if (!closed_tabs) allocfail();
961 } else if (GDK_KEY_i == key) {
962 gtk_notebook_next_page(
963 GTK_NOTEBOOK(mw.notebook));
965 } else if (GDK_KEY_p == key) {
967 webkit_settings_get_enable_javascript(
969 webkit_settings_set_enable_javascript(
971 webkit_web_view_set_settings(
972 WEBKIT_WEB_VIEW(c->web_view),
974 gtk_toggle_button_set_active(
975 GTK_TOGGLE_BUTTON(c->jsbutton), !on);
976 } else if (GDK_KEY_d == key) {
977 gtk_widget_grab_focus(c->location);
978 gtk_entry_set_text(GTK_ENTRY(c->location),
980 gtk_editable_set_position(
981 GTK_EDITABLE(c->location), -1);
983 } else if (GDK_KEY_equal == key) {
984 now = webkit_web_view_get_zoom_level(
985 WEBKIT_WEB_VIEW(c->web_view));
986 webkit_web_view_set_zoom_level(
987 WEBKIT_WEB_VIEW(c->web_view),
990 } else if (GDK_KEY_minus == key) {
991 now = webkit_web_view_get_zoom_level(
992 WEBKIT_WEB_VIEW(c->web_view));
993 webkit_web_view_set_zoom_level(
994 WEBKIT_WEB_VIEW(c->web_view),
997 } else if (GDK_KEY_0 == key) {
998 webkit_web_view_set_zoom_level(
999 WEBKIT_WEB_VIEW(c->web_view), 1);
1007 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
1009 struct Client *c = (struct Client *)data;
1011 if (key_common(widget, event, data)) return TRUE;
1013 if (event->type == GDK_KEY_PRESS) {
1014 int key = ((GdkEventKey *)event)->keyval;
1015 if ((GDK_KEY_KP_Enter == key) || (GDK_KEY_Return == key)) {
1016 gtk_widget_grab_focus(c->web_view);
1017 t = gtk_entry_get_text(GTK_ENTRY(c->location));
1018 if (t != NULL && t[0] == 's' && t[1] == '/') {
1019 if (search_text != NULL) g_free(search_text);
1020 search_text = g_strdup(t + 2);
1022 } else if (t != NULL && t[0] == 'w' && t[1] == '/') {
1023 int len = strlen(cfg_search_engine) +
1025 gchar *f = malloc(len + 1);
1026 if (!f) allocfail();
1027 snprintf(f, len + 1, "%s%s", cfg_search_engine,
1029 webkit_web_view_load_uri(
1030 WEBKIT_WEB_VIEW(c->web_view), f);
1033 webkit_web_view_load_uri(
1034 WEBKIT_WEB_VIEW(c->web_view),
1035 ensure_uri_scheme(t));
1038 } else if (GDK_KEY_Escape == key) {
1039 t = webkit_web_view_get_uri(
1040 WEBKIT_WEB_VIEW(c->web_view));
1041 gtk_entry_set_text(GTK_ENTRY(c->location),
1042 (t == NULL) ? "" : t);
1049 key_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data)
1051 GdkScrollDirection direction;
1052 if (event->type == GDK_BUTTON_RELEASE) {
1053 switch (((GdkEventButton *)event)->button) {
1055 client_destroy(NULL, data);
1058 } else if (event->type == GDK_SCROLL) {
1059 gdk_event_get_scroll_direction(event, &direction);
1060 switch (direction) {
1062 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
1064 case GDK_SCROLL_DOWN:
1065 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
1075 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
1077 struct Client *c = (struct Client *)data;
1080 if (key_common(widget, event, data)) return TRUE;
1082 if (event->type == GDK_KEY_PRESS) {
1083 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape) {
1084 webkit_web_view_stop_loading(
1085 WEBKIT_WEB_VIEW(c->web_view));
1086 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location),
1089 } else if (event->type == GDK_BUTTON_RELEASE) {
1090 GdkModifierType modifiers =
1091 gtk_accelerator_get_default_mod_mask();
1092 switch (((GdkEventButton *)event)->button) {
1094 if ((((GdkEventButton *)event)->state & modifiers) ==
1096 c->hover_uri != NULL) {
1097 client_new(c->hover_uri, NULL, TRUE, FALSE);
1102 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
1105 webkit_web_view_go_forward(
1106 WEBKIT_WEB_VIEW(c->web_view));
1109 } else if (event->type == GDK_SCROLL) {
1110 event->scroll.delta_y *= cfg_scroll_lines;
1111 if (((GdkEventScroll *)event)->state & GDK_CONTROL_MASK) {
1112 gdk_event_get_scroll_deltas(event, &dx, &dy);
1113 z = webkit_web_view_get_zoom_level(
1114 WEBKIT_WEB_VIEW(c->web_view));
1116 z = dx != 0 ? 1 : z;
1117 webkit_web_view_set_zoom_level(
1118 WEBKIT_WEB_VIEW(c->web_view), z);
1126 mainwindow_setup(void)
1128 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1129 gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
1130 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
1132 gchar *priv = (cfg.private) ? "-private" : "";
1133 gchar *title = malloc(strlen(priv) + 7);
1134 if (!title) allocfail();
1135 sprintf(title, "%s%s", "chorizo", priv);
1136 gtk_window_set_title(GTK_WINDOW(mw.win), title);
1139 mw.notebook = gtk_notebook_new();
1140 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
1141 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
1142 g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
1143 G_CALLBACK(notebook_switch_page), NULL);
1147 mainwindow_title(gint idx)
1149 GtkWidget *child, *widg, *tablabel;
1151 child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx);
1152 if (child == NULL) return;
1154 widg = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child);
1155 tablabel = (GtkWidget *)g_object_get_data(G_OBJECT(widg),
1156 "chorizo-tab-label");
1157 text = gtk_label_get_text(GTK_LABEL(tablabel));
1158 gtk_window_set_title(GTK_WINDOW(mw.win), text);
1162 notebook_switch_page(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data)
1164 mainwindow_title(idx);
1167 quit_if_nothing_active(void)
1170 if (downloads == 0) {
1174 downloadmanager_show();
1180 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1183 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1186 client_new(uri, NULL, TRUE, TRUE);
1193 show_web_view(WebKitWebView *web_view, gpointer data)
1195 struct Client *c = (struct Client *)data;
1199 gtk_widget_show_all(mw.win);
1201 if (c->focus_new_tab) {
1202 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
1204 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook),
1207 gtk_widget_grab_focus(c->web_view);
1212 trust_user_certs(WebKitWebContext *wc)
1214 GTlsCertificate *cert;
1215 gchar *basedir, *absfile;
1218 basedir = g_build_filename(g_get_user_data_dir(), "chorizo", "certs",
1220 dir = g_dir_open(basedir, 0, NULL);
1223 file = g_dir_read_name(dir);
1224 while (file != NULL) {
1225 absfile = g_build_filename(g_get_user_data_dir(),
1226 "chorizo", "certs", file,
1228 cert = g_tls_certificate_new_from_file(absfile, NULL);
1232 "chorizo: warning: could not load trusted cert: %s\n",
1235 webkit_web_context_allow_tls_certificate_for_host(
1237 file = g_dir_read_name(dir);
1246 printf("%s %s\n", "chorizo", VERSION);
1250 main(int argc, char **argv)
1255 cfg.noncooperative_instances = FALSE;
1256 cfg.cooperative_alone = TRUE;
1257 closed_tabs = malloc(0);
1258 if (!closed_tabs) allocfail();
1260 while ((opt = getopt(argc, argv, "cpvV")) != -1) {
1263 cfg.noncooperative_instances = TRUE;
1276 "usage: chorizo [OPTION]... [URI]...\n");
1281 if (cfg.verbose) version();
1283 gtk_init(&argc, &argv);
1285 // Keep clipboard contents after program closes
1286 gtk_clipboard_store(gtk_clipboard_get_for_display(
1287 gdk_display_get_default(), GDK_SELECTION_CLIPBOARD));
1289 if (!cfg.noncooperative_instances) cooperation_setup();
1291 if (cfg.noncooperative_instances || cfg.cooperative_alone)
1292 init_default_web_context();
1294 downloadmanager_setup();
1297 client_arr = malloc(sizeof(struct Client *));
1298 if (!client_arr) allocfail();
1300 if (optind >= argc) {
1301 client_new(cfg_home_uri, NULL, TRUE, TRUE);
1303 for (i = optind; i < argc; i++)
1304 client_new(argv[i], NULL, TRUE, TRUE);
1307 if (cfg.noncooperative_instances || cfg.cooperative_alone) {
1311 for (int i = 0; i < clients; i++) { free(&(client_arr[i])); }