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

[TIMOB-25862] Parity: Support async Ti.UI.Webview.evalJS call on Android & iOS #9889

Merged
merged 13 commits into from Sep 25, 2018

Conversation

sgtcoolguy
Copy link
Contributor

@sgtcoolguy sgtcoolguy commented Feb 27, 2018

JIRA: https://jira.appcelerator.org/browse/TIMOB-25862

Description:

  • Add an async variant of Ti.UI.WebView.evalJS, add unit test for it. This matches Windows API.
    This is on both iOS and Android.

@sgtcoolguy
Copy link
Contributor Author

@garymathews @hansemannn @jquick-axway
So if we're unable to find a sync version of this Api to use when run-on-main-thread is on, I think this means the async variant should be available cross-platform and should be the preferred variant - and that the sync variant with no callback needs to be deprecated.

@build build added the android label Feb 27, 2018
TAG,
"Synchronous evalJS is not available when running on the main thread. Please supply an additional callback function argument to be invoked with the result as it's only parameter, to be called when the result is available.");
// FIXME This just times out on main thread! I don't know how else to get this to work...
// EvalJSSyncRunnable future = new EvalJSSyncRunnable(view, code);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code hung just as if I didn't spin off a thread, probably because I have to block below on the get call and the Javascript APIs have to use the UI thread to complete. In other words, I don't think we can run a sync version on main/ui thread.

}

// FIXME: Android is dumb and assumes no trailing semicolon!
webview.evalJS('Ti.API.info("Hello, World!");"WebView.evalJS.TEST"', function (result) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Android implementation of evalJS leaves much to be desired. For one thing, it takes your input and appends:

+"";

So if you have a trailing semicolon (as you should!) it doesn't behave properly. Also, I think it may be double-escaping strings here? I'd expect the result to be 'WebView.evalJS.TEST' not '"WebView.evalJS.TEST"'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome to Android. This is par for the course. Nothing is ever easy.

// When on Android 19+ we can use the builtin evalAsync method!
if (Build.VERSION.SDK_INT >= 19) {
final KrollFunction theCallback = callback;
view.getWebView().evaluateJavascript(code, new ValueCallback<String>() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Google documents that evaluateJavascript() must be called on the main UI thread here...
https://developer.android.com/reference/android/webkit/WebView.html#evaluateJavascript

I looked at Google's source code on github. It'll throw an exception if invoked on any other thread.
https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/java/android/webkit/WebView.java#L1053

@sgtcoolguy sgtcoolguy added this to the 8.0.0 milestone Mar 13, 2018
@sgtcoolguy sgtcoolguy changed the title [TIMOB-25786] Android: WebView eval JS timeout error [TIMOB-25862] Parity: Support async Ti.UI.Webview.evalJS call on Android & iOS Mar 13, 2018
@sgtcoolguy sgtcoolguy modified the milestones: 8.0.0, 7.5.0 Sep 14, 2018
@tidev tidev deleted a comment from build Sep 20, 2018
…k the request into a static map keyed by a globally incrementing int and then post/handleMessage to pull it out and run it in the UI
@build
Copy link
Contributor

build commented Sep 25, 2018

Messages
📖

💾 Here's the generated SDK zipfile.

Generated by 🚫 dangerJS

@sgtcoolguy sgtcoolguy merged commit 2909c0e into tidev:master Sep 25, 2018
@sgtcoolguy sgtcoolguy deleted the eval-async branch September 25, 2018 19:47
sgtcoolguy added a commit to sgtcoolguy/titanium_mobile that referenced this pull request Sep 27, 2018
…oid & iOS (tidev#9889)

* Add an async variant of Ti.UI.WebView.evalJS
* Update docs to include async callback info to Ti.IOStream read/write
* Handle evalJS in async fashion when not invoked from UI
* Add Josh's thread/runnable for wrapping sync read/writes on main thread
* Add async Ti.IOStream #read()/#write() to iOS
sgtcoolguy added a commit to sgtcoolguy/titanium_mobile that referenced this pull request Sep 27, 2018
…oid & iOS (tidev#9889)

* Add an async variant of Ti.UI.WebView.evalJS
* Update docs to include async callback info to Ti.IOStream read/write
* Handle evalJS in async fashion when not invoked from UI
* Add Josh's thread/runnable for wrapping sync read/writes on main thread
* Add async Ti.IOStream #read()/#write() to iOS
sgtcoolguy added a commit to sgtcoolguy/titanium_mobile that referenced this pull request Sep 27, 2018
…oid & iOS (tidev#9889)

* Add an async variant of Ti.UI.WebView.evalJS
* Update docs to include async callback info to Ti.IOStream read/write
* Handle evalJS in async fashion when not invoked from UI
* Add Josh's thread/runnable for wrapping sync read/writes on main thread
* Add async Ti.IOStream #read()/#write() to iOS
sgtcoolguy added a commit to sgtcoolguy/titanium_mobile that referenced this pull request Sep 27, 2018
…oid & iOS (tidev#9889)

* Add an async variant of Ti.UI.WebView.evalJS
* Update docs to include async callback info to Ti.IOStream read/write
* Handle evalJS in async fashion when not invoked from UI
* Add Josh's thread/runnable for wrapping sync read/writes on main thread
* Add async Ti.IOStream #read()/#write() to iOS
sgtcoolguy added a commit to sgtcoolguy/titanium_mobile that referenced this pull request Oct 1, 2018
…oid & iOS (tidev#9889)

* Add an async variant of Ti.UI.WebView.evalJS
* Update docs to include async callback info to Ti.IOStream read/write
* Handle evalJS in async fashion when not invoked from UI
* Add Josh's thread/runnable for wrapping sync read/writes on main thread
* Add async Ti.IOStream #read()/#write() to iOS
sgtcoolguy added a commit that referenced this pull request Oct 1, 2018
…oid & iOS (#9889) (#10347)

* Add an async variant of Ti.UI.WebView.evalJS
* Update docs to include async callback info to Ti.IOStream read/write
* Handle evalJS in async fashion when not invoked from UI
* Add Josh's thread/runnable for wrapping sync read/writes on main thread
* Add async Ti.IOStream #read()/#write() to iOS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants