CB-7897: Update to work with whilelist plugins in Cordova 4.x

This commit is contained in:
Ian Clelland 2014-10-29 12:09:13 -04:00
parent 80010ae309
commit 80e4831a7f

View File

@ -54,10 +54,13 @@ import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView; import org.apache.cordova.CordovaWebView;
import org.apache.cordova.LOG; import org.apache.cordova.LOG;
import org.apache.cordova.PluginManager;
import org.apache.cordova.PluginResult; import org.apache.cordova.PluginResult;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import java.util.StringTokenizer; import java.util.StringTokenizer;
@ -117,9 +120,37 @@ public class InAppBrowser extends CordovaPlugin {
// SELF // SELF
if (SELF.equals(target)) { if (SELF.equals(target)) {
Log.d(LOG_TAG, "in self"); Log.d(LOG_TAG, "in self");
/* This code exists for compatibility between 3.x and 4.x versions of Cordova.
* Previously the Config class had a static method, isUrlWhitelisted(). That
* responsibility has been moved to the plugins, with an aggregating method in
* PluginManager.
*/
Boolean shouldAllowNavigation = null;
if (url.startsWith("javascript:")) {
shouldAllowNavigation = true;
}
if (shouldAllowNavigation == null) {
try {
Method iuw = Config.class.getMethod("isUrlWhiteListed", String.class);
shouldAllowNavigation = (Boolean)iuw.invoke(null, url);
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
if (shouldAllowNavigation == null) {
try {
Method gpm = webView.getClass().getMethod("getPluginManager");
PluginManager pm = (PluginManager)gpm.invoke(webView);
Method san = pm.getClass().getMethod("shouldAllowNavigation", String.class);
shouldAllowNavigation = (Boolean)san.invoke(pm, url);
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
// load in webview // load in webview
if (url.startsWith("file://") || url.startsWith("javascript:") if (Boolean.TRUE.equals(shouldAllowNavigation)) {
|| Config.isUrlWhiteListed(url)) {
Log.d(LOG_TAG, "loading in webview"); Log.d(LOG_TAG, "loading in webview");
webView.loadUrl(url); webView.loadUrl(url);
} }