This source file includes following definitions.
- JNINamespace
- getContext
- onCreate
- onDestroy
- onBind
- SuppressWarnings
- establishSurfaceTexturePeer
- SuppressWarnings
- getViewSurface
- SuppressWarnings
- getSurfaceTextureSurface
- nativeInitChildProcess
- nativeExitChildProcess
- nativeShutdownMainThread
package org.chromium.content.app;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.Process;
import android.os.RemoteException;
import android.util.Log;
import android.view.Surface;
import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
import org.chromium.base.library_loader.LibraryLoader;
import org.chromium.base.library_loader.Linker;
import org.chromium.base.library_loader.ProcessInitException;
import org.chromium.content.browser.ChildProcessConnection;
import org.chromium.content.browser.ChildProcessLauncher;
import org.chromium.content.common.IChildProcessCallback;
import org.chromium.content.common.IChildProcessService;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicReference;
@JNINamespace("content")
public class ChildProcessService extends Service {
private static final String MAIN_THREAD_NAME = "ChildProcessMain";
private static final String TAG = "ChildProcessService";
private IChildProcessCallback mCallback;
private Thread mMainThread;
private String[] mCommandLineParams;
private int mCpuCount;
private long mCpuFeatures;
private ArrayList<Integer> mFileIds;
private ArrayList<ParcelFileDescriptor> mFileFds;
private ChromiumLinkerParams mLinkerParams;
private static AtomicReference<Context> sContext = new AtomicReference<Context>(null);
private boolean mLibraryInitialized = false;
private boolean mIsBound = false;
private final IChildProcessService.Stub mBinder = new IChildProcessService.Stub() {
@Override
public int setupConnection(Bundle args, IChildProcessCallback callback) {
mCallback = callback;
synchronized (mMainThread) {
if (mCommandLineParams == null) {
mCommandLineParams = args.getStringArray(
ChildProcessConnection.EXTRA_COMMAND_LINE);
}
assert mCommandLineParams != null;
mCpuCount = args.getInt(ChildProcessConnection.EXTRA_CPU_COUNT);
mCpuFeatures = args.getLong(ChildProcessConnection.EXTRA_CPU_FEATURES);
assert mCpuCount > 0;
mFileIds = new ArrayList<Integer>();
mFileFds = new ArrayList<ParcelFileDescriptor>();
for (int i = 0;; i++) {
String fdName = ChildProcessConnection.EXTRA_FILES_PREFIX + i
+ ChildProcessConnection.EXTRA_FILES_FD_SUFFIX;
ParcelFileDescriptor parcel = args.getParcelable(fdName);
if (parcel == null) {
break;
}
mFileFds.add(parcel);
String idName = ChildProcessConnection.EXTRA_FILES_PREFIX + i
+ ChildProcessConnection.EXTRA_FILES_ID_SUFFIX;
mFileIds.add(args.getInt(idName));
}
Bundle sharedRelros = args.getBundle(Linker.EXTRA_LINKER_SHARED_RELROS);
if (sharedRelros != null) {
Linker.useSharedRelros(sharedRelros);
sharedRelros = null;
}
mMainThread.notifyAll();
}
return Process.myPid();
}
};
static Context getContext() {
return sContext.get();
}
@Override
public void onCreate() {
Log.i(TAG, "Creating new ChildProcessService pid=" + Process.myPid());
if (sContext.get() != null) {
Log.e(TAG, "ChildProcessService created again in process!");
}
sContext.set(this);
super.onCreate();
mMainThread = new Thread(new Runnable() {
@Override
public void run() {
try {
boolean useLinker = Linker.isUsed();
if (useLinker) {
synchronized (mMainThread) {
while (!mIsBound) {
mMainThread.wait();
}
}
if (mLinkerParams != null) {
if (mLinkerParams.mWaitForSharedRelro)
Linker.initServiceProcess(mLinkerParams.mBaseLoadAddress);
else
Linker.disableSharedRelros();
Linker.setTestRunnerClassName(mLinkerParams.mTestRunnerClassName);
}
}
try {
LibraryLoader.loadNow(getApplicationContext(), false);
} catch (ProcessInitException e) {
Log.e(TAG, "Failed to load native library, exiting child process", e);
System.exit(-1);
}
synchronized (mMainThread) {
while (mCommandLineParams == null) {
mMainThread.wait();
}
}
LibraryLoader.initialize(mCommandLineParams);
synchronized (mMainThread) {
mLibraryInitialized = true;
mMainThread.notifyAll();
while (mFileIds == null) {
mMainThread.wait();
}
}
assert mFileIds.size() == mFileFds.size();
int[] fileIds = new int[mFileIds.size()];
int[] fileFds = new int[mFileFds.size()];
for (int i = 0; i < mFileIds.size(); ++i) {
fileIds[i] = mFileIds.get(i);
fileFds[i] = mFileFds.get(i).detachFd();
}
ContentMain.initApplicationContext(sContext.get().getApplicationContext());
nativeInitChildProcess(sContext.get().getApplicationContext(),
ChildProcessService.this, fileIds, fileFds,
mCpuCount, mCpuFeatures);
ContentMain.start();
nativeExitChildProcess();
} catch (InterruptedException e) {
Log.w(TAG, MAIN_THREAD_NAME + " startup failed: " + e);
} catch (ProcessInitException e) {
Log.w(TAG, MAIN_THREAD_NAME + " startup failed: " + e);
}
}
}, MAIN_THREAD_NAME);
mMainThread.start();
}
@Override
public void onDestroy() {
Log.i(TAG, "Destroying ChildProcessService pid=" + Process.myPid());
super.onDestroy();
if (mCommandLineParams == null) {
return;
}
synchronized (mMainThread) {
try {
while (!mLibraryInitialized) {
mMainThread.wait();
}
} catch (InterruptedException e) {
}
}
nativeShutdownMainThread();
}
@Override
public IBinder onBind(Intent intent) {
stopSelf();
synchronized (mMainThread) {
mCommandLineParams = intent.getStringArrayExtra(
ChildProcessConnection.EXTRA_COMMAND_LINE);
mLinkerParams = null;
if (Linker.isUsed())
mLinkerParams = new ChromiumLinkerParams(intent);
mIsBound = true;
mMainThread.notifyAll();
}
return mBinder;
}
@SuppressWarnings("unused")
@CalledByNative
private void establishSurfaceTexturePeer(
int pid, Object surfaceObject, int primaryID, int secondaryID) {
if (mCallback == null) {
Log.e(TAG, "No callback interface has been provided.");
return;
}
Surface surface = null;
boolean needRelease = false;
if (surfaceObject instanceof Surface) {
surface = (Surface) surfaceObject;
} else if (surfaceObject instanceof SurfaceTexture) {
surface = new Surface((SurfaceTexture) surfaceObject);
needRelease = true;
} else {
Log.e(TAG, "Not a valid surfaceObject: " + surfaceObject);
return;
}
try {
mCallback.establishSurfacePeer(pid, surface, primaryID, secondaryID);
} catch (RemoteException e) {
Log.e(TAG, "Unable to call establishSurfaceTexturePeer: " + e);
return;
} finally {
if (needRelease) {
surface.release();
}
}
}
@SuppressWarnings("unused")
@CalledByNative
private Surface getViewSurface(int surfaceId) {
if (mCallback == null) {
Log.e(TAG, "No callback interface has been provided.");
return null;
}
try {
return mCallback.getViewSurface(surfaceId);
} catch (RemoteException e) {
Log.e(TAG, "Unable to call establishSurfaceTexturePeer: " + e);
return null;
}
}
@SuppressWarnings("unused")
@CalledByNative
private Surface getSurfaceTextureSurface(int primaryId, int secondaryId) {
if (mCallback == null) {
Log.e(TAG, "No callback interface has been provided.");
return null;
}
try {
return mCallback.getSurfaceTextureSurface(primaryId, secondaryId);
} catch (RemoteException e) {
Log.e(TAG, "Unable to call getSurfaceTextureSurface: " + e);
return null;
}
}
private static native void nativeInitChildProcess(Context applicationContext,
ChildProcessService service, int[] extraFileIds, int[] extraFileFds,
int cpuCount, long cpuFeatures);
private static native void nativeExitChildProcess();
private native void nativeShutdownMainThread();
}