File: multiprocess_test_android.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (90 lines) | stat: -rw-r--r-- 3,279 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/test/multiprocess_test.h"

#include <string.h>
#include <vector>

#include "base/android/context_utils.h"
#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/scoped_java_ref.h"
#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "jni/MainReturnCodeResult_jni.h"
#include "jni/MultiprocessTestClientLauncher_jni.h"

namespace base {

// A very basic implementation for Android. On Android tests can run in an APK
// and we don't have an executable to exec*. This implementation does the bare
// minimum to execute the method specified by procname (in the child process).
//  - All options except |fds_to_remap| are ignored.
//
// NOTE: This MUST NOT run on the main thread of the NativeTest application.
Process SpawnMultiProcessTestChild(const std::string& procname,
                                   const CommandLine& base_command_line,
                                   const LaunchOptions& options) {
  JNIEnv* env = android::AttachCurrentThread();
  DCHECK(env);

  std::vector<int> fd_keys;
  std::vector<int> fd_fds;
  if (options.fds_to_remap) {
    for (auto& iter : *options.fds_to_remap) {
      fd_keys.push_back(iter.second);
      fd_fds.push_back(iter.first);
    }
  }

  android::ScopedJavaLocalRef<jobjectArray> fds =
      android::Java_MultiprocessTestClientLauncher_makeFdInfoArray(
          env, base::android::ToJavaIntArray(env, fd_keys),
          base::android::ToJavaIntArray(env, fd_fds));

  CommandLine command_line(base_command_line);
  if (!command_line.HasSwitch(switches::kTestChildProcess)) {
    command_line.AppendSwitchASCII(switches::kTestChildProcess, procname);
  }

  android::ScopedJavaLocalRef<jobjectArray> j_argv =
      android::ToJavaArrayOfStrings(env, command_line.argv());
  jint pid = android::Java_MultiprocessTestClientLauncher_launchClient(
      env, android::GetApplicationContext(), j_argv, fds);
  return Process(pid);
}

bool WaitForMultiprocessTestChildExit(const Process& process,
                                      TimeDelta timeout,
                                      int* exit_code) {
  JNIEnv* env = android::AttachCurrentThread();
  DCHECK(env);

  base::android::ScopedJavaLocalRef<jobject> result_code =
      android::Java_MultiprocessTestClientLauncher_waitForMainToReturn(
          env, android::GetApplicationContext(), process.Pid(),
          static_cast<int32_t>(timeout.InMilliseconds()));
  if (result_code.is_null() ||
      Java_MainReturnCodeResult_hasTimedOut(env, result_code)) {
    return false;
  }
  if (exit_code) {
    *exit_code = Java_MainReturnCodeResult_getReturnCode(env, result_code);
  }
  return true;
}

bool TerminateMultiProcessTestChild(const Process& process,
                                    int exit_code,
                                    bool wait) {
  JNIEnv* env = android::AttachCurrentThread();
  DCHECK(env);

  return android::Java_MultiprocessTestClientLauncher_terminate(
      env, android::GetApplicationContext(), process.Pid(), exit_code, wait);
}

}  // namespace base