Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / mojo / shell / android / 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 "mojo/shell/android/mojo_main.h"
6
7 #include "base/android/java_handler_thread.h"
8 #include "base/android/jni_string.h"
9 #include "base/at_exit.h"
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/lazy_instance.h"
13 #include "base/logging.h"
14 #include "base/macros.h"
15 #include "base/message_loop/message_loop.h"
16 #include "jni/MojoMain_jni.h"
17 #include "mojo/application_manager/application_loader.h"
18 #include "mojo/application_manager/application_manager.h"
19 #include "mojo/shell/context.h"
20 #include "mojo/shell/init.h"
21 #include "mojo/shell/run.h"
22 #include "ui/gl/gl_surface_egl.h"
23
24 using base::LazyInstance;
25
26 namespace mojo {
27
28 namespace {
29
30 LazyInstance<scoped_ptr<base::MessageLoop> > g_java_message_loop =
31     LAZY_INSTANCE_INITIALIZER;
32
33 LazyInstance<scoped_ptr<shell::Context> > g_context =
34     LAZY_INSTANCE_INITIALIZER;
35
36 LazyInstance<scoped_ptr<base::android::JavaHandlerThread> > g_shell_thread =
37     LAZY_INSTANCE_INITIALIZER;
38
39 void RunShell(std::vector<GURL> app_urls) {
40   g_context.Get()->Init();
41   g_context.Get()->set_ui_loop(g_java_message_loop.Get().get());
42   shell::Run(g_context.Get().get(), app_urls);
43 }
44
45 }  // namespace
46
47 static void Init(JNIEnv* env, jclass clazz, jobject context) {
48   base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context);
49
50   base::android::InitApplicationContext(env, scoped_context);
51
52   base::CommandLine::Init(0, 0);
53   mojo::shell::InitializeLogging();
54
55   // We want ~MessageLoop to happen prior to ~Context. Initializing
56   // LazyInstances is akin to stack-allocating objects; their destructors
57   // will be invoked first-in-last-out.
58   shell::Context* shell_context = new shell::Context();
59   g_context.Get().reset(shell_context);
60   g_java_message_loop.Get().reset(new base::MessageLoopForUI);
61   base::MessageLoopForUI::current()->Start();
62
63   // TODO(abarth): At which point should we switch to cross-platform
64   // initialization?
65
66   gfx::GLSurface::InitializeOneOff();
67 }
68
69 static void Start(JNIEnv* env, jclass clazz, jstring jurl) {
70   std::vector<GURL> app_urls;
71 #if defined(MOJO_SHELL_DEBUG_URL)
72   app_urls.push_back(GURL(MOJO_SHELL_DEBUG_URL));
73   // Sleep for 5 seconds to give the debugger a chance to attach.
74   sleep(5);
75 #else
76   if (jurl)
77     app_urls.push_back(GURL(base::android::ConvertJavaStringToUTF8(env, jurl)));
78 #endif
79
80   g_shell_thread.Get().reset(
81       new base::android::JavaHandlerThread("shell_thread"));
82   g_shell_thread.Get()->Start();
83   g_shell_thread.Get()->message_loop()->PostTask(
84       FROM_HERE, base::Bind(&RunShell, app_urls));
85 }
86
87 bool RegisterMojoMain(JNIEnv* env) {
88   return RegisterNativesImpl(env);
89 }
90
91 }  // namespace mojo