| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.os.ParcelFileDescriptor; | 8 import android.os.ParcelFileDescriptor; |
| 9 | 9 |
| 10 import org.chromium.base.ContextUtils; | 10 import org.chromium.base.ContextUtils; |
| 11 import org.chromium.base.Log; | 11 import org.chromium.base.Log; |
| 12 import org.chromium.base.annotations.CalledByNative; | 12 import org.chromium.base.annotations.CalledByNative; |
| 13 import org.chromium.base.annotations.JNINamespace; | 13 import org.chromium.base.annotations.JNINamespace; |
| 14 import org.chromium.base.process_launcher.ChildProcessCreationParams; | 14 import org.chromium.base.process_launcher.ChildProcessCreationParams; |
| 15 import org.chromium.base.process_launcher.FileDescriptorInfo; | 15 import org.chromium.base.process_launcher.FileDescriptorInfo; |
| 16 | 16 |
| 17 import java.io.IOException; | 17 import java.io.IOException; |
| 18 | 18 |
| 19 /** | 19 /** |
| 20 * This is the java counterpart to ChildProcessLauncherHelper. It is owned by na
tive side and | 20 * This is the java counterpart to ChildProcessLauncherHelper. It is owned by na
tive side and |
| 21 * has an explicit destroy method. | 21 * has an explicit destroy method. |
| 22 * Each public or jni methods should have explicit documentation on what threads
they are called. | 22 * Each public or jni methods should have explicit documentation on what threads
they are called. |
| 23 */ | 23 */ |
| 24 @JNINamespace("content::internal") | 24 @JNINamespace("content::internal") |
| 25 class ChildProcessLauncherHelper { | 25 class ChildProcessLauncherHelper { |
| 26 private static final String TAG = "ChildProcLH"; | 26 private static final String TAG = "ChildProcLH"; |
| 27 | 27 |
| 28 // Represents an invalid process handle; same as base/process/process.h kNul
lProcessHandle. |
| 29 private static final int NULL_PROCESS_HANDLE = 0; |
| 30 |
| 28 // Note native pointer is only guaranteed live until nativeOnChildProcessSta
rted. | 31 // Note native pointer is only guaranteed live until nativeOnChildProcessSta
rted. |
| 29 private long mNativeChildProcessLauncherHelper; | 32 private long mNativeChildProcessLauncherHelper; |
| 30 private int mPid; | 33 private BaseChildProcessConnection mChildProcessConnection; |
| 31 | 34 |
| 32 @CalledByNative | 35 @CalledByNative |
| 33 private static FileDescriptorInfo makeFdInfo( | 36 private static FileDescriptorInfo makeFdInfo( |
| 34 int id, int fd, boolean autoClose, long offset, long size) { | 37 int id, int fd, boolean autoClose, long offset, long size) { |
| 35 assert LauncherThread.runningOnLauncherThread(); | 38 assert LauncherThread.runningOnLauncherThread(); |
| 36 ParcelFileDescriptor pFd; | 39 ParcelFileDescriptor pFd; |
| 37 if (autoClose) { | 40 if (autoClose) { |
| 38 // Adopt the FD, it will be closed when we close the ParcelFileDescr
iptor. | 41 // Adopt the FD, it will be closed when we close the ParcelFileDescr
iptor. |
| 39 pFd = ParcelFileDescriptor.adoptFd(fd); | 42 pFd = ParcelFileDescriptor.adoptFd(fd); |
| 40 } else { | 43 } else { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 57 } | 60 } |
| 58 | 61 |
| 59 private ChildProcessLauncherHelper(long nativePointer, int paramId, final St
ring[] commandLine, | 62 private ChildProcessLauncherHelper(long nativePointer, int paramId, final St
ring[] commandLine, |
| 60 int childProcessId, FileDescriptorInfo[] filesToBeMapped) { | 63 int childProcessId, FileDescriptorInfo[] filesToBeMapped) { |
| 61 assert LauncherThread.runningOnLauncherThread(); | 64 assert LauncherThread.runningOnLauncherThread(); |
| 62 mNativeChildProcessLauncherHelper = nativePointer; | 65 mNativeChildProcessLauncherHelper = nativePointer; |
| 63 | 66 |
| 64 ChildProcessLauncher.start(ContextUtils.getApplicationContext(), paramId
, commandLine, | 67 ChildProcessLauncher.start(ContextUtils.getApplicationContext(), paramId
, commandLine, |
| 65 childProcessId, filesToBeMapped, new ChildProcessLauncher.Launch
Callback() { | 68 childProcessId, filesToBeMapped, new ChildProcessLauncher.Launch
Callback() { |
| 66 @Override | 69 @Override |
| 67 public void onChildProcessStarted(int pid) { | 70 public void onChildProcessStarted(BaseChildProcessConnection
connection) { |
| 68 mPid = pid; | 71 mChildProcessConnection = connection; |
| 69 if (mNativeChildProcessLauncherHelper != 0) { | 72 if (mNativeChildProcessLauncherHelper != 0) { |
| 70 nativeOnChildProcessStarted(mNativeChildProcessLaunc
herHelper, pid); | 73 nativeOnChildProcessStarted( |
| 74 mNativeChildProcessLauncherHelper, getPid())
; |
| 71 } | 75 } |
| 72 mNativeChildProcessLauncherHelper = 0; | 76 mNativeChildProcessLauncherHelper = 0; |
| 73 } | 77 } |
| 74 }); | 78 }); |
| 75 } | 79 } |
| 76 | 80 |
| 81 private int getPid() { |
| 82 return mChildProcessConnection == null ? NULL_PROCESS_HANDLE |
| 83 : mChildProcessConnection.getPid(
); |
| 84 } |
| 85 |
| 77 // Called on client (UI or IO) thread. | 86 // Called on client (UI or IO) thread. |
| 78 @CalledByNative | 87 @CalledByNative |
| 79 private boolean isOomProtected() { | 88 private boolean isOomProtected() { |
| 80 return ChildProcessLauncher.getBindingManager().isOomProtected(mPid); | 89 // mChildProcessConnection is set on a different thread but does not cha
nge once it's been |
| 90 // set. So it is safe to test whether it's null from a different thread. |
| 91 if (mChildProcessConnection == null) { |
| 92 return false; |
| 93 } |
| 94 |
| 95 if (mChildProcessConnection instanceof ImportantChildProcessConnection)
{ |
| 96 // The connection was bound as BIND_IMPORTANT. This should prevent i
t from being killed |
| 97 // when the app is on the foreground (that's our best guess, but the
re is no absolute |
| 98 // guarantee). |
| 99 return ChildProcessLauncher.isApplicationInForeground(); |
| 100 } |
| 101 |
| 102 return ((ManagedChildProcessConnection) mChildProcessConnection) |
| 103 .isOomProtectedOrWasWhenDied(); |
| 81 } | 104 } |
| 82 | 105 |
| 83 @CalledByNative | 106 @CalledByNative |
| 84 private void setInForeground(int pid, boolean inForeground) { | 107 private void setInForeground(int pid, boolean inForeground) { |
| 85 assert LauncherThread.runningOnLauncherThread(); | 108 assert LauncherThread.runningOnLauncherThread(); |
| 86 assert mPid == pid; | 109 assert getPid() == pid; |
| 87 ChildProcessLauncher.getBindingManager().setInForeground(mPid, inForegro
und); | 110 ChildProcessLauncher.getBindingManager().setInForeground(pid, inForegrou
nd); |
| 88 } | 111 } |
| 89 | 112 |
| 90 @CalledByNative | 113 @CalledByNative |
| 91 private static void stop(int pid) { | 114 private static void stop(int pid) { |
| 92 assert LauncherThread.runningOnLauncherThread(); | 115 assert LauncherThread.runningOnLauncherThread(); |
| 93 ChildProcessLauncher.stop(pid); | 116 ChildProcessLauncher.stop(pid); |
| 94 } | 117 } |
| 95 | 118 |
| 96 // Called on UI thread. | 119 // Called on UI thread. |
| 97 @CalledByNative | 120 @CalledByNative |
| 98 private static int getNumberOfRendererSlots() { | 121 private static int getNumberOfRendererSlots() { |
| 99 final ChildProcessCreationParams params = ChildProcessCreationParams.get
Default(); | 122 final ChildProcessCreationParams params = ChildProcessCreationParams.get
Default(); |
| 100 final Context context = ContextUtils.getApplicationContext(); | 123 final Context context = ContextUtils.getApplicationContext(); |
| 101 final boolean inSandbox = true; | 124 final boolean inSandbox = true; |
| 102 final String packageName = | 125 final String packageName = |
| 103 params == null ? context.getPackageName() : params.getPackageNam
e(); | 126 params == null ? context.getPackageName() : params.getPackageNam
e(); |
| 104 try { | 127 try { |
| 105 return ChildConnectionAllocator.getNumberOfServices(context, inSandb
ox, packageName); | 128 return ChildConnectionAllocator.getNumberOfServices(context, inSandb
ox, packageName); |
| 106 } catch (RuntimeException e) { | 129 } catch (RuntimeException e) { |
| 107 // Unittest packages do not declare services. Some tests require a r
ealistic number | 130 // Unittest packages do not declare services. Some tests require a r
ealistic number |
| 108 // to test child process policies, so pick a high-ish number here. | 131 // to test child process policies, so pick a high-ish number here. |
| 109 return 65535; | 132 return 65535; |
| 110 } | 133 } |
| 111 } | 134 } |
| 112 | 135 |
| 113 // Can be called on a number of threads, including launcher, and binder. | 136 // Can be called on a number of threads, including launcher, and binder. |
| 114 private static native void nativeOnChildProcessStarted( | 137 private static native void nativeOnChildProcessStarted( |
| 115 long nativeChildProcessLauncherHelper, int pid); | 138 long nativeChildProcessLauncherHelper, int pid); |
| 116 } | 139 } |
| OLD | NEW |