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

Side by Side Diff: content/browser/child_process_launcher_helper_android.cc

Issue 2772793003: android: ChildProcessLauncherAndroid follow up cleanups (Closed)
Patch Set: Created 3 years, 9 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
« no previous file with comments | « no previous file | content/public/android/java/src/org/chromium/content/browser/ChildProcessLauncher.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "content/browser/child_process_launcher_helper_android.h" 5 #include "content/browser/child_process_launcher_helper_android.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/android/apk_assets.h" 9 #include "base/android/apk_assets.h"
10 #include "base/android/context_utils.h" 10 #include "base/android/context_utils.h"
(...skipping 20 matching lines...) Expand all
31 31
32 namespace content { 32 namespace content {
33 33
34 typedef base::Callback<void(base::ProcessHandle, int /* launch result */)> 34 typedef base::Callback<void(base::ProcessHandle, int /* launch result */)>
35 StartChildProcessCallback; 35 StartChildProcessCallback;
36 36
37 namespace internal { 37 namespace internal {
38 38
39 namespace { 39 namespace {
40 40
41 // Starts a process as a child process spawned by the Android ActivityManager.
42 // The created process handle is returned to the |callback| on success, 0 is
43 // returned if the process could not be created.
44 void StartChildProcess(const base::CommandLine::StringVector& argv,
45 int child_process_id,
46 content::FileDescriptorInfo* files_to_register,
47 const StartChildProcessCallback& callback) {
48 JNIEnv* env = AttachCurrentThread();
49 DCHECK(env);
50
51 // Create the Command line String[]
52 ScopedJavaLocalRef<jobjectArray> j_argv = ToJavaArrayOfStrings(env, argv);
53
54 size_t file_count = files_to_register->GetMappingSize();
55 DCHECK(file_count > 0);
56
57 ScopedJavaLocalRef<jclass> j_file_info_class = base::android::GetClass(
58 env, "org/chromium/base/process_launcher/FileDescriptorInfo");
59 ScopedJavaLocalRef<jobjectArray> j_file_infos(
60 env, env->NewObjectArray(file_count, j_file_info_class.obj(), NULL));
61 base::android::CheckException(env);
62
63 for (size_t i = 0; i < file_count; ++i) {
64 int fd = files_to_register->GetFDAt(i);
65 PCHECK(0 <= fd);
66 int id = files_to_register->GetIDAt(i);
67 const auto& region = files_to_register->GetRegionAt(i);
68 bool auto_close = files_to_register->OwnsFD(fd);
69 ScopedJavaLocalRef<jobject> j_file_info =
70 Java_ChildProcessLauncher_makeFdInfo(env, id, fd, auto_close,
71 region.offset, region.size);
72 PCHECK(j_file_info.obj());
73 env->SetObjectArrayElement(j_file_infos.obj(), i, j_file_info.obj());
74 if (auto_close) {
75 ignore_result(files_to_register->ReleaseFD(fd).release());
76 }
77 }
78
79 constexpr int param_key = 0; // TODO(boliu): Use this.
80 Java_ChildProcessLauncher_start(
81 env, base::android::GetApplicationContext(), param_key, j_argv,
82 child_process_id, j_file_infos,
83 reinterpret_cast<intptr_t>(new StartChildProcessCallback(callback)));
84 }
85
86 // Stops a child process based on the handle returned from StartChildProcess. 41 // Stops a child process based on the handle returned from StartChildProcess.
87 void StopChildProcess(base::ProcessHandle handle) { 42 void StopChildProcess(base::ProcessHandle handle) {
88 JNIEnv* env = AttachCurrentThread(); 43 JNIEnv* env = AttachCurrentThread();
89 DCHECK(env); 44 DCHECK(env);
90 Java_ChildProcessLauncher_stop(env, static_cast<jint>(handle)); 45 Java_ChildProcessLauncher_stop(env, static_cast<jint>(handle));
91 } 46 }
92 47
93 bool IsChildProcessOomProtected(base::ProcessHandle handle) {
94 JNIEnv* env = AttachCurrentThread();
95 DCHECK(env);
96 return Java_ChildProcessLauncher_isOomProtected(env,
97 static_cast<jint>(handle));
98 }
99
100 void SetChildProcessInForeground(base::ProcessHandle handle,
101 bool in_foreground) {
102 JNIEnv* env = AttachCurrentThread();
103 DCHECK(env);
104 return Java_ChildProcessLauncher_setInForeground(
105 env, static_cast<jint>(handle), static_cast<jboolean>(in_foreground));
106 }
107
108 // Callback invoked from Java once the process has been started. 48 // Callback invoked from Java once the process has been started.
109 void ChildProcessStartedCallback( 49 void ChildProcessStartedCallback(
110 ChildProcessLauncherHelper* helper, 50 ChildProcessLauncherHelper* helper,
111 base::ProcessHandle handle, 51 base::ProcessHandle handle,
112 int launch_result) { 52 int launch_result) {
113 // TODO(jcivelli): Remove this by defining better what happens on what thread 53 // TODO(jcivelli): Remove this by defining better what happens on what thread
114 // in the corresponding Java code. 54 // in the corresponding Java code.
115 ChildProcessLauncherHelper::Process process; 55 ChildProcessLauncherHelper::Process process;
116 process.process = base::Process(handle); 56 process.process = base::Process(handle);
117 if (BrowserThread::CurrentlyOn(BrowserThread::PROCESS_LAUNCHER)) { 57 if (BrowserThread::CurrentlyOn(BrowserThread::PROCESS_LAUNCHER)) {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 } 129 }
190 130
191 ChildProcessLauncherHelper::Process 131 ChildProcessLauncherHelper::Process
192 ChildProcessLauncherHelper::LaunchProcessOnLauncherThread( 132 ChildProcessLauncherHelper::LaunchProcessOnLauncherThread(
193 const base::LaunchOptions& options, 133 const base::LaunchOptions& options,
194 std::unique_ptr<FileDescriptorInfo> files_to_register, 134 std::unique_ptr<FileDescriptorInfo> files_to_register,
195 bool* is_synchronous_launch, 135 bool* is_synchronous_launch,
196 int* launch_result) { 136 int* launch_result) {
197 *is_synchronous_launch = false; 137 *is_synchronous_launch = false;
198 138
199 StartChildProcess(command_line()->argv(), 139 JNIEnv* env = AttachCurrentThread();
200 child_process_id(), 140 DCHECK(env);
201 files_to_register.get(), 141
202 base::Bind(&ChildProcessStartedCallback, 142 // Create the Command line String[]
203 RetainedRef(this))); 143 ScopedJavaLocalRef<jobjectArray> j_argv =
144 ToJavaArrayOfStrings(env, command_line()->argv());
145
146 size_t file_count = files_to_register->GetMappingSize();
147 DCHECK(file_count > 0);
148
149 ScopedJavaLocalRef<jclass> j_file_info_class = base::android::GetClass(
150 env, "org/chromium/base/process_launcher/FileDescriptorInfo");
151 ScopedJavaLocalRef<jobjectArray> j_file_infos(
152 env, env->NewObjectArray(file_count, j_file_info_class.obj(), NULL));
153 base::android::CheckException(env);
154
155 for (size_t i = 0; i < file_count; ++i) {
156 int fd = files_to_register->GetFDAt(i);
157 PCHECK(0 <= fd);
158 int id = files_to_register->GetIDAt(i);
159 const auto& region = files_to_register->GetRegionAt(i);
160 bool auto_close = files_to_register->OwnsFD(fd);
161 ScopedJavaLocalRef<jobject> j_file_info =
162 Java_ChildProcessLauncher_makeFdInfo(env, id, fd, auto_close,
163 region.offset, region.size);
164 PCHECK(j_file_info.obj());
165 env->SetObjectArrayElement(j_file_infos.obj(), i, j_file_info.obj());
166 if (auto_close) {
167 ignore_result(files_to_register->ReleaseFD(fd).release());
168 }
169 }
170
171 constexpr int param_key = 0; // TODO(boliu): Use this.
172 Java_ChildProcessLauncher_start(
173 env, base::android::GetApplicationContext(), param_key, j_argv,
174 child_process_id(), j_file_infos,
175 reinterpret_cast<intptr_t>(new StartChildProcessCallback(
176 base::Bind(&ChildProcessStartedCallback, RetainedRef(this)))));
204 177
205 return Process(); 178 return Process();
206 } 179 }
207 180
208 void ChildProcessLauncherHelper::AfterLaunchOnLauncherThread( 181 void ChildProcessLauncherHelper::AfterLaunchOnLauncherThread(
209 const ChildProcessLauncherHelper::Process& process, 182 const ChildProcessLauncherHelper::Process& process,
210 const base::LaunchOptions& options) { 183 const base::LaunchOptions& options) {
211 } 184 }
212 185
213 // static 186 // static
214 base::TerminationStatus ChildProcessLauncherHelper::GetTerminationStatus( 187 base::TerminationStatus ChildProcessLauncherHelper::GetTerminationStatus(
215 const ChildProcessLauncherHelper::Process& process, 188 const ChildProcessLauncherHelper::Process& process,
216 bool known_dead, 189 bool known_dead,
217 int* exit_code) { 190 int* exit_code) {
218 if (IsChildProcessOomProtected(process.process.Handle())) 191 if (Java_ChildProcessLauncher_isOomProtected(
192 AttachCurrentThread(), static_cast<jint>(process.process.Handle()))) {
219 return base::TERMINATION_STATUS_OOM_PROTECTED; 193 return base::TERMINATION_STATUS_OOM_PROTECTED;
194 }
220 return base::GetTerminationStatus(process.process.Handle(), exit_code); 195 return base::GetTerminationStatus(process.process.Handle(), exit_code);
221 } 196 }
222 197
223 // static 198 // static
224 bool ChildProcessLauncherHelper::TerminateProcess( 199 bool ChildProcessLauncherHelper::TerminateProcess(
225 const base::Process& process, int exit_code, bool wait) { 200 const base::Process& process, int exit_code, bool wait) {
226 StopChildProcess(process.Handle()); 201 StopChildProcess(process.Handle());
227 return true; 202 return true;
228 } 203 }
229 204
230 // static 205 // static
231 void ChildProcessLauncherHelper::ForceNormalProcessTerminationSync( 206 void ChildProcessLauncherHelper::ForceNormalProcessTerminationSync(
232 ChildProcessLauncherHelper::Process process) { 207 ChildProcessLauncherHelper::Process process) {
233 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER); 208 DCHECK_CURRENTLY_ON(BrowserThread::PROCESS_LAUNCHER);
234 VLOG(1) << "ChromeProcess: Stopping process with handle " 209 VLOG(1) << "ChromeProcess: Stopping process with handle "
235 << process.process.Handle(); 210 << process.process.Handle();
236 StopChildProcess(process.process.Handle()); 211 StopChildProcess(process.process.Handle());
237 } 212 }
238 213
239 // static 214 // static
240 void ChildProcessLauncherHelper::SetProcessBackgroundedOnLauncherThread( 215 void ChildProcessLauncherHelper::SetProcessBackgroundedOnLauncherThread(
241 base::Process process, bool background) { 216 base::Process process,
242 SetChildProcessInForeground(process.Handle(), !background); 217 bool background) {
218 JNIEnv* env = AttachCurrentThread();
219 DCHECK(env);
220 return Java_ChildProcessLauncher_setInForeground(
221 env, static_cast<jint>(process.Handle()), !background);
243 } 222 }
244 223
245 // static 224 // static
246 void ChildProcessLauncherHelper::SetRegisteredFilesForService( 225 void ChildProcessLauncherHelper::SetRegisteredFilesForService(
247 const std::string& service_name, 226 const std::string& service_name,
248 catalog::RequiredFileMap required_files) { 227 catalog::RequiredFileMap required_files) {
249 SetFilesToShareForServicePosix(service_name, std::move(required_files)); 228 SetFilesToShareForServicePosix(service_name, std::move(required_files));
250 } 229 }
251 230
252 // static 231 // static
253 void ChildProcessLauncherHelper::ResetRegisteredFilesForTesting() { 232 void ChildProcessLauncherHelper::ResetRegisteredFilesForTesting() {
254 ResetFilesToShareForTestingPosix(); 233 ResetFilesToShareForTestingPosix();
255 } 234 }
256 235
257 // static 236 // static
258 base::File OpenFileToShare(const base::FilePath& path, 237 base::File OpenFileToShare(const base::FilePath& path,
259 base::MemoryMappedFile::Region* region) { 238 base::MemoryMappedFile::Region* region) {
260 return base::File(base::android::OpenApkAsset(path.value(), region)); 239 return base::File(base::android::OpenApkAsset(path.value(), region));
261 } 240 }
262 241
263 } // namespace internal
264
265 // Called from ChildProcessLauncher.java when the ChildProcess was 242 // Called from ChildProcessLauncher.java when the ChildProcess was
266 // started. 243 // started.
267 // |client_context| is the pointer to StartChildProcessCallback which was 244 // |client_context| is the pointer to StartChildProcessCallback which was
268 // passed in from StartChildProcess. 245 // passed in from StartChildProcess.
269 // |handle| is the processID of the child process as originated in Java, 0 if 246 // |handle| is the processID of the child process as originated in Java, 0 if
270 // the ChildProcess could not be created. 247 // the ChildProcess could not be created.
271 static void OnChildProcessStarted(JNIEnv*, 248 static void OnChildProcessStarted(JNIEnv*,
272 const JavaParamRef<jclass>&, 249 const JavaParamRef<jclass>&,
273 jlong client_context, 250 jlong client_context,
274 jint handle) { 251 jint handle) {
275 StartChildProcessCallback* callback = 252 StartChildProcessCallback* callback =
276 reinterpret_cast<StartChildProcessCallback*>(client_context); 253 reinterpret_cast<StartChildProcessCallback*>(client_context);
277 int launch_result = (handle == base::kNullProcessHandle) 254 int launch_result = (handle == base::kNullProcessHandle)
278 ? LAUNCH_RESULT_FAILURE 255 ? LAUNCH_RESULT_FAILURE
279 : LAUNCH_RESULT_SUCCESS; 256 : LAUNCH_RESULT_SUCCESS;
280 callback->Run(static_cast<base::ProcessHandle>(handle), launch_result); 257 callback->Run(static_cast<base::ProcessHandle>(handle), launch_result);
281 delete callback; 258 delete callback;
282 } 259 }
283 260
261 } // namespace internal
262
284 bool RegisterChildProcessLauncher(JNIEnv* env) { 263 bool RegisterChildProcessLauncher(JNIEnv* env) {
285 return RegisterNativesImpl(env); 264 return internal::RegisterNativesImpl(env);
286 } 265 }
287 266
288 } // namespace content 267 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/public/android/java/src/org/chromium/content/browser/ChildProcessLauncher.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698