| Index: android_webview/java/src/org/chromium/android_webview/AndroidProtocolHandler.java
|
| diff --git a/android_webview/java/src/org/chromium/android_webview/AndroidProtocolHandler.java b/android_webview/java/src/org/chromium/android_webview/AndroidProtocolHandler.java
|
| index dff52eebbc6016f0437f7f9ab22239d3d79ee00b..20717b6e536f2a825f6f41d6319e886957f4a5ba 100644
|
| --- a/android_webview/java/src/org/chromium/android_webview/AndroidProtocolHandler.java
|
| +++ b/android_webview/java/src/org/chromium/android_webview/AndroidProtocolHandler.java
|
| @@ -4,12 +4,12 @@
|
|
|
| package org.chromium.android_webview;
|
|
|
| +import android.content.Context;
|
| import android.content.res.AssetManager;
|
| import android.net.Uri;
|
| import android.util.Log;
|
| import android.util.TypedValue;
|
|
|
| -import org.chromium.base.ContextUtils;
|
| import org.chromium.base.annotations.CalledByNative;
|
| import org.chromium.base.annotations.JNINamespace;
|
|
|
| @@ -33,12 +33,12 @@
|
|
|
| /**
|
| * Open an InputStream for an Android resource.
|
| - *
|
| + * @param context The context manager.
|
| * @param url The url to load.
|
| * @return An InputStream to the Android resource.
|
| */
|
| @CalledByNative
|
| - public static InputStream open(String url) {
|
| + public static InputStream open(Context context, String url) {
|
| Uri uri = verifyUrl(url);
|
| if (uri == null) {
|
| return null;
|
| @@ -47,12 +47,12 @@
|
| String path = uri.getPath();
|
| if (uri.getScheme().equals(FILE_SCHEME)) {
|
| if (path.startsWith(nativeGetAndroidAssetPath())) {
|
| - return openAsset(uri);
|
| + return openAsset(context, uri);
|
| } else if (path.startsWith(nativeGetAndroidResourcePath())) {
|
| - return openResource(uri);
|
| + return openResource(context, uri);
|
| }
|
| } else if (uri.getScheme().equals(CONTENT_SCHEME)) {
|
| - return openContent(uri);
|
| + return openContent(context, uri);
|
| }
|
| } catch (Exception ex) {
|
| Log.e(TAG, "Error opening inputstream: " + url);
|
| @@ -69,26 +69,26 @@
|
| return input.substring(0, lastDotIndex);
|
| }
|
|
|
| - private static Class<?> getClazz(String assetType) throws ClassNotFoundException {
|
| - return ContextUtils.getApplicationContext().getClassLoader().loadClass(
|
| - ContextUtils.getApplicationContext().getPackageName() + ".R$" + assetType);
|
| - }
|
| -
|
| - private static int getFieldId(String assetType, String assetName)
|
| - throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
|
| + private static Class<?> getClazz(Context context, String packageName, String assetType)
|
| + throws ClassNotFoundException {
|
| + return context.getClassLoader().loadClass(packageName + ".R$" + assetType);
|
| + }
|
| +
|
| + private static int getFieldId(Context context, String assetType, String assetName)
|
| + throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
|
| Class<?> clazz = null;
|
| try {
|
| - clazz = getClazz(assetType);
|
| + clazz = getClazz(context, context.getPackageName(), assetType);
|
| } catch (ClassNotFoundException e) {
|
| // Strip out components from the end so gradle generated application suffix such as
|
| // com.example.my.pkg.pro works. This is by no means bulletproof.
|
| - String packageName = ContextUtils.getApplicationContext().getPackageName();
|
| + String packageName = context.getPackageName();
|
| while (clazz == null) {
|
| packageName = removeOneSuffix(packageName);
|
| // Throw original exception which contains the entire package id.
|
| if (packageName == null) throw e;
|
| try {
|
| - clazz = getClazz(assetType);
|
| + clazz = getClazz(context, packageName, assetType);
|
| } catch (ClassNotFoundException ignored) {
|
| // Strip and try again.
|
| }
|
| @@ -100,13 +100,13 @@
|
| return id;
|
| }
|
|
|
| - private static int getValueType(int fieldId) {
|
| + private static int getValueType(Context context, int fieldId) {
|
| TypedValue value = new TypedValue();
|
| - ContextUtils.getApplicationContext().getResources().getValue(fieldId, value, true);
|
| + context.getResources().getValue(fieldId, value, true);
|
| return value.type;
|
| }
|
|
|
| - private static InputStream openResource(Uri uri) {
|
| + private static InputStream openResource(Context context, Uri uri) {
|
| assert uri.getScheme().equals(FILE_SCHEME);
|
| assert uri.getPath() != null;
|
| assert uri.getPath().startsWith(nativeGetAndroidResourcePath());
|
| @@ -127,10 +127,17 @@
|
| // Drop the file extension.
|
| assetName = assetName.split("\\.")[0];
|
| try {
|
| - int fieldId = getFieldId(assetType, assetName);
|
| - int valueType = getValueType(fieldId);
|
| + // Use the application context for resolving the resource package name so that we do
|
| + // not use the browser's own resources. Note that if 'context' here belongs to the
|
| + // test suite, it does not have a separate application context. In that case we use
|
| + // the original context object directly.
|
| + if (context.getApplicationContext() != null) {
|
| + context = context.getApplicationContext();
|
| + }
|
| + int fieldId = getFieldId(context, assetType, assetName);
|
| + int valueType = getValueType(context, fieldId);
|
| if (valueType == TypedValue.TYPE_STRING) {
|
| - return ContextUtils.getApplicationContext().getResources().openRawResource(fieldId);
|
| + return context.getResources().openRawResource(fieldId);
|
| } else {
|
| Log.e(TAG, "Asset not of type string: " + uri);
|
| return null;
|
| @@ -147,13 +154,13 @@
|
| }
|
| }
|
|
|
| - private static InputStream openAsset(Uri uri) {
|
| + private static InputStream openAsset(Context context, Uri uri) {
|
| assert uri.getScheme().equals(FILE_SCHEME);
|
| assert uri.getPath() != null;
|
| assert uri.getPath().startsWith(nativeGetAndroidAssetPath());
|
| String path = uri.getPath().replaceFirst(nativeGetAndroidAssetPath(), "");
|
| try {
|
| - AssetManager assets = ContextUtils.getApplicationContext().getAssets();
|
| + AssetManager assets = context.getAssets();
|
| return assets.open(path, AssetManager.ACCESS_STREAMING);
|
| } catch (IOException e) {
|
| Log.e(TAG, "Unable to open asset URL: " + uri);
|
| @@ -161,10 +168,10 @@
|
| }
|
| }
|
|
|
| - private static InputStream openContent(Uri uri) {
|
| + private static InputStream openContent(Context context, Uri uri) {
|
| assert uri.getScheme().equals(CONTENT_SCHEME);
|
| try {
|
| - return ContextUtils.getApplicationContext().getContentResolver().openInputStream(uri);
|
| + return context.getContentResolver().openInputStream(uri);
|
| } catch (Exception e) {
|
| Log.e(TAG, "Unable to open content URL: " + uri);
|
| return null;
|
| @@ -173,13 +180,13 @@
|
|
|
| /**
|
| * Determine the mime type for an Android resource.
|
| - *
|
| - * @param stream The opened input stream which to examine.
|
| - * @param url The url from which the stream was opened.
|
| + * @param context The context manager.
|
| + * @param stream The opened input stream which to examine.
|
| + * @param url The url from which the stream was opened.
|
| * @return The mime type or null if the type is unknown.
|
| */
|
| @CalledByNative
|
| - public static String getMimeType(InputStream stream, String url) {
|
| + public static String getMimeType(Context context, InputStream stream, String url) {
|
| Uri uri = verifyUrl(url);
|
| if (uri == null) {
|
| return null;
|
| @@ -188,10 +195,10 @@
|
| String path = uri.getPath();
|
| // The content URL type can be queried directly.
|
| if (uri.getScheme().equals(CONTENT_SCHEME)) {
|
| - return ContextUtils.getApplicationContext().getContentResolver().getType(uri);
|
| + return context.getContentResolver().getType(uri);
|
| // Asset files may have a known extension.
|
| } else if (uri.getScheme().equals(FILE_SCHEME)
|
| - && path.startsWith(nativeGetAndroidAssetPath())) {
|
| + && path.startsWith(nativeGetAndroidAssetPath())) {
|
| String mimeType = URLConnection.guessContentTypeFromName(path);
|
| if (mimeType != null) {
|
| return mimeType;
|
| @@ -211,7 +218,6 @@
|
|
|
| /**
|
| * Make sure the given string URL is correctly formed and parse it into a Uri.
|
| - *
|
| * @return a Uri instance, or null if the URL was invalid.
|
| */
|
| private static Uri verifyUrl(String url) {
|
| @@ -231,7 +237,16 @@
|
| return uri;
|
| }
|
|
|
| + /**
|
| + * Set the context to be used for resolving resource queries.
|
| + * @param context Context to be used, or null for the default application
|
| + * context.
|
| + */
|
| + public static void setResourceContextForTesting(Context context) {
|
| + nativeSetResourceContextForTesting(context);
|
| + }
|
| +
|
| + private static native void nativeSetResourceContextForTesting(Context context);
|
| private static native String nativeGetAndroidAssetPath();
|
| -
|
| private static native String nativeGetAndroidResourcePath();
|
| }
|
|
|