Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Unified Diff: android_webview/glue/java/src/com/android/webview/chromium/ResourcesContextWrapperFactory.java

Issue 1078693003: android_webview: stop leaking Contexts. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | android_webview/glue/java/src/com/android/webview/chromium/WebViewChromiumFactoryProvider.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: android_webview/glue/java/src/com/android/webview/chromium/ResourcesContextWrapperFactory.java
diff --git a/android_webview/glue/java/src/com/android/webview/chromium/ResourcesContextWrapperFactory.java b/android_webview/glue/java/src/com/android/webview/chromium/ResourcesContextWrapperFactory.java
index a6e58a32079e9d6bb081434c933103b816896afd..a802246c634c015e01084013d6811c862e83d9a8 100644
--- a/android_webview/glue/java/src/com/android/webview/chromium/ResourcesContextWrapperFactory.java
+++ b/android_webview/glue/java/src/com/android/webview/chromium/ResourcesContextWrapperFactory.java
@@ -11,87 +11,83 @@ import android.view.LayoutInflater;
import org.chromium.base.annotations.SuppressFBWarnings;
-import java.util.WeakHashMap;
-
/**
* This class allows us to wrap the application context so that the WebView implementation can
* correctly reference both org.chromium.* and application classes which is necessary to properly
- * inflate UI. We keep a weak map from contexts to wrapped contexts to avoid constantly re-wrapping
- * or doubly wrapping contexts.
+ * inflate UI.
*/
public class ResourcesContextWrapperFactory {
- private static WeakHashMap<Context, ContextWrapper> sCtxToWrapper =
- new WeakHashMap<Context, ContextWrapper>();
- private static final Object sLock = new Object();
-
private ResourcesContextWrapperFactory() {}
public static Context get(Context ctx) {
- ContextWrapper wrappedCtx;
- synchronized (sLock) {
- wrappedCtx = sCtxToWrapper.get(ctx);
- if (wrappedCtx == null) {
- wrappedCtx = createWrapper(ctx);
- sCtxToWrapper.put(ctx, wrappedCtx);
- }
+ // Avoid double-wrapping a context.
+ if (ctx instanceof WebViewContextWrapper) {
+ return ctx;
}
- return wrappedCtx;
+ return new WebViewContextWrapper(ctx);
}
- private static ContextWrapper createWrapper(final Context ctx) {
- return new ContextWrapper(ctx) {
- private Context mApplicationContext;
+ private static class WebViewContextWrapper extends ContextWrapper {
+ private Context mApplicationContext;
- @SuppressFBWarnings("DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED")
- @Override
- public ClassLoader getClassLoader() {
- final ClassLoader appCl = getBaseContext().getClassLoader();
- final ClassLoader webViewCl = this.getClass().getClassLoader();
- return new ClassLoader() {
- @Override
- protected Class<?> findClass(String name) throws ClassNotFoundException {
- // First look in the WebViewProvider class loader.
- try {
- return webViewCl.loadClass(name);
- } catch (ClassNotFoundException e) {
- // Look in the app class loader; allowing it to throw
- // ClassNotFoundException.
- return appCl.loadClass(name);
- }
- }
- };
- }
+ public WebViewContextWrapper(Context base) {
+ super(base);
+ }
- @Override
- public Object getSystemService(String name) {
- if (Context.LAYOUT_INFLATER_SERVICE.equals(name)) {
- LayoutInflater i = (LayoutInflater) getBaseContext().getSystemService(name);
- return i.cloneInContext(this);
- } else {
- return getBaseContext().getSystemService(name);
+ @SuppressFBWarnings("DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED")
+ @Override
+ public ClassLoader getClassLoader() {
+ final ClassLoader appCl = getBaseContext().getClassLoader();
+ final ClassLoader webViewCl = this.getClass().getClassLoader();
+ return new ClassLoader() {
+ @Override
+ protected Class<?> findClass(String name) throws ClassNotFoundException {
+ // First look in the WebViewProvider class loader.
+ try {
+ return webViewCl.loadClass(name);
+ } catch (ClassNotFoundException e) {
+ // Look in the app class loader; allowing it to throw
+ // ClassNotFoundException.
+ return appCl.loadClass(name);
+ }
}
+ };
+ }
+
+ @Override
+ public Object getSystemService(String name) {
+ if (Context.LAYOUT_INFLATER_SERVICE.equals(name)) {
+ LayoutInflater i = (LayoutInflater) getBaseContext().getSystemService(name);
+ return i.cloneInContext(this);
+ } else {
+ return getBaseContext().getSystemService(name);
}
+ }
- @Override
- public Context getApplicationContext() {
- if (mApplicationContext == null) {
- mApplicationContext = get(ctx.getApplicationContext());
+ @Override
+ public Context getApplicationContext() {
+ if (mApplicationContext == null) {
+ Context appCtx = getBaseContext().getApplicationContext();
+ if (appCtx == getBaseContext()) {
+ mApplicationContext = this;
+ } else {
+ mApplicationContext = get(appCtx);
}
- return mApplicationContext;
}
+ return mApplicationContext;
+ }
- @Override
- public void registerComponentCallbacks(ComponentCallbacks callback) {
- // We have to override registerComponentCallbacks and unregisterComponentCallbacks
- // since they call getApplicationContext().[un]registerComponentCallbacks()
- // which causes us to go into a loop.
- ctx.registerComponentCallbacks(callback);
- }
+ @Override
+ public void registerComponentCallbacks(ComponentCallbacks callback) {
+ // We have to override registerComponentCallbacks and unregisterComponentCallbacks
+ // since they call getApplicationContext().[un]registerComponentCallbacks()
+ // which causes us to go into a loop.
+ getBaseContext().registerComponentCallbacks(callback);
+ }
- @Override
- public void unregisterComponentCallbacks(ComponentCallbacks callback) {
- ctx.unregisterComponentCallbacks(callback);
- }
- };
+ @Override
+ public void unregisterComponentCallbacks(ComponentCallbacks callback) {
+ getBaseContext().unregisterComponentCallbacks(callback);
+ }
}
}
« no previous file with comments | « no previous file | android_webview/glue/java/src/com/android/webview/chromium/WebViewChromiumFactoryProvider.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698