From d3cbfd5467497b67bc631da9b1b5e0b064db7c7b Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Fri, 14 Sep 2012 16:21:35 -0400 Subject: [PATCH] Add a flag to disable exec() chaining for benchmarking. - Also moved ENABLE_LOCATION_CHANGE_EXEC_MODE to NativeToJsMessageQueue so that all exec() related flags are in one place. --- .../src/org/apache/cordova/CordovaWebViewClient.java | 5 +---- framework/src/org/apache/cordova/ExposedJsApi.java | 8 ++++++-- .../src/org/apache/cordova/NativeToJsMessageQueue.java | 10 +++++++++- .../src/org/apache/cordova/api/PluginManager.java | 8 +++++--- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/framework/src/org/apache/cordova/CordovaWebViewClient.java b/framework/src/org/apache/cordova/CordovaWebViewClient.java index 8229cf7a..fe0e9e9f 100755 --- a/framework/src/org/apache/cordova/CordovaWebViewClient.java +++ b/framework/src/org/apache/cordova/CordovaWebViewClient.java @@ -54,9 +54,6 @@ import android.webkit.WebViewClient; public class CordovaWebViewClient extends WebViewClient { private static final String TAG = "Cordova"; - // Disable URL-based exec() bridge by default since it's a bit of a - // security concern. - private static boolean ENABLE_LOCATION_CHANGE_EXEC_MODE = false; private static final String CORDOVA_EXEC_URL_PREFIX = "http://cdv_exec/"; CordovaInterface cordova; CordovaWebView appView; @@ -124,7 +121,7 @@ public class CordovaWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // Check if it's an exec() bridge command message. - if (ENABLE_LOCATION_CHANGE_EXEC_MODE && url.startsWith(CORDOVA_EXEC_URL_PREFIX)) { + if (NativeToJsMessageQueue.ENABLE_LOCATION_CHANGE_EXEC_MODE && url.startsWith(CORDOVA_EXEC_URL_PREFIX)) { handleExecUrl(url); } diff --git a/framework/src/org/apache/cordova/ExposedJsApi.java b/framework/src/org/apache/cordova/ExposedJsApi.java index b96ec64d..710b2e0c 100755 --- a/framework/src/org/apache/cordova/ExposedJsApi.java +++ b/framework/src/org/apache/cordova/ExposedJsApi.java @@ -40,8 +40,12 @@ import org.json.JSONException; public String exec(String service, String action, String callbackId, String arguments) throws JSONException { jsMessageQueue.setPaused(true); try { - pluginManager.exec(service, action, callbackId, arguments, true /* async */); - return jsMessageQueue.popAndEncode(); + boolean wasSync = pluginManager.exec(service, action, callbackId, arguments, true /* async */); + String ret = ""; + if (!NativeToJsMessageQueue.DISABLE_EXEC_CHAINING || wasSync) { + ret = jsMessageQueue.popAndEncode(); + } + return ret; } finally { jsMessageQueue.setPaused(false); } diff --git a/framework/src/org/apache/cordova/NativeToJsMessageQueue.java b/framework/src/org/apache/cordova/NativeToJsMessageQueue.java index 8e022b4d..d2732c46 100755 --- a/framework/src/org/apache/cordova/NativeToJsMessageQueue.java +++ b/framework/src/org/apache/cordova/NativeToJsMessageQueue.java @@ -41,6 +41,14 @@ public class NativeToJsMessageQueue { // Set this to true to force plugin results to be encoding as // JS instead of the custom format (useful for benchmarking). private static final boolean FORCE_ENCODE_USING_EVAL = false; + + // Disable URL-based exec() bridge by default since it's a bit of a + // security concern. + static final boolean ENABLE_LOCATION_CHANGE_EXEC_MODE = false; + + // Disable sending back native->JS messages during an exec() when the active + // exec() is asynchronous. Set this to true when running bridge benchmarks. + static final boolean DISABLE_EXEC_CHAINING = false; // Arbitrarily chosen upper limit for how much data to send to JS in one shot. private static final int MAX_PAYLOAD_SIZE = 50 * 1024; @@ -68,7 +76,7 @@ public class NativeToJsMessageQueue { private final CordovaInterface cordova; private final CordovaWebView webView; - + public NativeToJsMessageQueue(CordovaWebView webView, CordovaInterface cordova) { this.cordova = cordova; this.webView = webView; diff --git a/framework/src/org/apache/cordova/api/PluginManager.java b/framework/src/org/apache/cordova/api/PluginManager.java index 3230b57f..aef63023 100755 --- a/framework/src/org/apache/cordova/api/PluginManager.java +++ b/framework/src/org/apache/cordova/api/PluginManager.java @@ -213,8 +213,9 @@ public class PluginManager { * @param async Boolean indicating whether the calling JavaScript code is expecting an * immediate return value. If true, either Cordova.callbackSuccess(...) or * Cordova.callbackError(...) is called once the plugin code has executed. + * @return Whether the task completed synchronously. */ - public void exec(final String service, final String action, final String callbackId, final String jsonArgs, final boolean async) { + public boolean exec(final String service, final String action, final String callbackId, final String jsonArgs, final boolean async) { PluginResult cr = null; boolean runAsync = async; try { @@ -237,14 +238,14 @@ public class PluginManager { } } }); - return; + return false; } else { // Call execute on the plugin so that it can do it's thing cr = plugin.execute(action, args, callbackId); // If no result to be sent and keeping callback, then no need to sent back to JavaScript if ((cr.getStatus() == PluginResult.Status.NO_RESULT.ordinal()) && cr.getKeepCallback()) { - return; + return true; } } } @@ -263,6 +264,7 @@ public class PluginManager { cr = new PluginResult(PluginResult.Status.NO_RESULT); } app.sendPluginResult(cr, callbackId); + return true; } /**