1// Copyright (c) 2010 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/multiprocess_test.h"
6
7#include "base/base_switches.h"
8#include "base/command_line.h"
9
10#if defined(OS_POSIX)
11#include <sys/types.h>
12#include <unistd.h>
13#endif
14
15namespace base {
16
17MultiProcessTest::MultiProcessTest() {
18}
19
20ProcessHandle MultiProcessTest::SpawnChild(const std::string& procname,
21                                           bool debug_on_start) {
22#if defined(OS_WIN)
23  return SpawnChildImpl(procname, debug_on_start);
24#elif defined(OS_POSIX)
25  file_handle_mapping_vector empty_file_list;
26  return SpawnChildImpl(procname, empty_file_list, debug_on_start);
27#endif
28}
29
30#if defined(OS_POSIX)
31ProcessHandle MultiProcessTest::SpawnChild(
32    const std::string& procname,
33    const file_handle_mapping_vector& fds_to_map,
34    bool debug_on_start) {
35  return SpawnChildImpl(procname, fds_to_map, debug_on_start);
36}
37#endif
38
39CommandLine MultiProcessTest::MakeCmdLine(const std::string& procname,
40                                          bool debug_on_start) {
41  CommandLine cl(*CommandLine::ForCurrentProcess());
42  cl.AppendSwitchASCII(switches::kTestChildProcess, procname);
43  if (debug_on_start)
44    cl.AppendSwitch(switches::kDebugOnStart);
45  return cl;
46}
47
48#if defined(OS_WIN)
49
50ProcessHandle MultiProcessTest::SpawnChildImpl(const std::string& procname,
51                                               bool debug_on_start) {
52  ProcessHandle handle = static_cast<ProcessHandle>(NULL);
53  LaunchApp(MakeCmdLine(procname, debug_on_start),
54                  false, true, &handle);
55  return handle;
56}
57
58#elif defined(OS_POSIX)
59
60// TODO(port): with the CommandLine refactoring, this code is very similar
61// to the Windows code.  Investigate whether this can be made shorter.
62ProcessHandle MultiProcessTest::SpawnChildImpl(
63    const std::string& procname,
64    const file_handle_mapping_vector& fds_to_map,
65    bool debug_on_start) {
66  ProcessHandle handle = kNullProcessHandle;
67  LaunchApp(MakeCmdLine(procname, debug_on_start).argv(),
68                  fds_to_map, false, &handle);
69  return handle;
70}
71
72#endif
73
74}  // namespace base
75