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 "base/at_exit.h" | 7 #include "base/at_exit.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/debug/debugger.h" | 10 #include "base/debug/debugger.h" |
11 #include "base/i18n/icu_util.h" | 11 #include "base/i18n/icu_util.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/process/launch.h" | 15 #include "base/process/launch.h" |
16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
17 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
18 #include "components/tracing/common/trace_to_console.h" | 18 #include "components/tracing/common/trace_to_console.h" |
19 #include "components/tracing/common/tracing_switches.h" | 19 #include "components/tracing/common/tracing_switches.h" |
20 #include "content/public/common/content_switches.h" | 20 #include "content/public/common/content_switches.h" |
| 21 #include "content/public/common/service_names.h" |
21 #include "mash/package/mash_packaged_service.h" | 22 #include "mash/package/mash_packaged_service.h" |
22 #include "mojo/public/cpp/bindings/binding_set.h" | 23 #include "mojo/public/cpp/bindings/binding_set.h" |
| 24 #include "services/catalog/public/interfaces/catalog.mojom.h" |
23 #include "services/shell/background/background_shell.h" | 25 #include "services/shell/background/background_shell.h" |
24 #include "services/shell/native_runner_delegate.h" | 26 #include "services/shell/native_runner_delegate.h" |
25 #include "services/shell/public/cpp/connector.h" | 27 #include "services/shell/public/cpp/connector.h" |
26 #include "services/shell/public/cpp/identity.h" | 28 #include "services/shell/public/cpp/identity.h" |
27 #include "services/shell/public/cpp/service.h" | 29 #include "services/shell/public/cpp/service.h" |
28 #include "services/shell/public/cpp/service_context.h" | 30 #include "services/shell/public/cpp/service_context.h" |
29 #include "services/shell/public/interfaces/service_factory.mojom.h" | 31 #include "services/shell/public/interfaces/service_factory.mojom.h" |
30 #include "services/shell/runner/common/switches.h" | 32 #include "services/shell/runner/common/switches.h" |
31 #include "services/shell/runner/host/child_process_base.h" | 33 #include "services/shell/runner/host/child_process_base.h" |
32 #include "ui/base/resource/resource_bundle.h" | 34 #include "ui/base/resource/resource_bundle.h" |
33 #include "ui/base/ui_base_paths.h" | 35 #include "ui/base/ui_base_paths.h" |
34 #include "ui/base/ui_base_switches.h" | 36 #include "ui/base/ui_base_switches.h" |
35 | 37 |
36 using shell::mojom::ServiceFactory; | 38 using shell::mojom::ServiceFactory; |
37 | 39 |
38 namespace { | 40 namespace { |
39 | 41 |
40 // kProcessType used to identify child processes. | 42 // kProcessType used to identify child processes. |
41 const char* kMashChild = "mash-child"; | 43 const char* kMashChild = "mash-child"; |
42 | 44 |
| 45 const char kChromeMashServiceName[] = "service:chrome_mash"; |
| 46 |
| 47 const char kChromeMashContentBrowserPackageName[] = |
| 48 "chrome_mash_content_browser"; |
| 49 const char kChromeContentGpuPackageName[] = "chrome_content_gpu"; |
| 50 const char kChromeContentRendererPackageName[] = "chrome_content_renderer"; |
| 51 const char kChromeContentUtilityPackageName[] = "chrome_content_utility"; |
| 52 |
| 53 const char kPackagesPath[] = "Packages"; |
| 54 const char kManifestFilename[] = "manifest.json"; |
| 55 |
| 56 base::FilePath GetPackageManifestPath(const std::string& package_name) { |
| 57 base::FilePath exe = base::CommandLine::ForCurrentProcess()->GetProgram(); |
| 58 return exe.DirName().AppendASCII(kPackagesPath).AppendASCII(package_name) |
| 59 .AppendASCII(kManifestFilename); |
| 60 } |
| 61 |
43 bool IsChild() { | 62 bool IsChild() { |
44 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 63 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
45 switches::kProcessType) && | 64 switches::kProcessType) && |
46 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 65 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
47 switches::kProcessType) == kMashChild; | 66 switches::kProcessType) == kMashChild; |
48 } | 67 } |
49 | 68 |
50 void InitializeResources() { | 69 void InitializeResources() { |
51 ui::RegisterPathProvider(); | 70 ui::RegisterPathProvider(); |
52 const std::string locale = | 71 const std::string locale = |
53 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 72 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
54 switches::kLang); | 73 switches::kLang); |
55 // This loads the Chrome's resources (chrome_100_percent.pak etc.) | 74 // This loads the Chrome's resources (chrome_100_percent.pak etc.) |
56 ui::ResourceBundle::InitSharedInstanceWithLocale( | 75 ui::ResourceBundle::InitSharedInstanceWithLocale( |
57 locale, nullptr, ui::ResourceBundle::LOAD_COMMON_RESOURCES); | 76 locale, nullptr, ui::ResourceBundle::LOAD_COMMON_RESOURCES); |
58 } | 77 } |
59 | 78 |
60 // Convert the command line program from chrome_mash to chrome. This is | |
61 // necessary as the shell will attempt to start chrome_mash. We want chrome. | |
62 void ChangeChromeMashToChrome(base::CommandLine* command_line) { | |
63 base::FilePath exe_path(command_line->GetProgram()); | |
64 #if defined(OS_WIN) | |
65 exe_path = exe_path.DirName().Append(FILE_PATH_LITERAL("chrome.exe")); | |
66 #else | |
67 exe_path = exe_path.DirName().Append(FILE_PATH_LITERAL("chrome")); | |
68 #endif | |
69 command_line->SetProgram(exe_path); | |
70 } | |
71 | |
72 class NativeRunnerDelegateImpl : public shell::NativeRunnerDelegate { | 79 class NativeRunnerDelegateImpl : public shell::NativeRunnerDelegate { |
73 public: | 80 public: |
74 NativeRunnerDelegateImpl() {} | 81 NativeRunnerDelegateImpl() {} |
75 ~NativeRunnerDelegateImpl() override {} | 82 ~NativeRunnerDelegateImpl() override {} |
76 | 83 |
77 private: | 84 private: |
78 // shell::NativeRunnerDelegate: | 85 // shell::NativeRunnerDelegate: |
79 void AdjustCommandLineArgumentsForTarget( | 86 void AdjustCommandLineArgumentsForTarget( |
80 const shell::Identity& target, | 87 const shell::Identity& target, |
81 base::CommandLine* command_line) override { | 88 base::CommandLine* command_line) override { |
82 if (target.name() != "exe:chrome") { | 89 if (target.name() != content::kBrowserServiceName) { |
83 if (target.name() == "exe:chrome_mash") | 90 // If running anything other than the browser process, launch a mash |
84 ChangeChromeMashToChrome(command_line); | 91 // child process. The new process will execute MashRunner::RunChild(). |
85 command_line->AppendSwitchASCII(switches::kProcessType, kMashChild); | 92 command_line->AppendSwitchASCII(switches::kProcessType, kMashChild); |
86 #if defined(OS_WIN) | 93 #if defined(OS_WIN) |
87 command_line->AppendArg(switches::kPrefetchArgumentOther); | 94 command_line->AppendArg(switches::kPrefetchArgumentOther); |
88 #endif | 95 #endif |
89 return; | 96 return; |
90 } | 97 } |
91 | 98 |
| 99 // When launching the browser process, ensure that we don't inherit the |
| 100 // --mash flag so it proceeds with the normal content/browser startup path. |
92 base::CommandLine::StringVector argv(command_line->argv()); | 101 base::CommandLine::StringVector argv(command_line->argv()); |
93 auto iter = | 102 auto iter = |
94 std::find(argv.begin(), argv.end(), FILE_PATH_LITERAL("--mash")); | 103 std::find(argv.begin(), argv.end(), FILE_PATH_LITERAL("--mash")); |
95 if (iter != argv.end()) | 104 if (iter != argv.end()) |
96 argv.erase(iter); | 105 argv.erase(iter); |
97 *command_line = base::CommandLine(argv); | 106 *command_line = base::CommandLine(argv); |
98 } | 107 } |
99 | 108 |
100 DISALLOW_COPY_AND_ASSIGN(NativeRunnerDelegateImpl); | 109 DISALLOW_COPY_AND_ASSIGN(NativeRunnerDelegateImpl); |
101 }; | 110 }; |
(...skipping 17 matching lines...) Expand all Loading... |
119 // in chrome. | 128 // in chrome. |
120 NativeRunnerDelegateImpl native_runner_delegate; | 129 NativeRunnerDelegateImpl native_runner_delegate; |
121 shell::BackgroundShell background_shell; | 130 shell::BackgroundShell background_shell; |
122 std::unique_ptr<shell::BackgroundShell::InitParams> init_params( | 131 std::unique_ptr<shell::BackgroundShell::InitParams> init_params( |
123 new shell::BackgroundShell::InitParams); | 132 new shell::BackgroundShell::InitParams); |
124 init_params->native_runner_delegate = &native_runner_delegate; | 133 init_params->native_runner_delegate = &native_runner_delegate; |
125 background_shell.Init(std::move(init_params)); | 134 background_shell.Init(std::move(init_params)); |
126 service_.reset(new mash::MashPackagedService); | 135 service_.reset(new mash::MashPackagedService); |
127 service_->set_context(base::MakeUnique<shell::ServiceContext>( | 136 service_->set_context(base::MakeUnique<shell::ServiceContext>( |
128 service_.get(), | 137 service_.get(), |
129 background_shell.CreateServiceRequest("exe:chrome_mash"))); | 138 background_shell.CreateServiceRequest(kChromeMashServiceName))); |
| 139 |
| 140 // We need to send a sync messages to the Catalog, so we wait for a completed |
| 141 // connection first. |
| 142 std::unique_ptr<shell::Connection> catalog_connection = |
| 143 service_->connector()->Connect("service:catalog"); |
| 144 { |
| 145 base::RunLoop run_loop; |
| 146 catalog_connection->AddConnectionCompletedClosure(run_loop.QuitClosure()); |
| 147 run_loop.Run(); |
| 148 } |
| 149 |
| 150 // Synchronously override manifests needed for content process services. |
| 151 catalog::mojom::CatalogControlPtr catalog_control; |
| 152 catalog_connection->GetInterface(&catalog_control); |
| 153 CHECK(catalog_control->OverrideManifestPath( |
| 154 content::kBrowserServiceName, |
| 155 GetPackageManifestPath(kChromeMashContentBrowserPackageName))); |
| 156 CHECK(catalog_control->OverrideManifestPath( |
| 157 content::kGpuServiceName, |
| 158 GetPackageManifestPath(kChromeContentGpuPackageName))); |
| 159 CHECK(catalog_control->OverrideManifestPath( |
| 160 content::kRendererServiceName, |
| 161 GetPackageManifestPath(kChromeContentRendererPackageName))); |
| 162 CHECK(catalog_control->OverrideManifestPath( |
| 163 content::kUtilityServiceName, |
| 164 GetPackageManifestPath(kChromeContentUtilityPackageName))); |
| 165 |
| 166 // Ping mash_session to ensure an instance is brought up |
130 service_->connector()->Connect("service:mash_session"); | 167 service_->connector()->Connect("service:mash_session"); |
131 base::RunLoop().Run(); | 168 base::RunLoop().Run(); |
132 } | 169 } |
133 | 170 |
134 void MashRunner::RunChild() { | 171 void MashRunner::RunChild() { |
135 base::i18n::InitializeICU(); | 172 base::i18n::InitializeICU(); |
136 InitializeResources(); | 173 InitializeResources(); |
137 shell::ChildProcessMainWithCallback( | 174 shell::ChildProcessMainWithCallback( |
138 base::Bind(&MashRunner::StartChildApp, base::Unretained(this))); | 175 base::Bind(&MashRunner::StartChildApp, base::Unretained(this))); |
139 } | 176 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 tracing::GetConfigForTraceToConsole(); | 215 tracing::GetConfigForTraceToConsole(); |
179 base::trace_event::TraceLog::GetInstance()->SetEnabled( | 216 base::trace_event::TraceLog::GetInstance()->SetEnabled( |
180 trace_config, | 217 trace_config, |
181 base::trace_event::TraceLog::RECORDING_MODE); | 218 base::trace_event::TraceLog::RECORDING_MODE); |
182 } | 219 } |
183 | 220 |
184 MashRunner mash_runner; | 221 MashRunner mash_runner; |
185 mash_runner.Run(); | 222 mash_runner.Run(); |
186 return 0; | 223 return 0; |
187 } | 224 } |
OLD | NEW |