Index: chrome/test/base/mojo_test_connector.cc |
diff --git a/chrome/test/base/mojo_test_connector.cc b/chrome/test/base/mojo_test_connector.cc |
index fcbb99885f8e4c2abb2d735ab700834d3a3d638c..da87cdad625609c595195d4abc41afde42f51389 100644 |
--- a/chrome/test/base/mojo_test_connector.cc |
+++ b/chrome/test/base/mojo_test_connector.cc |
@@ -67,15 +67,11 @@ class MojoTestState : public content::TestState { |
#else |
#error "Unsupported" |
#endif |
- service_manager::mojom::ServicePtr service = |
- service_manager::PassServiceRequestOnCommandLine(&process_connection_, |
- command_line); |
- background_service_manager_->RegisterService( |
- service_manager::Identity(content::mojom::kPackagedServicesServiceName, |
- service_manager::mojom::kRootUserID), |
- std::move(service), |
- service_manager::mojom::PIDReceiverRequest(&pid_receiver_)); |
+ // Create the pipe token, as it must be passed to children processes via the |
+ // command line. |
+ service_ = service_manager::PassServiceRequestOnCommandLine( |
+ &process_connection_, command_line); |
// ChildProcessLaunched may be called on an arbitrary thread, so track the |
// current TaskRunner and post back to it when we want to send the PID. |
@@ -91,13 +87,22 @@ class MojoTestState : public content::TestState { |
handle, |
mojo::edk::ConnectionParams(platform_channel_->PassServerHandle())); |
- main_task_runner_->PostTask( |
- FROM_HERE, |
- base::Bind(&MojoTestState::SetPID, weak_factory_.GetWeakPtr(), pid)); |
+ main_task_runner_->PostTask(FROM_HERE, |
+ base::Bind(&MojoTestState::SetupService, |
+ weak_factory_.GetWeakPtr(), pid)); |
} |
// Called on the main thread only. |
- void SetPID(base::ProcessId pid) { |
+ // This registers the services needed for the test. This is not done until |
+ // after ChildProcessLaunched as previous test runs will tear down existing |
+ // connections. |
+ void SetupService(base::ProcessId pid) { |
+ background_service_manager_->RegisterService( |
+ service_manager::Identity(content::mojom::kPackagedServicesServiceName, |
+ service_manager::mojom::kRootUserID), |
+ std::move(service_), |
+ service_manager::mojom::PIDReceiverRequest(&pid_receiver_)); |
+ |
DCHECK(pid_receiver_.is_bound()); |
pid_receiver_->SetPID(pid); |
pid_receiver_.reset(); |
@@ -106,6 +111,11 @@ class MojoTestState : public content::TestState { |
mojo::edk::PendingProcessConnection process_connection_; |
service_manager::BackgroundServiceManager* const background_service_manager_; |
+ // The ServicePtr must be created before child process launch so that the pipe |
+ // can be set on the command line. It is held until SetupService is called at |
+ // which point |background_service_manager_| takes over ownership. |
+ service_manager::mojom::ServicePtr service_; |
+ |
// NOTE: HandlePassingInformation must remain valid through process launch, |
// hence it lives here instead of within Init()'s stack. |
mojo::edk::HandlePassingInformation handle_passing_info_; |