Skip to content

Commit

Permalink
Reland "Android: Remove GetApplicationContext part 2"
Browse files Browse the repository at this point in the history
Original: https://codereview.chromium.org/2784353002
Fix: Extra parameter in ClearBrowsingDataPreferencesBasic.java.

TBR=agrieve@chromium.org,brettw@chromium.org
BUG=646094

Review-Url: https://codereview.chromium.org/2799263002
Cr-Commit-Position: refs/heads/master@{#462457}
  • Loading branch information
wnwen authored and Commit bot committed Apr 6, 2017
1 parent 5784fd6 commit 95d6e72
Show file tree
Hide file tree
Showing 130 changed files with 593 additions and 854 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ protected void startChromiumLocked() {

final Context context = ContextUtils.getApplicationContext();
// Future calls to PlatformServiceBridge.getInstance() rely on it having been created here.
PlatformServiceBridge.getOrCreateInstance(context);
PlatformServiceBridge.getOrCreateInstance();

// Make sure that ResourceProvider is initialized before starting the browser process.
final PackageInfo webViewPackageInfo = WebViewFactory.getLoadedPackageInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,12 +33,12 @@ public class AndroidProtocolHandler {

/**
* 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(Context context, String url) {
public static InputStream open(String url) {
Uri uri = verifyUrl(url);
if (uri == null) {
return null;
Expand All @@ -47,12 +47,12 @@ public static InputStream open(Context context, String url) {
String path = uri.getPath();
if (uri.getScheme().equals(FILE_SCHEME)) {
if (path.startsWith(nativeGetAndroidAssetPath())) {
return openAsset(context, uri);
return openAsset(uri);
} else if (path.startsWith(nativeGetAndroidResourcePath())) {
return openResource(context, uri);
return openResource(uri);
}
} else if (uri.getScheme().equals(CONTENT_SCHEME)) {
return openContent(context, uri);
return openContent(uri);
}
} catch (Exception ex) {
Log.e(TAG, "Error opening inputstream: " + url);
Expand All @@ -69,26 +69,26 @@ private static String removeOneSuffix(String input) {
return input.substring(0, lastDotIndex);
}

private static Class<?> getClazz(Context context, String packageName, String assetType)
throws ClassNotFoundException {
return context.getClassLoader().loadClass(packageName + ".R$" + assetType);
private static Class<?> getClazz(String assetType) throws ClassNotFoundException {
return ContextUtils.getApplicationContext().getClassLoader().loadClass(
ContextUtils.getApplicationContext().getPackageName() + ".R$" + assetType);
}

private static int getFieldId(Context context, String assetType, String assetName)
throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
private static int getFieldId(String assetType, String assetName)
throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
Class<?> clazz = null;
try {
clazz = getClazz(context, context.getPackageName(), assetType);
clazz = getClazz(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 = context.getPackageName();
String packageName = ContextUtils.getApplicationContext().getPackageName();
while (clazz == null) {
packageName = removeOneSuffix(packageName);
// Throw original exception which contains the entire package id.
if (packageName == null) throw e;
try {
clazz = getClazz(context, packageName, assetType);
clazz = getClazz(assetType);
} catch (ClassNotFoundException ignored) {
// Strip and try again.
}
Expand All @@ -100,13 +100,13 @@ private static int getFieldId(Context context, String assetType, String assetNam
return id;
}

private static int getValueType(Context context, int fieldId) {
private static int getValueType(int fieldId) {
TypedValue value = new TypedValue();
context.getResources().getValue(fieldId, value, true);
ContextUtils.getApplicationContext().getResources().getValue(fieldId, value, true);
return value.type;
}

private static InputStream openResource(Context context, Uri uri) {
private static InputStream openResource(Uri uri) {
assert uri.getScheme().equals(FILE_SCHEME);
assert uri.getPath() != null;
assert uri.getPath().startsWith(nativeGetAndroidResourcePath());
Expand All @@ -127,17 +127,10 @@ private static InputStream openResource(Context context, Uri uri) {
// Drop the file extension.
assetName = assetName.split("\\.")[0];
try {
// 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);
int fieldId = getFieldId(assetType, assetName);
int valueType = getValueType(fieldId);
if (valueType == TypedValue.TYPE_STRING) {
return context.getResources().openRawResource(fieldId);
return ContextUtils.getApplicationContext().getResources().openRawResource(fieldId);
} else {
Log.e(TAG, "Asset not of type string: " + uri);
return null;
Expand All @@ -154,24 +147,24 @@ private static InputStream openResource(Context context, Uri uri) {
}
}

private static InputStream openAsset(Context context, Uri uri) {
private static InputStream openAsset(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 = context.getAssets();
AssetManager assets = ContextUtils.getApplicationContext().getAssets();
return assets.open(path, AssetManager.ACCESS_STREAMING);
} catch (IOException e) {
Log.e(TAG, "Unable to open asset URL: " + uri);
return null;
}
}

private static InputStream openContent(Context context, Uri uri) {
private static InputStream openContent(Uri uri) {
assert uri.getScheme().equals(CONTENT_SCHEME);
try {
return context.getContentResolver().openInputStream(uri);
return ContextUtils.getApplicationContext().getContentResolver().openInputStream(uri);
} catch (Exception e) {
Log.e(TAG, "Unable to open content URL: " + uri);
return null;
Expand All @@ -180,13 +173,13 @@ private static InputStream openContent(Context context, Uri uri) {

/**
* Determine the mime type for an Android resource.
* @param context The context manager.
* @param stream The opened input stream which to examine.
* @param url The url from which the stream was opened.
*
* @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(Context context, InputStream stream, String url) {
public static String getMimeType(InputStream stream, String url) {
Uri uri = verifyUrl(url);
if (uri == null) {
return null;
Expand All @@ -195,10 +188,10 @@ public static String getMimeType(Context context, InputStream stream, String url
String path = uri.getPath();
// The content URL type can be queried directly.
if (uri.getScheme().equals(CONTENT_SCHEME)) {
return context.getContentResolver().getType(uri);
return ContextUtils.getApplicationContext().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;
Expand All @@ -218,6 +211,7 @@ public static String getMimeType(Context context, InputStream stream, String url

/**
* 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) {
Expand All @@ -237,16 +231,7 @@ private static Uri verifyUrl(String url) {
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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.content.Context;
import android.webkit.ValueCallback;

import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.base.ThreadUtils;

Expand All @@ -27,7 +28,7 @@ public class PlatformServiceBridge {

protected PlatformServiceBridge() {}

public static PlatformServiceBridge getOrCreateInstance(Context context) {
public static PlatformServiceBridge getOrCreateInstance() {
// Just to avoid race conditions on sInstance - nothing special about the UI thread.
ThreadUtils.assertOnUiThread();

Expand All @@ -37,7 +38,7 @@ public static PlatformServiceBridge getOrCreateInstance(Context context) {
try {
Class<?> cls = Class.forName(PLATFORM_SERVICE_BRIDGE);
sInstance = (PlatformServiceBridge) cls.getDeclaredConstructor(Context.class)
.newInstance(context);
.newInstance(ContextUtils.getApplicationContext());
return sInstance;
} catch (ClassNotFoundException e) {
// This is not an error; it just means this device doesn't have specialized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ protected MinidumpUploader createMinidumpUploader(PersistableBundle unusedExtras
// Ensure we can use ContextUtils later on (from the minidump_uploader component).
ContextUtils.initApplicationContext(this.getApplicationContext());

return new MinidumpUploaderImpl(new AwMinidumpUploaderDelegate(this));
return new MinidumpUploaderImpl(new AwMinidumpUploaderDelegate());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.chromium.android_webview.PlatformServiceBridge;
import org.chromium.android_webview.command_line.CommandLineUtil;
import org.chromium.base.CommandLine;
import org.chromium.base.ContextUtils;
import org.chromium.base.ThreadUtils;
import org.chromium.base.VisibleForTesting;
import org.chromium.components.minidump_uploader.MinidumpUploaderDelegate;
Expand All @@ -23,21 +24,20 @@
* Android Webview-specific implementations for minidump uploading logic.
*/
public class AwMinidumpUploaderDelegate implements MinidumpUploaderDelegate {
private final Context mContext;
private final ConnectivityManager mConnectivityManager;

private boolean mPermittedByUser = false;

@VisibleForTesting
public AwMinidumpUploaderDelegate(Context context) {
mContext = context;
public AwMinidumpUploaderDelegate() {
mConnectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
(ConnectivityManager) ContextUtils.getApplicationContext().getSystemService(
Context.CONNECTIVITY_SERVICE);
}

@Override
public File getCrashParentDir() {
return CrashReceiverService.createWebViewCrashDir(mContext);
return CrashReceiverService.getOrCreateWebViewCrashDir();
}

@Override
Expand Down Expand Up @@ -76,7 +76,7 @@ public boolean isUploadEnabledForTests() {

@Override
public void prepareToUploadMinidumps(final Runnable startUploads) {
PlatformServiceBridge.getOrCreateInstance(mContext).queryMetricsSetting(
PlatformServiceBridge.getOrCreateInstance().queryMetricsSetting(
new ValueCallback<Boolean>() {
public void onReceiveValue(Boolean enabled) {
ThreadUtils.assertOnUiThread();
Expand Down
Loading

0 comments on commit 95d6e72

Please sign in to comment.