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