Commit 1140d32f authored by Tobias Sargeant's avatar Tobias Sargeant Committed by Commit Bot

[weblayer] Keep a reference to the remote class loader.

This allows us to support starting code loading earlier, as well as
supporting APIs that may not need or want to fully initialize WebLayer,
but just make use of Java code from the WebLayer provider.

Also clean up calls to getApplicationContext to ensure that they happen
at the public API surface rather than internally.

Bug: 1027076
Change-Id: I16d7ce196c680722981883ed7d2be463d482b801
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1928779Reviewed-by: default avatarPavel Shmakov <pshmakov@chromium.org>
Commit-Queue: Tobias Sargeant <tobiasjs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#717659}
parent f118d962
...@@ -28,7 +28,7 @@ public abstract class ChildProcessService extends Service { ...@@ -28,7 +28,7 @@ public abstract class ChildProcessService extends Service {
super.onCreate(); super.onCreate();
try { try {
Context appContext = getApplicationContext(); Context appContext = getApplicationContext();
ClassLoader remoteClassLoader = WebLayer.createRemoteClassLoader(appContext); ClassLoader remoteClassLoader = WebLayer.getOrCreateRemoteClassLoader(appContext);
mImpl = IChildProcessService.Stub.asInterface( mImpl = IChildProcessService.Stub.asInterface(
(IBinder) remoteClassLoader (IBinder) remoteClassLoader
.loadClass("org.chromium.weblayer_private.ChildProcessServiceImpl") .loadClass("org.chromium.weblayer_private.ChildProcessServiceImpl")
......
...@@ -44,6 +44,9 @@ public final class WebLayer { ...@@ -44,6 +44,9 @@ public final class WebLayer {
// load the code. Do not set this in production APKs! // load the code. Do not set this in production APKs!
private static final String PACKAGE_MANIFEST_KEY = "org.chromium.weblayer.WebLayerPackage"; private static final String PACKAGE_MANIFEST_KEY = "org.chromium.weblayer.WebLayerPackage";
@Nullable
private static ClassLoader sRemoteClassLoader;
@Nullable @Nullable
private static WebLayerLoader sLoader; private static WebLayerLoader sLoader;
...@@ -56,11 +59,9 @@ public final class WebLayer { ...@@ -56,11 +59,9 @@ public final class WebLayer {
*/ */
private static IWebLayer connectToWebLayerImplementation(@NonNull Context appContext) private static IWebLayer connectToWebLayerImplementation(@NonNull Context appContext)
throws UnsupportedVersionException { throws UnsupportedVersionException {
// Just in case the app passed an Activity context.
appContext = appContext.getApplicationContext();
ClassLoader remoteClassLoader; ClassLoader remoteClassLoader;
try { try {
remoteClassLoader = createRemoteClassLoader(appContext); remoteClassLoader = getOrCreateRemoteClassLoader(appContext);
} catch (Exception e) { } catch (Exception e) {
throw new AndroidRuntimeException(e); throw new AndroidRuntimeException(e);
} }
...@@ -94,7 +95,7 @@ public final class WebLayer { ...@@ -94,7 +95,7 @@ public final class WebLayer {
throws UnsupportedVersionException { throws UnsupportedVersionException {
ThreadCheck.ensureOnUiThread(); ThreadCheck.ensureOnUiThread();
if (sLoader == null) sLoader = new WebLayerLoader(); if (sLoader == null) sLoader = new WebLayerLoader();
sLoader.loadAsync(appContext, callback); sLoader.loadAsync(appContext.getApplicationContext(), callback);
} }
/** /**
...@@ -111,7 +112,7 @@ public final class WebLayer { ...@@ -111,7 +112,7 @@ public final class WebLayer {
throws UnsupportedVersionException { throws UnsupportedVersionException {
ThreadCheck.ensureOnUiThread(); ThreadCheck.ensureOnUiThread();
if (sLoader == null) sLoader = new WebLayerLoader(); if (sLoader == null) sLoader = new WebLayerLoader();
return sLoader.loadSync(appContext); return sLoader.loadSync(appContext.getApplicationContext());
} }
/** /**
...@@ -253,14 +254,18 @@ public final class WebLayer { ...@@ -253,14 +254,18 @@ public final class WebLayer {
/** /**
* Creates a ClassLoader for the remote (weblayer implementation) side. * Creates a ClassLoader for the remote (weblayer implementation) side.
*/ */
static ClassLoader createRemoteClassLoader(Context appContext) static ClassLoader getOrCreateRemoteClassLoader(Context appContext)
throws PackageManager.NameNotFoundException, ReflectiveOperationException { throws PackageManager.NameNotFoundException, ReflectiveOperationException {
if (sRemoteClassLoader != null) {
return sRemoteClassLoader;
}
String implPackageName = getImplPackageName(appContext); String implPackageName = getImplPackageName(appContext);
if (implPackageName == null) { if (implPackageName == null) {
return createRemoteClassLoaderFromWebViewFactory(appContext); sRemoteClassLoader = createRemoteClassLoaderFromWebViewFactory(appContext);
} else { } else {
return createRemoteClassLoaderFromPackage(appContext, implPackageName); sRemoteClassLoader = createRemoteClassLoaderFromPackage(appContext, implPackageName);
} }
return sRemoteClassLoader;
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment