Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(linux): don't share UserContentManager between webviews (v0.24) #1325

Merged
merged 7 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/linux-shared-webcontext-ipc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Fix Linux IPC handler and initialization scripts when sharing a WebContext between multiple WebViews.
35 changes: 12 additions & 23 deletions src/webview/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ use gtk::prelude::*;
#[cfg(any(debug_assertions, feature = "devtools"))]
use std::sync::atomic::{AtomicBool, Ordering};
use std::{
amrbashir marked this conversation as resolved.
Show resolved Hide resolved
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
rc::Rc,
sync::{Arc, Mutex},
};
use url::Url;
use webkit2gtk::{
traits::*, LoadEvent, NavigationPolicyDecision, PolicyDecisionType, URIRequest,
UserContentInjectedFrames, UserScript, UserScriptInjectionTime, WebView, WebViewBuilder,
UserContentInjectedFrames, UserContentManager, UserScript, UserScriptInjectionTime, WebView,
WebViewBuilder,
};
use webkit2gtk_sys::{
webkit_get_major_version, webkit_get_micro_version, webkit_get_minor_version,
Expand Down Expand Up @@ -63,9 +62,11 @@ impl InnerWebView {
}
};

let manager = UserContentManager::new();

let webview = {
let mut webview = WebViewBuilder::new();
webview = webview.user_content_manager(web_context.manager());
webview = webview.user_content_manager(&manager);
webview = webview.web_context(web_context.context());
webview = webview.is_controlled_by_automation(web_context.allows_automation());
webview.build()
Expand All @@ -77,15 +78,6 @@ impl InnerWebView {
let webview = Rc::new(webview);
let w = window_rc.clone();
let ipc_handler = attributes.ipc_handler.take();
let manager = web_context.manager();

// Use the window hash as the script handler name to prevent from conflict when sharing same
// web context.
let window_hash = {
let mut hasher = DefaultHasher::new();
w.id().hash(&mut hasher);
hasher.finish().to_string()
};

amrbashir marked this conversation as resolved.
Show resolved Hide resolved
// Connect before registering as recommended by the docs
manager.connect_script_message_received(None, move |_m, msg| {
Expand All @@ -99,8 +91,8 @@ impl InnerWebView {
}
});

// Register the handler we just connected
manager.register_script_message_handler(&window_hash);
// Register handler on JS side
manager.register_script_message_handler("ipc");

// Allow the webview to close it's own window
let close_window = window_rc.clone();
Expand Down Expand Up @@ -343,22 +335,19 @@ impl InnerWebView {
};

// Initialize message handler
let mut init = String::with_capacity(115 + 20 + 22);
init.push_str("Object.defineProperty(window, 'ipc', {value: Object.freeze({postMessage:function(x){window.webkit.messageHandlers[\"");
init.push_str(&window_hash);
init.push_str("\"].postMessage(x)}})})");
w.init(&init)?;
w.init("Object.defineProperty(window, 'ipc', { value: Object.freeze({ postMessage: function(x) { window.webkit.messageHandlers['ipc'].postMessage(x) } }) })")?;
amrbashir marked this conversation as resolved.
Show resolved Hide resolved

// Initialize scripts
for js in attributes.initialization_scripts {
w.init(&js)?;
}

for (name, handler) in attributes.custom_protocols {
match web_context.register_uri_scheme(&name, handler) {
match web_context.try_register_uri_scheme(&name, handler) {
// Swallow duplicate scheme errors to preserve current behavior.
// FIXME: we should log this error in the future
Err(Error::DuplicateCustomProtocol(_)) => (),
Err(Error::DuplicateCustomProtocol(_)) => {
log::warn!("Ignoring already registered custom protocol {name}");
}
Err(e) => return Err(e),
Ok(_) => (),
}
Expand Down
13 changes: 2 additions & 11 deletions src/webview/webkitgtk/web_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ use std::{
};
use url::Url;
use webkit2gtk::{
traits::*, ApplicationInfo, CookiePersistentStorage, LoadEvent, URIRequest, UserContentManager,
WebContext, WebContextBuilder, WebView, WebsiteDataManagerBuilder,
traits::*, ApplicationInfo, CookiePersistentStorage, LoadEvent, URIRequest, WebContext,
WebContextBuilder, WebView, WebsiteDataManagerBuilder,
};

#[derive(Debug)]
pub struct WebContextImpl {
context: WebContext,
manager: UserContentManager,
webview_uri_loader: Rc<WebviewUriLoader>,
registered_protocols: HashSet<String>,
automation: bool,
Expand Down Expand Up @@ -82,7 +81,6 @@ impl WebContextImpl {
Self {
context,
automation,
manager: UserContentManager::new(),
registered_protocols: Default::default(),
webview_uri_loader: Rc::default(),
app_info: Some(app_info),
Expand All @@ -101,9 +99,6 @@ pub trait WebContextExt {
/// The GTK [`WebContext`] of all webviews in the context.
fn context(&self) -> &WebContext;

/// The GTK [`UserContentManager`] of all webviews in the context.
fn manager(&self) -> &UserContentManager;

/// Register a custom protocol to the web context.
///
/// When duplicate schemes are registered, the duplicate handler will still be submitted and the
Expand Down Expand Up @@ -150,10 +145,6 @@ impl WebContextExt for super::WebContext {
&self.os.context
}

fn manager(&self) -> &UserContentManager {
&self.os.manager
}

fn register_uri_scheme<F>(&mut self, name: &str, handler: F) -> crate::Result<()>
where
F: Fn(&Request<Vec<u8>>) -> crate::Result<Response<Cow<'static, [u8]>>> + 'static,
Expand Down
Loading