Commit 93637728 authored by jcivelli's avatar jcivelli Committed by Commit bot

Making ChildProcessConnection only accessed from the launcher thread.

As a result, removing locks and synchronizations.
Also changing the way we store and report the OOM protected state.
We now update the OOM protected state every time it changes as long as we
are bound (that happens only on the launcher thread).
When retrieving that OOM protected state (which happens on the IO
thread), we return that state directly without the need of a lock.

BUG=714657

Review-Url: https://codereview.chromium.org/2840303002
Cr-Commit-Position: refs/heads/master@{#467892}
parent d97cc734
......@@ -96,6 +96,7 @@ android_library("content_shell_apk_java") {
java_files = [
"shell_apk/src/org/chromium/content_shell_apk/ChildProcessLauncherTestHelperService.java",
"shell_apk/src/org/chromium/content_shell_apk/ChildProcessLauncherTestUtils.java",
"shell_apk/src/org/chromium/content_shell_apk/ContentShellActivity.java",
"shell_apk/src/org/chromium/content_shell_apk/ContentShellApplication.java",
]
......
......@@ -5,7 +5,6 @@
package org.chromium.content_shell_apk;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
......@@ -22,12 +21,6 @@ import org.chromium.base.library_loader.ProcessInitException;
import org.chromium.base.process_launcher.ChildProcessCreationParams;
import org.chromium.base.process_launcher.FileDescriptorInfo;
import org.chromium.content.browser.BaseChildProcessConnection;
import org.chromium.content.browser.ChildProcessLauncher;
import org.chromium.content.browser.LauncherThread;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.Semaphore;
/**
* A Service that assists the ChildProcessLauncherTest that responds to one message, which
......@@ -53,53 +46,6 @@ public class ChildProcessLauncherTestHelperService extends Service {
private final HandlerThread mHandlerThread = new HandlerThread("Helper Service Handler");
public static void runOnLauncherThreadBlocking(final Runnable runnable) {
if (LauncherThread.runningOnLauncherThread()) {
runnable.run();
return;
}
final Semaphore done = new Semaphore(0);
LauncherThread.post(new Runnable() {
@Override
public void run() {
runnable.run();
done.release();
}
});
done.acquireUninterruptibly();
}
public static <R> R runOnLauncherAndGetResult(Callable<R> callable) {
if (LauncherThread.runningOnLauncherThread()) {
try {
return callable.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
try {
FutureTask<R> task = new FutureTask<R>(callable);
LauncherThread.post(task);
return task.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static BaseChildProcessConnection startInternalForTesting(final Context context,
final String[] commandLine, final FileDescriptorInfo[] filesToMap,
final ChildProcessCreationParams params) {
return runOnLauncherAndGetResult(new Callable<BaseChildProcessConnection>() {
@Override
public BaseChildProcessConnection call() {
return ChildProcessLauncher.startInternal(context, commandLine,
0 /* childProcessId */, filesToMap, null /* launchCallback */,
null /* childProcessCallback */, true /* inSandbox */,
false /* alwaysInForeground */, params);
}
});
}
@Override
public void onCreate() {
CommandLine.init(null);
......@@ -125,7 +71,8 @@ public class ChildProcessLauncherTestHelperService extends Service {
ChildProcessCreationParams params = new ChildProcessCreationParams(
getPackageName(), false, LibraryProcessType.PROCESS_CHILD, bindToCaller);
final BaseChildProcessConnection conn =
startInternalForTesting(this, commandLine, new FileDescriptorInfo[0], params);
ChildProcessLauncherTestUtils.startInternalForTesting(
this, commandLine, new FileDescriptorInfo[0], params);
// Poll the connection until it is set up. The main test in ChildProcessLauncherTest, which
// has bound the connection to this service, manages the timeout via the lifetime of this
......@@ -136,10 +83,11 @@ public class ChildProcessLauncherTestHelperService extends Service {
@Override
public void run() {
if (conn.getPid() != 0) {
int pid = ChildProcessLauncherTestUtils.getConnectionPid(conn);
if (pid != 0) {
try {
mReplyTo.send(Message.obtain(null, MSG_BIND_SERVICE_REPLY, conn.getPid(),
conn.getServiceNumber()));
mReplyTo.send(Message.obtain(null, MSG_BIND_SERVICE_REPLY, pid,
ChildProcessLauncherTestUtils.getConnectionServiceNumber(conn)));
} catch (RemoteException ex) {
throw new RuntimeException(ex);
}
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.content_shell_apk;
import android.content.Context;
import org.chromium.base.process_launcher.ChildProcessCreationParams;
import org.chromium.base.process_launcher.FileDescriptorInfo;
import org.chromium.base.process_launcher.IChildProcessService;
import org.chromium.content.browser.BaseChildProcessConnection;
import org.chromium.content.browser.ChildProcessLauncher;
import org.chromium.content.browser.LauncherThread;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.Semaphore;
/** An assortment of static methods used in tests that deal with launching child processes. */
public final class ChildProcessLauncherTestUtils {
// Do not instanciate, use static methods instead.
private ChildProcessLauncherTestUtils() {}
public static void runOnLauncherThreadBlocking(final Runnable runnable) {
if (LauncherThread.runningOnLauncherThread()) {
runnable.run();
return;
}
final Semaphore done = new Semaphore(0);
LauncherThread.post(new Runnable() {
@Override
public void run() {
runnable.run();
done.release();
}
});
done.acquireUninterruptibly();
}
public static <R> R runOnLauncherAndGetResult(Callable<R> callable) {
if (LauncherThread.runningOnLauncherThread()) {
try {
return callable.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
try {
FutureTask<R> task = new FutureTask<R>(callable);
LauncherThread.post(task);
return task.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static BaseChildProcessConnection startInternalForTesting(final Context context,
final String[] commandLine, final FileDescriptorInfo[] filesToMap,
final ChildProcessCreationParams params) {
return runOnLauncherAndGetResult(new Callable<BaseChildProcessConnection>() {
@Override
public BaseChildProcessConnection call() {
return ChildProcessLauncher.startInternal(context, commandLine,
0 /* childProcessId */, filesToMap, null /* launchCallback */,
null /* childProcessCallback */, true /* inSandbox */,
false /* alwaysInForeground */, params);
}
});
}
// Retrieves the PID of the passed in connection on the launcher thread as to not assert.
public static int getConnectionPid(final BaseChildProcessConnection connection) {
return runOnLauncherAndGetResult(new Callable<Integer>() {
@Override
public Integer call() {
return connection.getPid();
}
});
}
// Retrieves the service number of the passed in connection on the launcher thread as to not
// assert.
public static int getConnectionServiceNumber(final BaseChildProcessConnection connection) {
return runOnLauncherAndGetResult(new Callable<Integer>() {
@Override
public Integer call() {
return connection.getServiceNumber();
}
});
}
// Retrieves the service of the passed in connection on the launcher thread as to not assert.
public static IChildProcessService getConnectionService(
final BaseChildProcessConnection connection) {
return runOnLauncherAndGetResult(new Callable<IChildProcessService>() {
@Override
public IChildProcessService call() {
return connection.getService();
}
});
}
}
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