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/service_manager/service_loader.h"
17 #include "mojo/service_manager/service_manager.h"
18 #include "mojo/shell/context.h"
19 #include "mojo/shell/init.h"
20 #include "mojo/shell/run.h"
21 #include "ui/gl/gl_surface_egl.h"
23 using base::LazyInstance
;
29 LazyInstance
<scoped_ptr
<base::MessageLoop
> > g_java_message_loop
=
30 LAZY_INSTANCE_INITIALIZER
;
32 LazyInstance
<scoped_ptr
<shell::Context
> > g_context
=
33 LAZY_INSTANCE_INITIALIZER
;
37 static void Init(JNIEnv
* env
, jclass clazz
, jobject context
) {
38 base::android::ScopedJavaLocalRef
<jobject
> scoped_context(env
, context
);
40 base::android::InitApplicationContext(env
, scoped_context
);
42 base::CommandLine::Init(0, 0);
43 mojo::shell::InitializeLogging();
45 g_java_message_loop
.Get().reset(new base::MessageLoopForUI
);
46 base::MessageLoopForUI::current()->Start();
48 // TODO(abarth): At which point should we switch to cross-platform
51 gfx::GLSurface::InitializeOneOff();
54 static void Start(JNIEnv
* env
, jclass clazz
, jobject context
, jstring jurl
) {
55 std::vector
<GURL
> app_urls
;
56 #if defined(MOJO_SHELL_DEBUG_URL)
57 app_urls
.push_back(GURL(MOJO_SHELL_DEBUG_URL
));
58 // Sleep for 5 seconds to give the debugger a chance to attach.
62 app_urls
.push_back(GURL(base::android::ConvertJavaStringToUTF8(env
, jurl
)));
65 base::android::ScopedJavaGlobalRef
<jobject
> activity
;
66 activity
.Reset(env
, context
);
68 shell::Context
* shell_context
= new shell::Context();
69 shell_context
->set_activity(activity
.obj());
71 g_context
.Get().reset(shell_context
);
72 shell::Run(shell_context
, app_urls
);
75 bool RegisterMojoMain(JNIEnv
* env
) {
76 return RegisterNativesImpl(env
);