Skip to content

Commit

Permalink
feat(android): improve initialization scripts implementation (tauri-a…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Aug 24, 2022
1 parent 33e177b commit 1b26d60
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 53 deletions.
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

0 comments on commit 1b26d60

Please sign in to comment.