OLD | NEW |
| (Empty) |
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/command_line_android.h" | |
8 #include "base/android/java_handler_thread.h" | |
9 #include "base/android/jni_string.h" | |
10 #include "base/at_exit.h" | |
11 #include "base/bind.h" | |
12 #include "base/command_line.h" | |
13 #include "base/lazy_instance.h" | |
14 #include "base/logging.h" | |
15 #include "base/macros.h" | |
16 #include "base/message_loop/message_loop.h" | |
17 #include "jni/MojoMain_jni.h" | |
18 #include "mojo/application_manager/application_loader.h" | |
19 #include "mojo/application_manager/application_manager.h" | |
20 #include "mojo/shell/context.h" | |
21 #include "mojo/shell/init.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 LazyInstance<scoped_ptr<base::android::JavaHandlerThread> > g_shell_thread = | |
37 LAZY_INSTANCE_INITIALIZER; | |
38 | |
39 void RunShell(std::vector<GURL> app_urls) { | |
40 shell::Context* context = g_context.Pointer()->get(); | |
41 context->Init(); | |
42 context->set_ui_loop(g_java_message_loop.Get().get()); | |
43 for (std::vector<GURL>::const_iterator it = app_urls.begin(); | |
44 it != app_urls.end(); ++it) { | |
45 context->Run(*it); | |
46 } | |
47 } | |
48 | |
49 } // namespace | |
50 | |
51 static void Init(JNIEnv* env, | |
52 jclass clazz, | |
53 jobject context, | |
54 jobjectArray jparameters) { | |
55 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context); | |
56 | |
57 base::android::InitApplicationContext(env, scoped_context); | |
58 | |
59 base::android::InitNativeCommandLineFromJavaArray(env, jparameters); | |
60 mojo::shell::InitializeLogging(); | |
61 | |
62 // We want ~MessageLoop to happen prior to ~Context. Initializing | |
63 // LazyInstances is akin to stack-allocating objects; their destructors | |
64 // will be invoked first-in-last-out. | |
65 shell::Context* shell_context = new shell::Context(); | |
66 g_context.Get().reset(shell_context); | |
67 g_java_message_loop.Get().reset(new base::MessageLoopForUI); | |
68 base::MessageLoopForUI::current()->Start(); | |
69 | |
70 // TODO(abarth): At which point should we switch to cross-platform | |
71 // initialization? | |
72 | |
73 gfx::GLSurface::InitializeOneOff(); | |
74 } | |
75 | |
76 static void Start(JNIEnv* env, jclass clazz, jstring jurl) { | |
77 std::vector<GURL> app_urls; | |
78 #if defined(MOJO_SHELL_DEBUG_URL) | |
79 app_urls.push_back(GURL(MOJO_SHELL_DEBUG_URL)); | |
80 // Sleep for 5 seconds to give the debugger a chance to attach. | |
81 sleep(5); | |
82 #else | |
83 if (jurl) | |
84 app_urls.push_back(GURL(base::android::ConvertJavaStringToUTF8(env, jurl))); | |
85 #endif | |
86 | |
87 g_shell_thread.Get().reset( | |
88 new base::android::JavaHandlerThread("shell_thread")); | |
89 g_shell_thread.Get()->Start(); | |
90 g_shell_thread.Get()->message_loop()->PostTask( | |
91 FROM_HERE, base::Bind(&RunShell, app_urls)); | |
92 } | |
93 | |
94 bool RegisterMojoMain(JNIEnv* env) { | |
95 return RegisterNativesImpl(env); | |
96 } | |
97 | |
98 } // namespace mojo | |
OLD | NEW |