| 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" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
| 16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
| 17 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 18 #include "content/public/common/content_switches.h" | 18 #include "content/public/common/content_switches.h" |
| 19 #include "content/public/common/service_names.mojom.h" | 19 #include "content/public/common/service_names.mojom.h" |
| 20 #include "content/public/test/test_launcher.h" | 20 #include "content/public/test/test_launcher.h" |
| 21 #include "mash/session/public/interfaces/constants.mojom.h" |
| 21 #include "mojo/edk/embedder/embedder.h" | 22 #include "mojo/edk/embedder/embedder.h" |
| 22 #include "mojo/edk/embedder/outgoing_broker_client_invitation.h" | 23 #include "mojo/edk/embedder/outgoing_broker_client_invitation.h" |
| 23 #include "mojo/edk/embedder/platform_channel_pair.h" | 24 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 24 #include "mojo/edk/embedder/scoped_ipc_support.h" | 25 #include "mojo/edk/embedder/scoped_ipc_support.h" |
| 25 #include "mojo/public/cpp/bindings/interface_request.h" | 26 #include "mojo/public/cpp/bindings/interface_request.h" |
| 26 #include "services/catalog/store.h" | 27 #include "services/catalog/store.h" |
| 27 #include "services/service_manager/background/background_service_manager.h" | 28 #include "services/service_manager/background/background_service_manager.h" |
| 28 #include "services/service_manager/public/cpp/connector.h" | 29 #include "services/service_manager/public/cpp/connector.h" |
| 29 #include "services/service_manager/public/cpp/service.h" | 30 #include "services/service_manager/public/cpp/service.h" |
| 30 #include "services/service_manager/public/cpp/service_context.h" | 31 #include "services/service_manager/public/cpp/service_context.h" |
| 31 #include "services/service_manager/runner/common/client_util.h" | 32 #include "services/service_manager/runner/common/client_util.h" |
| 32 #include "services/service_manager/runner/common/switches.h" | 33 #include "services/service_manager/runner/common/switches.h" |
| 33 #include "services/service_manager/runner/host/service_process_launcher.h" | 34 #include "services/service_manager/runner/host/service_process_launcher.h" |
| 34 #include "services/service_manager/service_manager.h" | 35 #include "services/service_manager/service_manager.h" |
| 35 #include "services/service_manager/switches.h" | 36 #include "services/service_manager/switches.h" |
| 36 | 37 |
| 37 namespace { | 38 namespace { |
| 38 | 39 |
| 39 const char kMashTestRunnerName[] = "mash_browser_tests"; | 40 const char kMashTestRunnerName[] = "mash_browser_tests"; |
| 40 const char kMusTestRunnerName[] = "mus_browser_tests"; | 41 const char kMusTestRunnerName[] = "mus_browser_tests"; |
| 41 | 42 |
| 42 // 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 |
| 43 // service manager. | 44 // service manager. |
| 44 class MojoTestState : public content::TestState { | 45 class MojoTestState : public content::TestState { |
| 45 public: | 46 public: |
| 46 explicit MojoTestState( | 47 MojoTestState(MojoTestConnector* connector, |
| 47 service_manager::BackgroundServiceManager* background_service_manager, | 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) |
| 51 base::OnceClosure on_process_launched) | 52 : connector_(connector), |
| 52 : background_service_manager_(background_service_manager), | |
| 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)), | 55 on_process_launched_(std::move(on_process_launched)), |
| 56 weak_factory_(this) { | 56 weak_factory_(this) { |
| 57 command_line->AppendSwitch(MojoTestConnector::kTestSwitch); | 57 command_line->AppendSwitch(MojoTestConnector::kTestSwitch); |
| 58 command_line->AppendSwitchASCII(switches::kMusConfig, mus_config_switch); | 58 command_line->AppendSwitchASCII(switches::kMusConfig, mus_config_switch); |
| 59 | 59 |
| 60 platform_channel_->PrepareToPassClientHandleToChildProcess( | 60 platform_channel_->PrepareToPassClientHandleToChildProcess( |
| 61 command_line, &handle_passing_info_); | 61 command_line, &handle_passing_info_); |
| 62 #if defined(OS_WIN) | 62 #if defined(OS_WIN) |
| (...skipping 29 matching lines...) Expand all Loading... |
| 92 main_task_runner_->PostTask(FROM_HERE, | 92 main_task_runner_->PostTask(FROM_HERE, |
| 93 base::Bind(&MojoTestState::SetupService, | 93 base::Bind(&MojoTestState::SetupService, |
| 94 weak_factory_.GetWeakPtr(), pid)); | 94 weak_factory_.GetWeakPtr(), pid)); |
| 95 } | 95 } |
| 96 | 96 |
| 97 // Called on the main thread only. | 97 // Called on the main thread only. |
| 98 // This registers the services needed for the test. This is not done until | 98 // This registers the services needed for the test. This is not done until |
| 99 // after ChildProcessLaunched as previous test runs will tear down existing | 99 // after ChildProcessLaunched as previous test runs will tear down existing |
| 100 // connections. | 100 // connections. |
| 101 void SetupService(base::ProcessId pid) { | 101 void SetupService(base::ProcessId pid) { |
| 102 background_service_manager_->RegisterService( | 102 connector_->InitBackgroundServiceManager(); |
| 103 service_manager::BackgroundServiceManager* background_service_manager = |
| 104 connector_->background_service_manager(); |
| 105 background_service_manager->RegisterService( |
| 103 service_manager::Identity(content::mojom::kPackagedServicesServiceName, | 106 service_manager::Identity(content::mojom::kPackagedServicesServiceName, |
| 104 service_manager::mojom::kRootUserID), | 107 service_manager::mojom::kRootUserID), |
| 105 std::move(service_), mojo::MakeRequest(&pid_receiver_)); | 108 std::move(service_), mojo::MakeRequest(&pid_receiver_)); |
| 106 | 109 |
| 107 DCHECK(pid_receiver_.is_bound()); | 110 DCHECK(pid_receiver_.is_bound()); |
| 108 pid_receiver_->SetPID(pid); | 111 pid_receiver_->SetPID(pid); |
| 109 pid_receiver_.reset(); | 112 pid_receiver_.reset(); |
| 110 | 113 |
| 111 std::move(on_process_launched_).Run(); | 114 std::move(on_process_launched_).Run(); |
| 112 } | 115 } |
| 113 | 116 |
| 114 mojo::edk::OutgoingBrokerClientInvitation broker_client_invitation_; | 117 mojo::edk::OutgoingBrokerClientInvitation broker_client_invitation_; |
| 115 service_manager::BackgroundServiceManager* const background_service_manager_; | 118 MojoTestConnector* connector_; |
| 116 | 119 |
| 117 // The ServicePtr must be created before child process launch so that the pipe | 120 // The ServicePtr must be created before child process launch so that the pipe |
| 118 // can be set on the command line. It is held until SetupService is called at | 121 // can be set on the command line. It is held until SetupService is called at |
| 119 // which point |background_service_manager_| takes over ownership. | 122 // which point |background_service_manager_| takes over ownership. |
| 120 service_manager::mojom::ServicePtr service_; | 123 service_manager::mojom::ServicePtr service_; |
| 121 | 124 |
| 122 // NOTE: HandlePassingInformation must remain valid through process launch, | 125 // NOTE: HandlePassingInformation must remain valid through process launch, |
| 123 // hence it lives here instead of within Init()'s stack. | 126 // hence it lives here instead of within Init()'s stack. |
| 124 mojo::edk::HandlePassingInformation handle_passing_info_; | 127 mojo::edk::HandlePassingInformation handle_passing_info_; |
| 125 | 128 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 MojoTestConnector::MojoTestConnector( | 196 MojoTestConnector::MojoTestConnector( |
| 194 std::unique_ptr<base::Value> catalog_contents, | 197 std::unique_ptr<base::Value> catalog_contents, |
| 195 Config config) | 198 Config config) |
| 196 : config_(config), | 199 : config_(config), |
| 197 service_process_launcher_delegate_(new ServiceProcessLauncherDelegateImpl( | 200 service_process_launcher_delegate_(new ServiceProcessLauncherDelegateImpl( |
| 198 config == MojoTestConnector::Config::MASH ? kMashTestRunnerName | 201 config == MojoTestConnector::Config::MASH ? kMashTestRunnerName |
| 199 : kMusTestRunnerName)), | 202 : kMusTestRunnerName)), |
| 200 background_service_manager_(nullptr), | 203 background_service_manager_(nullptr), |
| 201 catalog_contents_(std::move(catalog_contents)) {} | 204 catalog_contents_(std::move(catalog_contents)) {} |
| 202 | 205 |
| 203 service_manager::mojom::ServiceRequest MojoTestConnector::Init() { | 206 void MojoTestConnector::Init() { |
| 204 // In single-process test mode, browser code will initialize the EDK and IPC. | 207 // In single-process test mode, browser code will initialize the EDK and IPC. |
| 205 // Otherwise we ensure it's initialized here. | 208 // Otherwise we ensure it's initialized here. |
| 206 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( | 209 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 207 content::kSingleProcessTestsFlag)) { | 210 content::kSingleProcessTestsFlag)) { |
| 208 mojo::edk::Init(); | 211 mojo::edk::Init(); |
| 209 ipc_thread_ = base::MakeUnique<base::Thread>("IPC thread"); | 212 ipc_thread_ = base::MakeUnique<base::Thread>("IPC thread"); |
| 210 ipc_thread_->StartWithOptions(base::Thread::Options( | 213 ipc_thread_->StartWithOptions(base::Thread::Options( |
| 211 base::MessageLoop::TYPE_IO, 0)); | 214 base::MessageLoop::TYPE_IO, 0)); |
| 212 ipc_support_ = base::MakeUnique<mojo::edk::ScopedIPCSupport>( | 215 ipc_support_ = base::MakeUnique<mojo::edk::ScopedIPCSupport>( |
| 213 ipc_thread_->task_runner(), | 216 ipc_thread_->task_runner(), |
| 214 mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST); | 217 mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST); |
| 215 } | 218 } |
| 219 } |
| 216 | 220 |
| 221 service_manager::mojom::ServiceRequest |
| 222 MojoTestConnector::InitBackgroundServiceManager() { |
| 217 service_manager::mojom::ServicePtr service; | 223 service_manager::mojom::ServicePtr service; |
| 218 auto request = mojo::MakeRequest(&service); | 224 auto request = mojo::MakeRequest(&service); |
| 219 | 225 |
| 220 // BackgroundServiceManager must be created after mojo::edk::Init() as it | 226 // BackgroundServiceManager must be created after mojo::edk::Init() as it |
| 221 // attempts to create mojo pipes for the provided catalog on a separate | 227 // attempts to create mojo pipes for the provided catalog on a separate |
| 222 // thread. | 228 // thread. |
| 223 background_service_manager_ = | 229 background_service_manager_ = |
| 224 base::MakeUnique<service_manager::BackgroundServiceManager>( | 230 base::MakeUnique<service_manager::BackgroundServiceManager>( |
| 225 service_process_launcher_delegate_.get(), | 231 service_process_launcher_delegate_.get(), |
| 226 std::move(catalog_contents_)); | 232 catalog_contents_->CreateDeepCopy()); |
| 227 background_service_manager_->RegisterService( | 233 background_service_manager_->RegisterService( |
| 228 service_manager::Identity(config_ == MojoTestConnector::Config::MASH | 234 service_manager::Identity(config_ == MojoTestConnector::Config::MASH |
| 229 ? kMashTestRunnerName | 235 ? kMashTestRunnerName |
| 230 : kMusTestRunnerName, | 236 : kMusTestRunnerName, |
| 231 service_manager::mojom::kRootUserID), | 237 service_manager::mojom::kRootUserID), |
| 232 std::move(service), nullptr); | 238 std::move(service), nullptr); |
| 233 return request; | 239 return request; |
| 234 } | 240 } |
| 235 | 241 |
| 236 MojoTestConnector::~MojoTestConnector() {} | 242 MojoTestConnector::~MojoTestConnector() {} |
| 237 | 243 |
| 238 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( | 244 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( |
| 239 base::CommandLine* command_line, | 245 base::CommandLine* command_line, |
| 240 base::TestLauncher::LaunchOptions* test_launch_options, | 246 base::TestLauncher::LaunchOptions* test_launch_options, |
| 241 base::OnceClosure on_process_launched) { | 247 base::OnceClosure on_process_launched) { |
| 242 return base::MakeUnique<MojoTestState>( | 248 return base::MakeUnique<MojoTestState>( |
| 243 background_service_manager_.get(), command_line, test_launch_options, | 249 this, command_line, test_launch_options, |
| 244 config_ == MojoTestConnector::Config::MASH ? switches::kMash | 250 config_ == MojoTestConnector::Config::MASH ? switches::kMash |
| 245 : switches::kMus, | 251 : switches::kMus, |
| 246 std::move(on_process_launched)); | 252 std::move(on_process_launched)); |
| 247 } | 253 } |
| 248 | 254 |
| 249 void MojoTestConnector::StartService(const std::string& service_name) { | 255 void MojoTestConnector::StartService(const std::string& service_name) { |
| 250 background_service_manager_->StartService(service_manager::Identity( | 256 background_service_manager_->StartService(service_manager::Identity( |
| 251 service_name, service_manager::mojom::kRootUserID)); | 257 service_name, service_manager::mojom::kRootUserID)); |
| 252 } | 258 } |
| OLD | NEW |