Skip to content

Commit

Permalink
feat(tauri-runtime-wry): drop the WebContext on WebView drop (#5240)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Oct 19, 2022
1 parent 4137ab4 commit 9d8b377
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changes/drop-web-context.md
@@ -0,0 +1,5 @@
---
"tauri-runtime-wry": patch
---

Drop the WebContext when the WebView is dropped.
59 changes: 43 additions & 16 deletions core/tauri-runtime-wry/src/lib.rs
Expand Up @@ -1550,10 +1550,30 @@ impl<T: UserEvent> Dispatch<T> for WryDispatcher<T> {

#[derive(Clone)]
enum WindowHandle {
Webview(Arc<WebView>),
Webview {
inner: Arc<WebView>,
context_store: WebContextStore,
// the key of the WebContext if it's not shared
context_key: Option<PathBuf>,
},
Window(Arc<Window>),
}

impl Drop for WindowHandle {
fn drop(&mut self) {
if let Self::Webview {
inner,
context_store,
context_key,
} = self
{
if Arc::get_mut(inner).is_some() {
context_store.lock().unwrap().remove(context_key);
}
}
}
}

impl fmt::Debug for WindowHandle {
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
Ok(())
Expand All @@ -1566,7 +1586,7 @@ impl Deref for WindowHandle {
#[inline(always)]
fn deref(&self) -> &Window {
match self {
Self::Webview(w) => w.window(),
Self::Webview { inner, .. } => inner.window(),
Self::Window(w) => w,
}
}
Expand All @@ -1576,7 +1596,7 @@ impl WindowHandle {
fn inner_size(&self) -> WryPhysicalSize<u32> {
match self {
WindowHandle::Window(w) => w.inner_size(),
WindowHandle::Webview(w) => w.inner_size(),
WindowHandle::Webview { inner, .. } => inner.inner_size(),
}
}
}
Expand Down Expand Up @@ -2235,7 +2255,7 @@ fn handle_user_message<T: UserEvent>(
match window_message {
#[cfg(desktop)]
WindowMessage::WithWebview(f) => {
if let WindowHandle::Webview(w) = window {
if let WindowHandle::Webview { inner: w, .. } = &window {
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
Expand Down Expand Up @@ -2276,19 +2296,19 @@ fn handle_user_message<T: UserEvent>(

#[cfg(any(debug_assertions, feature = "devtools"))]
WindowMessage::OpenDevTools => {
if let WindowHandle::Webview(w) = &window {
if let WindowHandle::Webview { inner: w, .. } = &window {
w.open_devtools();
}
}
#[cfg(any(debug_assertions, feature = "devtools"))]
WindowMessage::CloseDevTools => {
if let WindowHandle::Webview(w) = &window {
if let WindowHandle::Webview { inner: w, .. } = &window {
w.close_devtools();
}
}
#[cfg(any(debug_assertions, feature = "devtools"))]
WindowMessage::IsDevToolsOpen(tx) => {
if let WindowHandle::Webview(w) = &window {
if let WindowHandle::Webview { inner: w, .. } = &window {
tx.send(w.is_devtools_open()).unwrap();
} else {
tx.send(false).unwrap();
Expand Down Expand Up @@ -2427,7 +2447,7 @@ fn handle_user_message<T: UserEvent>(
}
Message::Webview(id, webview_message) => match webview_message {
WebviewMessage::EvaluateScript(script) => {
if let Some(WindowHandle::Webview(webview)) =
if let Some(WindowHandle::Webview { inner: webview, .. }) =
windows.borrow().get(&id).and_then(|w| w.inner.as_ref())
{
if let Err(e) = webview.evaluate_script(&script) {
Expand All @@ -2436,7 +2456,7 @@ fn handle_user_message<T: UserEvent>(
}
}
WebviewMessage::Print => {
if let Some(WindowHandle::Webview(webview)) =
if let Some(WindowHandle::Webview { inner: webview, .. }) =
windows.borrow().get(&id).and_then(|w| w.inner.as_ref())
{
let _ = webview.print();
Expand Down Expand Up @@ -2913,7 +2933,7 @@ fn to_wry_menu(
fn create_webview<T: UserEvent>(
window_id: WebviewId,
event_loop: &EventLoopWindowTarget<Message<T>>,
web_context: &WebContextStore,
web_context_store: &WebContextStore,
context: Context<T>,
pending: PendingWindow<T, Wry<T>>,
) -> Result<WindowWrapper> {
Expand Down Expand Up @@ -3002,19 +3022,18 @@ fn create_webview<T: UserEvent>(
webview_builder = webview_builder.with_initialization_script(&script);
}

let mut web_context = web_context.lock().expect("poisoned WebContext store");
let mut web_context = web_context_store.lock().expect("poisoned WebContext store");
let is_first_context = web_context.is_empty();
let automation_enabled = std::env::var("TAURI_AUTOMATION").as_deref() == Ok("true");
let entry = web_context.entry(
// force a unique WebContext when automation is false;
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 {
// random unique key
Some(Uuid::new_v4().as_hyphenated().to_string().into())
},
);
};
let entry = web_context.entry(web_context_key.clone());
let web_context = match entry {
Occupied(occupied) => occupied.into_mut(),
Vacant(vacant) => {
Expand Down Expand Up @@ -3077,7 +3096,15 @@ fn create_webview<T: UserEvent>(

Ok(WindowWrapper {
label,
inner: Some(WindowHandle::Webview(Arc::new(webview))),
inner: Some(WindowHandle::Webview {
inner: Arc::new(webview),
context_store: web_context_store.clone(),
context_key: if automation_enabled {
None
} else {
web_context_key
},
}),
menu_items,
window_event_listeners,
menu_event_listeners: Default::default(),
Expand Down

0 comments on commit 9d8b377

Please sign in to comment.