11e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
21e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
31e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// found in the LICENSE file.
41e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "mojo/shell/context.h"
61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
75f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include <vector>
85f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/command_line.h"
105c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "base/lazy_instance.h"
1103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
125c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "base/memory/scoped_vector.h"
135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "base/strings/string_split.h"
146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "build/build_config.h"
1503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "gpu/command_buffer/service/mailbox_manager.h"
166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "mojo/application_manager/application_loader.h"
176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "mojo/application_manager/application_manager.h"
186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "mojo/application_manager/background_shell_application_loader.h"
1923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "mojo/embedder/embedder.h"
2003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "mojo/embedder/simple_platform_support.h"
216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "mojo/public/cpp/application/application_connection.h"
226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "mojo/public/cpp/application/application_delegate.h"
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "mojo/public/cpp/application/application_impl.h"
246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "mojo/shell/dynamic_application_loader.h"
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "mojo/shell/in_process_dynamic_service_runner.h"
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "mojo/shell/out_of_process_dynamic_service_runner.h"
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "mojo/shell/switches.h"
286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "mojo/shell/ui_application_loader_android.h"
29c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "mojo/spy/spy.h"
301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
315c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#if defined(OS_LINUX)
326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "mojo/shell/dbus_application_loader_linux.h"
335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#endif  // defined(OS_LINUX)
345c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#if defined(OS_ANDROID)
361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "mojo/services/native_viewport/gpu_impl.h"
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "mojo/services/native_viewport/native_viewport_impl.h"
386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "mojo/shell/network_application_loader.h"
391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "ui/gl/gl_share_group.h"
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif  // defined(OS_ANDROID)
41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace mojo {
431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace shell {
445c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liunamespace {
455c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
46f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// These mojo: URLs are loaded directly from the local filesystem. They
47f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// correspond to shared libraries bundled alongside the mojo_shell.
48f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)const char* kLocalMojoURLs[] = {
49f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  "mojo:mojo_network_service",
50f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)};
51f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
525c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// Used to ensure we only init once.
535c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass Setup {
545c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu public:
555c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  Setup() {
5603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    embedder::Init(scoped_ptr<mojo::embedder::PlatformSupport>(
5703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        new mojo::embedder::SimplePlatformSupport()));
585c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  }
595c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
605c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  ~Setup() {
615c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  }
625c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
635c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu private:
645c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  DISALLOW_COPY_AND_ASSIGN(Setup);
655c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu};
665c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
67f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)static base::LazyInstance<Setup>::Leaky setup = LAZY_INSTANCE_INITIALIZER;
685c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
696e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void InitContentHandlers(DynamicApplicationLoader* loader,
705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                         base::CommandLine* command_line) {
711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Default content handlers.
721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  loader->RegisterContentHandler("image/png", GURL("mojo://mojo_png_viewer/"));
731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  loader->RegisterContentHandler("text/html", GURL("mojo://mojo_html_viewer/"));
741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Command-line-specified content handlers.
765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  std::string handlers_spec = command_line->GetSwitchValueASCII(
775f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      switches::kContentHandlers);
785f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (handlers_spec.empty())
795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return;
805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  std::vector<std::string> parts;
825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  base::SplitString(handlers_spec, ',', &parts);
835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (parts.size() % 2 != 0) {
845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    LOG(ERROR) << "Invalid value for switch " << switches::kContentHandlers
855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)               << ": must be a comma-separated list of mimetype/url pairs.";
865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return;
875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  for (size_t i = 0; i < parts.size(); i += 2) {
905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    GURL url(parts[i + 1]);
915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    if (!url.is_valid()) {
925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      LOG(ERROR) << "Invalid value for switch " << switches::kContentHandlers
935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                 << ": '" << parts[i + 1] << "' is not a valid URL.";
945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      return;
955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    }
965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    loader->RegisterContentHandler(parts[i], url);
975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass EmptyServiceProvider : public InterfaceImpl<ServiceProvider> {
1011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci private:
1021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual void ConnectToService(const mojo::String& service_name,
1031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                ScopedMessagePipeHandle client_handle)
1041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      MOJO_OVERRIDE {
1051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
1061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci};
1071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1085c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}  // namespace
1091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(OS_ANDROID)
1116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class Context::NativeViewportApplicationLoader
1126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    : public ApplicationLoader,
1136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      public ApplicationDelegate,
11403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      public InterfaceFactory<NativeViewport>,
11503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      public InterfaceFactory<Gpu> {
116a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch public:
11703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  NativeViewportApplicationLoader()
11803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      : share_group_(new gfx::GLShareGroup),
11903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        mailbox_manager_(new gpu::gles2::MailboxManager) {}
1206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual ~NativeViewportApplicationLoader() {}
121a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
122a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch private:
1236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // ApplicationLoader implementation.
1246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual void Load(ApplicationManager* manager,
1255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                    const GURL& url,
1265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                    scoped_refptr<LoadCallbacks> callbacks) OVERRIDE {
1275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication();
1285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    if (shell_handle.is_valid())
1296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      app_.reset(new ApplicationImpl(this, shell_handle.Pass()));
1306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
1316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
1321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual void OnApplicationError(ApplicationManager* manager,
1331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                  const GURL& url) OVERRIDE {}
1346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
1356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // ApplicationDelegate implementation.
1366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual bool ConfigureIncomingConnection(
1376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      mojo::ApplicationConnection* connection) OVERRIDE {
13803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    connection->AddService<NativeViewport>(this);
13903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    connection->AddService<Gpu>(this);
1406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return true;
141a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  }
142a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
1436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // InterfaceFactory<NativeViewport> implementation.
1446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual void Create(ApplicationConnection* connection,
1456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                      InterfaceRequest<NativeViewport> request) OVERRIDE {
1461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    BindToRequest(new NativeViewportImpl(app_.get(), false), &request);
147a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  }
148a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
14903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // InterfaceFactory<Gpu> implementation.
15003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  virtual void Create(ApplicationConnection* connection,
15103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                      InterfaceRequest<Gpu> request) OVERRIDE {
15203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    BindToRequest(new GpuImpl(share_group_.get(), mailbox_manager_.get()),
15303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)                  &request);
15403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  }
15503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
15603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  scoped_refptr<gfx::GLShareGroup> share_group_;
15703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  scoped_refptr<gpu::gles2::MailboxManager> mailbox_manager_;
158116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  scoped_ptr<ApplicationImpl> app_;
1596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(NativeViewportApplicationLoader);
160a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch};
1611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
162a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
1635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)Context::Context() {
1645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(!base::MessageLoop::current());
1655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
1665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciContext::~Context() {
1681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(!base::MessageLoop::current());
1691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void Context::Init() {
1721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  application_manager_.set_delegate(this);
1735c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  setup.Get();
1745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  task_runners_.reset(
1755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      new TaskRunners(base::MessageLoop::current()->message_loop_proxy()));
176f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
177f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  for (size_t i = 0; i < arraysize(kLocalMojoURLs); ++i)
178f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    mojo_url_resolver_.AddLocalFileMapping(GURL(kLocalMojoURLs[i]));
179f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
1805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
181a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_ptr<DynamicServiceRunnerFactory> runner_factory;
1825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (command_line->HasSwitch(switches::kEnableMultiprocess))
183a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    runner_factory.reset(new OutOfProcessDynamicServiceRunnerFactory());
184a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  else
185a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    runner_factory.reset(new InProcessDynamicServiceRunnerFactory());
186c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
1876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  DynamicApplicationLoader* dynamic_application_loader =
1886e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      new DynamicApplicationLoader(this, runner_factory.Pass());
1896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  InitContentHandlers(dynamic_application_loader, command_line);
1906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  application_manager_.set_default_loader(
1916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      scoped_ptr<ApplicationLoader>(dynamic_application_loader));
1925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
193010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // The native viewport service synchronously waits for certain messages. If we
194010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // don't run it on its own thread we can easily deadlock. Long term native
195010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // viewport should run its own process so that this isn't an issue.
196116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#if defined(OS_ANDROID)
1976e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  application_manager_.SetLoaderForURL(
1986e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      scoped_ptr<ApplicationLoader>(new UIApplicationLoader(
1996e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          scoped_ptr<ApplicationLoader>(new NativeViewportApplicationLoader()),
2006e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          this)),
201116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      GURL("mojo:mojo_native_viewport_service"));
2025c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#endif
2035c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
2045c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#if defined(OS_LINUX)
2056e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  application_manager_.SetLoaderForScheme(
2066e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      scoped_ptr<ApplicationLoader>(new DBusApplicationLoader(this)), "dbus");
2075c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#endif  // defined(OS_LINUX)
208c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
2095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (command_line->HasSwitch(switches::kSpy)) {
2106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    spy_.reset(
2116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        new mojo::Spy(&application_manager_,
2126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                      command_line->GetSwitchValueASCII(switches::kSpy)));
213c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  }
214116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
215116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#if defined(OS_ANDROID)
216116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // On android, the network service is bundled with the shell because the
217116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // network stack depends on the android runtime.
2185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  {
2196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    scoped_ptr<BackgroundShellApplicationLoader> loader(
2206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        new BackgroundShellApplicationLoader(
2216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)            scoped_ptr<ApplicationLoader>(new NetworkApplicationLoader()),
2225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)            "network_service",
2236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)            base::MessageLoop::TYPE_IO));
2246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    application_manager_.SetLoaderForURL(loader.PassAs<ApplicationLoader>(),
2256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                         GURL("mojo:mojo_network_service"));
2265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
227116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif
2281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid Context::OnApplicationError(const GURL& gurl) {
2311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (app_urls_.find(gurl) != app_urls_.end()) {
2321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    app_urls_.erase(gurl);
2331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (app_urls_.empty() && base::MessageLoop::current()->is_running())
2341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      base::MessageLoop::current()->Quit();
2351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
2361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
2371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid Context::Run(const GURL& url) {
2391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  EmptyServiceProvider* sp = new EmptyServiceProvider;
2401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  ServiceProviderPtr spp;
2411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  BindToProxy(sp, &spp);
2421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  app_urls_.insert(url);
2441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  application_manager_.ConnectToApplication(url, GURL(), spp.Pass());
2451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
2461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciScopedMessagePipeHandle Context::ConnectToServiceByName(
2481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const GURL& application_url,
2491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const std::string& service_name) {
2501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  app_urls_.insert(application_url);
2511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return application_manager_.ConnectToServiceByName(
2521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      application_url, service_name).Pass();
2531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}  // namespace shell
2561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}  // namespace mojo
257