-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhui_webview__webkit_gtk3.cc
More file actions
273 lines (201 loc) · 10.3 KB
/
hui_webview__webkit_gtk3.cc
File metadata and controls
273 lines (201 loc) · 10.3 KB
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
#ifndef _hui_backend_
#define _hui_backend_
#define _hui_webview_use_common_code
#include "HUI.hh"
#include "./hui_datatypes.h"
#include <fstream>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <gdk/gdkwayland.h>
#include <webkit2/webkit2.h>
#include <gtk-layer-shell.h>
#include <iostream>
namespace HUI {
std::vector< std::function<void(std::vector<std::string>)> > sent2cpp_handlers;
bool gtk_inited = false;
struct WebView::pImpl {
GtkWidget* window = NULL;
GtkWidget* webview = NULL;
};
WebView::WebView () {
impl = std::make_unique<pImpl>();
if (!gtk_inited) {gtk_init(NULL, NULL); gtk_inited = true;} // init so no extra global init is needed
// webview
impl->webview = GTK_WIDGET(webkit_web_view_new_with_context(webkit_web_context_new_ephemeral())); // private mode (should be always enabled)
//webkit_settings_set_enable_private_browsing (webkit_web_view_get_settings(webview), true); // deprecated private mode
//if (!webkit_web_context_get_sandbox_enabled ( webkit_web_view_get_context (webview) )) {std::cout<<"not sandboxed";} // not sandboxed by default
// transparency
gtk_widget_set_visual(GTK_WIDGET(impl->webview), gdk_screen_get_rgba_visual(gdk_screen_get_default()));
GdkRGBA transparent = {0,0,0,0};
webkit_web_view_set_background_color(WEBKIT_WEB_VIEW(impl->webview), &transparent);
// hui.js + hui.css + HUI_theme.css
hui_tweaks();
// just to unify all backends (not needed here)
//webkit_web_view_load_uri(WEBKIT_WEB_VIEW(impl->webview), "about:blank");
// debug
#if defined(HUI_DEBUG_ENABLED)
HUI_DEBUG_PRINT("enabling devtools ");
webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(impl->webview)), true);
webkit_web_inspector_show(webkit_web_view_get_inspector(WEBKIT_WEB_VIEW(impl->webview))); // takes extra RAM (about the same as the app itself)
#endif
// js<-->cpp
WebKitUserContentManager *manager = webkit_web_view_get_user_content_manager (WEBKIT_WEB_VIEW(impl->webview));
g_signal_connect (manager, "script-message-received::sent2cpp", G_CALLBACK(+[](WebKitUserContentManager* manager, WebKitJavascriptResult* message, gpointer user_data) -> void {
JSGlobalContextRef context = webkit_javascript_result_get_global_context(message);
JSCValue* result = webkit_javascript_result_get_js_value(message);
std::string msgstr = std::string(jsc_value_to_string(result));
HUI_DEBUG_PRINT("call from js to cpp '"<<msgstr<<"' ");
const int hi = stoi(msgstr.substr(0,msgstr.find(",")));
msgstr = msgstr.substr(msgstr.find(",")+1+1, msgstr.size()-1 -(msgstr.find(",")+1+1) );
std::vector<HUI::Str> msgdata_ = HUI::Str(msgstr).split("\",\"");
std::vector<std::string> msgdata;
for (auto it : msgdata_) msgdata.push_back ( it.replace("\\\"","\"").cpp_str() );
sent2cpp_handlers.at(hi)(msgdata);
}), NULL);
webkit_user_content_manager_register_script_message_handler (manager, "sent2cpp");
// window
impl->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
// TODO: handle close callback - maybe from js side <https://stackoverflow.com/questions/821011/prevent-a-webpage-from-navigating-away-using-javascript>
//g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
//g_signal_connect(window, "destroy", G_CALLBACK(+[](){ gtk_main_quit(); }), NULL);
// transparency
gtk_widget_set_app_paintable(GTK_WIDGET(impl->window), TRUE);
gtk_widget_set_visual(GTK_WIDGET(impl->window), gdk_screen_get_rgba_visual(gdk_screen_get_default()));
// add
gtk_container_add(GTK_CONTAINER(impl->window), GTK_WIDGET(impl->webview));
// show - this makes the window show up - but after that, some controls may not work (in that case the window is destroyed and the webview is moved to a new window; there may be alternative aproach by calling this once we get to the massage loop handling - <https://docs.gtk.org/glib/func.idle_add_once.html>)
gtk_widget_show_all(GTK_WIDGET(impl->window));
// TODO: kill 'WebKitNetworkProcess' to save memory (if the app works offline - it should)
}
WebView::~WebView () {
gdk_window_destroy (gtk_widget_get_window(impl->window));
}
void WebView::load_file (std::string file){
webkit_web_view_load_uri(WEBKIT_WEB_VIEW(impl->webview), std::string("file://"+file).c_str() );
hui_tweaks();
}
void WebView::load_uri (std::string uri){
webkit_web_view_load_uri(WEBKIT_WEB_VIEW(impl->webview), uri.c_str() );
hui_tweaks();
}
void WebView::load_str (std::string str){
webkit_web_view_load_html (WEBKIT_WEB_VIEW(impl->webview), str.c_str() , "none"); // TODO: fix js return when used
hui_tweaks();
}
void WebView::hui_tweaks (){
WebKitUserContentManager *manager = webkit_web_view_get_user_content_manager (WEBKIT_WEB_VIEW(impl->webview));
// if this looks like this loads the script for all future pages, its not like that (it has to be run every time)
webkit_user_content_manager_add_script(manager, webkit_user_script_new(
#include "hui.js"
,WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START, NULL,NULL) );
call_js(std::string(
"style = document.createElement('style'); style.textContent = \"")+
HUI::Str(
#include "hui.css" // JSERROR{unexpected EOF} --fixed
).replace("\n","\\n").replace("\"","\\\"").cpp_str()
+std::string("\"; document.head.append(style);"
).c_str(),false);
std::string css;
std::ifstream filestream (HUI::filepath(
#if defined(_WIN32)
"%userprofile%/HUI_theme.css"
#else
"$HOME/.config/HUI_theme.css"
#endif
).c_str());
if (filestream.good()) {
// same as following oneliner
//std::stringstream stringstream;
//stringstream << filestream.rdbuf();
//std::string str = stringstream.str();
//css = str;
css = std::string((std::istreambuf_iterator<char>(filestream)), std::istreambuf_iterator<char>()); // TODO: the fucking file must have unix line ends (not windows ones) - otherwise it breaks the js parsing resulting in "Unexpected EOF"
}
else {
css =
#include "HUI_theme.css"
;
}
call_js(std::string(
"style = document.createElement('style'); style.textContent = \"")+
std::string(HUI::Str(std::string(css.c_str())).replace("\n","\\n").replace("\"","\\\"").c_str())
+std::string("\"; document.head.append(style);"
),false);
/*WebKitUserContentManager *manager = webkit_web_view_get_user_content_manager (WEBKIT_WEB_VIEW(webview));
webkit_user_content_manager_add_script(manager, webkit_user_script_new(
#include "hui.js"
,WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START, NULL,NULL) );
webkit_user_content_manager_add_style_sheet(manager, webkit_user_style_sheet_new ( // doesnt work, maybe?
#include "hui.css"
,WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, WEBKIT_USER_STYLE_LEVEL_AUTHOR, NULL,NULL) );*/
}
std::string WebView::call_js (std::string code, bool return_data){
HUI_DEBUG_PRINT("call js from cpp '"<<code<<"' ");
if (return_data) {
struct upoint {HUI::Str str = "<error obtaining value>"; bool ok = false;} stat;
webkit_web_view_evaluate_javascript (WEBKIT_WEB_VIEW(impl->webview), code.c_str(), -1, NULL, NULL, NULL, +[](GObject *object, GAsyncResult *result, gpointer user_data) -> void {
JSCValue *value;
GError *error = NULL;
value = webkit_web_view_evaluate_javascript_finish (WEBKIT_WEB_VIEW (object), result, &error);
if (value and JSC_IS_VALUE(value)) ((upoint*)user_data)->str = jsc_value_to_string (value);
((upoint*)user_data)->ok = true;
}, &stat );
while (!stat.ok) handle_once();
return stat.str.c_str();;
}
else {
webkit_web_view_evaluate_javascript (WEBKIT_WEB_VIEW(impl->webview),code.c_str(),-1,NULL,NULL,NULL,NULL,NULL); //doesnt work at the begining of execution
webkit_user_content_manager_add_script( webkit_web_view_get_user_content_manager (WEBKIT_WEB_VIEW(impl->webview)), webkit_user_script_new(code.c_str(),WEBKIT_USER_CONTENT_INJECT_TOP_FRAME,WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_END,NULL,NULL) ); // has to be WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_END and not WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START otherwise it breaks js execution (however, for some reason this doesnt affect the 'test_webview_js_api' test)
return "";
}
}
std::string WebView::call_native (std::function<void(std::vector<std::string>)> handler, std::string process_args){
sent2cpp_handlers.push_back(handler);
auto dd = std::string()+"function(..._args_array){ window.webkit.messageHandlers['sent2cpp'].postMessage( '"+std::to_string(sent2cpp_handlers.size()-1)+"'+',\"'+("+process_args+")(..._args_array).join('\",\"')+'\"' ); }";
HUI_DEBUG_PRINT("created js function "<<dd);
return dd;
}
std::string WebView::backend_name (){
return "webkit-gtk3";
}
void* WebView::backend_object (){
HUI_DEBUG_PRINT ("webview pointer... "<<impl->window);
//return impl->window;
return static_cast<void*>(&(impl->window)); // pointer to GtkWidget* == GtkWidget**
}
std::string WebView::window_type (){
GdkDisplay *display = gtk_widget_get_display(impl->window);
if (GDK_IS_X11_DISPLAY(display)) return "x11";
else if (GDK_IS_WAYLAND_DISPLAY(display)) return "wayland";
else return "unknown";
}
void* WebView::window_handle (){
GdkWindow* gdk_window = gtk_widget_get_window(impl->window);
if (!gdk_window) return nullptr;
GdkDisplay* display = gtk_widget_get_display(impl->window);
if (GDK_IS_X11_DISPLAY(display)) {
Window x11_handle = gdk_x11_window_get_xid(gdk_window);
return reinterpret_cast<void*>(x11_handle);
}
else if (GDK_IS_WAYLAND_DISPLAY(display)) {
struct wl_surface* wayland_handle = gdk_wayland_window_get_wl_surface(gdk_window);
return static_cast<void*>(wayland_handle);
}
return nullptr;
}
void WebView::handle_forever (){
gtk_main();
}
void WebView::handle_once (){
while (gtk_events_pending()) {gtk_main_iteration();}
}
void/*std:thread*/ WebView::handle_threaded (){
/*return*/ // TODO
}
void WebView::exit (){
gtk_main_quit();
sent2cpp_handlers.clear(); // solves problem with python bindings when it was impossible to exit due to GIL error in gillstate_tss_set (not sure if still present?)
}
#include "./hui_windowcontrols__gtk3.cc"
} // HUI
#endif // _hui_backend_