Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): improve initialization scripts implementation #670

Merged
merged 1 commit into from
Aug 24, 2022
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/improve-init-scripts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": patch
---

Improve Android initialization script implementation.
7 changes: 1 addition & 6 deletions src/webview/android/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ use tao::platform::android::ndk_glue::jni::{
JNIEnv,
};

use super::{MainPipe, WebViewMessage, IPC, REQUEST_HANDLER};

#[allow(non_snake_case)]
pub unsafe fn runInitializationScripts(_: JNIEnv, _: JClass, _: JObject) {
MainPipe::send(WebViewMessage::RunInitializationScripts);
}
use super::{IPC, REQUEST_HANDLER};

fn handle_request(env: JNIEnv, request: JObject) -> Result<jobject, JniError> {
let mut request_builder = RequestBuilder::new();
Expand Down
59 changes: 19 additions & 40 deletions src/webview/android/main_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub static MAIN_PIPE: Lazy<[RawFd; 2]> = Lazy::new(|| {
pub struct MainPipe<'a> {
pub env: JNIEnv<'a>,
pub activity: GlobalRef,
pub initialization_scripts: Vec<String>,
pub webview: Option<GlobalRef>,
}

Expand All @@ -37,7 +36,7 @@ impl MainPipe<'_> {
let activity = self.activity.as_obj();
if let Ok(message) = CHANNEL.1.recv() {
match message {
WebViewMessage::CreateWebView(url, mut initialization_scripts, devtools) => {
WebViewMessage::CreateWebView(url, initialization_scripts, devtools) => {
// Create webview
let class = env.find_class("android/webkit/WebView")?;
let webview =
Expand Down Expand Up @@ -67,44 +66,38 @@ impl MainPipe<'_> {
&[devtools.into()],
)?;

// Initialize scripts
self
.initialization_scripts
.append(&mut initialization_scripts);
let string_class = env.find_class("java/lang/String")?;
let initialization_scripts_array = env.new_object_array(
initialization_scripts.len() as i32,
string_class,
env.new_string("")?,
)?;
for (i, script) in initialization_scripts.into_iter().enumerate() {
env.set_object_array_element(
initialization_scripts_array,
i as i32,
env.new_string(script)?,
)?;
}

// Create and set webview client
println!(
"[RUST] webview client {}/RustWebViewClient",
PACKAGE.get().unwrap()
);
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", &[])?;
let webview_client = env.new_object(
rust_webview_client_class,
"([Ljava/lang/String;)V",
&[initialization_scripts_array.into()],
)?;
env.call_method(
webview,
"setWebViewClient",
"(Landroid/webkit/WebViewClient;)V",
&[webview_client.into()],
)?;

// Create and set webchrome client
println!("[RUST] chrome client");
let rust_webchrome_client_class = find_my_class(
env,
activity,
format!("{}/RustWebChromeClient", PACKAGE.get().unwrap()),
)?;
let webchrome_client = env.new_object(rust_webchrome_client_class, "()V", &[])?;
env.call_method(
webview,
"setWebChromeClient",
"(Landroid/webkit/WebChromeClient;)V",
&[webchrome_client.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", &[])?;
Expand All @@ -126,19 +119,6 @@ impl MainPipe<'_> {
let webview = env.new_global_ref(webview)?;
self.webview = Some(webview);
}
WebViewMessage::RunInitializationScripts => {
if let Some(webview) = &self.webview {
for s in &self.initialization_scripts {
let s = env.new_string(s)?;
env.call_method(
webview.as_obj(),
"evaluateJavascript",
"(Ljava/lang/String;Landroid/webkit/ValueCallback;)V",
&[s.into(), JObject::null().into()],
)?;
}
}
}
WebViewMessage::Eval(script) => {
if let Some(webview) = &self.webview {
let s = env.new_string(script)?;
Expand Down Expand Up @@ -176,6 +156,5 @@ fn find_my_class<'a>(
#[derive(Debug)]
pub enum WebViewMessage {
CreateWebView(String, Vec<String>, bool),
RunInitializationScripts,
Eval(String),
}
7 changes: 0 additions & 7 deletions src/webview/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ macro_rules! android_binding {
webview::prelude::*,
};
tao_android_binding!($domain, $package, setup, $main);
android_fn!(
$domain,
$package,
RustWebChromeClient,
runInitializationScripts
);
android_fn!(
$domain,
$package,
Expand Down Expand Up @@ -71,7 +65,6 @@ pub unsafe fn setup(env: JNIEnv, looper: &ForeignLooper, activity: GlobalRef) {
let mut main_pipe = MainPipe {
env,
activity,
initialization_scripts: vec![],
webview: None,
};

Expand Down