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(core): share webcontext between webviews #11043

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions .changes/share-webcontext.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"tauri": "patch:bug"
"tauri-runtime-wry": "patch:bug"
---

Fix `localStorage` not shared between webviews that use the same data directory.

4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/tauri-runtime-wry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rustc-args = ["--cfg", "docsrs"]
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
wry = { version = "0.43.1", default-features = false, features = [
amrbashir marked this conversation as resolved.
Show resolved Hide resolved
wry = { version = "0.44", default-features = false, features = [
"drag-drop",
"protocol",
"os-webview",
Expand Down
44 changes: 22 additions & 22 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ use std::{
cell::RefCell,
collections::{
hash_map::Entry::{Occupied, Vacant},
BTreeMap, HashMap,
BTreeMap, HashMap, HashSet,
},
fmt,
ops::Deref,
Expand Down Expand Up @@ -131,7 +131,7 @@ mod undecorated_resizing;
mod webview;
pub use webview::Webview;

pub type WebContextStore = Arc<Mutex<HashMap<Option<PathBuf>, WebContext>>>;
pub type WebContextStore = Arc<Mutex<HashMap<Option<PathBuf>, (WebContext, HashSet<String>)>>>;
// window
pub type WindowEventHandler = Box<dyn Fn(&WindowEvent) + Send>;
pub type WindowEventListeners = Arc<Mutex<HashMap<WindowEventId, WindowEventHandler>>>;
Expand Down Expand Up @@ -216,7 +216,6 @@ pub struct Context<T: UserEvent> {
next_webview_id: Arc<AtomicU32>,
next_window_event_id: Arc<AtomicU32>,
next_webview_event_id: Arc<AtomicU32>,
next_webcontext_id: Arc<AtomicU32>,
}

impl<T: UserEvent> Context<T> {
Expand Down Expand Up @@ -246,10 +245,6 @@ impl<T: UserEvent> Context<T> {
fn next_webview_event_id(&self) -> u32 {
self.next_webview_event_id.fetch_add(1, Ordering::Relaxed)
}

fn next_webcontext_id(&self) -> u32 {
self.next_webcontext_id.fetch_add(1, Ordering::Relaxed)
}
}

impl<T: UserEvent> Context<T> {
Expand Down Expand Up @@ -2036,7 +2031,15 @@ impl Deref for WebviewWrapper {
impl Drop for WebviewWrapper {
fn drop(&mut self) {
if Rc::get_mut(&mut self.inner).is_some() {
self.context_store.lock().unwrap().remove(&self.context_key);
let mut context_store = self.context_store.lock().unwrap();

if let Some((_, label_store)) = context_store.get_mut(&self.context_key) {
label_store.remove(&self.label);

if label_store.is_empty() {
context_store.remove(&self.context_key);
}
}
}
}
}
Expand Down Expand Up @@ -2345,7 +2348,6 @@ impl<T: UserEvent> Wry<T> {
next_webview_id: Default::default(),
next_window_event_id: Default::default(),
next_webview_event_id: Default::default(),
next_webcontext_id: Default::default(),
};

Ok(Self {
Expand Down Expand Up @@ -4101,27 +4103,25 @@ fn create_webview<T: UserEvent>(
.lock()
.expect("poisoned WebContext store");
let is_first_context = web_context.is_empty();
// force a unique WebContext when automation is false;
// the context must be stored on the HashMap because it must outlive the WebView on macOS
let automation_enabled = std::env::var("TAURI_WEBVIEW_AUTOMATION").as_deref() == Ok("true");
let web_context_key = // force a unique WebContext when automation is false;
// the context must be stored on the HashMap because it must outlive the WebView on macOS
if automation_enabled {
webview_attributes.data_directory.clone()
} else {
// unique key
let key = context.next_webcontext_id().to_string().into();
Some(key)
};
let web_context_key = webview_attributes.data_directory;
let entry = web_context.entry(web_context_key.clone());
let web_context = match entry {
Occupied(occupied) => occupied.into_mut(),
let (web_context, _) = match entry {
Occupied(occupied) => {
let occupied = occupied.into_mut();
occupied.1.insert(label.clone());
occupied
}
Vacant(vacant) => {
let mut web_context = WebContext::new(webview_attributes.data_directory);
let mut web_context = WebContext::new(web_context_key.clone());
web_context.set_allows_automation(if automation_enabled {
is_first_context
} else {
false
});
vacant.insert(web_context)
vacant.insert((web_context, [label.clone()].into()))
}
};

Expand Down
Loading