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/child_process_launcher_android.h" | 5 #include "content/browser/android/child_process_launcher_android.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" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 StartChildProcessCallback* callback = | 95 StartChildProcessCallback* callback = |
96 reinterpret_cast<StartChildProcessCallback*>(client_context); | 96 reinterpret_cast<StartChildProcessCallback*>(client_context); |
97 if (handle) | 97 if (handle) |
98 callback->Run(static_cast<base::ProcessHandle>(handle)); | 98 callback->Run(static_cast<base::ProcessHandle>(handle)); |
99 delete callback; | 99 delete callback; |
100 } | 100 } |
101 | 101 |
102 void StartChildProcess( | 102 void StartChildProcess( |
103 const base::CommandLine::StringVector& argv, | 103 const base::CommandLine::StringVector& argv, |
104 int child_process_id, | 104 int child_process_id, |
105 const std::vector<content::FileDescriptorInfo>& files_to_register, | 105 scoped_ptr<content::FileDescriptorInfo> files_to_register, |
106 const StartChildProcessCallback& callback) { | 106 const StartChildProcessCallback& callback) { |
107 JNIEnv* env = AttachCurrentThread(); | 107 JNIEnv* env = AttachCurrentThread(); |
108 DCHECK(env); | 108 DCHECK(env); |
109 | 109 |
110 // Create the Command line String[] | 110 // Create the Command line String[] |
111 ScopedJavaLocalRef<jobjectArray> j_argv = ToJavaArrayOfStrings(env, argv); | 111 ScopedJavaLocalRef<jobjectArray> j_argv = ToJavaArrayOfStrings(env, argv); |
112 | 112 |
113 size_t file_count = files_to_register.size(); | 113 size_t file_count = files_to_register->GetMappingSize(); |
114 DCHECK(file_count > 0); | 114 DCHECK(file_count > 0); |
115 | 115 |
116 ScopedJavaLocalRef<jintArray> j_file_ids(env, env->NewIntArray(file_count)); | 116 ScopedJavaLocalRef<jintArray> j_file_ids(env, env->NewIntArray(file_count)); |
117 base::android::CheckException(env); | 117 base::android::CheckException(env); |
118 jint* file_ids = env->GetIntArrayElements(j_file_ids.obj(), NULL); | 118 jint* file_ids = env->GetIntArrayElements(j_file_ids.obj(), NULL); |
119 base::android::CheckException(env); | 119 base::android::CheckException(env); |
120 ScopedJavaLocalRef<jintArray> j_file_fds(env, env->NewIntArray(file_count)); | 120 ScopedJavaLocalRef<jintArray> j_file_fds(env, env->NewIntArray(file_count)); |
121 base::android::CheckException(env); | 121 base::android::CheckException(env); |
122 jint* file_fds = env->GetIntArrayElements(j_file_fds.obj(), NULL); | 122 jint* file_fds = env->GetIntArrayElements(j_file_fds.obj(), NULL); |
123 base::android::CheckException(env); | 123 base::android::CheckException(env); |
124 ScopedJavaLocalRef<jbooleanArray> j_file_auto_close( | 124 ScopedJavaLocalRef<jbooleanArray> j_file_auto_close( |
125 env, env->NewBooleanArray(file_count)); | 125 env, env->NewBooleanArray(file_count)); |
126 base::android::CheckException(env); | 126 base::android::CheckException(env); |
127 jboolean* file_auto_close = | 127 jboolean* file_auto_close = |
128 env->GetBooleanArrayElements(j_file_auto_close.obj(), NULL); | 128 env->GetBooleanArrayElements(j_file_auto_close.obj(), NULL); |
129 base::android::CheckException(env); | 129 base::android::CheckException(env); |
130 for (size_t i = 0; i < file_count; ++i) { | 130 for (size_t i = 0; i < file_count; ++i) { |
131 const content::FileDescriptorInfo& fd_info = files_to_register[i]; | 131 // Owners of passed descriptors can outlive this function and we don't know |
132 file_ids[i] = fd_info.id; | 132 // when it is safe to close() them. So we pass dup()-ed FD and |
133 file_fds[i] = fd_info.fd.fd; | 133 // let ChildProcessLauncher in java take care of their lifetimes. |
134 file_auto_close[i] = fd_info.fd.auto_close; | 134 // TODO(morrita): Drop FileDescriptorInfo.mAutoClose on Java side. |
| 135 file_auto_close[i] = true; // This indicates ownership transfer. |
| 136 file_ids[i] = files_to_register->GetIDAt(i); |
| 137 file_fds[i] = dup(files_to_register->GetFDAt(i)); |
| 138 PCHECK(0 <= file_fds[i]); |
135 } | 139 } |
136 env->ReleaseIntArrayElements(j_file_ids.obj(), file_ids, 0); | 140 env->ReleaseIntArrayElements(j_file_ids.obj(), file_ids, 0); |
137 env->ReleaseIntArrayElements(j_file_fds.obj(), file_fds, 0); | 141 env->ReleaseIntArrayElements(j_file_fds.obj(), file_fds, 0); |
138 env->ReleaseBooleanArrayElements(j_file_auto_close.obj(), file_auto_close, 0); | 142 env->ReleaseBooleanArrayElements(j_file_auto_close.obj(), file_auto_close, 0); |
139 | 143 |
140 Java_ChildProcessLauncher_start(env, | 144 Java_ChildProcessLauncher_start(env, |
141 base::android::GetApplicationContext(), | 145 base::android::GetApplicationContext(), |
142 j_argv.obj(), | 146 j_argv.obj(), |
143 child_process_id, | 147 child_process_id, |
144 j_file_ids.obj(), | 148 j_file_ids.obj(), |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 jboolean IsSingleProcess(JNIEnv* env, jclass clazz) { | 217 jboolean IsSingleProcess(JNIEnv* env, jclass clazz) { |
214 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 218 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
215 switches::kSingleProcess); | 219 switches::kSingleProcess); |
216 } | 220 } |
217 | 221 |
218 bool RegisterChildProcessLauncher(JNIEnv* env) { | 222 bool RegisterChildProcessLauncher(JNIEnv* env) { |
219 return RegisterNativesImpl(env); | 223 return RegisterNativesImpl(env); |
220 } | 224 } |
221 | 225 |
222 } // namespace content | 226 } // namespace content |
OLD | NEW |