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.
5 #include "mojo/shell/android/mojo_main.h"
7 #include "base/android/jni_string.h"
8 #include "base/at_exit.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"
25 using base::LazyInstance;
31 LazyInstance<scoped_ptr<base::MessageLoop> > g_java_message_loop =
32 LAZY_INSTANCE_INITIALIZER;
34 LazyInstance<scoped_ptr<shell::Context> > g_context =
35 LAZY_INSTANCE_INITIALIZER;
38 LazyInstance<scoped_ptr<mojo::Environment> > g_env =
39 LAZY_INSTANCE_INITIALIZER;
43 static void Init(JNIEnv* env, jclass clazz, jobject context) {
44 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context);
46 base::android::InitApplicationContext(env, scoped_context);
48 CommandLine::Init(0, 0);
49 mojo::shell::InitializeLogging();
51 g_java_message_loop.Get().reset(new base::MessageLoopForUI);
52 base::MessageLoopForUI::current()->Start();
54 // TODO(abarth): At which point should we switch to cross-platform
57 gfx::GLSurface::InitializeOneOff();
60 static void Start(JNIEnv* env, jclass clazz, jobject context, jstring jurl) {
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.
68 app_url = base::android::ConvertJavaStringToUTF8(env, jurl);
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);
77 g_env.Get().reset(new Environment);
79 base::android::ScopedJavaGlobalRef<jobject> activity;
80 activity.Reset(env, context);
82 shell::Context* shell_context = new shell::Context();
83 shell_context->set_activity(activity.obj());
85 g_context.Get().reset(shell_context);
86 shell::Run(shell_context);
89 bool RegisterMojoMain(JNIEnv* env) {
90 return RegisterNativesImpl(env);