diff options
Diffstat (limited to 'android_webview')
2 files changed, 64 insertions, 63 deletions
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 a6e58a3..a802246 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); + } } } diff --git a/android_webview/glue/java/src/com/android/webview/chromium/WebViewChromiumFactoryProvider.java b/android_webview/glue/java/src/com/android/webview/chromium/WebViewChromiumFactoryProvider.java index 5d3d668..041ffe2 100644 --- a/android_webview/glue/java/src/com/android/webview/chromium/WebViewChromiumFactoryProvider.java +++ b/android_webview/glue/java/src/com/android/webview/chromium/WebViewChromiumFactoryProvider.java @@ -79,6 +79,7 @@ public class WebViewChromiumFactoryProvider implements WebViewFactoryProvider { private WebStorageAdapter mWebStorage; private WebViewDatabaseAdapter mWebViewDatabase; private AwDevToolsServer mDevToolsServer; + private Context mWrappedAppContext; private ArrayList<WeakReference<WebViewChromium>> mWebViewsToStart = new ArrayList<WeakReference<WebViewChromium>>(); @@ -291,7 +292,11 @@ public class WebViewChromiumFactoryProvider implements WebViewFactoryProvider { } private Context getWrappedCurrentApplicationContext() { - return ResourcesContextWrapperFactory.get(mWebViewDelegate.getApplication()); + if (mWrappedAppContext == null) { + mWrappedAppContext = ResourcesContextWrapperFactory.get( + mWebViewDelegate.getApplication()); + } + return mWrappedAppContext; } AwBrowserContext getBrowserContext() { |