| 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 const std::string& mus_config_switch) { |
| 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( |
| 156 const std::string& test_runner_name) |
| 157 : test_runner_name_(test_runner_name) {} |
| 154 ~ServiceProcessLauncherDelegateImpl() override {} | 158 ~ServiceProcessLauncherDelegateImpl() override {} |
| 155 | 159 |
| 156 private: | 160 private: |
| 157 // service_manager::ServiceProcessLauncherDelegate: | 161 // service_manager::ServiceProcessLauncherDelegate: |
| 158 void AdjustCommandLineArgumentsForTarget( | 162 void AdjustCommandLineArgumentsForTarget( |
| 159 const service_manager::Identity& target, | 163 const service_manager::Identity& target, |
| 160 base::CommandLine* command_line) override { | 164 base::CommandLine* command_line) override { |
| 161 if (target.name() != content::mojom::kPackagedServicesServiceName) { | 165 if (target.name() != content::mojom::kPackagedServicesServiceName) { |
| 162 if (target.name() == kTestRunnerName) { | 166 if (target.name() == test_runner_name_) { |
| 163 RemoveMashFromBrowserTests(command_line); | 167 RemoveMashFromBrowserTests(command_line); |
| 164 command_line->SetProgram( | 168 command_line->SetProgram( |
| 165 base::CommandLine::ForCurrentProcess()->GetProgram()); | 169 base::CommandLine::ForCurrentProcess()->GetProgram()); |
| 166 } | 170 } |
| 167 command_line->AppendSwitch(MojoTestConnector::kMashApp); | 171 command_line->AppendSwitch(MojoTestConnector::kMashApp); |
| 168 return; | 172 return; |
| 169 } | 173 } |
| 170 | 174 |
| 171 base::CommandLine::StringVector argv(command_line->argv()); | 175 base::CommandLine::StringVector argv(command_line->argv()); |
| 172 auto iter = | 176 auto iter = |
| 173 std::find(argv.begin(), argv.end(), FILE_PATH_LITERAL("--run-in-mash")); | 177 std::find(argv.begin(), argv.end(), FILE_PATH_LITERAL("--run-in-mash")); |
| 174 if (iter != argv.end()) | 178 if (iter != argv.end()) |
| 175 argv.erase(iter); | 179 argv.erase(iter); |
| 176 *command_line = base::CommandLine(argv); | 180 *command_line = base::CommandLine(argv); |
| 177 } | 181 } |
| 178 | 182 |
| 183 const std::string test_runner_name_; |
| 184 |
| 179 DISALLOW_COPY_AND_ASSIGN(ServiceProcessLauncherDelegateImpl); | 185 DISALLOW_COPY_AND_ASSIGN(ServiceProcessLauncherDelegateImpl); |
| 180 }; | 186 }; |
| 181 | 187 |
| 182 // static | 188 // static |
| 183 const char MojoTestConnector::kTestSwitch[] = "is_test"; | 189 const char MojoTestConnector::kTestSwitch[] = "is_test"; |
| 184 // static | 190 // static |
| 185 const char MojoTestConnector::kMashApp[] = "mash-app"; | 191 const char MojoTestConnector::kMashApp[] = "mash-app"; |
| 186 | 192 |
| 187 MojoTestConnector::MojoTestConnector( | 193 MojoTestConnector::MojoTestConnector( |
| 188 std::unique_ptr<base::Value> catalog_contents) | 194 std::unique_ptr<base::Value> catalog_contents, |
| 189 : service_process_launcher_delegate_( | 195 Config config) |
| 190 new ServiceProcessLauncherDelegateImpl), | 196 : config_(config), |
| 197 service_process_launcher_delegate_(new ServiceProcessLauncherDelegateImpl( |
| 198 config == MojoTestConnector::Config::MASH ? kMashTestRunnerName |
| 199 : kMusTestRunnerName)), |
| 191 background_service_manager_(nullptr), | 200 background_service_manager_(nullptr), |
| 192 catalog_contents_(std::move(catalog_contents)) {} | 201 catalog_contents_(std::move(catalog_contents)) {} |
| 193 | 202 |
| 194 service_manager::mojom::ServiceRequest MojoTestConnector::Init() { | 203 service_manager::mojom::ServiceRequest MojoTestConnector::Init() { |
| 195 // In single-process test mode, browser code will initialize the EDK and IPC. | 204 // In single-process test mode, browser code will initialize the EDK and IPC. |
| 196 // Otherwise we ensure it's initialized here. | 205 // Otherwise we ensure it's initialized here. |
| 197 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | 206 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 198 content::kSingleProcessTestsFlag)) { | 207 content::kSingleProcessTestsFlag)) { |
| 199 mojo::edk::Init(); | 208 mojo::edk::Init(); |
| 200 ipc_thread_ = base::MakeUnique<base::Thread>("IPC thread"); | 209 ipc_thread_ = base::MakeUnique<base::Thread>("IPC thread"); |
| 201 ipc_thread_->StartWithOptions(base::Thread::Options( | 210 ipc_thread_->StartWithOptions(base::Thread::Options( |
| 202 base::MessageLoop::TYPE_IO, 0)); | 211 base::MessageLoop::TYPE_IO, 0)); |
| 203 ipc_support_ = base::MakeUnique<mojo::edk::ScopedIPCSupport>( | 212 ipc_support_ = base::MakeUnique<mojo::edk::ScopedIPCSupport>( |
| 204 ipc_thread_->task_runner(), | 213 ipc_thread_->task_runner(), |
| 205 mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST); | 214 mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST); |
| 206 } | 215 } |
| 207 | 216 |
| 208 service_manager::mojom::ServicePtr service; | 217 service_manager::mojom::ServicePtr service; |
| 209 service_manager::mojom::ServiceRequest request(&service); | 218 service_manager::mojom::ServiceRequest request(&service); |
| 210 | 219 |
| 211 // BackgroundServiceManager must be created after mojo::edk::Init() as it | 220 // BackgroundServiceManager must be created after mojo::edk::Init() as it |
| 212 // attempts to create mojo pipes for the provided catalog on a separate | 221 // attempts to create mojo pipes for the provided catalog on a separate |
| 213 // thread. | 222 // thread. |
| 214 background_service_manager_ = | 223 background_service_manager_ = |
| 215 base::MakeUnique<service_manager::BackgroundServiceManager>( | 224 base::MakeUnique<service_manager::BackgroundServiceManager>( |
| 216 service_process_launcher_delegate_.get(), | 225 service_process_launcher_delegate_.get(), |
| 217 std::move(catalog_contents_)); | 226 std::move(catalog_contents_)); |
| 218 background_service_manager_->RegisterService( | 227 background_service_manager_->RegisterService( |
| 219 service_manager::Identity(kTestRunnerName, | 228 service_manager::Identity(config_ == MojoTestConnector::Config::MASH |
| 229 ? kMashTestRunnerName |
| 230 : kMusTestRunnerName, |
| 220 service_manager::mojom::kRootUserID), | 231 service_manager::mojom::kRootUserID), |
| 221 std::move(service), nullptr); | 232 std::move(service), nullptr); |
| 222 return request; | 233 return request; |
| 223 } | 234 } |
| 224 | 235 |
| 225 MojoTestConnector::~MojoTestConnector() {} | 236 MojoTestConnector::~MojoTestConnector() {} |
| 226 | 237 |
| 227 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( | 238 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( |
| 228 base::CommandLine* command_line, | 239 base::CommandLine* command_line, |
| 229 base::TestLauncher::LaunchOptions* test_launch_options) { | 240 base::TestLauncher::LaunchOptions* test_launch_options) { |
| 230 auto test_state = | 241 auto test_state = |
| 231 base::MakeUnique<MojoTestState>(background_service_manager_.get()); | 242 base::MakeUnique<MojoTestState>(background_service_manager_.get()); |
| 232 test_state->Init(command_line, test_launch_options); | 243 test_state->Init(command_line, test_launch_options, |
| 244 config_ == MojoTestConnector::Config::MASH ? switches::kMash |
| 245 : switches::kMus); |
| 233 return test_state; | 246 return test_state; |
| 234 } | 247 } |
| OLD | NEW |