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/bind.h" | |
10 #include "base/command_line.h" | |
11 #include "base/files/file_path.h" | |
12 #include "base/lazy_instance.h" | |
13 #include "base/macros.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/memory/scoped_vector.h" | |
16 #include "base/strings/string_split.h" | |
17 #include "build/build_config.h" | |
18 #include "mojo/application_manager/application_loader.h" | |
19 #include "mojo/application_manager/application_manager.h" | |
20 #include "mojo/application_manager/background_shell_application_loader.h" | |
21 #include "mojo/common/tracing_impl.h" | |
22 #include "mojo/edk/embedder/embedder.h" | |
23 #include "mojo/edk/embedder/simple_platform_support.h" | |
24 #include "mojo/public/cpp/application/application_connection.h" | |
25 #include "mojo/public/cpp/application/application_delegate.h" | |
26 #include "mojo/public/cpp/application/application_impl.h" | |
27 #include "mojo/shell/dynamic_application_loader.h" | |
28 #include "mojo/shell/external_application_listener.h" | |
29 #include "mojo/shell/in_process_dynamic_service_runner.h" | |
30 #include "mojo/shell/out_of_process_dynamic_service_runner.h" | |
31 #include "mojo/shell/switches.h" | |
32 #include "mojo/shell/ui_application_loader_android.h" | |
33 #include "mojo/spy/spy.h" | |
34 #include "services/tracing/tracing.mojom.h" | |
35 #include "url/gurl.h" | |
36 | |
37 #if defined(OS_ANDROID) | |
38 #include "mojo/shell/android/android_handler_loader.h" | |
39 #include "mojo/shell/network_application_loader.h" | |
40 #include "services/gles2/gpu_impl.h" | |
41 #include "services/native_viewport/native_viewport_impl.h" | |
42 #endif // defined(OS_ANDROID) | |
43 | |
44 namespace mojo { | |
45 namespace shell { | |
46 namespace { | |
47 | |
48 // These mojo: URLs are loaded directly from the local filesystem. They | |
49 // correspond to shared libraries bundled alongside the mojo_shell. | |
50 const char* kLocalMojoURLs[] = { | |
51 "mojo:network_service", | |
52 }; | |
53 | |
54 // Used to ensure we only init once. | |
55 class Setup { | |
56 public: | |
57 Setup() { | |
58 embedder::Init(scoped_ptr<mojo::embedder::PlatformSupport>( | |
59 new mojo::embedder::SimplePlatformSupport())); | |
60 } | |
61 | |
62 ~Setup() { | |
63 } | |
64 | |
65 private: | |
66 DISALLOW_COPY_AND_ASSIGN(Setup); | |
67 }; | |
68 | |
69 static base::LazyInstance<Setup>::Leaky setup = LAZY_INSTANCE_INITIALIZER; | |
70 | |
71 void InitContentHandlers(DynamicApplicationLoader* loader, | |
72 base::CommandLine* command_line) { | |
73 // Default content handlers. | |
74 loader->RegisterContentHandler("application/pdf", GURL("mojo:pdf_viewer")); | |
75 loader->RegisterContentHandler("image/png", GURL("mojo:png_viewer")); | |
76 loader->RegisterContentHandler("text/html", GURL("mojo:html_viewer")); | |
77 | |
78 // Command-line-specified content handlers. | |
79 std::string handlers_spec = command_line->GetSwitchValueASCII( | |
80 switches::kContentHandlers); | |
81 if (handlers_spec.empty()) | |
82 return; | |
83 | |
84 std::vector<std::string> parts; | |
85 base::SplitString(handlers_spec, ',', &parts); | |
86 if (parts.size() % 2 != 0) { | |
87 LOG(ERROR) << "Invalid value for switch " << switches::kContentHandlers | |
88 << ": must be a comma-separated list of mimetype/url pairs."; | |
89 return; | |
90 } | |
91 | |
92 for (size_t i = 0; i < parts.size(); i += 2) { | |
93 GURL url(parts[i + 1]); | |
94 if (!url.is_valid()) { | |
95 LOG(ERROR) << "Invalid value for switch " << switches::kContentHandlers | |
96 << ": '" << parts[i + 1] << "' is not a valid URL."; | |
97 return; | |
98 } | |
99 loader->RegisterContentHandler(parts[i], url); | |
100 } | |
101 } | |
102 | |
103 class EmptyServiceProvider : public InterfaceImpl<ServiceProvider> { | |
104 private: | |
105 void ConnectToService(const mojo::String& service_name, | |
106 ScopedMessagePipeHandle client_handle) override {} | |
107 }; | |
108 | |
109 bool ConfigureURLMappings(const std::string& mappings, | |
110 mojo::shell::MojoURLResolver* resolver) { | |
111 base::StringPairs pairs; | |
112 if (!base::SplitStringIntoKeyValuePairs(mappings, '=', ',', &pairs)) | |
113 return false; | |
114 using StringPair = std::pair<std::string, std::string>; | |
115 for (const StringPair& pair : pairs) { | |
116 const GURL from(pair.first); | |
117 const GURL to(pair.second); | |
118 if (!from.is_valid() || !to.is_valid()) | |
119 return false; | |
120 resolver->AddCustomMapping(from, to); | |
121 } | |
122 return true; | |
123 } | |
124 | |
125 } // namespace | |
126 | |
127 #if defined(OS_ANDROID) | |
128 class Context::NativeViewportApplicationLoader | |
129 : public ApplicationLoader, | |
130 public ApplicationDelegate, | |
131 public InterfaceFactory<NativeViewport>, | |
132 public InterfaceFactory<Gpu> { | |
133 public: | |
134 NativeViewportApplicationLoader() : gpu_state_(new GpuImpl::State) {} | |
135 virtual ~NativeViewportApplicationLoader() {} | |
136 | |
137 private: | |
138 // ApplicationLoader implementation. | |
139 virtual void Load(ApplicationManager* manager, | |
140 const GURL& url, | |
141 ScopedMessagePipeHandle shell_handle, | |
142 LoadCallback callback) override { | |
143 DCHECK(shell_handle.is_valid()); | |
144 app_.reset(new ApplicationImpl(this, shell_handle.Pass())); | |
145 } | |
146 | |
147 virtual void OnApplicationError(ApplicationManager* manager, | |
148 const GURL& url) override {} | |
149 | |
150 // ApplicationDelegate implementation. | |
151 virtual bool ConfigureIncomingConnection( | |
152 mojo::ApplicationConnection* connection) override { | |
153 connection->AddService<NativeViewport>(this); | |
154 connection->AddService<Gpu>(this); | |
155 return true; | |
156 } | |
157 | |
158 // InterfaceFactory<NativeViewport> implementation. | |
159 virtual void Create(ApplicationConnection* connection, | |
160 InterfaceRequest<NativeViewport> request) override { | |
161 BindToRequest(new NativeViewportImpl(app_.get(), false), &request); | |
162 } | |
163 | |
164 // InterfaceFactory<Gpu> implementation. | |
165 virtual void Create(ApplicationConnection* connection, | |
166 InterfaceRequest<Gpu> request) override { | |
167 new GpuImpl(request.Pass(), gpu_state_); | |
168 } | |
169 | |
170 scoped_refptr<GpuImpl::State> gpu_state_; | |
171 scoped_ptr<ApplicationImpl> app_; | |
172 DISALLOW_COPY_AND_ASSIGN(NativeViewportApplicationLoader); | |
173 }; | |
174 #endif | |
175 | |
176 Context::Context() : application_manager_(this) { | |
177 DCHECK(!base::MessageLoop::current()); | |
178 } | |
179 | |
180 Context::~Context() { | |
181 DCHECK(!base::MessageLoop::current()); | |
182 } | |
183 | |
184 void Context::EnsureEmbedderIsInitialized() { | |
185 setup.Get(); | |
186 } | |
187 | |
188 bool Context::Init() { | |
189 EnsureEmbedderIsInitialized(); | |
190 task_runners_.reset( | |
191 new TaskRunners(base::MessageLoop::current()->message_loop_proxy())); | |
192 | |
193 for (size_t i = 0; i < arraysize(kLocalMojoURLs); ++i) | |
194 mojo_url_resolver_.AddLocalFileMapping(GURL(kLocalMojoURLs[i])); | |
195 | |
196 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
197 | |
198 if (command_line->HasSwitch(switches::kEnableExternalApplications)) { | |
199 listener_ = ExternalApplicationListener::Create( | |
200 task_runners_->shell_runner(), task_runners_->io_runner()); | |
201 | |
202 base::FilePath socket_path = | |
203 command_line->GetSwitchValuePath(switches::kEnableExternalApplications); | |
204 if (socket_path.empty()) | |
205 socket_path = ExternalApplicationListener::ConstructDefaultSocketPath(); | |
206 | |
207 listener_->ListenInBackground( | |
208 socket_path, | |
209 base::Bind(&ApplicationManager::RegisterExternalApplication, | |
210 base::Unretained(&application_manager_))); | |
211 } | |
212 if (command_line->HasSwitch(switches::kOrigin)) { | |
213 mojo_url_resolver()->SetBaseURL( | |
214 GURL(command_line->GetSwitchValueASCII(switches::kOrigin))); | |
215 } | |
216 if (command_line->HasSwitch(switches::kURLMappings) && | |
217 !ConfigureURLMappings( | |
218 command_line->GetSwitchValueASCII(switches::kURLMappings), | |
219 mojo_url_resolver())) { | |
220 return false; | |
221 } | |
222 | |
223 scoped_ptr<DynamicServiceRunnerFactory> runner_factory; | |
224 if (command_line->HasSwitch(switches::kEnableMultiprocess)) | |
225 runner_factory.reset(new OutOfProcessDynamicServiceRunnerFactory()); | |
226 else | |
227 runner_factory.reset(new InProcessDynamicServiceRunnerFactory()); | |
228 | |
229 DynamicApplicationLoader* dynamic_application_loader = | |
230 new DynamicApplicationLoader(this, runner_factory.Pass()); | |
231 InitContentHandlers(dynamic_application_loader, command_line); | |
232 application_manager_.set_default_loader( | |
233 scoped_ptr<ApplicationLoader>(dynamic_application_loader)); | |
234 | |
235 // The native viewport service synchronously waits for certain messages. If we | |
236 // don't run it on its own thread we can easily deadlock. Long term native | |
237 // viewport should run its own process so that this isn't an issue. | |
238 #if defined(OS_ANDROID) | |
239 application_manager_.SetLoaderForURL( | |
240 scoped_ptr<ApplicationLoader>(new UIApplicationLoader( | |
241 scoped_ptr<ApplicationLoader>(new NativeViewportApplicationLoader()), | |
242 this)), | |
243 GURL("mojo:native_viewport_service")); | |
244 #endif | |
245 | |
246 if (command_line->HasSwitch(switches::kSpy)) { | |
247 spy_.reset( | |
248 new mojo::Spy(&application_manager_, | |
249 command_line->GetSwitchValueASCII(switches::kSpy))); | |
250 // TODO(cpu): the spy can snoop, but can't tell anybody until | |
251 // the Spy::WebSocketDelegate is implemented. In the original repo this | |
252 // was implemented by src\mojo\spy\websocket_server.h and .cc. | |
253 } | |
254 | |
255 #if defined(OS_ANDROID) | |
256 // On android, the network service is bundled with the shell because the | |
257 // network stack depends on the android runtime. | |
258 { | |
259 scoped_ptr<BackgroundShellApplicationLoader> loader( | |
260 new BackgroundShellApplicationLoader( | |
261 scoped_ptr<ApplicationLoader>(new NetworkApplicationLoader()), | |
262 "network_service", | |
263 base::MessageLoop::TYPE_IO)); | |
264 application_manager_.SetLoaderForURL(loader.Pass(), | |
265 GURL("mojo:network_service")); | |
266 } | |
267 { | |
268 scoped_ptr<BackgroundShellApplicationLoader> loader( | |
269 new BackgroundShellApplicationLoader( | |
270 scoped_ptr<ApplicationLoader>(new AndroidHandlerLoader()), | |
271 "android_handler", base::MessageLoop::TYPE_DEFAULT)); | |
272 application_manager_.SetLoaderForURL(loader.Pass(), | |
273 GURL("mojo:android_handler")); | |
274 } | |
275 #endif | |
276 | |
277 tracing::TraceDataCollectorPtr trace_data_collector_ptr; | |
278 application_manager_.ConnectToService(GURL("mojo:tracing"), | |
279 &trace_data_collector_ptr); | |
280 TracingImpl::Create(trace_data_collector_ptr.Pass()); | |
281 | |
282 if (listener_) | |
283 listener_->WaitForListening(); | |
284 | |
285 return true; | |
286 } | |
287 | |
288 void Context::OnApplicationError(const GURL& url) { | |
289 if (app_urls_.find(url) != app_urls_.end()) { | |
290 app_urls_.erase(url); | |
291 if (app_urls_.empty() && base::MessageLoop::current()->is_running()) | |
292 base::MessageLoop::current()->Quit(); | |
293 } | |
294 } | |
295 | |
296 GURL Context::ResolveURL(const GURL& url) { | |
297 return mojo_url_resolver_.Resolve(url); | |
298 } | |
299 | |
300 void Context::Run(const GURL& url) { | |
301 EmptyServiceProvider* sp = new EmptyServiceProvider; | |
302 ServiceProviderPtr spp; | |
303 BindToProxy(sp, &spp); | |
304 | |
305 app_urls_.insert(url); | |
306 application_manager_.ConnectToApplication(url, GURL(), spp.Pass()); | |
307 } | |
308 | |
309 ScopedMessagePipeHandle Context::ConnectToServiceByName( | |
310 const GURL& application_url, | |
311 const std::string& service_name) { | |
312 app_urls_.insert(application_url); | |
313 return application_manager_.ConnectToServiceByName( | |
314 application_url, service_name).Pass(); | |
315 } | |
316 | |
317 } // namespace shell | |
318 } // namespace mojo | |
OLD | NEW |