cc: Make picture pile base thread safe.
[chromium-blink-merge.git] / mojo / shell / context.cc
blob215ed7b2259d98389f2b6f79f3b620f131f3d602
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/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 "gpu/command_buffer/service/mailbox_manager_impl.h"
19 #include "mojo/application_manager/application_loader.h"
20 #include "mojo/application_manager/application_manager.h"
21 #include "mojo/application_manager/background_shell_application_loader.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"
35 #if defined(OS_ANDROID)
36 #include "mojo/services/native_viewport/gpu_impl.h"
37 #include "mojo/services/native_viewport/native_viewport_impl.h"
38 #include "mojo/shell/network_application_loader.h"
39 #include "ui/gl/gl_share_group.h"
40 #endif // defined(OS_ANDROID)
42 namespace mojo {
43 namespace shell {
44 namespace {
46 // These mojo: URLs are loaded directly from the local filesystem. They
47 // correspond to shared libraries bundled alongside the mojo_shell.
48 const char* kLocalMojoURLs[] = {
49 "mojo:network_service",
52 // Used to ensure we only init once.
53 class Setup {
54 public:
55 Setup() {
56 embedder::Init(scoped_ptr<mojo::embedder::PlatformSupport>(
57 new mojo::embedder::SimplePlatformSupport()));
60 ~Setup() {
63 private:
64 DISALLOW_COPY_AND_ASSIGN(Setup);
67 static base::LazyInstance<Setup>::Leaky setup = LAZY_INSTANCE_INITIALIZER;
69 void InitContentHandlers(DynamicApplicationLoader* loader,
70 base::CommandLine* command_line) {
71 // Default content handlers.
72 loader->RegisterContentHandler("image/png", GURL("mojo://png_viewer/"));
73 loader->RegisterContentHandler("text/html", GURL("mojo://html_viewer/"));
75 // Command-line-specified content handlers.
76 std::string handlers_spec = command_line->GetSwitchValueASCII(
77 switches::kContentHandlers);
78 if (handlers_spec.empty())
79 return;
81 std::vector<std::string> parts;
82 base::SplitString(handlers_spec, ',', &parts);
83 if (parts.size() % 2 != 0) {
84 LOG(ERROR) << "Invalid value for switch " << switches::kContentHandlers
85 << ": must be a comma-separated list of mimetype/url pairs.";
86 return;
89 for (size_t i = 0; i < parts.size(); i += 2) {
90 GURL url(parts[i + 1]);
91 if (!url.is_valid()) {
92 LOG(ERROR) << "Invalid value for switch " << switches::kContentHandlers
93 << ": '" << parts[i + 1] << "' is not a valid URL.";
94 return;
96 loader->RegisterContentHandler(parts[i], url);
100 class EmptyServiceProvider : public InterfaceImpl<ServiceProvider> {
101 private:
102 void ConnectToService(const mojo::String& service_name,
103 ScopedMessagePipeHandle client_handle) override {}
106 } // namespace
108 #if defined(OS_ANDROID)
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::MailboxManagerImpl) {}
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(app_.get(), false), &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);
159 #endif
161 Context::Context() {
162 DCHECK(!base::MessageLoop::current());
165 Context::~Context() {
166 DCHECK(!base::MessageLoop::current());
169 void Context::Init() {
170 application_manager_.set_delegate(this);
171 setup.Get();
172 task_runners_.reset(
173 new TaskRunners(base::MessageLoop::current()->message_loop_proxy()));
175 for (size_t i = 0; i < arraysize(kLocalMojoURLs); ++i)
176 mojo_url_resolver_.AddLocalFileMapping(GURL(kLocalMojoURLs[i]));
178 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
180 if (command_line->HasSwitch(switches::kEnableExternalApplications)) {
181 listener_ = ExternalApplicationListener::Create(
182 task_runners_->shell_runner(), task_runners_->io_runner());
184 base::FilePath socket_path =
185 command_line->GetSwitchValuePath(switches::kEnableExternalApplications);
186 if (socket_path.empty())
187 socket_path = ExternalApplicationListener::ConstructDefaultSocketPath();
189 listener_->ListenInBackground(
190 socket_path,
191 base::Bind(&ApplicationManager::RegisterExternalApplication,
192 base::Unretained(&application_manager_)));
194 scoped_ptr<DynamicServiceRunnerFactory> runner_factory;
195 if (command_line->HasSwitch(switches::kEnableMultiprocess))
196 runner_factory.reset(new OutOfProcessDynamicServiceRunnerFactory());
197 else
198 runner_factory.reset(new InProcessDynamicServiceRunnerFactory());
200 DynamicApplicationLoader* dynamic_application_loader =
201 new DynamicApplicationLoader(this, runner_factory.Pass());
202 InitContentHandlers(dynamic_application_loader, command_line);
203 application_manager_.set_default_loader(
204 scoped_ptr<ApplicationLoader>(dynamic_application_loader));
206 // The native viewport service synchronously waits for certain messages. If we
207 // don't run it on its own thread we can easily deadlock. Long term native
208 // viewport should run its own process so that this isn't an issue.
209 #if defined(OS_ANDROID)
210 application_manager_.SetLoaderForURL(
211 scoped_ptr<ApplicationLoader>(new UIApplicationLoader(
212 scoped_ptr<ApplicationLoader>(new NativeViewportApplicationLoader()),
213 this)),
214 GURL("mojo:native_viewport_service"));
215 #endif
217 if (command_line->HasSwitch(switches::kSpy)) {
218 spy_.reset(
219 new mojo::Spy(&application_manager_,
220 command_line->GetSwitchValueASCII(switches::kSpy)));
221 // TODO(cpu): the spy can snoop, but can't tell anybody until
222 // the Spy::WebSocketDelegate is implemented. In the original repo this
223 // was implemented by src\mojo\spy\websocket_server.h and .cc.
226 #if defined(OS_ANDROID)
227 // On android, the network service is bundled with the shell because the
228 // network stack depends on the android runtime.
230 scoped_ptr<BackgroundShellApplicationLoader> loader(
231 new BackgroundShellApplicationLoader(
232 scoped_ptr<ApplicationLoader>(new NetworkApplicationLoader()),
233 "network_service",
234 base::MessageLoop::TYPE_IO));
235 application_manager_.SetLoaderForURL(loader.Pass(),
236 GURL("mojo:network_service"));
238 #endif
240 if (listener_)
241 listener_->WaitForListening();
244 void Context::OnApplicationError(const GURL& gurl) {
245 if (app_urls_.find(gurl) != app_urls_.end()) {
246 app_urls_.erase(gurl);
247 if (app_urls_.empty() && base::MessageLoop::current()->is_running())
248 base::MessageLoop::current()->Quit();
252 void Context::Run(const GURL& url) {
253 EmptyServiceProvider* sp = new EmptyServiceProvider;
254 ServiceProviderPtr spp;
255 BindToProxy(sp, &spp);
257 app_urls_.insert(url);
258 application_manager_.ConnectToApplication(url, GURL(), spp.Pass());
261 ScopedMessagePipeHandle Context::ConnectToServiceByName(
262 const GURL& application_url,
263 const std::string& service_name) {
264 app_urls_.insert(application_url);
265 return application_manager_.ConnectToServiceByName(
266 application_url, service_name).Pass();
269 } // namespace shell
270 } // namespace mojo