OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 "mojo/shell/context.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/base_switches.h" |
| 10 #include "base/bind.h" |
| 11 #include "base/command_line.h" |
| 12 #include "base/files/file_path.h" |
| 13 #include "base/lazy_instance.h" |
| 14 #include "base/macros.h" |
| 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/scoped_vector.h" |
| 17 #include "base/path_service.h" |
| 18 #include "base/run_loop.h" |
| 19 #include "base/strings/string_split.h" |
| 20 #include "base/strings/string_util.h" |
| 21 #include "base/trace_event/trace_event.h" |
| 22 #include "build/build_config.h" |
| 23 #include "mojo/common/trace_controller_impl.h" |
| 24 #include "mojo/common/tracing_impl.h" |
| 25 #include "mojo/edk/embedder/embedder.h" |
| 26 #include "mojo/edk/embedder/simple_platform_support.h" |
| 27 #include "mojo/public/cpp/application/application_connection.h" |
| 28 #include "mojo/public/cpp/application/application_delegate.h" |
| 29 #include "mojo/public/cpp/application/application_impl.h" |
| 30 #include "mojo/services/tracing/tracing.mojom.h" |
| 31 #include "mojo/shell/application_manager/application_loader.h" |
| 32 #include "mojo/shell/application_manager/application_manager.h" |
| 33 #include "mojo/shell/command_line_util.h" |
| 34 #include "mojo/shell/filename_util.h" |
| 35 #include "mojo/shell/in_process_native_runner.h" |
| 36 #include "mojo/shell/out_of_process_native_runner.h" |
| 37 #include "mojo/shell/switches.h" |
| 38 #include "url/gurl.h" |
| 39 |
| 40 namespace mojo { |
| 41 namespace shell { |
| 42 namespace { |
| 43 |
| 44 // Used to ensure we only init once. |
| 45 class Setup { |
| 46 public: |
| 47 Setup() { |
| 48 embedder::Init(make_scoped_ptr(new embedder::SimplePlatformSupport())); |
| 49 } |
| 50 |
| 51 ~Setup() {} |
| 52 |
| 53 private: |
| 54 DISALLOW_COPY_AND_ASSIGN(Setup); |
| 55 }; |
| 56 |
| 57 bool ConfigureURLMappings(const base::CommandLine& command_line, |
| 58 Context* context) { |
| 59 URLResolver* resolver = context->url_resolver(); |
| 60 |
| 61 // Configure the resolution of unknown mojo: URLs. |
| 62 GURL base_url; |
| 63 if (command_line.HasSwitch(switches::kOrigin)) |
| 64 base_url = GURL(command_line.GetSwitchValueASCII(switches::kOrigin)); |
| 65 else |
| 66 // Use the shell's file root if the base was not specified. |
| 67 base_url = context->ResolveShellFileURL(""); |
| 68 |
| 69 if (!base_url.is_valid()) |
| 70 return false; |
| 71 |
| 72 resolver->SetMojoBaseURL(base_url); |
| 73 |
| 74 // The network service must be loaded from the filesystem. |
| 75 // This mapping is done before the command line URL mapping are processed, so |
| 76 // that it can be overridden. |
| 77 resolver->AddURLMapping( |
| 78 GURL("mojo:network_service"), |
| 79 context->ResolveShellFileURL("file:network_service.mojo")); |
| 80 |
| 81 // Command line URL mapping. |
| 82 std::vector<URLResolver::OriginMapping> origin_mappings = |
| 83 URLResolver::GetOriginMappings(command_line.argv()); |
| 84 for (const auto& origin_mapping : origin_mappings) |
| 85 resolver->AddOriginMapping(GURL(origin_mapping.origin), |
| 86 GURL(origin_mapping.base_url)); |
| 87 |
| 88 if (command_line.HasSwitch(switches::kURLMappings)) { |
| 89 const std::string mappings = |
| 90 command_line.GetSwitchValueASCII(switches::kURLMappings); |
| 91 |
| 92 base::StringPairs pairs; |
| 93 if (!base::SplitStringIntoKeyValuePairs(mappings, '=', ',', &pairs)) |
| 94 return false; |
| 95 using StringPair = std::pair<std::string, std::string>; |
| 96 for (const StringPair& pair : pairs) { |
| 97 const GURL from(pair.first); |
| 98 const GURL to = context->ResolveCommandLineURL(pair.second); |
| 99 if (!from.is_valid() || !to.is_valid()) |
| 100 return false; |
| 101 resolver->AddURLMapping(from, to); |
| 102 } |
| 103 } |
| 104 return true; |
| 105 } |
| 106 |
| 107 void InitContentHandlers(ApplicationManager* manager, |
| 108 const base::CommandLine& command_line) { |
| 109 // Default content handlers. |
| 110 manager->RegisterContentHandler("application/pdf", GURL("mojo:pdf_viewer")); |
| 111 manager->RegisterContentHandler("image/png", GURL("mojo:png_viewer")); |
| 112 manager->RegisterContentHandler("text/html", GURL("mojo:html_viewer")); |
| 113 |
| 114 // Command-line-specified content handlers. |
| 115 std::string handlers_spec = |
| 116 command_line.GetSwitchValueASCII(switches::kContentHandlers); |
| 117 if (handlers_spec.empty()) |
| 118 return; |
| 119 |
| 120 #if defined(OS_ANDROID) |
| 121 // TODO(eseidel): On Android we pass command line arguments is via the |
| 122 // 'parameters' key on the intent, which we specify during 'am shell start' |
| 123 // via --esa, however that expects comma-separated values and says: |
| 124 // am shell --help: |
| 125 // [--esa <EXTRA_KEY> <EXTRA_STRING_VALUE>[,<EXTRA_STRING_VALUE...]] |
| 126 // (to embed a comma into a string escape it using "\,") |
| 127 // Whatever takes 'parameters' and constructs a CommandLine is failing to |
| 128 // un-escape the commas, we need to move this fix to that file. |
| 129 ReplaceSubstringsAfterOffset(&handlers_spec, 0, "\\,", ","); |
| 130 #endif |
| 131 |
| 132 std::vector<std::string> parts; |
| 133 base::SplitString(handlers_spec, ',', &parts); |
| 134 if (parts.size() % 2 != 0) { |
| 135 LOG(ERROR) << "Invalid value for switch " << switches::kContentHandlers |
| 136 << ": must be a comma-separated list of mimetype/url pairs." |
| 137 << handlers_spec; |
| 138 return; |
| 139 } |
| 140 |
| 141 for (size_t i = 0; i < parts.size(); i += 2) { |
| 142 GURL url(parts[i + 1]); |
| 143 if (!url.is_valid()) { |
| 144 LOG(ERROR) << "Invalid value for switch " << switches::kContentHandlers |
| 145 << ": '" << parts[i + 1] << "' is not a valid URL."; |
| 146 return; |
| 147 } |
| 148 // TODO(eseidel): We should also validate that the mimetype is valid |
| 149 // net/base/mime_util.h could do this, but we don't want to depend on net. |
| 150 manager->RegisterContentHandler(parts[i], url); |
| 151 } |
| 152 } |
| 153 |
| 154 void InitNativeOptions(ApplicationManager* manager, |
| 155 const base::CommandLine& command_line) { |
| 156 std::vector<std::string> force_in_process_url_list; |
| 157 base::SplitString(command_line.GetSwitchValueASCII(switches::kForceInProcess), |
| 158 ',', &force_in_process_url_list); |
| 159 for (const auto& force_in_process_url : force_in_process_url_list) { |
| 160 GURL gurl(force_in_process_url); |
| 161 if (!gurl.is_valid()) { |
| 162 LOG(ERROR) << "Invalid value for switch " << switches::kForceInProcess |
| 163 << ": '" << force_in_process_url << "'is not a valid URL."; |
| 164 return; |
| 165 } |
| 166 |
| 167 NativeRunnerFactory::Options options; |
| 168 options.force_in_process = true; |
| 169 manager->SetNativeOptionsForURL(options, gurl); |
| 170 } |
| 171 } |
| 172 |
| 173 class TracingServiceProvider : public ServiceProvider { |
| 174 public: |
| 175 explicit TracingServiceProvider(InterfaceRequest<ServiceProvider> request) |
| 176 : binding_(this, request.Pass()) {} |
| 177 ~TracingServiceProvider() override {} |
| 178 |
| 179 void ConnectToService(const String& service_name, |
| 180 ScopedMessagePipeHandle client_handle) override { |
| 181 if (service_name == tracing::TraceController::Name_) { |
| 182 new TraceControllerImpl( |
| 183 MakeRequest<tracing::TraceController>(client_handle.Pass())); |
| 184 } |
| 185 } |
| 186 |
| 187 private: |
| 188 StrongBinding<ServiceProvider> binding_; |
| 189 |
| 190 DISALLOW_COPY_AND_ASSIGN(TracingServiceProvider); |
| 191 }; |
| 192 |
| 193 } // namespace |
| 194 |
| 195 Context::Context() : application_manager_(this) { |
| 196 DCHECK(!base::MessageLoop::current()); |
| 197 |
| 198 // By default assume that the local apps reside alongside the shell. |
| 199 // TODO(ncbray): really, this should be passed in rather than defaulting. |
| 200 // This default makes sense for desktop but not Android. |
| 201 base::FilePath shell_dir; |
| 202 PathService::Get(base::DIR_MODULE, &shell_dir); |
| 203 SetShellFileRoot(shell_dir); |
| 204 |
| 205 base::FilePath cwd; |
| 206 PathService::Get(base::DIR_CURRENT, &cwd); |
| 207 SetCommandLineCWD(cwd); |
| 208 } |
| 209 |
| 210 Context::~Context() { |
| 211 DCHECK(!base::MessageLoop::current()); |
| 212 } |
| 213 |
| 214 // static |
| 215 void Context::EnsureEmbedderIsInitialized() { |
| 216 static base::LazyInstance<Setup>::Leaky setup = LAZY_INSTANCE_INITIALIZER; |
| 217 setup.Get(); |
| 218 } |
| 219 |
| 220 void Context::SetShellFileRoot(const base::FilePath& path) { |
| 221 shell_file_root_ = AddTrailingSlashIfNeeded(FilePathToFileURL(path)); |
| 222 } |
| 223 |
| 224 GURL Context::ResolveShellFileURL(const std::string& path) { |
| 225 return shell_file_root_.Resolve(path); |
| 226 } |
| 227 |
| 228 void Context::SetCommandLineCWD(const base::FilePath& path) { |
| 229 command_line_cwd_ = AddTrailingSlashIfNeeded(FilePathToFileURL(path)); |
| 230 } |
| 231 |
| 232 GURL Context::ResolveCommandLineURL(const std::string& path) { |
| 233 return command_line_cwd_.Resolve(path); |
| 234 } |
| 235 |
| 236 bool Context::Init() { |
| 237 TRACE_EVENT0("mojo_shell", "Context::Init"); |
| 238 const base::CommandLine& command_line = |
| 239 *base::CommandLine::ForCurrentProcess(); |
| 240 |
| 241 if (command_line.HasSwitch(switches::kWaitForDebugger)) |
| 242 base::debug::WaitForDebugger(60, true); |
| 243 |
| 244 EnsureEmbedderIsInitialized(); |
| 245 task_runners_.reset( |
| 246 new TaskRunners(base::MessageLoop::current()->message_loop_proxy())); |
| 247 |
| 248 // TODO(vtl): Probably these failures should be checked before |Init()|, and |
| 249 // this function simply shouldn't fail. |
| 250 if (!shell_file_root_.is_valid()) |
| 251 return false; |
| 252 if (!ConfigureURLMappings(command_line, this)) |
| 253 return false; |
| 254 |
| 255 // TODO(vtl): This should be MASTER, not NONE. |
| 256 embedder::InitIPCSupport( |
| 257 embedder::ProcessType::NONE, task_runners_->shell_runner(), this, |
| 258 task_runners_->io_runner(), embedder::ScopedPlatformHandle()); |
| 259 |
| 260 scoped_ptr<NativeRunnerFactory> runner_factory; |
| 261 if (command_line.HasSwitch(switches::kEnableMultiprocess)) |
| 262 runner_factory.reset(new OutOfProcessNativeRunnerFactory(this)); |
| 263 else |
| 264 runner_factory.reset(new InProcessNativeRunnerFactory(this)); |
| 265 application_manager_.set_blocking_pool(task_runners_->blocking_pool()); |
| 266 application_manager_.set_native_runner_factory(runner_factory.Pass()); |
| 267 application_manager_.set_disable_cache( |
| 268 base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 269 switches::kDisableCache)); |
| 270 |
| 271 InitContentHandlers(&application_manager_, command_line); |
| 272 InitNativeOptions(&application_manager_, command_line); |
| 273 |
| 274 ServiceProviderPtr tracing_service_provider_ptr; |
| 275 new TracingServiceProvider(GetProxy(&tracing_service_provider_ptr)); |
| 276 application_manager_.ConnectToApplication( |
| 277 GURL("mojo:tracing"), GURL(""), nullptr, |
| 278 tracing_service_provider_ptr.Pass(), base::Closure()); |
| 279 |
| 280 return true; |
| 281 } |
| 282 |
| 283 void Context::Shutdown() { |
| 284 TRACE_EVENT0("mojo_shell", "Context::Shutdown"); |
| 285 DCHECK_EQ(base::MessageLoop::current()->task_runner(), |
| 286 task_runners_->shell_runner()); |
| 287 embedder::ShutdownIPCSupport(); |
| 288 // We'll quit when we get OnShutdownComplete(). |
| 289 base::MessageLoop::current()->Run(); |
| 290 } |
| 291 |
| 292 GURL Context::ResolveURL(const GURL& url) { |
| 293 return url_resolver_.ResolveMojoURL(url); |
| 294 } |
| 295 |
| 296 GURL Context::ResolveMappings(const GURL& url) { |
| 297 return url_resolver_.ApplyMappings(url); |
| 298 } |
| 299 |
| 300 void Context::OnShutdownComplete() { |
| 301 DCHECK_EQ(base::MessageLoop::current()->task_runner(), |
| 302 task_runners_->shell_runner()); |
| 303 base::MessageLoop::current()->Quit(); |
| 304 } |
| 305 |
| 306 void Context::Run(const GURL& url) { |
| 307 ServiceProviderPtr services; |
| 308 ServiceProviderPtr exposed_services; |
| 309 |
| 310 app_urls_.insert(url); |
| 311 application_manager_.ConnectToApplication( |
| 312 url, GURL(), GetProxy(&services), exposed_services.Pass(), |
| 313 base::Bind(&Context::OnApplicationEnd, base::Unretained(this), url)); |
| 314 } |
| 315 |
| 316 void Context::OnApplicationEnd(const GURL& url) { |
| 317 if (app_urls_.find(url) != app_urls_.end()) { |
| 318 app_urls_.erase(url); |
| 319 if (app_urls_.empty() && base::MessageLoop::current()->is_running()) { |
| 320 DCHECK_EQ(base::MessageLoop::current()->task_runner(), |
| 321 task_runners_->shell_runner()); |
| 322 base::MessageLoop::current()->Quit(); |
| 323 } |
| 324 } |
| 325 } |
| 326 |
| 327 } // namespace shell |
| 328 } // namespace mojo |
OLD | NEW |