Chromium Code Reviews| 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 "mojo/application_manager/application_manager.h" | 21 #include "mojo/application_manager/application_manager.h" |
| 22 #include "mojo/application_manager/background_shell_application_loader.h" | 22 #include "mojo/application_manager/background_shell_application_loader.h" |
| 23 #include "services/gles2/gpu_impl.h" | 23 #include "services/gles2/gpu_impl.h" |
| 24 #include "services/native_viewport/native_viewport_impl.h" | 24 #include "services/native_viewport/native_viewport_impl.h" |
| 25 #include "shell/android/android_handler_loader.h" | 25 #include "shell/android/android_handler_loader.h" |
| 26 #include "shell/android/ui_application_loader_android.h" | 26 #include "shell/android/ui_application_loader_android.h" |
| 27 #include "shell/command_line_util.h" | |
| 27 #include "shell/context.h" | 28 #include "shell/context.h" |
| 28 #include "shell/init.h" | 29 #include "shell/init.h" |
| 29 #include "ui/gl/gl_surface_egl.h" | 30 #include "ui/gl/gl_surface_egl.h" |
| 30 | 31 |
| 31 using base::LazyInstance; | 32 using base::LazyInstance; |
| 32 | 33 |
| 33 namespace mojo { | 34 namespace mojo { |
| 34 namespace shell { | 35 namespace shell { |
| 35 | 36 |
| 36 namespace { | 37 namespace { |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 // Android handler is bundled with the Mojo shell, because it uses the | 101 // Android handler is bundled with the Mojo shell, because it uses the |
| 101 // MojoShell application as the JNI bridge to bootstrap execution of other | 102 // MojoShell application as the JNI bridge to bootstrap execution of other |
| 102 // Android Mojo apps that need JNI. | 103 // Android Mojo apps that need JNI. |
| 103 context->application_manager()->SetLoaderForURL( | 104 context->application_manager()->SetLoaderForURL( |
| 104 make_scoped_ptr(new BackgroundShellApplicationLoader( | 105 make_scoped_ptr(new BackgroundShellApplicationLoader( |
| 105 make_scoped_ptr(new AndroidHandlerLoader()), "android_handler", | 106 make_scoped_ptr(new AndroidHandlerLoader()), "android_handler", |
| 106 base::MessageLoop::TYPE_DEFAULT)), | 107 base::MessageLoop::TYPE_DEFAULT)), |
| 107 GURL("mojo:android_handler")); | 108 GURL("mojo:android_handler")); |
| 108 } | 109 } |
| 109 | 110 |
| 110 void RunShell(std::vector<GURL> app_urls) { | 111 void StartShellOnShellThread() { |
| 111 Context* context = g_context.Pointer()->get(); | 112 Context* context = g_context.Pointer()->get(); |
| 112 ConfigureAndroidServices(context); | 113 ConfigureAndroidServices(context); |
| 113 context->Init(); | 114 context->Init(); |
| 114 for (std::vector<GURL>::const_iterator it = app_urls.begin(); | 115 RunCommandLineApps(context); |
| 115 it != app_urls.end(); ++it) { | |
| 116 context->Run(*it); | |
| 117 } | |
| 118 } | 116 } |
| 119 | 117 |
| 120 } // namespace | 118 } // namespace |
| 121 | 119 |
| 122 static void Init(JNIEnv* env, | 120 static void Init(JNIEnv* env, |
| 123 jclass clazz, | 121 jclass clazz, |
| 124 jobject context, | 122 jobject context, |
| 125 jstring mojo_shell_path, | 123 jstring mojo_shell_path, |
| 126 jobjectArray jparameters, | 124 jobjectArray jparameters, |
| 127 jstring j_local_apps_directory) { | 125 jstring j_local_apps_directory) { |
| 128 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context); | 126 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context); |
| 129 base::android::InitApplicationContext(env, scoped_context); | 127 base::android::InitApplicationContext(env, scoped_context); |
| 130 | 128 |
| 131 base::android::InitNativeCommandLineFromJavaArray(env, jparameters); | 129 std::vector<std::string> parameters; |
| 132 base::FilePath mojo_shell_file_path( | 130 parameters.push_back( |
| 133 base::android::ConvertJavaStringToUTF8(env, mojo_shell_path)); | 131 base::android::ConvertJavaStringToUTF8(env, mojo_shell_path)); |
| 134 base::CommandLine::ForCurrentProcess()->SetProgram(mojo_shell_file_path); | 132 base::android::AppendJavaStringArrayToStringVector(env, jparameters, |
| 133 ¶meters); | |
| 134 CommandLine::Init(0, nullptr); | |
| 135 base::CommandLine::ForCurrentProcess()->InitFromArgv(parameters); | |
| 136 | |
| 135 InitializeLogging(); | 137 InitializeLogging(); |
| 136 | 138 |
| 137 // We want ~MessageLoop to happen prior to ~Context. Initializing | 139 // We want ~MessageLoop to happen prior to ~Context. Initializing |
| 138 // LazyInstances is akin to stack-allocating objects; their destructors | 140 // LazyInstances is akin to stack-allocating objects; their destructors |
| 139 // will be invoked first-in-last-out. | 141 // will be invoked first-in-last-out. |
| 140 Context* shell_context = new Context(); | 142 Context* shell_context = new Context(); |
| 141 shell_context->mojo_url_resolver()->SetLocalAppsPath(base::FilePath( | 143 shell_context->mojo_url_resolver()->SetLocalAppsPath(base::FilePath( |
| 142 base::android::ConvertJavaStringToUTF8(env, j_local_apps_directory))); | 144 base::android::ConvertJavaStringToUTF8(env, j_local_apps_directory))); |
| 145 for (auto& args : parameters) { | |
| 146 ApplyApplicationArgs(shell_context, args); | |
| 147 } | |
| 148 | |
| 143 g_context.Get().reset(shell_context); | 149 g_context.Get().reset(shell_context); |
| 144 | 150 |
| 145 g_java_message_loop.Get().reset(new base::MessageLoopForUI); | 151 g_java_message_loop.Get().reset(new base::MessageLoopForUI); |
| 146 base::MessageLoopForUI::current()->Start(); | 152 base::MessageLoopForUI::current()->Start(); |
| 147 | 153 |
| 148 // TODO(abarth): At which point should we switch to cross-platform | 154 // TODO(abarth): At which point should we switch to cross-platform |
| 149 // initialization? | 155 // initialization? |
| 150 | 156 |
| 151 gfx::GLSurface::InitializeOneOff(); | 157 gfx::GLSurface::InitializeOneOff(); |
| 152 } | 158 } |
| 153 | 159 |
| 154 static void Start(JNIEnv* env, jclass clazz, jstring jurl) { | 160 static jboolean Start(JNIEnv* env, jclass clazz) { |
| 155 std::vector<GURL> app_urls; | 161 if (!base::CommandLine::ForCurrentProcess()->GetArgs().size()) { |
|
sky
2014/12/17 16:44:04
nit: no {}
qsr
2014/12/18 14:21:35
Done.
| |
| 162 return false; | |
| 163 } | |
| 164 | |
| 156 #if defined(MOJO_SHELL_DEBUG_URL) | 165 #if defined(MOJO_SHELL_DEBUG_URL) |
| 157 app_urls.push_back(GURL(MOJO_SHELL_DEBUG_URL)); | 166 base::CommandLine::ForCurrentProcess()->AppendArg(MOJO_SHELL_DEBUG_URL); |
| 158 // Sleep for 5 seconds to give the debugger a chance to attach. | 167 // Sleep for 5 seconds to give the debugger a chance to attach. |
| 159 sleep(5); | 168 sleep(5); |
| 160 #else | |
| 161 if (jurl) | |
| 162 app_urls.push_back(GURL(base::android::ConvertJavaStringToUTF8(env, jurl))); | |
| 163 #endif | 169 #endif |
| 164 | 170 |
| 165 g_shell_thread.Get().reset( | 171 g_shell_thread.Get().reset( |
| 166 new base::android::JavaHandlerThread("shell_thread")); | 172 new base::android::JavaHandlerThread("shell_thread")); |
| 167 g_shell_thread.Get()->Start(); | 173 g_shell_thread.Get()->Start(); |
| 168 g_shell_thread.Get()->message_loop()->PostTask( | 174 g_shell_thread.Get()->message_loop()->PostTask( |
| 169 FROM_HERE, base::Bind(&RunShell, app_urls)); | 175 FROM_HERE, base::Bind(&StartShellOnShellThread)); |
| 176 return true; | |
| 177 } | |
| 178 | |
| 179 static void AddApplicationURL(JNIEnv* env, jclass clazz, jstring jurl) { | |
| 180 base::CommandLine::ForCurrentProcess()->AppendArg( | |
| 181 base::android::ConvertJavaStringToUTF8(env, jurl)); | |
| 170 } | 182 } |
| 171 | 183 |
| 172 bool RegisterMojoMain(JNIEnv* env) { | 184 bool RegisterMojoMain(JNIEnv* env) { |
| 173 return RegisterNativesImpl(env); | 185 return RegisterNativesImpl(env); |
| 174 } | 186 } |
| 175 | 187 |
| 176 } // namespace shell | 188 } // namespace shell |
| 177 } // namespace mojo | 189 } // namespace mojo |
| OLD | NEW |