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/java_handler_thread.h"
8 #include "base/android/jni_string.h"
9 #include "base/at_exit.h"
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/lazy_instance.h"
13 #include "base/logging.h"
14 #include "base/macros.h"
15 #include "base/message_loop/message_loop.h"
16 #include "jni/MojoMain_jni.h"
17 #include "mojo/application_manager/application_loader.h"
18 #include "mojo/application_manager/application_manager.h"
19 #include "mojo/shell/context.h"
20 #include "mojo/shell/init.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
;
35 LazyInstance
<scoped_ptr
<base::android::JavaHandlerThread
> > g_shell_thread
=
36 LAZY_INSTANCE_INITIALIZER
;
38 void RunShell(std::vector
<GURL
> app_urls
) {
39 shell::Context
* context
= g_context
.Pointer()->get();
41 context
->set_ui_loop(g_java_message_loop
.Get().get());
42 for (std::vector
<GURL
>::const_iterator it
= app_urls
.begin();
43 it
!= app_urls
.end(); ++it
) {
50 static void Init(JNIEnv
* env
, jclass clazz
, jobject context
) {
51 base::android::ScopedJavaLocalRef
<jobject
> scoped_context(env
, context
);
53 base::android::InitApplicationContext(env
, scoped_context
);
55 base::CommandLine::Init(0, 0);
56 mojo::shell::InitializeLogging();
58 // We want ~MessageLoop to happen prior to ~Context. Initializing
59 // LazyInstances is akin to stack-allocating objects; their destructors
60 // will be invoked first-in-last-out.
61 shell::Context
* shell_context
= new shell::Context();
62 g_context
.Get().reset(shell_context
);
63 g_java_message_loop
.Get().reset(new base::MessageLoopForUI
);
64 base::MessageLoopForUI::current()->Start();
66 // TODO(abarth): At which point should we switch to cross-platform
69 gfx::GLSurface::InitializeOneOff();
72 static void Start(JNIEnv
* env
, jclass clazz
, jstring jurl
) {
73 std::vector
<GURL
> app_urls
;
74 #if defined(MOJO_SHELL_DEBUG_URL)
75 app_urls
.push_back(GURL(MOJO_SHELL_DEBUG_URL
));
76 // Sleep for 5 seconds to give the debugger a chance to attach.
80 app_urls
.push_back(GURL(base::android::ConvertJavaStringToUTF8(env
, jurl
)));
83 g_shell_thread
.Get().reset(
84 new base::android::JavaHandlerThread("shell_thread"));
85 g_shell_thread
.Get()->Start();
86 g_shell_thread
.Get()->message_loop()->PostTask(
87 FROM_HERE
, base::Bind(&RunShell
, app_urls
));
90 bool RegisterMojoMain(JNIEnv
* env
) {
91 return RegisterNativesImpl(env
);