Skip to content

Commit 045ea3b

Browse files
committed
Implement distinct storages on Linux.
1 parent 81f15a7 commit 045ea3b

4 files changed

Lines changed: 69 additions & 6 deletions

File tree

webview/platform/linux/webview_linux_interface.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<arg type='i' name='g' direction='in'/>
3131
<arg type='i' name='b' direction='in'/>
3232
<arg type='i' name='a' direction='in'/>
33+
<arg type='s' name='path' direction='in'/>
3334
</method>
3435
<method name='Reload'/>
3536
<method name='Resolve'>

webview/platform/linux/webview_linux_webkitgtk.cpp

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,13 @@ bool Instance::create(Config config) {
211211

212212
const ::base::has_weak_ptr guard;
213213
std::optional<bool> success;
214+
const auto debug = _debug;
214215
const auto r = config.opaqueBg.red();
215216
const auto g = config.opaqueBg.green();
216217
const auto b = config.opaqueBg.blue();
217218
const auto a = config.opaqueBg.alpha();
218-
_helper.call_create(_debug, r, g, b, a, crl::guard(&guard, [&](
219+
const auto path = config.userDataPath;
220+
_helper.call_create(debug, r, g, b, a, path, crl::guard(&guard, [&](
219221
GObject::Object source_object,
220222
Gio::AsyncResult res) {
221223
success = _helper.call_create_finish(res, nullptr);
@@ -263,7 +265,38 @@ bool Instance::create(Config config) {
263265
} else {
264266
gtk_widget_show_all(_window);
265267
}
266-
_webview = webkit_web_view_new();
268+
269+
const auto base = config.userDataPath;
270+
const auto baseCache = base + "/cache";
271+
const auto baseData = base + "/data";
272+
273+
if (webkit_network_session_new) {
274+
_webview = GTK_WIDGET(g_object_new(
275+
WEBKIT_TYPE_WEB_VIEW,
276+
"network-session",
277+
webkit_network_session_new(baseData.c_str(), baseCache.c_str())));
278+
} else {
279+
// const auto diskCache = base + "/disk";
280+
// const auto indexedDB = base + "/db";
281+
// const auto localStorage = base + "/local";
282+
// const auto offlineAppCache = base + "/offline";
283+
// const auto webSQL = base + "/sql";
284+
WebKitWebsiteDataManager *data = webkit_website_data_manager_new(
285+
"base-cache-directory", baseCache.c_str(),
286+
"base-data-directory", baseData.c_str(),
287+
//"disk-cache-directory", diskCache.c_str(),
288+
//"indexeddb-directory", indexedDB.c_str(),
289+
//"local-storage-directory", localStorage.c_str(),
290+
//"offline-application-cache-directory", offlineAppCache.c_str(),
291+
//"websql-directory", webSQL.c_str(),
292+
nullptr);
293+
WebKitWebContext *context = webkit_web_context_new_with_website_data_manager(data);
294+
g_object_unref(data);
295+
296+
_webview = webkit_web_view_new_with_context(context);
297+
g_object_unref(context);
298+
}
299+
267300
WebKitUserContentManager *manager =
268301
webkit_web_view_get_user_content_manager(WEBKIT_WEB_VIEW(_webview));
269302
g_signal_connect_swapped(
@@ -1054,8 +1087,13 @@ void Instance::registerHelperMethodHandlers() {
10541087
int r,
10551088
int g,
10561089
int b,
1057-
int a) {
1058-
if (create({ .opaqueBg = QColor(r, g, b, a), .debug = debug })) {
1090+
int a,
1091+
const std::string &path) {
1092+
if (create({
1093+
.opaqueBg = QColor(r, g, b, a),
1094+
.userDataPath = path,
1095+
.debug = debug,
1096+
})) {
10591097
_helper.complete_create(invocation);
10601098
} else {
10611099
invocation.return_gerror(MethodError());

webview/platform/linux/webview_linux_webkitgtk_library.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ ResolveResult Resolve(bool wayland) {
4444
&& (wayland
4545
|| LOAD_LIBRARY_SYMBOL(lib, gdk_x11_surface_get_xid)
4646
|| LOAD_LIBRARY_SYMBOL(lib, gdk_x11_window_get_xid))
47-
&& LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_new)
4847
&& LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_get_type)
4948
&& LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_get_user_content_manager)
5049
&& LOAD_LIBRARY_SYMBOL(lib, webkit_user_content_manager_register_script_message_handler)
@@ -72,6 +71,19 @@ ResolveResult Resolve(bool wayland) {
7271
LOAD_LIBRARY_SYMBOL(lib, gtk_widget_show_all);
7372
LOAD_LIBRARY_SYMBOL(lib, gtk_widget_get_screen);
7473
LOAD_LIBRARY_SYMBOL(lib, webkit_javascript_result_get_js_value);
74+
{
75+
const auto available1 = LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_new_with_context)
76+
&& LOAD_LIBRARY_SYMBOL(lib, webkit_website_data_manager_new)
77+
&& LOAD_LIBRARY_SYMBOL(lib, webkit_web_context_new_with_website_data_manager);
78+
79+
const auto available2 = LOAD_LIBRARY_SYMBOL(lib, webkit_web_view_get_type)
80+
&& LOAD_LIBRARY_SYMBOL(lib, webkit_network_session_new);
81+
if (!available1 && !available2) {
82+
return ResolveResult::NoLibrary;
83+
}
84+
}
85+
LOAD_LIBRARY_SYMBOL(lib, webkit_website_data_manager_new);
86+
LOAD_LIBRARY_SYMBOL(lib, webkit_web_context_new_with_website_data_manager);
7587
{
7688
const auto available1 = LOAD_LIBRARY_SYMBOL(lib, jsc_value_to_string);
7789

webview/platform/linux/webview_linux_webkitgtk_library.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ typedef struct _WebKitUserScript WebKitUserScript;
6363
typedef struct _WebKitWebView WebKitWebView;
6464
typedef struct _WebKitSettings WebKitSettings;
6565
typedef struct _WebKitScriptDialog WebKitScriptDialog;
66+
typedef struct _WebKitWebsiteDataManager WebKitWebsiteDataManager;
67+
typedef struct _WebKitWebContext WebKitWebContext;
68+
typedef struct _WebKitNetworkSession WebKitNetworkSession;
6669

6770
typedef enum {
6871
GTK_WINDOW_TOPLEVEL,
@@ -197,7 +200,8 @@ inline void (*webkit_script_dialog_prompt_set_text)(
197200
WebKitScriptDialog *dialog,
198201
const gchar *text);
199202

200-
inline GtkWidget *(*webkit_web_view_new)(void);
203+
inline GtkWidget *(*webkit_web_view_new)();
204+
inline GtkWidget *(*webkit_web_view_new_with_context)(WebKitWebContext *context);
201205
inline GType (*webkit_web_view_get_type)(void);
202206
inline WebKitUserContentManager *(*webkit_web_view_get_user_content_manager)(
203207
WebKitWebView *web_view);
@@ -244,6 +248,14 @@ inline void (*webkit_web_view_run_javascript)(
244248
inline void (*webkit_web_view_set_background_color)(
245249
WebKitWebView *web_view,
246250
const GdkRGBA *rgba);
251+
inline WebKitWebsiteDataManager *(*webkit_website_data_manager_new)(
252+
const gchar *first_option_name,
253+
...);
254+
inline WebKitWebContext *(*webkit_web_context_new_with_website_data_manager)(
255+
WebKitWebsiteDataManager* manager);
256+
inline WebKitNetworkSession *(*webkit_network_session_new)(
257+
const char* data_directory,
258+
const char* cache_directory);
247259

248260
enum class ResolveResult {
249261
Success,

0 commit comments

Comments
 (0)