Fix ConcurrentModificationException in UrlRemap
Project: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/commit/7e9f136c Tree: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/tree/7e9f136c Diff: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/diff/7e9f136c Branch: refs/heads/master Commit: 7e9f136c3d98106e0cab55a46600a33175be34fd Parents: a924bb0 Author: Andrew Grieve <[email protected]> Authored: Tue Jun 17 16:03:40 2014 -0400 Committer: Andrew Grieve <[email protected]> Committed: Tue Jun 17 16:40:02 2014 -0400 ---------------------------------------------------------------------- UrlRemap/src/android/UrlRemap.java | 110 +++++++++++++++++--------------- 1 file changed, 60 insertions(+), 50 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/7e9f136c/UrlRemap/src/android/UrlRemap.java ---------------------------------------------------------------------- diff --git a/UrlRemap/src/android/UrlRemap.java b/UrlRemap/src/android/UrlRemap.java index b926e6a..fc6bf19 100644 --- a/UrlRemap/src/android/UrlRemap.java +++ b/UrlRemap/src/android/UrlRemap.java @@ -45,34 +45,38 @@ public class UrlRemap extends CordovaPlugin { @Override public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext) throws JSONException { - if ("addAlias".equals(action)) { - RouteParams params = new RouteParams(); - params.matchRegex = Pattern.compile(args.getString(0)); - params.replaceRegex = Pattern.compile(args.getString(1)); - params.replacer = args.getString(2); - params.redirectToReplacedUrl = args.getBoolean(3); - params.allowFurtherRemapping = args.getBoolean(4); - rerouteParams.add(params); - } else if ("clearAllAliases".equals(action)) { - resetMappings(); - } else if ("injectJs".equals(action)) { - RouteParams params = new RouteParams(); - params.matchRegex = Pattern.compile(args.getString(0)); - params.jsToInject = args.getString(1); - rerouteParams.add(params); - } else if ("setResetUrl".equals(action)) { - resetUrlParams = new RouteParams(); - resetUrlParams.matchRegex = Pattern.compile(args.getString(0)); - } else { - return false; + synchronized (rerouteParams) { + if ("addAlias".equals(action)) { + RouteParams params = new RouteParams(); + params.matchRegex = Pattern.compile(args.getString(0)); + params.replaceRegex = Pattern.compile(args.getString(1)); + params.replacer = args.getString(2); + params.redirectToReplacedUrl = args.getBoolean(3); + params.allowFurtherRemapping = args.getBoolean(4); + rerouteParams.add(params); + } else if ("clearAllAliases".equals(action)) { + resetMappings(); + } else if ("injectJs".equals(action)) { + RouteParams params = new RouteParams(); + params.matchRegex = Pattern.compile(args.getString(0)); + params.jsToInject = args.getString(1); + rerouteParams.add(params); + } else if ("setResetUrl".equals(action)) { + resetUrlParams = new RouteParams(); + resetUrlParams.matchRegex = Pattern.compile(args.getString(0)); + } else { + return false; + } + callbackContext.success(); + return true; } - callbackContext.success(); - return true; } public void resetMappings() { - resetUrlParams = null; - rerouteParams.clear(); + synchronized (rerouteParams) { + resetUrlParams = null; + rerouteParams.clear(); + } } private RouteParams getChosenParams(String url, boolean forInjection) { @@ -86,23 +90,25 @@ public class UrlRemap extends CordovaPlugin { @Override public boolean onOverrideUrlLoading(String url) { - if (resetUrlParams != null && resetUrlParams.matchRegex.matcher(url).find()) { - resetMappings(); - } - - RouteParams params = getChosenParams(url, false); - // Check if we need to replace the url - if (params != null && params.redirectToReplacedUrl) { - String newUrl = params.replaceRegex.matcher(url).replaceFirst(params.replacer); - - if (resetUrlParams != null && resetUrlParams.matchRegex.matcher(newUrl).find()) { + synchronized (rerouteParams) { + if (resetUrlParams != null && resetUrlParams.matchRegex.matcher(url).find()) { resetMappings(); } - - webView.loadUrlIntoView(newUrl, false); - return true; + + RouteParams params = getChosenParams(url, false); + // Check if we need to replace the url + if (params != null && params.redirectToReplacedUrl) { + String newUrl = params.replaceRegex.matcher(url).replaceFirst(params.replacer); + + if (resetUrlParams != null && resetUrlParams.matchRegex.matcher(newUrl).find()) { + resetMappings(); + } + + webView.loadUrlIntoView(newUrl, false); + return true; + } + return false; } - return false; } @Override @@ -110,9 +116,11 @@ public class UrlRemap extends CordovaPlugin { // Look for top level navigation changes if ("onPageFinished".equals(id) && data != null) { String url = data.toString(); - RouteParams params = getChosenParams(url, true); - if (params != null) { - webView.sendJavascript(params.jsToInject); + synchronized (rerouteParams) { + RouteParams params = getChosenParams(url, true); + if (params != null) { + webView.sendJavascript(params.jsToInject); + } } } return null; @@ -120,16 +128,18 @@ public class UrlRemap extends CordovaPlugin { @Override public Uri remapUri(Uri uri) { - String uriAsString = uri.toString(); - RouteParams params = getChosenParams(uriAsString, false); - if (params != null && !params.redirectToReplacedUrl) { - String newUrl = params.replaceRegex.matcher(uriAsString).replaceFirst(params.replacer); - Uri ret = Uri.parse(newUrl); - if (params.allowFurtherRemapping) { - ret = webView.getResourceApi().remapUri(ret); + synchronized (rerouteParams) { + String uriAsString = uri.toString(); + RouteParams params = getChosenParams(uriAsString, false); + if (params != null && !params.redirectToReplacedUrl) { + String newUrl = params.replaceRegex.matcher(uriAsString).replaceFirst(params.replacer); + Uri ret = Uri.parse(newUrl); + if (params.allowFurtherRemapping) { + ret = webView.getResourceApi().remapUri(ret); + } + return ret; } - return ret; + return null; } - return null; } }
