| 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> |
| 8 |
| 7 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
| 8 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
| 9 #include "base/base_switches.h" | 11 #include "base/base_switches.h" |
| 10 #include "base/bind.h" | 12 #include "base/bind.h" |
| 11 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 12 #include "base/debug/debugger.h" | 14 #include "base/debug/debugger.h" |
| 15 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 14 #include "base/i18n/icu_util.h" | 17 #include "base/i18n/icu_util.h" |
| 18 #include "base/json/json_reader.h" |
| 15 #include "base/logging.h" | 19 #include "base/logging.h" |
| 16 #include "base/memory/ptr_util.h" | 20 #include "base/memory/ptr_util.h" |
| 17 #include "base/message_loop/message_loop.h" | 21 #include "base/message_loop/message_loop.h" |
| 18 #include "base/path_service.h" | 22 #include "base/path_service.h" |
| 19 #include "base/process/launch.h" | 23 #include "base/process/launch.h" |
| 20 #include "base/process/process.h" | 24 #include "base/process/process.h" |
| 21 #include "base/run_loop.h" | 25 #include "base/run_loop.h" |
| 22 #include "base/sys_info.h" | 26 #include "base/sys_info.h" |
| 23 #include "base/task_scheduler/task_scheduler.h" | 27 #include "base/task_scheduler/task_scheduler.h" |
| 24 #include "base/threading/sequenced_worker_pool.h" | 28 #include "base/threading/sequenced_worker_pool.h" |
| 29 #include "base/threading/thread.h" |
| 25 #include "base/trace_event/trace_event.h" | 30 #include "base/trace_event/trace_event.h" |
| 26 #include "components/tracing/common/trace_to_console.h" | 31 #include "components/tracing/common/trace_to_console.h" |
| 27 #include "components/tracing/common/tracing_switches.h" | 32 #include "components/tracing/common/tracing_switches.h" |
| 28 #include "content/public/common/content_switches.h" | 33 #include "content/public/common/content_switches.h" |
| 29 #include "content/public/common/service_names.mojom.h" | 34 #include "content/public/common/service_names.mojom.h" |
| 30 #include "mash/package/mash_packaged_service.h" | 35 #include "mash/package/mash_packaged_service.h" |
| 31 #include "mash/session/public/interfaces/constants.mojom.h" | 36 #include "mash/session/public/interfaces/constants.mojom.h" |
| 37 #include "mojo/edk/embedder/embedder.h" |
| 38 #include "mojo/edk/embedder/scoped_ipc_support.h" |
| 32 #include "mojo/public/cpp/bindings/binding_set.h" | 39 #include "mojo/public/cpp/bindings/binding_set.h" |
| 33 #include "services/catalog/public/interfaces/catalog.mojom.h" | 40 #include "services/catalog/public/interfaces/catalog.mojom.h" |
| 34 #include "services/catalog/public/interfaces/constants.mojom.h" | 41 #include "services/catalog/public/interfaces/constants.mojom.h" |
| 35 #include "services/service_manager/background/background_service_manager.h" | 42 #include "services/service_manager/background/background_service_manager.h" |
| 36 #include "services/service_manager/public/cpp/connector.h" | 43 #include "services/service_manager/public/cpp/connector.h" |
| 37 #include "services/service_manager/public/cpp/identity.h" | 44 #include "services/service_manager/public/cpp/identity.h" |
| 38 #include "services/service_manager/public/cpp/service.h" | 45 #include "services/service_manager/public/cpp/service.h" |
| 39 #include "services/service_manager/public/cpp/service_context.h" | 46 #include "services/service_manager/public/cpp/service_context.h" |
| 40 #include "services/service_manager/public/cpp/standalone_service/standalone_serv
ice.h" | 47 #include "services/service_manager/public/cpp/standalone_service/standalone_serv
ice.h" |
| 41 #include "services/service_manager/public/interfaces/service_factory.mojom.h" | 48 #include "services/service_manager/public/interfaces/service_factory.mojom.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 54 | 61 |
| 55 using service_manager::mojom::ServiceFactory; | 62 using service_manager::mojom::ServiceFactory; |
| 56 | 63 |
| 57 namespace { | 64 namespace { |
| 58 | 65 |
| 59 // kProcessType used to identify child processes. | 66 // kProcessType used to identify child processes. |
| 60 const char* kMashChild = "mash-child"; | 67 const char* kMashChild = "mash-child"; |
| 61 | 68 |
| 62 const char kChromeMashServiceName[] = "chrome_mash"; | 69 const char kChromeMashServiceName[] = "chrome_mash"; |
| 63 | 70 |
| 64 const char kChromeContentBrowserPackageName[] = "chrome_content_browser"; | 71 const base::FilePath::CharType kChromeMashCatalogFilename[] = |
| 65 const char kChromeContentGpuPackageName[] = "chrome_content_gpu"; | 72 FILE_PATH_LITERAL("chrome_mash_catalog.json"); |
| 66 const char kChromeContentRendererPackageName[] = "chrome_content_renderer"; | |
| 67 const char kChromeContentUtilityPackageName[] = "chrome_content_utility"; | |
| 68 | |
| 69 const char kPackagesPath[] = "Packages"; | |
| 70 const char kManifestFilename[] = "manifest.json"; | |
| 71 | |
| 72 base::FilePath GetPackageManifestPath(const std::string& package_name) { | |
| 73 base::FilePath exe = base::CommandLine::ForCurrentProcess()->GetProgram(); | |
| 74 return exe.DirName().AppendASCII(kPackagesPath).AppendASCII(package_name) | |
| 75 .AppendASCII(kManifestFilename); | |
| 76 } | |
| 77 | 73 |
| 78 bool IsChild() { | 74 bool IsChild() { |
| 79 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 75 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 80 switches::kProcessType) && | 76 switches::kProcessType) && |
| 81 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 77 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 82 switches::kProcessType) == kMashChild; | 78 switches::kProcessType) == kMashChild; |
| 83 } | 79 } |
| 84 | 80 |
| 85 void InitializeResources() { | 81 void InitializeResources() { |
| 86 ui::RegisterPathProvider(); | 82 ui::RegisterPathProvider(); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 | 161 |
| 166 if (IsChild()) | 162 if (IsChild()) |
| 167 return RunChild(); | 163 return RunChild(); |
| 168 RunMain(); | 164 RunMain(); |
| 169 return 0; | 165 return 0; |
| 170 } | 166 } |
| 171 | 167 |
| 172 void MashRunner::RunMain() { | 168 void MashRunner::RunMain() { |
| 173 base::SequencedWorkerPool::EnableWithRedirectionToTaskSchedulerForProcess(); | 169 base::SequencedWorkerPool::EnableWithRedirectionToTaskSchedulerForProcess(); |
| 174 | 170 |
| 171 mojo::edk::Init(); |
| 172 |
| 173 base::Thread ipc_thread("IPC thread"); |
| 174 ipc_thread.StartWithOptions( |
| 175 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 176 mojo::edk::ScopedIPCSupport ipc_support( |
| 177 ipc_thread.task_runner(), |
| 178 mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST); |
| 179 |
| 180 std::string catalog_contents; |
| 181 base::FilePath exe_path; |
| 182 base::PathService::Get(base::DIR_EXE, &exe_path); |
| 183 base::FilePath catalog_path = exe_path.Append(kChromeMashCatalogFilename); |
| 184 DCHECK(base::ReadFileToString(catalog_path, &catalog_contents)); |
| 185 std::unique_ptr<base::Value> manifest_value = |
| 186 base::JSONReader::Read(catalog_contents); |
| 187 DCHECK(manifest_value); |
| 188 |
| 175 // TODO(sky): refactor BackgroundServiceManager so can supply own context, we | 189 // TODO(sky): refactor BackgroundServiceManager so can supply own context, we |
| 176 // 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 |
| 177 // in chrome. | 191 // in chrome. |
| 178 ServiceProcessLauncherDelegateImpl service_process_launcher_delegate; | 192 ServiceProcessLauncherDelegateImpl service_process_launcher_delegate; |
| 179 service_manager::BackgroundServiceManager background_service_manager; | 193 service_manager::BackgroundServiceManager background_service_manager( |
| 180 std::unique_ptr<service_manager::BackgroundServiceManager::InitParams> | 194 &service_process_launcher_delegate, std::move(manifest_value)); |
| 181 init_params(new service_manager::BackgroundServiceManager::InitParams); | 195 service_manager::mojom::ServicePtr service; |
| 182 init_params->service_process_launcher_delegate = | |
| 183 &service_process_launcher_delegate; | |
| 184 background_service_manager.Init(std::move(init_params)); | |
| 185 context_.reset(new service_manager::ServiceContext( | 196 context_.reset(new service_manager::ServiceContext( |
| 186 base::MakeUnique<mash::MashPackagedService>(), | 197 base::MakeUnique<mash::MashPackagedService>(), |
| 187 background_service_manager.CreateServiceRequest(kChromeMashServiceName))); | 198 service_manager::mojom::ServiceRequest(&service))); |
| 188 | 199 background_service_manager.RegisterService( |
| 189 // We need to send a sync messages to the Catalog, so we wait for a completed | 200 service_manager::Identity( |
| 190 // connection first. | 201 kChromeMashServiceName, service_manager::mojom::kRootUserID), |
| 191 std::unique_ptr<service_manager::Connection> catalog_connection = | 202 std::move(service), nullptr); |
| 192 context_->connector()->Connect(catalog::mojom::kServiceName); | |
| 193 { | |
| 194 base::RunLoop run_loop; | |
| 195 catalog_connection->AddConnectionCompletedClosure(run_loop.QuitClosure()); | |
| 196 run_loop.Run(); | |
| 197 } | |
| 198 | |
| 199 // Synchronously override manifests needed for content process services. | |
| 200 catalog::mojom::CatalogControlPtr catalog_control; | |
| 201 catalog_connection->GetInterface(&catalog_control); | |
| 202 CHECK(catalog_control->OverrideManifestPath( | |
| 203 content::mojom::kBrowserServiceName, | |
| 204 GetPackageManifestPath(kChromeContentBrowserPackageName))); | |
| 205 CHECK(catalog_control->OverrideManifestPath( | |
| 206 content::mojom::kGpuServiceName, | |
| 207 GetPackageManifestPath(kChromeContentGpuPackageName))); | |
| 208 CHECK(catalog_control->OverrideManifestPath( | |
| 209 content::mojom::kRendererServiceName, | |
| 210 GetPackageManifestPath(kChromeContentRendererPackageName))); | |
| 211 CHECK(catalog_control->OverrideManifestPath( | |
| 212 content::mojom::kUtilityServiceName, | |
| 213 GetPackageManifestPath(kChromeContentUtilityPackageName))); | |
| 214 | 203 |
| 215 // Ping mash_session to ensure an instance is brought up | 204 // Ping mash_session to ensure an instance is brought up |
| 216 context_->connector()->Connect(mash::session::mojom::kServiceName); | 205 context_->connector()->Connect(mash::session::mojom::kServiceName); |
| 217 base::RunLoop().Run(); | 206 base::RunLoop().Run(); |
| 218 | 207 |
| 219 base::TaskScheduler::GetInstance()->Shutdown(); | 208 base::TaskScheduler::GetInstance()->Shutdown(); |
| 220 } | 209 } |
| 221 | 210 |
| 222 int MashRunner::RunChild() { | 211 int MashRunner::RunChild() { |
| 223 service_manager::WaitForDebuggerIfNecessary(); | 212 service_manager::WaitForDebuggerIfNecessary(); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 command_line->GetSwitchValueASCII(switches::kWaitForDebugger)) { | 283 command_line->GetSwitchValueASCII(switches::kWaitForDebugger)) { |
| 295 return; | 284 return; |
| 296 } | 285 } |
| 297 | 286 |
| 298 // Include the pid as logging may not have been initialized yet (the pid | 287 // Include the pid as logging may not have been initialized yet (the pid |
| 299 // printed out by logging is wrong). | 288 // printed out by logging is wrong). |
| 300 LOG(WARNING) << "waiting for debugger to attach for service " << service_name | 289 LOG(WARNING) << "waiting for debugger to attach for service " << service_name |
| 301 << " pid=" << base::Process::Current().Pid(); | 290 << " pid=" << base::Process::Current().Pid(); |
| 302 base::debug::WaitForDebugger(120, true); | 291 base::debug::WaitForDebugger(120, true); |
| 303 } | 292 } |
| OLD | NEW |