OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "mojo/edk/test/multiprocess_test_helper.h" | 5 #include "mojo/edk/test/multiprocess_test_helper.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/process/kill.h" | 9 #include "base/process/kill.h" |
10 #include "base/process/process_handle.h" | 10 #include "base/process/process_handle.h" |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "mojo/edk/embedder/embedder.h" |
12 #include "mojo/edk/embedder/platform_channel_pair.h" | 13 #include "mojo/edk/embedder/platform_channel_pair.h" |
13 | 14 |
14 #if defined(OS_WIN) | 15 #if defined(OS_WIN) |
15 #include "base/win/windows_version.h" | 16 #include "base/win/windows_version.h" |
16 #endif | 17 #endif |
17 | 18 |
18 namespace mojo { | 19 namespace mojo { |
19 namespace edk { | 20 namespace edk { |
20 namespace test { | 21 namespace test { |
21 | 22 |
| 23 const char kBrokerHandleSwitch[] = "broker-handle"; |
| 24 |
22 MultiprocessTestHelper::MultiprocessTestHelper() { | 25 MultiprocessTestHelper::MultiprocessTestHelper() { |
23 platform_channel_pair_.reset(new PlatformChannelPair()); | 26 platform_channel_pair_.reset(new PlatformChannelPair()); |
24 server_platform_handle = platform_channel_pair_->PassServerHandle(); | 27 server_platform_handle = platform_channel_pair_->PassServerHandle(); |
| 28 broker_platform_channel_pair_.reset(new PlatformChannelPair()); |
25 } | 29 } |
26 | 30 |
27 MultiprocessTestHelper::~MultiprocessTestHelper() { | 31 MultiprocessTestHelper::~MultiprocessTestHelper() { |
28 CHECK(!test_child_.IsValid()); | 32 CHECK(!test_child_.IsValid()); |
29 server_platform_handle.reset(); | 33 server_platform_handle.reset(); |
30 platform_channel_pair_.reset(); | 34 platform_channel_pair_.reset(); |
31 } | 35 } |
32 | 36 |
33 void MultiprocessTestHelper::StartChild(const std::string& test_child_name) { | 37 void MultiprocessTestHelper::StartChild(const std::string& test_child_name) { |
34 StartChildWithExtraSwitch(test_child_name, std::string(), std::string()); | 38 StartChildWithExtraSwitch(test_child_name, std::string(), std::string()); |
35 } | 39 } |
36 | 40 |
37 void MultiprocessTestHelper::StartChildWithExtraSwitch( | 41 void MultiprocessTestHelper::StartChildWithExtraSwitch( |
38 const std::string& test_child_name, | 42 const std::string& test_child_name, |
39 const std::string& switch_string, | 43 const std::string& switch_string, |
40 const std::string& switch_value) { | 44 const std::string& switch_value) { |
41 CHECK(platform_channel_pair_); | 45 CHECK(platform_channel_pair_); |
42 CHECK(!test_child_name.empty()); | 46 CHECK(!test_child_name.empty()); |
43 CHECK(!test_child_.IsValid()); | 47 CHECK(!test_child_.IsValid()); |
44 | 48 |
45 std::string test_child_main = test_child_name + "TestChildMain"; | 49 std::string test_child_main = test_child_name + "TestChildMain"; |
46 | 50 |
47 base::CommandLine command_line( | 51 base::CommandLine command_line( |
48 base::GetMultiProcessTestChildBaseCommandLine()); | 52 base::GetMultiProcessTestChildBaseCommandLine()); |
49 HandlePassingInformation handle_passing_info; | 53 HandlePassingInformation handle_passing_info; |
50 platform_channel_pair_->PrepareToPassClientHandleToChildProcess( | 54 platform_channel_pair_->PrepareToPassClientHandleToChildProcess( |
51 &command_line, &handle_passing_info); | 55 &command_line, &handle_passing_info); |
52 | 56 |
| 57 std::string broker_handle = broker_platform_channel_pair_-> |
| 58 PrepareToPassClientHandleToChildProcessAsString(&handle_passing_info); |
| 59 command_line.AppendSwitchASCII(kBrokerHandleSwitch, broker_handle); |
| 60 |
53 if (!switch_string.empty()) { | 61 if (!switch_string.empty()) { |
54 CHECK(!command_line.HasSwitch(switch_string)); | 62 CHECK(!command_line.HasSwitch(switch_string)); |
55 if (!switch_value.empty()) | 63 if (!switch_value.empty()) |
56 command_line.AppendSwitchASCII(switch_string, switch_value); | 64 command_line.AppendSwitchASCII(switch_string, switch_value); |
57 else | 65 else |
58 command_line.AppendSwitch(switch_string); | 66 command_line.AppendSwitch(switch_string); |
59 } | 67 } |
60 | 68 |
61 base::LaunchOptions options; | 69 base::LaunchOptions options; |
62 #if defined(OS_POSIX) | 70 #if defined(OS_POSIX) |
63 options.fds_to_remap = &handle_passing_info; | 71 options.fds_to_remap = &handle_passing_info; |
64 #elif defined(OS_WIN) | 72 #elif defined(OS_WIN) |
65 options.start_hidden = true; | 73 options.start_hidden = true; |
66 if (base::win::GetVersion() >= base::win::VERSION_VISTA) | 74 if (base::win::GetVersion() >= base::win::VERSION_VISTA) |
67 options.handles_to_inherit = &handle_passing_info; | 75 options.handles_to_inherit = &handle_passing_info; |
68 else | 76 else |
69 options.inherit_handles = true; | 77 options.inherit_handles = true; |
70 #else | 78 #else |
71 #error "Not supported yet." | 79 #error "Not supported yet." |
72 #endif | 80 #endif |
73 | 81 |
74 test_child_ = | 82 test_child_ = |
75 base::SpawnMultiProcessTestChild(test_child_main, command_line, options); | 83 base::SpawnMultiProcessTestChild(test_child_main, command_line, options); |
76 platform_channel_pair_->ChildProcessLaunched(); | 84 platform_channel_pair_->ChildProcessLaunched(); |
77 | 85 |
| 86 broker_platform_channel_pair_->ChildProcessLaunched(); |
| 87 ChildProcessLaunched(test_child_.Handle(), |
| 88 broker_platform_channel_pair_->PassServerHandle()); |
| 89 |
78 CHECK(test_child_.IsValid()); | 90 CHECK(test_child_.IsValid()); |
79 } | 91 } |
80 | 92 |
81 int MultiprocessTestHelper::WaitForChildShutdown() { | 93 int MultiprocessTestHelper::WaitForChildShutdown() { |
82 CHECK(test_child_.IsValid()); | 94 CHECK(test_child_.IsValid()); |
83 | 95 |
84 int rv = -1; | 96 int rv = -1; |
85 CHECK( | 97 CHECK( |
86 test_child_.WaitForExitWithTimeout(TestTimeouts::action_timeout(), &rv)); | 98 test_child_.WaitForExitWithTimeout(TestTimeouts::action_timeout(), &rv)); |
87 test_child_.Close(); | 99 test_child_.Close(); |
88 return rv; | 100 return rv; |
89 } | 101 } |
90 | 102 |
91 bool MultiprocessTestHelper::WaitForChildTestShutdown() { | 103 bool MultiprocessTestHelper::WaitForChildTestShutdown() { |
92 return WaitForChildShutdown() == 0; | 104 return WaitForChildShutdown() == 0; |
93 } | 105 } |
94 | 106 |
95 // static | 107 // static |
96 void MultiprocessTestHelper::ChildSetup() { | 108 void MultiprocessTestHelper::ChildSetup() { |
97 CHECK(base::CommandLine::InitializedForCurrentProcess()); | 109 CHECK(base::CommandLine::InitializedForCurrentProcess()); |
98 client_platform_handle = | 110 client_platform_handle = |
99 PlatformChannelPair::PassClientHandleFromParentProcess( | 111 PlatformChannelPair::PassClientHandleFromParentProcess( |
100 *base::CommandLine::ForCurrentProcess()); | 112 *base::CommandLine::ForCurrentProcess()); |
| 113 |
| 114 std::string broker_handle_str = |
| 115 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 116 kBrokerHandleSwitch); |
| 117 ScopedPlatformHandle broker_handle = |
| 118 PlatformChannelPair::PassClientHandleFromParentProcessFromString( |
| 119 broker_handle_str); |
| 120 SetParentPipeHandle(broker_handle.Pass()); |
101 } | 121 } |
102 | 122 |
103 // static | 123 // static |
104 ScopedPlatformHandle MultiprocessTestHelper::client_platform_handle; | 124 ScopedPlatformHandle MultiprocessTestHelper::client_platform_handle; |
105 | 125 |
106 } // namespace test | 126 } // namespace test |
107 } // namespace edk | 127 } // namespace edk |
108 } // namespace mojo | 128 } // namespace mojo |
OLD | NEW |