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/app/mash/mash_runner.h" | 5 #include "chrome/app/mash/mash_runner.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
(...skipping 14 matching lines...) Expand all Loading... | |
25 #include "base/sys_info.h" | 25 #include "base/sys_info.h" |
26 #include "base/task_scheduler/task_scheduler.h" | 26 #include "base/task_scheduler/task_scheduler.h" |
27 #include "base/threading/sequenced_worker_pool.h" | 27 #include "base/threading/sequenced_worker_pool.h" |
28 #include "base/threading/thread.h" | 28 #include "base/threading/thread.h" |
29 #include "base/trace_event/trace_event.h" | 29 #include "base/trace_event/trace_event.h" |
30 #include "chrome/app/mash/chrome_mash_catalog.h" | 30 #include "chrome/app/mash/chrome_mash_catalog.h" |
31 #include "components/tracing/common/trace_to_console.h" | 31 #include "components/tracing/common/trace_to_console.h" |
32 #include "components/tracing/common/tracing_switches.h" | 32 #include "components/tracing/common/tracing_switches.h" |
33 #include "content/public/common/content_switches.h" | 33 #include "content/public/common/content_switches.h" |
34 #include "content/public/common/service_names.mojom.h" | 34 #include "content/public/common/service_names.mojom.h" |
35 #include "mash/common/config.h" | |
35 #include "mash/package/mash_packaged_service.h" | 36 #include "mash/package/mash_packaged_service.h" |
36 #include "mash/session/public/interfaces/constants.mojom.h" | 37 #include "mash/quick_launch/public/interfaces/constants.mojom.h" |
37 #include "mojo/edk/embedder/embedder.h" | 38 #include "mojo/edk/embedder/embedder.h" |
38 #include "mojo/edk/embedder/scoped_ipc_support.h" | 39 #include "mojo/edk/embedder/scoped_ipc_support.h" |
39 #include "mojo/public/cpp/bindings/binding_set.h" | 40 #include "mojo/public/cpp/bindings/binding_set.h" |
40 #include "services/catalog/public/interfaces/catalog.mojom.h" | 41 #include "services/catalog/public/interfaces/catalog.mojom.h" |
41 #include "services/catalog/public/interfaces/constants.mojom.h" | 42 #include "services/catalog/public/interfaces/constants.mojom.h" |
42 #include "services/service_manager/background/background_service_manager.h" | 43 #include "services/service_manager/background/background_service_manager.h" |
43 #include "services/service_manager/public/cpp/connector.h" | 44 #include "services/service_manager/public/cpp/connector.h" |
44 #include "services/service_manager/public/cpp/identity.h" | 45 #include "services/service_manager/public/cpp/identity.h" |
45 #include "services/service_manager/public/cpp/service.h" | 46 #include "services/service_manager/public/cpp/service.h" |
46 #include "services/service_manager/public/cpp/service_context.h" | 47 #include "services/service_manager/public/cpp/service_context.h" |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 MashRunner::MashRunner() {} | 152 MashRunner::MashRunner() {} |
152 | 153 |
153 MashRunner::~MashRunner() {} | 154 MashRunner::~MashRunner() {} |
154 | 155 |
155 int MashRunner::Run() { | 156 int MashRunner::Run() { |
156 base::TaskScheduler::CreateAndSetSimpleTaskScheduler( | 157 base::TaskScheduler::CreateAndSetSimpleTaskScheduler( |
157 base::SysInfo::NumberOfProcessors()); | 158 base::SysInfo::NumberOfProcessors()); |
158 | 159 |
159 if (IsChild()) | 160 if (IsChild()) |
160 return RunChild(); | 161 return RunChild(); |
161 RunMain(); | 162 |
162 return 0; | 163 return RunMain(); |
163 } | 164 } |
164 | 165 |
165 void MashRunner::RunMain() { | 166 int MashRunner::RunMain() { |
166 base::SequencedWorkerPool::EnableWithRedirectionToTaskSchedulerForProcess(); | 167 base::SequencedWorkerPool::EnableWithRedirectionToTaskSchedulerForProcess(); |
167 | 168 |
168 mojo::edk::Init(); | 169 mojo::edk::Init(); |
169 | 170 |
170 base::Thread ipc_thread("IPC thread"); | 171 base::Thread ipc_thread("IPC thread"); |
171 ipc_thread.StartWithOptions( | 172 ipc_thread.StartWithOptions( |
172 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | 173 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
173 mojo::edk::ScopedIPCSupport ipc_support( | 174 mojo::edk::ScopedIPCSupport ipc_support( |
174 ipc_thread.task_runner(), | 175 ipc_thread.task_runner(), |
175 mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST); | 176 mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST); |
176 | 177 |
178 RunServiceManagerInMain(); | |
179 | |
180 ipc_thread.Stop(); | |
181 base::TaskScheduler::GetInstance()->Shutdown(); | |
182 return main_return_value_; | |
183 } | |
184 | |
185 void MashRunner::RunServiceManagerInMain() { | |
186 // Ensure run loop exists before instance quit callback is constructed. | |
187 main_run_loop_ = base::MakeUnique<base::RunLoop>(); | |
188 | |
177 // TODO(sky): refactor BackgroundServiceManager so can supply own context, we | 189 // TODO(sky): refactor BackgroundServiceManager so can supply own context, we |
178 // shouldn't we using context as it has a lot of stuff we don't really want | 190 // shouldn't we using context as it has a lot of stuff we don't really want |
179 // in chrome. | 191 // in chrome. |
180 ServiceProcessLauncherDelegateImpl service_process_launcher_delegate; | 192 ServiceProcessLauncherDelegateImpl service_process_launcher_delegate; |
181 service_manager::BackgroundServiceManager background_service_manager( | 193 service_manager::BackgroundServiceManager background_service_manager( |
182 &service_process_launcher_delegate, CreateChromeMashCatalog()); | 194 &service_process_launcher_delegate, CreateChromeMashCatalog()); |
183 service_manager::mojom::ServicePtr service; | 195 service_manager::mojom::ServicePtr service; |
184 context_.reset(new service_manager::ServiceContext( | 196 context_.reset(new service_manager::ServiceContext( |
185 base::MakeUnique<mash::MashPackagedService>(), | 197 base::MakeUnique<mash::MashPackagedService>(), |
186 service_manager::mojom::ServiceRequest(&service))); | 198 service_manager::mojom::ServiceRequest(&service))); |
187 background_service_manager.RegisterService( | 199 background_service_manager.RegisterService( |
188 service_manager::Identity( | 200 service_manager::Identity( |
189 kChromeMashServiceName, service_manager::mojom::kRootUserID), | 201 kChromeMashServiceName, service_manager::mojom::kRootUserID), |
190 std::move(service), nullptr); | 202 std::move(service), nullptr); |
191 | 203 |
192 // Ping mash_session to ensure an instance is brought up | 204 // Quit the main process if an important child (e.g. window manager) dies. |
193 context_->connector()->Connect(mash::session::mojom::kServiceName); | 205 background_service_manager.SetInstanceQuitCallback( |
194 base::RunLoop().Run(); | 206 base::Bind(&MashRunner::OnInstanceQuitInMain, base::Unretained(this))); |
Ken Rockot(use gerrit already)
2017/01/31 23:33:28
Instead of making the RunLoop and return values in
Ken Rockot(use gerrit already)
2017/01/31 23:34:51
Note: I just realized this would also mean there's
James Cook
2017/02/01 00:41:32
Done and done. I used an anonymous function becaus
| |
195 | 207 |
196 base::TaskScheduler::GetInstance()->Shutdown(); | 208 // Ping services that we know we want to launch on startup. |
209 // TODO(jamescook): Start the window server / ui service explicitly. | |
210 context_->connector()->Connect(mash::common::GetWindowManagerServiceName()); | |
211 context_->connector()->Connect(mash::quick_launch::mojom::kServiceName); | |
212 | |
213 main_run_loop_->Run(); | |
214 | |
215 context_.reset(); | |
216 } | |
217 | |
218 void MashRunner::OnInstanceQuitInMain( | |
219 const service_manager::Identity& identity) { | |
220 // If the window manager goes down then exit the main process with an error. | |
221 // On Chrome OS the OS-level session_manager will restart the main process. | |
222 // TODO(jamescook): Also shut down if the window server dies. | |
223 if (identity.name() != mash::common::GetWindowManagerServiceName()) | |
224 return; | |
225 | |
226 if (!main_run_loop_->running()) | |
227 return; | |
228 | |
229 main_return_value_ = 1; | |
230 main_run_loop_->Quit(); | |
197 } | 231 } |
198 | 232 |
199 int MashRunner::RunChild() { | 233 int MashRunner::RunChild() { |
200 service_manager::WaitForDebuggerIfNecessary(); | 234 service_manager::WaitForDebuggerIfNecessary(); |
201 | 235 |
202 base::i18n::InitializeICU(); | 236 base::i18n::InitializeICU(); |
203 InitializeResources(); | 237 InitializeResources(); |
204 service_manager::RunStandaloneService( | 238 service_manager::RunStandaloneService( |
205 base::Bind(&MashRunner::StartChildApp, base::Unretained(this))); | 239 base::Bind(&MashRunner::StartChildApp, base::Unretained(this))); |
206 return 0; | 240 return 0; |
207 } | 241 } |
208 | 242 |
209 void MashRunner::StartChildApp( | 243 void MashRunner::StartChildApp( |
210 service_manager::mojom::ServiceRequest service_request) { | 244 service_manager::mojom::ServiceRequest service_request) { |
211 // TODO(sad): Normally, this would be a TYPE_DEFAULT message loop. However, | 245 // TODO(sad): Normally, this would be a TYPE_DEFAULT message loop. However, |
212 // TYPE_UI is needed for mojo:ui. But it is not known whether the child app is | 246 // TYPE_UI is needed for mojo:ui. But it is not known whether the child app is |
213 // going to be mojo:ui at this point. So always create a TYPE_UI message loop | 247 // going to be mojo:ui at this point. So always create a TYPE_UI message loop |
214 // for now. | 248 // for now. |
215 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); | 249 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); |
250 base::RunLoop run_loop; | |
216 context_.reset(new service_manager::ServiceContext( | 251 context_.reset(new service_manager::ServiceContext( |
217 base::MakeUnique<mash::MashPackagedService>(), | 252 base::MakeUnique<mash::MashPackagedService>(), |
218 std::move(service_request))); | 253 std::move(service_request))); |
219 base::RunLoop().Run(); | 254 // Quit the child process if it loses its connection to service manager. |
255 context_->SetConnectionLostClosure(run_loop.QuitClosure()); | |
256 run_loop.Run(); | |
220 } | 257 } |
221 | 258 |
222 int MashMain() { | 259 int MashMain() { |
223 #if !defined(OFFICIAL_BUILD) && defined(OS_WIN) | 260 #if !defined(OFFICIAL_BUILD) && defined(OS_WIN) |
224 base::RouteStdioToConsole(false); | 261 base::RouteStdioToConsole(false); |
225 #endif | 262 #endif |
226 // TODO(sky): wire this up correctly. | 263 // TODO(sky): wire this up correctly. |
227 service_manager::InitializeLogging(); | 264 service_manager::InitializeLogging(); |
228 | 265 |
229 #if defined(OS_LINUX) | 266 #if defined(OS_LINUX) |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
271 command_line->GetSwitchValueASCII(switches::kWaitForDebugger)) { | 308 command_line->GetSwitchValueASCII(switches::kWaitForDebugger)) { |
272 return; | 309 return; |
273 } | 310 } |
274 | 311 |
275 // Include the pid as logging may not have been initialized yet (the pid | 312 // Include the pid as logging may not have been initialized yet (the pid |
276 // printed out by logging is wrong). | 313 // printed out by logging is wrong). |
277 LOG(WARNING) << "waiting for debugger to attach for service " << service_name | 314 LOG(WARNING) << "waiting for debugger to attach for service " << service_name |
278 << " pid=" << base::Process::Current().Pid(); | 315 << " pid=" << base::Process::Current().Pid(); |
279 base::debug::WaitForDebugger(120, true); | 316 base::debug::WaitForDebugger(120, true); |
280 } | 317 } |
OLD | NEW |