Skip to content
Permalink
Browse files
Android: Update whitelist tests to work with Cordova-Android 4.0
  • Loading branch information
clelland committed Feb 18, 2015
1 parent b0fa400 commit 420e330eeb8f73f8599f6feb8e6a8b4be67dd3c0
Showing 1 changed file with 32 additions and 1 deletion.
@@ -27,6 +27,10 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.json.JSONArray;
import org.json.JSONException;

import org.apache.cordova.PluginManager;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class WhitelistAPI extends CordovaPlugin {
/**
* Executes the request and returns PluginResult.
@@ -50,7 +54,34 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
return true;
} else if (action.equals("URLIsAllowed")) {
String url = args.getString(0);
boolean isAllowed = Config.isUrlWhiteListed(url);
/* This code exists for compatibility between 3.x and 4.x versions of Cordova.
* Previously the CordovaWebView class had a method, getWhitelist, which would
* return a Whitelist object. Since the fixed whitelist is removed in Cordova 4.x,
* the correct call now is to shouldAllowRequest from the plugin manager.
*/
Boolean isAllowed = null;
try {
Method isUrlWhiteListed = Config.class.getDeclaredMethod("isUrlWhitelisted", String.class);
isAllowed = (Boolean)isUrlWhiteListed.invoke(url);
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
if (isAllowed == null) {
try {
Method gpm = webView.getClass().getMethod("getPluginManager");
PluginManager pm = (PluginManager)gpm.invoke(webView);
Method isAllowedMethod = pm.getClass().getMethod("shouldAllowRequest", String.class);
isAllowed = (Boolean)isAllowedMethod.invoke(pm, url);
if (isAllowed == null) {
isAllowed = false;
}
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}

callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, isAllowed));
return true;
}

0 comments on commit 420e330

Please sign in to comment.