|
|
@@ -1,3 +1,5 @@
|
|
|
+use std::path::PathBuf;
|
|
|
+
|
|
|
use libadwaita as adw;
|
|
|
|
|
|
use adw::prelude::*;
|
|
|
@@ -5,14 +7,15 @@ use adw::{Application, ApplicationWindow};
|
|
|
use gtk4::gio::ApplicationFlags;
|
|
|
use gtk4::gio::prelude::ApplicationExtManual;
|
|
|
use gtk4::glib;
|
|
|
-use gtk4::glib::prelude::Cast;
|
|
|
+use gtk4::glib::prelude::{Cast, IsA, ObjectExt};
|
|
|
use gtk4::prelude::{NativeExt, WidgetExt};
|
|
|
use gtk4::{Align, Overlay, PackType, WindowControls};
|
|
|
use gtk4::gdk::prelude::{DeviceExt, SeatExt, ToplevelExt};
|
|
|
use gtk4::gdk;
|
|
|
use webkit6::prelude::*;
|
|
|
use webkit6::{
|
|
|
- UserContentInjectedFrames, UserContentManager, UserScript, UserScriptInjectionTime, WebView,
|
|
|
+ CookieAcceptPolicy, CookiePersistentStorage, NetworkSession, UserContentInjectedFrames,
|
|
|
+ UserContentManager, UserScript, UserScriptInjectionTime, WebView,
|
|
|
};
|
|
|
|
|
|
/// JavaScript `window.webkit.messageHandlers.<name>` must match this identifier.
|
|
|
@@ -21,6 +24,9 @@ const WINDOW_DRAG_MESSAGE_HANDLER: &str = "webruntimeWindowDrag";
|
|
|
/// Mark draggable chrome in your HTML, e.g. `<div data-webruntime-window-drag>…</div>`.
|
|
|
const WINDOW_DRAG_ATTR: &str = "data-webruntime-window-drag";
|
|
|
|
|
|
+/// Lazily created shared profile; one store for every origin (WebKit partitions internally).
|
|
|
+const APP_NETWORK_SESSION_KEY: &str = "webruntime-network-session";
|
|
|
+
|
|
|
fn window_drag_bridge_source() -> String {
|
|
|
format!(
|
|
|
r#"(function () {{
|
|
|
@@ -79,7 +85,7 @@ fn begin_move_from_pointer_on_window(window: &ApplicationWindow) {
|
|
|
}
|
|
|
|
|
|
/// WebView with injected bridge + script handler; call after `window` exists so moves target it.
|
|
|
-fn build_webruntime_webview(window: &ApplicationWindow) -> WebView {
|
|
|
+fn build_webruntime_webview(window: &ApplicationWindow, network_session: &NetworkSession) -> WebView {
|
|
|
let ucm = UserContentManager::new();
|
|
|
if !ucm.register_script_message_handler(WINDOW_DRAG_MESSAGE_HANDLER, None) {
|
|
|
eprintln!("webruntime: failed to register `{WINDOW_DRAG_MESSAGE_HANDLER}` message handler");
|
|
|
@@ -101,6 +107,7 @@ fn build_webruntime_webview(window: &ApplicationWindow) -> WebView {
|
|
|
|
|
|
WebView::builder()
|
|
|
.user_content_manager(&ucm)
|
|
|
+ .network_session(network_session)
|
|
|
.build()
|
|
|
}
|
|
|
|
|
|
@@ -130,6 +137,52 @@ fn build_overlay_with_webview(webview: &WebView) -> Overlay {
|
|
|
overlay
|
|
|
}
|
|
|
|
|
|
+/// One persistent [`NetworkSession`] for the whole app: all origins share this profile directory;
|
|
|
+/// WebKit keeps cookies / storage isolated per origin inside it.
|
|
|
+fn network_session_for_app(app: &impl IsA<glib::Object>) -> NetworkSession {
|
|
|
+ unsafe {
|
|
|
+ let obj = app.as_ref();
|
|
|
+ if let Some(p) = obj.data::<NetworkSession>(APP_NETWORK_SESSION_KEY) {
|
|
|
+ return p.as_ref().clone();
|
|
|
+ }
|
|
|
+ let session = build_persistent_network_session();
|
|
|
+ obj.set_data(APP_NETWORK_SESSION_KEY, session.clone());
|
|
|
+ session
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+fn build_persistent_network_session() -> NetworkSession {
|
|
|
+ let session = match persistent_profile_paths() {
|
|
|
+ Ok((data_dir, cache_dir)) => match (data_dir.to_str(), cache_dir.to_str()) {
|
|
|
+ (Some(data), Some(cache)) => NetworkSession::new(Some(data), Some(cache)),
|
|
|
+ _ => {
|
|
|
+ eprintln!("webruntime: non-UTF8 storage path, using ephemeral session");
|
|
|
+ NetworkSession::new_ephemeral()
|
|
|
+ }
|
|
|
+ },
|
|
|
+ Err(e) => {
|
|
|
+ eprintln!("webruntime: could not create storage dirs ({e}), using ephemeral session");
|
|
|
+ NetworkSession::new_ephemeral()
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ session.set_itp_enabled(false);
|
|
|
+ session.set_persistent_credential_storage_enabled(true);
|
|
|
+
|
|
|
+ if let Some(cm) = session.cookie_manager() {
|
|
|
+ cm.set_accept_policy(CookieAcceptPolicy::Always);
|
|
|
+
|
|
|
+ if let Ok((data_dir, _)) = persistent_profile_paths() {
|
|
|
+ let cookies_db = data_dir.join("cookies.sqlite");
|
|
|
+ if let Some(path) = cookies_db.to_str() {
|
|
|
+ cm.set_persistent_storage(path, CookiePersistentStorage::Sqlite);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ session
|
|
|
+}
|
|
|
+
|
|
|
fn load_uri_into_overlay_webview(win: &ApplicationWindow, url: &str) {
|
|
|
let Some(shell) = win.content() else {
|
|
|
return;
|
|
|
@@ -137,12 +190,23 @@ fn load_uri_into_overlay_webview(win: &ApplicationWindow, url: &str) {
|
|
|
let Some(overlay) = shell.downcast_ref::<Overlay>() else {
|
|
|
return;
|
|
|
};
|
|
|
- let Some(child) = overlay.child() else {
|
|
|
+
|
|
|
+ if let Some(child) = overlay.child() {
|
|
|
+ if let Ok(webview) = child.downcast::<WebView>() {
|
|
|
+ webview.load_uri(url);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ let Some(app) = win.application() else {
|
|
|
return;
|
|
|
};
|
|
|
- if let Ok(webview) = child.downcast::<WebView>() {
|
|
|
- webview.load_uri(url);
|
|
|
- }
|
|
|
+ let session = network_session_for_app(&app);
|
|
|
+ let webview = build_webruntime_webview(win, &session);
|
|
|
+ webview.set_hexpand(true);
|
|
|
+ webview.set_vexpand(true);
|
|
|
+ overlay.set_child(Some(&webview));
|
|
|
+ webview.load_uri(url);
|
|
|
}
|
|
|
|
|
|
fn normalize_url(arg: &str) -> Result<String, &'static str> {
|
|
|
@@ -160,6 +224,20 @@ fn normalize_url(arg: &str) -> Result<String, &'static str> {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+fn persistent_profile_paths() -> Result<(PathBuf, PathBuf), std::io::Error> {
|
|
|
+ let data_dir: PathBuf = glib::user_data_dir()
|
|
|
+ .join("webruntime")
|
|
|
+ .join("profile")
|
|
|
+ .join("data");
|
|
|
+ let cache_dir: PathBuf = glib::user_cache_dir()
|
|
|
+ .join("webruntime")
|
|
|
+ .join("profile")
|
|
|
+ .join("cache");
|
|
|
+ std::fs::create_dir_all(&data_dir)?;
|
|
|
+ std::fs::create_dir_all(&cache_dir)?;
|
|
|
+ Ok((data_dir, cache_dir))
|
|
|
+}
|
|
|
+
|
|
|
fn main() -> glib::ExitCode {
|
|
|
if let Err(e) = adw::init() {
|
|
|
eprintln!("webruntime: failed to initialize libadwaita: {e}");
|
|
|
@@ -213,7 +291,8 @@ fn main() -> glib::ExitCode {
|
|
|
.default_height(768)
|
|
|
.build();
|
|
|
|
|
|
- let webview = build_webruntime_webview(&window);
|
|
|
+ let network_session = network_session_for_app(app);
|
|
|
+ let webview = build_webruntime_webview(&window, &network_session);
|
|
|
let shell = build_overlay_with_webview(&webview);
|
|
|
window.set_content(Some(&shell));
|
|
|
webview.load_uri(&url);
|