-
Notifications
You must be signed in to change notification settings - Fork 1
/
badwolf.c
1564 lines (1265 loc) · 52.3 KB
/
badwolf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// BadWolf: Minimalist and privacy-oriented WebKitGTK+ browser
// Copyright © 2019-2021 Badwolf Authors <https://hacktivis.me/projects/badwolf>
// SPDX-License-Identifier: BSD-3-Clause
#include "badwolf.h"
#include "tabstack.h"
#include "bookmarks.h"
#include "config.h"
#include "downloads.h"
#include "keybindings.h"
#include "uri.h"
#include "time.h"
#include <assert.h>
#include <glib/gi18n.h> /* _() and other internationalization/localization helpers */
#include <libsoup/soup.h> /* soup* */
#include <locale.h> /* LC_* */
#include <stdio.h> /* perror(), fprintf(), snprintf() */
#include <stdlib.h> /* malloc() */
#include <unistd.h> /* access() */
#ifdef __OpenBSD__
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#endif
const gchar *homepage = "https://hacktivis.me/projects/badwolf";
const gchar *version = VERSION;
gint g_bkp_current_page;
static gchar *web_extensions_directory;
static uint64_t context_id_counter = 0;
GtkTreeModel *bookmarks_completion_model;
gboolean g_dark_mode = FALSE;
gboolean g_kiosk_mode = FALSE;
static gchar *dark_mode_css = "a { color: #40ECD0 !important; }"
"table, td, th, tr, ul"
"{ border: 0px !important; background: none !important; background-color: #212529 !important; color: #aeb3b7 !important; }"
"article, aside, body, blockquote, dd, dl, dt, form, "
"h1, h2, h3, h4, h5, header, footer, div, iframe, input, label, "
"li, option, p, pre, root, select, span, small"
"{ background: none !important; background-color: #212529 !important; color: #aeb3b7 !important; }"; //#a3aaaf #899095
static gboolean
openProtocolOnExternalApp(gchar *givenUrl);
static gboolean
WebViewCb_button_press_event(GtkWidget *widget, GdkEvent *event, gpointer user_data);
static gboolean WebViewCb_close(WebKitWebView *webView, gpointer user_data);
static gboolean WebViewCb_web_process_terminated(WebKitWebView *webView,
WebKitWebProcessTerminationReason reason,
gpointer user_data);
static gboolean
WebViewCb_notify__uri(WebKitWebView *webView, GParamSpec *pspec, gpointer user_data);
static gboolean
WebViewCb_notify__title(WebKitWebView *webView, GParamSpec *pspec, gpointer user_data);
static gboolean
WebViewCb_notify__is__playing__audio(WebKitWebView *webView, GParamSpec *pspec, gpointer user_data);
static gboolean WebViewCb_notify__estimated_load_progress(WebKitWebView *webView,
GParamSpec *pspec,
gpointer user_data);
static gboolean WebViewCb_mouse_target_changed(WebKitWebView *webView,
WebKitHitTestResult *hit,
guint modifiers,
gpointer user_data);
static WebKitWebView *WebViewCb_create(WebKitWebView *related_web_view,
WebKitNavigationAction *navigation_action,
gpointer user_data);
static gboolean WebViewCb_permission_request(WebKitWebView *web_view,
WebKitPermissionRequest *request,
gpointer user_data);
static gboolean WebViewCb_decide_policy(WebKitWebView *web_view,
WebKitPolicyDecision *decision,
WebKitPolicyDecisionType decision_type,
gpointer user_data);
static void
WebViewCb_load_changed(WebKitWebView *webView, WebKitLoadEvent load_event, gpointer user_data);
static void web_contextCb_download_started(WebKitWebContext *web_context,
WebKitDownload *download,
gpointer user_data);
static gboolean locationCb_activate(GtkEntry *location, gpointer user_data);
static gboolean
locationCompletion_match_selected(GtkEntryCompletion *widget,
GtkTreeModel *model,
GtkTreeIter *iter,
gpointer user_data);
static gboolean javascriptCb_toggled(GtkButton *javascript, gpointer user_data);
static gboolean auto_load_imagesCb_toggled(GtkButton *auto_load_images, gpointer user_data);
static void backCb_clicked(GtkButton *back, gpointer user_data);
static void forwardCb_clicked(GtkButton *forward, gpointer user_data);
static gboolean SearchEntryCb_next__match(GtkSearchEntry *search, gpointer user_data);
static gboolean SearchEntryCb_previous__match(GtkSearchEntry *search, gpointer user_data);
static gboolean SearchEntryCb_search__changed(GtkSearchEntry *search, gpointer user_data);
static gboolean SearchEntryCb_stop__search(GtkSearchEntry *search, gpointer user_data);
static gboolean SearchEntryCb_key_press__event(GtkSearchEntry *search, GdkEvent *event, gpointer user_data);
static void new_tabCb_clicked(GtkButton *new_tab, gpointer user_data);
static void closeCb_clicked(GtkButton *close, gpointer user_data);
static void
notebookCb_switch__page(GtkNotebook *notebook, GtkWidget *page, guint page_num, gpointer user_data);
void content_managerCb_ready(GObject *store, GAsyncResult *result, gpointer user_data);
static gboolean
WebViewCb_close(WebKitWebView *webView, gpointer user_data)
{
(void)webView;
struct Client *browser = (struct Client *)user_data;
gtk_widget_destroy(browser->box);
free(browser);
return TRUE;
}
static gboolean
WebViewCb_web_process_terminated(WebKitWebView *webView,
WebKitWebProcessTerminationReason reason,
gpointer user_data)
{
(void)webView;
struct Client *browser = (struct Client *)user_data;
switch(reason)
{
case WEBKIT_WEB_PROCESS_CRASHED:
fprintf(stderr, "%s", _("the web process crashed.\n"));
webView_tab_label_change(browser, _("Crashed"));
break;
case WEBKIT_WEB_PROCESS_EXCEEDED_MEMORY_LIMIT:
fprintf(stderr, "%s", _("the web process exceeded the memory limit.\n"));
webView_tab_label_change(browser, _("Out of Memory"));
break;
default:
fprintf(stderr, "%s", _("the web process terminated for an unknown reason.\n"));
webView_tab_label_change(browser, _("Unknown Crash"));
}
return FALSE;
}
static gboolean
WebViewCb_notify__uri(WebKitWebView *webView, GParamSpec *pspec, gpointer user_data)
{
(void)webView;
(void)pspec;
const gchar *location_uri;
struct Client *browser = (struct Client *)user_data;
location_uri = webkit_web_view_get_uri(browser->webView);
gtk_entry_set_text(GTK_ENTRY(browser->location), location_uri);
if(webkit_uri_for_display(location_uri) != location_uri)
gtk_widget_set_tooltip_text(browser->location, webkit_uri_for_display(location_uri));
else
gtk_widget_set_has_tooltip(browser->location, false);
return TRUE;
}
GtkWidget *
badwolf_new_tab_box(const gchar *title, struct Client *browser)
{
(void)browser;
GtkWidget *tab_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_widget_set_name(tab_box, "browser__tabbox");
GtkWidget *close =
gtk_button_new_from_icon_name("window-close-symbolic", GTK_ICON_SIZE_LARGE_TOOLBAR);
gtk_widget_set_name(close, "browser__tabbox__close");
GtkWidget *label = gtk_label_new(title);
gtk_widget_set_name(label, "browser__tabbox__label");
GtkWidget *label_event_box = gtk_event_box_new();
gtk_container_add(GTK_CONTAINER(label_event_box), label);
GtkWidget *playing =
gtk_image_new_from_icon_name("audio-volume-high-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR);
gtk_widget_set_name(playing, "browser__tabbox__playing");
GtkWidget *playing_event_box = gtk_event_box_new();
gtk_container_add(GTK_CONTAINER(playing_event_box), playing);
#ifdef BADWOLF_TAB_BOX_WIDTH
gtk_widget_set_size_request(label, BADWOLF_TAB_BOX_WIDTH, -1);
#endif
#ifdef BADWOLF_TAB_LABEL_CHARWIDTH
gtk_label_set_width_chars(GTK_LABEL(label), BADWOLF_TAB_LABEL_CHARWIDTH);
#endif
gtk_widget_set_hexpand(tab_box, BADWOLF_TAB_HEXPAND);
gtk_label_set_ellipsize(GTK_LABEL(label), BADWOLF_TAB_LABEL_ELLIPSIZE);
gtk_label_set_single_line_mode(GTK_LABEL(label), TRUE);
//gtk_label_set_single_line_mode(GTK_LABEL(context_label), TRUE);
//gtk_box_pack_start(GTK_BOX(tab_box), context_label_event_box, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(tab_box), playing_event_box, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(tab_box), label_event_box, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(tab_box), close, FALSE, FALSE, 0);
gtk_button_set_relief(GTK_BUTTON(close), GTK_RELIEF_NONE);
g_signal_connect(close, "clicked", G_CALLBACK(closeCb_clicked), browser);
gtk_widget_set_tooltip_text(tab_box, title);
gtk_widget_show_all(tab_box);
gtk_widget_set_visible(playing, webkit_web_view_is_playing_audio(browser->webView));
gtk_widget_set_events(label, GDK_BUTTON_RELEASE_MASK);
g_signal_connect(
tab_box, "button-release-event", G_CALLBACK(tab_boxCb_button_release_event), browser);
return tab_box;
}
static gboolean
WebViewCb_notify__title(WebKitWebView *webView, GParamSpec *pspec, gpointer user_data)
{
(void)webView;
(void)pspec;
struct Client *browser = (struct Client *)user_data;
webView_tab_label_change(browser, NULL);
return TRUE;
}
static gboolean
WebViewCb_notify__is__playing__audio(WebKitWebView *webView, GParamSpec *pspec, gpointer user_data)
{
(void)webView;
(void)pspec;
struct Client *browser = (struct Client *)user_data;
webView_tab_label_change(browser, NULL);
return TRUE;
}
void
webView_tab_label_change(struct Client *browser, const gchar *title)
{
GtkWidget *notebook = browser->window->notebook;
#define title_IS_EMPTY (title == NULL) || strnlen(title, 2) == 0
if(title_IS_EMPTY) title = webkit_web_view_get_title(browser->webView);
if(title_IS_EMPTY) title = webkit_web_view_get_uri(browser->webView);
if(title_IS_EMPTY) title = "BadWolf";
gtk_notebook_set_tab_label(
GTK_NOTEBOOK(notebook), browser->box, badwolf_new_tab_box(title, browser));
gtk_notebook_set_menu_label_text(GTK_NOTEBOOK(notebook), browser->box, title);
// Set the window title if the title change was on the current tab
if(gtk_notebook_page_num(GTK_NOTEBOOK(notebook), browser->box) ==
gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook)))
gtk_window_set_title(GTK_WINDOW(browser->window->main_window), title);
}
static gboolean
WebViewCb_notify__estimated_load_progress(WebKitWebView *webView,
GParamSpec *pspec,
gpointer user_data)
{
(void)webView;
(void)pspec;
struct Client *browser = (struct Client *)user_data;
gdouble progress;
progress = webkit_web_view_get_estimated_load_progress(browser->webView);
if(progress >= 1) progress = 0;
gtk_entry_set_progress_fraction(GTK_ENTRY(browser->location), progress);
return TRUE;
}
static gboolean
WebViewCb_mouse_target_changed(WebKitWebView *webView,
WebKitHitTestResult *hit,
guint modifiers,
gpointer user_data)
{
(void)webView;
(void)modifiers;
struct Client *browser = (struct Client *)user_data;
if(webkit_hit_test_result_context_is_link(hit))
{
const gchar *link_uri = webkit_hit_test_result_get_link_uri(hit);
gtk_label_set_text(GTK_LABEL(browser->statuslabel), webkit_uri_for_display(link_uri));
}
else
gtk_label_set_text(GTK_LABEL(browser->statuslabel), NULL);
return FALSE;
}
static gboolean
WebViewCb_scroll_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
(void)widget;
struct Client *browser = (struct Client *)data;
gdouble delta_x, delta_y;
gdouble zoom;
if(((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
{
gdk_event_get_scroll_deltas(event, &delta_x, &delta_y);
zoom = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(browser->webView));
zoom -= delta_y * 0.1;
webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(browser->webView), zoom);
return TRUE;
}
return FALSE;
}
static WebKitWebView *
WebViewCb_create(WebKitWebView *related_web_view,
WebKitNavigationAction *navigation_action,
gpointer user_data)
{
(void)navigation_action;
struct Client *old_browser = (struct Client *)user_data;
struct Client *browser = NULL;
// shouldn't be needed but better be safe
old_browser->webView = related_web_view;
browser = new_browser(old_browser->window, NULL, old_browser);
gint newtab=badwolf_new_tab(GTK_NOTEBOOK(old_browser->window->notebook), browser, FALSE, FALSE);
if(newtab == 0)
{
WebKitSettings *oldSettings = webkit_web_view_get_settings(related_web_view);
gboolean oldJSValue = webkit_settings_get_enable_javascript_markup(oldSettings);
gboolean oldImgValue = webkit_settings_get_auto_load_images(oldSettings);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(browser->javascript), oldJSValue);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(browser->auto_load_images), oldImgValue);
gtk_notebook_set_current_page(GTK_NOTEBOOK(browser->window->notebook),
gtk_notebook_get_current_page(GTK_NOTEBOOK(browser->window->notebook))+1);
gtk_widget_grab_focus (GTK_WIDGET (browser->webView));
}
else if(newtab < 0)
{
return NULL;
}
return browser->webView;
}
static gboolean
WebViewCb_permission_request(WebKitWebView *web_view,
WebKitPermissionRequest *request,
gpointer user_data)
{
(void)web_view;
(void)user_data;
webkit_permission_request_deny(request);
return TRUE; /* Stop other handlers */
}
static gboolean
WebViewCb_decide_policy(WebKitWebView *web_view,
WebKitPolicyDecision *decision,
WebKitPolicyDecisionType decision_type,
gpointer user_data)
{
WebKitResponsePolicyDecision *r;
(void)web_view;
(void)user_data;
switch(decision_type)
{
case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
if(!webkit_response_policy_decision_is_mime_type_supported(r))
webkit_policy_decision_download(decision);
else
webkit_policy_decision_use(decision);
break;
default:
/* Use whatever default there is. */
return FALSE;
}
return TRUE;
}
static char *
detail_tls_certificate_flags(GTlsCertificateFlags tls_errors)
{
GString *errors = g_string_new(NULL);
g_string_append_printf(errors,
_("Couldn't verify the TLS certificate to ensure a better security of the "
"connection. You might want to verify your machine and network.\n\n"));
if(tls_errors & G_TLS_CERTIFICATE_UNKNOWN_CA)
g_string_append_printf(errors, _("Error: The X509 Certificate Authority is unknown.\n"));
if(tls_errors & G_TLS_CERTIFICATE_BAD_IDENTITY)
g_string_append(errors, _("Error: The given identity doesn't match the expected one.\n"));
if(tls_errors & G_TLS_CERTIFICATE_NOT_ACTIVATED)
g_string_append(errors,
_("Error: The certificate isn't valid yet. Check your system's clock.\n"));
if(tls_errors & G_TLS_CERTIFICATE_EXPIRED)
g_string_append(errors, _("Error: The certificate has expired. Check your system's clock.\n"));
if(tls_errors & G_TLS_CERTIFICATE_REVOKED)
g_string_append(errors, _("Error: The certificate has been revoked.\n"));
if(tls_errors & G_TLS_CERTIFICATE_INSECURE)
g_string_append(errors, _("Error: The certificate is considered to be insecure.\n"));
if(tls_errors & G_TLS_CERTIFICATE_GENERIC_ERROR)
g_string_append(errors, _("Error: Some unknown error occurred validating the certificate.\n"));
return g_string_free(errors, FALSE);
}
static gboolean
WebViewCb_load_failed_with_tls_errors(WebKitWebView *web_view,
gchar *failing_text,
GTlsCertificate *certificate,
GTlsCertificateFlags errors,
gpointer user_data)
{
(void)web_view;
(void)certificate;
(void)errors;
struct Client *browser = (struct Client *)user_data;
gchar *error_details = detail_tls_certificate_flags(errors);
gint dialog_response;
#ifndef USE_LIBSOUP2
GUri *failing_uri = g_uri_parse(failing_text, G_URI_FLAGS_NONE, NULL);
#else
SoupURI *failing_uri = soup_uri_new(failing_text);
#endif
GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(browser->window->main_window),
GTK_DIALOG_MODAL & GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_NONE,
_("TLS Error for %s."),
failing_text);
gtk_dialog_add_buttons(
GTK_DIALOG(dialog), _("Temporarily Add Exception"), 1, _("Continue"), 0, NULL);
if(!failing_uri)
{
gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), 1, FALSE);
}
gtk_dialog_set_default_response(GTK_DIALOG(dialog), 0);
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), "%s\n", error_details);
dialog_response = gtk_dialog_run(GTK_DIALOG(dialog));
if(dialog_response == 1)
{
#ifndef USE_LIBSOUP2
webkit_web_context_allow_tls_certificate_for_host(
webkit_web_view_get_context(browser->webView), certificate, g_uri_get_host(failing_uri));
#else
webkit_web_context_allow_tls_certificate_for_host(
webkit_web_view_get_context(browser->webView), certificate, failing_uri->host);
#endif
webkit_web_view_reload(browser->webView);
}
#ifndef USE_LIBSOUP2
/* Calling g_free(failing_uri) ought to be the correct way but this causes a segfault.
*
* - documentation describes it as something which should be free
* - implementation seems to make it a pointer that should be freed
* - epiphany doesn't seems to free/unref it but gnome code seems to frequently have memleaks
*
* Decided to at least continue to try with using g_uri_unref(failing_uri) instead.
* Related fediverse post: <https://queer.hacktivis.me/objects/cec7c4e8-6a58-4358-85bf-b66f9bb21a98>
*/
g_uri_unref(failing_uri);
#else
soup_uri_free(failing_uri);
#endif
g_free(error_details);
gtk_widget_destroy(dialog);
return FALSE; /* propagate the event further */
}
static void
web_contextCb_download_started(WebKitWebContext *web_context,
WebKitDownload *webkit_download,
gpointer user_data)
{
(void)web_context;
struct Client *browser = (struct Client *)user_data;
struct Download *download = malloc(sizeof(struct Download));
assert(webkit_download);
if(download != NULL)
{
download->window = browser->window;
download_new_entry(webkit_download, download);
g_signal_connect(
G_OBJECT(webkit_download), "received-data", G_CALLBACK(downloadCb_received_data), download);
g_signal_connect(G_OBJECT(webkit_download),
"created-destination",
G_CALLBACK(downloadCb_created_destination),
download);
g_signal_connect(G_OBJECT(webkit_download), "failed", G_CALLBACK(downloadCb_failed), download);
g_signal_connect(
G_OBJECT(webkit_download), "finished", G_CALLBACK(downloadCb_finished), download);
}
g_signal_connect(G_OBJECT(webkit_download),
"decide-destination",
G_CALLBACK(downloadCb_decide_destination),
user_data);
}
static gboolean
javascriptCb_toggled(GtkButton *javascript, gpointer user_data)
{
struct Client *browser = (struct Client *)user_data;
WebKitSettings *settings = webkit_web_view_get_settings(browser->webView);
webkit_settings_set_enable_javascript_markup(
settings, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(javascript)));
webkit_web_view_set_settings(browser->webView, settings);
return TRUE;
}
static gboolean
auto_load_imagesCb_toggled(GtkButton *auto_load_images, gpointer user_data)
{
struct Client *browser = (struct Client *)user_data;
WebKitSettings *settings = webkit_web_view_get_settings(browser->webView);
webkit_settings_set_auto_load_images(
settings, gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(auto_load_images)));
webkit_web_view_set_settings(browser->webView, settings);
return TRUE;
}
static void
backCb_clicked(GtkButton *back, gpointer user_data)
{
(void)back;
struct Client *browser = (struct Client *)user_data;
webkit_web_view_go_back(browser->webView);
}
static void
forwardCb_clicked(GtkButton *forward, gpointer user_data)
{
(void)forward;
struct Client *browser = (struct Client *)user_data;
webkit_web_view_go_forward(browser->webView);
}
static gboolean
SearchEntryCb_next__match(GtkSearchEntry *search, gpointer user_data)
{
(void)search;
struct Client *browser = (struct Client *)user_data;
WebKitFindController *findController = webkit_web_view_get_find_controller(browser->webView);
webkit_find_controller_search_next(findController);
return TRUE;
}
static gboolean
SearchEntryCb_previous__match(GtkSearchEntry *search, gpointer user_data)
{
(void)search;
struct Client *browser = (struct Client *)user_data;
WebKitFindController *findController = webkit_web_view_get_find_controller(browser->webView);
webkit_find_controller_search_previous(findController);
return TRUE;
}
static gboolean
SearchEntryCb_search__changed(GtkSearchEntry *search, gpointer user_data)
{
struct Client *browser = (struct Client *)user_data;
WebKitFindController *findController = webkit_web_view_get_find_controller(browser->webView);
const gchar *search_text = gtk_entry_get_text(GTK_ENTRY(search));
webkit_find_controller_search(findController, search_text, 0, 0);
return TRUE;
}
static gboolean
SearchEntryCb_stop__search(GtkSearchEntry *search, gpointer user_data)
{
(void)search;
struct Client *browser = (struct Client *)user_data;
WebKitFindController *findController = webkit_web_view_get_find_controller(browser->webView);
webkit_find_controller_search_finish(findController);
return TRUE;
}
static gboolean
widgetCb_drop_button3_event(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
(void)widget;
(void)user_data;
// Button3 being right-click on right-handed mode, left-click on left-handed mode
return ((GdkEventButton *)event)->button == 3;
}
struct Client *
new_browser(struct Window *window, const gchar *target_url, struct Client *old_browser)
{
target_url = badwolf_ensure_uri_scheme(target_url, (old_browser == NULL));
if (openProtocolOnExternalApp(strdup(target_url))) return NULL;
struct Client *browser = malloc(sizeof(struct Client));
char *badwolf_l10n = NULL;
WebKitWebContext *web_context;
if(browser == NULL) return NULL;
browser->window = window;
browser->context_id = old_browser == NULL ? context_id_counter++ : old_browser->context_id;
browser->box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_widget_set_name(browser->box, "browser__box");
browser->toolbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_widget_set_name(browser->toolbar, "browser__toolbar");
browser->back =
gtk_button_new_from_icon_name("go-previous-symbolic", GTK_ICON_SIZE_LARGE_TOOLBAR);
gtk_widget_set_name(browser->back, "browser__back");
browser->forward = gtk_button_new_from_icon_name("go-next-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR);
gtk_widget_set_name(browser->forward, "browser__forward");
GtkWidget *toolbar_separator = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
browser->javascript = gtk_toggle_button_new_with_mnemonic(_("_JS"));
gtk_widget_set_name(browser->javascript, "browser__javascript");
gtk_widget_set_tooltip_text(browser->javascript, _("Toggle javascript"));
gtk_button_set_relief(GTK_BUTTON(browser->javascript), GTK_RELIEF_NONE);
browser->auto_load_images = gtk_toggle_button_new_with_mnemonic(_("_IMG"));
gtk_widget_set_name(browser->auto_load_images, "browser__load_images");
gtk_widget_set_tooltip_text(browser->auto_load_images, _("Toggle loading images automatically"));
gtk_button_set_relief(GTK_BUTTON(browser->auto_load_images), GTK_RELIEF_NONE);
browser->location = gtk_entry_new();
gtk_widget_set_name(browser->location, "browser__location");
browser->statusbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_widget_set_name(browser->statusbar, "browser__statusbar");
browser->search = gtk_search_entry_new();
gtk_widget_set_name(browser->search, "browser__search");
browser->statuslabel = gtk_label_new(NULL);
gtk_widget_set_name(browser->statuslabel, "browser__statuslabel");
if(old_browser == NULL)
{
WebKitWebsiteDataManager *website_data_manager = webkit_website_data_manager_new_ephemeral();
webkit_website_data_manager_set_itp_enabled(website_data_manager, TRUE);
web_context = webkit_web_context_new_with_website_data_manager(website_data_manager);
g_object_unref(website_data_manager);
webkit_web_context_set_sandbox_enabled(web_context, TRUE);
//webkit_web_context_set_process_model(web_context,
// WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
webkit_web_context_set_web_extensions_directory(web_context, web_extensions_directory);
g_signal_connect(G_OBJECT(web_context),
"download-started",
G_CALLBACK(web_contextCb_download_started),
browser);
}
else
{
web_context = webkit_web_view_get_context(old_browser->webView);
}
badwolf_l10n = getenv("BADWOLF_L10N");
if(badwolf_l10n != NULL)
{
gchar **languages = g_strsplit(badwolf_l10n, ":", -1);
webkit_web_context_set_spell_checking_languages(web_context, (const gchar *const *)languages);
g_strfreev(languages);
webkit_web_context_set_spell_checking_enabled(web_context, TRUE);
}
WebKitSettings *settings = webkit_settings_new_with_settings(BADWOLF_WEBKIT_SETTINGS);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(browser->javascript),
webkit_settings_get_enable_javascript_markup(settings));
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(browser->auto_load_images),
webkit_settings_get_auto_load_images(settings));
browser->webView = WEBKIT_WEB_VIEW(g_object_new(WEBKIT_TYPE_WEB_VIEW,
"web-context",
web_context,
"related-view",
old_browser == NULL ? NULL : old_browser->webView,
"settings",
settings,
"user-content-manager",
window->content_manager,
NULL));
gtk_widget_set_name(GTK_WIDGET(browser->webView), "browser__webView");
if(old_browser == NULL)
{
g_object_unref(web_context);
}
g_object_unref(settings);
gtk_box_pack_start(
GTK_BOX(browser->toolbar), GTK_WIDGET(browser->back), FALSE, FALSE, BADWOLF_TOOLBAR_PADDING);
gtk_box_pack_start(GTK_BOX(browser->toolbar),
GTK_WIDGET(browser->forward),
FALSE,
FALSE,
BADWOLF_TOOLBAR_PADDING);
gtk_box_pack_start(GTK_BOX(browser->toolbar),
toolbar_separator,
FALSE,
FALSE,
BADWOLF_TOOLBAR_SEPARATOR_PADDING);
gtk_box_pack_start(GTK_BOX(browser->toolbar),
GTK_WIDGET(browser->javascript),
FALSE,
FALSE,
BADWOLF_TOOLBAR_PADDING);
gtk_box_pack_start(GTK_BOX(browser->toolbar),
GTK_WIDGET(browser->auto_load_images),
FALSE,
FALSE,
BADWOLF_TOOLBAR_PADDING);
gtk_box_pack_start(GTK_BOX(browser->toolbar),
GTK_WIDGET(browser->location),
TRUE,
TRUE,
BADWOLF_TOOLBAR_PADDING);
gtk_container_set_focus_child(GTK_CONTAINER(browser->box), browser->toolbar);
gtk_container_set_focus_child(GTK_CONTAINER(browser->toolbar), browser->location);
gtk_box_pack_start(
GTK_BOX(browser->box), GTK_WIDGET(browser->toolbar), FALSE, FALSE, BADWOLF_BOX_PADDING);
gtk_box_pack_start(
GTK_BOX(browser->box), GTK_WIDGET(browser->webView), TRUE, TRUE, BADWOLF_BOX_PADDING);
gtk_box_pack_start(
GTK_BOX(browser->box), GTK_WIDGET(browser->statusbar), FALSE, FALSE, BADWOLF_BOX_PADDING);
gtk_box_pack_start(GTK_BOX(browser->statusbar),
GTK_WIDGET(browser->search),
FALSE,
FALSE,
BADWOLF_STATUSBAR_PADDING);
gtk_box_pack_start(GTK_BOX(browser->statusbar),
GTK_WIDGET(browser->statuslabel),
FALSE,
FALSE,
BADWOLF_STATUSBAR_PADDING);
gtk_widget_set_halign(browser->statusbar, GTK_ALIGN_START);
gtk_label_set_single_line_mode(GTK_LABEL(browser->statuslabel), TRUE);
gtk_label_set_ellipsize(GTK_LABEL(browser->statuslabel), BADWOLF_STATUSLABEL_ELLIPSIZE);
if(bookmarks_completion_model != NULL)
{
GtkEntryCompletion *location_completion = gtk_entry_completion_new();
GtkTreeModel *location_completion_model = bookmarks_completion_model;
bookmarks_completion_setup(location_completion, location_completion_model);
gtk_entry_set_completion(GTK_ENTRY(browser->location), location_completion);
g_signal_connect(G_OBJECT(location_completion), "match-selected", G_CALLBACK(locationCompletion_match_selected), browser);
}
gtk_entry_set_text(GTK_ENTRY(browser->location), target_url);
gtk_entry_set_input_purpose(GTK_ENTRY(browser->location), GTK_INPUT_PURPOSE_URL);
gtk_entry_set_placeholder_text(GTK_ENTRY(browser->search), _("search in current page"));
/* signals for back/forward buttons */
g_signal_connect(browser->back, "clicked", G_CALLBACK(backCb_clicked), browser);
g_signal_connect(browser->forward, "clicked", G_CALLBACK(forwardCb_clicked), browser);
/* prevents GtkNotebook from spawning it's context-menu */
g_signal_connect(
browser->back, "button-press-event", G_CALLBACK(widgetCb_drop_button3_event), NULL);
g_signal_connect(
browser->back, "button-release-event", G_CALLBACK(widgetCb_drop_button3_event), NULL);
g_signal_connect(
browser->forward, "button-press-event", G_CALLBACK(widgetCb_drop_button3_event), NULL);
g_signal_connect(
browser->forward, "button-release-event", G_CALLBACK(widgetCb_drop_button3_event), NULL);
/* signals for javacript toggle widget */
g_signal_connect(browser->javascript, "toggled", G_CALLBACK(javascriptCb_toggled), browser);
/* prevents GtkNotebook from spawning it's context-menu */
g_signal_connect(
browser->javascript, "button-press-event", G_CALLBACK(widgetCb_drop_button3_event), NULL);
g_signal_connect(
browser->javascript, "button-release-event", G_CALLBACK(widgetCb_drop_button3_event), NULL);
/* signals for auto_load_images toggle widget */
g_signal_connect(
browser->auto_load_images, "toggled", G_CALLBACK(auto_load_imagesCb_toggled), browser);
/* prevents GtkNotebook from spawning it's context-menu */
g_signal_connect(browser->auto_load_images,
"button-press-event",
G_CALLBACK(widgetCb_drop_button3_event),
NULL);
g_signal_connect(browser->auto_load_images,
"button-release-event",
G_CALLBACK(widgetCb_drop_button3_event),
NULL);
/* signals for location entry widget */
g_signal_connect(browser->location, "activate", G_CALLBACK(locationCb_activate), browser);
/* signals for WebView widget */
g_signal_connect(browser->webView,
"web-process-terminated",
G_CALLBACK(WebViewCb_web_process_terminated),
browser);
g_signal_connect(browser->webView, "notify::uri", G_CALLBACK(WebViewCb_notify__uri), browser);
g_signal_connect(browser->webView, "notify::title", G_CALLBACK(WebViewCb_notify__title), browser);
g_signal_connect(browser->webView,
"notify::is-playing-audio",
G_CALLBACK(WebViewCb_notify__is__playing__audio),
browser);
g_signal_connect(browser->webView,
"mouse-target-changed",
G_CALLBACK(WebViewCb_mouse_target_changed),
browser);
g_signal_connect(browser->webView,
"notify::estimated-load-progress",
G_CALLBACK(WebViewCb_notify__estimated_load_progress),
browser);
g_signal_connect(browser->webView, "create", G_CALLBACK(WebViewCb_create), browser);
g_signal_connect(browser->webView, "close", G_CALLBACK(WebViewCb_close), browser);
g_signal_connect(
browser->webView, "key-press-event", G_CALLBACK(WebViewCb_key_press_event), browser);
g_signal_connect(browser->webView, "scroll-event", G_CALLBACK(WebViewCb_scroll_event), browser);
g_signal_connect(
browser->webView, "permission-request", G_CALLBACK(WebViewCb_permission_request), NULL);
g_signal_connect(browser->webView, "decide-policy", G_CALLBACK(WebViewCb_decide_policy), NULL);
g_signal_connect(browser->webView,
"load-failed-with-tls-errors",
G_CALLBACK(WebViewCb_load_failed_with_tls_errors),
browser);
g_signal_connect(browser->webView, "load-changed", G_CALLBACK(WebViewCb_load_changed), browser);
g_signal_connect(browser->webView, "button-press-event", G_CALLBACK(WebViewCb_button_press_event), browser);
/* signals for WebView's WebContext */
/*g_signal_connect(G_OBJECT(web_context),
"download-started",
G_CALLBACK(web_contextCb_download_started),
browser);*/
/* signals for search widget */
g_signal_connect(browser->search, "next-match", G_CALLBACK(SearchEntryCb_next__match), browser);
g_signal_connect(
browser->search, "previous-match", G_CALLBACK(SearchEntryCb_previous__match), browser);
g_signal_connect(
browser->search, "search-changed", G_CALLBACK(SearchEntryCb_search__changed), browser);
g_signal_connect(browser->search, "stop-search", G_CALLBACK(SearchEntryCb_stop__search), browser);
g_signal_connect(browser->search, "key-press-event", G_CALLBACK(SearchEntryCb_key_press__event), browser);
/* signals for box container */
g_signal_connect(browser->box, "key-press-event", G_CALLBACK(boxCb_key_press_event), browser);
if(old_browser == NULL) webkit_web_view_load_uri(browser->webView, target_url);
return browser;
}
/* badwolf_new_tab: Inserts struct Client *browser in GtkNotebook *notebook
* and optionally switches selected tab to it.
*
* returns:
* 0 : Ran successfully
* -1 : Failed to insert a page for browser->box
* -2 : browser is NULL
*/
int
badwolf_new_tab(GtkNotebook *notebook, struct Client *browser, bool auto_switch, bool force_kiosk_mode)
{
if (g_kiosk_mode == TRUE && force_kiosk_mode == FALSE) return -2;
gint current_page = gtk_notebook_get_current_page(notebook);
gchar *title = _("New tab");
if(browser == NULL) return -2;
gtk_widget_show_all(browser->box);
if(gtk_notebook_insert_page(notebook, browser->box, NULL, (current_page + 1)) == -1) return -1;
//gtk_notebook_set_tab_reorderable(notebook, browser->box, TRUE);
gtk_notebook_set_tab_label(notebook, browser->box, badwolf_new_tab_box(title, browser));
gtk_notebook_set_menu_label_text(GTK_NOTEBOOK(notebook), browser->box, title);
gtk_widget_queue_draw(GTK_WIDGET(notebook));
if(auto_switch)
{
gtk_notebook_set_current_page(notebook, gtk_notebook_page_num(notebook, browser->box));
}
set_dark_mode(browser->webView);
set_kiosk_mode(browser);
return 0;
}
static void
new_tabCb_clicked(GtkButton *new_tab, gpointer user_data)
{
(void)new_tab;
struct Window *window = (struct Window *)user_data;
struct Client *browser = new_browser(window, NULL, NULL);
badwolf_new_tab(GTK_NOTEBOOK(window->notebook), browser, TRUE, FALSE);
}
static void
closeCb_clicked(GtkButton *close, gpointer user_data)
{
(void)close;
struct Client *browser = (struct Client *)user_data;
webkit_web_view_try_close(browser->webView);
}
void