Chromium Code Reviews| 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" |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 29 #include "services/service_manager/public/cpp/service.h" | 29 #include "services/service_manager/public/cpp/service.h" |
| 30 #include "services/service_manager/public/cpp/service_context.h" | 30 #include "services/service_manager/public/cpp/service_context.h" |
| 31 #include "services/service_manager/runner/common/client_util.h" | 31 #include "services/service_manager/runner/common/client_util.h" |
| 32 #include "services/service_manager/runner/common/switches.h" | 32 #include "services/service_manager/runner/common/switches.h" |
| 33 #include "services/service_manager/runner/host/service_process_launcher.h" | 33 #include "services/service_manager/runner/host/service_process_launcher.h" |
| 34 #include "services/service_manager/service_manager.h" | 34 #include "services/service_manager/service_manager.h" |
| 35 #include "services/service_manager/switches.h" | 35 #include "services/service_manager/switches.h" |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 const char kTestRunnerName[] = "mash_browser_tests"; | 39 const char kMashTestRunnerName[] = "mash_browser_tests"; |
| 40 const char kMusTestRunnerName[] = "mus_browser_tests"; | |
| 40 | 41 |
| 41 // State created per test to register a client process with the background | 42 // State created per test to register a client process with the background |
| 42 // service manager. | 43 // service manager. |
| 43 class MojoTestState : public content::TestState { | 44 class MojoTestState : public content::TestState { |
| 44 public: | 45 public: |
| 45 explicit MojoTestState( | 46 explicit MojoTestState( |
| 46 service_manager::BackgroundServiceManager* background_service_manager) | 47 service_manager::BackgroundServiceManager* background_service_manager) |
| 47 : background_service_manager_(background_service_manager), | 48 : background_service_manager_(background_service_manager), |
| 48 weak_factory_(this) {} | 49 weak_factory_(this) {} |
| 49 ~MojoTestState() override {} | 50 ~MojoTestState() override {} |
| 50 | 51 |
| 51 void Init(base::CommandLine* command_line, | 52 void Init(base::CommandLine* command_line, |
| 52 base::TestLauncher::LaunchOptions* test_launch_options) { | 53 base::TestLauncher::LaunchOptions* test_launch_options, |
| 54 std::string mus_config_switch) { | |
|
sky
2017/04/20 20:52:58
const std::string&
jonross
2017/04/21 14:04:21
Done.
| |
| 53 command_line->AppendSwitch(MojoTestConnector::kTestSwitch); | 55 command_line->AppendSwitch(MojoTestConnector::kTestSwitch); |
| 54 command_line->AppendSwitch(switches::kChildProcess); | 56 command_line->AppendSwitch(switches::kChildProcess); |
| 55 command_line->AppendSwitchASCII(switches::kMusConfig, switches::kMash); | 57 command_line->AppendSwitchASCII(switches::kMusConfig, mus_config_switch); |
| 56 | 58 |
| 57 platform_channel_ = base::MakeUnique<mojo::edk::PlatformChannelPair>(); | 59 platform_channel_ = base::MakeUnique<mojo::edk::PlatformChannelPair>(); |
| 58 | 60 |
| 59 platform_channel_->PrepareToPassClientHandleToChildProcess( | 61 platform_channel_->PrepareToPassClientHandleToChildProcess( |
| 60 command_line, &handle_passing_info_); | 62 command_line, &handle_passing_info_); |
| 61 #if defined(OS_WIN) | 63 #if defined(OS_WIN) |
| 62 test_launch_options->inherit_handles = true; | 64 test_launch_options->inherit_handles = true; |
| 63 test_launch_options->handles_to_inherit = &handle_passing_info_; | 65 test_launch_options->handles_to_inherit = &handle_passing_info_; |
| 64 #if defined(OFFICIAL_BUILD) | 66 #if defined(OFFICIAL_BUILD) |
| 65 CHECK(false) << "Launching mojo process with inherit_handles is insecure!"; | 67 CHECK(false) << "Launching mojo process with inherit_handles is insecure!"; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 command_line->SetProgram(exe_path); | 145 command_line->SetProgram(exe_path); |
| 144 } | 146 } |
| 145 | 147 |
| 146 } // namespace | 148 } // namespace |
| 147 | 149 |
| 148 // ServiceProcessLauncher::Delegate that makes exe:mash_browser_tests to | 150 // ServiceProcessLauncher::Delegate that makes exe:mash_browser_tests to |
| 149 // exe:browser_tests and removes '--run-in-mash'. | 151 // exe:browser_tests and removes '--run-in-mash'. |
| 150 class MojoTestConnector::ServiceProcessLauncherDelegateImpl | 152 class MojoTestConnector::ServiceProcessLauncherDelegateImpl |
| 151 : public service_manager::ServiceProcessLauncher::Delegate { | 153 : public service_manager::ServiceProcessLauncher::Delegate { |
| 152 public: | 154 public: |
| 153 ServiceProcessLauncherDelegateImpl() {} | 155 explicit ServiceProcessLauncherDelegateImpl(std::string test_runner_name) |
|
sky
2017/04/20 20:52:58
const std::string&
jonross
2017/04/21 14:04:21
Done.
| |
| 156 : test_runner_name_(test_runner_name) {} | |
| 154 ~ServiceProcessLauncherDelegateImpl() override {} | 157 ~ServiceProcessLauncherDelegateImpl() override {} |
| 155 | 158 |
| 156 private: | 159 private: |
| 157 // service_manager::ServiceProcessLauncherDelegate: | 160 // service_manager::ServiceProcessLauncherDelegate: |
| 158 void AdjustCommandLineArgumentsForTarget( | 161 void AdjustCommandLineArgumentsForTarget( |
| 159 const service_manager::Identity& target, | 162 const service_manager::Identity& target, |
| 160 base::CommandLine* command_line) override { | 163 base::CommandLine* command_line) override { |
| 161 if (target.name() != content::mojom::kPackagedServicesServiceName) { | 164 if (target.name() != content::mojom::kPackagedServicesServiceName) { |
| 162 if (target.name() == kTestRunnerName) { | 165 if (target.name() == test_runner_name_) { |
| 163 RemoveMashFromBrowserTests(command_line); | 166 RemoveMashFromBrowserTests(command_line); |
| 164 command_line->SetProgram( | 167 command_line->SetProgram( |
| 165 base::CommandLine::ForCurrentProcess()->GetProgram()); | 168 base::CommandLine::ForCurrentProcess()->GetProgram()); |
| 166 } | 169 } |
| 167 command_line->AppendSwitch(MojoTestConnector::kMashApp); | 170 command_line->AppendSwitch(MojoTestConnector::kMashApp); |
| 168 return; | 171 return; |
| 169 } | 172 } |
| 170 | 173 |
| 171 base::CommandLine::StringVector argv(command_line->argv()); | 174 base::CommandLine::StringVector argv(command_line->argv()); |
| 172 auto iter = | 175 auto iter = |
| 173 std::find(argv.begin(), argv.end(), FILE_PATH_LITERAL("--run-in-mash")); | 176 std::find(argv.begin(), argv.end(), FILE_PATH_LITERAL("--run-in-mash")); |
| 174 if (iter != argv.end()) | 177 if (iter != argv.end()) |
| 175 argv.erase(iter); | 178 argv.erase(iter); |
| 176 *command_line = base::CommandLine(argv); | 179 *command_line = base::CommandLine(argv); |
| 177 } | 180 } |
| 178 | 181 |
| 182 std::string test_runner_name_; | |
|
sky
2017/04/20 20:52:58
const
jonross
2017/04/21 14:04:21
Done.
| |
| 183 | |
| 179 DISALLOW_COPY_AND_ASSIGN(ServiceProcessLauncherDelegateImpl); | 184 DISALLOW_COPY_AND_ASSIGN(ServiceProcessLauncherDelegateImpl); |
| 180 }; | 185 }; |
| 181 | 186 |
| 182 // static | 187 // static |
| 183 const char MojoTestConnector::kTestSwitch[] = "is_test"; | 188 const char MojoTestConnector::kTestSwitch[] = "is_test"; |
| 184 // static | 189 // static |
| 185 const char MojoTestConnector::kMashApp[] = "mash-app"; | 190 const char MojoTestConnector::kMashApp[] = "mash-app"; |
| 186 | 191 |
| 187 MojoTestConnector::MojoTestConnector( | 192 MojoTestConnector::MojoTestConnector( |
| 188 std::unique_ptr<base::Value> catalog_contents) | 193 std::unique_ptr<base::Value> catalog_contents, |
| 189 : service_process_launcher_delegate_( | 194 Config config) |
| 190 new ServiceProcessLauncherDelegateImpl), | 195 : config_(config), |
| 196 service_process_launcher_delegate_(new ServiceProcessLauncherDelegateImpl( | |
| 197 config == MojoTestConnector::Config::MASH ? kMashTestRunnerName | |
| 198 : kMusTestRunnerName)), | |
| 191 background_service_manager_(nullptr), | 199 background_service_manager_(nullptr), |
| 192 catalog_contents_(std::move(catalog_contents)) {} | 200 catalog_contents_(std::move(catalog_contents)) {} |
| 193 | 201 |
| 194 service_manager::mojom::ServiceRequest MojoTestConnector::Init() { | 202 service_manager::mojom::ServiceRequest MojoTestConnector::Init() { |
| 195 // In single-process test mode, browser code will initialize the EDK and IPC. | 203 // In single-process test mode, browser code will initialize the EDK and IPC. |
| 196 // Otherwise we ensure it's initialized here. | 204 // Otherwise we ensure it's initialized here. |
| 197 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | 205 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 198 content::kSingleProcessTestsFlag)) { | 206 content::kSingleProcessTestsFlag)) { |
| 199 mojo::edk::Init(); | 207 mojo::edk::Init(); |
| 200 ipc_thread_ = base::MakeUnique<base::Thread>("IPC thread"); | 208 ipc_thread_ = base::MakeUnique<base::Thread>("IPC thread"); |
| 201 ipc_thread_->StartWithOptions(base::Thread::Options( | 209 ipc_thread_->StartWithOptions(base::Thread::Options( |
| 202 base::MessageLoop::TYPE_IO, 0)); | 210 base::MessageLoop::TYPE_IO, 0)); |
| 203 ipc_support_ = base::MakeUnique<mojo::edk::ScopedIPCSupport>( | 211 ipc_support_ = base::MakeUnique<mojo::edk::ScopedIPCSupport>( |
| 204 ipc_thread_->task_runner(), | 212 ipc_thread_->task_runner(), |
| 205 mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST); | 213 mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST); |
| 206 } | 214 } |
| 207 | 215 |
| 208 service_manager::mojom::ServicePtr service; | 216 service_manager::mojom::ServicePtr service; |
| 209 service_manager::mojom::ServiceRequest request(&service); | 217 service_manager::mojom::ServiceRequest request(&service); |
| 210 | 218 |
| 211 // BackgroundServiceManager must be created after mojo::edk::Init() as it | 219 // BackgroundServiceManager must be created after mojo::edk::Init() as it |
| 212 // attempts to create mojo pipes for the provided catalog on a separate | 220 // attempts to create mojo pipes for the provided catalog on a separate |
| 213 // thread. | 221 // thread. |
| 214 background_service_manager_ = | 222 background_service_manager_ = |
| 215 base::MakeUnique<service_manager::BackgroundServiceManager>( | 223 base::MakeUnique<service_manager::BackgroundServiceManager>( |
| 216 service_process_launcher_delegate_.get(), | 224 service_process_launcher_delegate_.get(), |
| 217 std::move(catalog_contents_)); | 225 std::move(catalog_contents_)); |
| 218 background_service_manager_->RegisterService( | 226 background_service_manager_->RegisterService( |
| 219 service_manager::Identity(kTestRunnerName, | 227 service_manager::Identity(config_ == MojoTestConnector::Config::MASH |
| 228 ? kMashTestRunnerName | |
| 229 : kMusTestRunnerName, | |
| 220 service_manager::mojom::kRootUserID), | 230 service_manager::mojom::kRootUserID), |
| 221 std::move(service), nullptr); | 231 std::move(service), nullptr); |
| 222 return request; | 232 return request; |
| 223 } | 233 } |
| 224 | 234 |
| 225 MojoTestConnector::~MojoTestConnector() {} | 235 MojoTestConnector::~MojoTestConnector() {} |
| 226 | 236 |
| 227 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( | 237 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( |
| 228 base::CommandLine* command_line, | 238 base::CommandLine* command_line, |
| 229 base::TestLauncher::LaunchOptions* test_launch_options) { | 239 base::TestLauncher::LaunchOptions* test_launch_options) { |
| 230 auto test_state = | 240 auto test_state = |
| 231 base::MakeUnique<MojoTestState>(background_service_manager_.get()); | 241 base::MakeUnique<MojoTestState>(background_service_manager_.get()); |
| 232 test_state->Init(command_line, test_launch_options); | 242 test_state->Init(command_line, test_launch_options, |
| 243 config_ == MojoTestConnector::Config::MASH ? switches::kMash | |
| 244 : switches::kMus); | |
| 233 return test_state; | 245 return test_state; |
| 234 } | 246 } |
| OLD | NEW |