| 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/sandboxed_process_launcher.h" | |
| 6 | |
| 7 #include "base/android/jni_android.h" | |
| 8 #include "base/android/jni_array.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "jni/SandboxedProcessLauncher_jni.h" | |
| 12 | |
| 13 using base::android::AttachCurrentThread; | |
| 14 using base::android::ToJavaArrayOfStrings; | |
| 15 using base::android::ScopedJavaLocalRef; | |
| 16 using content::StartSandboxedProcessCallback; | |
| 17 | |
| 18 namespace content { | |
| 19 | |
| 20 // Called from SandboxedProcessLauncher.java when the SandboxedProcess was | |
| 21 // started. | |
| 22 // |client_context| is the pointer to StartSandboxedProcessCallback which was | |
| 23 // passed in from StartSandboxedProcess. | |
| 24 // |handle| is the processID of the child process as originated in Java, 0 if | |
| 25 // the SandboxedProcess could not be created. | |
| 26 static void OnSandboxedProcessStarted(JNIEnv*, | |
| 27 jclass, | |
| 28 jint client_context, | |
| 29 jint handle) { | |
| 30 StartSandboxedProcessCallback* callback = | |
| 31 reinterpret_cast<StartSandboxedProcessCallback*>(client_context); | |
| 32 if (handle) | |
| 33 callback->Run(static_cast<base::ProcessHandle>(handle)); | |
| 34 delete callback; | |
| 35 } | |
| 36 | |
| 37 void StartSandboxedProcess( | |
| 38 const CommandLine::StringVector& argv, | |
| 39 const std::vector<content::FileDescriptorInfo>& files_to_register, | |
| 40 const StartSandboxedProcessCallback& callback) { | |
| 41 JNIEnv* env = AttachCurrentThread(); | |
| 42 DCHECK(env); | |
| 43 | |
| 44 // Create the Command line String[] | |
| 45 ScopedJavaLocalRef<jobjectArray> j_argv = ToJavaArrayOfStrings(env, argv); | |
| 46 | |
| 47 size_t file_count = files_to_register.size(); | |
| 48 DCHECK(file_count > 0); | |
| 49 | |
| 50 ScopedJavaLocalRef<jintArray> j_file_ids(env, env->NewIntArray(file_count)); | |
| 51 base::android::CheckException(env); | |
| 52 jint* file_ids = env->GetIntArrayElements(j_file_ids.obj(), NULL); | |
| 53 base::android::CheckException(env); | |
| 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; | |
| 69 } | |
| 70 env->ReleaseIntArrayElements(j_file_ids.obj(), file_ids, 0); | |
| 71 env->ReleaseIntArrayElements(j_file_fds.obj(), file_fds, 0); | |
| 72 env->ReleaseBooleanArrayElements(j_file_auto_close.obj(), file_auto_close, 0); | |
| 73 | |
| 74 Java_SandboxedProcessLauncher_start(env, | |
| 75 base::android::GetApplicationContext(), | |
| 76 j_argv.obj(), | |
| 77 j_file_ids.obj(), | |
| 78 j_file_fds.obj(), | |
| 79 j_file_auto_close.obj(), | |
| 80 reinterpret_cast<jint>(new StartSandboxedProcessCallback(callback))); | |
| 81 } | |
| 82 | |
| 83 void StopSandboxedProcess(base::ProcessHandle handle) { | |
| 84 JNIEnv* env = AttachCurrentThread(); | |
| 85 DCHECK(env); | |
| 86 Java_SandboxedProcessLauncher_stop(env, static_cast<jint>(handle)); | |
| 87 } | |
| 88 | |
| 89 bool RegisterSandboxedProcessLauncher(JNIEnv* env) { | |
| 90 return RegisterNativesImpl(env); | |
| 91 } | |
| 92 | |
| 93 } // namespace content | |
| OLD | NEW |