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/fetcher/base_application_fetcher.h" | 32 #include "mojo/runner/about_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" |
39 #include "mojo/shell/connect_to_application_params.h" | 40 #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 |
62 void InitContentHandlers(shell::ApplicationManager* manager, | 113 void InitContentHandlers(shell::ApplicationManager* manager, |
63 const base::CommandLine& command_line) { | 114 const base::CommandLine& command_line) { |
64 // Default content handlers. | 115 // Default content handlers. |
65 manager->RegisterContentHandler("application/pdf", GURL("mojo:pdf_viewer")); | 116 manager->RegisterContentHandler("application/pdf", GURL("mojo:pdf_viewer")); |
66 manager->RegisterContentHandler("image/png", GURL("mojo:png_viewer")); | 117 manager->RegisterContentHandler("image/png", GURL("mojo:png_viewer")); |
67 manager->RegisterContentHandler("text/html", GURL("mojo:html_viewer")); | 118 manager->RegisterContentHandler("text/html", GURL("mojo:html_viewer")); |
68 | 119 |
69 // Command-line-specified content handlers. | 120 // Command-line-specified content handlers. |
70 std::string handlers_spec = | 121 std::string handlers_spec = |
71 command_line.GetSwitchValueASCII(switches::kContentHandlers); | 122 command_line.GetSwitchValueASCII(switches::kContentHandlers); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 } | 223 } |
173 | 224 |
174 private: | 225 private: |
175 StrongBinding<ServiceProvider> binding_; | 226 StrongBinding<ServiceProvider> binding_; |
176 | 227 |
177 DISALLOW_COPY_AND_ASSIGN(TracingServiceProvider); | 228 DISALLOW_COPY_AND_ASSIGN(TracingServiceProvider); |
178 }; | 229 }; |
179 | 230 |
180 } // namespace | 231 } // namespace |
181 | 232 |
182 Context::Context(const base::FilePath& shell_file_root) | 233 Context::Context() |
183 : application_manager_(new shell::ApplicationManager(make_scoped_ptr( | 234 : application_manager_(this), main_entry_time_(base::Time::Now()) { |
184 new fetcher::BaseApplicationFetcher(shell_file_root)))), | 235 DCHECK(!base::MessageLoop::current()); |
185 main_entry_time_(base::Time::Now()) {} | 236 |
| 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 } |
186 | 248 |
187 Context::~Context() { | 249 Context::~Context() { |
188 DCHECK(!base::MessageLoop::current()); | 250 DCHECK(!base::MessageLoop::current()); |
189 } | 251 } |
190 | 252 |
191 // static | 253 // static |
192 void Context::EnsureEmbedderIsInitialized() { | 254 void Context::EnsureEmbedderIsInitialized() { |
193 static base::LazyInstance<Setup>::Leaky setup = LAZY_INSTANCE_INITIALIZER; | 255 static base::LazyInstance<Setup>::Leaky setup = LAZY_INSTANCE_INITIALIZER; |
194 setup.Get(); | 256 setup.Get(); |
195 } | 257 } |
196 | 258 |
| 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 |
197 bool Context::Init() { | 277 bool Context::Init() { |
198 TRACE_EVENT0("mojo_shell", "Context::Init"); | 278 TRACE_EVENT0("mojo_shell", "Context::Init"); |
199 const base::CommandLine& command_line = | 279 const base::CommandLine& command_line = |
200 *base::CommandLine::ForCurrentProcess(); | 280 *base::CommandLine::ForCurrentProcess(); |
201 | 281 |
202 EnsureEmbedderIsInitialized(); | 282 EnsureEmbedderIsInitialized(); |
203 task_runners_.reset( | 283 task_runners_.reset( |
204 new TaskRunners(base::MessageLoop::current()->task_runner())); | 284 new TaskRunners(base::MessageLoop::current()->task_runner())); |
205 | 285 |
| 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 |
206 // TODO(vtl): This should be MASTER, not NONE. | 293 // TODO(vtl): This should be MASTER, not NONE. |
207 embedder::InitIPCSupport( | 294 embedder::InitIPCSupport( |
208 embedder::ProcessType::NONE, task_runners_->shell_runner(), this, | 295 embedder::ProcessType::NONE, task_runners_->shell_runner(), this, |
209 task_runners_->io_runner(), embedder::ScopedPlatformHandle()); | 296 task_runners_->io_runner(), embedder::ScopedPlatformHandle()); |
210 | 297 |
211 scoped_ptr<shell::NativeRunnerFactory> runner_factory; | 298 scoped_ptr<shell::NativeRunnerFactory> runner_factory; |
212 if (command_line.HasSwitch(switches::kEnableMultiprocess)) | 299 if (command_line.HasSwitch(switches::kEnableMultiprocess)) |
213 runner_factory.reset(new OutOfProcessNativeRunnerFactory(this)); | 300 runner_factory.reset(new OutOfProcessNativeRunnerFactory(this)); |
214 else | 301 else |
215 runner_factory.reset(new InProcessNativeRunnerFactory(this)); | 302 runner_factory.reset(new InProcessNativeRunnerFactory(this)); |
216 application_manager_->set_blocking_pool(task_runners_->blocking_pool()); | 303 application_manager_.set_blocking_pool(task_runners_->blocking_pool()); |
217 application_manager_->set_native_runner_factory(runner_factory.Pass()); | 304 application_manager_.set_native_runner_factory(runner_factory.Pass()); |
| 305 application_manager_.set_disable_cache( |
| 306 base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 307 switches::kDisableCache)); |
218 | 308 |
219 InitContentHandlers(application_manager_.get(), command_line); | 309 InitContentHandlers(&application_manager_, command_line); |
220 InitNativeOptions(application_manager_.get(), command_line); | 310 InitNativeOptions(&application_manager_, command_line); |
221 | 311 |
222 ServiceProviderPtr service_provider_ptr; | 312 ServiceProviderPtr service_provider_ptr; |
223 ServiceProviderPtr tracing_service_provider_ptr; | 313 ServiceProviderPtr tracing_service_provider_ptr; |
224 new TracingServiceProvider(GetProxy(&tracing_service_provider_ptr)); | 314 new TracingServiceProvider(GetProxy(&tracing_service_provider_ptr)); |
225 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 315 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
226 request->url = mojo::String::From("mojo:tracing"); | 316 request->url = mojo::String::From("mojo:tracing"); |
227 application_manager_->ConnectToApplication( | 317 application_manager_.ConnectToApplication( |
228 nullptr, request.Pass(), std::string(), GetProxy(&service_provider_ptr), | 318 nullptr, request.Pass(), std::string(), GetProxy(&service_provider_ptr), |
229 tracing_service_provider_ptr.Pass(), | 319 tracing_service_provider_ptr.Pass(), |
230 shell::GetPermissiveCapabilityFilter(), base::Closure(), | 320 shell::GetPermissiveCapabilityFilter(), base::Closure(), |
231 shell::EmptyConnectCallback()); | 321 shell::EmptyConnectCallback()); |
232 | 322 |
233 // Record the shell startup metrics used for performance testing. | 323 // Record the shell startup metrics used for performance testing. |
234 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 324 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
235 tracing::kEnableStatsCollectionBindings)) { | 325 tracing::kEnableStatsCollectionBindings)) { |
236 tracing::StartupPerformanceDataCollectorPtr collector; | 326 tracing::StartupPerformanceDataCollectorPtr collector; |
237 service_provider_ptr->ConnectToService( | 327 service_provider_ptr->ConnectToService( |
238 tracing::StartupPerformanceDataCollector::Name_, | 328 tracing::StartupPerformanceDataCollector::Name_, |
239 GetProxy(&collector).PassMessagePipe()); | 329 GetProxy(&collector).PassMessagePipe()); |
240 #if defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_LINUX) | 330 #if defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_LINUX) |
241 // CurrentProcessInfo::CreationTime is only defined on some platforms. | 331 // CurrentProcessInfo::CreationTime is only defined on some platforms. |
242 const base::Time creation_time = base::CurrentProcessInfo::CreationTime(); | 332 const base::Time creation_time = base::CurrentProcessInfo::CreationTime(); |
243 collector->SetShellProcessCreationTime(creation_time.ToInternalValue()); | 333 collector->SetShellProcessCreationTime(creation_time.ToInternalValue()); |
244 #endif | 334 #endif |
245 collector->SetShellMainEntryPointTime(main_entry_time_.ToInternalValue()); | 335 collector->SetShellMainEntryPointTime(main_entry_time_.ToInternalValue()); |
246 } | 336 } |
247 | 337 |
248 InitDevToolsServiceIfNeeded(application_manager_.get(), command_line); | 338 InitDevToolsServiceIfNeeded(&application_manager_, command_line); |
249 | 339 |
250 return true; | 340 return true; |
251 } | 341 } |
252 | 342 |
253 void Context::Shutdown() { | 343 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 | |
259 TRACE_EVENT0("mojo_shell", "Context::Shutdown"); | 344 TRACE_EVENT0("mojo_shell", "Context::Shutdown"); |
260 DCHECK_EQ(base::MessageLoop::current()->task_runner(), | 345 DCHECK_EQ(base::MessageLoop::current()->task_runner(), |
261 task_runners_->shell_runner()); | 346 task_runners_->shell_runner()); |
262 embedder::ShutdownIPCSupport(); | 347 embedder::ShutdownIPCSupport(); |
263 // We'll quit when we get OnShutdownComplete(). | 348 // We'll quit when we get OnShutdownComplete(). |
264 base::MessageLoop::current()->Run(); | 349 base::MessageLoop::current()->Run(); |
265 } | 350 } |
266 | 351 |
| 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 |
267 void Context::OnShutdownComplete() { | 371 void Context::OnShutdownComplete() { |
268 DCHECK_EQ(base::MessageLoop::current()->task_runner(), | 372 DCHECK_EQ(base::MessageLoop::current()->task_runner(), |
269 task_runners_->shell_runner()); | 373 task_runners_->shell_runner()); |
270 base::MessageLoop::current()->Quit(); | 374 base::MessageLoop::current()->Quit(); |
271 } | 375 } |
272 | 376 |
273 void Context::Run(const GURL& url) { | 377 void Context::Run(const GURL& url) { |
274 DCHECK(app_complete_callback_.is_null()); | 378 DCHECK(app_complete_callback_.is_null()); |
275 ServiceProviderPtr services; | 379 ServiceProviderPtr services; |
276 ServiceProviderPtr exposed_services; | 380 ServiceProviderPtr exposed_services; |
277 | 381 |
278 app_urls_.insert(url); | 382 app_urls_.insert(url); |
279 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 383 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
280 request->url = mojo::String::From(url.spec()); | 384 request->url = mojo::String::From(url.spec()); |
281 application_manager_->ConnectToApplication( | 385 application_manager_.ConnectToApplication( |
282 nullptr, request.Pass(), std::string(), GetProxy(&services), | 386 nullptr, request.Pass(), std::string(), GetProxy(&services), |
283 exposed_services.Pass(), shell::GetPermissiveCapabilityFilter(), | 387 exposed_services.Pass(), shell::GetPermissiveCapabilityFilter(), |
284 base::Bind(&Context::OnApplicationEnd, base::Unretained(this), url), | 388 base::Bind(&Context::OnApplicationEnd, base::Unretained(this), url), |
285 shell::EmptyConnectCallback()); | 389 shell::EmptyConnectCallback()); |
286 } | 390 } |
287 | 391 |
288 void Context::RunCommandLineApplication(const base::Closure& callback) { | 392 void Context::RunCommandLineApplication(const base::Closure& callback) { |
289 DCHECK(app_urls_.empty()); | 393 DCHECK(app_urls_.empty()); |
290 DCHECK(app_complete_callback_.is_null()); | 394 DCHECK(app_complete_callback_.is_null()); |
291 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 395 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
(...skipping 18 matching lines...) Expand all Loading... |
310 base::MessageLoop::current()->Quit(); | 414 base::MessageLoop::current()->Quit(); |
311 } else { | 415 } else { |
312 app_complete_callback_.Run(); | 416 app_complete_callback_.Run(); |
313 } | 417 } |
314 } | 418 } |
315 } | 419 } |
316 } | 420 } |
317 | 421 |
318 } // namespace runner | 422 } // namespace runner |
319 } // namespace mojo | 423 } // namespace mojo |
OLD | NEW |