Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: content/public/android/java/src/org/chromium/content/browser/ChildProcessLauncherHelper.java

Issue 2828793002: Refactoring ChildProcessConnection. (Closed)
Patch Set: Refactoring ChildProcessConnection. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 17 matching lines...) Expand all
58 } 61 }
59 62
60 private ChildProcessLauncherHelper(long nativePointer, Context context, int paramId, 63 private ChildProcessLauncherHelper(long nativePointer, Context context, int paramId,
61 final String[] commandLine, int childProcessId, FileDescriptorInfo[] filesToBeMapped) { 64 final String[] commandLine, int childProcessId, FileDescriptorInfo[] filesToBeMapped) {
62 assert LauncherThread.runningOnLauncherThread(); 65 assert LauncherThread.runningOnLauncherThread();
63 mNativeChildProcessLauncherHelper = nativePointer; 66 mNativeChildProcessLauncherHelper = nativePointer;
64 67
65 ChildProcessLauncher.start(context, paramId, commandLine, childProcessId , filesToBeMapped, 68 ChildProcessLauncher.start(context, paramId, commandLine, childProcessId , filesToBeMapped,
66 new ChildProcessLauncher.LaunchCallback() { 69 new ChildProcessLauncher.LaunchCallback() {
67 @Override 70 @Override
68 public void onChildProcessStarted(int pid) { 71 public void onChildProcessStarted(BaseChildProcessConnection connection) {
69 mPid = pid; 72 mChildProcessConnection = connection;
70 if (mNativeChildProcessLauncherHelper != 0) { 73 if (mNativeChildProcessLauncherHelper != 0) {
71 nativeOnChildProcessStarted(mNativeChildProcessLaunc herHelper, pid); 74 nativeOnChildProcessStarted(
75 mNativeChildProcessLauncherHelper, getPid()) ;
72 } 76 }
73 mNativeChildProcessLauncherHelper = 0; 77 mNativeChildProcessLauncherHelper = 0;
74 } 78 }
75 }); 79 });
76 } 80 }
77 81
82 private int getPid() {
83 return mChildProcessConnection == null ? NULL_PROCESS_HANDLE
84 : mChildProcessConnection.getPid( );
85 }
78 // Called on client (UI or IO) thread. 86 // Called on client (UI or IO) thread.
79 @CalledByNative 87 @CalledByNative
80 private boolean isOomProtected() { 88 private boolean isOomProtected() {
81 return ChildProcessLauncher.getBindingManager().isOomProtected(mPid); 89 if (mChildProcessConnection == null) {
boliu 2017/04/20 22:33:34 hmm, this is "kind of" thread safe, only because m
Jay Civelli 2017/04/25 06:02:46 As you mentioned before, reference assignation are
90 return false;
91 }
92
93 if (mChildProcessConnection instanceof ImportantChildProcessConnection) {
94 // The connection was bound as BIND_IMPORTANT. This should prevent i t from being killed
95 // when the app is on the foreground (that's our best guess, but the re is no absolute
96 // guarantee).
97 return ChildProcessLauncher.isApplicationInForeground();
98 }
99
100 return ((ManagedChildProcessConnection) mChildProcessConnection)
101 .isOomProtectedOrWasWhenDied();
82 } 102 }
83 103
84 @CalledByNative 104 @CalledByNative
85 private void setInForeground(int pid, boolean inForeground) { 105 private void setInForeground(int pid, boolean inForeground) {
86 assert LauncherThread.runningOnLauncherThread(); 106 assert LauncherThread.runningOnLauncherThread();
87 assert mPid == pid; 107 assert getPid() == pid;
88 ChildProcessLauncher.getBindingManager().setInForeground(mPid, inForegro und); 108 ChildProcessLauncher.getBindingManager().setInForeground(pid, inForegrou nd);
89 } 109 }
90 110
91 @CalledByNative 111 @CalledByNative
92 private static void stop(int pid) { 112 private static void stop(int pid) {
93 assert LauncherThread.runningOnLauncherThread(); 113 assert LauncherThread.runningOnLauncherThread();
94 ChildProcessLauncher.stop(pid); 114 ChildProcessLauncher.stop(pid);
95 } 115 }
96 116
97 // Called on UI thread. 117 // Called on UI thread.
98 @CalledByNative 118 @CalledByNative
99 private static int getNumberOfRendererSlots() { 119 private static int getNumberOfRendererSlots() {
100 final ChildProcessCreationParams params = ChildProcessCreationParams.get Default(); 120 final ChildProcessCreationParams params = ChildProcessCreationParams.get Default();
101 final Context context = ContextUtils.getApplicationContext(); 121 final Context context = ContextUtils.getApplicationContext();
102 final boolean inSandbox = true; 122 final boolean inSandbox = true;
103 final String packageName = 123 final String packageName =
104 params == null ? context.getPackageName() : params.getPackageNam e(); 124 params == null ? context.getPackageName() : params.getPackageNam e();
105 try { 125 try {
106 return ChildConnectionAllocator.getNumberOfServices(context, inSandb ox, packageName); 126 return ChildConnectionAllocator.getNumberOfServices(context, inSandb ox, packageName);
107 } catch (RuntimeException e) { 127 } catch (RuntimeException e) {
108 // Unittest packages do not declare services. Some tests require a r ealistic number 128 // Unittest packages do not declare services. Some tests require a r ealistic number
109 // to test child process policies, so pick a high-ish number here. 129 // to test child process policies, so pick a high-ish number here.
110 return 65535; 130 return 65535;
111 } 131 }
112 } 132 }
113 133
114 // Can be called on a number of threads, including launcher, and binder. 134 // Can be called on a number of threads, including launcher, and binder.
115 private static native void nativeOnChildProcessStarted( 135 private static native void nativeOnChildProcessStarted(
116 long nativeChildProcessLauncherHelper, int pid); 136 long nativeChildProcessLauncherHelper, int pid);
117 } 137 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698