OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "content/browser/mojo_shell_context_impl.h" | |
6 | |
7 #include "base/callback.h" | |
8 #include "base/message_loop/message_loop_proxy.h" | |
9 #include "base/run_loop.h" | |
10 #include "base/sequenced_task_runner.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "content/public/browser/browser_thread.h" | |
13 #include "content/public/browser/mojo_app_connection.h" | |
14 #include "content/public/browser/utility_process_host.h" | |
15 #include "content/public/browser/utility_process_host_client.h" | |
16 #include "content/public/common/process.mojom.h" | |
17 #include "content/public/common/service_registry.h" | |
18 #include "content/public/common/static_mojo_application_loader.h" | |
19 #include "mojo/shell/application_loader.h" | |
20 #include "net/interfaces/proxy_resolver_service.mojom.h" | |
21 #include "net/proxy/mojo_proxy_resolver_factory_impl.h" | |
22 #include "third_party/mojo/src/mojo/public/cpp/application/application_connectio n.h" | |
23 #include "third_party/mojo/src/mojo/public/cpp/application/application_delegate. h" | |
24 #include "third_party/mojo/src/mojo/public/cpp/application/application_impl.h" | |
25 | |
26 namespace content { | |
27 | |
28 namespace { | |
29 | |
30 // The loader for all out-of-process applications. This launches a utility | |
31 // process and forwards the Load request to a Process service there. This is the | |
32 // default loader used by content's Mojo shell. | |
33 class UtilityProcessLoader : public mojo::shell::ApplicationLoader { | |
34 public: | |
35 UtilityProcessLoader() {} | |
36 ~UtilityProcessLoader() override {} | |
37 | |
38 private: | |
39 // mojo::shell::ApplicationLoader | |
40 void Load( | |
41 const GURL& url, | |
42 mojo::InterfaceRequest<mojo::Application> application_request) override { | |
43 ProcessPtr process; | |
44 auto process_request = mojo::GetProxy(&process); | |
45 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO) | |
46 ->PostTask(FROM_HERE, | |
47 base::Bind(&UtilityProcessLoader::StartProcessOnIOThread, | |
48 base::Passed(&process_request))); | |
49 process->LoadApplication( | |
50 mojo::String::From(url.spec()), application_request.Pass(), | |
51 base::Bind(&UtilityProcessLoader::OnApplicationLoaded)); | |
52 } | |
53 | |
54 static void StartProcessOnIOThread( | |
55 mojo::InterfaceRequest<Process> process_request) { | |
56 UtilityProcessHost* process_host = | |
57 UtilityProcessHost::Create(nullptr, nullptr); | |
58 process_host->SetName(base::UTF8ToUTF16("Mojo Application")); | |
xhwang
2015/05/13 04:36:20
This will appear in task manager and is visible to
| |
59 if (process_host->StartMojoMode()) { | |
60 ServiceRegistry* services = process_host->GetServiceRegistry(); | |
61 services->ConnectToRemoteService(process_request.Pass()); | |
62 } else { | |
63 LOG(ERROR) << "Unable to start utility process."; | |
64 } | |
65 } | |
66 | |
67 static void OnApplicationLoaded(LaunchResult result) { | |
68 if (result != LAUNCH_RESULT_OK) { | |
69 LOG(ERROR) << "Failed to launch Mojo application."; | |
70 } | |
71 } | |
72 }; | |
73 | |
74 } // namespace | |
75 | |
76 MojoShellContextImpl::MojoShellContextImpl() | |
77 : application_manager_(new mojo::shell::ApplicationManager(this)) { | |
78 MojoAppConnection::SetApplicationManager(application_manager_.get()); | |
79 application_manager_->set_default_loader( | |
80 scoped_ptr<mojo::shell::ApplicationLoader>(new UtilityProcessLoader)); | |
81 } | |
82 | |
83 MojoShellContextImpl::~MojoShellContextImpl() { | |
84 MojoAppConnection::SetApplicationManager(nullptr); | |
85 } | |
86 | |
87 void MojoShellContextImpl::AddCustomLoader( | |
88 const GURL& url, | |
89 scoped_ptr<mojo::shell::ApplicationLoader> loader) { | |
90 application_manager_->SetLoaderForURL(loader.Pass(), url); | |
91 } | |
92 | |
93 // static | |
94 scoped_ptr<MojoShellContext> MojoShellContext::Create() { | |
95 return scoped_ptr<MojoShellContext>(new MojoShellContextImpl); | |
96 } | |
97 | |
98 // static | |
99 scoped_ptr<mojo::shell::ApplicationLoader> MojoShellContext::CreateStaticLoader( | |
100 const std::string& name, | |
101 const base::Callback<scoped_ptr<mojo::ApplicationDelegate>()>& factory) { | |
102 return scoped_ptr<mojo::shell::ApplicationLoader>( | |
103 new StaticMojoApplicationLoader(name, factory)); | |
104 } | |
105 | |
106 GURL MojoShellContextImpl::ResolveMappings(const GURL& url) { | |
107 return url; | |
108 } | |
109 | |
110 GURL MojoShellContextImpl::ResolveMojoURL(const GURL& url) { | |
111 return url; | |
112 } | |
113 | |
114 } // namespace content | |
OLD | NEW |