| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "mojo/runner/context.h" | 5 #include "mojo/runner/context.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 23 #include "components/devtools_service/public/cpp/switches.h" | 23 #include "components/devtools_service/public/cpp/switches.h" |
| 24 #include "components/devtools_service/public/interfaces/devtools_service.mojom.h
" | 24 #include "components/devtools_service/public/interfaces/devtools_service.mojom.h
" |
| 25 #include "mojo/application/public/cpp/application_connection.h" | 25 #include "mojo/application/public/cpp/application_connection.h" |
| 26 #include "mojo/application/public/cpp/application_delegate.h" | 26 #include "mojo/application/public/cpp/application_delegate.h" |
| 27 #include "mojo/application/public/cpp/application_impl.h" | 27 #include "mojo/application/public/cpp/application_impl.h" |
| 28 #include "mojo/common/trace_controller_impl.h" | 28 #include "mojo/common/trace_controller_impl.h" |
| 29 #include "mojo/common/tracing_impl.h" | 29 #include "mojo/common/tracing_impl.h" |
| 30 #include "mojo/edk/embedder/embedder.h" | 30 #include "mojo/edk/embedder/embedder.h" |
| 31 #include "mojo/edk/embedder/simple_platform_support.h" | 31 #include "mojo/edk/embedder/simple_platform_support.h" |
| 32 #include "mojo/runner/about_fetcher.h" | 32 #include "mojo/fetcher/base_application_fetcher.h" |
| 33 #include "mojo/runner/in_process_native_runner.h" | 33 #include "mojo/runner/in_process_native_runner.h" |
| 34 #include "mojo/runner/out_of_process_native_runner.h" | 34 #include "mojo/runner/out_of_process_native_runner.h" |
| 35 #include "mojo/runner/switches.h" | 35 #include "mojo/runner/switches.h" |
| 36 #include "mojo/services/tracing/public/cpp/switches.h" | 36 #include "mojo/services/tracing/public/cpp/switches.h" |
| 37 #include "mojo/services/tracing/public/interfaces/tracing.mojom.h" | 37 #include "mojo/services/tracing/public/interfaces/tracing.mojom.h" |
| 38 #include "mojo/shell/application_loader.h" | 38 #include "mojo/shell/application_loader.h" |
| 39 #include "mojo/shell/application_manager.h" | |
| 40 #include "mojo/shell/connect_to_application_params.h" | 39 #include "mojo/shell/connect_to_application_params.h" |
| 40 #include "mojo/shell/query_util.h" |
| 41 #include "mojo/shell/switches.h" | 41 #include "mojo/shell/switches.h" |
| 42 #include "mojo/util/filename_util.h" | 42 #include "mojo/util/filename_util.h" |
| 43 #include "url/gurl.h" | 43 #include "url/gurl.h" |
| 44 | 44 |
| 45 namespace mojo { | 45 namespace mojo { |
| 46 namespace runner { | 46 namespace runner { |
| 47 namespace { | 47 namespace { |
| 48 | 48 |
| 49 // Used to ensure we only init once. | 49 // Used to ensure we only init once. |
| 50 class Setup { | 50 class Setup { |
| 51 public: | 51 public: |
| 52 Setup() { | 52 Setup() { |
| 53 embedder::Init(make_scoped_ptr(new embedder::SimplePlatformSupport())); | 53 embedder::Init(make_scoped_ptr(new embedder::SimplePlatformSupport())); |
| 54 } | 54 } |
| 55 | 55 |
| 56 ~Setup() {} | 56 ~Setup() {} |
| 57 | 57 |
| 58 private: | 58 private: |
| 59 DISALLOW_COPY_AND_ASSIGN(Setup); | 59 DISALLOW_COPY_AND_ASSIGN(Setup); |
| 60 }; | 60 }; |
| 61 | 61 |
| 62 bool ConfigureURLMappings(const base::CommandLine& command_line, | |
| 63 Context* context) { | |
| 64 URLResolver* resolver = context->url_resolver(); | |
| 65 | |
| 66 // Configure the resolution of unknown mojo: URLs. | |
| 67 GURL base_url; | |
| 68 if (!command_line.HasSwitch(switches::kUseUpdater)) { | |
| 69 // Use the shell's file root if the base was not specified. | |
| 70 base_url = context->ResolveShellFileURL(std::string()); | |
| 71 } | |
| 72 | |
| 73 if (base_url.is_valid()) | |
| 74 resolver->SetMojoBaseURL(base_url); | |
| 75 | |
| 76 // The network service and updater must be loaded from the filesystem. | |
| 77 // This mapping is done before the command line URL mapping are processed, so | |
| 78 // that it can be overridden. | |
| 79 resolver->AddURLMapping(GURL("mojo:network_service"), | |
| 80 context->ResolveShellFileURL( | |
| 81 "file:network_service/network_service.mojo")); | |
| 82 resolver->AddURLMapping( | |
| 83 GURL("mojo:updater"), | |
| 84 context->ResolveShellFileURL("file:updater/updater.mojo")); | |
| 85 | |
| 86 // Command line URL mapping. | |
| 87 std::vector<URLResolver::OriginMapping> origin_mappings = | |
| 88 URLResolver::GetOriginMappings(command_line.argv()); | |
| 89 for (const auto& origin_mapping : origin_mappings) | |
| 90 resolver->AddOriginMapping(GURL(origin_mapping.origin), | |
| 91 GURL(origin_mapping.base_url)); | |
| 92 | |
| 93 if (command_line.HasSwitch(switches::kURLMappings)) { | |
| 94 const std::string mappings = | |
| 95 command_line.GetSwitchValueASCII(switches::kURLMappings); | |
| 96 | |
| 97 base::StringPairs pairs; | |
| 98 if (!base::SplitStringIntoKeyValuePairs(mappings, '=', ',', &pairs)) | |
| 99 return false; | |
| 100 using StringPair = std::pair<std::string, std::string>; | |
| 101 for (const StringPair& pair : pairs) { | |
| 102 const GURL from(pair.first); | |
| 103 const GURL to = context->ResolveCommandLineURL(pair.second); | |
| 104 if (!from.is_valid() || !to.is_valid()) | |
| 105 return false; | |
| 106 resolver->AddURLMapping(from, to); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 return true; | |
| 111 } | |
| 112 | |
| 113 void InitContentHandlers(shell::ApplicationManager* manager, | 62 void InitContentHandlers(shell::ApplicationManager* manager, |
| 114 const base::CommandLine& command_line) { | 63 const base::CommandLine& command_line) { |
| 115 // Default content handlers. | 64 // Default content handlers. |
| 116 manager->RegisterContentHandler("application/pdf", GURL("mojo:pdf_viewer")); | 65 manager->RegisterContentHandler("application/pdf", GURL("mojo:pdf_viewer")); |
| 117 manager->RegisterContentHandler("image/png", GURL("mojo:png_viewer")); | 66 manager->RegisterContentHandler("image/png", GURL("mojo:png_viewer")); |
| 118 manager->RegisterContentHandler("text/html", GURL("mojo:html_viewer")); | 67 manager->RegisterContentHandler("text/html", GURL("mojo:html_viewer")); |
| 119 | 68 |
| 120 // Command-line-specified content handlers. | 69 // Command-line-specified content handlers. |
| 121 std::string handlers_spec = | 70 std::string handlers_spec = |
| 122 command_line.GetSwitchValueASCII(switches::kContentHandlers); | 71 command_line.GetSwitchValueASCII(switches::kContentHandlers); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 } | 172 } |
| 224 | 173 |
| 225 private: | 174 private: |
| 226 StrongBinding<ServiceProvider> binding_; | 175 StrongBinding<ServiceProvider> binding_; |
| 227 | 176 |
| 228 DISALLOW_COPY_AND_ASSIGN(TracingServiceProvider); | 177 DISALLOW_COPY_AND_ASSIGN(TracingServiceProvider); |
| 229 }; | 178 }; |
| 230 | 179 |
| 231 } // namespace | 180 } // namespace |
| 232 | 181 |
| 233 Context::Context() | 182 Context::Context(const base::FilePath& shell_file_root) |
| 234 : application_manager_(this), main_entry_time_(base::Time::Now()) { | 183 : application_manager_(new shell::ApplicationManager(make_scoped_ptr( |
| 235 DCHECK(!base::MessageLoop::current()); | 184 new fetcher::BaseApplicationFetcher(shell_file_root)))), |
| 236 | 185 main_entry_time_(base::Time::Now()) {} |
| 237 // By default assume that the local apps reside alongside the shell. | |
| 238 // TODO(ncbray): really, this should be passed in rather than defaulting. | |
| 239 // This default makes sense for desktop but not Android. | |
| 240 base::FilePath shell_dir; | |
| 241 PathService::Get(base::DIR_MODULE, &shell_dir); | |
| 242 SetShellFileRoot(shell_dir); | |
| 243 | |
| 244 base::FilePath cwd; | |
| 245 PathService::Get(base::DIR_CURRENT, &cwd); | |
| 246 SetCommandLineCWD(cwd); | |
| 247 } | |
| 248 | 186 |
| 249 Context::~Context() { | 187 Context::~Context() { |
| 250 DCHECK(!base::MessageLoop::current()); | 188 DCHECK(!base::MessageLoop::current()); |
| 251 } | 189 } |
| 252 | 190 |
| 253 // static | 191 // static |
| 254 void Context::EnsureEmbedderIsInitialized() { | 192 void Context::EnsureEmbedderIsInitialized() { |
| 255 static base::LazyInstance<Setup>::Leaky setup = LAZY_INSTANCE_INITIALIZER; | 193 static base::LazyInstance<Setup>::Leaky setup = LAZY_INSTANCE_INITIALIZER; |
| 256 setup.Get(); | 194 setup.Get(); |
| 257 } | 195 } |
| 258 | 196 |
| 259 void Context::SetShellFileRoot(const base::FilePath& path) { | |
| 260 shell_file_root_ = | |
| 261 util::AddTrailingSlashIfNeeded(util::FilePathToFileURL(path)); | |
| 262 } | |
| 263 | |
| 264 GURL Context::ResolveShellFileURL(const std::string& path) { | |
| 265 return shell_file_root_.Resolve(path); | |
| 266 } | |
| 267 | |
| 268 void Context::SetCommandLineCWD(const base::FilePath& path) { | |
| 269 command_line_cwd_ = | |
| 270 util::AddTrailingSlashIfNeeded(util::FilePathToFileURL(path)); | |
| 271 } | |
| 272 | |
| 273 GURL Context::ResolveCommandLineURL(const std::string& path) { | |
| 274 return command_line_cwd_.Resolve(path); | |
| 275 } | |
| 276 | |
| 277 bool Context::Init() { | 197 bool Context::Init() { |
| 278 TRACE_EVENT0("mojo_shell", "Context::Init"); | 198 TRACE_EVENT0("mojo_shell", "Context::Init"); |
| 279 const base::CommandLine& command_line = | 199 const base::CommandLine& command_line = |
| 280 *base::CommandLine::ForCurrentProcess(); | 200 *base::CommandLine::ForCurrentProcess(); |
| 281 | 201 |
| 282 EnsureEmbedderIsInitialized(); | 202 EnsureEmbedderIsInitialized(); |
| 283 task_runners_.reset( | 203 task_runners_.reset( |
| 284 new TaskRunners(base::MessageLoop::current()->task_runner())); | 204 new TaskRunners(base::MessageLoop::current()->task_runner())); |
| 285 | 205 |
| 286 // TODO(vtl): Probably these failures should be checked before |Init()|, and | |
| 287 // this function simply shouldn't fail. | |
| 288 if (!shell_file_root_.is_valid()) | |
| 289 return false; | |
| 290 if (!ConfigureURLMappings(command_line, this)) | |
| 291 return false; | |
| 292 | |
| 293 // TODO(vtl): This should be MASTER, not NONE. | 206 // TODO(vtl): This should be MASTER, not NONE. |
| 294 embedder::InitIPCSupport( | 207 embedder::InitIPCSupport( |
| 295 embedder::ProcessType::NONE, task_runners_->shell_runner(), this, | 208 embedder::ProcessType::NONE, task_runners_->shell_runner(), this, |
| 296 task_runners_->io_runner(), embedder::ScopedPlatformHandle()); | 209 task_runners_->io_runner(), embedder::ScopedPlatformHandle()); |
| 297 | 210 |
| 298 scoped_ptr<shell::NativeRunnerFactory> runner_factory; | 211 scoped_ptr<shell::NativeRunnerFactory> runner_factory; |
| 299 if (command_line.HasSwitch(switches::kEnableMultiprocess)) | 212 if (command_line.HasSwitch(switches::kEnableMultiprocess)) |
| 300 runner_factory.reset(new OutOfProcessNativeRunnerFactory(this)); | 213 runner_factory.reset(new OutOfProcessNativeRunnerFactory(this)); |
| 301 else | 214 else |
| 302 runner_factory.reset(new InProcessNativeRunnerFactory(this)); | 215 runner_factory.reset(new InProcessNativeRunnerFactory(this)); |
| 303 application_manager_.set_blocking_pool(task_runners_->blocking_pool()); | 216 application_manager_->set_blocking_pool(task_runners_->blocking_pool()); |
| 304 application_manager_.set_native_runner_factory(runner_factory.Pass()); | 217 application_manager_->set_native_runner_factory(runner_factory.Pass()); |
| 305 application_manager_.set_disable_cache( | |
| 306 base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 307 switches::kDisableCache)); | |
| 308 | 218 |
| 309 InitContentHandlers(&application_manager_, command_line); | 219 InitContentHandlers(application_manager_.get(), command_line); |
| 310 InitNativeOptions(&application_manager_, command_line); | 220 InitNativeOptions(application_manager_.get(), command_line); |
| 311 | 221 |
| 312 ServiceProviderPtr service_provider_ptr; | 222 ServiceProviderPtr service_provider_ptr; |
| 313 ServiceProviderPtr tracing_service_provider_ptr; | 223 ServiceProviderPtr tracing_service_provider_ptr; |
| 314 new TracingServiceProvider(GetProxy(&tracing_service_provider_ptr)); | 224 new TracingServiceProvider(GetProxy(&tracing_service_provider_ptr)); |
| 315 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 225 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
| 316 request->url = mojo::String::From("mojo:tracing"); | 226 request->url = mojo::String::From("mojo:tracing"); |
| 317 application_manager_.ConnectToApplication( | 227 application_manager_->ConnectToApplication( |
| 318 nullptr, request.Pass(), std::string(), GetProxy(&service_provider_ptr), | 228 nullptr, request.Pass(), std::string(), GetProxy(&service_provider_ptr), |
| 319 tracing_service_provider_ptr.Pass(), | 229 tracing_service_provider_ptr.Pass(), |
| 320 shell::GetPermissiveCapabilityFilter(), base::Closure(), | 230 shell::GetPermissiveCapabilityFilter(), base::Closure(), |
| 321 shell::EmptyConnectCallback()); | 231 shell::EmptyConnectCallback()); |
| 322 | 232 |
| 323 // Record the shell startup metrics used for performance testing. | 233 // Record the shell startup metrics used for performance testing. |
| 324 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 234 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 325 tracing::kEnableStatsCollectionBindings)) { | 235 tracing::kEnableStatsCollectionBindings)) { |
| 326 tracing::StartupPerformanceDataCollectorPtr collector; | 236 tracing::StartupPerformanceDataCollectorPtr collector; |
| 327 service_provider_ptr->ConnectToService( | 237 service_provider_ptr->ConnectToService( |
| 328 tracing::StartupPerformanceDataCollector::Name_, | 238 tracing::StartupPerformanceDataCollector::Name_, |
| 329 GetProxy(&collector).PassMessagePipe()); | 239 GetProxy(&collector).PassMessagePipe()); |
| 330 #if defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_LINUX) | 240 #if defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_LINUX) |
| 331 // CurrentProcessInfo::CreationTime is only defined on some platforms. | 241 // CurrentProcessInfo::CreationTime is only defined on some platforms. |
| 332 const base::Time creation_time = base::CurrentProcessInfo::CreationTime(); | 242 const base::Time creation_time = base::CurrentProcessInfo::CreationTime(); |
| 333 collector->SetShellProcessCreationTime(creation_time.ToInternalValue()); | 243 collector->SetShellProcessCreationTime(creation_time.ToInternalValue()); |
| 334 #endif | 244 #endif |
| 335 collector->SetShellMainEntryPointTime(main_entry_time_.ToInternalValue()); | 245 collector->SetShellMainEntryPointTime(main_entry_time_.ToInternalValue()); |
| 336 } | 246 } |
| 337 | 247 |
| 338 InitDevToolsServiceIfNeeded(&application_manager_, command_line); | 248 InitDevToolsServiceIfNeeded(application_manager_.get(), command_line); |
| 339 | 249 |
| 340 return true; | 250 return true; |
| 341 } | 251 } |
| 342 | 252 |
| 343 void Context::Shutdown() { | 253 void Context::Shutdown() { |
| 254 // Actions triggered by ApplicationManager's destructor may require a current |
| 255 // message loop, so we should destruct it explicitly now as ~Context() occurs |
| 256 // post message loop shutdown. |
| 257 application_manager_.reset(); |
| 258 |
| 344 TRACE_EVENT0("mojo_shell", "Context::Shutdown"); | 259 TRACE_EVENT0("mojo_shell", "Context::Shutdown"); |
| 345 DCHECK_EQ(base::MessageLoop::current()->task_runner(), | 260 DCHECK_EQ(base::MessageLoop::current()->task_runner(), |
| 346 task_runners_->shell_runner()); | 261 task_runners_->shell_runner()); |
| 347 embedder::ShutdownIPCSupport(); | 262 embedder::ShutdownIPCSupport(); |
| 348 // We'll quit when we get OnShutdownComplete(). | 263 // We'll quit when we get OnShutdownComplete(). |
| 349 base::MessageLoop::current()->Run(); | 264 base::MessageLoop::current()->Run(); |
| 350 } | 265 } |
| 351 | 266 |
| 352 GURL Context::ResolveMappings(const GURL& url) { | |
| 353 return url_resolver_.ApplyMappings(url); | |
| 354 } | |
| 355 | |
| 356 GURL Context::ResolveMojoURL(const GURL& url) { | |
| 357 return url_resolver_.ResolveMojoURL(url); | |
| 358 } | |
| 359 | |
| 360 bool Context::CreateFetcher( | |
| 361 const GURL& url, | |
| 362 const shell::Fetcher::FetchCallback& loader_callback) { | |
| 363 if (url.SchemeIs(AboutFetcher::kAboutScheme)) { | |
| 364 AboutFetcher::Start(url, loader_callback); | |
| 365 return true; | |
| 366 } | |
| 367 | |
| 368 return false; | |
| 369 } | |
| 370 | |
| 371 void Context::OnShutdownComplete() { | 267 void Context::OnShutdownComplete() { |
| 372 DCHECK_EQ(base::MessageLoop::current()->task_runner(), | 268 DCHECK_EQ(base::MessageLoop::current()->task_runner(), |
| 373 task_runners_->shell_runner()); | 269 task_runners_->shell_runner()); |
| 374 base::MessageLoop::current()->Quit(); | 270 base::MessageLoop::current()->Quit(); |
| 375 } | 271 } |
| 376 | 272 |
| 377 void Context::Run(const GURL& url) { | 273 void Context::Run(const GURL& url) { |
| 378 DCHECK(app_complete_callback_.is_null()); | 274 DCHECK(app_complete_callback_.is_null()); |
| 379 ServiceProviderPtr services; | 275 ServiceProviderPtr services; |
| 380 ServiceProviderPtr exposed_services; | 276 ServiceProviderPtr exposed_services; |
| 381 | 277 |
| 382 app_urls_.insert(url); | 278 app_urls_.insert(url); |
| 383 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 279 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
| 384 request->url = mojo::String::From(url.spec()); | 280 request->url = mojo::String::From(url.spec()); |
| 385 application_manager_.ConnectToApplication( | 281 application_manager_->ConnectToApplication( |
| 386 nullptr, request.Pass(), std::string(), GetProxy(&services), | 282 nullptr, request.Pass(), std::string(), GetProxy(&services), |
| 387 exposed_services.Pass(), shell::GetPermissiveCapabilityFilter(), | 283 exposed_services.Pass(), shell::GetPermissiveCapabilityFilter(), |
| 388 base::Bind(&Context::OnApplicationEnd, base::Unretained(this), url), | 284 base::Bind(&Context::OnApplicationEnd, base::Unretained(this), url), |
| 389 shell::EmptyConnectCallback()); | 285 shell::EmptyConnectCallback()); |
| 390 } | 286 } |
| 391 | 287 |
| 392 void Context::RunCommandLineApplication(const base::Closure& callback) { | 288 void Context::RunCommandLineApplication(const base::Closure& callback) { |
| 393 DCHECK(app_urls_.empty()); | 289 DCHECK(app_urls_.empty()); |
| 394 DCHECK(app_complete_callback_.is_null()); | 290 DCHECK(app_complete_callback_.is_null()); |
| 395 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 291 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 414 base::MessageLoop::current()->Quit(); | 310 base::MessageLoop::current()->Quit(); |
| 415 } else { | 311 } else { |
| 416 app_complete_callback_.Run(); | 312 app_complete_callback_.Run(); |
| 417 } | 313 } |
| 418 } | 314 } |
| 419 } | 315 } |
| 420 } | 316 } |
| 421 | 317 |
| 422 } // namespace runner | 318 } // namespace runner |
| 423 } // namespace mojo | 319 } // namespace mojo |
| OLD | NEW |