From aef96e95e82545a2321d798b469ec7fe60f72803 Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Thu, 10 Jul 2014 11:29:26 -0400 Subject: [PATCH 1/2] Tweak log messages in CordovaBridge with bridgeSecret is wrong --- .../src/org/apache/cordova/CordovaBridge.java | 50 +++++++++---------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/framework/src/org/apache/cordova/CordovaBridge.java b/framework/src/org/apache/cordova/CordovaBridge.java index 2bfad116..081127d5 100644 --- a/framework/src/org/apache/cordova/CordovaBridge.java +++ b/framework/src/org/apache/cordova/CordovaBridge.java @@ -33,7 +33,7 @@ public class CordovaBridge { private static final String LOG_TAG = "CordovaBridge"; private PluginManager pluginManager; private NativeToJsMessageQueue jsMessageQueue; - private volatile int bridgeSecret = -1; // written by UI thread, read by JS thread. + private volatile int expectedBridgeSecret = -1; // written by UI thread, read by JS thread. private String loadedUrl; public CordovaBridge(PluginManager pluginManager, NativeToJsMessageQueue jsMessageQueue) { @@ -41,23 +41,10 @@ public class CordovaBridge { this.jsMessageQueue = jsMessageQueue; } - private final boolean checkBridgeEnabled(String action) { - if (!jsMessageQueue.isBridgeEnabled()) { - if (bridgeSecret == -1) { - Log.d(LOG_TAG, action + " call made before bridge was enabled."); - } else { - Log.d(LOG_TAG, "Ignoring " + action + " from previous page load."); - } - return false; - } - return true; - } - public String jsExec(int bridgeSecret, String service, String action, String callbackId, String arguments) throws JSONException, IllegalAccessException { - if (!checkBridgeEnabled("exec()")) { - return ""; + if (!verifySecret("exec()", bridgeSecret)) { + return null; } - verifySecret(bridgeSecret); // If the arguments weren't received, send a message back to JS. It will switch bridge modes and try again. See CB-2666. // We send a message meant specifically for this case. It starts with "@" so no other message can be encoded into the same string. if (arguments == null) { @@ -70,7 +57,7 @@ public class CordovaBridge { CordovaResourceApi.jsThread = Thread.currentThread(); pluginManager.exec(service, action, callbackId, arguments); - String ret = ""; + String ret = null; if (!NativeToJsMessageQueue.DISABLE_EXEC_CHAINING) { ret = jsMessageQueue.popAndEncode(false); } @@ -84,33 +71,44 @@ public class CordovaBridge { } public void jsSetNativeToJsBridgeMode(int bridgeSecret, int value) throws IllegalAccessException { - verifySecret(bridgeSecret); + if (!verifySecret("setNativeToJsBridgeMode()", bridgeSecret)) { + return; + } jsMessageQueue.setBridgeMode(value); } public String jsRetrieveJsMessages(int bridgeSecret, boolean fromOnlineEvent) throws IllegalAccessException { - if (!checkBridgeEnabled("retrieveJsMessages()")) { - return ""; + if (!verifySecret("retrieveJsMessages()", bridgeSecret)) { + return null; } - verifySecret(bridgeSecret); return jsMessageQueue.popAndEncode(fromOnlineEvent); } - private void verifySecret(int value) throws IllegalAccessException { - if (bridgeSecret < 0 || value != bridgeSecret) { + private boolean verifySecret(String action, int bridgeSecret) throws IllegalAccessException { + if (!jsMessageQueue.isBridgeEnabled()) { + if (bridgeSecret == -1) { + Log.d(LOG_TAG, action + " call made before bridge was enabled."); + } else { + Log.d(LOG_TAG, "Ignoring " + action + " from previous page load."); + } + return false; + } + // Bridge secret wrong and bridge not due to it being from the previous page. + if (expectedBridgeSecret < 0 || bridgeSecret != expectedBridgeSecret) { throw new IllegalAccessException(); } + return true; } /** Called on page transitions */ void clearBridgeSecret() { - bridgeSecret = -1; + expectedBridgeSecret = -1; } /** Called by cordova.js to initialize the bridge. */ int generateBridgeSecret() { - bridgeSecret = (int)(Math.random() * Integer.MAX_VALUE); - return bridgeSecret; + expectedBridgeSecret = (int)(Math.random() * Integer.MAX_VALUE); + return expectedBridgeSecret; } public void reset(String loadedUrl) { From a14c7942557fbaea41438bd3fe104b47997d8371 Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Thu, 10 Jul 2014 11:29:44 -0400 Subject: [PATCH 2/2] Un-deprecate CordovaActivity.init() - it's needed to tweak prefs in onCreate --- .../org/apache/cordova/CordovaActivity.java | 78 +++++++++---------- 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/framework/src/org/apache/cordova/CordovaActivity.java b/framework/src/org/apache/cordova/CordovaActivity.java index d69dd591..1367f1c6 100755 --- a/framework/src/org/apache/cordova/CordovaActivity.java +++ b/framework/src/org/apache/cordova/CordovaActivity.java @@ -207,37 +207,6 @@ public class CordovaActivity extends Activity implements CordovaInterface { } loadConfig(); - - if(!preferences.getBoolean("ShowTitle", false)) - { - getWindow().requestFeature(Window.FEATURE_NO_TITLE); - } - - if(preferences.getBoolean("SetFullscreen", false)) - { - Log.d(TAG, "The SetFullscreen configuration is deprecated in favor of Fullscreen, and will be removed in a future version."); - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - } else if (preferences.getBoolean("Fullscreen", false)) { - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - } else { - getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); - } - - appView = makeWebView(); - appView.init(this, makeWebViewClient(appView), makeChromeClient(appView), pluginEntries, whitelist, preferences); - - // TODO: Have the views set this themselves. - if (preferences.getBoolean("DisallowOverscroll", false)) { - appView.setOverScrollMode(View.OVER_SCROLL_NEVER); - } - createViews(); - - // TODO: Make this a preference (CB-6153) - // Setup the hardware volume controls to handle volume control - setVolumeControlStream(AudioManager.STREAM_MUSIC); } @SuppressWarnings("deprecation") @@ -323,32 +292,58 @@ public class CordovaActivity extends Activity implements CordovaInterface { return webView.makeWebChromeClient(this); } - @Deprecated // No need to call init() anymore. public void init() { this.init(appView, null, null); } @SuppressLint("NewApi") - @Deprecated // No need to call init() anymore. + @Deprecated // Call init() instead and override makeWebView() to customize. public void init(CordovaWebView webView, CordovaWebViewClient webViewClient, CordovaChromeClient webChromeClient) { LOG.d(TAG, "CordovaActivity.init()"); - appView = webView; + if(!preferences.getBoolean("ShowTitle", false)) + { + getWindow().requestFeature(Window.FEATURE_NO_TITLE); + } - if (webViewClient != null) { - this.appView.setWebViewClient(webViewClient); - webViewClient.setWebView(this.appView); + if(preferences.getBoolean("SetFullscreen", false)) + { + Log.d(TAG, "The SetFullscreen configuration is deprecated in favor of Fullscreen, and will be removed in a future version."); + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN); + } else if (preferences.getBoolean("Fullscreen", false)) { + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN); + } else { + getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); } - if (webChromeClient != null) { - this.appView.setWebChromeClient(webChromeClient); - webChromeClient.setWebView(this.appView); + + appView = webView != null ? webView : makeWebView(); + if (appView.pluginManager == null) { + appView.init(this, webViewClient != null ? webViewClient : makeWebViewClient(appView), + webChromeClient != null ? webChromeClient : makeChromeClient(appView), + pluginEntries, whitelist, preferences); } + + // TODO: Have the views set this themselves. + if (preferences.getBoolean("DisallowOverscroll", false)) { + appView.setOverScrollMode(View.OVER_SCROLL_NEVER); + } + createViews(); + + // TODO: Make this a preference (CB-6153) + // Setup the hardware volume controls to handle volume control + setVolumeControlStream(AudioManager.STREAM_MUSIC); } /** * Load the url into the webview. */ public void loadUrl(String url) { + if (appView == null) { + init(); + } this.splashscreenTime = preferences.getInteger("SplashScreenDelay", this.splashscreenTime); String splash = preferences.getString("SplashScreen", null); if(this.splashscreenTime > 0 && splash != null) @@ -436,6 +431,9 @@ public class CordovaActivity extends Activity implements CordovaInterface { */ @Deprecated // Call method on appView directly. public void clearCache() { + if (appView == null) { + init(); + } this.appView.clearCache(true); }