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/message_loop/message_loop.h"
14 #include "jni/MojoMain_jni.h"
15 #include "mojo/shell/context.h"
16 #include "mojo/shell/init.h"
17 #include "mojo/shell/run.h"
18 #include "ui/gl/gl_surface_egl.h"
19
20 using base::LazyInstance;
21
22 namespace mojo {
23
24 namespace {
25
26 base::AtExitManager* g_at_exit = 0;
27
28 LazyInstance<scoped_ptr<base::MessageLoop> > g_java_message_loop =
29 LAZY_INSTANCE_INITIALIZER;
30
31 LazyInstance<scoped_ptr<shell::Context> > g_context =
32 LAZY_INSTANCE_INITIALIZER;
33
34 } // namspace
35
Init(JNIEnv * env,jclass clazz,jobject context)36 static void Init(JNIEnv* env, jclass clazz, jobject context) {
37 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context);
38
39 base::android::InitApplicationContext(env, scoped_context);
40
41 if (g_at_exit)
42 return;
43 g_at_exit = new base::AtExitManager();
44 // TODO(abarth): Currently we leak g_at_exit.
45
46 CommandLine::Init(0, 0);
47 mojo::shell::InitializeLogging();
48
49 g_java_message_loop.Get().reset(
50 new base::MessageLoop(base::MessageLoop::TYPE_UI));
51 base::MessageLoopForUI::current()->Start();
52
53 // TODO(abarth): At which point should we switch to cross-platform
54 // initialization?
55
56 gfx::GLSurface::InitializeOneOff();
57 }
58
Start(JNIEnv * env,jclass clazz,jobject context,jstring jurl)59 static void Start(JNIEnv* env, jclass clazz, jobject context, jstring jurl) {
60 if (jurl) {
61 std::string app_url = base::android::ConvertJavaStringToUTF8(env, jurl);
62 std::vector<std::string> argv;
63 argv.push_back("mojo_shell");
64 argv.push_back("--app=" + app_url);
65 CommandLine::ForCurrentProcess()->InitFromArgv(argv);
66 }
67
68 base::android::ScopedJavaGlobalRef<jobject> activity;
69 activity.Reset(env, context);
70
71 shell::Context* shell_context = new shell::Context();
72 shell_context->set_activity(activity.obj());
73 g_context.Get().reset(shell_context);
74 shell::Run(shell_context);
75 }
76
RegisterMojoMain(JNIEnv * env)77 bool RegisterMojoMain(JNIEnv* env) {
78 return RegisterNativesImpl(env);
79 }
80
81 } // namespace mojo
82