| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/shell/android/mojo_main.h" | 5 #include "mojo/shell/android/mojo_main.h" |
| 6 | 6 |
| 7 #include "base/android/jni_string.h" | 7 #include "base/android/jni_string.h" |
| 8 #include "base/at_exit.h" | 8 #include "base/at_exit.h" |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/threading/thread.h" | |
| 14 #include "jni/MojoMain_jni.h" | 13 #include "jni/MojoMain_jni.h" |
| 15 #include "mojo/shell/init.h" | 14 #include "mojo/shell/init.h" |
| 16 #include "mojo/shell/run.h" | 15 #include "mojo/shell/run.h" |
| 17 #include "ui/gl/gl_surface_egl.h" | 16 #include "ui/gl/gl_surface_egl.h" |
| 18 | 17 |
| 19 using base::LazyInstance; | 18 using base::LazyInstance; |
| 20 | 19 |
| 21 namespace mojo { | 20 namespace mojo { |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| 25 base::AtExitManager* g_at_exit = 0; | 24 base::AtExitManager* g_at_exit = 0; |
| 26 | 25 |
| 27 LazyInstance<scoped_ptr<base::MessageLoop> > g_java_message_loop = | 26 LazyInstance<scoped_ptr<base::MessageLoop> > g_java_message_loop = |
| 28 LAZY_INSTANCE_INITIALIZER; | 27 LAZY_INSTANCE_INITIALIZER; |
| 29 | 28 |
| 30 LazyInstance<scoped_ptr<base::Thread> > g_shell_thread = | |
| 31 LAZY_INSTANCE_INITIALIZER; | |
| 32 | |
| 33 LazyInstance<scoped_ptr<shell::Context> > g_context = | 29 LazyInstance<scoped_ptr<shell::Context> > g_context = |
| 34 LAZY_INSTANCE_INITIALIZER; | 30 LAZY_INSTANCE_INITIALIZER; |
| 35 | 31 |
| 36 struct ShellInit { | |
| 37 scoped_refptr<base::SingleThreadTaskRunner> java_runner; | |
| 38 base::android::ScopedJavaGlobalRef<jobject> activity; | |
| 39 }; | |
| 40 | |
| 41 void StartOnShellThread(ShellInit* init) { | |
| 42 shell::Context* context = new shell::Context(); | |
| 43 | |
| 44 context->set_activity(init->activity.obj()); | |
| 45 context->task_runners()->set_java_runner(init->java_runner.get()); | |
| 46 delete init; | |
| 47 | |
| 48 g_context.Get().reset(context); | |
| 49 shell::Run(context); | |
| 50 } | |
| 51 | |
| 52 } // namspace | 32 } // namspace |
| 53 | 33 |
| 54 static void Init(JNIEnv* env, jclass clazz, jobject context) { | 34 static void Init(JNIEnv* env, jclass clazz, jobject context) { |
| 55 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context); | 35 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context); |
| 56 | 36 |
| 57 base::android::InitApplicationContext(env, scoped_context); | 37 base::android::InitApplicationContext(env, scoped_context); |
| 58 | 38 |
| 59 if (g_at_exit) | 39 if (g_at_exit) |
| 60 return; | 40 return; |
| 61 g_at_exit = new base::AtExitManager(); | 41 g_at_exit = new base::AtExitManager(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 76 | 56 |
| 77 static void Start(JNIEnv* env, jclass clazz, jobject context, jstring jurl) { | 57 static void Start(JNIEnv* env, jclass clazz, jobject context, jstring jurl) { |
| 78 if (jurl) { | 58 if (jurl) { |
| 79 std::string app_url = base::android::ConvertJavaStringToUTF8(env, jurl); | 59 std::string app_url = base::android::ConvertJavaStringToUTF8(env, jurl); |
| 80 std::vector<std::string> argv; | 60 std::vector<std::string> argv; |
| 81 argv.push_back("mojo_shell"); | 61 argv.push_back("mojo_shell"); |
| 82 argv.push_back("--app=" + app_url); | 62 argv.push_back("--app=" + app_url); |
| 83 CommandLine::ForCurrentProcess()->InitFromArgv(argv); | 63 CommandLine::ForCurrentProcess()->InitFromArgv(argv); |
| 84 } | 64 } |
| 85 | 65 |
| 86 ShellInit* init = new ShellInit(); | 66 base::android::ScopedJavaGlobalRef<jobject> activity; |
| 87 init->java_runner = base::MessageLoopForUI::current()->message_loop_proxy(); | 67 activity.Reset(env, context); |
| 88 init->activity.Reset(env, context); | |
| 89 | 68 |
| 90 g_shell_thread.Get().reset(new base::Thread("shell_thread")); | 69 shell::Context* shell_context = new shell::Context(); |
| 91 g_shell_thread.Get()->Start(); | 70 shell_context->set_activity(activity.obj()); |
| 92 g_shell_thread.Get()->message_loop()->PostTask(FROM_HERE, | 71 g_context.Get().reset(shell_context); |
| 93 base::Bind(StartOnShellThread, init)); | 72 shell::Run(shell_context); |
| 94 | |
| 95 // TODO(abarth): Currently we leak g_shell_thread. | |
| 96 } | 73 } |
| 97 | 74 |
| 98 bool RegisterMojoMain(JNIEnv* env) { | 75 bool RegisterMojoMain(JNIEnv* env) { |
| 99 return RegisterNativesImpl(env); | 76 return RegisterNativesImpl(env); |
| 100 } | 77 } |
| 101 | 78 |
| 102 } // namespace mojo | 79 } // namespace mojo |
| OLD | NEW |