| 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 29 matching lines...) Expand all Loading... |
| 40 const char kMashTestRunnerName[] = "mash_browser_tests"; | 40 const char kMashTestRunnerName[] = "mash_browser_tests"; |
| 41 const char kMusTestRunnerName[] = "mus_browser_tests"; | 41 const char kMusTestRunnerName[] = "mus_browser_tests"; |
| 42 | 42 |
| 43 // State created per test to register a client process with the background | 43 // State created per test to register a client process with the background |
| 44 // service manager. | 44 // service manager. |
| 45 class MojoTestState : public content::TestState { | 45 class MojoTestState : public content::TestState { |
| 46 public: | 46 public: |
| 47 MojoTestState(MojoTestConnector* connector, | 47 MojoTestState(MojoTestConnector* connector, |
| 48 base::CommandLine* command_line, | 48 base::CommandLine* command_line, |
| 49 base::TestLauncher::LaunchOptions* test_launch_options, | 49 base::TestLauncher::LaunchOptions* test_launch_options, |
| 50 const std::string& mus_config_switch, | 50 const std::string& mus_config_switch) |
| 51 base::OnceClosure on_process_launched) | |
| 52 : connector_(connector), | 51 : connector_(connector), |
| 52 background_service_manager_(nullptr), |
| 53 platform_channel_(base::MakeUnique<mojo::edk::PlatformChannelPair>()), | 53 platform_channel_(base::MakeUnique<mojo::edk::PlatformChannelPair>()), |
| 54 main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 54 main_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 55 on_process_launched_(std::move(on_process_launched)), | |
| 56 weak_factory_(this) { | 55 weak_factory_(this) { |
| 57 command_line->AppendSwitch(MojoTestConnector::kTestSwitch); | 56 command_line->AppendSwitch(MojoTestConnector::kTestSwitch); |
| 58 command_line->AppendSwitchASCII(switches::kMusConfig, mus_config_switch); | 57 command_line->AppendSwitchASCII(switches::kMusConfig, mus_config_switch); |
| 59 | 58 |
| 60 platform_channel_->PrepareToPassClientHandleToChildProcess( | 59 platform_channel_->PrepareToPassClientHandleToChildProcess( |
| 61 command_line, &handle_passing_info_); | 60 command_line, &handle_passing_info_); |
| 62 #if defined(OS_WIN) | 61 #if defined(OS_WIN) |
| 63 test_launch_options->inherit_handles = true; | 62 test_launch_options->inherit_handles = true; |
| 64 test_launch_options->handles_to_inherit = &handle_passing_info_; | 63 test_launch_options->handles_to_inherit = &handle_passing_info_; |
| 65 #if defined(OFFICIAL_BUILD) | 64 #if defined(OFFICIAL_BUILD) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 92 main_task_runner_->PostTask(FROM_HERE, | 91 main_task_runner_->PostTask(FROM_HERE, |
| 93 base::Bind(&MojoTestState::SetupService, | 92 base::Bind(&MojoTestState::SetupService, |
| 94 weak_factory_.GetWeakPtr(), pid)); | 93 weak_factory_.GetWeakPtr(), pid)); |
| 95 } | 94 } |
| 96 | 95 |
| 97 // Called on the main thread only. | 96 // Called on the main thread only. |
| 98 // This registers the services needed for the test. This is not done until | 97 // This registers the services needed for the test. This is not done until |
| 99 // after ChildProcessLaunched as previous test runs will tear down existing | 98 // after ChildProcessLaunched as previous test runs will tear down existing |
| 100 // connections. | 99 // connections. |
| 101 void SetupService(base::ProcessId pid) { | 100 void SetupService(base::ProcessId pid) { |
| 102 connector_->InitBackgroundServiceManager(); | 101 service_manager::mojom::ServicePtr service; |
| 103 service_manager::BackgroundServiceManager* background_service_manager = | 102 auto request = mojo::MakeRequest(&service); |
| 104 connector_->background_service_manager(); | 103 |
| 105 background_service_manager->RegisterService( | 104 // BackgroundServiceManager must be created after mojo::edk::Init() as it |
| 105 // attempts to create mojo pipes for the provided catalog on a separate |
| 106 // thread. |
| 107 background_service_manager_ = |
| 108 connector_->CreateBackgroundServiceManager(std::move(service)); |
| 109 background_service_manager_->RegisterService( |
| 106 service_manager::Identity(content::mojom::kPackagedServicesServiceName, | 110 service_manager::Identity(content::mojom::kPackagedServicesServiceName, |
| 107 service_manager::mojom::kRootUserID), | 111 service_manager::mojom::kRootUserID), |
| 108 std::move(service_), mojo::MakeRequest(&pid_receiver_)); | 112 std::move(service_), mojo::MakeRequest(&pid_receiver_)); |
| 109 | 113 |
| 110 DCHECK(pid_receiver_.is_bound()); | 114 DCHECK(pid_receiver_.is_bound()); |
| 111 pid_receiver_->SetPID(pid); | 115 pid_receiver_->SetPID(pid); |
| 112 pid_receiver_.reset(); | 116 pid_receiver_.reset(); |
| 113 | 117 |
| 114 std::move(on_process_launched_).Run(); | 118 background_service_manager_->StartService( |
| 119 service_manager::Identity(mash::session::mojom::kServiceName, |
| 120 service_manager::mojom::kRootUserID)); |
| 115 } | 121 } |
| 116 | 122 |
| 117 mojo::edk::OutgoingBrokerClientInvitation broker_client_invitation_; | 123 mojo::edk::OutgoingBrokerClientInvitation broker_client_invitation_; |
| 118 MojoTestConnector* connector_; | 124 MojoTestConnector* connector_; |
| 125 std::unique_ptr<service_manager::BackgroundServiceManager> |
| 126 background_service_manager_; |
| 119 | 127 |
| 120 // The ServicePtr must be created before child process launch so that the pipe | 128 // The ServicePtr must be created before child process launch so that the pipe |
| 121 // can be set on the command line. It is held until SetupService is called at | 129 // can be set on the command line. It is held until SetupService is called at |
| 122 // which point |background_service_manager_| takes over ownership. | 130 // which point |background_service_manager_| takes over ownership. |
| 123 service_manager::mojom::ServicePtr service_; | 131 service_manager::mojom::ServicePtr service_; |
| 124 | 132 |
| 125 // NOTE: HandlePassingInformation must remain valid through process launch, | 133 // NOTE: HandlePassingInformation must remain valid through process launch, |
| 126 // hence it lives here instead of within Init()'s stack. | 134 // hence it lives here instead of within Init()'s stack. |
| 127 mojo::edk::HandlePassingInformation handle_passing_info_; | 135 mojo::edk::HandlePassingInformation handle_passing_info_; |
| 128 | 136 |
| 129 std::unique_ptr<mojo::edk::PlatformChannelPair> platform_channel_; | 137 std::unique_ptr<mojo::edk::PlatformChannelPair> platform_channel_; |
| 130 service_manager::mojom::PIDReceiverPtr pid_receiver_; | 138 service_manager::mojom::PIDReceiverPtr pid_receiver_; |
| 131 const scoped_refptr<base::TaskRunner> main_task_runner_; | 139 const scoped_refptr<base::TaskRunner> main_task_runner_; |
| 132 base::OnceClosure on_process_launched_; | |
| 133 | 140 |
| 134 base::WeakPtrFactory<MojoTestState> weak_factory_; | 141 base::WeakPtrFactory<MojoTestState> weak_factory_; |
| 135 | 142 |
| 136 DISALLOW_COPY_AND_ASSIGN(MojoTestState); | 143 DISALLOW_COPY_AND_ASSIGN(MojoTestState); |
| 137 }; | 144 }; |
| 138 | 145 |
| 139 // The name in the manifest results in getting exe:mash_browser_tests used, | 146 // The name in the manifest results in getting exe:mash_browser_tests used, |
| 140 // remap that to browser_tests. | 147 // remap that to browser_tests. |
| 141 void RemoveMashFromBrowserTests(base::CommandLine* command_line) { | 148 void RemoveMashFromBrowserTests(base::CommandLine* command_line) { |
| 142 base::FilePath exe_path(command_line->GetProgram()); | 149 base::FilePath exe_path(command_line->GetProgram()); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 ipc_support_ = base::MakeUnique<mojo::edk::ScopedIPCSupport>( | 222 ipc_support_ = base::MakeUnique<mojo::edk::ScopedIPCSupport>( |
| 216 ipc_thread_->task_runner(), | 223 ipc_thread_->task_runner(), |
| 217 mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST); | 224 mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST); |
| 218 } | 225 } |
| 219 } | 226 } |
| 220 | 227 |
| 221 service_manager::mojom::ServiceRequest | 228 service_manager::mojom::ServiceRequest |
| 222 MojoTestConnector::InitBackgroundServiceManager() { | 229 MojoTestConnector::InitBackgroundServiceManager() { |
| 223 service_manager::mojom::ServicePtr service; | 230 service_manager::mojom::ServicePtr service; |
| 224 auto request = mojo::MakeRequest(&service); | 231 auto request = mojo::MakeRequest(&service); |
| 232 background_service_manager_ = |
| 233 CreateBackgroundServiceManager(std::move(service)); |
| 234 return request; |
| 235 } |
| 225 | 236 |
| 237 std::unique_ptr<service_manager::BackgroundServiceManager> |
| 238 MojoTestConnector::CreateBackgroundServiceManager( |
| 239 service_manager::mojom::ServicePtr service) { |
| 226 // BackgroundServiceManager must be created after mojo::edk::Init() as it | 240 // BackgroundServiceManager must be created after mojo::edk::Init() as it |
| 227 // attempts to create mojo pipes for the provided catalog on a separate | 241 // attempts to create mojo pipes for the provided catalog on a separate |
| 228 // thread. | 242 // thread. |
| 229 background_service_manager_ = | 243 std::unique_ptr<service_manager::BackgroundServiceManager> |
| 230 base::MakeUnique<service_manager::BackgroundServiceManager>( | 244 background_service_manager = |
| 231 service_process_launcher_delegate_.get(), | 245 base::MakeUnique<service_manager::BackgroundServiceManager>( |
| 232 catalog_contents_->CreateDeepCopy()); | 246 service_process_launcher_delegate_.get(), |
| 233 background_service_manager_->RegisterService( | 247 catalog_contents_->CreateDeepCopy()); |
| 248 background_service_manager->RegisterService( |
| 234 service_manager::Identity(config_ == MojoTestConnector::Config::MASH | 249 service_manager::Identity(config_ == MojoTestConnector::Config::MASH |
| 235 ? kMashTestRunnerName | 250 ? kMashTestRunnerName |
| 236 : kMusTestRunnerName, | 251 : kMusTestRunnerName, |
| 237 service_manager::mojom::kRootUserID), | 252 service_manager::mojom::kRootUserID), |
| 238 std::move(service), nullptr); | 253 std::move(service), nullptr); |
| 239 return request; | 254 |
| 255 return background_service_manager; |
| 240 } | 256 } |
| 241 | 257 |
| 242 MojoTestConnector::~MojoTestConnector() {} | 258 MojoTestConnector::~MojoTestConnector() {} |
| 243 | 259 |
| 244 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( | 260 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( |
| 245 base::CommandLine* command_line, | 261 base::CommandLine* command_line, |
| 246 base::TestLauncher::LaunchOptions* test_launch_options, | 262 base::TestLauncher::LaunchOptions* test_launch_options) { |
| 247 base::OnceClosure on_process_launched) { | |
| 248 return base::MakeUnique<MojoTestState>( | 263 return base::MakeUnique<MojoTestState>( |
| 249 this, command_line, test_launch_options, | 264 this, command_line, test_launch_options, |
| 250 config_ == MojoTestConnector::Config::MASH ? switches::kMash | 265 config_ == MojoTestConnector::Config::MASH ? switches::kMash |
| 251 : switches::kMus, | 266 : switches::kMus); |
| 252 std::move(on_process_launched)); | |
| 253 } | 267 } |
| 254 | 268 |
| 255 void MojoTestConnector::StartService(const std::string& service_name) { | 269 void MojoTestConnector::StartService(const std::string& service_name) { |
| 256 background_service_manager_->StartService(service_manager::Identity( | 270 background_service_manager_->StartService(service_manager::Identity( |
| 257 service_name, service_manager::mojom::kRootUserID)); | 271 service_name, service_manager::mojom::kRootUserID)); |
| 258 } | 272 } |
| OLD | NEW |