diff --git a/.changes/improve-init-scripts.md b/.changes/improve-init-scripts.md new file mode 100644 index 000000000..5fbf0dd38 --- /dev/null +++ b/.changes/improve-init-scripts.md @@ -0,0 +1,5 @@ +--- +"wry": patch +--- + +Improve Android initialization script implementation. diff --git a/src/webview/android/binding.rs b/src/webview/android/binding.rs index 928572ed2..c6284e76b 100644 --- a/src/webview/android/binding.rs +++ b/src/webview/android/binding.rs @@ -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 { let mut request_builder = RequestBuilder::new(); diff --git a/src/webview/android/main_pipe.rs b/src/webview/android/main_pipe.rs index 0f42c8f86..fba8a370b 100644 --- a/src/webview/android/main_pipe.rs +++ b/src/webview/android/main_pipe.rs @@ -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, pub webview: Option, } @@ -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 = @@ -67,22 +66,31 @@ 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", @@ -90,21 +98,6 @@ impl MainPipe<'_> { &[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", &[])?; @@ -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)?; @@ -176,6 +156,5 @@ fn find_my_class<'a>( #[derive(Debug)] pub enum WebViewMessage { CreateWebView(String, Vec, bool), - RunInitializationScripts, Eval(String), } diff --git a/src/webview/android/mod.rs b/src/webview/android/mod.rs index 0afa3ba0d..b14c4fd8e 100644 --- a/src/webview/android/mod.rs +++ b/src/webview/android/mod.rs @@ -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, @@ -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, };