OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "base/test/android/multiprocess_test_client_service.h" |
| 6 |
| 7 #include "base/android/jni_array.h" |
| 8 #include "base/files/scoped_file.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/native_library.h" |
| 11 #include "base/posix/global_descriptors.h" |
| 12 #include "base/test/android/android_main_function_thunk.h" |
| 13 #include "jni/MultiprocessTestClientService_jni.h" |
| 14 |
| 15 namespace base { |
| 16 namespace android { |
| 17 |
| 18 AndroidMainFunctionThunk::MainFuntion g_main_function = nullptr; |
| 19 AndroidMainFunctionThunk::AndroidMainFunctionThunk(MainFuntion main_function) { |
| 20 g_main_function = main_function; |
| 21 } |
| 22 |
| 23 bool RegisterMultiprocessTestClientServiceJni(JNIEnv* env) { |
| 24 return RegisterNativesImpl(env); |
| 25 } |
| 26 |
| 27 // static |
| 28 void RunMain(JNIEnv* env, |
| 29 const base::android::JavaParamRef<_jobject*>& jcaller, |
| 30 const base::android::JavaParamRef<jobjectArray>& command_line, |
| 31 const base::android::JavaParamRef<jintArray>& fds_to_map_ids, |
| 32 const base::android::JavaParamRef<jintArray>& fds_to_map_fds) { |
| 33 // Guards against process being reused. |
| 34 // Static variables, singletons, lazy instances make running code again in the |
| 35 // same child process difficult. |
| 36 static bool alreadyRun = false; |
| 37 CHECK(!alreadyRun); |
| 38 alreadyRun = true; |
| 39 |
| 40 std::vector<std::string> cpp_command_line; |
| 41 AppendJavaStringArrayToStringVector(env, command_line, &cpp_command_line); |
| 42 |
| 43 std::vector<int> fd_ids; |
| 44 base::android::JavaIntArrayToIntVector(env, fds_to_map_ids, &fd_ids); |
| 45 std::vector<int> fd_fds; |
| 46 base::android::JavaIntArrayToIntVector(env, fds_to_map_fds, &fd_fds); |
| 47 DCHECK(fd_ids.size() == fd_fds.size()); |
| 48 |
| 49 std::vector<base::ScopedFD> fd_scoped_fds; |
| 50 for (size_t i = 0; i < fd_ids.size(); i++) { |
| 51 base::GlobalDescriptors::GetInstance()->Set(fd_ids[i], fd_fds[i]); |
| 52 } |
| 53 std::vector<char*> c_command_line; |
| 54 for (auto& entry : cpp_command_line) { |
| 55 c_command_line.push_back(&entry[0]); |
| 56 } |
| 57 |
| 58 // If you hit this CHECK, it means you are using a multiprocess test and not |
| 59 // defining an AndroidMainFunctionThunk to point to your main in your main cc |
| 60 // file (typically run_all_tests.cc). |
| 61 CHECK(g_main_function); |
| 62 int result = g_main_function(c_command_line.size(), &c_command_line[0]); |
| 63 |
| 64 Java_MultiprocessTestClientService_setMainReturnValue(env, jcaller, result); |
| 65 } |
| 66 |
| 67 } // namespace android |
| 68 } // namespace base |
OLD | NEW |