Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a5cdd51c authored by Ben Murdoch's avatar Ben Murdoch
Browse files

Add WebView.evaluateJavaScript(String, ValueCallback) API.

This API is intended to replace the "WebView.loadUrl("javascript:...")
pattern that is the current mechanism for executing JavaScript in the
context of the current page displayed in the WebView.

The new API is more convenient - it doesn't trigger the normal URL
loading path - and so does not have side effects such as hiding
the keyboard - and allows the caller to specify a callback that will
be invoked once the script execution is complete.

BUG=9814043

Change-Id: I3f27e8ff5371077d9265430090d61381a3a86e76
parent c0743ec4
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -849,6 +849,23 @@ public class WebView extends AbsoluteLayout
        mProvider.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
    }

    /**
     * Asynchronously evaluates JavaScript in the context of the currently displayed page.
     * If non-null, |resultCallback| will be invoked with any result returned from that
     * execution. This method must be called on the UI thread and the callback will
     * be made on the UI thread.
     *
     * @param script the JavaScript to execute.
     * @param resultCallback A callback to be invoked when the script execution
     *                       completes with the result of the execution (if any).
     *                       May be null if no notificaion of the result is required.
     * @hide pending API council approval and CTS test coverage.
     */
    public void evaluateJavascript(String script, ValueCallback<String> resultCallback) {
        checkThread();
        mProvider.evaluateJavaScript(script, resultCallback);
    }

    /**
     * Saves the current view as a web archive.
     *
+6 −0
Original line number Diff line number Diff line
@@ -2656,6 +2656,12 @@ public final class WebViewClassic implements WebViewProvider, WebViewProvider.Sc
        clearHelpers();
    }

    @Override
    public void evaluateJavaScript(String script, ValueCallback<String> resultCallback) {
        // K-only API not implemented in WebViewClassic.
        throw new IllegalStateException("This API not supported in Classic WebView.");
    }

    /**
     * See {@link WebView#saveWebArchive(String)}
     */
+2 −0
Original line number Diff line number Diff line
@@ -114,6 +114,8 @@ public interface WebViewProvider {
    public void loadDataWithBaseURL(String baseUrl, String data,
            String mimeType, String encoding, String historyUrl);

    public void evaluateJavaScript(String script, ValueCallback<String> resultCallback);

    public void saveWebArchive(String filename);

    public void saveWebArchive(String basename, boolean autoname, ValueCallback<String> callback);