| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.android_webview; | 5 package org.chromium.android_webview; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.content.res.AssetManager; | 8 import android.content.res.AssetManager; |
| 9 import android.net.Uri; | 9 import android.net.Uri; |
| 10 import android.util.Log; | 10 import android.util.Log; |
| 11 import android.util.TypedValue; | 11 import android.util.TypedValue; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 | 60 |
| 61 private static int getFieldId(Context context, String assetType, String asse
tName) | 61 private static int getFieldId(Context context, String assetType, String asse
tName) |
| 62 throws ClassNotFoundException, NoSuchFieldException, IllegalAccessExcept
ion { | 62 throws ClassNotFoundException, NoSuchFieldException, IllegalAccessExcept
ion { |
| 63 Class<?> d = context.getClassLoader() | 63 Class<?> d = context.getClassLoader() |
| 64 .loadClass(context.getPackageName() + ".R$" + assetType); | 64 .loadClass(context.getPackageName() + ".R$" + assetType); |
| 65 java.lang.reflect.Field field = d.getField(assetName); | 65 java.lang.reflect.Field field = d.getField(assetName); |
| 66 int id = field.getInt(null); | 66 int id = field.getInt(null); |
| 67 return id; | 67 return id; |
| 68 } | 68 } |
| 69 | 69 |
| 70 private static int getValueType(Context context, int field_id) { | 70 private static int getValueType(Context context, int fieldId) { |
| 71 TypedValue value = new TypedValue(); | 71 TypedValue value = new TypedValue(); |
| 72 context.getResources().getValue(field_id, value, true); | 72 context.getResources().getValue(fieldId, value, true); |
| 73 return value.type; | 73 return value.type; |
| 74 } | 74 } |
| 75 | 75 |
| 76 private static InputStream openResource(Context context, Uri uri) { | 76 private static InputStream openResource(Context context, Uri uri) { |
| 77 assert(uri.getScheme().equals(FILE_SCHEME)); | 77 assert uri.getScheme().equals(FILE_SCHEME); |
| 78 assert(uri.getPath() != null); | 78 assert uri.getPath() != null; |
| 79 assert(uri.getPath().startsWith(nativeGetAndroidResourcePath())); | 79 assert uri.getPath().startsWith(nativeGetAndroidResourcePath()); |
| 80 // The path must be of the form "/android_res/asset_type/asset_name.ext"
. | 80 // The path must be of the form "/android_res/asset_type/asset_name.ext"
. |
| 81 List<String> pathSegments = uri.getPathSegments(); | 81 List<String> pathSegments = uri.getPathSegments(); |
| 82 if (pathSegments.size() != 3) { | 82 if (pathSegments.size() != 3) { |
| 83 Log.e(TAG, "Incorrect resource path: " + uri); | 83 Log.e(TAG, "Incorrect resource path: " + uri); |
| 84 return null; | 84 return null; |
| 85 } | 85 } |
| 86 String assetPath = pathSegments.get(0); | 86 String assetPath = pathSegments.get(0); |
| 87 String assetType = pathSegments.get(1); | 87 String assetType = pathSegments.get(1); |
| 88 String assetName = pathSegments.get(2); | 88 String assetName = pathSegments.get(2); |
| 89 if (!("/" + assetPath + "/").equals(nativeGetAndroidResourcePath())) { | 89 if (!("/" + assetPath + "/").equals(nativeGetAndroidResourcePath())) { |
| 90 Log.e(TAG, "Resource path does not start with " + nativeGetAndroidRe
sourcePath() + | 90 Log.e(TAG, "Resource path does not start with " + nativeGetAndroidRe
sourcePath() + |
| 91 ": " + uri); | 91 ": " + uri); |
| 92 return null; | 92 return null; |
| 93 } | 93 } |
| 94 // Drop the file extension. | 94 // Drop the file extension. |
| 95 assetName = assetName.split("\\.")[0]; | 95 assetName = assetName.split("\\.")[0]; |
| 96 try { | 96 try { |
| 97 // Use the application context for resolving the resource package na
me so that we do | 97 // Use the application context for resolving the resource package na
me so that we do |
| 98 // not use the browser's own resources. Note that if 'context' here
belongs to the | 98 // not use the browser's own resources. Note that if 'context' here
belongs to the |
| 99 // test suite, it does not have a separate application context. In t
hat case we use | 99 // test suite, it does not have a separate application context. In t
hat case we use |
| 100 // the original context object directly. | 100 // the original context object directly. |
| 101 if (context.getApplicationContext() != null) { | 101 if (context.getApplicationContext() != null) { |
| 102 context = context.getApplicationContext(); | 102 context = context.getApplicationContext(); |
| 103 } | 103 } |
| 104 int field_id = getFieldId(context, assetType, assetName); | 104 int fieldId = getFieldId(context, assetType, assetName); |
| 105 int value_type = getValueType(context, field_id); | 105 int valueType = getValueType(context, fieldId); |
| 106 if (value_type == TypedValue.TYPE_STRING) { | 106 if (valueType == TypedValue.TYPE_STRING) { |
| 107 return context.getResources().openRawResource(field_id); | 107 return context.getResources().openRawResource(fieldId); |
| 108 } else { | 108 } else { |
| 109 Log.e(TAG, "Asset not of type string: " + uri); | 109 Log.e(TAG, "Asset not of type string: " + uri); |
| 110 return null; | 110 return null; |
| 111 } | 111 } |
| 112 } catch (ClassNotFoundException e) { | 112 } catch (ClassNotFoundException e) { |
| 113 Log.e(TAG, "Unable to open resource URL: " + uri, e); | 113 Log.e(TAG, "Unable to open resource URL: " + uri, e); |
| 114 return null; | 114 return null; |
| 115 } catch (NoSuchFieldException e) { | 115 } catch (NoSuchFieldException e) { |
| 116 Log.e(TAG, "Unable to open resource URL: " + uri, e); | 116 Log.e(TAG, "Unable to open resource URL: " + uri, e); |
| 117 return null; | 117 return null; |
| 118 } catch (IllegalAccessException e) { | 118 } catch (IllegalAccessException e) { |
| 119 Log.e(TAG, "Unable to open resource URL: " + uri, e); | 119 Log.e(TAG, "Unable to open resource URL: " + uri, e); |
| 120 return null; | 120 return null; |
| 121 } | 121 } |
| 122 } | 122 } |
| 123 | 123 |
| 124 private static InputStream openAsset(Context context, Uri uri) { | 124 private static InputStream openAsset(Context context, Uri uri) { |
| 125 assert(uri.getScheme().equals(FILE_SCHEME)); | 125 assert uri.getScheme().equals(FILE_SCHEME); |
| 126 assert(uri.getPath() != null); | 126 assert uri.getPath() != null; |
| 127 assert(uri.getPath().startsWith(nativeGetAndroidAssetPath())); | 127 assert uri.getPath().startsWith(nativeGetAndroidAssetPath()); |
| 128 String path = uri.getPath().replaceFirst(nativeGetAndroidAssetPath(), ""
); | 128 String path = uri.getPath().replaceFirst(nativeGetAndroidAssetPath(), ""
); |
| 129 try { | 129 try { |
| 130 AssetManager assets = context.getAssets(); | 130 AssetManager assets = context.getAssets(); |
| 131 return assets.open(path, AssetManager.ACCESS_STREAMING); | 131 return assets.open(path, AssetManager.ACCESS_STREAMING); |
| 132 } catch (IOException e) { | 132 } catch (IOException e) { |
| 133 Log.e(TAG, "Unable to open asset URL: " + uri); | 133 Log.e(TAG, "Unable to open asset URL: " + uri); |
| 134 return null; | 134 return null; |
| 135 } | 135 } |
| 136 } | 136 } |
| 137 | 137 |
| 138 private static InputStream openContent(Context context, Uri uri) { | 138 private static InputStream openContent(Context context, Uri uri) { |
| 139 assert(uri.getScheme().equals(CONTENT_SCHEME)); | 139 assert uri.getScheme().equals(CONTENT_SCHEME); |
| 140 try { | 140 try { |
| 141 return context.getContentResolver().openInputStream(uri); | 141 return context.getContentResolver().openInputStream(uri); |
| 142 } catch (Exception e) { | 142 } catch (Exception e) { |
| 143 Log.e(TAG, "Unable to open content URL: " + uri); | 143 Log.e(TAG, "Unable to open content URL: " + uri); |
| 144 return null; | 144 return null; |
| 145 } | 145 } |
| 146 } | 146 } |
| 147 | 147 |
| 148 /** | 148 /** |
| 149 * Determine the mime type for an Android resource. | 149 * Determine the mime type for an Android resource. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 * context. | 207 * context. |
| 208 */ | 208 */ |
| 209 public static void setResourceContextForTesting(Context context) { | 209 public static void setResourceContextForTesting(Context context) { |
| 210 nativeSetResourceContextForTesting(context); | 210 nativeSetResourceContextForTesting(context); |
| 211 } | 211 } |
| 212 | 212 |
| 213 private static native void nativeSetResourceContextForTesting(Context contex
t); | 213 private static native void nativeSetResourceContextForTesting(Context contex
t); |
| 214 private static native String nativeGetAndroidAssetPath(); | 214 private static native String nativeGetAndroidAssetPath(); |
| 215 private static native String nativeGetAndroidResourcePath(); | 215 private static native String nativeGetAndroidResourcePath(); |
| 216 } | 216 } |
| OLD | NEW |