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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 test_launch_options->inherit_handles = true; | 60 test_launch_options->inherit_handles = true; |
61 test_launch_options->handles_to_inherit = &handle_passing_info_; | 61 test_launch_options->handles_to_inherit = &handle_passing_info_; |
62 #if defined(OFFICIAL_BUILD) | 62 #if defined(OFFICIAL_BUILD) |
63 CHECK(false) << "Launching mojo process with inherit_handles is insecure!"; | 63 CHECK(false) << "Launching mojo process with inherit_handles is insecure!"; |
64 #endif | 64 #endif |
65 #elif defined(OS_POSIX) | 65 #elif defined(OS_POSIX) |
66 test_launch_options->fds_to_remap = &handle_passing_info_; | 66 test_launch_options->fds_to_remap = &handle_passing_info_; |
67 #else | 67 #else |
68 #error "Unsupported" | 68 #error "Unsupported" |
69 #endif | 69 #endif |
70 service_manager::mojom::ServicePtr service = | |
71 service_manager::PassServiceRequestOnCommandLine(&process_connection_, | |
72 command_line); | |
73 | 70 |
74 background_service_manager_->RegisterService( | 71 // Create the pipe token, as it must be passed to children processes via the |
75 service_manager::Identity(content::mojom::kPackagedServicesServiceName, | 72 // command line. |
76 service_manager::mojom::kRootUserID), | 73 service_ = service_manager::PassServiceRequestOnCommandLine( |
77 std::move(service), | 74 &process_connection_, command_line); |
78 service_manager::mojom::PIDReceiverRequest(&pid_receiver_)); | |
79 | 75 |
80 // ChildProcessLaunched may be called on an arbitrary thread, so track the | 76 // ChildProcessLaunched may be called on an arbitrary thread, so track the |
81 // current TaskRunner and post back to it when we want to send the PID. | 77 // current TaskRunner and post back to it when we want to send the PID. |
82 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 78 main_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
83 } | 79 } |
84 | 80 |
85 private: | 81 private: |
86 // content::TestState: | 82 // content::TestState: |
87 void ChildProcessLaunched(base::ProcessHandle handle, | 83 void ChildProcessLaunched(base::ProcessHandle handle, |
88 base::ProcessId pid) override { | 84 base::ProcessId pid) override { |
89 platform_channel_->ChildProcessLaunched(); | 85 platform_channel_->ChildProcessLaunched(); |
90 process_connection_.Connect( | 86 process_connection_.Connect( |
91 handle, | 87 handle, |
92 mojo::edk::ConnectionParams(platform_channel_->PassServerHandle())); | 88 mojo::edk::ConnectionParams(platform_channel_->PassServerHandle())); |
93 | 89 |
94 main_task_runner_->PostTask( | 90 main_task_runner_->PostTask(FROM_HERE, |
95 FROM_HERE, | 91 base::Bind(&MojoTestState::SetupService, |
96 base::Bind(&MojoTestState::SetPID, weak_factory_.GetWeakPtr(), pid)); | 92 weak_factory_.GetWeakPtr(), pid)); |
97 } | 93 } |
98 | 94 |
99 // Called on the main thread only. | 95 // Called on the main thread only. |
100 void SetPID(base::ProcessId pid) { | 96 // This registers the services needed for the test. This is not done until |
| 97 // after ChildProcessLaunched as previous test runs will tear down existing |
| 98 // connections. |
| 99 void SetupService(base::ProcessId pid) { |
| 100 background_service_manager_->RegisterService( |
| 101 service_manager::Identity(content::mojom::kPackagedServicesServiceName, |
| 102 service_manager::mojom::kRootUserID), |
| 103 std::move(service_), |
| 104 service_manager::mojom::PIDReceiverRequest(&pid_receiver_)); |
| 105 |
101 DCHECK(pid_receiver_.is_bound()); | 106 DCHECK(pid_receiver_.is_bound()); |
102 pid_receiver_->SetPID(pid); | 107 pid_receiver_->SetPID(pid); |
103 pid_receiver_.reset(); | 108 pid_receiver_.reset(); |
104 } | 109 } |
105 | 110 |
106 mojo::edk::PendingProcessConnection process_connection_; | 111 mojo::edk::PendingProcessConnection process_connection_; |
107 service_manager::BackgroundServiceManager* const background_service_manager_; | 112 service_manager::BackgroundServiceManager* const background_service_manager_; |
108 | 113 |
| 114 // The ServicePtr must be created before child process launch so that the pipe |
| 115 // can be set on the command line. It is held until SetupService is called at |
| 116 // which point |background_service_manager_| takes over ownership. |
| 117 service_manager::mojom::ServicePtr service_; |
| 118 |
109 // NOTE: HandlePassingInformation must remain valid through process launch, | 119 // NOTE: HandlePassingInformation must remain valid through process launch, |
110 // hence it lives here instead of within Init()'s stack. | 120 // hence it lives here instead of within Init()'s stack. |
111 mojo::edk::HandlePassingInformation handle_passing_info_; | 121 mojo::edk::HandlePassingInformation handle_passing_info_; |
112 | 122 |
113 std::unique_ptr<mojo::edk::PlatformChannelPair> platform_channel_; | 123 std::unique_ptr<mojo::edk::PlatformChannelPair> platform_channel_; |
114 service_manager::mojom::PIDReceiverPtr pid_receiver_; | 124 service_manager::mojom::PIDReceiverPtr pid_receiver_; |
115 scoped_refptr<base::TaskRunner> main_task_runner_ = nullptr; | 125 scoped_refptr<base::TaskRunner> main_task_runner_ = nullptr; |
116 | 126 |
117 base::WeakPtrFactory<MojoTestState> weak_factory_; | 127 base::WeakPtrFactory<MojoTestState> weak_factory_; |
118 | 128 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 MojoTestConnector::~MojoTestConnector() {} | 215 MojoTestConnector::~MojoTestConnector() {} |
206 | 216 |
207 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( | 217 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( |
208 base::CommandLine* command_line, | 218 base::CommandLine* command_line, |
209 base::TestLauncher::LaunchOptions* test_launch_options) { | 219 base::TestLauncher::LaunchOptions* test_launch_options) { |
210 auto test_state = | 220 auto test_state = |
211 base::MakeUnique<MojoTestState>(&background_service_manager_); | 221 base::MakeUnique<MojoTestState>(&background_service_manager_); |
212 test_state->Init(command_line, test_launch_options); | 222 test_state->Init(command_line, test_launch_options); |
213 return test_state; | 223 return test_state; |
214 } | 224 } |
OLD | NEW |