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/application/application.h"
17 #include "mojo/service_manager/service_loader.h"
18 #include "mojo/service_manager/service_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 } // namespace
37
Init(JNIEnv * env,jclass clazz,jobject context)38 static void Init(JNIEnv* env, jclass clazz, jobject context) {
39 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context);
40
41 base::android::InitApplicationContext(env, scoped_context);
42
43 base::CommandLine::Init(0, 0);
44 mojo::shell::InitializeLogging();
45
46 g_java_message_loop.Get().reset(new base::MessageLoopForUI);
47 base::MessageLoopForUI::current()->Start();
48
49 // TODO(abarth): At which point should we switch to cross-platform
50 // initialization?
51
52 gfx::GLSurface::InitializeOneOff();
53 }
54
Start(JNIEnv * env,jclass clazz,jobject context,jstring jurl)55 static void Start(JNIEnv* env, jclass clazz, jobject context, jstring jurl) {
56 std::vector<GURL> app_urls;
57 #if defined(MOJO_SHELL_DEBUG_URL)
58 app_urls.push_back(GURL(MOJO_SHELL_DEBUG_URL));
59 // Sleep for 5 seconds to give the debugger a chance to attach.
60 sleep(5);
61 #else
62 if (jurl)
63 app_urls.push_back(GURL(base::android::ConvertJavaStringToUTF8(env, jurl)));
64 #endif
65
66 base::android::ScopedJavaGlobalRef<jobject> activity;
67 activity.Reset(env, context);
68
69 shell::Context* shell_context = new shell::Context();
70 shell_context->set_activity(activity.obj());
71
72 g_context.Get().reset(shell_context);
73 shell::Run(shell_context, app_urls);
74 }
75
RegisterMojoMain(JNIEnv * env)76 bool RegisterMojoMain(JNIEnv* env) {
77 return RegisterNativesImpl(env);
78 }
79
80 } // namespace mojo
81