OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/test/base/mojo_test_connector.h" | 5 #include "chrome/test/base/mojo_test_connector.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
12 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
13 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
14 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
15 #include "content/public/common/content_switches.h" | 17 #include "content/public/common/content_switches.h" |
16 #include "content/public/test/test_launcher.h" | 18 #include "content/public/test/test_launcher.h" |
17 #include "mojo/edk/embedder/embedder.h" | 19 #include "mojo/edk/embedder/embedder.h" |
18 #include "mojo/edk/embedder/platform_channel_pair.h" | 20 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 21 #include "mojo/edk/embedder/scoped_ipc_support.h" |
19 #include "mojo/public/cpp/bindings/interface_request.h" | 22 #include "mojo/public/cpp/bindings/interface_request.h" |
20 #include "services/catalog/store.h" | 23 #include "services/catalog/store.h" |
| 24 #include "services/service_manager/background/background_service_manager.h" |
21 #include "services/service_manager/public/cpp/connector.h" | 25 #include "services/service_manager/public/cpp/connector.h" |
22 #include "services/service_manager/public/cpp/service.h" | 26 #include "services/service_manager/public/cpp/service.h" |
23 #include "services/service_manager/public/cpp/service_context.h" | 27 #include "services/service_manager/public/cpp/service_context.h" |
24 #include "services/service_manager/runner/common/client_util.h" | 28 #include "services/service_manager/runner/common/client_util.h" |
25 #include "services/service_manager/runner/common/switches.h" | 29 #include "services/service_manager/runner/common/switches.h" |
26 #include "services/service_manager/runner/host/service_process_launcher.h" | 30 #include "services/service_manager/runner/host/service_process_launcher.h" |
27 #include "services/service_manager/service_manager.h" | 31 #include "services/service_manager/service_manager.h" |
28 #include "services/service_manager/switches.h" | 32 #include "services/service_manager/switches.h" |
29 | 33 |
30 namespace { | 34 namespace { |
31 | 35 |
32 const char kTestRunnerName[] = "mash_browser_tests"; | 36 const char kTestRunnerName[] = "mash_browser_tests"; |
33 const char kTestName[] = "content_browser"; | 37 const char kTestName[] = "content_browser"; |
34 | 38 |
35 // BackgroundTestState maintains all the state necessary to bind the test to | 39 // State created per test to register a client process with the background |
36 // mojo. This class is only used on the thread created by | 40 // service manager. |
37 // BackgroundServiceManager. | 41 class MojoTestState : public content::TestState { |
38 class BackgroundTestState { | |
39 public: | 42 public: |
40 BackgroundTestState() : child_token_(mojo::edk::GenerateRandomToken()) {} | 43 explicit MojoTestState( |
41 ~BackgroundTestState() {} | 44 service_manager::BackgroundServiceManager* background_service_manager) |
| 45 : child_token_(mojo::edk::GenerateRandomToken()), |
| 46 background_service_manager_(background_service_manager), |
| 47 weak_factory_(this) {} |
| 48 ~MojoTestState() override {} |
42 | 49 |
43 // Prepares the command line and other setup for connecting the test to mojo. | 50 void Init(base::CommandLine* command_line, |
44 // Must be paired with a call to ChildProcessLaunched(). | 51 base::TestLauncher::LaunchOptions* test_launch_options) { |
45 void Connect(base::CommandLine* command_line, | |
46 service_manager::ServiceManager* service_manager, | |
47 base::TestLauncher::LaunchOptions* test_launch_options) { | |
48 command_line->AppendSwitch(MojoTestConnector::kTestSwitch); | 52 command_line->AppendSwitch(MojoTestConnector::kTestSwitch); |
49 command_line->AppendSwitch(switches::kChildProcess); | 53 command_line->AppendSwitch(switches::kChildProcess); |
50 mojo_ipc_channel_.reset(new mojo::edk::PlatformChannelPair); | 54 |
51 mojo_ipc_channel_->PrepareToPassClientHandleToChildProcess( | 55 platform_channel_ = base::MakeUnique<mojo::edk::PlatformChannelPair>(); |
| 56 |
| 57 platform_channel_->PrepareToPassClientHandleToChildProcess( |
52 command_line, &handle_passing_info_); | 58 command_line, &handle_passing_info_); |
53 #if defined(OS_WIN) | 59 #if defined(OS_WIN) |
54 test_launch_options->inherit_handles = true; | 60 test_launch_options->inherit_handles = true; |
55 test_launch_options->handles_to_inherit = &handle_passing_info_; | 61 test_launch_options->handles_to_inherit = &handle_passing_info_; |
56 #if defined(OFFICIAL_BUILD) | 62 #if defined(OFFICIAL_BUILD) |
57 CHECK(false) << "Launching mojo process with inherit_handles is insecure!"; | 63 CHECK(false) << "Launching mojo process with inherit_handles is insecure!"; |
58 #endif | 64 #endif |
59 #elif defined(OS_POSIX) | 65 #elif defined(OS_POSIX) |
60 test_launch_options->fds_to_remap = &handle_passing_info_; | 66 test_launch_options->fds_to_remap = &handle_passing_info_; |
61 #else | 67 #else |
62 #error "Unsupported" | 68 #error "Unsupported" |
63 #endif | 69 #endif |
64 service_manager::mojom::ServicePtr service = | 70 service_manager::mojom::ServicePtr service = |
65 service_manager::PassServiceRequestOnCommandLine(command_line, | 71 service_manager::PassServiceRequestOnCommandLine(command_line, |
66 child_token_); | 72 child_token_); |
67 | 73 |
68 std::unique_ptr<service_manager::ConnectParams> params( | 74 background_service_manager_->RegisterService( |
69 new service_manager::ConnectParams); | 75 service_manager::Identity( |
70 params->set_source(service_manager::CreateServiceManagerIdentity()); | 76 kTestName, service_manager::mojom::kRootUserID), |
71 // Use the default instance name (which should be "browser"). Otherwise a | 77 std::move(service), |
72 // service (e.g. ash) that connects to the default "content_browser" | 78 service_manager::mojom::PIDReceiverRequest(&pid_receiver_)); |
73 // will spawn a new instance. | |
74 params->set_target(service_manager::Identity( | |
75 kTestName, service_manager::mojom::kRootUserID)); | |
76 params->set_client_process_info(std::move(service), | |
77 MakeRequest(&pid_receiver_)); | |
78 service_manager->Connect(std::move(params)); | |
79 } | |
80 | 79 |
81 // Called after the test process has launched. Completes the registration done | 80 // ChildProcessLaunched may be called on an arbitrary thread, so track the |
82 // in Connect(). | 81 // current TaskRunner and post back to it when we want to send the PID. |
83 void ChildProcessLaunched(base::ProcessHandle handle, base::ProcessId pid) { | 82 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
84 pid_receiver_->SetPID(pid); | |
85 mojo_ipc_channel_->ChildProcessLaunched(); | |
86 mojo::edk::ChildProcessLaunched( | |
87 handle, mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle( | |
88 mojo_ipc_channel_->PassServerHandle().release().handle)), | |
89 child_token_); | |
90 } | |
91 | |
92 private: | |
93 // Used to back the NodeChannel between the parent and child node. | |
94 const std::string child_token_; | |
95 std::unique_ptr<mojo::edk::PlatformChannelPair> mojo_ipc_channel_; | |
96 | |
97 mojo::edk::HandlePassingInformation handle_passing_info_; | |
98 | |
99 service_manager::mojom::PIDReceiverPtr pid_receiver_; | |
100 | |
101 DISALLOW_COPY_AND_ASSIGN(BackgroundTestState); | |
102 }; | |
103 | |
104 // Called used destroy BackgroundTestState on the background thread. | |
105 void DestroyBackgroundStateOnBackgroundThread( | |
106 std::unique_ptr<BackgroundTestState> state, | |
107 service_manager::ServiceManager* service_manager) {} | |
108 | |
109 // State created per test. Manages creation of the corresponding | |
110 // BackgroundTestState and making sure processing runs on the right threads. | |
111 class MojoTestState : public content::TestState { | |
112 public: | |
113 explicit MojoTestState( | |
114 service_manager::BackgroundServiceManager* background_service_manager) | |
115 : background_service_manager_(background_service_manager) {} | |
116 | |
117 ~MojoTestState() override { | |
118 DCHECK(background_state_); | |
119 // BackgroundState needs to be destroyed on the background thread. We're | |
120 // guaranteed |background_service_manager_| has been created by the time we | |
121 // reach | |
122 // here as Init() blocks until |background_service_manager_| has been | |
123 // created. | |
124 background_service_manager_->ExecuteOnServiceManagerThread( | |
125 base::Bind(&DestroyBackgroundStateOnBackgroundThread, | |
126 base::Passed(&background_state_))); | |
127 } | |
128 | |
129 void Init(base::CommandLine* command_line, | |
130 base::TestLauncher::LaunchOptions* test_launch_options) { | |
131 base::WaitableEvent signal(base::WaitableEvent::ResetPolicy::MANUAL, | |
132 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
133 background_service_manager_->ExecuteOnServiceManagerThread(base::Bind( | |
134 &MojoTestState::BindOnBackgroundThread, base::Unretained(this), &signal, | |
135 command_line, test_launch_options)); | |
136 signal.Wait(); | |
137 } | 83 } |
138 | 84 |
139 private: | 85 private: |
140 // content::TestState: | 86 // content::TestState: |
141 void ChildProcessLaunched(base::ProcessHandle handle, | 87 void ChildProcessLaunched(base::ProcessHandle handle, |
142 base::ProcessId pid) override { | 88 base::ProcessId pid) override { |
143 // This is called on a random thread. We need to ensure BackgroundTestState | 89 platform_channel_->ChildProcessLaunched(); |
144 // is only called on the background thread, and we wait for | 90 mojo::edk::ChildProcessLaunched( |
145 // ChildProcessLaunchedOnBackgroundThread() to be run before continuing so | 91 handle, platform_channel_->PassServerHandle(), child_token_); |
146 // that |handle| is still valid. | 92 |
147 base::WaitableEvent signal(base::WaitableEvent::ResetPolicy::MANUAL, | 93 main_task_runner_->PostTask( |
148 base::WaitableEvent::InitialState::NOT_SIGNALED); | 94 FROM_HERE, |
149 background_service_manager_->ExecuteOnServiceManagerThread( | 95 base::Bind(&MojoTestState::SetPID, weak_factory_.GetWeakPtr(), pid)); |
150 base::Bind(&MojoTestState::ChildProcessLaunchedOnBackgroundThread, | |
151 base::Unretained(this), handle, pid, &signal)); | |
152 signal.Wait(); | |
153 } | 96 } |
154 | 97 |
155 void ChildProcessLaunchedOnBackgroundThread( | 98 // Called on the main thread only. |
156 base::ProcessHandle handle, | 99 void SetPID(base::ProcessId pid) { |
157 base::ProcessId pid, | 100 DCHECK(pid_receiver_.is_bound()); |
158 base::WaitableEvent* signal, | 101 pid_receiver_->SetPID(pid); |
159 service_manager::ServiceManager* service_manager) { | 102 pid_receiver_.reset(); |
160 background_state_->ChildProcessLaunched(handle, pid); | |
161 signal->Signal(); | |
162 } | 103 } |
163 | 104 |
164 void BindOnBackgroundThread( | 105 const std::string child_token_; |
165 base::WaitableEvent* signal, | 106 service_manager::BackgroundServiceManager* const background_service_manager_; |
166 base::CommandLine* command_line, | |
167 base::TestLauncher::LaunchOptions* test_launch_options, | |
168 service_manager::ServiceManager* service_manager) { | |
169 background_state_.reset(new BackgroundTestState); | |
170 background_state_->Connect(command_line, service_manager, | |
171 test_launch_options); | |
172 signal->Signal(); | |
173 } | |
174 | 107 |
175 service_manager::BackgroundServiceManager* background_service_manager_; | 108 // NOTE: HandlePassingInformation must remain valid through process launch, |
176 std::unique_ptr<BackgroundTestState> background_state_; | 109 // hence it lives here instead of within Init()'s stack. |
| 110 mojo::edk::HandlePassingInformation handle_passing_info_; |
| 111 |
| 112 std::unique_ptr<mojo::edk::PlatformChannelPair> platform_channel_; |
| 113 service_manager::mojom::PIDReceiverPtr pid_receiver_; |
| 114 scoped_refptr<base::TaskRunner> main_task_runner_ = nullptr; |
| 115 |
| 116 base::WeakPtrFactory<MojoTestState> weak_factory_; |
177 | 117 |
178 DISALLOW_COPY_AND_ASSIGN(MojoTestState); | 118 DISALLOW_COPY_AND_ASSIGN(MojoTestState); |
179 }; | 119 }; |
180 | 120 |
181 // The name in the manifest results in getting exe:mash_browser_tests used, | 121 // The name in the manifest results in getting exe:mash_browser_tests used, |
182 // remap that to browser_tests. | 122 // remap that to browser_tests. |
183 void RemoveMashFromBrowserTests(base::CommandLine* command_line) { | 123 void RemoveMashFromBrowserTests(base::CommandLine* command_line) { |
184 base::FilePath exe_path(command_line->GetProgram()); | 124 base::FilePath exe_path(command_line->GetProgram()); |
185 #if defined(OS_WIN) | 125 #if defined(OS_WIN) |
186 exe_path = exe_path.DirName().Append(FILE_PATH_LITERAL("browser_tests.exe")); | 126 exe_path = exe_path.DirName().Append(FILE_PATH_LITERAL("browser_tests.exe")); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 } | 164 } |
225 | 165 |
226 DISALLOW_COPY_AND_ASSIGN(ServiceProcessLauncherDelegateImpl); | 166 DISALLOW_COPY_AND_ASSIGN(ServiceProcessLauncherDelegateImpl); |
227 }; | 167 }; |
228 | 168 |
229 // static | 169 // static |
230 const char MojoTestConnector::kTestSwitch[] = "is_test"; | 170 const char MojoTestConnector::kTestSwitch[] = "is_test"; |
231 // static | 171 // static |
232 const char MojoTestConnector::kMashApp[] = "mash-app"; | 172 const char MojoTestConnector::kMashApp[] = "mash-app"; |
233 | 173 |
234 MojoTestConnector::MojoTestConnector() {} | 174 MojoTestConnector::MojoTestConnector( |
| 175 std::unique_ptr<base::Value> catalog_contents) |
| 176 : service_process_launcher_delegate_( |
| 177 new ServiceProcessLauncherDelegateImpl), |
| 178 background_service_manager_(service_process_launcher_delegate_.get(), |
| 179 std::move(catalog_contents)) {} |
235 | 180 |
236 service_manager::mojom::ServiceRequest MojoTestConnector::Init() { | 181 service_manager::mojom::ServiceRequest MojoTestConnector::Init() { |
237 service_process_launcher_delegate_ = | 182 // In single-process test mode, browser code will initialize the EDK and IPC. |
238 base::MakeUnique<ServiceProcessLauncherDelegateImpl>(); | 183 // Otherwise we ensure it's initialized here. |
| 184 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 185 content::kSingleProcessTestsFlag)) { |
| 186 mojo::edk::Init(); |
| 187 ipc_thread_ = base::MakeUnique<base::Thread>("IPC thread"); |
| 188 ipc_thread_->StartWithOptions(base::Thread::Options( |
| 189 base::MessageLoop::TYPE_IO, 0)); |
| 190 ipc_support_ = base::MakeUnique<mojo::edk::ScopedIPCSupport>( |
| 191 ipc_thread_->task_runner(), |
| 192 mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST); |
| 193 } |
239 | 194 |
240 std::unique_ptr<service_manager::BackgroundServiceManager::InitParams> | 195 service_manager::mojom::ServicePtr service; |
241 init_params = base::MakeUnique< | 196 service_manager::mojom::ServiceRequest request(&service); |
242 service_manager::BackgroundServiceManager::InitParams>(); | 197 background_service_manager_.RegisterService( |
243 // When running in single_process mode chrome initializes the edk. | 198 service_manager::Identity(kTestRunnerName, |
244 init_params->init_edk = !base::CommandLine::ForCurrentProcess()->HasSwitch( | 199 service_manager::mojom::kRootUserID), |
245 content::kSingleProcessTestsFlag); | 200 std::move(service), nullptr); |
246 init_params->service_process_launcher_delegate = | 201 return request; |
247 service_process_launcher_delegate_.get(); | |
248 background_service_manager_.Init(std::move(init_params)); | |
249 return background_service_manager_.CreateServiceRequest(kTestRunnerName); | |
250 } | 202 } |
251 | 203 |
252 MojoTestConnector::~MojoTestConnector() {} | 204 MojoTestConnector::~MojoTestConnector() {} |
253 | 205 |
254 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( | 206 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( |
255 base::CommandLine* command_line, | 207 base::CommandLine* command_line, |
256 base::TestLauncher::LaunchOptions* test_launch_options) { | 208 base::TestLauncher::LaunchOptions* test_launch_options) { |
257 std::unique_ptr<MojoTestState> test_state( | 209 auto test_state = |
258 new MojoTestState(&background_service_manager_)); | 210 base::MakeUnique<MojoTestState>(&background_service_manager_); |
259 test_state->Init(command_line, test_launch_options); | 211 test_state->Init(command_line, test_launch_options); |
260 return std::move(test_state); | 212 return test_state; |
261 } | 213 } |
OLD | NEW |