Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(133)

Side by Side Diff: content/browser/android/sandboxed_process_launcher.cc

Issue 10949027: Close leaking FDs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed comments. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
51 scoped_array<jint> file_to_register_id_files( 49 ScopedJavaLocalRef<jintArray> j_file_ids(env, env->NewIntArray(file_count));
52 new jint[files_to_register.size() * 2]); 50 jint* file_ids = env->GetIntArrayElements(j_file_ids.obj(), NULL);
53 for (size_t i = 0; i < files_to_register.size(); ++i) { 51 ScopedJavaLocalRef<jintArray> j_file_fds(env, env->NewIntArray(file_count));
54 const GlobalDescriptors::KeyFDPair& id_file = files_to_register[i]; 52 jint* file_fds = env->GetIntArrayElements(j_file_fds.obj(), NULL);
55 file_to_register_id_files[2 * i] = id_file.first; 53 ScopedJavaLocalRef<jbooleanArray> j_file_autoclose(
56 file_to_register_id_files[(2 * i) + 1] = id_file.second; 54 env, env->NewBooleanArray(file_count));
55 jboolean* file_autoclose =
56 env->GetBooleanArrayElements(j_file_autoclose.obj(), NULL);
57 base::android::CheckException(env);
58 for (size_t i = 0; i < file_count; ++i) {
59 const content::FileDescriptorInfo& fd_info = files_to_register[i];
60 file_ids[i] = fd_info.id;
61 file_fds[i] = fd_info.fd.fd;
62 file_autoclose[i] = fd_info.fd.auto_close;
57 } 63 }
58 env->SetIntArrayRegion(j_file_to_register_id_files.obj(), 64 env->ReleaseIntArrayElements(j_file_ids.obj(), file_ids, JNI_ABORT);
59 0, files_to_register.size() * 2, 65 env->ReleaseIntArrayElements(j_file_fds.obj(), file_fds, JNI_ABORT);
60 file_to_register_id_files.get()); 66 env->ReleaseBooleanArrayElements(j_file_autoclose.obj(), file_autoclose,
67 JNI_ABORT);
68
61 Java_SandboxedProcessLauncher_start(env, 69 Java_SandboxedProcessLauncher_start(env,
62 base::android::GetApplicationContext(), 70 base::android::GetApplicationContext(),
63 static_cast<jobjectArray>(j_argv.obj()), 71 j_argv.obj(),
64 static_cast<jint>(ipc_fd), 72 j_file_ids.obj(),
65 j_file_to_register_id_files.obj(), 73 j_file_fds.obj(),
66 reinterpret_cast<jint>(new StartSandboxedProcessCallback(callback))); 74 j_file_autoclose.obj(),
75 reinterpret_cast<jint>(new StartSandboxedProcessCallback(callback)));
67 } 76 }
68 77
69 void StopSandboxedProcess(base::ProcessHandle handle) { 78 void StopSandboxedProcess(base::ProcessHandle handle) {
70 JNIEnv* env = AttachCurrentThread(); 79 JNIEnv* env = AttachCurrentThread();
71 DCHECK(env); 80 DCHECK(env);
72 Java_SandboxedProcessLauncher_stop(env, static_cast<jint>(handle)); 81 Java_SandboxedProcessLauncher_stop(env, static_cast<jint>(handle));
73 } 82 }
74 83
75 bool RegisterSandboxedProcessLauncher(JNIEnv* env) { 84 bool RegisterSandboxedProcessLauncher(JNIEnv* env) {
76 return RegisterNativesImpl(env); 85 return RegisterNativesImpl(env);
77 } 86 }
78 87
79 } // namespace content 88 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698