Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / mojo / shell / context.cc
blob706c511489a72a1f4564f3c3f22e7cc3536ee1c5
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.
5 #include "mojo/shell/context.h"
7 #include <vector>
9 #include "base/command_line.h"
10 #include "base/lazy_instance.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/strings/string_split.h"
14 #include "build/build_config.h"
15 #include "gpu/command_buffer/service/mailbox_manager.h"
16 #include "mojo/application_manager/application_loader.h"
17 #include "mojo/application_manager/application_manager.h"
18 #include "mojo/application_manager/background_shell_application_loader.h"
19 #include "mojo/embedder/embedder.h"
20 #include "mojo/embedder/simple_platform_support.h"
21 #include "mojo/public/cpp/application/application_connection.h"
22 #include "mojo/public/cpp/application/application_delegate.h"
23 #include "mojo/public/cpp/application/application_impl.h"
24 #include "mojo/services/native_viewport/gpu_impl.h"
25 #include "mojo/services/native_viewport/native_viewport_impl.h"
26 #include "mojo/shell/dynamic_application_loader.h"
27 #include "mojo/shell/in_process_dynamic_service_runner.h"
28 #include "mojo/shell/out_of_process_dynamic_service_runner.h"
29 #include "mojo/shell/switches.h"
30 #include "mojo/shell/ui_application_loader_android.h"
31 #include "mojo/spy/spy.h"
32 #include "ui/gl/gl_share_group.h"
34 #if defined(OS_LINUX)
35 #include "mojo/shell/dbus_application_loader_linux.h"
36 #endif // defined(OS_LINUX)
38 #if defined(OS_ANDROID)
39 #include "mojo/shell/network_application_loader.h"
40 #endif // defined(OS_ANDROID)
42 #if defined(USE_AURA)
43 #include "mojo/shell/view_manager_loader.h"
44 #endif
46 namespace mojo {
47 namespace shell {
48 namespace {
50 // These mojo: URLs are loaded directly from the local filesystem. They
51 // correspond to shared libraries bundled alongside the mojo_shell.
52 const char* kLocalMojoURLs[] = {
53 "mojo:mojo_network_service",
56 // Used to ensure we only init once.
57 class Setup {
58 public:
59 Setup() {
60 embedder::Init(scoped_ptr<mojo::embedder::PlatformSupport>(
61 new mojo::embedder::SimplePlatformSupport()));
64 ~Setup() {
67 private:
68 DISALLOW_COPY_AND_ASSIGN(Setup);
71 static base::LazyInstance<Setup>::Leaky setup = LAZY_INSTANCE_INITIALIZER;
73 void InitContentHandlers(DynamicApplicationLoader* loader,
74 base::CommandLine* command_line) {
75 std::string handlers_spec = command_line->GetSwitchValueASCII(
76 switches::kContentHandlers);
77 if (handlers_spec.empty())
78 return;
80 std::vector<std::string> parts;
81 base::SplitString(handlers_spec, ',', &parts);
82 if (parts.size() % 2 != 0) {
83 LOG(ERROR) << "Invalid value for switch " << switches::kContentHandlers
84 << ": must be a comma-separated list of mimetype/url pairs.";
85 return;
88 for (size_t i = 0; i < parts.size(); i += 2) {
89 GURL url(parts[i + 1]);
90 if (!url.is_valid()) {
91 LOG(ERROR) << "Invalid value for switch " << switches::kContentHandlers
92 << ": '" << parts[i + 1] << "' is not a valid URL.";
93 return;
95 loader->RegisterContentHandler(parts[i], url);
99 class EmptyServiceProvider : public InterfaceImpl<ServiceProvider> {
100 private:
101 virtual void ConnectToService(const mojo::String& service_name,
102 ScopedMessagePipeHandle client_handle)
103 MOJO_OVERRIDE {
107 } // namespace
109 class Context::NativeViewportApplicationLoader
110 : public ApplicationLoader,
111 public ApplicationDelegate,
112 public InterfaceFactory<NativeViewport>,
113 public InterfaceFactory<Gpu> {
114 public:
115 NativeViewportApplicationLoader()
116 : share_group_(new gfx::GLShareGroup),
117 mailbox_manager_(new gpu::gles2::MailboxManager) {}
118 virtual ~NativeViewportApplicationLoader() {}
120 private:
121 // ApplicationLoader implementation.
122 virtual void Load(ApplicationManager* manager,
123 const GURL& url,
124 scoped_refptr<LoadCallbacks> callbacks) OVERRIDE {
125 ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication();
126 if (shell_handle.is_valid())
127 app_.reset(new ApplicationImpl(this, shell_handle.Pass()));
130 virtual void OnApplicationError(ApplicationManager* manager,
131 const GURL& url) OVERRIDE {}
133 // ApplicationDelegate implementation.
134 virtual bool ConfigureIncomingConnection(
135 mojo::ApplicationConnection* connection) OVERRIDE {
136 connection->AddService<NativeViewport>(this);
137 connection->AddService<Gpu>(this);
138 return true;
141 // InterfaceFactory<NativeViewport> implementation.
142 virtual void Create(ApplicationConnection* connection,
143 InterfaceRequest<NativeViewport> request) OVERRIDE {
144 BindToRequest(new NativeViewportImpl, &request);
147 // InterfaceFactory<Gpu> implementation.
148 virtual void Create(ApplicationConnection* connection,
149 InterfaceRequest<Gpu> request) OVERRIDE {
150 BindToRequest(new GpuImpl(share_group_.get(), mailbox_manager_.get()),
151 &request);
154 scoped_refptr<gfx::GLShareGroup> share_group_;
155 scoped_refptr<gpu::gles2::MailboxManager> mailbox_manager_;
156 scoped_ptr<ApplicationImpl> app_;
157 DISALLOW_COPY_AND_ASSIGN(NativeViewportApplicationLoader);
160 Context::Context() {
161 DCHECK(!base::MessageLoop::current());
164 Context::~Context() {
165 DCHECK(!base::MessageLoop::current());
168 void Context::Init() {
169 application_manager_.set_delegate(this);
170 setup.Get();
171 task_runners_.reset(
172 new TaskRunners(base::MessageLoop::current()->message_loop_proxy()));
174 for (size_t i = 0; i < arraysize(kLocalMojoURLs); ++i)
175 mojo_url_resolver_.AddLocalFileMapping(GURL(kLocalMojoURLs[i]));
177 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
178 scoped_ptr<DynamicServiceRunnerFactory> runner_factory;
179 if (command_line->HasSwitch(switches::kEnableMultiprocess))
180 runner_factory.reset(new OutOfProcessDynamicServiceRunnerFactory());
181 else
182 runner_factory.reset(new InProcessDynamicServiceRunnerFactory());
184 DynamicApplicationLoader* dynamic_application_loader =
185 new DynamicApplicationLoader(this, runner_factory.Pass());
186 InitContentHandlers(dynamic_application_loader, command_line);
187 application_manager_.set_default_loader(
188 scoped_ptr<ApplicationLoader>(dynamic_application_loader));
190 // The native viewport service synchronously waits for certain messages. If we
191 // don't run it on its own thread we can easily deadlock. Long term native
192 // viewport should run its own process so that this isn't an issue.
193 #if defined(OS_ANDROID)
194 application_manager_.SetLoaderForURL(
195 scoped_ptr<ApplicationLoader>(new UIApplicationLoader(
196 scoped_ptr<ApplicationLoader>(new NativeViewportApplicationLoader()),
197 this)),
198 GURL("mojo:mojo_native_viewport_service"));
199 #else
201 scoped_ptr<BackgroundShellApplicationLoader> loader(
202 new BackgroundShellApplicationLoader(
203 scoped_ptr<ApplicationLoader>(
204 new NativeViewportApplicationLoader()),
205 "native_viewport",
206 base::MessageLoop::TYPE_UI));
207 application_manager_.SetLoaderForURL(
208 loader.PassAs<ApplicationLoader>(),
209 GURL("mojo:mojo_native_viewport_service"));
211 #endif
212 #if defined(USE_AURA)
213 // TODO(sky): need a better way to find this. It shouldn't be linked in.
214 application_manager_.SetLoaderForURL(
215 scoped_ptr<ApplicationLoader>(new ViewManagerLoader()),
216 GURL("mojo:mojo_view_manager"));
217 #endif
219 #if defined(OS_LINUX)
220 application_manager_.SetLoaderForScheme(
221 scoped_ptr<ApplicationLoader>(new DBusApplicationLoader(this)), "dbus");
222 #endif // defined(OS_LINUX)
224 if (command_line->HasSwitch(switches::kSpy)) {
225 spy_.reset(
226 new mojo::Spy(&application_manager_,
227 command_line->GetSwitchValueASCII(switches::kSpy)));
230 #if defined(OS_ANDROID)
231 // On android, the network service is bundled with the shell because the
232 // network stack depends on the android runtime.
234 scoped_ptr<BackgroundShellApplicationLoader> loader(
235 new BackgroundShellApplicationLoader(
236 scoped_ptr<ApplicationLoader>(new NetworkApplicationLoader()),
237 "network_service",
238 base::MessageLoop::TYPE_IO));
239 application_manager_.SetLoaderForURL(loader.PassAs<ApplicationLoader>(),
240 GURL("mojo:mojo_network_service"));
242 #endif
245 void Context::OnApplicationError(const GURL& gurl) {
246 if (app_urls_.find(gurl) != app_urls_.end()) {
247 app_urls_.erase(gurl);
248 if (app_urls_.empty() && base::MessageLoop::current()->is_running())
249 base::MessageLoop::current()->Quit();
253 void Context::Run(const GURL& url) {
254 EmptyServiceProvider* sp = new EmptyServiceProvider;
255 ServiceProviderPtr spp;
256 BindToProxy(sp, &spp);
258 app_urls_.insert(url);
259 application_manager_.ConnectToApplication(url, GURL(), spp.Pass());
262 ScopedMessagePipeHandle Context::ConnectToServiceByName(
263 const GURL& application_url,
264 const std::string& service_name) {
265 app_urls_.insert(application_url);
266 return application_manager_.ConnectToServiceByName(
267 application_url, service_name).Pass();
270 } // namespace shell
271 } // namespace mojo