Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: chrome/test/base/mojo_test_connector.cc

Issue 2645973006: [Service Manager] Get rid of dynamic service discovery (Closed)
Patch Set: . Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/message_loop/message_loop.h"
11 #include "base/run_loop.h" 12 #include "base/run_loop.h"
12 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
13 #include "base/synchronization/waitable_event.h" 14 #include "base/synchronization/waitable_event.h"
14 #include "base/threading/thread.h" 15 #include "base/threading/thread.h"
15 #include "content/public/common/content_switches.h" 16 #include "content/public/common/content_switches.h"
16 #include "content/public/test/test_launcher.h" 17 #include "content/public/test/test_launcher.h"
17 #include "mojo/edk/embedder/embedder.h" 18 #include "mojo/edk/embedder/embedder.h"
18 #include "mojo/edk/embedder/platform_channel_pair.h" 19 #include "mojo/edk/embedder/platform_channel_pair.h"
20 #include "mojo/edk/embedder/scoped_ipc_support.h"
19 #include "mojo/public/cpp/bindings/interface_request.h" 21 #include "mojo/public/cpp/bindings/interface_request.h"
20 #include "services/catalog/store.h" 22 #include "services/catalog/store.h"
23 #include "services/service_manager/background/background_service_manager.h"
21 #include "services/service_manager/public/cpp/connector.h" 24 #include "services/service_manager/public/cpp/connector.h"
22 #include "services/service_manager/public/cpp/service.h" 25 #include "services/service_manager/public/cpp/service.h"
23 #include "services/service_manager/public/cpp/service_context.h" 26 #include "services/service_manager/public/cpp/service_context.h"
24 #include "services/service_manager/runner/common/client_util.h" 27 #include "services/service_manager/runner/common/client_util.h"
25 #include "services/service_manager/runner/common/switches.h" 28 #include "services/service_manager/runner/common/switches.h"
26 #include "services/service_manager/runner/host/service_process_launcher.h" 29 #include "services/service_manager/runner/host/service_process_launcher.h"
27 #include "services/service_manager/service_manager.h" 30 #include "services/service_manager/service_manager.h"
28 #include "services/service_manager/switches.h" 31 #include "services/service_manager/switches.h"
29 32
30 namespace { 33 namespace {
31 34
32 const char kTestRunnerName[] = "mash_browser_tests"; 35 const char kTestRunnerName[] = "mash_browser_tests";
33 const char kTestName[] = "content_browser"; 36 const char kTestName[] = "content_browser";
34 37
35 // BackgroundTestState maintains all the state necessary to bind the test to 38 // State created per test to register a client process with the background
36 // mojo. This class is only used on the thread created by 39 // service manager.
37 // BackgroundServiceManager. 40 class MojoTestState : public content::TestState {
38 class BackgroundTestState {
39 public: 41 public:
40 BackgroundTestState() : child_token_(mojo::edk::GenerateRandomToken()) {} 42 explicit MojoTestState(
41 ~BackgroundTestState() {} 43 service_manager::BackgroundServiceManager* background_service_manager)
44 : child_token_(mojo::edk::GenerateRandomToken()),
45 background_service_manager_(background_service_manager) {}
46 ~MojoTestState() override {}
42 47
43 // Prepares the command line and other setup for connecting the test to mojo. 48 void Init(base::CommandLine* command_line,
44 // Must be paired with a call to ChildProcessLaunched(). 49 base::TestLauncher::LaunchOptions* test_launch_options) {
45 void Connect(base::CommandLine* command_line,
46 service_manager::ServiceManager* service_manager,
47 base::TestLauncher::LaunchOptions* test_launch_options) {
48 command_line->AppendSwitch(MojoTestConnector::kTestSwitch); 50 command_line->AppendSwitch(MojoTestConnector::kTestSwitch);
49 command_line->AppendSwitch(switches::kChildProcess); 51 command_line->AppendSwitch(switches::kChildProcess);
50 mojo_ipc_channel_.reset(new mojo::edk::PlatformChannelPair); 52
51 mojo_ipc_channel_->PrepareToPassClientHandleToChildProcess( 53 platform_channel_ = base::MakeUnique<mojo::edk::PlatformChannelPair>();
54
55 platform_channel_->PrepareToPassClientHandleToChildProcess(
52 command_line, &handle_passing_info_); 56 command_line, &handle_passing_info_);
53 #if defined(OS_WIN) 57 #if defined(OS_WIN)
54 test_launch_options->inherit_handles = true; 58 test_launch_options->inherit_handles = true;
55 test_launch_options->handles_to_inherit = &handle_passing_info_; 59 test_launch_options->handles_to_inherit = &handle_passing_info_;
56 #if defined(OFFICIAL_BUILD) 60 #if defined(OFFICIAL_BUILD)
57 CHECK(false) << "Launching mojo process with inherit_handles is insecure!"; 61 CHECK(false) << "Launching mojo process with inherit_handles is insecure!";
58 #endif 62 #endif
59 #elif defined(OS_POSIX) 63 #elif defined(OS_POSIX)
60 test_launch_options->fds_to_remap = &handle_passing_info_; 64 test_launch_options->fds_to_remap = &handle_passing_info_;
61 #else 65 #else
62 #error "Unsupported" 66 #error "Unsupported"
63 #endif 67 #endif
64 service_manager::mojom::ServicePtr service = 68 service_manager::mojom::ServicePtr service =
65 service_manager::PassServiceRequestOnCommandLine(command_line, 69 service_manager::PassServiceRequestOnCommandLine(command_line,
66 child_token_); 70 child_token_);
67 71
68 std::unique_ptr<service_manager::ConnectParams> params( 72 service_manager::mojom::PIDReceiverPtr pid_receiver;
69 new service_manager::ConnectParams); 73 background_service_manager_->RegisterService(
70 params->set_source(service_manager::CreateServiceManagerIdentity()); 74 service_manager::Identity(
71 // Use the default instance name (which should be "browser"). Otherwise a 75 kTestName, service_manager::mojom::kRootUserID),
72 // service (e.g. ash) that connects to the default "content_browser" 76 std::move(service),
73 // will spawn a new instance. 77 service_manager::mojom::PIDReceiverRequest(&pid_receiver));
74 params->set_target(service_manager::Identity(
75 kTestName, service_manager::mojom::kRootUserID));
76 params->set_client_process_info(std::move(service),
77 MakeRequest(&pid_receiver_));
78 service_manager->Connect(std::move(params));
79 }
80 78
81 // Called after the test process has launched. Completes the registration done 79 // ChildProcessLaunched may be called on an arbitrary thread, so we unbind
82 // in Connect(). 80 // the InterfacePtr here and will rebind it later on that thread.
83 void ChildProcessLaunched(base::ProcessHandle handle, base::ProcessId pid) { 81 pid_receiver_info_ = pid_receiver.PassInterface();
84 pid_receiver_->SetPID(pid);
85 mojo_ipc_channel_->ChildProcessLaunched();
86 mojo::edk::ChildProcessLaunched(
87 handle, mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle(
88 mojo_ipc_channel_->PassServerHandle().release().handle)),
89 child_token_);
90 }
91
92 private:
93 // Used to back the NodeChannel between the parent and child node.
94 const std::string child_token_;
95 std::unique_ptr<mojo::edk::PlatformChannelPair> mojo_ipc_channel_;
96
97 mojo::edk::HandlePassingInformation handle_passing_info_;
98
99 service_manager::mojom::PIDReceiverPtr pid_receiver_;
100
101 DISALLOW_COPY_AND_ASSIGN(BackgroundTestState);
102 };
103
104 // Called used destroy BackgroundTestState on the background thread.
105 void DestroyBackgroundStateOnBackgroundThread(
106 std::unique_ptr<BackgroundTestState> state,
107 service_manager::ServiceManager* service_manager) {}
108
109 // State created per test. Manages creation of the corresponding
110 // BackgroundTestState and making sure processing runs on the right threads.
111 class MojoTestState : public content::TestState {
112 public:
113 explicit MojoTestState(
114 service_manager::BackgroundServiceManager* background_service_manager)
115 : background_service_manager_(background_service_manager) {}
116
117 ~MojoTestState() override {
118 DCHECK(background_state_);
119 // BackgroundState needs to be destroyed on the background thread. We're
120 // guaranteed |background_service_manager_| has been created by the time we
121 // reach
122 // here as Init() blocks until |background_service_manager_| has been
123 // created.
124 background_service_manager_->ExecuteOnServiceManagerThread(
125 base::Bind(&DestroyBackgroundStateOnBackgroundThread,
126 base::Passed(&background_state_)));
127 }
128
129 void Init(base::CommandLine* command_line,
130 base::TestLauncher::LaunchOptions* test_launch_options) {
131 base::WaitableEvent signal(base::WaitableEvent::ResetPolicy::MANUAL,
132 base::WaitableEvent::InitialState::NOT_SIGNALED);
133 background_service_manager_->ExecuteOnServiceManagerThread(base::Bind(
134 &MojoTestState::BindOnBackgroundThread, base::Unretained(this), &signal,
135 command_line, test_launch_options));
136 signal.Wait();
137 } 82 }
138 83
139 private: 84 private:
140 // content::TestState: 85 // content::TestState:
141 void ChildProcessLaunched(base::ProcessHandle handle, 86 void ChildProcessLaunched(base::ProcessHandle handle,
142 base::ProcessId pid) override { 87 base::ProcessId pid) override {
143 // This is called on a random thread. We need to ensure BackgroundTestState 88 service_manager::mojom::PIDReceiverPtr pid_receiver;
144 // is only called on the background thread, and we wait for 89 pid_receiver.Bind(std::move(pid_receiver_info_));
145 // ChildProcessLaunchedOnBackgroundThread() to be run before continuing so 90 pid_receiver->SetPID(pid);
146 // that |handle| is still valid. 91
147 base::WaitableEvent signal(base::WaitableEvent::ResetPolicy::MANUAL, 92 platform_channel_->ChildProcessLaunched();
148 base::WaitableEvent::InitialState::NOT_SIGNALED); 93 mojo::edk::ChildProcessLaunched(
149 background_service_manager_->ExecuteOnServiceManagerThread( 94 handle, platform_channel_->PassServerHandle(), child_token_);
150 base::Bind(&MojoTestState::ChildProcessLaunchedOnBackgroundThread,
151 base::Unretained(this), handle, pid, &signal));
152 signal.Wait();
153 } 95 }
154 96
155 void ChildProcessLaunchedOnBackgroundThread( 97 const std::string child_token_;
156 base::ProcessHandle handle, 98 service_manager::BackgroundServiceManager* const background_service_manager_;
157 base::ProcessId pid,
158 base::WaitableEvent* signal,
159 service_manager::ServiceManager* service_manager) {
160 background_state_->ChildProcessLaunched(handle, pid);
161 signal->Signal();
162 }
163 99
164 void BindOnBackgroundThread( 100 // NOTE: HandlePassingInformation must remain valid through process launch,
165 base::WaitableEvent* signal, 101 // hence it lives here instead of within Init()'s stack.
166 base::CommandLine* command_line, 102 mojo::edk::HandlePassingInformation handle_passing_info_;
167 base::TestLauncher::LaunchOptions* test_launch_options,
168 service_manager::ServiceManager* service_manager) {
169 background_state_.reset(new BackgroundTestState);
170 background_state_->Connect(command_line, service_manager,
171 test_launch_options);
172 signal->Signal();
173 }
174 103
175 service_manager::BackgroundServiceManager* background_service_manager_; 104 std::unique_ptr<mojo::edk::PlatformChannelPair> platform_channel_;
176 std::unique_ptr<BackgroundTestState> background_state_; 105 service_manager::mojom::PIDReceiverPtrInfo pid_receiver_info_;
177 106
178 DISALLOW_COPY_AND_ASSIGN(MojoTestState); 107 DISALLOW_COPY_AND_ASSIGN(MojoTestState);
179 }; 108 };
180 109
181 // The name in the manifest results in getting exe:mash_browser_tests used, 110 // The name in the manifest results in getting exe:mash_browser_tests used,
182 // remap that to browser_tests. 111 // remap that to browser_tests.
183 void RemoveMashFromBrowserTests(base::CommandLine* command_line) { 112 void RemoveMashFromBrowserTests(base::CommandLine* command_line) {
184 base::FilePath exe_path(command_line->GetProgram()); 113 base::FilePath exe_path(command_line->GetProgram());
185 #if defined(OS_WIN) 114 #if defined(OS_WIN)
186 exe_path = exe_path.DirName().Append(FILE_PATH_LITERAL("browser_tests.exe")); 115 exe_path = exe_path.DirName().Append(FILE_PATH_LITERAL("browser_tests.exe"));
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 } 153 }
225 154
226 DISALLOW_COPY_AND_ASSIGN(ServiceProcessLauncherDelegateImpl); 155 DISALLOW_COPY_AND_ASSIGN(ServiceProcessLauncherDelegateImpl);
227 }; 156 };
228 157
229 // static 158 // static
230 const char MojoTestConnector::kTestSwitch[] = "is_test"; 159 const char MojoTestConnector::kTestSwitch[] = "is_test";
231 // static 160 // static
232 const char MojoTestConnector::kMashApp[] = "mash-app"; 161 const char MojoTestConnector::kMashApp[] = "mash-app";
233 162
234 MojoTestConnector::MojoTestConnector() {} 163 MojoTestConnector::MojoTestConnector(
164 std::unique_ptr<base::Value> catalog_contents)
165 : service_process_launcher_delegate_(
166 new ServiceProcessLauncherDelegateImpl),
167 background_service_manager_(service_process_launcher_delegate_.get(),
168 std::move(catalog_contents)) {}
235 169
236 service_manager::mojom::ServiceRequest MojoTestConnector::Init() { 170 service_manager::mojom::ServiceRequest MojoTestConnector::Init() {
237 service_process_launcher_delegate_ = 171 // In single-process test mode, browser code will initialize the EDK and IPC.
238 base::MakeUnique<ServiceProcessLauncherDelegateImpl>(); 172 // Otherwise we ensure it's initialized here.
173 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
174 content::kSingleProcessTestsFlag)) {
175 mojo::edk::Init();
176 ipc_thread_ = base::MakeUnique<base::Thread>("IPC thread");
177 ipc_thread_->StartWithOptions(base::Thread::Options(
178 base::MessageLoop::TYPE_IO, 0));
179 ipc_support_ = base::MakeUnique<mojo::edk::ScopedIPCSupport>(
180 ipc_thread_->task_runner(),
181 mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST);
182 }
239 183
240 std::unique_ptr<service_manager::BackgroundServiceManager::InitParams> 184 service_manager::mojom::ServicePtr service;
241 init_params = base::MakeUnique< 185 service_manager::mojom::ServiceRequest request(&service);
242 service_manager::BackgroundServiceManager::InitParams>(); 186 background_service_manager_.RegisterService(
243 // When running in single_process mode chrome initializes the edk. 187 service_manager::Identity(kTestRunnerName,
244 init_params->init_edk = !base::CommandLine::ForCurrentProcess()->HasSwitch( 188 service_manager::mojom::kRootUserID),
245 content::kSingleProcessTestsFlag); 189 std::move(service), nullptr);
246 init_params->service_process_launcher_delegate = 190 return request;
247 service_process_launcher_delegate_.get();
248 background_service_manager_.Init(std::move(init_params));
249 return background_service_manager_.CreateServiceRequest(kTestRunnerName);
250 } 191 }
251 192
252 MojoTestConnector::~MojoTestConnector() {} 193 MojoTestConnector::~MojoTestConnector() {}
253 194
254 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( 195 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest(
255 base::CommandLine* command_line, 196 base::CommandLine* command_line,
256 base::TestLauncher::LaunchOptions* test_launch_options) { 197 base::TestLauncher::LaunchOptions* test_launch_options) {
257 std::unique_ptr<MojoTestState> test_state( 198 auto test_state =
258 new MojoTestState(&background_service_manager_)); 199 base::MakeUnique<MojoTestState>(&background_service_manager_);
259 test_state->Init(command_line, test_launch_options); 200 test_state->Init(command_line, test_launch_options);
260 return std::move(test_state); 201 return test_state;
261 } 202 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698