Skip to content

Commit 84b0ec2

Browse files
JeffTsangpewsheen
andauthored
feat(ios/macos): Add handler for web content process termination (#1624)
* Add handler for web content process termination * Move handler to platform specific attributes * chore: code format --------- Co-authored-by: Jason Tsai <[email protected]>
1 parent 9604b0e commit 84b0ec2

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wry": minor
3+
---
4+
5+
Add handler for web content process termination.

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,7 @@ pub(crate) struct PlatformSpecificWebViewAttributes {
14621462
data_store_identifier: Option<[u8; 16]>,
14631463
traffic_light_inset: Option<dpi::Position>,
14641464
allow_link_preview: bool,
1465+
on_web_content_process_terminate_handler: Option<Box<dyn Fn()>>,
14651466
#[cfg(target_os = "ios")]
14661467
input_accessory_view_builder: Option<Box<InputAccessoryViewBuilder>>,
14671468
#[cfg(target_os = "ios")]
@@ -1478,6 +1479,7 @@ impl Default for PlatformSpecificWebViewAttributes {
14781479
traffic_light_inset: None,
14791480
// platform default for this is true
14801481
allow_link_preview: true,
1482+
on_web_content_process_terminate_handler: None,
14811483
#[cfg(target_os = "ios")]
14821484
input_accessory_view_builder: None,
14831485
#[cfg(target_os = "ios")]
@@ -1509,6 +1511,8 @@ pub trait WebViewBuilderExtDarwin {
15091511
///
15101512
/// See https://developer.apple.com/documentation/webkit/wkwebview/allowslinkpreview
15111513
fn with_allow_link_preview(self, allow_link_preview: bool) -> Self;
1514+
/// Set a handler closure to respond to web content process termination. Available on macOS and iOS only.
1515+
fn with_on_web_content_process_terminate_handler(self, handler: impl Fn() + 'static) -> Self;
15121516
}
15131517

15141518
#[cfg(any(target_os = "macos", target_os = "ios"))]
@@ -1527,6 +1531,13 @@ impl WebViewBuilderExtDarwin for WebViewBuilder<'_> {
15271531
self.platform_specific.allow_link_preview = allow_link_preview;
15281532
self
15291533
}
1534+
1535+
fn with_on_web_content_process_terminate_handler(mut self, handler: impl Fn() + 'static) -> Self {
1536+
self
1537+
.platform_specific
1538+
.on_web_content_process_terminate_handler = Some(Box::new(handler));
1539+
self
1540+
}
15301541
}
15311542

15321543
#[cfg(target_os = "macos")]

src/wkwebview/class/wry_navigation_delegate.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::{
2222
download::{navigation_download_action, navigation_download_response},
2323
navigation::{
2424
did_commit_navigation, did_finish_navigation, navigation_policy, navigation_policy_response,
25+
web_content_process_did_terminate,
2526
},
2627
},
2728
PageLoadEvent, WryWebView,
@@ -35,6 +36,7 @@ pub struct WryNavigationDelegateIvars {
3536
pub navigation_policy_function: Box<dyn Fn(String) -> bool>,
3637
pub download_delegate: Option<Retained<WryDownloadDelegate>>,
3738
pub on_page_load_handler: Option<Box<dyn Fn(PageLoadEvent)>>,
39+
pub on_web_content_process_terminate_handler: Option<Box<dyn Fn()>>,
3840
}
3941

4042
define_class!(
@@ -96,6 +98,11 @@ define_class!(
9698
) {
9799
navigation_download_response(self, webview, response, download);
98100
}
101+
102+
#[unsafe(method(webViewWebContentProcessDidTerminate:))]
103+
fn web_content_process_did_terminate(&self, webview: &WKWebView) {
104+
web_content_process_did_terminate(self, webview);
105+
}
99106
}
100107
);
101108

@@ -108,6 +115,7 @@ impl WryNavigationDelegate {
108115
navigation_handler: Option<Box<dyn Fn(String) -> bool>>,
109116
download_delegate: Option<Retained<WryDownloadDelegate>>,
110117
on_page_load_handler: Option<Box<dyn Fn(PageLoadEvent, String)>>,
118+
on_web_content_process_terminate_handler: Option<Box<dyn Fn()>>,
111119
mtm: MainThreadMarker,
112120
) -> Retained<Self> {
113121
let navigation_policy_function = Box::new(move |url: String| -> bool {
@@ -125,6 +133,16 @@ impl WryNavigationDelegate {
125133
None
126134
};
127135

136+
let on_web_content_process_terminate_handler =
137+
if let Some(handler) = on_web_content_process_terminate_handler {
138+
let custom_handler = Box::new(move || {
139+
handler();
140+
}) as Box<dyn Fn()>;
141+
Some(custom_handler)
142+
} else {
143+
None
144+
};
145+
128146
let delegate = mtm
129147
.alloc::<WryNavigationDelegate>()
130148
.set_ivars(WryNavigationDelegateIvars {
@@ -133,6 +151,7 @@ impl WryNavigationDelegate {
133151
has_download_handler,
134152
download_delegate,
135153
on_page_load_handler,
154+
on_web_content_process_terminate_handler,
136155
});
137156

138157
unsafe { msg_send![super(delegate), init] }

src/wkwebview/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ impl InnerWebView {
542542
attributes.navigation_handler,
543543
download_delegate.clone(),
544544
attributes.on_page_load_handler,
545+
pl_attrs.on_web_content_process_terminate_handler,
545546
mtm,
546547
);
547548

src/wkwebview/navigation.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,14 @@ pub(crate) fn navigation_policy_response(
102102
(*handler).call((WKNavigationResponsePolicy::Allow,));
103103
}
104104
}
105+
106+
pub(crate) fn web_content_process_did_terminate(
107+
this: &WryNavigationDelegate,
108+
_webview: &WKWebView,
109+
) {
110+
if let Some(on_web_content_process_terminate) =
111+
&this.ivars().on_web_content_process_terminate_handler
112+
{
113+
on_web_content_process_terminate();
114+
}
115+
}

0 commit comments

Comments
 (0)