| 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 "shell/android/mojo_main.h" | 5 #include "shell/android/mojo_main.h" |
| 6 | 6 |
| 7 #include "base/android/command_line_android.h" | |
| 8 #include "base/android/java_handler_thread.h" | 7 #include "base/android/java_handler_thread.h" |
| 9 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
| 9 #include "base/android/jni_array.h" |
| 10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| 11 #include "base/at_exit.h" | 11 #include "base/at_exit.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
| 19 #include "jni/MojoMain_jni.h" | 19 #include "jni/MojoMain_jni.h" |
| 20 #include "mojo/application_manager/application_loader.h" | 20 #include "mojo/application_manager/application_loader.h" |
| 21 #include "shell/android/android_handler_loader.h" | 21 #include "shell/android/android_handler_loader.h" |
| 22 #include "shell/android/background_application_loader.h" | 22 #include "shell/android/background_application_loader.h" |
| 23 #include "shell/android/native_viewport_application_loader.h" | 23 #include "shell/android/native_viewport_application_loader.h" |
| 24 #include "shell/android/ui_application_loader_android.h" | 24 #include "shell/android/ui_application_loader_android.h" |
| 25 #include "shell/command_line_util.h" |
| 25 #include "shell/context.h" | 26 #include "shell/context.h" |
| 26 #include "shell/init.h" | 27 #include "shell/init.h" |
| 27 #include "ui/gl/gl_surface_egl.h" | 28 #include "ui/gl/gl_surface_egl.h" |
| 28 | 29 |
| 29 using base::LazyInstance; | 30 using base::LazyInstance; |
| 30 | 31 |
| 31 namespace mojo { | 32 namespace mojo { |
| 32 namespace shell { | 33 namespace shell { |
| 33 | 34 |
| 34 namespace { | 35 namespace { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 51 // Android handler is bundled with the Mojo shell, because it uses the | 52 // Android handler is bundled with the Mojo shell, because it uses the |
| 52 // MojoShell application as the JNI bridge to bootstrap execution of other | 53 // MojoShell application as the JNI bridge to bootstrap execution of other |
| 53 // Android Mojo apps that need JNI. | 54 // Android Mojo apps that need JNI. |
| 54 context->application_manager()->SetLoaderForURL( | 55 context->application_manager()->SetLoaderForURL( |
| 55 make_scoped_ptr(new BackgroundApplicationLoader( | 56 make_scoped_ptr(new BackgroundApplicationLoader( |
| 56 make_scoped_ptr(new AndroidHandlerLoader()), "android_handler", | 57 make_scoped_ptr(new AndroidHandlerLoader()), "android_handler", |
| 57 base::MessageLoop::TYPE_DEFAULT)), | 58 base::MessageLoop::TYPE_DEFAULT)), |
| 58 GURL("mojo:android_handler")); | 59 GURL("mojo:android_handler")); |
| 59 } | 60 } |
| 60 | 61 |
| 61 void RunShell(std::vector<GURL> app_urls) { | 62 void StartShellOnShellThread() { |
| 62 Context* context = g_context.Pointer()->get(); | 63 Context* context = g_context.Pointer()->get(); |
| 63 ConfigureAndroidServices(context); | 64 ConfigureAndroidServices(context); |
| 64 context->Init(); | 65 context->Init(); |
| 65 for (std::vector<GURL>::const_iterator it = app_urls.begin(); | 66 RunCommandLineApps(context); |
| 66 it != app_urls.end(); ++it) { | |
| 67 context->Run(*it); | |
| 68 } | |
| 69 } | 67 } |
| 70 | 68 |
| 71 } // namespace | 69 } // namespace |
| 72 | 70 |
| 73 static void Init(JNIEnv* env, | 71 static void Init(JNIEnv* env, |
| 74 jclass clazz, | 72 jclass clazz, |
| 75 jobject context, | 73 jobject context, |
| 76 jstring mojo_shell_path, | 74 jstring mojo_shell_path, |
| 77 jobjectArray jparameters, | 75 jobjectArray jparameters, |
| 78 jstring j_local_apps_directory) { | 76 jstring j_local_apps_directory) { |
| 79 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context); | 77 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context); |
| 80 base::android::InitApplicationContext(env, scoped_context); | 78 base::android::InitApplicationContext(env, scoped_context); |
| 81 | 79 |
| 82 base::android::InitNativeCommandLineFromJavaArray(env, jparameters); | 80 std::vector<std::string> parameters; |
| 83 base::FilePath mojo_shell_file_path( | 81 parameters.push_back( |
| 84 base::android::ConvertJavaStringToUTF8(env, mojo_shell_path)); | 82 base::android::ConvertJavaStringToUTF8(env, mojo_shell_path)); |
| 85 base::CommandLine::ForCurrentProcess()->SetProgram(mojo_shell_file_path); | 83 base::android::AppendJavaStringArrayToStringVector(env, jparameters, |
| 84 ¶meters); |
| 85 CommandLine::Init(0, nullptr); |
| 86 base::CommandLine::ForCurrentProcess()->InitFromArgv(parameters); |
| 87 |
| 86 InitializeLogging(); | 88 InitializeLogging(); |
| 87 | 89 |
| 88 // We want ~MessageLoop to happen prior to ~Context. Initializing | 90 // We want ~MessageLoop to happen prior to ~Context. Initializing |
| 89 // LazyInstances is akin to stack-allocating objects; their destructors | 91 // LazyInstances is akin to stack-allocating objects; their destructors |
| 90 // will be invoked first-in-last-out. | 92 // will be invoked first-in-last-out. |
| 91 Context* shell_context = new Context(); | 93 Context* shell_context = new Context(); |
| 92 shell_context->mojo_url_resolver()->SetLocalAppsPath(base::FilePath( | 94 shell_context->mojo_url_resolver()->SetLocalAppsPath(base::FilePath( |
| 93 base::android::ConvertJavaStringToUTF8(env, j_local_apps_directory))); | 95 base::android::ConvertJavaStringToUTF8(env, j_local_apps_directory))); |
| 96 for (auto& args : parameters) |
| 97 ApplyApplicationArgs(shell_context, args); |
| 98 |
| 94 g_context.Get().reset(shell_context); | 99 g_context.Get().reset(shell_context); |
| 95 | 100 |
| 96 g_java_message_loop.Get().reset(new base::MessageLoopForUI); | 101 g_java_message_loop.Get().reset(new base::MessageLoopForUI); |
| 97 base::MessageLoopForUI::current()->Start(); | 102 base::MessageLoopForUI::current()->Start(); |
| 98 | 103 |
| 99 // TODO(abarth): At which point should we switch to cross-platform | 104 // TODO(abarth): At which point should we switch to cross-platform |
| 100 // initialization? | 105 // initialization? |
| 101 | 106 |
| 102 gfx::GLSurface::InitializeOneOff(); | 107 gfx::GLSurface::InitializeOneOff(); |
| 103 } | 108 } |
| 104 | 109 |
| 105 static void Start(JNIEnv* env, jclass clazz, jstring jurl) { | 110 static jboolean Start(JNIEnv* env, jclass clazz) { |
| 106 std::vector<GURL> app_urls; | 111 if (!base::CommandLine::ForCurrentProcess()->GetArgs().size()) |
| 112 return false; |
| 113 |
| 107 #if defined(MOJO_SHELL_DEBUG_URL) | 114 #if defined(MOJO_SHELL_DEBUG_URL) |
| 108 app_urls.push_back(GURL(MOJO_SHELL_DEBUG_URL)); | 115 base::CommandLine::ForCurrentProcess()->AppendArg(MOJO_SHELL_DEBUG_URL); |
| 109 // Sleep for 5 seconds to give the debugger a chance to attach. | 116 // Sleep for 5 seconds to give the debugger a chance to attach. |
| 110 sleep(5); | 117 sleep(5); |
| 111 #else | |
| 112 if (jurl) | |
| 113 app_urls.push_back(GURL(base::android::ConvertJavaStringToUTF8(env, jurl))); | |
| 114 #endif | 118 #endif |
| 115 | 119 |
| 116 g_shell_thread.Get().reset( | 120 g_shell_thread.Get().reset( |
| 117 new base::android::JavaHandlerThread("shell_thread")); | 121 new base::android::JavaHandlerThread("shell_thread")); |
| 118 g_shell_thread.Get()->Start(); | 122 g_shell_thread.Get()->Start(); |
| 119 g_shell_thread.Get()->message_loop()->PostTask( | 123 g_shell_thread.Get()->message_loop()->PostTask( |
| 120 FROM_HERE, base::Bind(&RunShell, app_urls)); | 124 FROM_HERE, base::Bind(&StartShellOnShellThread)); |
| 125 return true; |
| 126 } |
| 127 |
| 128 static void AddApplicationURL(JNIEnv* env, jclass clazz, jstring jurl) { |
| 129 base::CommandLine::ForCurrentProcess()->AppendArg( |
| 130 base::android::ConvertJavaStringToUTF8(env, jurl)); |
| 121 } | 131 } |
| 122 | 132 |
| 123 bool RegisterMojoMain(JNIEnv* env) { | 133 bool RegisterMojoMain(JNIEnv* env) { |
| 124 return RegisterNativesImpl(env); | 134 return RegisterNativesImpl(env); |
| 125 } | 135 } |
| 126 | 136 |
| 127 } // namespace shell | 137 } // namespace shell |
| 128 } // namespace mojo | 138 } // namespace mojo |
| OLD | NEW |