janela 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +95 -0
  3. package/bin/janela.mjs +271 -0
  4. package/package.json +43 -0
  5. package/runtime/janela.ts +193 -0
  6. package/shim/wvshim.cc +201 -0
  7. package/templates/index.html +48 -0
  8. package/templates/janela.conf.json +10 -0
  9. package/templates/main.ts +37 -0
  10. package/vendor-webview/LICENSE +22 -0
  11. package/vendor-webview/core/include/webview/api.h +251 -0
  12. package/vendor-webview/core/include/webview/backends.hh +40 -0
  13. package/vendor-webview/core/include/webview/c_api_impl.hh +258 -0
  14. package/vendor-webview/core/include/webview/detail/backends/cocoa_webkit.hh +626 -0
  15. package/vendor-webview/core/include/webview/detail/backends/gtk_webkitgtk.hh +355 -0
  16. package/vendor-webview/core/include/webview/detail/backends/win32_edge.hh +909 -0
  17. package/vendor-webview/core/include/webview/detail/basic_result.hh +119 -0
  18. package/vendor-webview/core/include/webview/detail/engine_base.hh +382 -0
  19. package/vendor-webview/core/include/webview/detail/exceptions.hh +42 -0
  20. package/vendor-webview/core/include/webview/detail/json.hh +337 -0
  21. package/vendor-webview/core/include/webview/detail/native_library.hh +170 -0
  22. package/vendor-webview/core/include/webview/detail/optional.hh +118 -0
  23. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSApplication.hh +107 -0
  24. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSBundle.hh +56 -0
  25. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSEvent.hh +71 -0
  26. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSInvocation.hh +66 -0
  27. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSMethodSignature.hh +52 -0
  28. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSNotification.hh +51 -0
  29. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSNumber.hh +53 -0
  30. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSObject.hh +51 -0
  31. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSOpenPanel.hh +71 -0
  32. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSPoint.hh +53 -0
  33. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSRect.hh +53 -0
  34. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSSavePanel.hh +52 -0
  35. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSSize.hh +53 -0
  36. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSString.hh +87 -0
  37. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSURL.hh +63 -0
  38. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSURLRequest.hh +52 -0
  39. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSValue.hh +56 -0
  40. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSView.hh +91 -0
  41. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSWindow.hh +137 -0
  42. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/cocoa.hh +59 -0
  43. package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/types.hh +49 -0
  44. package/vendor-webview/core/include/webview/detail/platform/darwin/objc/Class.hh +55 -0
  45. package/vendor-webview/core/include/webview/detail/platform/darwin/objc/autoreleasepool.hh +70 -0
  46. package/vendor-webview/core/include/webview/detail/platform/darwin/objc/invoke.hh +75 -0
  47. package/vendor-webview/core/include/webview/detail/platform/darwin/objc/memory.hh +57 -0
  48. package/vendor-webview/core/include/webview/detail/platform/darwin/objc/objc.hh +47 -0
  49. package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/WKOpenPanelParameters.hh +57 -0
  50. package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/WKScriptMessage.hh +51 -0
  51. package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/WKUserContentController.hh +61 -0
  52. package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/WKUserScript.hh +71 -0
  53. package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/WKWebView.hh +108 -0
  54. package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/WKWebViewConfiguration.hh +60 -0
  55. package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/webkit.hh +46 -0
  56. package/vendor-webview/core/include/webview/detail/platform/linux/gtk/compat.hh +137 -0
  57. package/vendor-webview/core/include/webview/detail/platform/linux/webkitgtk/compat.hh +142 -0
  58. package/vendor-webview/core/include/webview/detail/platform/linux/webkitgtk/dmabuf.hh +167 -0
  59. package/vendor-webview/core/include/webview/detail/platform/windows/com_init_wrapper.hh +126 -0
  60. package/vendor-webview/core/include/webview/detail/platform/windows/dpi.hh +161 -0
  61. package/vendor-webview/core/include/webview/detail/platform/windows/dwmapi.hh +67 -0
  62. package/vendor-webview/core/include/webview/detail/platform/windows/iid.hh +78 -0
  63. package/vendor-webview/core/include/webview/detail/platform/windows/ntdll.hh +58 -0
  64. package/vendor-webview/core/include/webview/detail/platform/windows/reg_key.hh +135 -0
  65. package/vendor-webview/core/include/webview/detail/platform/windows/shcore.hh +59 -0
  66. package/vendor-webview/core/include/webview/detail/platform/windows/theme.hh +76 -0
  67. package/vendor-webview/core/include/webview/detail/platform/windows/user32.hh +90 -0
  68. package/vendor-webview/core/include/webview/detail/platform/windows/version.hh +151 -0
  69. package/vendor-webview/core/include/webview/detail/platform/windows/webview2/loader.hh +391 -0
  70. package/vendor-webview/core/include/webview/detail/user_script.hh +76 -0
  71. package/vendor-webview/core/include/webview/detail/utility/string.hh +99 -0
  72. package/vendor-webview/core/include/webview/errors.h +67 -0
  73. package/vendor-webview/core/include/webview/errors.hh +85 -0
  74. package/vendor-webview/core/include/webview/json_deprecated.hh +61 -0
  75. package/vendor-webview/core/include/webview/macros.h +123 -0
  76. package/vendor-webview/core/include/webview/types.h +81 -0
  77. package/vendor-webview/core/include/webview/types.hh +48 -0
  78. package/vendor-webview/core/include/webview/version.h +67 -0
  79. package/vendor-webview/core/include/webview/webview.h +32 -0
  80. package/vendor-webview/core/include/webview.h +15 -0
