Skip to content

Commit a49fc99

Browse files
authored
fix(core): store the hosting Window directly on Webview and WebviewWindow (#11161)
closes #11159
1 parent 1d8b67b commit a49fc99

File tree

6 files changed

+141
-115
lines changed

6 files changed

+141
-115
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": "patch:bug"
3+
---
4+
5+
Fix internal crash when trying to close the same window multiple times.

crates/tauri/src/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ impl<R: Runtime> Clone for AppHandle<R> {
364364
impl<'de, R: Runtime> CommandArg<'de, R> for AppHandle<R> {
365365
/// Grabs the [`Window`] from the [`CommandItem`] and returns the associated [`AppHandle`]. This will never fail.
366366
fn from_command(command: CommandItem<'de, R>) -> std::result::Result<Self, InvokeError> {
367-
Ok(command.message.webview().window().app_handle)
367+
Ok(command.message.webview().app_handle)
368368
}
369369
}
370370

crates/tauri/src/lib.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,12 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
584584
/// Fetch a single webview window from the manager.
585585
fn get_webview_window(&self, label: &str) -> Option<WebviewWindow<R>> {
586586
self.manager().get_webview(label).and_then(|webview| {
587-
if webview.window().is_webview_window() {
588-
Some(WebviewWindow { webview })
587+
let window = webview.window();
588+
if window.is_webview_window() {
589+
Some(WebviewWindow {
590+
window: window.clone(),
591+
webview,
592+
})
589593
} else {
590594
None
591595
}
@@ -599,8 +603,15 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
599603
.webviews()
600604
.into_iter()
601605
.filter_map(|(label, webview)| {
602-
if webview.window().is_webview_window() {
603-
Some((label, WebviewWindow { webview }))
606+
let window = webview.window();
607+
if window.is_webview_window() {
608+
Some((
609+
label,
610+
WebviewWindow {
611+
window: window.clone(),
612+
webview,
613+
},
614+
))
604615
} else {
605616
None
606617
}

crates/tauri/src/webview/mod.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -792,19 +792,19 @@ fn main() {
792792
/// Webview.
793793
#[default_runtime(crate::Wry, wry)]
794794
pub struct Webview<R: Runtime> {
795-
window_label: Arc<Mutex<String>>,
795+
pub(crate) window: Arc<Mutex<Window<R>>>,
796+
/// The webview created by the runtime.
797+
pub(crate) webview: DetachedWebview<EventLoopMessage, R>,
796798
/// The manager to associate this webview with.
797799
pub(crate) manager: Arc<AppManager<R>>,
798800
pub(crate) app_handle: AppHandle<R>,
799-
/// The webview created by the runtime.
800-
pub(crate) webview: DetachedWebview<EventLoopMessage, R>,
801801
pub(crate) resources_table: Arc<Mutex<ResourceTable>>,
802802
}
803803

804804
impl<R: Runtime> std::fmt::Debug for Webview<R> {
805805
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
806806
f.debug_struct("Window")
807-
.field("window_label", &self.window_label)
807+
.field("window", &self.window.lock().unwrap())
808808
.field("webview", &self.webview)
809809
.finish()
810810
}
@@ -813,10 +813,10 @@ impl<R: Runtime> std::fmt::Debug for Webview<R> {
813813
impl<R: Runtime> Clone for Webview<R> {
814814
fn clone(&self) -> Self {
815815
Self {
816-
window_label: self.window_label.clone(),
816+
window: self.window.clone(),
817+
webview: self.webview.clone(),
817818
manager: self.manager.clone(),
818819
app_handle: self.app_handle.clone(),
819-
webview: self.webview.clone(),
820820
resources_table: self.resources_table.clone(),
821821
}
822822
}
@@ -842,9 +842,9 @@ impl<R: Runtime> Webview<R> {
842842
/// Create a new webview that is attached to the window.
843843
pub(crate) fn new(window: Window<R>, webview: DetachedWebview<EventLoopMessage, R>) -> Self {
844844
Self {
845-
window_label: Arc::new(Mutex::new(window.label().into())),
846845
manager: window.manager.clone(),
847846
app_handle: window.app_handle.clone(),
847+
window: Arc::new(Mutex::new(window)),
848848
webview,
849849
resources_table: Default::default(),
850850
}
@@ -957,14 +957,13 @@ impl<R: Runtime> Webview<R> {
957957
pub fn reparent(&self, window: &Window<R>) -> crate::Result<()> {
958958
#[cfg(not(feature = "unstable"))]
959959
{
960-
let current_window = self.window();
961-
if current_window.is_webview_window() || window.is_webview_window() {
960+
if self.window_ref().is_webview_window() || window.is_webview_window() {
962961
return Err(crate::Error::CannotReparentWebviewWindow);
963962
}
964963
}
965964

965+
*self.window.lock().unwrap() = window.clone();
966966
self.webview.dispatcher.reparent(window.window.id)?;
967-
*self.window_label.lock().unwrap() = window.label().to_string();
968967
Ok(())
969968
}
970969

@@ -1000,14 +999,16 @@ impl<R: Runtime> Webview<R> {
1000999
impl<R: Runtime> Webview<R> {
10011000
/// The window that is hosting this webview.
10021001
pub fn window(&self) -> Window<R> {
1003-
self
1004-
.manager
1005-
.get_window(&self.window_label.lock().unwrap())
1006-
.expect("could not locate webview parent window")
1002+
self.window.lock().unwrap().clone()
1003+
}
1004+
1005+
/// A reference to the window that is hosting this webview.
1006+
pub fn window_ref(&self) -> MutexGuard<'_, Window<R>> {
1007+
self.window.lock().unwrap()
10071008
}
10081009

10091010
pub(crate) fn window_label(&self) -> String {
1010-
self.window_label.lock().unwrap().clone()
1011+
self.window_ref().label().to_string()
10111012
}
10121013

10131014
/// Executes a closure, providing it with the webview handle that is specific to the current platform.
@@ -1193,7 +1194,7 @@ fn main() {
11931194
let runtime_authority = manager.runtime_authority.lock().unwrap();
11941195
let acl = runtime_authority.resolve_access(
11951196
&request.cmd,
1196-
message.webview.window().label(),
1197+
message.webview.window_ref().label(),
11971198
message.webview.label(),
11981199
&acl_origin,
11991200
);

crates/tauri/src/webview/plugin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ mod desktop_commands {
5757
.webviews()
5858
.values()
5959
.map(|webview| WebviewRef {
60-
window_label: webview.window().label().into(),
60+
window_label: webview.window_ref().label().into(),
6161
label: webview.label().into(),
6262
})
6363
.collect()

0 commit comments

Comments
 (0)