OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/app/mash/mash_runner.h" |
| 6 |
| 7 #include "ash/mus/sysui_application.h" |
| 8 #include "base/at_exit.h" |
| 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" |
| 11 #include "base/debug/debugger.h" |
| 12 #include "base/i18n/icu_util.h" |
| 13 #include "base/logging.h" |
| 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/process/launch.h" |
| 16 #include "components/mus/mus_app.h" |
| 17 #include "components/resource_provider/resource_provider_app.h" |
| 18 #include "content/public/common/content_switches.h" |
| 19 #include "mash/quick_launch/quick_launch_application.h" |
| 20 #include "mash/shell/shell_application_delegate.h" |
| 21 #include "mash/wm/window_manager_application.h" |
| 22 #include "mojo/common/mojo_scheme_register.h" |
| 23 #include "mojo/public/cpp/bindings/weak_binding_set.h" |
| 24 #include "mojo/shell/background/background_shell.h" |
| 25 #include "mojo/shell/identity.h" |
| 26 #include "mojo/shell/native_runner_delegate.h" |
| 27 #include "mojo/shell/public/cpp/shell_client.h" |
| 28 #include "mojo/shell/public/cpp/shell_connection.h" |
| 29 #include "mojo/shell/public/interfaces/shell_client_factory.mojom.h" |
| 30 #include "mojo/shell/runner/common/switches.h" |
| 31 #include "mojo/shell/runner/host/child_process_base.h" |
| 32 #include "url/gurl.h" |
| 33 #include "url/url_util.h" |
| 34 |
| 35 #if defined(OS_LINUX) |
| 36 #include "components/font_service/font_service_app.h" |
| 37 #endif |
| 38 |
| 39 using mojo::shell::mojom::ShellClientFactory; |
| 40 |
| 41 namespace { |
| 42 |
| 43 // kProcessType used to identify child processes. |
| 44 const char* kMashChild = "mash-child"; |
| 45 |
| 46 // ShellClient responsible for starting the appropriate app. |
| 47 class DefaultShellClient : public mojo::ShellClient, |
| 48 public ShellClientFactory, |
| 49 public mojo::InterfaceFactory<ShellClientFactory> { |
| 50 public: |
| 51 DefaultShellClient() {} |
| 52 ~DefaultShellClient() override {} |
| 53 |
| 54 // mojo::ShellClient: |
| 55 bool AcceptConnection(mojo::Connection* connection) override { |
| 56 connection->AddInterface<ShellClientFactory>(this); |
| 57 return true; |
| 58 } |
| 59 |
| 60 // mojo::InterfaceFactory<ShellClientFactory> |
| 61 void Create(mojo::Connection* connection, |
| 62 mojo::InterfaceRequest<ShellClientFactory> request) override { |
| 63 shell_client_factory_bindings_.AddBinding(this, std::move(request)); |
| 64 } |
| 65 |
| 66 // ShellClientFactory: |
| 67 void CreateShellClient(mojo::shell::mojom::ShellClientRequest request, |
| 68 const mojo::String& mojo_url) override { |
| 69 const GURL url = GURL(std::string(mojo_url)); |
| 70 if (shell_client_) { |
| 71 LOG(ERROR) << "request to create additional app " << url; |
| 72 return; |
| 73 } |
| 74 shell_client_ = CreateShellClient(url); |
| 75 if (shell_client_) { |
| 76 shell_connection_.reset( |
| 77 new mojo::ShellConnection(shell_client_.get(), std::move(request))); |
| 78 return; |
| 79 } |
| 80 LOG(ERROR) << "unknown url " << url; |
| 81 NOTREACHED(); |
| 82 } |
| 83 |
| 84 private: |
| 85 // TODO(sky): move this into mash. |
| 86 scoped_ptr<mojo::ShellClient> CreateShellClient(const GURL& url) { |
| 87 if (url == GURL("mojo:ash_sysui")) |
| 88 return make_scoped_ptr(new ash::sysui::SysUIApplication); |
| 89 if (url == GURL("mojo:desktop_wm")) |
| 90 return make_scoped_ptr(new mash::wm::WindowManagerApplication); |
| 91 if (url == GURL("mojo:mash_shell")) |
| 92 return make_scoped_ptr(new mash::shell::ShellApplicationDelegate); |
| 93 if (url == GURL("mojo:mus")) |
| 94 return make_scoped_ptr(new mus::MandolineUIServicesApp); |
| 95 if (url == GURL("mojo:quick_launch")) |
| 96 return make_scoped_ptr(new mash::quick_launch::QuickLaunchApplication); |
| 97 if (url == GURL("mojo:resource_provider")) { |
| 98 return make_scoped_ptr( |
| 99 new resource_provider::ResourceProviderApp("mojo:resource_provider")); |
| 100 } |
| 101 #if defined(OS_LINUX) |
| 102 if (url == GURL("mojo:font_service")) |
| 103 return make_scoped_ptr(new font_service::FontServiceApp); |
| 104 #endif |
| 105 return nullptr; |
| 106 } |
| 107 |
| 108 mojo::WeakBindingSet<ShellClientFactory> shell_client_factory_bindings_; |
| 109 scoped_ptr<mojo::ShellClient> shell_client_; |
| 110 scoped_ptr<mojo::ShellConnection> shell_connection_; |
| 111 |
| 112 DISALLOW_COPY_AND_ASSIGN(DefaultShellClient); |
| 113 }; |
| 114 |
| 115 bool IsChild() { |
| 116 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 117 switches::kProcessType) && |
| 118 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 119 switches::kProcessType) == kMashChild; |
| 120 } |
| 121 |
| 122 // Convert the command line program from chrome_mash to chrome. This is |
| 123 // necessary as the shell will attempt to start chrome_mash. We want chrome. |
| 124 void ChangeChromeMashToChrome(base::CommandLine* command_line) { |
| 125 base::FilePath exe_path(command_line->GetProgram()); |
| 126 #if defined(OS_WIN) |
| 127 exe_path = exe_path.DirName().Append(FILE_PATH_LITERAL("chrome.exe")); |
| 128 #else |
| 129 exe_path = exe_path.DirName().Append(FILE_PATH_LITERAL("chrome")); |
| 130 #endif |
| 131 command_line->SetProgram(exe_path); |
| 132 } |
| 133 |
| 134 class NativeRunnerDelegateImpl : public mojo::shell::NativeRunnerDelegate { |
| 135 public: |
| 136 NativeRunnerDelegateImpl() {} |
| 137 ~NativeRunnerDelegateImpl() override {} |
| 138 |
| 139 private: |
| 140 // mojo::shell::NativeRunnerDelegate: |
| 141 void AdjustCommandLineArgumentsForTarget( |
| 142 const mojo::shell::Identity& target, |
| 143 base::CommandLine* command_line) override { |
| 144 if (target.url() != GURL("exe:chrome")) { |
| 145 if (target.url() == GURL("exe:chrome_mash")) |
| 146 ChangeChromeMashToChrome(command_line); |
| 147 command_line->AppendSwitchASCII(switches::kProcessType, kMashChild); |
| 148 #if defined(OS_WIN) |
| 149 command_line->AppendArg(switches::kPrefetchArgumentOther); |
| 150 #endif |
| 151 return; |
| 152 } |
| 153 |
| 154 base::CommandLine::StringVector argv(command_line->argv()); |
| 155 auto iter = |
| 156 std::find(argv.begin(), argv.end(), FILE_PATH_LITERAL("--mash")); |
| 157 if (iter != argv.end()) |
| 158 argv.erase(iter); |
| 159 *command_line = base::CommandLine(argv); |
| 160 } |
| 161 |
| 162 DISALLOW_COPY_AND_ASSIGN(NativeRunnerDelegateImpl); |
| 163 }; |
| 164 |
| 165 } // namespace |
| 166 |
| 167 MashRunner::MashRunner() {} |
| 168 |
| 169 MashRunner::~MashRunner() {} |
| 170 |
| 171 void MashRunner::Run() { |
| 172 if (IsChild()) |
| 173 RunChild(); |
| 174 else |
| 175 RunMain(); |
| 176 } |
| 177 |
| 178 void MashRunner::RunMain() { |
| 179 // TODO(sky): refactor backgroundshell so can supply own context, we |
| 180 // shouldn't we using context as it has a lot of stuff we don't really want |
| 181 // in chrome. |
| 182 NativeRunnerDelegateImpl native_runner_delegate; |
| 183 mojo::shell::BackgroundShell background_shell; |
| 184 background_shell.Init(&native_runner_delegate); |
| 185 shell_client_.reset(new DefaultShellClient); |
| 186 shell_connection_.reset(new mojo::ShellConnection( |
| 187 shell_client_.get(), |
| 188 background_shell.CreateShellClientRequest(GURL("exe:chrome_mash")))); |
| 189 shell_connection_->WaitForInitialize(); |
| 190 static_cast<mojo::Shell*>(shell_connection_.get()) |
| 191 ->Connect("mojo:mash_shell"); |
| 192 base::MessageLoop::current()->Run(); |
| 193 } |
| 194 |
| 195 void MashRunner::RunChild() { |
| 196 base::i18n::InitializeICU(); |
| 197 mojo::shell::ChildProcessMain( |
| 198 base::Bind(&MashRunner::StartChildApp, base::Unretained(this))); |
| 199 } |
| 200 |
| 201 void MashRunner::StartChildApp( |
| 202 mojo::shell::mojom::ShellClientRequest client_request) { |
| 203 // TODO(sky): use MessagePumpMojo. |
| 204 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); |
| 205 shell_client_.reset(new DefaultShellClient); |
| 206 shell_connection_.reset(new mojo::ShellConnection(shell_client_.get(), |
| 207 std::move(client_request))); |
| 208 message_loop.Run(); |
| 209 } |
| 210 |
| 211 int MashMain() { |
| 212 #if defined(OS_WIN) |
| 213 base::RouteStdioToConsole(false); |
| 214 #endif |
| 215 // TODO(sky): wire this up correctly. |
| 216 logging::LoggingSettings settings; |
| 217 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; |
| 218 logging::InitLogging(settings); |
| 219 // To view log output with IDs and timestamps use "adb logcat -v threadtime". |
| 220 logging::SetLogItems(false, // Process ID |
| 221 false, // Thread ID |
| 222 false, // Timestamp |
| 223 false); // Tick count |
| 224 |
| 225 mojo::RegisterMojoSchemes(); |
| 226 // TODO(sky): use MessagePumpMojo. |
| 227 scoped_ptr<base::MessageLoop> message_loop; |
| 228 #if defined(OS_LINUX) |
| 229 base::AtExitManager exit_manager; |
| 230 #endif |
| 231 if (!IsChild()) |
| 232 message_loop.reset(new base::MessageLoop(base::MessageLoop::TYPE_UI)); |
| 233 MashRunner mash_runner; |
| 234 mash_runner.Run(); |
| 235 return 0; |
| 236 } |
OLD | NEW |