Commit f7053a84 authored by Peter Kotwicz's avatar Peter Kotwicz Committed by Commit Bot

Remove ability to register multiple ChildProcessCreationParams objects part 2

This CL:
- Removes ChildProcessLauncherHelper#mCreationParams
- Moves public static ChildProcessCreationParams accessors from
  ChildProcessLauncherHelper.java to ChildProcessCreationParams.java
- Changes type of
  ChildProcessLauncherHelper#sSandboxedChildConnectionAllocatorMap
  from
    Map<String, ChildConnectionAllocator>
  to
    ChildConnectionAllocator
  (The map is no longer needed because WebAPKs no longer have special
   ChildProcessCreationParams)
- As there is now just one sandboxed ChildConnectionAllocator we now no
  longer free the allocator when the last connection is freed. This matches
  what we do for
  ChildProcessLauncherHelper#sPrivilegedChildConnectionAllocator

Deleted tests:
- Tested creating multiple allocators
  - testAllocatorForPackage()
  - testCustomCreationParamDoesNotReuseWarmupConnection() introduced in
    https://codereview.chromium.org/2705133002
- Tested that ChildConnectionAllocator is freed when last sandboxed connection
  was freed
  - testSandboxedAllocatorFreed() introduced in
    https://chromium-review.googlesource.com/c/chromium/src/+/513587
  - testSandboxedAllocatorFreedWith2Connections() introduced in
    https://chromium-review.googlesource.com/c/chromium/src/+/588380

BUG=797999

