New issue
Advanced search Search tips

Issue 746556 link

Starred by 2 users

Issue metadata

Status: Verified
Owner:
Closed: Aug 2017
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: Android
Pri: 3
Type: Bug



Sign in to add a comment

addJavascriptInterface() does not inject java object

Reported by ed.bake...@gmail.com, Jul 19 2017

Issue description

THIS TEMPLATE IS FOR FILING BUGS ON THE ANDROID SYSTEM WEBVIEW. GENERAL WEB BUGS SHOULD BE FILED USING A DIFFERENT TEMPLATE!

Device name: BlackBerry Priv
Android version: 6.0.1
WebView version (from system settings -> Apps -> Android System WebView): 59.0.3071.125
Application: n/a
Application version: n/a

URLs (if applicable):

Steps to reproduce:
(1) Create a WebView
(2) Use addJavascriptInterface() to inject a Java object
(3) Load data into WebView with <img> element in the html
(4) When the page has loaded enable Javascript
(5) Attempt to access the injected Java object in the DOM

Expected result:
(6) The injected Java object should be accessible in the DOM

Actual result:
(6) The injected Java object is not accessible in the DOM. Consistent behaviour expected, the same html data without an <img> element will have the Java object accessible in the DOM.

Notes:
A reduced test case apk and source code is attached, the test case shows the difference in behaviour with html content with and without an <img> element in two WebViews
 
app-debug.apk
1.3 MB Download
Testwebview.zip
974 KB Download
Labels: -Pri-2 Pri-3
Owner: ntfschr@chromium.org
Status: Assigned (was: Unconfirmed)
This is really strange. I can repro this on M61.

For what it's worth, I think you can work around it by moving the call to WebSettings#setJavaScriptEnabled() earlier (I can't think of a good reason not to do so.

Nonetheless, this sounds to me like a bug. I'll try to investigate more this week or next, so I'm self-assigning.

Comment 2 by torne@chromium.org, Jul 20 2017

This is indeed weird but I think this is just a timing issue in an unsupported 
order of operations. You're waiting for onPageFinished, and loading different content is going to change the timing of how long that takes.

The order of operations with JS interface injection has always been significant (since it doesn't take effect until after a navigation) - I don't think it's particularly surprising that trying to enable JS after the fact doesn't work reliably, and I think it's just a fluke of timing that it works in the case without the <img> tag.

Enable JS first, before calling addJavascriptInterface or navigating.
I agree it's really an edge case, but I have been able to reproduce this with all sorts of long and short html content. The key difference was just adding a single <img> would cause the Java object to not get injected into the DOM.

Comment 4 by torne@chromium.org, Jul 20 2017

<img> typically involves an external resource (even though yours doesn't), and so I can believe that is specifically going to delay onPageFinished as that waits for external resource loads to complete.

As I said, the surprising thing here is that it works in *any* case, not that it doesn't work when you have an <img> :)
Cc: torne@chromium.org
torne@, would a documentation change be in order for addJavascriptInterface?

These orders *seem* ok:

 * setJavaScriptEnabled, addJavascriptInterface, loadData
 * addJavascriptInterface, setJavaScriptEnabled, loadData

While this order is bad:

 * addJavascriptInterface, loadData, setJavaScriptEnabled

For simplicity, we could just say that setJavaScriptEnabled should always be called before addJavascriptInterface, and not promise anything for other orders.
Status: Fixed (was: Assigned)
I resolved this by adding a note in the docs for addJavascriptInterface. I changed the code example to specifically call WebSettings#enableJavaScript() as well as mentioning this in the text of the docs.

Nothing to verify here, we just have to wait a long time (best guess is ~1 year) until the public docs get the update.
Status: Verified (was: Fixed)
Bulk edit: marking stale 'fixed' bugs as 'verified' since they don't need verification at this point.

Sign in to add a comment