Skip to content

Commit

Permalink
fix(android): run initialization scripts before page loads (#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Aug 13, 2022
1 parent b464b8a commit 3d66ad0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changes/android-init-scripts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

Fix webview initialization scripts implementation on Android.
37 changes: 27 additions & 10 deletions src/platform_impl/android/ndk_glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ macro_rules! android_fn {
android_fn!($domain, $package, MainActivity, destroy);
android_fn!($domain, $package, MainActivity, memory);
android_fn!($domain, $package, MainActivity, focus, i32);
android_fn!($domain, $package, RustClient, eval);
android_fn!($domain, $package, RustChromeClient, runInitializationScripts);
android_fn!($domain, $package, RustClient, handleRequest, JObject, jobject);
android_fn!($domain, $package, IpcInterface, ipc, JString);
}
Expand Down Expand Up @@ -86,7 +86,7 @@ static MAIN_PIPE: Lazy<[RawFd; 2]> = Lazy::new(|| {
pub struct MainPipe<'a> {
env: JNIEnv<'a>,
activity: GlobalRef,
scripts: Vec<String>,
initialization_scripts: Vec<String>,
webview: Option<GlobalRef>,
}

Expand All @@ -103,7 +103,7 @@ impl MainPipe<'_> {
let activity = self.activity.as_obj();
if let Ok(message) = CHANNEL.1.recv() {
match message {
WebViewMessage::CreateWebView(url, mut scripts, devtools) => {
WebViewMessage::CreateWebView(url, mut initialization_scripts, devtools) => {
// Create webview
let class = env.find_class("android/webkit/WebView")?;
let webview =
Expand Down Expand Up @@ -134,7 +134,9 @@ impl MainPipe<'_> {
)?;

// Initialize scripts
self.scripts.append(&mut scripts);
self
.initialization_scripts
.append(&mut initialization_scripts);

// Set webview client
let client = env.call_method(
Expand All @@ -150,6 +152,20 @@ impl MainPipe<'_> {
&[client.into()],
)?;

// Set chrome client
let chrome_client = env.call_method(
activity,
"getChromeClient",
"()Landroid/webkit/WebChromeClient;",
&[],
)?;
env.call_method(
webview,
"setWebChromeClient",
"(Landroid/webkit/WebChromeClient;)V",
&[chrome_client.into()],
)?;

// Add javascript interface (IPC)
let sig = format!("()L{}/IpcInterface;", PACKAGE.get().unwrap());
let handler = env.call_method(activity, "getIpc", sig, &[])?;
Expand All @@ -171,9 +187,9 @@ impl MainPipe<'_> {
let webview = env.new_global_ref(webview)?;
self.webview = Some(webview);
}
WebViewMessage::Eval => {
WebViewMessage::RunInitializationScripts => {
if let Some(webview) = &self.webview {
for s in self.scripts.drain(..).into_iter() {
for s in &self.initialization_scripts {
let s = env.new_string(s)?;
env.call_method(
webview.as_obj(),
Expand All @@ -193,7 +209,7 @@ impl MainPipe<'_> {
#[derive(Debug)]
pub enum WebViewMessage {
CreateWebView(String, Vec<String>, bool),
Eval,
RunInitializationScripts,
}

pub static IPC: OnceCell<UnsafeIpc> = OnceCell::new();
Expand Down Expand Up @@ -340,7 +356,7 @@ pub unsafe fn create(env: JNIEnv, _jclass: JClass, jobject: JObject, main: fn())
let mut main_pipe = MainPipe {
env,
activity,
scripts: vec![],
initialization_scripts: vec![],
webview: None,
};
let looper = ThreadLooper::for_thread().unwrap().into_foreign();
Expand Down Expand Up @@ -414,8 +430,9 @@ pub unsafe fn create(env: JNIEnv, _jclass: JClass, jobject: JObject, main: fn())
.unwrap();
}

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

pub struct WebResourceRequest {
Expand Down

0 comments on commit 3d66ad0

Please sign in to comment.