From c12d93e77f41a363521f064f5e8904796dbcc339 Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Thu, 19 Feb 2015 10:00:56 -0500 Subject: [PATCH] Move newly added should* methods of CordovaUriHelper into PluginManager Doing this so that clients won't mistakenly call the wrong one. --- .../org/apache/cordova/AndroidWebView.java | 6 +- .../src/org/apache/cordova/CordovaBridge.java | 6 +- .../org/apache/cordova/CordovaUriHelper.java | 66 +------------------ .../cordova/IceCreamCordovaWebViewClient.java | 2 +- .../src/org/apache/cordova/PluginManager.java | 49 +++++++------- 5 files changed, 34 insertions(+), 95 deletions(-) diff --git a/framework/src/org/apache/cordova/AndroidWebView.java b/framework/src/org/apache/cordova/AndroidWebView.java index c814c666..799318c2 100755 --- a/framework/src/org/apache/cordova/AndroidWebView.java +++ b/framework/src/org/apache/cordova/AndroidWebView.java @@ -138,7 +138,7 @@ public class AndroidWebView extends WebView implements CordovaWebView { cordova.getActivity().runOnUiThread(r); } })); - bridge = new CordovaBridge(pluginManager, nativeToJsMessageQueue, this.cordova.getActivity().getPackageName(), helper); + bridge = new CordovaBridge(pluginManager, nativeToJsMessageQueue, this.cordova.getActivity().getPackageName()); initWebViewSettings(); pluginManager.addService(CoreAndroid.PLUGIN_NAME, CoreAndroid.class.getCanonicalName()); pluginManager.init(); @@ -383,7 +383,7 @@ public class AndroidWebView extends WebView implements CordovaWebView { if (LOG.isLoggable(LOG.DEBUG) && !url.startsWith("javascript:")) { LOG.d(TAG, ">>> loadUrlNow()"); } - if (url.startsWith("javascript:") || helper.shouldAllowNavigation(url)) { + if (url.startsWith("javascript:") || pluginManager.shouldAllowNavigation(url)) { super.loadUrl(url); } } @@ -458,7 +458,7 @@ public class AndroidWebView extends WebView implements CordovaWebView { if (!openExternal) { // Make sure url is in whitelist - if (helper.shouldAllowNavigation(url)) { + if (pluginManager.shouldAllowNavigation(url)) { // TODO: What about params? // Load new URL loadUrlIntoView(url, true); diff --git a/framework/src/org/apache/cordova/CordovaBridge.java b/framework/src/org/apache/cordova/CordovaBridge.java index a6a97dfb..c55f152d 100644 --- a/framework/src/org/apache/cordova/CordovaBridge.java +++ b/framework/src/org/apache/cordova/CordovaBridge.java @@ -38,13 +38,11 @@ public class CordovaBridge { private volatile int expectedBridgeSecret = -1; // written by UI thread, read by JS thread. private String loadedUrl; private String appContentUrlPrefix; - protected CordovaUriHelper helper; - public CordovaBridge(PluginManager pluginManager, NativeToJsMessageQueue jsMessageQueue, String packageName, CordovaUriHelper helper) { + public CordovaBridge(PluginManager pluginManager, NativeToJsMessageQueue jsMessageQueue, String packageName) { this.pluginManager = pluginManager; this.jsMessageQueue = jsMessageQueue; this.appContentUrlPrefix = "content://" + packageName + "."; - this.helper = helper; } public String jsExec(int bridgeSecret, String service, String action, String callbackId, String arguments) throws JSONException, IllegalAccessException { @@ -173,7 +171,7 @@ public class CordovaBridge { // to navigate to anyway. if (origin.startsWith("file:") || origin.startsWith(this.appContentUrlPrefix) || - helper.shouldAllowNavigation(origin)) { + pluginManager.shouldAllowNavigation(origin)) { // Enable the bridge int bridgeMode = Integer.parseInt(defaultValue.substring(9)); jsMessageQueue.setBridgeMode(bridgeMode); diff --git a/framework/src/org/apache/cordova/CordovaUriHelper.java b/framework/src/org/apache/cordova/CordovaUriHelper.java index 64a0a002..5685c705 100644 --- a/framework/src/org/apache/cordova/CordovaUriHelper.java +++ b/framework/src/org/apache/cordova/CordovaUriHelper.java @@ -39,67 +39,6 @@ public class CordovaUriHelper { cordova = cdv; } - /** - * Determine whether the webview should be allowed to navigate to a given URL. - * - * This method implements the default whitelist policy when no plugins override - * shouldAllowNavigation - */ - public boolean shouldAllowNavigation(String url) { - Boolean pluginManagerAllowsNavigation = this.appView.getPluginManager().shouldAllowNavigation(url); - if (pluginManagerAllowsNavigation == null) { - // Default policy: - // Internal urls on file:// or data:// that do not contain "/app_webview/" are allowed for navigation - if(url.startsWith("file://") || url.startsWith("data:")) - { - //This directory on WebKit/Blink based webviews contains SQLite databases! - //DON'T CHANGE THIS UNLESS YOU KNOW WHAT YOU'RE DOING! - return !url.contains("/app_webview/"); - } - return false; - } - return pluginManagerAllowsNavigation; - } - - /** - * Determine whether the webview should be allowed to launch an intent for a given URL. - * - * This method implements the default whitelist policy when no plugins override - * shouldOpenExternalUrl - */ - public boolean shouldOpenExternalUrl(String url) { - Boolean pluginManagerAllowsExternalUrl = this.appView.getPluginManager().shouldOpenExternalUrl(url); - if (pluginManagerAllowsExternalUrl == null) { - // Default policy: - // External URLs are not allowed - return false; - } - return pluginManagerAllowsExternalUrl; - } - - /** - * Determine whether the webview should be allowed to request a resource from a given URL. - * - * This method implements the default whitelist policy when no plugins override - * shouldAllowRequest - */ - public boolean shouldAllowRequest(String url) { - - Boolean pluginManagerAllowsRequest = this.appView.getPluginManager().shouldAllowRequest(url); - if (pluginManagerAllowsRequest == null) { - // Default policy: - // Internal urls on file:// or data:// that do not contain "/app_webview/" are allowed for navigation - if(url.startsWith("file://") || url.startsWith("data:")) - { - //This directory on WebKit/Blink based webviews contains SQLite databases! - //DON'T CHANGE THIS UNLESS YOU KNOW WHAT YOU'RE DOING! - return !url.contains("/app_webview/"); - } - return false; - } - return pluginManagerAllowsRequest; - } - /** * Give the host application a chance to take over the control when a new url * is about to be loaded in the current WebView. @@ -109,18 +48,17 @@ public class CordovaUriHelper { * Internal urls on file:// or data:// that do not contain "app_webview" are allowed for navigation * External urls are not allowed. * - * @param view The WebView that is initiating the callback. * @param url The url to be loaded. * @return true to override, false for default behavior */ @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) public boolean shouldOverrideUrlLoading(String url) { // Give plugins the chance to handle the url - if (shouldAllowNavigation(url)) { + if (appView.getPluginManager().shouldAllowNavigation(url)) { // Allow internal navigation return false; } - if (shouldOpenExternalUrl(url)) { + if (appView.getPluginManager().shouldOpenExternalUrl(url)) { // Do nothing other than what the plugins wanted. // If any returned false, then the request was either blocked // completely, or handled out-of-band by the plugin. If they all diff --git a/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java b/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java index 33b7bace..83ebfc6e 100644 --- a/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java +++ b/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java @@ -45,7 +45,7 @@ public class IceCreamCordovaWebViewClient extends AndroidWebViewClient { try { // Check the against the whitelist and lock out access to the WebView directory // Changing this will cause problems for your application - if (!helper.shouldAllowRequest(url)) { + if (!appView.getPluginManager().shouldAllowRequest(url)) { LOG.w(TAG, "URL blocked by whitelist: " + url); // Results in a 404. return new WebResourceResponse("text/plain", "UTF-8", null); diff --git a/framework/src/org/apache/cordova/PluginManager.java b/framework/src/org/apache/cordova/PluginManager.java index 133a22eb..e004b639 100755 --- a/framework/src/org/apache/cordova/PluginManager.java +++ b/framework/src/org/apache/cordova/PluginManager.java @@ -313,22 +313,25 @@ public class PluginManager { * false: At least one plugin returned false (block the * resource) */ - public Boolean shouldAllowRequest(String url) { - Boolean anyResponded = null; + public boolean shouldAllowRequest(String url) { for (PluginEntry entry : this.entryMap.values()) { CordovaPlugin plugin = pluginMap.get(entry.service); if (plugin != null) { Boolean result = plugin.shouldAllowRequest(url); if (result != null) { - anyResponded = true; - if (!result) { - return false; - } + return result; } } } - // This will be true if all plugins allow the request, or null if no plugins override the method - return anyResponded; + + // Default policy: + // Internal urls on file:// or data:// that do not contain "/app_webview/" are allowed for navigation + if (url.startsWith("file://") || url.startsWith("data:")) { + //This directory on WebKit/Blink based webviews contains SQLite databases! + //DON'T CHANGE THIS UNLESS YOU KNOW WHAT YOU'RE DOING! + return !url.contains("/app_webview/"); + } + return false; } /** @@ -347,22 +350,25 @@ public class PluginManager { * false: At least one plugin returned false (block the * navigation) */ - public Boolean shouldAllowNavigation(String url) { - Boolean anyResponded = null; + public boolean shouldAllowNavigation(String url) { for (PluginEntry entry : this.entryMap.values()) { CordovaPlugin plugin = pluginMap.get(entry.service); if (plugin != null) { Boolean result = plugin.shouldAllowNavigation(url); if (result != null) { - anyResponded = true; - if (!result) { - return false; - } + return result; } } } - // This will be true if all plugins allow the request, or null if no plugins override the method - return anyResponded; + + // Default policy: + // Internal urls on file:// or data:// that do not contain "/app_webview/" are allowed for navigation + if (url.startsWith("file://") || url.startsWith("data:")) { + // This directory on WebKit/Blink based webviews contains SQLite databases! + // DON'T CHANGE THIS UNLESS YOU KNOW WHAT YOU'RE DOING! + return !url.contains("/app_webview/"); + } + return false; } /** @@ -384,21 +390,18 @@ public class PluginManager { * intent) */ public Boolean shouldOpenExternalUrl(String url) { - Boolean anyResponded = null; for (PluginEntry entry : this.entryMap.values()) { CordovaPlugin plugin = pluginMap.get(entry.service); if (plugin != null) { Boolean result = plugin.shouldOpenExternalUrl(url); if (result != null) { - anyResponded = true; - if (!result) { - return false; - } + return result; } } } - // This will be true if all plugins allow the request, or null if no plugins override the method - return anyResponded; + // Default policy: + // External URLs are not allowed + return false; } /**