Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changes/web-content-process-termination.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": minor
---

Add handler for web content process termination.
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1462,6 +1462,7 @@ pub(crate) struct PlatformSpecificWebViewAttributes {
data_store_identifier: Option<[u8; 16]>,
traffic_light_inset: Option<dpi::Position>,
allow_link_preview: bool,
on_web_content_process_terminate_handler: Option<Box<dyn Fn()>>,
#[cfg(target_os = "ios")]
input_accessory_view_builder: Option<Box<InputAccessoryViewBuilder>>,
#[cfg(target_os = "ios")]
Expand All @@ -1478,6 +1479,7 @@ impl Default for PlatformSpecificWebViewAttributes {
traffic_light_inset: None,
// platform default for this is true
allow_link_preview: true,
on_web_content_process_terminate_handler: None,
#[cfg(target_os = "ios")]
input_accessory_view_builder: None,
#[cfg(target_os = "ios")]
Expand Down Expand Up @@ -1509,6 +1511,8 @@ pub trait WebViewBuilderExtDarwin {
///
/// See https://developer.apple.com/documentation/webkit/wkwebview/allowslinkpreview
fn with_allow_link_preview(self, allow_link_preview: bool) -> Self;
/// Set a handler closure to respond to web content process termination. Available on macOS and iOS only.
fn with_on_web_content_process_terminate_handler(self, handler: impl Fn() + 'static) -> Self;
}

#[cfg(any(target_os = "macos", target_os = "ios"))]
Expand All @@ -1527,6 +1531,13 @@ impl WebViewBuilderExtDarwin for WebViewBuilder<'_> {
self.platform_specific.allow_link_preview = allow_link_preview;
self
}

fn with_on_web_content_process_terminate_handler(mut self, handler: impl Fn() + 'static) -> Self {
self
.platform_specific
.on_web_content_process_terminate_handler = Some(Box::new(handler));
self
}
}

#[cfg(target_os = "macos")]
Expand Down
19 changes: 19 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,16 @@ 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 +151,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,
pl_attrs.on_web_content_process_terminate_handler,
mtm,
);

Expand Down
11 changes: 11 additions & 0 deletions src/wkwebview/navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,14 @@ 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();
}
}
Loading