| 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 #include "content/browser/child_process_launcher_helper_android.h" | 5 #include "content/browser/child_process_launcher_helper.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" | |
| 11 #include "base/android/jni_array.h" | |
| 12 #include "base/android/unguessable_token_android.h" | |
| 13 #include "base/i18n/icu_util.h" | 10 #include "base/i18n/icu_util.h" |
| 14 #include "base/logging.h" | 11 #include "base/logging.h" |
| 15 #include "base/metrics/field_trial.h" | 12 #include "base/metrics/field_trial.h" |
| 16 #include "content/browser/android/scoped_surface_request_manager.h" | 13 #include "content/browser/android/child_process_launcher_android.h" |
| 17 #include "content/browser/child_process_launcher_helper.h" | |
| 18 #include "content/browser/child_process_launcher_helper_posix.h" | 14 #include "content/browser/child_process_launcher_helper_posix.h" |
| 19 #include "content/browser/file_descriptor_info_impl.h" | 15 #include "content/browser/file_descriptor_info_impl.h" |
| 20 #include "content/browser/web_contents/web_contents_impl.h" | 16 #include "content/browser/web_contents/web_contents_impl.h" |
| 21 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 22 #include "content/public/browser/render_process_host.h" | 18 #include "content/public/browser/render_process_host.h" |
| 23 #include "content/public/common/content_descriptors.h" | 19 #include "content/public/common/content_descriptors.h" |
| 24 #include "content/public/common/content_switches.h" | 20 #include "content/public/common/content_switches.h" |
| 25 #include "gin/v8_initializer.h" | 21 #include "gin/v8_initializer.h" |
| 26 #include "gpu/ipc/common/gpu_surface_tracker.h" | |
| 27 #include "jni/ChildProcessLauncher_jni.h" | |
| 28 | |
| 29 using base::android::AttachCurrentThread; | |
| 30 using base::android::JavaParamRef; | |
| 31 using base::android::ScopedJavaGlobalRef; | |
| 32 using base::android::ScopedJavaLocalRef; | |
| 33 using base::android::ToJavaArrayOfStrings; | |
| 34 | 22 |
| 35 namespace content { | 23 namespace content { |
| 36 | |
| 37 typedef base::Callback<void(base::ProcessHandle, int /* launch result */)> | |
| 38 StartChildProcessCallback; | |
| 39 | |
| 40 namespace internal { | 24 namespace internal { |
| 41 | 25 |
| 42 namespace { | 26 namespace { |
| 43 | 27 |
| 44 // Starts a process as a child process spawned by the Android ActivityManager. | |
| 45 // The created process handle is returned to the |callback| on success, 0 is | |
| 46 // returned if the process could not be created. | |
| 47 void StartChildProcess(const base::CommandLine::StringVector& argv, | |
| 48 int child_process_id, | |
| 49 content::FileDescriptorInfo* files_to_register, | |
| 50 const StartChildProcessCallback& callback) { | |
| 51 JNIEnv* env = AttachCurrentThread(); | |
| 52 DCHECK(env); | |
| 53 | |
| 54 // Create the Command line String[] | |
| 55 ScopedJavaLocalRef<jobjectArray> j_argv = ToJavaArrayOfStrings(env, argv); | |
| 56 | |
| 57 size_t file_count = files_to_register->GetMappingSize(); | |
| 58 DCHECK(file_count > 0); | |
| 59 | |
| 60 ScopedJavaLocalRef<jclass> j_file_info_class = base::android::GetClass( | |
| 61 env, "org/chromium/content/common/FileDescriptorInfo"); | |
| 62 ScopedJavaLocalRef<jobjectArray> j_file_infos( | |
| 63 env, env->NewObjectArray(file_count, j_file_info_class.obj(), NULL)); | |
| 64 base::android::CheckException(env); | |
| 65 | |
| 66 for (size_t i = 0; i < file_count; ++i) { | |
| 67 int fd = files_to_register->GetFDAt(i); | |
| 68 PCHECK(0 <= fd); | |
| 69 int id = files_to_register->GetIDAt(i); | |
| 70 const auto& region = files_to_register->GetRegionAt(i); | |
| 71 bool auto_close = files_to_register->OwnsFD(fd); | |
| 72 ScopedJavaLocalRef<jobject> j_file_info = | |
| 73 Java_ChildProcessLauncher_makeFdInfo(env, id, fd, auto_close, | |
| 74 region.offset, region.size); | |
| 75 PCHECK(j_file_info.obj()); | |
| 76 env->SetObjectArrayElement(j_file_infos.obj(), i, j_file_info.obj()); | |
| 77 if (auto_close) { | |
| 78 ignore_result(files_to_register->ReleaseFD(fd).release()); | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 constexpr int param_key = 0; // TODO(boliu): Use this. | |
| 83 Java_ChildProcessLauncher_start( | |
| 84 env, base::android::GetApplicationContext(), param_key, j_argv, | |
| 85 child_process_id, j_file_infos, | |
| 86 reinterpret_cast<intptr_t>(new StartChildProcessCallback(callback))); | |
| 87 } | |
| 88 | |
| 89 // Stops a child process based on the handle returned from StartChildProcess. | |
| 90 void StopChildProcess(base::ProcessHandle handle) { | |
| 91 JNIEnv* env = AttachCurrentThread(); | |
| 92 DCHECK(env); | |
| 93 Java_ChildProcessLauncher_stop(env, static_cast<jint>(handle)); | |
| 94 } | |
| 95 | |
| 96 bool IsChildProcessOomProtected(base::ProcessHandle handle) { | |
| 97 JNIEnv* env = AttachCurrentThread(); | |
| 98 DCHECK(env); | |
| 99 return Java_ChildProcessLauncher_isOomProtected(env, | |
| 100 static_cast<jint>(handle)); | |
| 101 } | |
| 102 | |
| 103 void SetChildProcessInForeground(base::ProcessHandle handle, | |
| 104 bool in_foreground) { | |
| 105 JNIEnv* env = AttachCurrentThread(); | |
| 106 DCHECK(env); | |
| 107 return Java_ChildProcessLauncher_setInForeground( | |
| 108 env, static_cast<jint>(handle), static_cast<jboolean>(in_foreground)); | |
| 109 } | |
| 110 | |
| 111 // Callback invoked from Java once the process has been started. | 28 // Callback invoked from Java once the process has been started. |
| 112 void ChildProcessStartedCallback( | 29 void ChildProcessStartedCallback( |
| 113 ChildProcessLauncherHelper* helper, | 30 ChildProcessLauncherHelper* helper, |
| 114 base::ProcessHandle handle, | 31 base::ProcessHandle handle, |
| 115 int launch_result) { | 32 int launch_result) { |
| 116 // TODO(jcivelli): Remove this by defining better what happens on what thread | 33 // TODO(jcivelli): Remove this by defining better what happens on what thread |
| 117 // in the corresponding Java code. | 34 // in the corresponding Java code. |
| 118 ChildProcessLauncherHelper::Process process; | 35 ChildProcessLauncherHelper::Process process; |
| 119 process.process = base::Process(handle); | 36 process.process = base::Process(handle); |
| 120 if (BrowserThread::CurrentlyOn(BrowserThread::PROCESS_LAUNCHER)) { | 37 if (BrowserThread::CurrentlyOn(BrowserThread::PROCESS_LAUNCHER)) { |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 ResetFilesToShareForTestingPosix(); | 174 ResetFilesToShareForTestingPosix(); |
| 258 } | 175 } |
| 259 | 176 |
| 260 // static | 177 // static |
| 261 base::File OpenFileToShare(const base::FilePath& path, | 178 base::File OpenFileToShare(const base::FilePath& path, |
| 262 base::MemoryMappedFile::Region* region) { | 179 base::MemoryMappedFile::Region* region) { |
| 263 return base::File(base::android::OpenApkAsset(path.value(), region)); | 180 return base::File(base::android::OpenApkAsset(path.value(), region)); |
| 264 } | 181 } |
| 265 | 182 |
| 266 } // namespace internal | 183 } // namespace internal |
| 267 | |
| 268 // Called from ChildProcessLauncher.java when the ChildProcess was | |
| 269 // started. | |
| 270 // |client_context| is the pointer to StartChildProcessCallback which was | |
| 271 // passed in from StartChildProcess. | |
| 272 // |handle| is the processID of the child process as originated in Java, 0 if | |
| 273 // the ChildProcess could not be created. | |
| 274 static void OnChildProcessStarted(JNIEnv*, | |
| 275 const JavaParamRef<jclass>&, | |
| 276 jlong client_context, | |
| 277 jint handle) { | |
| 278 StartChildProcessCallback* callback = | |
| 279 reinterpret_cast<StartChildProcessCallback*>(client_context); | |
| 280 int launch_result = (handle == base::kNullProcessHandle) | |
| 281 ? LAUNCH_RESULT_FAILURE | |
| 282 : LAUNCH_RESULT_SUCCESS; | |
| 283 callback->Run(static_cast<base::ProcessHandle>(handle), launch_result); | |
| 284 delete callback; | |
| 285 } | |
| 286 | |
| 287 void CompleteScopedSurfaceRequest(JNIEnv* env, | |
| 288 const JavaParamRef<jclass>& clazz, | |
| 289 const JavaParamRef<jobject>& token, | |
| 290 const JavaParamRef<jobject>& surface) { | |
| 291 base::UnguessableToken requestToken = | |
| 292 base::android::UnguessableTokenAndroid::FromJavaUnguessableToken(env, | |
| 293 token); | |
| 294 if (!requestToken) { | |
| 295 DLOG(ERROR) << "Received invalid surface request token."; | |
| 296 return; | |
| 297 } | |
| 298 | |
| 299 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 300 | |
| 301 ScopedJavaGlobalRef<jobject> jsurface; | |
| 302 jsurface.Reset(env, surface); | |
| 303 ScopedSurfaceRequestManager::GetInstance()->FulfillScopedSurfaceRequest( | |
| 304 requestToken, gl::ScopedJavaSurface(jsurface)); | |
| 305 } | |
| 306 | |
| 307 jboolean IsSingleProcess(JNIEnv* env, const JavaParamRef<jclass>& clazz) { | |
| 308 return base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 309 switches::kSingleProcess); | |
| 310 } | |
| 311 | |
| 312 base::android::ScopedJavaLocalRef<jobject> GetViewSurface( | |
| 313 JNIEnv* env, | |
| 314 const base::android::JavaParamRef<jclass>& jcaller, | |
| 315 jint surface_id) { | |
| 316 gl::ScopedJavaSurface surface_view = | |
| 317 gpu::GpuSurfaceTracker::GetInstance()->AcquireJavaSurface(surface_id); | |
| 318 return base::android::ScopedJavaLocalRef<jobject>(surface_view.j_surface()); | |
| 319 } | |
| 320 | |
| 321 bool RegisterChildProcessLauncher(JNIEnv* env) { | |
| 322 return RegisterNativesImpl(env); | |
| 323 } | |
| 324 | |
| 325 } // namespace content | 184 } // namespace content |
| OLD | NEW |