-
-
Notifications
You must be signed in to change notification settings - Fork 455
Expand file tree
/
Copy pathwry_navigation_delegate.rs
More file actions
186 lines (165 loc) · 6.09 KB
/
wry_navigation_delegate.rs
File metadata and controls
186 lines (165 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2020-2024 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use std::sync::{Arc, Mutex};
use objc2::{define_class, msg_send, rc::Retained, runtime::NSObject, MainThreadOnly};
use objc2_foundation::{MainThreadMarker, NSObjectProtocol};
#[cfg(target_os = "macos")]
use objc2_foundation::{
NSURLAuthenticationChallenge, NSURLCredential, NSURLSessionAuthChallengeDisposition,
};
use objc2_web_kit::{
WKDownload, WKNavigation, WKNavigationAction, WKNavigationActionPolicy, WKNavigationDelegate,
WKNavigationResponse, WKNavigationResponsePolicy,
};
#[cfg(target_os = "ios")]
use crate::wkwebview::ios::WKWebView::WKWebView;
#[cfg(target_os = "macos")]
use objc2_web_kit::WKWebView;
use crate::{
url_from_webview,
wkwebview::{
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,
};
use super::wry_download_delegate::WryDownloadDelegate;
pub struct WryNavigationDelegateIvars {
pub pending_scripts: Arc<Mutex<Option<Vec<String>>>>,
pub has_download_handler: bool,
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()>>,
pub client_certificate_p12: Option<Vec<u8>>,
pub client_certificate_password: Option<String>,
pub trusted_ca_certificate: Option<Vec<u8>>,
}
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[ivars = WryNavigationDelegateIvars]
pub struct WryNavigationDelegate;
unsafe impl NSObjectProtocol for WryNavigationDelegate {}
unsafe impl WKNavigationDelegate for WryNavigationDelegate {
#[unsafe(method(webView:decidePolicyForNavigationAction:decisionHandler:))]
fn navigation_policy(
&self,
webview: &WKWebView,
action: &WKNavigationAction,
handler: &block2::Block<dyn Fn(WKNavigationActionPolicy)>,
) {
navigation_policy(self, webview, action, handler);
}
#[unsafe(method(webView:decidePolicyForNavigationResponse:decisionHandler:))]
fn navigation_policy_response(
&self,
webview: &WKWebView,
response: &WKNavigationResponse,
handler: &block2::Block<dyn Fn(WKNavigationResponsePolicy)>,
) {
navigation_policy_response(self, webview, response, handler);
}
#[unsafe(method(webView:didFinishNavigation:))]
fn did_finish_navigation(&self, webview: &WKWebView, navigation: &WKNavigation) {
did_finish_navigation(self, webview, navigation);
}
#[unsafe(method(webView:didCommitNavigation:))]
fn did_commit_navigation(&self, webview: &WKWebView, navigation: &WKNavigation) {
did_commit_navigation(self, webview, navigation);
}
#[unsafe(method(webView:navigationAction:didBecomeDownload:))]
fn navigation_download_action(
&self,
webview: &WKWebView,
action: &WKNavigationAction,
download: &WKDownload,
) {
navigation_download_action(self, webview, action, download);
}
#[unsafe(method(webView:navigationResponse:didBecomeDownload:))]
fn navigation_download_response(
&self,
webview: &WKWebView,
response: &WKNavigationResponse,
download: &WKDownload,
) {
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);
}
#[cfg(target_os = "macos")]
#[unsafe(method(webView:didReceiveAuthenticationChallenge:completionHandler:))]
fn did_receive_authentication_challenge(
&self,
_webview: &WKWebView,
challenge: &NSURLAuthenticationChallenge,
handler: &block2::Block<
dyn Fn(NSURLSessionAuthChallengeDisposition, *mut NSURLCredential),
>,
) {
crate::wkwebview::navigation_auth::did_receive_authentication_challenge(
self, challenge, handler,
);
}
}
);
impl WryNavigationDelegate {
#[allow(clippy::too_many_arguments)]
pub fn new(
webview: Retained<WryWebView>,
pending_scripts: Arc<Mutex<Option<Vec<String>>>>,
has_download_handler: bool,
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()>>,
client_certificate_p12: Option<Vec<u8>>,
client_certificate_password: Option<String>,
trusted_ca_certificate: Option<Vec<u8>>,
mtm: MainThreadMarker,
) -> Retained<Self> {
let navigation_policy_function = Box::new(move |url: String| -> bool {
navigation_handler
.as_ref()
.map_or(true, |navigation_handler| (navigation_handler)(url))
});
let on_page_load_handler = if let Some(handler) = on_page_load_handler {
let custom_handler = Box::new(move |event| {
handler(event, url_from_webview(&webview).unwrap_or_default());
}) as Box<dyn Fn(PageLoadEvent)>;
Some(custom_handler)
} else {
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 {
pending_scripts,
navigation_policy_function,
has_download_handler,
download_delegate,
on_page_load_handler,
on_web_content_process_terminate_handler,
client_certificate_p12,
client_certificate_password,
trusted_ca_certificate,
});
unsafe { msg_send![super(delegate), init] }
}
}