From f1d4c0119085a62910055802dbc4d96324e0b37e Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Thu, 19 Feb 2015 10:03:50 -0500 Subject: [PATCH] Merge IceCreamCordovaWebViewClient into AndroidWebViewClient. There was no reason to have it separate. --- .../org/apache/cordova/AndroidWebView.java | 6 +- .../apache/cordova/AndroidWebViewClient.java | 61 ++++++++++++ .../cordova/IceCreamCordovaWebViewClient.java | 97 ------------------- 3 files changed, 62 insertions(+), 102 deletions(-) delete mode 100644 framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java diff --git a/framework/src/org/apache/cordova/AndroidWebView.java b/framework/src/org/apache/cordova/AndroidWebView.java index 799318c2..c576af0a 100755 --- a/framework/src/org/apache/cordova/AndroidWebView.java +++ b/framework/src/org/apache/cordova/AndroidWebView.java @@ -49,7 +49,6 @@ import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebSettings.LayoutAlgorithm; import android.webkit.WebViewClient; -import android.webkit.CookieManager; import android.widget.FrameLayout; @@ -144,12 +143,9 @@ public class AndroidWebView extends WebView implements CordovaWebView { pluginManager.init(); if (this.viewClient == null) { - setWebViewClient(Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH ? - new AndroidWebViewClient(cordova, this) : - new IceCreamCordovaWebViewClient(cordova, this)); + setWebViewClient(new AndroidWebViewClient(cordova, this)); } - if (this.chromeClient == null) { setWebChromeClient(new AndroidChromeClient(cordova, this)); } diff --git a/framework/src/org/apache/cordova/AndroidWebViewClient.java b/framework/src/org/apache/cordova/AndroidWebViewClient.java index 5fac30e7..06d0c2bd 100755 --- a/framework/src/org/apache/cordova/AndroidWebViewClient.java +++ b/framework/src/org/apache/cordova/AndroidWebViewClient.java @@ -18,6 +18,8 @@ */ package org.apache.cordova; +import java.io.FileNotFoundException; +import java.io.IOException; import java.util.Hashtable; import org.json.JSONException; @@ -28,11 +30,14 @@ import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.graphics.Bitmap; +import android.net.Uri; import android.net.http.SslError; +import android.os.Build; import android.view.View; import android.webkit.ClientCertRequest; import android.webkit.HttpAuthHandler; import android.webkit.SslErrorHandler; +import android.webkit.WebResourceResponse; import android.webkit.WebView; import android.webkit.WebViewClient; @@ -358,4 +363,60 @@ public class AndroidWebViewClient extends WebViewClient { public void clearAuthenticationTokens() { this.authenticationTokens.clear(); } + + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + @Override + public WebResourceResponse shouldInterceptRequest(WebView view, String url) { + try { + // Check the against the whitelist and lock out access to the WebView directory + // Changing this will cause problems for your application + 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); + } + + CordovaResourceApi resourceApi = appView.getResourceApi(); + Uri origUri = Uri.parse(url); + // Allow plugins to intercept WebView requests. + Uri remappedUri = resourceApi.remapUri(origUri); + + if (!origUri.equals(remappedUri) || needsSpecialsInAssetUrlFix(origUri) || needsKitKatContentUrlFix(origUri)) { + CordovaResourceApi.OpenForReadResult result = resourceApi.openForRead(remappedUri, true); + return new WebResourceResponse(result.mimeType, "UTF-8", result.inputStream); + } + // If we don't need to special-case the request, let the browser load it. + return null; + } catch (IOException e) { + if (!(e instanceof FileNotFoundException)) { + LOG.e("IceCreamCordovaWebViewClient", "Error occurred while loading a file (returning a 404).", e); + } + // Results in a 404. + return new WebResourceResponse("text/plain", "UTF-8", null); + } + } + + private static boolean needsKitKatContentUrlFix(Uri uri) { + return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT && "content".equals(uri.getScheme()); + } + + private static boolean needsSpecialsInAssetUrlFix(Uri uri) { + if (CordovaResourceApi.getUriType(uri) != CordovaResourceApi.URI_TYPE_ASSET) { + return false; + } + if (uri.getQuery() != null || uri.getFragment() != null) { + return true; + } + + if (!uri.toString().contains("%")) { + return false; + } + + switch(android.os.Build.VERSION.SDK_INT){ + case android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH: + case android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1: + return true; + } + return false; + } } diff --git a/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java b/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java deleted file mode 100644 index 83ebfc6e..00000000 --- a/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - Licensed to the Apache Software Foundation (ASF) under one - or more contributor license agreements. See the NOTICE file - distributed with this work for additional information - regarding copyright ownership. The ASF licenses this file - to you under the Apache License, Version 2.0 (the - "License"); you may not use this file except in compliance - with the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, - software distributed under the License is distributed on an - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - KIND, either express or implied. See the License for the - specific language governing permissions and limitations - under the License. -*/ -package org.apache.cordova; - -import java.io.FileNotFoundException; -import java.io.IOException; - -import org.apache.cordova.CordovaInterface; -import org.apache.cordova.CordovaResourceApi.OpenForReadResult; -import org.apache.cordova.LOG; - -import android.annotation.TargetApi; -import android.net.Uri; -import android.os.Build; -import android.webkit.WebResourceResponse; -import android.webkit.WebView; - -@TargetApi(Build.VERSION_CODES.HONEYCOMB) -public class IceCreamCordovaWebViewClient extends AndroidWebViewClient { - - private static final String TAG = "IceCreamCordovaWebViewClient"; - - public IceCreamCordovaWebViewClient(CordovaInterface cordova, AndroidWebView view) { - super(cordova, view); - } - - @Override - public WebResourceResponse shouldInterceptRequest(WebView view, String url) { - try { - // Check the against the whitelist and lock out access to the WebView directory - // Changing this will cause problems for your application - 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); - } - - CordovaResourceApi resourceApi = appView.getResourceApi(); - Uri origUri = Uri.parse(url); - // Allow plugins to intercept WebView requests. - Uri remappedUri = resourceApi.remapUri(origUri); - - if (!origUri.equals(remappedUri) || needsSpecialsInAssetUrlFix(origUri) || needsKitKatContentUrlFix(origUri)) { - OpenForReadResult result = resourceApi.openForRead(remappedUri, true); - return new WebResourceResponse(result.mimeType, "UTF-8", result.inputStream); - } - // If we don't need to special-case the request, let the browser load it. - return null; - } catch (IOException e) { - if (!(e instanceof FileNotFoundException)) { - LOG.e("IceCreamCordovaWebViewClient", "Error occurred while loading a file (returning a 404).", e); - } - // Results in a 404. - return new WebResourceResponse("text/plain", "UTF-8", null); - } - } - - private static boolean needsKitKatContentUrlFix(Uri uri) { - return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT && "content".equals(uri.getScheme()); - } - - private static boolean needsSpecialsInAssetUrlFix(Uri uri) { - if (CordovaResourceApi.getUriType(uri) != CordovaResourceApi.URI_TYPE_ASSET) { - return false; - } - if (uri.getQuery() != null || uri.getFragment() != null) { - return true; - } - - if (!uri.toString().contains("%")) { - return false; - } - - switch(android.os.Build.VERSION.SDK_INT){ - case android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH: - case android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1: - return true; - } - return false; - } -}