Skip to content
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/web-content-process-termination.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"tauri": minor:feat
"tauri-runtime": minor:feat
"tauri-runtime-wry": minor:feat
---

Add handler for web content process termination on macOS and iOS.
7 changes: 7 additions & 0 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4891,6 +4891,13 @@ You may have it installed on another user account, but it is not available for t

webview_builder =
webview_builder.with_allow_link_preview(webview_attributes.allow_link_preview);

if let Some(on_web_content_process_terminate_handler) =
pending.on_web_content_process_terminate_handler
{
webview_builder = webview_builder
.with_on_web_content_process_terminate_handler(on_web_content_process_terminate_handler);
}
}

#[cfg(target_os = "ios")]
Expand Down
8 changes: 8 additions & 0 deletions crates/tauri-runtime/src/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type DocumentTitleChangedHandler = dyn Fn(String) + Send + 'static;

type DownloadHandler = dyn Fn(DownloadEvent) -> bool + Send + Sync;

#[cfg(any(target_os = "macos", target_os = "ios"))]
type OnWebContentProcessTerminateHandler = dyn Fn() + Send;

#[cfg(target_os = "ios")]
type InputAccessoryViewBuilderFn = dyn Fn(&objc2_ui_kit::UIView) -> Option<objc2::rc::Retained<objc2_ui_kit::UIView>>
+ Send
Expand Down Expand Up @@ -224,6 +227,9 @@ pub struct PendingWebview<T: UserEvent, R: Runtime<T>> {
pub on_page_load_handler: Option<Box<OnPageLoadHandler>>,

pub download_handler: Option<Arc<DownloadHandler>>,

#[cfg(any(target_os = "macos", target_os = "ios"))]
pub on_web_content_process_terminate_handler: Option<Box<OnWebContentProcessTerminateHandler>>,
}

impl<T: UserEvent, R: Runtime<T>> PendingWebview<T, R> {
Expand All @@ -250,6 +256,8 @@ impl<T: UserEvent, R: Runtime<T>> PendingWebview<T, R> {
web_resource_request_handler: None,
on_page_load_handler: None,
download_handler: None,
#[cfg(any(target_os = "macos", target_os = "ios"))]
on_web_content_process_terminate_handler: None,
})
}
}
Expand Down
40 changes: 40 additions & 0 deletions crates/tauri/src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub(crate) type UriSchemeProtocolHandler =
pub(crate) type OnPageLoad<R> = dyn Fn(Webview<R>, PageLoadPayload<'_>) + Send + Sync + 'static;
pub(crate) type OnDocumentTitleChanged<R> = dyn Fn(Webview<R>, String) + Send + 'static;
pub(crate) type DownloadHandler<R> = dyn Fn(Webview<R>, DownloadEvent<'_>) -> bool + Send + Sync;
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub(crate) type OnWebContentProcessTerminateHandler<R> = dyn Fn(Webview<R>) + Send;

#[derive(Clone, Serialize)]
pub(crate) struct CreatedEvent {
Expand Down Expand Up @@ -278,6 +280,9 @@ unstable_struct!(
pub(crate) on_page_load_handler: Option<Box<OnPageLoad<R>>>,
pub(crate) document_title_changed_handler: Option<Box<OnDocumentTitleChanged<R>>>,
pub(crate) download_handler: Option<Arc<DownloadHandler<R>>>,
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub(crate) on_web_content_process_terminate_handler:
Option<Box<OnWebContentProcessTerminateHandler<R>>>,
}
);

Expand Down Expand Up @@ -356,6 +361,8 @@ async fn create_window(app: tauri::AppHandle) {
on_page_load_handler: None,
document_title_changed_handler: None,
download_handler: None,
#[cfg(any(target_os = "macos", target_os = "ios"))]
on_web_content_process_terminate_handler: None,
}
}

Expand Down Expand Up @@ -435,6 +442,8 @@ async fn create_window(app: tauri::AppHandle) {
on_page_load_handler: None,
document_title_changed_handler: None,
download_handler: None,
#[cfg(any(target_os = "macos", target_os = "ios"))]
on_web_content_process_terminate_handler: None,
}
}

Expand Down Expand Up @@ -697,6 +706,22 @@ tauri::Builder::default()
self
}

/// Defines a closure to be executed when the web content process terminates.
///
/// ## Platform-specific
///
/// - **Linux / Windows / Android:** Unsupported.
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub fn on_web_content_process_terminate<F: Fn(Webview<R>) + Send + 'static>(
mut self,
f: F,
) -> Self {
self
.on_web_content_process_terminate_handler
.replace(Box::new(f));
self
}

pub(crate) fn into_pending_webview<M: Manager<R>>(
mut self,
manager: &M,
Expand Down Expand Up @@ -776,6 +801,21 @@ tauri::Builder::default()
}
}));

#[cfg(any(target_os = "macos", target_os = "ios"))]
if let Some(on_web_content_process_terminate_handler) =
self.on_web_content_process_terminate_handler.take()
{
let label = pending.label.clone();
let manager = manager.manager_owned();
pending
.on_web_content_process_terminate_handler
.replace(Box::new(move || {
if let Some(w) = manager.get_webview(&label) {
on_web_content_process_terminate_handler(w);
}
}));
}

manager
.manager()
.webview
Expand Down
14 changes: 14 additions & 0 deletions crates/tauri/src/webview/webview_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,20 @@ tauri::Builder::default()
self
}

/// Defines a closure to be executed when the web content process terminates.
///
/// ## Platform-specific
///
/// - **Linux / Windows / Android:** Unsupported.
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub fn on_web_content_process_terminate<F: Fn(Webview<R>) + Send + 'static>(
mut self,
f: F,
) -> Self {
self.webview_builder = self.webview_builder.on_web_content_process_terminate(f);
self
}

/// Creates a new window.
pub fn build(self) -> crate::Result<WebviewWindow<R>> {
let (window, webview) = self.window_builder.with_webview(self.webview_builder)?;
Expand Down
Loading