9591ae8f663a9b0b24a7522a57202a55326388ee
[platform/framework/web/crosswalk.git] / src / mojo / shell / desktop / mojo_main.cc
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/command_line.h"
7 #include "base/logging.h"
8 #include "base/macros.h"  // TODO(vtl): Remove.
9 #include "base/message_loop/message_loop.h"
10 #include "mojo/common/message_pump_mojo.h"  // TODO(vtl): Remove.
11 #include "mojo/shell/child_process.h"
12 #include "mojo/shell/child_process_host.h"  // TODO(vtl): Remove.
13 #include "mojo/shell/context.h"
14 #include "mojo/shell/init.h"
15 #include "mojo/shell/run.h"
16 #include "ui/gl/gl_surface.h"
17
18 namespace {
19
20 // TODO(vtl): Remove.
21 class TestChildProcessHostDelegate
22     : public mojo::shell::ChildProcessHost::Delegate {
23  public:
24   TestChildProcessHostDelegate() {}
25   virtual ~TestChildProcessHostDelegate() {}
26   virtual void WillStart() OVERRIDE {
27     VLOG(2) << "TestChildProcessHostDelegate::WillStart()";
28   }
29   virtual void DidStart(bool success) OVERRIDE {
30     VLOG(2) << "TestChildProcessHostDelegate::DidStart(" << success << ")";
31     base::MessageLoop::current()->QuitWhenIdle();
32   }
33 };
34
35 }  // namespace
36
37 int main(int argc, char** argv) {
38   base::AtExitManager at_exit;
39   CommandLine::Init(argc, argv);
40   mojo::shell::InitializeLogging();
41
42   // TODO(vtl): Move this a proper test (and remove includes marked "remove").
43   if (CommandLine::ForCurrentProcess()->HasSwitch("run-test-child")) {
44     base::MessageLoop message_loop(
45         scoped_ptr<base::MessagePump>(new mojo::common::MessagePumpMojo()));
46
47     mojo::shell::Context context;
48     TestChildProcessHostDelegate child_process_host_delegate;
49     mojo::shell::ChildProcessHost child_process_host(
50         &context, &child_process_host_delegate,
51         mojo::shell::ChildProcess::TYPE_TEST);
52     child_process_host.Start();
53     message_loop.Run();
54     int exit_code = child_process_host.Join();
55     VLOG(2) << "Joined child: exit_code = " << exit_code;
56     return 0;
57   }
58
59   // TODO(vtl): Unify parent and child process cases to the extent possible.
60   if (scoped_ptr<mojo::shell::ChildProcess> child_process =
61           mojo::shell::ChildProcess::Create(
62               *CommandLine::ForCurrentProcess())) {
63     child_process->Main();
64   } else {
65     gfx::GLSurface::InitializeOneOff();
66
67     base::MessageLoop message_loop;
68     mojo::shell::Context context;
69     message_loop.PostTask(FROM_HERE, base::Bind(mojo::shell::Run, &context));
70
71     message_loop.Run();
72   }
73
74   return 0;
75 }