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