-
-
Notifications
You must be signed in to change notification settings - Fork 243
/
main_pipe.rs
223 lines (206 loc) · 6.5 KB
/
main_pipe.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
// Copyright 2020-2022 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use crate::{webview::RGBA, Error};
use crossbeam_channel::*;
use once_cell::sync::Lazy;
use std::os::unix::prelude::*;
use tao::platform::android::ndk_glue::{
jni::{
errors::Error as JniError,
objects::{GlobalRef, JObject},
JNIEnv,
},
PACKAGE,
};
use super::find_my_class;
static CHANNEL: Lazy<(Sender<WebViewMessage>, Receiver<WebViewMessage>)> = Lazy::new(|| bounded(8));
pub static MAIN_PIPE: Lazy<[RawFd; 2]> = Lazy::new(|| {
let mut pipe: [RawFd; 2] = Default::default();
unsafe { libc::pipe(pipe.as_mut_ptr()) };
pipe
});
pub struct MainPipe<'a> {
pub env: JNIEnv<'a>,
pub activity: GlobalRef,
pub webview: Option<GlobalRef>,
pub webchrome_client: GlobalRef,
}
impl MainPipe<'_> {
pub fn send(message: WebViewMessage) {
let size = std::mem::size_of::<bool>();
if let Ok(()) = CHANNEL.0.send(message) {
unsafe { libc::write(MAIN_PIPE[1], &true as *const _ as *const _, size) };
}
}
pub fn recv(&mut self) -> Result<(), JniError> {
let env = self.env;
let activity = self.activity.as_obj();
if let Ok(message) = CHANNEL.1.recv() {
match message {
WebViewMessage::CreateWebView(attrs) => {
let CreateWebViewAttributes {
url,
devtools,
transparent,
background_color,
} = attrs;
// Create webview
let rust_webview_class = find_my_class(
env,
activity,
format!("{}/RustWebView", PACKAGE.get().unwrap()),
)?;
let webview = env.new_object(
rust_webview_class,
"(Landroid/content/Context;)V",
&[activity.into()],
)?;
// Load URL
if let Ok(url) = env.new_string(url) {
env.call_method(
webview,
"loadUrlMainThread",
"(Ljava/lang/String;)V",
&[url.into()],
)?;
}
// Enable devtools
#[cfg(any(debug_assertions, feature = "devtools"))]
env.call_static_method(
rust_webview_class,
"setWebContentsDebuggingEnabled",
"(Z)V",
&[devtools.into()],
)?;
if transparent {
set_background_color(env, webview, (0, 0, 0, 0))?;
} else {
if let Some(color) = background_color {
set_background_color(env, webview, color)?;
}
}
// Create and set webview client
let rust_webview_client_class = find_my_class(
env,
activity,
format!("{}/RustWebViewClient", PACKAGE.get().unwrap()),
)?;
let webview_client = env.new_object(rust_webview_client_class, "()V", &[])?;
env.call_method(
webview,
"setWebViewClient",
"(Landroid/webkit/WebViewClient;)V",
&[webview_client.into()],
)?;
// set webchrome client
env.call_method(
webview,
"setWebChromeClient",
"(Landroid/webkit/WebChromeClient;)V",
&[self.webchrome_client.as_obj().into()],
)?;
// Add javascript interface (IPC)
let ipc_class = find_my_class(env, activity, format!("{}/Ipc", PACKAGE.get().unwrap()))?;
let ipc = env.new_object(ipc_class, "()V", &[])?;
let ipc_str = env.new_string("ipc")?;
env.call_method(
webview,
"addJavascriptInterface",
"(Ljava/lang/Object;Ljava/lang/String;)V",
&[ipc.into(), ipc_str.into()],
)?;
// Set content view
env.call_method(
activity,
"setContentView",
"(Landroid/view/View;)V",
&[webview.into()],
)?;
let webview = env.new_global_ref(webview)?;
self.webview = Some(webview);
}
WebViewMessage::Eval(script) => {
if let Some(webview) = &self.webview {
let s = env.new_string(script)?;
env.call_method(
webview.as_obj(),
"evaluateJavascript",
"(Ljava/lang/String;Landroid/webkit/ValueCallback;)V",
&[s.into(), JObject::null().into()],
)?;
}
}
WebViewMessage::SetBackgroundColor(background_color) => {
if let Some(webview) = &self.webview {
set_background_color(env, webview.as_obj(), background_color)?;
}
}
WebViewMessage::GetWebViewVersion(tx) => {
match env
.call_method(activity, "getVersion", "()Ljava/lang/String;", &[])
.and_then(|v| v.l())
.and_then(|s| env.get_string(s.into()))
{
Ok(version) => {
tx.send(Ok(version.to_string_lossy().into())).unwrap();
}
Err(e) => tx.send(Err(e.into())).unwrap(),
}
}
WebViewMessage::GetUrl(tx) => {
if let Some(webview) = &self.webview {
let url = env
.call_method(webview.as_obj(), "getUrl", "()Ljava/lang/String", &[])
.and_then(|v| v.l())
.and_then(|s| env.get_string(s.into()))
.map(|u| u.to_string_lossy().into())
.unwrap_or_default();
tx.send(url).unwrap()
}
}
WebViewMessage::Jni(f) => {
if let Some(webview) = &self.webview {
f(env, activity, webview.as_obj());
}
}
}
}
Ok(())
}
}
fn set_background_color<'a>(
env: JNIEnv<'a>,
webview: JObject<'a>,
background_color: RGBA,
) -> Result<(), JniError> {
let color_class = env.find_class("android/graphics/Color")?;
let color = env.call_static_method(
color_class,
"argb",
"(IIII)I",
&[
background_color.3.into(),
background_color.0.into(),
background_color.1.into(),
background_color.2.into(),
],
)?;
env.call_method(webview, "setBackgroundColor", "(I)V", &[color])?;
Ok(())
}
pub enum WebViewMessage {
CreateWebView(CreateWebViewAttributes),
Eval(String),
SetBackgroundColor(RGBA),
GetWebViewVersion(Sender<Result<String, Error>>),
GetUrl(Sender<String>),
Jni(Box<dyn FnOnce(JNIEnv, JObject, JObject) + Send>),
}
#[derive(Debug)]
pub struct CreateWebViewAttributes {
pub url: String,
pub devtools: bool,
pub transparent: bool,
pub background_color: Option<RGBA>,
}