Skip to content

Commit 9d8b377

Browse files
authored
feat(tauri-runtime-wry): drop the WebContext on WebView drop (#5240)
1 parent 4137ab4 commit 9d8b377

2 files changed

Lines changed: 48 additions & 16 deletions

File tree

.changes/drop-web-context.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-runtime-wry": patch
3+
---
4+
5+
Drop the WebContext when the WebView is dropped.

core/tauri-runtime-wry/src/lib.rs

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,10 +1550,30 @@ impl<T: UserEvent> Dispatch<T> for WryDispatcher<T> {
15501550

15511551
#[derive(Clone)]
15521552
enum WindowHandle {
1553-
Webview(Arc<WebView>),
1553+
Webview {
1554+
inner: Arc<WebView>,
1555+
context_store: WebContextStore,
1556+
// the key of the WebContext if it's not shared
1557+
context_key: Option<PathBuf>,
1558+
},
15541559
Window(Arc<Window>),
15551560
}
15561561

1562+
impl Drop for WindowHandle {
1563+
fn drop(&mut self) {
1564+
if let Self::Webview {
1565+
inner,
1566+
context_store,
1567+
context_key,
1568+
} = self
1569+
{
1570+
if Arc::get_mut(inner).is_some() {
1571+
context_store.lock().unwrap().remove(context_key);
1572+
}
1573+
}
1574+
}
1575+
}
1576+
15571577
impl fmt::Debug for WindowHandle {
15581578
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
15591579
Ok(())
@@ -1566,7 +1586,7 @@ impl Deref for WindowHandle {
15661586
#[inline(always)]
15671587
fn deref(&self) -> &Window {
15681588
match self {
1569-
Self::Webview(w) => w.window(),
1589+
Self::Webview { inner, .. } => inner.window(),
15701590
Self::Window(w) => w,
15711591
}
15721592
}
@@ -1576,7 +1596,7 @@ impl WindowHandle {
15761596
fn inner_size(&self) -> WryPhysicalSize<u32> {
15771597
match self {
15781598
WindowHandle::Window(w) => w.inner_size(),
1579-
WindowHandle::Webview(w) => w.inner_size(),
1599+
WindowHandle::Webview { inner, .. } => inner.inner_size(),
15801600
}
15811601
}
15821602
}
@@ -2235,7 +2255,7 @@ fn handle_user_message<T: UserEvent>(
22352255
match window_message {
22362256
#[cfg(desktop)]
22372257
WindowMessage::WithWebview(f) => {
2238-
if let WindowHandle::Webview(w) = window {
2258+
if let WindowHandle::Webview { inner: w, .. } = &window {
22392259
#[cfg(any(
22402260
target_os = "linux",
22412261
target_os = "dragonfly",
@@ -2276,19 +2296,19 @@ fn handle_user_message<T: UserEvent>(
22762296

22772297
#[cfg(any(debug_assertions, feature = "devtools"))]
22782298
WindowMessage::OpenDevTools => {
2279-
if let WindowHandle::Webview(w) = &window {
2299+
if let WindowHandle::Webview { inner: w, .. } = &window {
22802300
w.open_devtools();
22812301
}
22822302
}
22832303
#[cfg(any(debug_assertions, feature = "devtools"))]
22842304
WindowMessage::CloseDevTools => {
2285-
if let WindowHandle::Webview(w) = &window {
2305+
if let WindowHandle::Webview { inner: w, .. } = &window {
22862306
w.close_devtools();
22872307
}
22882308
}
22892309
#[cfg(any(debug_assertions, feature = "devtools"))]
22902310
WindowMessage::IsDevToolsOpen(tx) => {
2291-
if let WindowHandle::Webview(w) = &window {
2311+
if let WindowHandle::Webview { inner: w, .. } = &window {
22922312
tx.send(w.is_devtools_open()).unwrap();
22932313
} else {
22942314
tx.send(false).unwrap();
@@ -2427,7 +2447,7 @@ fn handle_user_message<T: UserEvent>(
24272447
}
24282448
Message::Webview(id, webview_message) => match webview_message {
24292449
WebviewMessage::EvaluateScript(script) => {
2430-
if let Some(WindowHandle::Webview(webview)) =
2450+
if let Some(WindowHandle::Webview { inner: webview, .. }) =
24312451
windows.borrow().get(&id).and_then(|w| w.inner.as_ref())
24322452
{
24332453
if let Err(e) = webview.evaluate_script(&script) {
@@ -2436,7 +2456,7 @@ fn handle_user_message<T: UserEvent>(
24362456
}
24372457
}
24382458
WebviewMessage::Print => {
2439-
if let Some(WindowHandle::Webview(webview)) =
2459+
if let Some(WindowHandle::Webview { inner: webview, .. }) =
24402460
windows.borrow().get(&id).and_then(|w| w.inner.as_ref())
24412461
{
24422462
let _ = webview.print();
@@ -2913,7 +2933,7 @@ fn to_wry_menu(
29132933
fn create_webview<T: UserEvent>(
29142934
window_id: WebviewId,
29152935
event_loop: &EventLoopWindowTarget<Message<T>>,
2916-
web_context: &WebContextStore,
2936+
web_context_store: &WebContextStore,
29172937
context: Context<T>,
29182938
pending: PendingWindow<T, Wry<T>>,
29192939
) -> Result<WindowWrapper> {
@@ -3002,19 +3022,18 @@ fn create_webview<T: UserEvent>(
30023022
webview_builder = webview_builder.with_initialization_script(&script);
30033023
}
30043024

3005-
let mut web_context = web_context.lock().expect("poisoned WebContext store");
3025+
let mut web_context = web_context_store.lock().expect("poisoned WebContext store");
30063026
let is_first_context = web_context.is_empty();
30073027
let automation_enabled = std::env::var("TAURI_AUTOMATION").as_deref() == Ok("true");
3008-
let entry = web_context.entry(
3009-
// force a unique WebContext when automation is false;
3028+
let web_context_key = // force a unique WebContext when automation is false;
30103029
// the context must be stored on the HashMap because it must outlive the WebView on macOS
30113030
if automation_enabled {
30123031
webview_attributes.data_directory.clone()
30133032
} else {
30143033
// random unique key
30153034
Some(Uuid::new_v4().as_hyphenated().to_string().into())
3016-
},
3017-
);
3035+
};
3036+
let entry = web_context.entry(web_context_key.clone());
30183037
let web_context = match entry {
30193038
Occupied(occupied) => occupied.into_mut(),
30203039
Vacant(vacant) => {
@@ -3077,7 +3096,15 @@ fn create_webview<T: UserEvent>(
30773096

30783097
Ok(WindowWrapper {
30793098
label,
3080-
inner: Some(WindowHandle::Webview(Arc::new(webview))),
3099+
inner: Some(WindowHandle::Webview {
3100+
inner: Arc::new(webview),
3101+
context_store: web_context_store.clone(),
3102+
context_key: if automation_enabled {
3103+
None
3104+
} else {
3105+
web_context_key
3106+
},
3107+
}),
30813108
menu_items,
30823109
window_event_listeners,
30833110
menu_event_listeners: Default::default(),

0 commit comments

Comments
 (0)