-
-
Notifications
You must be signed in to change notification settings - Fork 240
/
mod.rs
344 lines (307 loc) · 9.92 KB
/
mod.rs
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Copyright 2020-2022 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use super::{WebContext, WebViewAttributes, RGBA};
use crate::{application::window::Window, Result};
use crossbeam_channel::*;
use html5ever::{interface::QualName, namespace_url, ns, tendril::TendrilSink, LocalName};
use http::{
header::{HeaderValue, CONTENT_SECURITY_POLICY, CONTENT_TYPE},
Request, Response,
};
use kuchiki::NodeRef;
use once_cell::sync::OnceCell;
use sha2::{Digest, Sha256};
use std::{borrow::Cow, rc::Rc};
use tao::platform::android::ndk_glue::{
jni::{
errors::Error as JniError,
objects::{GlobalRef, JClass, JObject},
JNIEnv,
},
ndk::looper::{FdEvent, ForeignLooper},
PACKAGE,
};
use url::Url;
pub(crate) mod binding;
mod main_pipe;
use main_pipe::{CreateWebViewAttributes, MainPipe, WebViewMessage, MAIN_PIPE};
#[macro_export]
macro_rules! android_binding {
($domain:ident, $package:ident, $main: ident) => {
android_binding!($domain, $package, $main, ::wry)
};
($domain:ident, $package:ident, $main: ident, $wry: path) => {
use $wry::{
application::{
android_binding as tao_android_binding, android_fn, generate_package_name,
platform::android::ndk_glue::*,
},
webview::prelude::*,
};
tao_android_binding!($domain, $package, setup, $main);
android_fn!(
$domain,
$package,
RustWebViewClient,
handleRequest,
[JObject],
jobject
);
android_fn!($domain, $package, Ipc, ipc, [JString]);
android_fn!(
$domain,
$package,
RustWebChromeClient,
handleReceivedTitle,
[JObject, JString],
);
};
}
pub static IPC: OnceCell<UnsafeIpc> = OnceCell::new();
pub static REQUEST_HANDLER: OnceCell<UnsafeRequestHandler> = OnceCell::new();
pub static TITLE_CHANGE_HANDLER: OnceCell<UnsafeTitleHandler> = OnceCell::new();
pub struct UnsafeIpc(Box<dyn Fn(&Window, String)>, Rc<Window>);
impl UnsafeIpc {
pub fn new(f: Box<dyn Fn(&Window, String)>, w: Rc<Window>) -> Self {
Self(f, w)
}
}
unsafe impl Send for UnsafeIpc {}
unsafe impl Sync for UnsafeIpc {}
pub struct UnsafeRequestHandler(
Box<dyn Fn(Request<Vec<u8>>) -> Option<Response<Cow<'static, [u8]>>>>,
);
impl UnsafeRequestHandler {
pub fn new(f: Box<dyn Fn(Request<Vec<u8>>) -> Option<Response<Cow<'static, [u8]>>>>) -> Self {
Self(f)
}
}
unsafe impl Send for UnsafeRequestHandler {}
unsafe impl Sync for UnsafeRequestHandler {}
pub struct UnsafeTitleHandler(Box<dyn Fn(&Window, String)>, Rc<Window>);
impl UnsafeTitleHandler {
pub fn new(f: Box<dyn Fn(&Window, String)>, w: Rc<Window>) -> Self {
Self(f, w)
}
}
unsafe impl Send for UnsafeTitleHandler {}
unsafe impl Sync for UnsafeTitleHandler {}
pub unsafe fn setup(env: JNIEnv, looper: &ForeignLooper, activity: GlobalRef) {
// we must create the WebChromeClient here because it calls `registerForActivityResult`,
// which gives an `LifecycleOwners must call register before they are STARTED.` error when called outside the onCreate hook
let rust_webchrome_client_class = find_my_class(
env,
activity.as_obj(),
format!("{}/RustWebChromeClient", PACKAGE.get().unwrap()),
)
.unwrap();
let webchrome_client = env
.new_object(
rust_webchrome_client_class,
"(Landroidx/appcompat/app/AppCompatActivity;)V",
&[activity.as_obj().into()],
)
.unwrap();
let mut main_pipe = MainPipe {
env,
activity,
webview: None,
webchrome_client: env.new_global_ref(webchrome_client).unwrap(),
};
looper
.add_fd_with_callback(MAIN_PIPE[0], FdEvent::INPUT, move |_| {
let size = std::mem::size_of::<bool>();
let mut wake = false;
if libc::read(MAIN_PIPE[0], &mut wake as *mut _ as *mut _, size) == size as libc::ssize_t {
match main_pipe.recv() {
Ok(_) => true,
Err(_) => false,
}
} else {
false
}
})
.unwrap();
}
pub(crate) struct InnerWebView {
#[allow(unused)]
pub window: Rc<Window>,
}
impl InnerWebView {
pub fn new(
window: Rc<Window>,
attributes: WebViewAttributes,
_pl_attrs: super::PlatformSpecificWebViewAttributes,
_web_context: Option<&mut WebContext>,
) -> Result<Self> {
let WebViewAttributes {
url,
initialization_scripts,
ipc_handler,
devtools,
custom_protocols,
background_color,
transparent,
..
} = attributes;
if let Some(u) = url {
let mut url_string = String::from(u.as_str());
let name = u.scheme();
let is_custom_protocol = custom_protocols.iter().any(|(n, _)| n == name);
if is_custom_protocol {
url_string = u
.as_str()
.replace(&format!("{}://", name), &format!("https://{}.", name))
}
MainPipe::send(WebViewMessage::CreateWebView(CreateWebViewAttributes {
url: url_string,
devtools,
background_color,
transparent,
}));
}
REQUEST_HANDLER.get_or_init(move || {
UnsafeRequestHandler::new(Box::new(move |mut request| {
if let Some(custom_protocol) = custom_protocols.iter().find(|(name, _)| {
request
.uri()
.to_string()
.starts_with(&format!("https://{}.", name))
}) {
*request.uri_mut() = request
.uri()
.to_string()
.replace(
&format!("https://{}.", custom_protocol.0),
&format!("{}://", custom_protocol.0),
)
.parse()
.unwrap();
if let Ok(mut response) = (custom_protocol.1)(&request) {
if response.headers().get(CONTENT_TYPE) == Some(&HeaderValue::from_static("text/html"))
{
if !initialization_scripts.is_empty() {
let mut document =
kuchiki::parse_html().one(String::from_utf8_lossy(response.body()).into_owned());
let csp = response.headers_mut().get_mut(CONTENT_SECURITY_POLICY);
let mut hashes = Vec::new();
with_html_head(&mut document, |head| {
// iterate in reverse order since we are prepending each script to the head tag
for script in initialization_scripts.iter().rev() {
let script_el =
NodeRef::new_element(QualName::new(None, ns!(html), "script".into()), None);
script_el.append(NodeRef::new_text(script));
head.prepend(script_el);
if csp.is_some() {
hashes.push(hash_script(script));
}
}
});
if let Some(csp) = csp {
let csp_string = csp.to_str().unwrap().to_string();
let csp_string = if csp_string.contains("script-src") {
csp_string.replace("script-src", &format!("script-src {}", hashes.join(" ")))
} else {
format!("{} script-src {}", csp_string, hashes.join(" "))
};
*csp = HeaderValue::from_str(&csp_string).unwrap();
}
*response.body_mut() = document.to_string().into_bytes().into();
}
}
return Some(response);
}
}
None
}))
});
let w = window.clone();
if let Some(i) = ipc_handler {
IPC.get_or_init(move || UnsafeIpc::new(Box::new(i), w));
}
let w = window.clone();
if let Some(i) = attributes.document_title_changed_handler {
TITLE_CHANGE_HANDLER.get_or_init(move || UnsafeTitleHandler::new(i, w));
}
Ok(Self { window })
}
pub fn print(&self) {}
pub fn url(&self) -> Url {
let (tx, rx) = bounded(1);
MainPipe::send(WebViewMessage::GetUrl(tx));
let uri = rx.recv().unwrap();
Url::parse(uri.as_str()).unwrap()
}
pub fn eval(&self, js: &str) -> Result<()> {
MainPipe::send(WebViewMessage::Eval(js.into()));
Ok(())
}
#[cfg(any(debug_assertions, feature = "devtools"))]
pub fn open_devtools(&self) {}
#[cfg(any(debug_assertions, feature = "devtools"))]
pub fn close_devtools(&self) {}
#[cfg(any(debug_assertions, feature = "devtools"))]
pub fn is_devtools_open(&self) -> bool {
false
}
pub fn zoom(&self, _scale_factor: f64) {}
pub fn set_background_color(&self, background_color: RGBA) -> Result<()> {
MainPipe::send(WebViewMessage::SetBackgroundColor(background_color));
Ok(())
}
pub fn load_url(&self, url: &str) {
MainPipe::send(WebViewMessage::LoadUrl(url.to_string()));
}
}
#[derive(Clone, Copy)]
pub struct JniHandle;
impl JniHandle {
/// Execute jni code on the thread of the webview.
/// Provided function will be provided with the jni evironment, Android activity and WebView
pub fn exec<F>(&self, func: F)
where
F: FnOnce(JNIEnv, JObject, JObject) + Send + 'static,
{
MainPipe::send(WebViewMessage::Jni(Box::new(func)));
}
}
pub fn platform_webview_version() -> Result<String> {
let (tx, rx) = bounded(1);
MainPipe::send(WebViewMessage::GetWebViewVersion(tx));
rx.recv().unwrap()
}
fn with_html_head<F: FnOnce(&NodeRef)>(document: &mut NodeRef, f: F) {
if let Ok(ref node) = document.select_first("head") {
f(node.as_node())
} else {
let node = NodeRef::new_element(
QualName::new(None, ns!(html), LocalName::from("head")),
None,
);
f(&node);
document.prepend(node)
}
}
fn hash_script(script: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(script);
let hash = hasher.finalize();
format!("'sha256-{}'", base64::encode(hash))
}
fn find_my_class<'a>(
env: JNIEnv<'a>,
activity: JObject<'a>,
name: String,
) -> std::result::Result<JClass<'a>, JniError> {
let class_name = env.new_string(name.replace('/', "."))?;
let my_class = env
.call_method(
activity,
"getAppClass",
"(Ljava/lang/String;)Ljava/lang/Class;",
&[class_name.into()],
)?
.l()?;
Ok(my_class.into())
}