| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/android/child_process_launcher_android.h" |
| 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <memory> |
| 11 #include <utility> |
| 12 |
| 13 #include "base/android/context_utils.h" |
| 14 #include "base/android/jni_android.h" |
| 15 #include "base/android/jni_array.h" |
| 16 #include "base/android/unguessable_token_android.h" |
| 17 #include "base/logging.h" |
| 18 #include "content/browser/android/scoped_surface_request_manager.h" |
| 19 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 20 #include "content/browser/media/android/browser_media_player_manager.h" |
| 21 #include "content/browser/media/android/media_web_contents_observer_android.h" |
| 22 #include "content/browser/web_contents/web_contents_impl.h" |
| 23 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/common/content_switches.h" |
| 25 #include "gpu/ipc/common/gpu_surface_tracker.h" |
| 26 #include "jni/ChildProcessLauncher_jni.h" |
| 27 #include "media/base/android/media_player_android.h" |
| 28 #include "ui/gl/android/surface_texture.h" |
| 29 |
| 30 using base::android::AttachCurrentThread; |
| 31 using base::android::JavaParamRef; |
| 32 using base::android::JavaRef; |
| 33 using base::android::ToJavaArrayOfStrings; |
| 34 using base::android::ScopedJavaGlobalRef; |
| 35 using base::android::ScopedJavaLocalRef; |
| 36 using content::StartChildProcessCallback; |
| 37 |
| 38 namespace content { |
| 39 |
| 40 // Called from ChildProcessLauncher.java when the ChildProcess was |
| 41 // started. |
| 42 // |client_context| is the pointer to StartChildProcessCallback which was |
| 43 // passed in from StartChildProcess. |
| 44 // |handle| is the processID of the child process as originated in Java, 0 if |
| 45 // the ChildProcess could not be created. |
| 46 static void OnChildProcessStarted(JNIEnv*, |
| 47 const JavaParamRef<jclass>&, |
| 48 jlong client_context, |
| 49 jint handle) { |
| 50 StartChildProcessCallback* callback = |
| 51 reinterpret_cast<StartChildProcessCallback*>(client_context); |
| 52 int launch_result = (handle == base::kNullProcessHandle) |
| 53 ? LAUNCH_RESULT_FAILURE |
| 54 : LAUNCH_RESULT_SUCCESS; |
| 55 callback->Run(static_cast<base::ProcessHandle>(handle), launch_result); |
| 56 delete callback; |
| 57 } |
| 58 |
| 59 void StartChildProcess( |
| 60 const base::CommandLine::StringVector& argv, |
| 61 int child_process_id, |
| 62 content::FileDescriptorInfo* files_to_register, |
| 63 const StartChildProcessCallback& callback) { |
| 64 JNIEnv* env = AttachCurrentThread(); |
| 65 DCHECK(env); |
| 66 |
| 67 // Create the Command line String[] |
| 68 ScopedJavaLocalRef<jobjectArray> j_argv = ToJavaArrayOfStrings(env, argv); |
| 69 |
| 70 size_t file_count = files_to_register->GetMappingSize(); |
| 71 DCHECK(file_count > 0); |
| 72 |
| 73 ScopedJavaLocalRef<jclass> j_file_info_class = base::android::GetClass( |
| 74 env, "org/chromium/content/common/FileDescriptorInfo"); |
| 75 ScopedJavaLocalRef<jobjectArray> j_file_infos( |
| 76 env, env->NewObjectArray(file_count, j_file_info_class.obj(), NULL)); |
| 77 base::android::CheckException(env); |
| 78 |
| 79 for (size_t i = 0; i < file_count; ++i) { |
| 80 int fd = files_to_register->GetFDAt(i); |
| 81 PCHECK(0 <= fd); |
| 82 int id = files_to_register->GetIDAt(i); |
| 83 const auto& region = files_to_register->GetRegionAt(i); |
| 84 bool auto_close = files_to_register->OwnsFD(fd); |
| 85 ScopedJavaLocalRef<jobject> j_file_info = |
| 86 Java_ChildProcessLauncher_makeFdInfo(env, id, fd, auto_close, |
| 87 region.offset, region.size); |
| 88 PCHECK(j_file_info.obj()); |
| 89 env->SetObjectArrayElement(j_file_infos.obj(), i, j_file_info.obj()); |
| 90 if (auto_close) { |
| 91 ignore_result(files_to_register->ReleaseFD(fd).release()); |
| 92 } |
| 93 } |
| 94 |
| 95 constexpr int param_key = 0; // TODO(boliu): Use this. |
| 96 Java_ChildProcessLauncher_start( |
| 97 env, base::android::GetApplicationContext(), param_key, j_argv, |
| 98 child_process_id, j_file_infos, |
| 99 reinterpret_cast<intptr_t>(new StartChildProcessCallback(callback))); |
| 100 } |
| 101 |
| 102 void StopChildProcess(base::ProcessHandle handle) { |
| 103 JNIEnv* env = AttachCurrentThread(); |
| 104 DCHECK(env); |
| 105 Java_ChildProcessLauncher_stop(env, static_cast<jint>(handle)); |
| 106 } |
| 107 |
| 108 bool IsChildProcessOomProtected(base::ProcessHandle handle) { |
| 109 JNIEnv* env = AttachCurrentThread(); |
| 110 DCHECK(env); |
| 111 return Java_ChildProcessLauncher_isOomProtected(env, |
| 112 static_cast<jint>(handle)); |
| 113 } |
| 114 |
| 115 void SetChildProcessInForeground(base::ProcessHandle handle, |
| 116 bool in_foreground) { |
| 117 JNIEnv* env = AttachCurrentThread(); |
| 118 DCHECK(env); |
| 119 return Java_ChildProcessLauncher_setInForeground(env, |
| 120 static_cast<jint>(handle), static_cast<jboolean>(in_foreground)); |
| 121 } |
| 122 |
| 123 void CompleteScopedSurfaceRequest(JNIEnv* env, |
| 124 const JavaParamRef<jclass>& clazz, |
| 125 const JavaParamRef<jobject>& token, |
| 126 const JavaParamRef<jobject>& surface) { |
| 127 base::UnguessableToken requestToken = |
| 128 base::android::UnguessableTokenAndroid::FromJavaUnguessableToken(env, |
| 129 token); |
| 130 if (!requestToken) { |
| 131 DLOG(ERROR) << "Received invalid surface request token."; |
| 132 return; |
| 133 } |
| 134 |
| 135 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 136 |
| 137 ScopedJavaGlobalRef<jobject> jsurface; |
| 138 jsurface.Reset(env, surface); |
| 139 ScopedSurfaceRequestManager::GetInstance()->FulfillScopedSurfaceRequest( |
| 140 requestToken, gl::ScopedJavaSurface(jsurface)); |
| 141 } |
| 142 |
| 143 jboolean IsSingleProcess(JNIEnv* env, const JavaParamRef<jclass>& clazz) { |
| 144 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 145 switches::kSingleProcess); |
| 146 } |
| 147 |
| 148 base::android::ScopedJavaLocalRef<jobject> GetViewSurface(JNIEnv* env, |
| 149 const base::android::JavaParamRef<jclass>& jcaller, |
| 150 jint surface_id) { |
| 151 gl::ScopedJavaSurface surface_view = |
| 152 gpu::GpuSurfaceTracker::GetInstance()->AcquireJavaSurface(surface_id); |
| 153 return base::android::ScopedJavaLocalRef<jobject>(surface_view.j_surface()); |
| 154 } |
| 155 |
| 156 bool RegisterChildProcessLauncher(JNIEnv* env) { |
| 157 return RegisterNativesImpl(env); |
| 158 } |
| 159 |
| 160 } // namespace content |
| OLD | NEW |