mirror of
https://github.com/apache/cordova-android.git
synced 2025-03-16 16:51:02 +08:00
Move newly added should* methods of CordovaUriHelper into PluginManager
Doing this so that clients won't mistakenly call the wrong one.
This commit is contained in:
parent
204130a598
commit
c12d93e77f
@ -138,7 +138,7 @@ public class AndroidWebView extends WebView implements CordovaWebView {
|
|||||||
cordova.getActivity().runOnUiThread(r);
|
cordova.getActivity().runOnUiThread(r);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
bridge = new CordovaBridge(pluginManager, nativeToJsMessageQueue, this.cordova.getActivity().getPackageName(), helper);
|
bridge = new CordovaBridge(pluginManager, nativeToJsMessageQueue, this.cordova.getActivity().getPackageName());
|
||||||
initWebViewSettings();
|
initWebViewSettings();
|
||||||
pluginManager.addService(CoreAndroid.PLUGIN_NAME, CoreAndroid.class.getCanonicalName());
|
pluginManager.addService(CoreAndroid.PLUGIN_NAME, CoreAndroid.class.getCanonicalName());
|
||||||
pluginManager.init();
|
pluginManager.init();
|
||||||
@ -383,7 +383,7 @@ public class AndroidWebView extends WebView implements CordovaWebView {
|
|||||||
if (LOG.isLoggable(LOG.DEBUG) && !url.startsWith("javascript:")) {
|
if (LOG.isLoggable(LOG.DEBUG) && !url.startsWith("javascript:")) {
|
||||||
LOG.d(TAG, ">>> loadUrlNow()");
|
LOG.d(TAG, ">>> loadUrlNow()");
|
||||||
}
|
}
|
||||||
if (url.startsWith("javascript:") || helper.shouldAllowNavigation(url)) {
|
if (url.startsWith("javascript:") || pluginManager.shouldAllowNavigation(url)) {
|
||||||
super.loadUrl(url);
|
super.loadUrl(url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -458,7 +458,7 @@ public class AndroidWebView extends WebView implements CordovaWebView {
|
|||||||
if (!openExternal) {
|
if (!openExternal) {
|
||||||
|
|
||||||
// Make sure url is in whitelist
|
// Make sure url is in whitelist
|
||||||
if (helper.shouldAllowNavigation(url)) {
|
if (pluginManager.shouldAllowNavigation(url)) {
|
||||||
// TODO: What about params?
|
// TODO: What about params?
|
||||||
// Load new URL
|
// Load new URL
|
||||||
loadUrlIntoView(url, true);
|
loadUrlIntoView(url, true);
|
||||||
|
@ -38,13 +38,11 @@ public class CordovaBridge {
|
|||||||
private volatile int expectedBridgeSecret = -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;
|
private String loadedUrl;
|
||||||
private String appContentUrlPrefix;
|
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.pluginManager = pluginManager;
|
||||||
this.jsMessageQueue = jsMessageQueue;
|
this.jsMessageQueue = jsMessageQueue;
|
||||||
this.appContentUrlPrefix = "content://" + packageName + ".";
|
this.appContentUrlPrefix = "content://" + packageName + ".";
|
||||||
this.helper = helper;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String jsExec(int bridgeSecret, String service, String action, String callbackId, String arguments) throws JSONException, IllegalAccessException {
|
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.
|
// to navigate to anyway.
|
||||||
if (origin.startsWith("file:") ||
|
if (origin.startsWith("file:") ||
|
||||||
origin.startsWith(this.appContentUrlPrefix) ||
|
origin.startsWith(this.appContentUrlPrefix) ||
|
||||||
helper.shouldAllowNavigation(origin)) {
|
pluginManager.shouldAllowNavigation(origin)) {
|
||||||
// Enable the bridge
|
// Enable the bridge
|
||||||
int bridgeMode = Integer.parseInt(defaultValue.substring(9));
|
int bridgeMode = Integer.parseInt(defaultValue.substring(9));
|
||||||
jsMessageQueue.setBridgeMode(bridgeMode);
|
jsMessageQueue.setBridgeMode(bridgeMode);
|
||||||
|
@ -39,67 +39,6 @@ public class CordovaUriHelper {
|
|||||||
cordova = cdv;
|
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
|
* Give the host application a chance to take over the control when a new url
|
||||||
* is about to be loaded in the current WebView.
|
* 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
|
* Internal urls on file:// or data:// that do not contain "app_webview" are allowed for navigation
|
||||||
* External urls are not allowed.
|
* External urls are not allowed.
|
||||||
*
|
*
|
||||||
* @param view The WebView that is initiating the callback.
|
|
||||||
* @param url The url to be loaded.
|
* @param url The url to be loaded.
|
||||||
* @return true to override, false for default behavior
|
* @return true to override, false for default behavior
|
||||||
*/
|
*/
|
||||||
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
|
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
|
||||||
public boolean shouldOverrideUrlLoading(String url) {
|
public boolean shouldOverrideUrlLoading(String url) {
|
||||||
// Give plugins the chance to handle the url
|
// Give plugins the chance to handle the url
|
||||||
if (shouldAllowNavigation(url)) {
|
if (appView.getPluginManager().shouldAllowNavigation(url)) {
|
||||||
// Allow internal navigation
|
// Allow internal navigation
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (shouldOpenExternalUrl(url)) {
|
if (appView.getPluginManager().shouldOpenExternalUrl(url)) {
|
||||||
// Do nothing other than what the plugins wanted.
|
// Do nothing other than what the plugins wanted.
|
||||||
// If any returned false, then the request was either blocked
|
// If any returned false, then the request was either blocked
|
||||||
// completely, or handled out-of-band by the plugin. If they all
|
// completely, or handled out-of-band by the plugin. If they all
|
||||||
|
@ -45,7 +45,7 @@ public class IceCreamCordovaWebViewClient extends AndroidWebViewClient {
|
|||||||
try {
|
try {
|
||||||
// Check the against the whitelist and lock out access to the WebView directory
|
// Check the against the whitelist and lock out access to the WebView directory
|
||||||
// Changing this will cause problems for your application
|
// 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);
|
LOG.w(TAG, "URL blocked by whitelist: " + url);
|
||||||
// Results in a 404.
|
// Results in a 404.
|
||||||
return new WebResourceResponse("text/plain", "UTF-8", null);
|
return new WebResourceResponse("text/plain", "UTF-8", null);
|
||||||
|
@ -313,22 +313,25 @@ public class PluginManager {
|
|||||||
* false: At least one plugin returned false (block the
|
* false: At least one plugin returned false (block the
|
||||||
* resource)
|
* resource)
|
||||||
*/
|
*/
|
||||||
public Boolean shouldAllowRequest(String url) {
|
public boolean shouldAllowRequest(String url) {
|
||||||
Boolean anyResponded = null;
|
|
||||||
for (PluginEntry entry : this.entryMap.values()) {
|
for (PluginEntry entry : this.entryMap.values()) {
|
||||||
CordovaPlugin plugin = pluginMap.get(entry.service);
|
CordovaPlugin plugin = pluginMap.get(entry.service);
|
||||||
if (plugin != null) {
|
if (plugin != null) {
|
||||||
Boolean result = plugin.shouldAllowRequest(url);
|
Boolean result = plugin.shouldAllowRequest(url);
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
anyResponded = true;
|
return result;
|
||||||
if (!result) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 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
|
* false: At least one plugin returned false (block the
|
||||||
* navigation)
|
* navigation)
|
||||||
*/
|
*/
|
||||||
public Boolean shouldAllowNavigation(String url) {
|
public boolean shouldAllowNavigation(String url) {
|
||||||
Boolean anyResponded = null;
|
|
||||||
for (PluginEntry entry : this.entryMap.values()) {
|
for (PluginEntry entry : this.entryMap.values()) {
|
||||||
CordovaPlugin plugin = pluginMap.get(entry.service);
|
CordovaPlugin plugin = pluginMap.get(entry.service);
|
||||||
if (plugin != null) {
|
if (plugin != null) {
|
||||||
Boolean result = plugin.shouldAllowNavigation(url);
|
Boolean result = plugin.shouldAllowNavigation(url);
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
anyResponded = true;
|
return result;
|
||||||
if (!result) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 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)
|
* intent)
|
||||||
*/
|
*/
|
||||||
public Boolean shouldOpenExternalUrl(String url) {
|
public Boolean shouldOpenExternalUrl(String url) {
|
||||||
Boolean anyResponded = null;
|
|
||||||
for (PluginEntry entry : this.entryMap.values()) {
|
for (PluginEntry entry : this.entryMap.values()) {
|
||||||
CordovaPlugin plugin = pluginMap.get(entry.service);
|
CordovaPlugin plugin = pluginMap.get(entry.service);
|
||||||
if (plugin != null) {
|
if (plugin != null) {
|
||||||
Boolean result = plugin.shouldOpenExternalUrl(url);
|
Boolean result = plugin.shouldOpenExternalUrl(url);
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
anyResponded = true;
|
return result;
|
||||||
if (!result) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// This will be true if all plugins allow the request, or null if no plugins override the method
|
// Default policy:
|
||||||
return anyResponded;
|
// External URLs are not allowed
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user