Change-Id: I2147ae24d99a7e8b2d93bb7f5a4a2b5ae37172f8
Reviewed-on: https://chromium-review.googlesource.com/884621
Commit-Queue: Peter Kotwicz <pkotwicz@chromium.org>
Reviewed-by: default avatarBo <boliu@chromium.org>
Reviewed-by: default avatarJay Civelli <jcivelli@chromium.org>
Cr-Commit-Position: refs/heads/master@{#532653}
parent 6d8c81c8
......@@ -6,6 +6,7 @@ package org.chromium.content.browser;
import android.os.Bundle;
import org.chromium.base.ContextUtils;
import org.chromium.base.library_loader.LibraryProcessType;
/**
......@@ -49,24 +50,29 @@ public class ChildProcessCreationParams {
mIgnoreVisibilityForImportance = ignoreVisibilityForImportance;
}
public String getPackageNameForSandboxedService() {
return mPackageNameForSandboxedService;
public void addIntentExtras(Bundle extras) {
extras.putInt(EXTRA_LIBRARY_PROCESS_TYPE, mLibraryProcessType);
}
public boolean getIsSandboxedServiceExternal() {
return mIsSandboxedServiceExternal;
public static String getPackageNameForSandboxedService() {
ChildProcessCreationParams params = ChildProcessCreationParams.getDefault();
return params != null ? params.mPackageNameForSandboxedService
: ContextUtils.getApplicationContext().getPackageName();
}
public boolean getBindToCallerCheck() {
return mBindToCallerCheck;
public static boolean getIsSandboxedServiceExternal() {
ChildProcessCreationParams params = ChildProcessCreationParams.getDefault();
return params != null && params.mIsSandboxedServiceExternal;
}
public boolean getIgnoreVisibilityForImportance() {
return mIgnoreVisibilityForImportance;
public static boolean getBindToCallerCheck() {
ChildProcessCreationParams params = ChildProcessCreationParams.getDefault();
return params != null && params.mBindToCallerCheck;
}
public void addIntentExtras(Bundle extras) {
extras.putInt(EXTRA_LIBRARY_PROCESS_TYPE, mLibraryProcessType);
public static boolean getIgnoreVisibilityForImportance() {
ChildProcessCreationParams params = ChildProcessCreationParams.getDefault();
return params != null && params.mIgnoreVisibilityForImportance;
}
public static int getLibraryProcessType(Bundle extras) {
......
......@@ -57,9 +57,8 @@ public class ChildProcessLauncherHelper {
// A warmed-up connection to a sandboxed service.
private static SpareChildConnection sSpareSandboxedConnection;
// Map from package name to ChildConnectionAllocator.
private static final Map<String, ChildConnectionAllocator>
sSandboxedChildConnectionAllocatorMap = new HashMap<>();
// Allocator used for sandboxed services.
private static ChildConnectionAllocator sSandboxedChildConnectionAllocator;
// Map from PID to ChildProcessLauncherHelper.
@SuppressLint("UseSparseArrays") // TODO(crbug.com/799070): Fix this.
......@@ -82,8 +81,6 @@ public class ChildProcessLauncherHelper {
// Whether the connection is managed by the BindingManager.
private final boolean mUseBindingManager;
private final ChildProcessCreationParams mCreationParams;
// Whether the created process should be sandboxed.
private final boolean mSandboxed;
......@@ -102,7 +99,7 @@ public class ChildProcessLauncherHelper {
@Override
public void onBeforeConnectionAllocated(Bundle bundle) {
populateServiceBundle(bundle, mCreationParams);
populateServiceBundle(bundle);
}
@Override
......@@ -181,7 +178,6 @@ public class ChildProcessLauncherHelper {
public static ChildProcessLauncherHelper createAndStart(
long nativePointer, String[] commandLine, FileDescriptorInfo[] filesToBeMapped) {
assert LauncherThread.runningOnLauncherThread();
ChildProcessCreationParams creationParams = ChildProcessCreationParams.getDefault();
String processType =
ContentSwitches.getSwitchValue(commandLine, ContentSwitches.SWITCH_PROCESS_TYPE);
......@@ -205,8 +201,8 @@ public class ChildProcessLauncherHelper {
? new GpuProcessCallback()
: null;
ChildProcessLauncherHelper processLauncher = new ChildProcessLauncherHelper(nativePointer,
creationParams, commandLine, filesToBeMapped, sandboxed, binderCallback);
ChildProcessLauncherHelper processLauncher = new ChildProcessLauncherHelper(
nativePointer, commandLine, filesToBeMapped, sandboxed, binderCallback);
processLauncher.mLauncher.start(
true /* doSetupConnection */, true /* queueIfNoFreeConnection */);
return processLauncher;
......@@ -232,27 +228,14 @@ public class ChildProcessLauncherHelper {
return;
}
ChildProcessCreationParams creationParams = ChildProcessCreationParams.getDefault();
Bundle serviceBundle = populateServiceBundle(new Bundle(), creationParams);
ChildConnectionAllocator allocator =
getConnectionAllocator(context, creationParams, true /* sandboxed */);
Bundle serviceBundle = populateServiceBundle(new Bundle());
ChildConnectionAllocator allocator = getConnectionAllocator(context, true /* sandboxed */);
sSpareSandboxedConnection = new SpareChildConnection(context, allocator, serviceBundle);
}
public static String getPackageNameFromCreationParams(
Context context, ChildProcessCreationParams params, boolean sandboxed) {
return (sandboxed && params != null) ? params.getPackageNameForSandboxedService()
: context.getPackageName();
}
public static boolean isServiceExternalFromCreationParams(
ChildProcessCreationParams params, boolean sandboxed) {
return sandboxed && params != null && params.getIsSandboxedServiceExternal();
}
public static boolean isServiceBoundToCallerFromCreationParams(
ChildProcessCreationParams params) {
return params == null ? false : params.getBindToCallerCheck();
public static String getPackageNameForService(boolean sandboxed) {
return sandboxed ? ChildProcessCreationParams.getPackageNameForSandboxedService()
: ContextUtils.getApplicationContext().getPackageName();
}
/**
......@@ -268,8 +251,8 @@ public class ChildProcessLauncherHelper {
LauncherThread.post(new Runnable() {
@Override
public void run() {
ChildConnectionAllocator allocator = getConnectionAllocator(
context, ChildProcessCreationParams.getDefault(), true /* sandboxed */);
ChildConnectionAllocator allocator =
getConnectionAllocator(context, true /* sandboxed */);
getBindingManager().startModerateBindingManagement(
context, allocator.getNumberOfServices());
}
......@@ -329,14 +312,12 @@ public class ChildProcessLauncherHelper {
}
@VisibleForTesting
static ChildConnectionAllocator getConnectionAllocator(
Context context, ChildProcessCreationParams creationParams, boolean sandboxed) {
static ChildConnectionAllocator getConnectionAllocator(Context context, boolean sandboxed) {
assert LauncherThread.runningOnLauncherThread();
final String packageName =
getPackageNameFromCreationParams(context, creationParams, sandboxed);
boolean bindToCaller = isServiceBoundToCallerFromCreationParams(creationParams);
final String packageName = getPackageNameForService(sandboxed);
boolean bindToCaller = ChildProcessCreationParams.getBindToCallerCheck();
boolean bindAsExternalService =
isServiceExternalFromCreationParams(creationParams, sandboxed);
sandboxed && ChildProcessCreationParams.getIsSandboxedServiceExternal();
if (!sandboxed) {
if (sPrivilegedChildConnectionAllocator == null) {
......@@ -348,7 +329,7 @@ public class ChildProcessLauncherHelper {
return sPrivilegedChildConnectionAllocator;
}
if (!sSandboxedChildConnectionAllocatorMap.containsKey(packageName)) {
if (sSandboxedChildConnectionAllocator == null) {
Log.w(TAG,
"Create a new ChildConnectionAllocator with package name = %s,"
+ " sandboxed = true",
......@@ -372,10 +353,9 @@ public class ChildProcessLauncherHelper {
connectionAllocator.setConnectionFactoryForTesting(
sSandboxedServiceFactoryForTesting);
}
sSandboxedChildConnectionAllocatorMap.put(packageName, connectionAllocator);
sSandboxedChildConnectionAllocator = connectionAllocator;
final ChildConnectionAllocator finalConnectionAllocator = connectionAllocator;
// Tracks connections removal so the allocator can be freed when no longer used.
connectionAllocator.addListener(new ChildConnectionAllocator.Listener() {
@Override
public void onConnectionAllocated(
......@@ -389,57 +369,21 @@ public class ChildProcessLauncherHelper {
getBindingManager().releaseAllModerateBindings();
}
}
@Override
public void onConnectionFreed(final ChildConnectionAllocator allocator,
ChildProcessConnection connection) {
assert allocator == finalConnectionAllocator;
final ChildConnectionAllocator.Listener listener = this;
// The ChildProcessLauncher may have posted a restart that will create a new
// connection with |allocator|. Delay clearing the allocator until that
// potential restart task has been run.
LauncherThread.post(new Runnable() {
@Override
public void run() {
if (allocator.anyConnectionAllocated()
|| !sSandboxedChildConnectionAllocatorMap.containsKey(
packageName)) {
// Note the second condition above guards against multiple tasks
// like this one posted consecutively removing the listener more
// than once (and asserting).
return;
}
// Last connection was freed, the allocator is going aways, remove
// the listener.
allocator.removeListener(listener);
Log.w(TAG,
"Removing empty ChildConnectionAllocator for package "
+ "name = %s,",
packageName);
ChildConnectionAllocator removedAllocator =
sSandboxedChildConnectionAllocatorMap.remove(packageName);
assert removedAllocator == allocator;
}
});
}
});
}
return sSandboxedChildConnectionAllocatorMap.get(packageName);
return sSandboxedChildConnectionAllocator;
}
private ChildProcessLauncherHelper(long nativePointer,
ChildProcessCreationParams creationParams, String[] commandLine,
private ChildProcessLauncherHelper(long nativePointer, String[] commandLine,
FileDescriptorInfo[] filesToBeMapped, boolean sandboxed, IBinder binderCallback) {
assert LauncherThread.runningOnLauncherThread();
mNativeChildProcessLauncherHelper = nativePointer;
mCreationParams = creationParams;
mUseBindingManager = sandboxed;
mSandboxed = sandboxed;
ChildConnectionAllocator connectionAllocator = getConnectionAllocator(
ContextUtils.getApplicationContext(), mCreationParams, sandboxed);
ChildConnectionAllocator connectionAllocator =
getConnectionAllocator(ContextUtils.getApplicationContext(), sandboxed);
mLauncher = new ChildProcessLauncher(LauncherThread.getHandler(), mLauncherDelegate,
commandLine, filesToBeMapped, connectionAllocator,
binderCallback == null ? null : Arrays.asList(binderCallback));
......@@ -493,7 +437,7 @@ public class ChildProcessLauncherHelper {
}
}
if (mCreationParams != null && mCreationParams.getIgnoreVisibilityForImportance()) {
if (ChildProcessCreationParams.getIgnoreVisibilityForImportance()) {
foreground = false;
boostForPendingViews = false;
}
......@@ -539,10 +483,8 @@ public class ChildProcessLauncherHelper {
return sSandboxedServicesCountForTesting;
}
final ChildProcessCreationParams params = ChildProcessCreationParams.getDefault();
final Context context = ContextUtils.getApplicationContext();
final String packageName = ChildProcessLauncherHelper.getPackageNameFromCreationParams(
context, params, true /* inSandbox */);
final String packageName = ChildProcessCreationParams.getPackageNameForSandboxedService();
try {
return ChildConnectionAllocator.getNumberOfServices(
context, packageName, NUM_SANDBOXED_SERVICES_KEY);
......@@ -589,14 +531,13 @@ public class ChildProcessLauncherHelper {
}
}
private static Bundle populateServiceBundle(
Bundle bundle, ChildProcessCreationParams creationParams) {
boolean bindToCallerCheck = false;
private static Bundle populateServiceBundle(Bundle bundle) {
ChildProcessCreationParams creationParams = ChildProcessCreationParams.getDefault();
if (creationParams != null) {
bindToCallerCheck = creationParams.getBindToCallerCheck();
creationParams.addIntentExtras(bundle);
}
bundle.putBoolean(ChildProcessConstants.EXTRA_BIND_TO_CALLER, bindToCallerCheck);
bundle.putBoolean(ChildProcessConstants.EXTRA_BIND_TO_CALLER,
ChildProcessCreationParams.getBindToCallerCheck());
ChromiumLinkerParams linkerParams = getLinkerParamsForNewConnection();
if (linkerParams != null) linkerParams.populateBundle(bundle);
return bundle;
......@@ -614,12 +555,11 @@ public class ChildProcessLauncherHelper {
}
@VisibleForTesting
public static ChildProcessLauncherHelper createAndStartForTesting(
ChildProcessCreationParams creationParams, String[] commandLine,
public static ChildProcessLauncherHelper createAndStartForTesting(String[] commandLine,
FileDescriptorInfo[] filesToBeMapped, boolean sandboxed, IBinder binderCallback,
boolean doSetupConnection) {
ChildProcessLauncherHelper launcherHelper = new ChildProcessLauncherHelper(
0L, creationParams, commandLine, filesToBeMapped, sandboxed, binderCallback);
0L, commandLine, filesToBeMapped, sandboxed, binderCallback);
launcherHelper.mLauncher.start(doSetupConnection, true /* queueIfNoFreeConnection */);
return launcherHelper;
}
......@@ -630,23 +570,14 @@ public class ChildProcessLauncherHelper {
int count = sPrivilegedChildConnectionAllocator == null
? 0
: sPrivilegedChildConnectionAllocator.allocatedConnectionsCountForTesting();
return count + getConnectedSandboxedServicesCountForTesting(null /* packageName */);
return count + getConnectedSandboxedServicesCountForTesting();
}
@VisibleForTesting
public static int getConnectedSandboxedServicesCountForTesting(String packageName) {
int count = 0;
for (ChildConnectionAllocator allocator : sSandboxedChildConnectionAllocatorMap.values()) {
if (packageName == null || packageName.equals(allocator.getPackageName())) {
count += allocator.allocatedConnectionsCountForTesting();
}
}
return count;
}
@VisibleForTesting
static boolean hasSandboxedConnectionAllocatorForPackage(String packageName) {
return sSandboxedChildConnectionAllocatorMap.containsKey(packageName);
public static int getConnectedSandboxedServicesCountForTesting() {
return sSandboxedChildConnectionAllocator == null
? 0
: sSandboxedChildConnectionAllocator.allocatedConnectionsCountForTesting();
}
@VisibleForTesting
......
......@@ -50,7 +50,6 @@ public class ChildProcessLauncherHelperTest {
// channels that are not being set up in this test.
private static final String[] sProcessWaitArguments = {
"_", "--" + BaseSwitches.RENDERER_WAIT_FOR_JAVA_DEBUGGER};
private static final String EXTERNAL_APK_PACKAGE_NAME = "org.chromium.external.apk";
private static final String DEFAULT_SANDBOXED_PROCESS_SERVICE =
"org.chromium.content.app.SandboxedProcessService";
......@@ -63,24 +62,6 @@ public class ChildProcessLauncherHelperTest {
LibraryLoader.get(LibraryProcessType.PROCESS_CHILD).ensureInitialized();
}
/**
* Tests that external APKs and regular use different ChildConnectionAllocators.
*/
@Test
@MediumTest
@Feature({"ProcessManagement"})
@ChildProcessAllocatorSettings(
sandboxedServiceCount = 4, sandboxedServiceName = DEFAULT_SANDBOXED_PROCESS_SERVICE)
public void testAllocatorForPackage() {
Context appContext = InstrumentationRegistry.getTargetContext();
ChildConnectionAllocator connectionAllocator = getChildConnectionAllocator(
appContext, appContext.getPackageName(), true /* sandboxed */);
ChildConnectionAllocator externalConnectionAllocator = getChildConnectionAllocator(
appContext, EXTERNAL_APK_PACKAGE_NAME, true /* sandboxed */);
Assert.assertNotEquals(connectionAllocator, externalConnectionAllocator);
}
/**
* Tests binding to the same sandboxed service process from multiple processes in the
* same package. This uses the ChildProcessLauncherTestHelperService declared in
......@@ -95,7 +76,8 @@ public class ChildProcessLauncherHelperTest {
@DisabledTest
@ChildProcessAllocatorSettings(
sandboxedServiceCount = 2, sandboxedServiceName = DEFAULT_SANDBOXED_PROCESS_SERVICE)
public void testBindServiceFromMultipleProcesses() throws RemoteException {
public void
testBindServiceFromMultipleProcesses() throws RemoteException {
final Context context = InstrumentationRegistry.getTargetContext();
// Start the Helper service.
......@@ -167,12 +149,12 @@ public class ChildProcessLauncherHelperTest {
// Launch a service from this process. Since slot 0 is already bound by the Helper, it
// will fail to start and the ChildProcessLauncher will retry and use the slot 1.
ChildProcessCreationParams creationParams =
ChildProcessCreationParams.registerDefault(
new ChildProcessCreationParams(context.getPackageName(),
false /* isExternalService */, LibraryProcessType.PROCESS_CHILD,
true /* bindToCallerCheck */, false /* ignoreVisibilityForImportance */);
ChildProcessLauncherHelper launcher = startSandboxedChildProcessWithCreationParams(
creationParams, BLOCK_UNTIL_SETUP, true /* doSetupConnection */);
true /* bindToCallerCheck */, false /* ignoreVisibilityForImportance */));
ChildProcessLauncherHelper launcher =
startSandboxedChildProcess(BLOCK_UNTIL_SETUP, true /* doSetupConnection */);
final ChildProcessConnection retryConnection =
ChildProcessLauncherTestUtils.getConnection(launcher);
......@@ -226,8 +208,7 @@ public class ChildProcessLauncherHelperTest {
replyHandler.mMessage.what);
// The 0th connection should now be usable.
launcher = startSandboxedChildProcessWithCreationParams(
creationParams, BLOCK_UNTIL_SETUP, true /* doSetupConnection */);
launcher = startSandboxedChildProcess(BLOCK_UNTIL_SETUP, true /* doSetupConnection */);
ChildProcessConnection connection = ChildProcessLauncherTestUtils.getConnection(launcher);
Assert.assertEquals(
0, ChildProcessLauncherTestUtils.getConnectionServiceNumber(connection));
......@@ -245,18 +226,14 @@ public class ChildProcessLauncherHelperTest {
blockUntilConnected(connection);
}
private void testWarmUpWithCreationParams(ChildProcessCreationParams creationParams) {
if (creationParams != null) {
ChildProcessCreationParams.registerDefault(creationParams);
}
private void testWarmUpImpl() {
Context context = InstrumentationRegistry.getTargetContext();
warmUpOnUiThreadBlocking(context);
Assert.assertEquals(1, getConnectedSandboxedServicesCount());
ChildProcessLauncherHelper launcherHelper = startSandboxedChildProcessWithCreationParams(
creationParams, BLOCK_UNTIL_SETUP, true /* doSetupConnection */);
ChildProcessLauncherHelper launcherHelper =
startSandboxedChildProcess(BLOCK_UNTIL_SETUP, true /* doSetupConnection */);
// The warm-up connection was used, so no new process should have been created.
Assert.assertEquals(1, getConnectedSandboxedServicesCount());
......@@ -274,7 +251,7 @@ public class ChildProcessLauncherHelperTest {
@Feature({"ProcessManagement"})
public void testWarmUp() {
// Use the default creation parameters.
testWarmUpWithCreationParams(null /* creationParams */);
testWarmUpImpl();
}
@Test
......@@ -282,11 +259,11 @@ public class ChildProcessLauncherHelperTest {
@Feature({"ProcessManagement"})
public void testWarmUpWithBindToCaller() {
Context context = InstrumentationRegistry.getTargetContext();
ChildProcessCreationParams creationParams =
ChildProcessCreationParams.registerDefault(
new ChildProcessCreationParams(context.getPackageName(),
false /* isExternalService */, LibraryProcessType.PROCESS_CHILD,
true /* bindToCallerCheck */, false /* ignoreVisibilityForImportance */);
testWarmUpWithCreationParams(creationParams);
true /* bindToCallerCheck */, false /* ignoreVisibilityForImportance */));
testWarmUpImpl();
}
// Tests that the warm-up connection is freed from its allocator if it crashes.
......@@ -310,8 +287,8 @@ public class ChildProcessLauncherHelperTest {
waitForConnectedSandboxedServicesCount(0);
// And subsequent process launches should work.
ChildProcessLauncherHelper launcher = startSandboxedChildProcess(
null /* packageName */, BLOCK_UNTIL_SETUP, true /* doSetupConnection */);
ChildProcessLauncherHelper launcher =
startSandboxedChildProcess(BLOCK_UNTIL_SETUP, true /* doSetupConnection */);
Assert.assertEquals(1, getConnectedSandboxedServicesCount());
Assert.assertNotNull(ChildProcessLauncherTestUtils.getConnection(launcher));
}
......@@ -326,8 +303,8 @@ public class ChildProcessLauncherHelperTest {
Assert.assertEquals(1, getConnectedSandboxedServicesCount());
ChildProcessLauncherHelper launcherHelper = startSandboxedChildProcessWithCreationParams(
null /* creationParams */, BLOCK_UNTIL_SETUP, true /* doSetupConnection */);
ChildProcessLauncherHelper launcherHelper =
startSandboxedChildProcess(BLOCK_UNTIL_SETUP, true /* doSetupConnection */);
// The warm-up connection was used, so no new process should have been created.
Assert.assertEquals(1, getConnectedSandboxedServicesCount());
......@@ -341,93 +318,12 @@ public class ChildProcessLauncherHelperTest {
waitForConnectedSandboxedServicesCount(0);
}
@Test
@MediumTest
@Feature({"ProcessManagement"})
public void testSandboxedAllocatorFreed() {
final String packageName = InstrumentationRegistry.getTargetContext().getPackageName();
ChildProcessLauncherHelper launcher = startSandboxedChildProcess(
null /* packageName */, BLOCK_UNTIL_SETUP, true /* doSetupConnection */);
Assert.assertTrue(hasSandboxedConnectionAllocatorForPackage(packageName));
stopProcess(launcher);
// Poll until allocator is removed. Need to poll here because actually freeing a connection
// from allocator is a posted task, rather than a direct call from stop.
CriteriaHelper.pollInstrumentationThread(
new Criteria("The connection allocator was not removed.") {
@Override
public boolean isSatisfied() {
return !hasSandboxedConnectionAllocatorForPackage(packageName);
}
});
}
/**
* Tests that 2 connections stopping consecutively don't trigger an assert.
* https://crbug.com/749149
*/
@Test
@MediumTest
@Feature({"ProcessManagement"})
public void testSandboxedAllocatorFreedWith2Connections() {
ChildProcessLauncherHelper launcher1 = startSandboxedChildProcess(
null /* packageName */, BLOCK_UNTIL_SETUP, true /* doSetupConnection */);
ChildProcessLauncherHelper launcher2 = startSandboxedChildProcess(
null /* packageName */, BLOCK_UNTIL_SETUP, true /* doSetupConnection */);
stopProcesses(launcher1, launcher2);
// Wait for the allocator to be removed, so we know both connections' listeners have been
// run.
final String packageName = InstrumentationRegistry.getTargetContext().getPackageName();
CriteriaHelper.pollInstrumentationThread(
new Criteria("The connection allocator was not removed.") {
@Override
public boolean isSatisfied() {
return !hasSandboxedConnectionAllocatorForPackage(packageName);
}
});
}
@Test
@MediumTest
@Feature({"ProcessManagement"})
@ChildProcessAllocatorSettings(sandboxedServiceCount = 4)
public void testCustomCreationParamDoesNotReuseWarmupConnection() {
// Since warmUp only uses default params.
final Context context = InstrumentationRegistry.getTargetContext();
ChildProcessCreationParams defaultCreationParams =
getDefaultChildProcessCreationParams(context.getPackageName());
ChildProcessCreationParams.registerDefault(defaultCreationParams);
ChildProcessCreationParams otherCreationParams = getDefaultChildProcessCreationParams(
InstrumentationRegistry.getContext().getPackageName());
warmUpOnUiThreadBlocking(context);
Assert.assertEquals(1, getConnectedSandboxedServicesCount());
// First create a connnection with different creation params than the default, it should not
// use the warmup connection (note that it won't bind since we are using the wrong package,
// but we need to use a different package to differentiate them, and we can only have 1
// valid package per app).
startSandboxedChildProcessWithCreationParams(
otherCreationParams, DONT_BLOCK, false /* doSetupConnection */);
Assert.assertNotNull(getWarmUpConnection());
// Then start a process with the default creation params, the warmup-connection should be
// used.
startSandboxedChildProcessWithCreationParams(
defaultCreationParams, BLOCK_UNTIL_SETUP, true /* doSetupConnection */);
Assert.assertNull(getWarmUpConnection());
}
@Test
@MediumTest
@Feature({"ProcessManagement"})
public void testLauncherCleanup() throws RemoteException {
ChildProcessLauncherHelper launcher = startSandboxedChildProcess(
null /* packageName */, BLOCK_UNTIL_SETUP, true /* doSetupConnection */);
ChildProcessLauncherHelper launcher =
startSandboxedChildProcess(BLOCK_UNTIL_SETUP, true /* doSetupConnection */);
int pid = getPid(launcher);
Assert.assertNotEquals(0, pid);
......@@ -435,8 +331,7 @@ public class ChildProcessLauncherHelperTest {
stopProcess(launcher);
waitForConnectedSandboxedServicesCount(0);
launcher = startSandboxedChildProcess(
null /* packageName */, BLOCK_UNTIL_SETUP, true /* doSetupConnection */);
launcher = startSandboxedChildProcess(BLOCK_UNTIL_SETUP, true /* doSetupConnection */);
pid = getPid(launcher);
Assert.assertNotEquals(0, pid);
......@@ -447,16 +342,7 @@ public class ChildProcessLauncherHelperTest {
}
private static ChildProcessLauncherHelper startSandboxedChildProcess(
final String packageName, int blockingPolicy, final boolean doSetupConnection) {
ChildProcessCreationParams creationParams =
packageName == null ? null : getDefaultChildProcessCreationParams(packageName);
return startSandboxedChildProcessWithCreationParams(
creationParams, blockingPolicy, doSetupConnection);
}
private static ChildProcessLauncherHelper startSandboxedChildProcessWithCreationParams(
final ChildProcessCreationParams creationParams, int blockingPolicy,
final boolean doSetupConnection) {
int blockingPolicy, final boolean doSetupConnection) {
assert doSetupConnection || blockingPolicy != BLOCK_UNTIL_SETUP;
ChildProcessLauncherHelper launcher =
ChildProcessLauncherTestUtils.runOnLauncherAndGetResult(
......@@ -464,9 +350,9 @@ public class ChildProcessLauncherHelperTest {
@Override
public ChildProcessLauncherHelper call() {
return ChildProcessLauncherHelper.createAndStartForTesting(
creationParams, sProcessWaitArguments,
new FileDescriptorInfo[0], true /* sandboxed */,
null /* binderCallback */, doSetupConnection);
sProcessWaitArguments, new FileDescriptorInfo[0],
true /* sandboxed */, null /* binderCallback */,
doSetupConnection);
}
});
if (blockingPolicy != DONT_BLOCK) {
......@@ -510,31 +396,12 @@ public class ChildProcessLauncherHelperTest {
});
}
private static ChildConnectionAllocator getChildConnectionAllocator(
final Context context, final String packageName, final boolean sandboxed) {
return ChildProcessLauncherTestUtils.runOnLauncherAndGetResult(
new Callable<ChildConnectionAllocator>() {
@Override
public ChildConnectionAllocator call() {
return ChildProcessLauncherHelper.getConnectionAllocator(context,
getDefaultChildProcessCreationParams(packageName), sandboxed);
}
});
}
// Returns the number of sandboxed connection currently connected,
private static int getConnectedSandboxedServicesCount() {
return getConnectedSandboxedServicesCountForPackage(null /* packageName */);
}
// Returns the number of sandboxed connection matching the specificed package name that are
// connected,
private static int getConnectedSandboxedServicesCountForPackage(final String packageName) {
return ChildProcessLauncherTestUtils.runOnLauncherAndGetResult(new Callable<Integer>() {
@Override
public Integer call() {
return ChildProcessLauncherHelper.getConnectedSandboxedServicesCountForTesting(
packageName);
return ChildProcessLauncherHelper.getConnectedSandboxedServicesCountForTesting();
}
});
}
......@@ -545,30 +412,11 @@ public class ChildProcessLauncherHelperTest {
Criteria.equals(targetCount, new Callable<Integer>() {
@Override
public Integer call() {
return getConnectedSandboxedServicesCountForPackage(null /* packageName */);
return getConnectedSandboxedServicesCount();
}
}));
}
private static ChildProcessCreationParams getDefaultChildProcessCreationParams(
String packageName) {
return packageName == null
? null
: new ChildProcessCreationParams(packageName, false /* isExternalService */,
LibraryProcessType.PROCESS_CHILD, false /* bindToCallerCheck */,
false /* ignoreVisibilityForImportance */);
}
private static boolean hasSandboxedConnectionAllocatorForPackage(final String packageName) {
return ChildProcessLauncherTestUtils.runOnLauncherAndGetResult(new Callable<Boolean>() {
@Override
public Boolean call() {
return ChildProcessLauncherHelper.hasSandboxedConnectionAllocatorForPackage(
packageName);
}
});
}
private static ChildProcessConnection retrieveConnection(
final ChildProcessLauncherHelper launcherHelper) {
CriteriaHelper.pollInstrumentationThread(
......
......@@ -77,11 +77,11 @@ public class ChildProcessLauncherTestHelperService extends Service {
private void doBindService(final Message msg) {
String[] commandLine = { "_", "--" + BaseSwitches.RENDERER_WAIT_FOR_JAVA_DEBUGGER };
final boolean bindToCaller = true;
ChildProcessCreationParams params = new ChildProcessCreationParams(getPackageName(), false,
LibraryProcessType.PROCESS_CHILD, bindToCaller,
false /* ignoreVisibilityForImportance */);
ChildProcessCreationParams.registerDefault(new ChildProcessCreationParams(getPackageName(),
false, LibraryProcessType.PROCESS_CHILD, bindToCaller,
false /* ignoreVisibilityForImportance */));
mProcessLauncher = ChildProcessLauncherTestUtils.startForTesting(true /* sandboxed */,
commandLine, new FileDescriptorInfo[0], params, true /* doSetupConnection */);
commandLine, new FileDescriptorInfo[0], true /* doSetupConnection */);
// Poll the launcher until the connection is set up. The main test in
// ChildProcessLauncherTest, which has bound the connection to this service, manages the
......
......@@ -7,7 +7,6 @@ package org.chromium.content_shell_apk;
import org.chromium.base.process_launcher.ChildProcessConnection;
import org.chromium.base.process_launcher.FileDescriptorInfo;
import org.chromium.base.process_launcher.IChildProcessService;
import org.chromium.content.browser.ChildProcessCreationParams;
import org.chromium.content.browser.ChildProcessLauncherHelper;
import org.chromium.content.browser.LauncherThread;
......@@ -55,11 +54,11 @@ public final class ChildProcessLauncherTestUtils {
public static ChildProcessLauncherHelper startForTesting(final boolean sandboxed,
final String[] commandLine, final FileDescriptorInfo[] filesToBeMapped,
final ChildProcessCreationParams params, final boolean doSetupConnection) {
final boolean doSetupConnection) {
return runOnLauncherAndGetResult(new Callable<ChildProcessLauncherHelper>() {
@Override
public ChildProcessLauncherHelper call() {
return ChildProcessLauncherHelper.createAndStartForTesting(params, commandLine,
return ChildProcessLauncherHelper.createAndStartForTesting(commandLine,
filesToBeMapped, sandboxed, null /* binderCallback */, doSetupConnection);
}
});
......
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