Skip to content
Closed
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
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,13 @@ pub struct WebViewAttributes<'a> {

/// Whether JavaScript should be disabled.
pub javascript_disabled: bool,

/// Set a handler closure to respond to web content process termination.
///
/// ## Platform-specific:
///
/// - **Linux / Windows / Android**: Unsupported.
pub on_web_content_process_terminate_handler: Option<Box<dyn Fn()>>,
}

impl Default for WebViewAttributes<'_> {
Expand Down Expand Up @@ -834,6 +841,7 @@ impl Default for WebViewAttributes<'_> {
}),
background_throttling: None,
javascript_disabled: false,
on_web_content_process_terminate_handler: None,
}
}
}
Expand Down Expand Up @@ -1405,6 +1413,15 @@ impl<'a> WebViewBuilder<'a> {
self
}

/// Set a handler to respond to web content process termination.
pub fn with_on_web_content_process_terminate_handler(
mut self,
handler: impl Fn() + 'static,
) -> Self {
self.attrs.on_web_content_process_terminate_handler = Some(Box::new(handler));
self
}

/// Consume the builder and create the [`WebView`] from a type that implements [`HasWindowHandle`].
///
/// # Platform-specific:
Expand Down
18 changes: 18 additions & 0 deletions src/wkwebview/class/wry_navigation_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::{
download::{navigation_download_action, navigation_download_response},
navigation::{
did_commit_navigation, did_finish_navigation, navigation_policy, navigation_policy_response,
web_content_process_did_terminate,
},
},
PageLoadEvent, WryWebView,
Expand All @@ -35,6 +36,7 @@ pub struct WryNavigationDelegateIvars {
pub navigation_policy_function: Box<dyn Fn(String) -> bool>,
pub download_delegate: Option<Retained<WryDownloadDelegate>>,
pub on_page_load_handler: Option<Box<dyn Fn(PageLoadEvent)>>,
pub on_web_content_process_terminate_handler: Option<Box<dyn Fn()>>,
}

define_class!(
Expand Down Expand Up @@ -96,6 +98,11 @@ define_class!(
) {
navigation_download_response(self, webview, response, download);
}

#[unsafe(method(webViewWebContentProcessDidTerminate:))]
fn web_content_process_did_terminate(&self, webview: &WKWebView) {
web_content_process_did_terminate(self, webview);
}
}
);

Expand All @@ -108,6 +115,7 @@ impl WryNavigationDelegate {
navigation_handler: Option<Box<dyn Fn(String) -> bool>>,
download_delegate: Option<Retained<WryDownloadDelegate>>,
on_page_load_handler: Option<Box<dyn Fn(PageLoadEvent, String)>>,
on_web_content_process_terminate_handler: Option<Box<dyn Fn()>>,
mtm: MainThreadMarker,
) -> Retained<Self> {
let navigation_policy_function = Box::new(move |url: String| -> bool {
Expand All @@ -125,6 +133,15 @@ impl WryNavigationDelegate {
None
};

let on_web_content_process_terminate_handler = if let Some(handler) = on_web_content_process_terminate_handler {
let custom_handler = Box::new(move || {
handler();
}) as Box<dyn Fn()>;
Some(custom_handler)
} else {
None
};

let delegate = mtm
.alloc::<WryNavigationDelegate>()
.set_ivars(WryNavigationDelegateIvars {
Expand All @@ -133,6 +150,7 @@ impl WryNavigationDelegate {
has_download_handler,
download_delegate,
on_page_load_handler,
on_web_content_process_terminate_handler,
});

unsafe { msg_send![super(delegate), init] }
Expand Down
1 change: 1 addition & 0 deletions src/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ impl InnerWebView {
attributes.navigation_handler,
download_delegate.clone(),
attributes.on_page_load_handler,
attributes.on_web_content_process_terminate_handler,
mtm,
);

Expand Down
9 changes: 9 additions & 0 deletions src/wkwebview/navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,12 @@ pub(crate) fn navigation_policy_response(
(*handler).call((WKNavigationResponsePolicy::Allow,));
}
}

pub(crate) fn web_content_process_did_terminate(
this: &WryNavigationDelegate,
_webview: &WKWebView,
) {
if let Some(on_web_content_process_terminate) = &this.ivars().on_web_content_process_terminate_handler {
on_web_content_process_terminate();
}
}