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 "base/at_exit.h"
6#include "base/bind.h"
7#include "base/command_line.h"
8#include "base/logging.h"
9#include "base/message_loop/message_loop.h"
10#include "mojo/shell/child_process.h"
11#include "mojo/shell/context.h"
12#include "mojo/shell/init.h"
13#include "mojo/shell/switches.h"
14#include "ui/gfx/switches.h"
15
16#if defined(COMPONENT_BUILD)
17#include "ui/gl/gl_surface.h"
18#endif
19
20namespace {
21
22void RunApps(mojo::shell::Context* context) {
23  const base::CommandLine& command_line =
24      *base::CommandLine::ForCurrentProcess();
25  base::CommandLine::StringVector args = command_line.GetArgs();
26  for (base::CommandLine::StringVector::const_iterator it = args.begin();
27       it != args.end();
28       ++it) {
29    context->Run(GURL(*it));
30  }
31}
32
33}  // namespace
34
35int main(int argc, char** argv) {
36  base::AtExitManager at_exit;
37  base::CommandLine::Init(argc, argv);
38#if defined(OS_LINUX)
39  // We use gfx::RenderText from multiple threads concurrently and the pango
40  // backend (currently the default on linux) is not close to threadsafe. Force
41  // use of the harfbuzz backend for now.
42  base::CommandLine::ForCurrentProcess()->AppendSwitch(
43      switches::kEnableHarfBuzzRenderText);
44#endif
45  mojo::shell::InitializeLogging();
46
47  // TODO(vtl): Unify parent and child process cases to the extent possible.
48  if (scoped_ptr<mojo::shell::ChildProcess> child_process =
49          mojo::shell::ChildProcess::Create(
50              *base::CommandLine::ForCurrentProcess())) {
51    child_process->Main();
52  } else {
53#if defined(COMPONENT_BUILD)
54    gfx::GLSurface::InitializeOneOff();
55#endif
56    // We want the shell::Context to outlive the MessageLoop so that pipes are
57    // all gracefully closed / error-out before we try to shut the Context down.
58    mojo::shell::Context shell_context;
59    {
60      base::MessageLoop message_loop;
61      shell_context.Init();
62
63      const base::CommandLine& command_line =
64          *base::CommandLine::ForCurrentProcess();
65      if (command_line.HasSwitch(switches::kOrigin)) {
66        shell_context.mojo_url_resolver()->SetBaseURL(
67            GURL(command_line.GetSwitchValueASCII(switches::kOrigin)));
68      }
69
70      message_loop.PostTask(FROM_HERE, base::Bind(RunApps, &shell_context));
71      message_loop.Run();
72    }
73  }
74  return 0;
75}
76