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.
- package/LICENSE +21 -0
- package/README.md +95 -0
- package/bin/janela.mjs +271 -0
- package/package.json +43 -0
- package/runtime/janela.ts +193 -0
- package/shim/wvshim.cc +201 -0
- package/templates/index.html +48 -0
- package/templates/janela.conf.json +10 -0
- package/templates/main.ts +37 -0
- package/vendor-webview/LICENSE +22 -0
- package/vendor-webview/core/include/webview/api.h +251 -0
- package/vendor-webview/core/include/webview/backends.hh +40 -0
- package/vendor-webview/core/include/webview/c_api_impl.hh +258 -0
- package/vendor-webview/core/include/webview/detail/backends/cocoa_webkit.hh +626 -0
- package/vendor-webview/core/include/webview/detail/backends/gtk_webkitgtk.hh +355 -0
- package/vendor-webview/core/include/webview/detail/backends/win32_edge.hh +909 -0
- package/vendor-webview/core/include/webview/detail/basic_result.hh +119 -0
- package/vendor-webview/core/include/webview/detail/engine_base.hh +382 -0
- package/vendor-webview/core/include/webview/detail/exceptions.hh +42 -0
- package/vendor-webview/core/include/webview/detail/json.hh +337 -0
- package/vendor-webview/core/include/webview/detail/native_library.hh +170 -0
- package/vendor-webview/core/include/webview/detail/optional.hh +118 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSApplication.hh +107 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSBundle.hh +56 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSEvent.hh +71 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSInvocation.hh +66 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSMethodSignature.hh +52 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSNotification.hh +51 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSNumber.hh +53 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSObject.hh +51 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSOpenPanel.hh +71 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSPoint.hh +53 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSRect.hh +53 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSSavePanel.hh +52 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSSize.hh +53 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSString.hh +87 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSURL.hh +63 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSURLRequest.hh +52 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSValue.hh +56 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSView.hh +91 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/NSWindow.hh +137 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/cocoa.hh +59 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/cocoa/types.hh +49 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/objc/Class.hh +55 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/objc/autoreleasepool.hh +70 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/objc/invoke.hh +75 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/objc/memory.hh +57 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/objc/objc.hh +47 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/WKOpenPanelParameters.hh +57 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/WKScriptMessage.hh +51 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/WKUserContentController.hh +61 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/WKUserScript.hh +71 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/WKWebView.hh +108 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/WKWebViewConfiguration.hh +60 -0
- package/vendor-webview/core/include/webview/detail/platform/darwin/webkit/webkit.hh +46 -0
- package/vendor-webview/core/include/webview/detail/platform/linux/gtk/compat.hh +137 -0
- package/vendor-webview/core/include/webview/detail/platform/linux/webkitgtk/compat.hh +142 -0
- package/vendor-webview/core/include/webview/detail/platform/linux/webkitgtk/dmabuf.hh +167 -0
- package/vendor-webview/core/include/webview/detail/platform/windows/com_init_wrapper.hh +126 -0
- package/vendor-webview/core/include/webview/detail/platform/windows/dpi.hh +161 -0
- package/vendor-webview/core/include/webview/detail/platform/windows/dwmapi.hh +67 -0
- package/vendor-webview/core/include/webview/detail/platform/windows/iid.hh +78 -0
- package/vendor-webview/core/include/webview/detail/platform/windows/ntdll.hh +58 -0
- package/vendor-webview/core/include/webview/detail/platform/windows/reg_key.hh +135 -0
- package/vendor-webview/core/include/webview/detail/platform/windows/shcore.hh +59 -0
- package/vendor-webview/core/include/webview/detail/platform/windows/theme.hh +76 -0
- package/vendor-webview/core/include/webview/detail/platform/windows/user32.hh +90 -0
- package/vendor-webview/core/include/webview/detail/platform/windows/version.hh +151 -0
- package/vendor-webview/core/include/webview/detail/platform/windows/webview2/loader.hh +391 -0
- package/vendor-webview/core/include/webview/detail/user_script.hh +76 -0
- package/vendor-webview/core/include/webview/detail/utility/string.hh +99 -0
- package/vendor-webview/core/include/webview/errors.h +67 -0
- package/vendor-webview/core/include/webview/errors.hh +85 -0
- package/vendor-webview/core/include/webview/json_deprecated.hh +61 -0
- package/vendor-webview/core/include/webview/macros.h +123 -0
- package/vendor-webview/core/include/webview/types.h +81 -0
- package/vendor-webview/core/include/webview/types.hh +48 -0
- package/vendor-webview/core/include/webview/version.h +67 -0
- package/vendor-webview/core/include/webview/webview.h +32 -0
- package/vendor-webview/core/include/webview.h +15 -0
|
@@ -0,0 +1,337 @@
|
|
|
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_DETAIL_JSON_HH
|
|
27
|
+
#define WEBVIEW_DETAIL_JSON_HH
|
|
28
|
+
|
|
29
|
+
#if defined(__cplusplus) && !defined(WEBVIEW_HEADER)
|
|
30
|
+
|
|
31
|
+
#include <cassert>
|
|
32
|
+
#include <cstring>
|
|
33
|
+
#include <string>
|
|
34
|
+
|
|
35
|
+
namespace webview {
|
|
36
|
+
namespace detail {
|
|
37
|
+
|
|
38
|
+
inline int json_parse_c(const char *s, size_t sz, const char *key, size_t keysz,
|
|
39
|
+
const char **value, size_t *valuesz) {
|
|
40
|
+
enum {
|
|
41
|
+
JSON_STATE_VALUE,
|
|
42
|
+
JSON_STATE_LITERAL,
|
|
43
|
+
JSON_STATE_STRING,
|
|
44
|
+
JSON_STATE_ESCAPE,
|
|
45
|
+
JSON_STATE_UTF8
|
|
46
|
+
} state = JSON_STATE_VALUE;
|
|
47
|
+
const char *k = nullptr;
|
|
48
|
+
int index = 1;
|
|
49
|
+
int depth = 0;
|
|
50
|
+
int utf8_bytes = 0;
|
|
51
|
+
|
|
52
|
+
*value = nullptr;
|
|
53
|
+
*valuesz = 0;
|
|
54
|
+
|
|
55
|
+
if (key == nullptr) {
|
|
56
|
+
index = static_cast<decltype(index)>(keysz);
|
|
57
|
+
if (index < 0) {
|
|
58
|
+
return -1;
|
|
59
|
+
}
|
|
60
|
+
keysz = 0;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
for (; sz > 0; s++, sz--) {
|
|
64
|
+
enum {
|
|
65
|
+
JSON_ACTION_NONE,
|
|
66
|
+
JSON_ACTION_START,
|
|
67
|
+
JSON_ACTION_END,
|
|
68
|
+
JSON_ACTION_START_STRUCT,
|
|
69
|
+
JSON_ACTION_END_STRUCT
|
|
70
|
+
} action = JSON_ACTION_NONE;
|
|
71
|
+
auto c = static_cast<unsigned char>(*s);
|
|
72
|
+
switch (state) {
|
|
73
|
+
case JSON_STATE_VALUE:
|
|
74
|
+
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == ',' ||
|
|
75
|
+
c == ':') {
|
|
76
|
+
continue;
|
|
77
|
+
} else if (c == '"') {
|
|
78
|
+
action = JSON_ACTION_START;
|
|
79
|
+
state = JSON_STATE_STRING;
|
|
80
|
+
} else if (c == '{' || c == '[') {
|
|
81
|
+
action = JSON_ACTION_START_STRUCT;
|
|
82
|
+
} else if (c == '}' || c == ']') {
|
|
83
|
+
action = JSON_ACTION_END_STRUCT;
|
|
84
|
+
} else if (c == 't' || c == 'f' || c == 'n' || c == '-' ||
|
|
85
|
+
(c >= '0' && c <= '9')) {
|
|
86
|
+
action = JSON_ACTION_START;
|
|
87
|
+
state = JSON_STATE_LITERAL;
|
|
88
|
+
} else {
|
|
89
|
+
return -1;
|
|
90
|
+
}
|
|
91
|
+
break;
|
|
92
|
+
case JSON_STATE_LITERAL:
|
|
93
|
+
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == ',' ||
|
|
94
|
+
c == ']' || c == '}' || c == ':') {
|
|
95
|
+
state = JSON_STATE_VALUE;
|
|
96
|
+
s--;
|
|
97
|
+
sz++;
|
|
98
|
+
action = JSON_ACTION_END;
|
|
99
|
+
} else if (c < 32 || c > 126) {
|
|
100
|
+
return -1;
|
|
101
|
+
} // fallthrough
|
|
102
|
+
case JSON_STATE_STRING:
|
|
103
|
+
if (c < 32 || (c > 126 && c < 192)) {
|
|
104
|
+
return -1;
|
|
105
|
+
} else if (c == '"') {
|
|
106
|
+
action = JSON_ACTION_END;
|
|
107
|
+
state = JSON_STATE_VALUE;
|
|
108
|
+
} else if (c == '\\') {
|
|
109
|
+
state = JSON_STATE_ESCAPE;
|
|
110
|
+
} else if (c >= 192 && c < 224) {
|
|
111
|
+
utf8_bytes = 1;
|
|
112
|
+
state = JSON_STATE_UTF8;
|
|
113
|
+
} else if (c >= 224 && c < 240) {
|
|
114
|
+
utf8_bytes = 2;
|
|
115
|
+
state = JSON_STATE_UTF8;
|
|
116
|
+
} else if (c >= 240 && c < 247) {
|
|
117
|
+
utf8_bytes = 3;
|
|
118
|
+
state = JSON_STATE_UTF8;
|
|
119
|
+
} else if (c >= 128 && c < 192) {
|
|
120
|
+
return -1;
|
|
121
|
+
}
|
|
122
|
+
break;
|
|
123
|
+
case JSON_STATE_ESCAPE:
|
|
124
|
+
if (c == '"' || c == '\\' || c == '/' || c == 'b' || c == 'f' ||
|
|
125
|
+
c == 'n' || c == 'r' || c == 't' || c == 'u') {
|
|
126
|
+
state = JSON_STATE_STRING;
|
|
127
|
+
} else {
|
|
128
|
+
return -1;
|
|
129
|
+
}
|
|
130
|
+
break;
|
|
131
|
+
case JSON_STATE_UTF8:
|
|
132
|
+
if (c < 128 || c > 191) {
|
|
133
|
+
return -1;
|
|
134
|
+
}
|
|
135
|
+
utf8_bytes--;
|
|
136
|
+
if (utf8_bytes == 0) {
|
|
137
|
+
state = JSON_STATE_STRING;
|
|
138
|
+
}
|
|
139
|
+
break;
|
|
140
|
+
default:
|
|
141
|
+
return -1;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (action == JSON_ACTION_END_STRUCT) {
|
|
145
|
+
depth--;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (depth == 1) {
|
|
149
|
+
if (action == JSON_ACTION_START || action == JSON_ACTION_START_STRUCT) {
|
|
150
|
+
if (index == 0) {
|
|
151
|
+
*value = s;
|
|
152
|
+
} else if (keysz > 0 && index == 1) {
|
|
153
|
+
k = s;
|
|
154
|
+
} else {
|
|
155
|
+
index--;
|
|
156
|
+
}
|
|
157
|
+
} else if (action == JSON_ACTION_END ||
|
|
158
|
+
action == JSON_ACTION_END_STRUCT) {
|
|
159
|
+
if (*value != nullptr && index == 0) {
|
|
160
|
+
*valuesz = static_cast<size_t>(s + 1 - *value);
|
|
161
|
+
return 0;
|
|
162
|
+
} else if (keysz > 0 && k != nullptr) {
|
|
163
|
+
if (keysz == static_cast<size_t>(s - k - 1) &&
|
|
164
|
+
memcmp(key, k + 1, keysz) == 0) {
|
|
165
|
+
index = 0;
|
|
166
|
+
} else {
|
|
167
|
+
index = 2;
|
|
168
|
+
}
|
|
169
|
+
k = nullptr;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (action == JSON_ACTION_START_STRUCT) {
|
|
175
|
+
depth++;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return -1;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
constexpr bool is_json_special_char(char c) {
|
|
182
|
+
return c == '"' || c == '\\' || c == '\b' || c == '\f' || c == '\n' ||
|
|
183
|
+
c == '\r' || c == '\t';
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
constexpr bool is_ascii_control_char(char c) {
|
|
187
|
+
// `char` may be unsigned on some targets (e.g. ARM64).
|
|
188
|
+
return static_cast<unsigned char>(c) <= 0x1f;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
inline std::string json_escape(const std::string &s, bool add_quotes = true) {
|
|
192
|
+
// Calculate the size of the resulting string.
|
|
193
|
+
// Add space for the double quotes.
|
|
194
|
+
size_t required_length = add_quotes ? 2 : 0;
|
|
195
|
+
for (auto c : s) {
|
|
196
|
+
if (is_json_special_char(c)) {
|
|
197
|
+
// '\' and a single following character
|
|
198
|
+
required_length += 2;
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
if (is_ascii_control_char(c)) {
|
|
202
|
+
// '\', 'u', 4 digits
|
|
203
|
+
required_length += 6;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
++required_length;
|
|
207
|
+
}
|
|
208
|
+
// Allocate memory for resulting string only once.
|
|
209
|
+
std::string result;
|
|
210
|
+
result.reserve(required_length);
|
|
211
|
+
if (add_quotes) {
|
|
212
|
+
result += '"';
|
|
213
|
+
}
|
|
214
|
+
// Copy string while escaping characters.
|
|
215
|
+
for (auto c : s) {
|
|
216
|
+
if (is_json_special_char(c)) {
|
|
217
|
+
static constexpr char special_escape_table[256] =
|
|
218
|
+
"\0\0\0\0\0\0\0\0btn\0fr\0\0"
|
|
219
|
+
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
|
220
|
+
"\0\0\"\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
|
221
|
+
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
|
222
|
+
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
|
223
|
+
"\0\0\0\0\0\0\0\0\0\0\0\0\\";
|
|
224
|
+
result += '\\';
|
|
225
|
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index)
|
|
226
|
+
result += special_escape_table[static_cast<unsigned char>(c)];
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
if (is_ascii_control_char(c)) {
|
|
230
|
+
// Escape as \u00xx
|
|
231
|
+
static constexpr char hex_alphabet[]{"0123456789abcdef"};
|
|
232
|
+
auto uc = static_cast<unsigned char>(c);
|
|
233
|
+
auto h = (uc >> 4) & 0x0f;
|
|
234
|
+
auto l = uc & 0x0f;
|
|
235
|
+
result += "\\u00";
|
|
236
|
+
// NOLINTBEGIN(cppcoreguidelines-pro-bounds-constant-array-index)
|
|
237
|
+
result += hex_alphabet[h];
|
|
238
|
+
result += hex_alphabet[l];
|
|
239
|
+
// NOLINTEND(cppcoreguidelines-pro-bounds-constant-array-index)
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
result += c;
|
|
243
|
+
}
|
|
244
|
+
if (add_quotes) {
|
|
245
|
+
result += '"';
|
|
246
|
+
}
|
|
247
|
+
// Should have calculated the exact amount of memory needed
|
|
248
|
+
assert(required_length == result.size());
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
inline int json_unescape(const char *s, size_t n, char *out) {
|
|
253
|
+
int r = 0;
|
|
254
|
+
if (*s++ != '"') {
|
|
255
|
+
return -1;
|
|
256
|
+
}
|
|
257
|
+
while (n > 2) {
|
|
258
|
+
char c = *s;
|
|
259
|
+
if (c == '\\') {
|
|
260
|
+
s++;
|
|
261
|
+
n--;
|
|
262
|
+
switch (*s) {
|
|
263
|
+
case 'b':
|
|
264
|
+
c = '\b';
|
|
265
|
+
break;
|
|
266
|
+
case 'f':
|
|
267
|
+
c = '\f';
|
|
268
|
+
break;
|
|
269
|
+
case 'n':
|
|
270
|
+
c = '\n';
|
|
271
|
+
break;
|
|
272
|
+
case 'r':
|
|
273
|
+
c = '\r';
|
|
274
|
+
break;
|
|
275
|
+
case 't':
|
|
276
|
+
c = '\t';
|
|
277
|
+
break;
|
|
278
|
+
case '\\':
|
|
279
|
+
c = '\\';
|
|
280
|
+
break;
|
|
281
|
+
case '/':
|
|
282
|
+
c = '/';
|
|
283
|
+
break;
|
|
284
|
+
case '\"':
|
|
285
|
+
c = '\"';
|
|
286
|
+
break;
|
|
287
|
+
default: // TODO: support unicode decoding
|
|
288
|
+
return -1;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
if (out != nullptr) {
|
|
292
|
+
*out++ = c;
|
|
293
|
+
}
|
|
294
|
+
s++;
|
|
295
|
+
n--;
|
|
296
|
+
r++;
|
|
297
|
+
}
|
|
298
|
+
if (*s != '"') {
|
|
299
|
+
return -1;
|
|
300
|
+
}
|
|
301
|
+
if (out != nullptr) {
|
|
302
|
+
*out = '\0';
|
|
303
|
+
}
|
|
304
|
+
return r;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
inline std::string json_parse(const std::string &s, const std::string &key,
|
|
308
|
+
const int index) {
|
|
309
|
+
const char *value;
|
|
310
|
+
size_t value_sz;
|
|
311
|
+
if (key.empty()) {
|
|
312
|
+
json_parse_c(s.c_str(), s.length(), nullptr, index, &value, &value_sz);
|
|
313
|
+
} else {
|
|
314
|
+
json_parse_c(s.c_str(), s.length(), key.c_str(), key.length(), &value,
|
|
315
|
+
&value_sz);
|
|
316
|
+
}
|
|
317
|
+
if (value != nullptr) {
|
|
318
|
+
if (value[0] != '"') {
|
|
319
|
+
return {value, value_sz};
|
|
320
|
+
}
|
|
321
|
+
int n = json_unescape(value, value_sz, nullptr);
|
|
322
|
+
if (n > 0) {
|
|
323
|
+
char *decoded = new char[n + 1];
|
|
324
|
+
json_unescape(value, value_sz, decoded);
|
|
325
|
+
std::string result(decoded, n);
|
|
326
|
+
delete[] decoded;
|
|
327
|
+
return result;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return "";
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
} // namespace detail
|
|
334
|
+
} // namespace webview
|
|
335
|
+
|
|
336
|
+
#endif // defined(__cplusplus) && !defined(WEBVIEW_HEADER)
|
|
337
|
+
#endif // WEBVIEW_DETAIL_JSON_HH
|
|
@@ -0,0 +1,170 @@
|
|
|
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_DETAIL_NATIVE_LIBRARY_HH
|
|
27
|
+
#define WEBVIEW_DETAIL_NATIVE_LIBRARY_HH
|
|
28
|
+
|
|
29
|
+
#if defined(__cplusplus) && !defined(WEBVIEW_HEADER)
|
|
30
|
+
|
|
31
|
+
#include "utility/string.hh"
|
|
32
|
+
|
|
33
|
+
#include <string>
|
|
34
|
+
|
|
35
|
+
#if defined(_WIN32)
|
|
36
|
+
#ifndef WIN32_LEAN_AND_MEAN
|
|
37
|
+
#define WIN32_LEAN_AND_MEAN
|
|
38
|
+
#endif
|
|
39
|
+
#include <windows.h>
|
|
40
|
+
#else
|
|
41
|
+
#include <dlfcn.h>
|
|
42
|
+
#endif
|
|
43
|
+
|
|
44
|
+
namespace webview {
|
|
45
|
+
namespace detail {
|
|
46
|
+
|
|
47
|
+
// Holds a symbol name and associated type for code clarity.
|
|
48
|
+
template <typename T> class library_symbol {
|
|
49
|
+
public:
|
|
50
|
+
using type = T;
|
|
51
|
+
|
|
52
|
+
constexpr explicit library_symbol(const char *name) : m_name(name) {}
|
|
53
|
+
constexpr const char *get_name() const { return m_name; }
|
|
54
|
+
|
|
55
|
+
private:
|
|
56
|
+
const char *m_name;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// Loads a native shared library and allows one to get addresses for those
|
|
60
|
+
// symbols.
|
|
61
|
+
class native_library {
|
|
62
|
+
public:
|
|
63
|
+
native_library() = default;
|
|
64
|
+
|
|
65
|
+
explicit native_library(const std::string &name)
|
|
66
|
+
: m_handle{load_library(name)} {}
|
|
67
|
+
|
|
68
|
+
#ifdef _WIN32
|
|
69
|
+
explicit native_library(const std::wstring &name)
|
|
70
|
+
: m_handle{load_library(name)} {}
|
|
71
|
+
#endif
|
|
72
|
+
|
|
73
|
+
~native_library() {
|
|
74
|
+
if (m_handle) {
|
|
75
|
+
#ifdef _WIN32
|
|
76
|
+
FreeLibrary(m_handle);
|
|
77
|
+
#else
|
|
78
|
+
dlclose(m_handle);
|
|
79
|
+
#endif
|
|
80
|
+
m_handle = nullptr;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
native_library(const native_library &other) = delete;
|
|
85
|
+
native_library &operator=(const native_library &other) = delete;
|
|
86
|
+
native_library(native_library &&other) noexcept { *this = std::move(other); }
|
|
87
|
+
|
|
88
|
+
native_library &operator=(native_library &&other) noexcept {
|
|
89
|
+
if (this == &other) {
|
|
90
|
+
return *this;
|
|
91
|
+
}
|
|
92
|
+
m_handle = other.m_handle;
|
|
93
|
+
other.m_handle = nullptr;
|
|
94
|
+
return *this;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Returns true if the library is currently loaded; otherwise false.
|
|
98
|
+
operator bool() const { return is_loaded(); }
|
|
99
|
+
|
|
100
|
+
// Get the address for the specified symbol or nullptr if not found.
|
|
101
|
+
template <typename Symbol>
|
|
102
|
+
typename Symbol::type get(const Symbol &symbol) const {
|
|
103
|
+
if (is_loaded()) {
|
|
104
|
+
// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast)
|
|
105
|
+
#ifdef _WIN32
|
|
106
|
+
#ifdef __GNUC__
|
|
107
|
+
#pragma GCC diagnostic push
|
|
108
|
+
#pragma GCC diagnostic ignored "-Wcast-function-type"
|
|
109
|
+
#endif
|
|
110
|
+
return reinterpret_cast<typename Symbol::type>(
|
|
111
|
+
GetProcAddress(m_handle, symbol.get_name()));
|
|
112
|
+
#ifdef __GNUC__
|
|
113
|
+
#pragma GCC diagnostic pop
|
|
114
|
+
#endif
|
|
115
|
+
#else
|
|
116
|
+
return reinterpret_cast<typename Symbol::type>(
|
|
117
|
+
dlsym(m_handle, symbol.get_name()));
|
|
118
|
+
#endif
|
|
119
|
+
// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
|
|
120
|
+
}
|
|
121
|
+
return nullptr;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Returns true if the library is currently loaded; otherwise false.
|
|
125
|
+
bool is_loaded() const { return !!m_handle; }
|
|
126
|
+
|
|
127
|
+
void detach() { m_handle = nullptr; }
|
|
128
|
+
|
|
129
|
+
// Returns true if the library by the given name is currently loaded; otherwise false.
|
|
130
|
+
static inline bool is_loaded(const std::string &name) {
|
|
131
|
+
#ifdef _WIN32
|
|
132
|
+
auto handle = GetModuleHandleW(widen_string(name).c_str());
|
|
133
|
+
#else
|
|
134
|
+
auto handle = dlopen(name.c_str(), RTLD_NOW | RTLD_NOLOAD);
|
|
135
|
+
if (handle) {
|
|
136
|
+
dlclose(handle);
|
|
137
|
+
}
|
|
138
|
+
#endif
|
|
139
|
+
return !!handle;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private:
|
|
143
|
+
#ifdef _WIN32
|
|
144
|
+
using mod_handle_t = HMODULE;
|
|
145
|
+
#else
|
|
146
|
+
using mod_handle_t = void *;
|
|
147
|
+
#endif
|
|
148
|
+
|
|
149
|
+
static inline mod_handle_t load_library(const std::string &name) {
|
|
150
|
+
#ifdef _WIN32
|
|
151
|
+
return load_library(widen_string(name));
|
|
152
|
+
#else
|
|
153
|
+
return dlopen(name.c_str(), RTLD_NOW);
|
|
154
|
+
#endif
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
#ifdef _WIN32
|
|
158
|
+
static inline mod_handle_t load_library(const std::wstring &name) {
|
|
159
|
+
return LoadLibraryW(name.c_str());
|
|
160
|
+
}
|
|
161
|
+
#endif
|
|
162
|
+
|
|
163
|
+
mod_handle_t m_handle{};
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
} // namespace detail
|
|
167
|
+
} // namespace webview
|
|
168
|
+
|
|
169
|
+
#endif // defined(__cplusplus) && !defined(WEBVIEW_HEADER)
|
|
170
|
+
#endif // WEBVIEW_DETAIL_NATIVE_LIBRARY_HH
|
|
@@ -0,0 +1,118 @@
|
|
|
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_DETAIL_OPTIONAL_HH
|
|
27
|
+
#define WEBVIEW_DETAIL_OPTIONAL_HH
|
|
28
|
+
|
|
29
|
+
#if defined(__cplusplus) && !defined(WEBVIEW_HEADER)
|
|
30
|
+
|
|
31
|
+
#include "exceptions.hh"
|
|
32
|
+
|
|
33
|
+
#include <new>
|
|
34
|
+
#include <type_traits>
|
|
35
|
+
#include <utility>
|
|
36
|
+
|
|
37
|
+
namespace webview {
|
|
38
|
+
namespace detail {
|
|
39
|
+
|
|
40
|
+
template <typename T> class optional {
|
|
41
|
+
public:
|
|
42
|
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init, hicpp-member-init)
|
|
43
|
+
optional() = default;
|
|
44
|
+
|
|
45
|
+
optional(const T &other) noexcept : m_has_data{true} {
|
|
46
|
+
new (&m_data) T{other};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
optional(T &&other) noexcept : m_has_data{true} {
|
|
50
|
+
new (&m_data) T{std::move(other)};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
optional(const optional<T> &other) noexcept { *this = other; }
|
|
54
|
+
|
|
55
|
+
optional &operator=(const optional<T> &other) noexcept {
|
|
56
|
+
if (this == &other) {
|
|
57
|
+
return *this;
|
|
58
|
+
}
|
|
59
|
+
m_has_data = other.has_value();
|
|
60
|
+
if (m_has_data) {
|
|
61
|
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
|
|
62
|
+
new (&m_data) T{*reinterpret_cast<const T *>(&other.m_data)};
|
|
63
|
+
}
|
|
64
|
+
return *this;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
optional(optional<T> &&other) noexcept { *this = std::move(other); }
|
|
68
|
+
|
|
69
|
+
optional &operator=(optional<T> &&other) noexcept {
|
|
70
|
+
if (this == &other) {
|
|
71
|
+
return *this;
|
|
72
|
+
}
|
|
73
|
+
m_has_data = other.has_value();
|
|
74
|
+
if (m_has_data) {
|
|
75
|
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
|
|
76
|
+
new (&m_data) T{std::move(*reinterpret_cast<T *>(&other.m_data))};
|
|
77
|
+
}
|
|
78
|
+
return *this;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
~optional() {
|
|
82
|
+
if (m_has_data) {
|
|
83
|
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
|
|
84
|
+
reinterpret_cast<T *>(&m_data)->~T();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const T &get() const {
|
|
89
|
+
if (!m_has_data) {
|
|
90
|
+
throw bad_access{};
|
|
91
|
+
}
|
|
92
|
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
|
|
93
|
+
return *reinterpret_cast<const T *>(&m_data);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
T &get() {
|
|
97
|
+
if (!m_has_data) {
|
|
98
|
+
throw bad_access{};
|
|
99
|
+
}
|
|
100
|
+
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
|
|
101
|
+
return *reinterpret_cast<T *>(&m_data);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
bool has_value() const { return m_has_data; }
|
|
105
|
+
|
|
106
|
+
private:
|
|
107
|
+
// NOLINTNEXTLINE(bugprone-sizeof-expression): pointer to aggregate is OK
|
|
108
|
+
typename std::aligned_storage<sizeof(T), alignof(T)>::type m_data;
|
|
109
|
+
bool m_has_data{};
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
template <> class optional<void> {};
|
|
113
|
+
|
|
114
|
+
} // namespace detail
|
|
115
|
+
} // namespace webview
|
|
116
|
+
|
|
117
|
+
#endif // defined(__cplusplus) && !defined(WEBVIEW_HEADER)
|
|
118
|
+
#endif // WEBVIEW_DETAIL_OPTIONAL_HH
|