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 "base/test/multiprocess_test.h" | 5 #include "base/test/multiprocess_test.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/android/context_utils.h" | 10 #include "base/android/context_utils.h" |
11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
12 #include "base/android/jni_array.h" | 12 #include "base/android/jni_array.h" |
13 #include "base/android/scoped_java_ref.h" | 13 #include "base/android/scoped_java_ref.h" |
14 #include "base/base_switches.h" | 14 #include "base/base_switches.h" |
15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "jni/MainReturnCodeResult_jni.h" | 17 #include "jni/MainReturnCodeResult_jni.h" |
18 #include "jni/MultiprocessTestClientLauncher_jni.h" | 18 #include "jni/MultiprocessTestClientLauncher_jni.h" |
19 | 19 |
20 namespace base { | 20 namespace base { |
21 | 21 |
22 // A very basic implementation for Android. On Android tests can run in an APK | 22 // A very basic implementation for Android. On Android tests can run in an APK |
23 // and we don't have an executable to exec*. This implementation does the bare | 23 // and we don't have an executable to exec*. This implementation does the bare |
24 // minimum to execute the method specified by procname (in the child process). | 24 // minimum to execute the method specified by procname (in the child process). |
25 // - All options except |fds_to_remap| are ignored. | 25 // - All options except |fds_to_remap| are ignored. |
26 // | 26 // |
27 // NOTE: This MUST NOT run on the main thread of the NativeTest application. | 27 // NOTE: This MUST NOT run on the main thread of the NativeTest application. |
28 Process SpawnMultiProcessTestChild(const std::string& procname, | 28 SpawnChildResult SpawnMultiProcessTestChild( |
29 const CommandLine& base_command_line, | 29 const std::string& procname, |
30 const LaunchOptions& options) { | 30 const CommandLine& base_command_line, |
| 31 const LaunchOptions& options) { |
31 JNIEnv* env = android::AttachCurrentThread(); | 32 JNIEnv* env = android::AttachCurrentThread(); |
32 DCHECK(env); | 33 DCHECK(env); |
33 | 34 |
34 std::vector<int> fd_keys; | 35 std::vector<int> fd_keys; |
35 std::vector<int> fd_fds; | 36 std::vector<int> fd_fds; |
36 if (options.fds_to_remap) { | 37 if (options.fds_to_remap) { |
37 for (auto& iter : *options.fds_to_remap) { | 38 for (auto& iter : *options.fds_to_remap) { |
38 fd_keys.push_back(iter.second); | 39 fd_keys.push_back(iter.second); |
39 fd_fds.push_back(iter.first); | 40 fd_fds.push_back(iter.first); |
40 } | 41 } |
41 } | 42 } |
42 | 43 |
43 android::ScopedJavaLocalRef<jobjectArray> fds = | 44 android::ScopedJavaLocalRef<jobjectArray> fds = |
44 android::Java_MultiprocessTestClientLauncher_makeFdInfoArray( | 45 android::Java_MultiprocessTestClientLauncher_makeFdInfoArray( |
45 env, base::android::ToJavaIntArray(env, fd_keys), | 46 env, base::android::ToJavaIntArray(env, fd_keys), |
46 base::android::ToJavaIntArray(env, fd_fds)); | 47 base::android::ToJavaIntArray(env, fd_fds)); |
47 | 48 |
48 CommandLine command_line(base_command_line); | 49 CommandLine command_line(base_command_line); |
49 if (!command_line.HasSwitch(switches::kTestChildProcess)) { | 50 if (!command_line.HasSwitch(switches::kTestChildProcess)) { |
50 command_line.AppendSwitchASCII(switches::kTestChildProcess, procname); | 51 command_line.AppendSwitchASCII(switches::kTestChildProcess, procname); |
51 } | 52 } |
52 | 53 |
53 android::ScopedJavaLocalRef<jobjectArray> j_argv = | 54 android::ScopedJavaLocalRef<jobjectArray> j_argv = |
54 android::ToJavaArrayOfStrings(env, command_line.argv()); | 55 android::ToJavaArrayOfStrings(env, command_line.argv()); |
55 jint pid = android::Java_MultiprocessTestClientLauncher_launchClient( | 56 jint pid = android::Java_MultiprocessTestClientLauncher_launchClient( |
56 env, android::GetApplicationContext(), j_argv, fds); | 57 env, android::GetApplicationContext(), j_argv, fds); |
57 return Process(pid); | 58 |
| 59 SpawnChildResult result; |
| 60 result.process = Process(pid); |
| 61 return result; |
58 } | 62 } |
59 | 63 |
60 bool WaitForMultiprocessTestChildExit(const Process& process, | 64 bool WaitForMultiprocessTestChildExit(const Process& process, |
61 TimeDelta timeout, | 65 TimeDelta timeout, |
62 int* exit_code) { | 66 int* exit_code) { |
63 JNIEnv* env = android::AttachCurrentThread(); | 67 JNIEnv* env = android::AttachCurrentThread(); |
64 DCHECK(env); | 68 DCHECK(env); |
65 | 69 |
66 base::android::ScopedJavaLocalRef<jobject> result_code = | 70 base::android::ScopedJavaLocalRef<jobject> result_code = |
67 android::Java_MultiprocessTestClientLauncher_waitForMainToReturn( | 71 android::Java_MultiprocessTestClientLauncher_waitForMainToReturn( |
(...skipping 13 matching lines...) Expand all Loading... |
81 int exit_code, | 85 int exit_code, |
82 bool wait) { | 86 bool wait) { |
83 JNIEnv* env = android::AttachCurrentThread(); | 87 JNIEnv* env = android::AttachCurrentThread(); |
84 DCHECK(env); | 88 DCHECK(env); |
85 | 89 |
86 return android::Java_MultiprocessTestClientLauncher_terminate( | 90 return android::Java_MultiprocessTestClientLauncher_terminate( |
87 env, android::GetApplicationContext(), process.Pid(), exit_code, wait); | 91 env, android::GetApplicationContext(), process.Pid(), exit_code, wait); |
88 } | 92 } |
89 | 93 |
90 } // namespace base | 94 } // namespace base |
OLD | NEW |