| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/android/sandboxed_process_launcher.h" | 5 #include "content/browser/android/sandboxed_process_launcher.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_array.h" | 8 #include "base/android/jni_array.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "jni/SandboxedProcessLauncher_jni.h" | 11 #include "jni/SandboxedProcessLauncher_jni.h" |
| 12 | 12 |
| 13 using base::android::AttachCurrentThread; | 13 using base::android::AttachCurrentThread; |
| 14 using base::android::ToJavaArrayOfStrings; | 14 using base::android::ToJavaArrayOfStrings; |
| 15 using base::android::ScopedJavaLocalRef; | 15 using base::android::ScopedJavaLocalRef; |
| 16 using base::GlobalDescriptors; | |
| 17 using content::StartSandboxedProcessCallback; | 16 using content::StartSandboxedProcessCallback; |
| 18 | 17 |
| 19 namespace content { | 18 namespace content { |
| 20 | 19 |
| 21 // Called from SandboxedProcessLauncher.java when the SandboxedProcess was | 20 // Called from SandboxedProcessLauncher.java when the SandboxedProcess was |
| 22 // started. | 21 // started. |
| 23 // |client_context| is the pointer to StartSandboxedProcessCallback which was | 22 // |client_context| is the pointer to StartSandboxedProcessCallback which was |
| 24 // passed in from StartSandboxedProcess. | 23 // passed in from StartSandboxedProcess. |
| 25 // |handle| is the processID of the child process as originated in Java, 0 if | 24 // |handle| is the processID of the child process as originated in Java, 0 if |
| 26 // the SandboxedProcess could not be created. | 25 // the SandboxedProcess could not be created. |
| 27 static void OnSandboxedProcessStarted(JNIEnv*, | 26 static void OnSandboxedProcessStarted(JNIEnv*, |
| 28 jclass, | 27 jclass, |
| 29 jint client_context, | 28 jint client_context, |
| 30 jint handle) { | 29 jint handle) { |
| 31 StartSandboxedProcessCallback* callback = | 30 StartSandboxedProcessCallback* callback = |
| 32 reinterpret_cast<StartSandboxedProcessCallback*>(client_context); | 31 reinterpret_cast<StartSandboxedProcessCallback*>(client_context); |
| 33 if (handle) | 32 if (handle) |
| 34 callback->Run(static_cast<base::ProcessHandle>(handle)); | 33 callback->Run(static_cast<base::ProcessHandle>(handle)); |
| 35 delete callback; | 34 delete callback; |
| 36 } | 35 } |
| 37 | 36 |
| 38 void StartSandboxedProcess( | 37 void StartSandboxedProcess( |
| 39 const CommandLine::StringVector& argv, | 38 const CommandLine::StringVector& argv, |
| 40 int ipc_fd, | 39 const std::vector<content::FileDescriptorInfo>& files_to_register, |
| 41 const GlobalDescriptors::Mapping& files_to_register, | |
| 42 const StartSandboxedProcessCallback& callback) { | 40 const StartSandboxedProcessCallback& callback) { |
| 43 JNIEnv* env = AttachCurrentThread(); | 41 JNIEnv* env = AttachCurrentThread(); |
| 44 DCHECK(env); | 42 DCHECK(env); |
| 45 | 43 |
| 46 // Create the Command line String[] | 44 // Create the Command line String[] |
| 47 ScopedJavaLocalRef<jobjectArray> j_argv = ToJavaArrayOfStrings(env, argv); | 45 ScopedJavaLocalRef<jobjectArray> j_argv = ToJavaArrayOfStrings(env, argv); |
| 48 | 46 |
| 49 ScopedJavaLocalRef<jintArray> j_file_to_register_id_files(env, | 47 size_t file_count = files_to_register.size(); |
| 50 env->NewIntArray(files_to_register.size() * 2)); | 48 DCHECK(file_count > 0); |
| 51 scoped_array<jint> file_to_register_id_files( | 49 |
| 52 new jint[files_to_register.size() * 2]); | 50 ScopedJavaLocalRef<jintArray> j_file_ids(env, env->NewIntArray(file_count)); |
| 53 for (size_t i = 0; i < files_to_register.size(); ++i) { | 51 base::android::CheckException(env); |
| 54 const GlobalDescriptors::KeyFDPair& id_file = files_to_register[i]; | 52 jint* file_ids = env->GetIntArrayElements(j_file_ids.obj(), NULL); |
| 55 file_to_register_id_files[2 * i] = id_file.first; | 53 base::android::CheckException(env); |
| 56 file_to_register_id_files[(2 * i) + 1] = id_file.second; | 54 ScopedJavaLocalRef<jintArray> j_file_fds(env, env->NewIntArray(file_count)); |
| 55 base::android::CheckException(env); |
| 56 jint* file_fds = env->GetIntArrayElements(j_file_fds.obj(), NULL); |
| 57 base::android::CheckException(env); |
| 58 ScopedJavaLocalRef<jbooleanArray> j_file_auto_close( |
| 59 env, env->NewBooleanArray(file_count)); |
| 60 base::android::CheckException(env); |
| 61 jboolean* file_auto_close = |
| 62 env->GetBooleanArrayElements(j_file_auto_close.obj(), NULL); |
| 63 base::android::CheckException(env); |
| 64 for (size_t i = 0; i < file_count; ++i) { |
| 65 const content::FileDescriptorInfo& fd_info = files_to_register[i]; |
| 66 file_ids[i] = fd_info.id; |
| 67 file_fds[i] = fd_info.fd.fd; |
| 68 file_auto_close[i] = fd_info.fd.auto_close; |
| 57 } | 69 } |
| 58 env->SetIntArrayRegion(j_file_to_register_id_files.obj(), | 70 env->ReleaseIntArrayElements(j_file_ids.obj(), file_ids, 0); |
| 59 0, files_to_register.size() * 2, | 71 env->ReleaseIntArrayElements(j_file_fds.obj(), file_fds, 0); |
| 60 file_to_register_id_files.get()); | 72 env->ReleaseBooleanArrayElements(j_file_auto_close.obj(), file_auto_close, 0); |
| 73 |
| 61 Java_SandboxedProcessLauncher_start(env, | 74 Java_SandboxedProcessLauncher_start(env, |
| 62 base::android::GetApplicationContext(), | 75 base::android::GetApplicationContext(), |
| 63 static_cast<jobjectArray>(j_argv.obj()), | 76 j_argv.obj(), |
| 64 static_cast<jint>(ipc_fd), | 77 j_file_ids.obj(), |
| 65 j_file_to_register_id_files.obj(), | 78 j_file_fds.obj(), |
| 66 reinterpret_cast<jint>(new StartSandboxedProcessCallback(callback))); | 79 j_file_auto_close.obj(), |
| 80 reinterpret_cast<jint>(new StartSandboxedProcessCallback(callback))); |
| 67 } | 81 } |
| 68 | 82 |
| 69 void StopSandboxedProcess(base::ProcessHandle handle) { | 83 void StopSandboxedProcess(base::ProcessHandle handle) { |
| 70 JNIEnv* env = AttachCurrentThread(); | 84 JNIEnv* env = AttachCurrentThread(); |
| 71 DCHECK(env); | 85 DCHECK(env); |
| 72 Java_SandboxedProcessLauncher_stop(env, static_cast<jint>(handle)); | 86 Java_SandboxedProcessLauncher_stop(env, static_cast<jint>(handle)); |
| 73 } | 87 } |
| 74 | 88 |
| 75 bool RegisterSandboxedProcessLauncher(JNIEnv* env) { | 89 bool RegisterSandboxedProcessLauncher(JNIEnv* env) { |
| 76 return RegisterNativesImpl(env); | 90 return RegisterNativesImpl(env); |
| 77 } | 91 } |
| 78 | 92 |
| 79 } // namespace content | 93 } // namespace content |
| OLD | NEW |