@@ -0,0 +1,355 @@
1
+ /*
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2017 Serge Zaitsev
5
+ * Copyright (c) 2022 Steffen André Langnes
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ #ifndef WEBVIEW_BACKENDS_GTK_WEBKITGTK_HH
27
+ #define WEBVIEW_BACKENDS_GTK_WEBKITGTK_HH
28
+
29
+ #if defined(__cplusplus) && !defined(WEBVIEW_HEADER)
30
+
31
+ #include "../../macros.h"
32
+
33
+ #if defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK)
34
+
35
+ //
36
+ // ====================================================================
37
+ //
38
+ // This implementation uses webkit2gtk backend. It requires GTK and
39
+ // WebKitGTK libraries. Proper compiler flags can be retrieved via:
40
+ //
41
+ // pkg-config --cflags --libs gtk4 webkitgtk-6.0
42
+ // pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.1
43
+ // pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.0
44
+ //
45
+ // ====================================================================
46
+ //
47
+
48
+ #include "../../errors.hh"
49
+ #include "../../types.hh"
50
+ #include "../engine_base.hh"
51
+ #include "../platform/linux/gtk/compat.hh"
52
+ #include "../platform/linux/webkitgtk/compat.hh"
53
+ #include "../platform/linux/webkitgtk/dmabuf.hh"
54
+ #include "../user_script.hh"
55
+
56
+ #include <functional>
57
+ #include <list>
58
+ #include <memory>
59
+ #include <string>
60
+
61
+ #include <gtk/gtk.h>
62
+
63
+ #if GTK_MAJOR_VERSION >= 4
64
+
65
+ #include <jsc/jsc.h>
66
+ #include <webkit/webkit.h>
67
+
68
+ #elif GTK_MAJOR_VERSION >= 3
69
+
70
+ #include <JavaScriptCore/JavaScript.h>
71
+ #include <webkit2/webkit2.h>
72
+
73
+ #endif
74
+
75
+ #include <fcntl.h>
76
+ #include <sys/stat.h>
77
+
78
+ namespace webview {
79
+ namespace detail {
80
+
81
+ class user_script::impl {
82
+ public:
83
+ impl(WebKitUserScript *script) : m_script{script} {
84
+ webkit_user_script_ref(script);
85
+ }
86
+
87
+ ~impl() { webkit_user_script_unref(m_script); }
88
+
89
+ impl(const impl &) = delete;
90
+ impl &operator=(const impl &) = delete;
91
+ impl(impl &&) = delete;
92
+ impl &operator=(impl &&) = delete;
93
+
94
+ WebKitUserScript *get_native() const { return m_script; }
95
+
96
+ private:
97
+ WebKitUserScript *m_script{};
98
+ };
99
+
100
+ class gtk_webkit_engine : public engine_base {
101
+ public:
102
+ gtk_webkit_engine(bool debug, void *window) : engine_base{!window} {
103
+ window_init(window);
104
+ window_settings(debug);
105
+ dispatch_size_default();
106
+ }
107
+
108
+ gtk_webkit_engine(const gtk_webkit_engine &) = delete;
109
+ gtk_webkit_engine &operator=(const gtk_webkit_engine &) = delete;
110
+ gtk_webkit_engine(gtk_webkit_engine &&) = delete;
111
+ gtk_webkit_engine &operator=(gtk_webkit_engine &&) = delete;
112
+
113
+ virtual ~gtk_webkit_engine() {
114
+ if (m_window) {
115
+ if (owns_window()) {
116
+ // Disconnect handlers to avoid callbacks invoked during destruction.
117
+ g_signal_handlers_disconnect_by_data(GTK_WINDOW(m_window), this);
118
+ gtk_window_close(GTK_WINDOW(m_window));
119
+ on_window_destroyed(true);
120
+ } else {
121
+ gtk_compat::window_remove_child(GTK_WINDOW(m_window),
122
+ GTK_WIDGET(m_webview));
123
+ }
124
+ }
125
+ if (m_webview) {
126
+ g_object_unref(m_webview);
127
+ }
128
+ if (owns_window()) {
129
+ // Needed for the window to close immediately.
130
+ deplete_run_loop_event_queue();
131
+ }
132
+ }
133
+
134
+ protected:
135
+ result<void *> window_impl() override {
136
+ if (m_window) {
137
+ return m_window;
138
+ }
139
+ return error_info{WEBVIEW_ERROR_INVALID_STATE};
140
+ }
141
+
142
+ result<void *> widget_impl() override {
143
+ if (m_webview) {
144
+ return m_webview;
145
+ }
146
+ return error_info{WEBVIEW_ERROR_INVALID_STATE};
147
+ }
148
+
149
+ result<void *> browser_controller_impl() override {
150
+ if (m_webview) {
151
+ return m_webview;
152
+ }
153
+ return error_info{WEBVIEW_ERROR_INVALID_STATE};
154
+ }
155
+
156
+ noresult run_impl() override {
157
+ m_stop_run_loop = false;
158
+ while (!m_stop_run_loop) {
159
+ g_main_context_iteration(nullptr, TRUE);
160
+ }
161
+ return {};
162
+ }
163
+
164
+ noresult terminate_impl() override {
165
+ return dispatch_impl([&] { m_stop_run_loop = true; });
166
+ }
167
+
168
+ noresult dispatch_impl(std::function<void()> f) override {
169
+ g_idle_add_full(G_PRIORITY_HIGH_IDLE, (GSourceFunc)([](void *fn) -> int {
170
+ (*static_cast<dispatch_fn_t *>(fn))();
171
+ return G_SOURCE_REMOVE;
172
+ }),
173
+ new std::function<void()>(f),
174
+ [](void *fn) { delete static_cast<dispatch_fn_t *>(fn); });
175
+ return {};
176
+ }
177
+
178
+ noresult set_title_impl(const std::string &title) override {
179
+ gtk_window_set_title(GTK_WINDOW(m_window), title.c_str());
180
+ return {};
181
+ }
182
+
183
+ noresult set_size_impl(int width, int height, webview_hint_t hints) override {
184
+ gtk_window_set_resizable(GTK_WINDOW(m_window), hints != WEBVIEW_HINT_FIXED);
185
+ if (hints == WEBVIEW_HINT_NONE || hints == WEBVIEW_HINT_FIXED) {
186
+ gtk_compat::window_set_size(GTK_WINDOW(m_window), width, height);
187
+ } else if (hints == WEBVIEW_HINT_MIN) {
188
+ gtk_widget_set_size_request(m_window, width, height);
189
+ } else if (hints == WEBVIEW_HINT_MAX) {
190
+ gtk_compat::window_set_max_size(GTK_WINDOW(m_window), width, height);
191
+ } else {
192
+ return error_info{WEBVIEW_ERROR_INVALID_ARGUMENT, "Invalid hint"};
193
+ }
194
+ return window_show();
195
+ }
196
+
197
+ noresult navigate_impl(const std::string &url) override {
198
+ webkit_web_view_load_uri(WEBKIT_WEB_VIEW(m_webview), url.c_str());
199
+ return {};
200
+ }
201
+
202
+ noresult set_html_impl(const std::string &html) override {
203
+ webkit_web_view_load_html(WEBKIT_WEB_VIEW(m_webview), html.c_str(),
204
+ nullptr);
205
+ return {};
206
+ }
207
+
208
+ noresult eval_impl(const std::string &js) override {
209
+ // URI is null before content has begun loading.
210
+ if (!webkit_web_view_get_uri(WEBKIT_WEB_VIEW(m_webview))) {
211
+ return {};
212
+ }
213
+ #if (WEBKIT_MAJOR_VERSION == 2 && WEBKIT_MINOR_VERSION >= 40) || \
214
+ WEBKIT_MAJOR_VERSION > 2
215
+ webkit_web_view_evaluate_javascript(WEBKIT_WEB_VIEW(m_webview), js.c_str(),
216
+ static_cast<gssize>(js.size()), nullptr,
217
+ nullptr, nullptr, nullptr, nullptr);
218
+ #else
219
+ webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(m_webview), js.c_str(),
220
+ nullptr, nullptr, nullptr);
221
+ #endif
222
+ return {};
223
+ }
224
+
225
+ user_script add_user_script_impl(const std::string &js) override {
226
+ auto *wk_script = webkit_user_script_new(
227
+ js.c_str(), WEBKIT_USER_CONTENT_INJECT_TOP_FRAME,
228
+ WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START, nullptr, nullptr);
229
+ webkit_user_content_manager_add_script(m_user_content_manager, wk_script);
230
+ user_script script{
231
+ js, user_script::impl_ptr{new user_script::impl{wk_script},
232
+ [](user_script::impl *p) { delete p; }}};
233
+ webkit_user_script_unref(wk_script);
234
+ return script;
235
+ }
236
+
237
+ void remove_all_user_scripts_impl(
238
+ const std::list<user_script> & /*scripts*/) override {
239
+ webkit_user_content_manager_remove_all_scripts(m_user_content_manager);
240
+ }
241
+
242
+ bool are_user_scripts_equal_impl(const user_script &first,
243
+ const user_script &second) override {
244
+ auto *wk_first = first.get_impl().get_native();
245
+ auto *wk_second = second.get_impl().get_native();
246
+ return wk_first == wk_second;
247
+ }
248
+
249
+ private:
250
+ #if GTK_MAJOR_VERSION >= 4
251
+ static char *get_string_from_js_result(JSCValue *r) {
252
+ return jsc_value_to_string(r);
253
+ }
254
+ #else
255
+ static char *get_string_from_js_result(WebKitJavascriptResult *r) {
256
+ char *s;
257
+ #if (WEBKIT_MAJOR_VERSION == 2 && WEBKIT_MINOR_VERSION >= 22) || \
258
+ WEBKIT_MAJOR_VERSION > 2
259
+ JSCValue *value = webkit_javascript_result_get_js_value(r);
260
+ s = jsc_value_to_string(value);
261
+ #else
262
+ JSGlobalContextRef ctx = webkit_javascript_result_get_global_context(r);
263
+ JSValueRef value = webkit_javascript_result_get_value(r);
264
+ JSStringRef js = JSValueToStringCopy(ctx, value, nullptr);
265
+ size_t n = JSStringGetMaximumUTF8CStringSize(js);
266
+ s = g_new(char, n);
267
+ JSStringGetUTF8CString(js, s, n);
268
+ JSStringRelease(js);
269
+ #endif
270
+ return s;
271
+ }
272
+ #endif
273
+
274
+ void window_init(void *window) {
275
+ m_window = static_cast<GtkWidget *>(window);
276
+ if (owns_window()) {
277
+ if (!gtk_compat::init_check()) {
278
+ throw exception{WEBVIEW_ERROR_UNSPECIFIED, "GTK init failed"};
279
+ }
280
+ m_window = gtk_compat::window_new();
281
+ on_window_created();
282
+ auto on_window_destroy = +[](GtkWidget *, gpointer arg) {
283
+ auto *w = static_cast<gtk_webkit_engine *>(arg);
284
+ w->m_window = nullptr;
285
+ w->on_window_destroyed();
286
+ };
287
+ g_signal_connect(G_OBJECT(m_window), "destroy",
288
+ G_CALLBACK(on_window_destroy), this);
289
+ }
290
+ webkit_dmabuf::apply_webkit_dmabuf_workaround();
291
+ // Initialize webview widget
292
+ m_webview = webkit_web_view_new();
293
+ g_object_ref_sink(m_webview);
294
+ WebKitUserContentManager *manager = m_user_content_manager =
295
+ webkit_web_view_get_user_content_manager(WEBKIT_WEB_VIEW(m_webview));
296
+ webkitgtk_compat::connect_script_message_received(
297
+ manager, "__webview__",
298
+ [this](WebKitUserContentManager *, const std::string &r) {
299
+ on_message(r);
300
+ });
301
+ webkitgtk_compat::user_content_manager_register_script_message_handler(
302
+ manager, "__webview__");
303
+ add_init_script("function(message) {\n\
304
+ return window.webkit.messageHandlers.__webview__.postMessage(message);\n\
305
+ }");
306
+ }
307
+
308
+ void window_settings(bool debug) {
309
+ WebKitSettings *settings =
310
+ webkit_web_view_get_settings(WEBKIT_WEB_VIEW(m_webview));
311
+ webkit_settings_set_javascript_can_access_clipboard(settings, true);
312
+ if (debug) {
313
+ webkit_settings_set_enable_write_console_messages_to_stdout(settings,
314
+ true);
315
+ webkit_settings_set_enable_developer_extras(settings, true);
316
+ }
317
+ }
318
+
319
+ noresult window_show() {
320
+ if (m_is_window_shown) {
321
+ return {};
322
+ }
323
+ gtk_compat::window_set_child(GTK_WINDOW(m_window), GTK_WIDGET(m_webview));
324
+ gtk_compat::widget_set_visible(GTK_WIDGET(m_webview), true);
325
+
326
+ if (owns_window()) {
327
+ gtk_widget_grab_focus(GTK_WIDGET(m_webview));
328
+ gtk_compat::widget_set_visible(GTK_WIDGET(m_window), true);
329
+ }
330
+ m_is_window_shown = true;
331
+ return {};
332
+ }
333
+
334
+ void run_event_loop_while(std::function<bool()> fn) override {
335
+ while (fn()) {
336
+ g_main_context_iteration(nullptr, TRUE);
337
+ }
338
+ }
339
+
340
+ GtkWidget *m_window{};
341
+ GtkWidget *m_webview{};
342
+ WebKitUserContentManager *m_user_content_manager{};
343
+ bool m_stop_run_loop{};
344
+ bool m_is_window_shown{};
345
+ };
346
+
347
+ } // namespace detail
348
+
349
+ using browser_engine = detail::gtk_webkit_engine;
350
+
351
+ } // namespace webview
352
+
353
+ #endif // defined(WEBVIEW_PLATFORM_LINUX) && defined(WEBVIEW_GTK)
354
+ #endif // defined(__cplusplus) && !defined(WEBVIEW_HEADER)
355
+ #endif // WEBVIEW_BACKENDS_GTK_WEBKITGTK_HH