Upstream version 7.36.149.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/jni_string.h"
8 #include "base/at_exit.h"
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "base/message_loop/message_loop.h"
15 #include "jni/MojoMain_jni.h"
16 #include "mojo/public/cpp/environment/environment.h"
17 #include "mojo/public/cpp/shell/application.h"
18 #include "mojo/service_manager/service_loader.h"
19 #include "mojo/service_manager/service_manager.h"
20 #include "mojo/shell/context.h"
21 #include "mojo/shell/init.h"
22 #include "mojo/shell/run.h"
23 #include "ui/gl/gl_surface_egl.h"
24
25 using base::LazyInstance;
26
27 namespace mojo {
28
29 namespace {
30
31 LazyInstance<scoped_ptr<base::MessageLoop> > g_java_message_loop =
32     LAZY_INSTANCE_INITIALIZER;
33
34 LazyInstance<scoped_ptr<shell::Context> > g_context =
35     LAZY_INSTANCE_INITIALIZER;
36
37
38 LazyInstance<scoped_ptr<mojo::Environment> > g_env =
39     LAZY_INSTANCE_INITIALIZER;
40
41 }  // namspace
42
43 static void Init(JNIEnv* env, jclass clazz, jobject context) {
44   base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context);
45
46   base::android::InitApplicationContext(env, scoped_context);
47
48   CommandLine::Init(0, 0);
49   mojo::shell::InitializeLogging();
50
51   g_java_message_loop.Get().reset(new base::MessageLoopForUI);
52   base::MessageLoopForUI::current()->Start();
53
54   // TODO(abarth): At which point should we switch to cross-platform
55   // initialization?
56
57   gfx::GLSurface::InitializeOneOff();
58 }
59
60 static void Start(JNIEnv* env, jclass clazz, jobject context, jstring jurl) {
61   std::string app_url;
62 #if defined(MOJO_SHELL_DEBUG_URL)
63   app_url = MOJO_SHELL_DEBUG_URL;
64   // Sleep for 5 seconds to give the debugger a chance to attach.
65   sleep(5);
66 #else
67   if (jurl)
68     app_url = base::android::ConvertJavaStringToUTF8(env, jurl);
69 #endif
70   if (!app_url.empty()) {
71     std::vector<std::string> argv;
72     argv.push_back("mojo_shell");
73     argv.push_back(app_url);
74     CommandLine::ForCurrentProcess()->InitFromArgv(argv);
75   }
76
77   g_env.Get().reset(new Environment);
78
79   base::android::ScopedJavaGlobalRef<jobject> activity;
80   activity.Reset(env, context);
81
82   shell::Context* shell_context = new shell::Context();
83   shell_context->set_activity(activity.obj());
84
85   g_context.Get().reset(shell_context);
86   shell::Run(shell_context);
87 }
88
89 bool RegisterMojoMain(JNIEnv* env) {
90   return RegisterNativesImpl(env);
91 }
92
93 }  // namespace mojo