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/main.h" | 5 #include "shell/android/main.h" |
| 6 | 6 |
| 7 #include "base/android/fifo_utils.h" | 7 #include "base/android/fifo_utils.h" |
| 8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
| 9 #include "base/android/jni_array.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/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 16 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/macros.h" | 18 #include "base/macros.h" |
| 19 #include "base/message_loop/message_loop.h" | 19 #include "base/message_loop/message_loop.h" |
| 20 #include "base/run_loop.h" | 20 #include "base/run_loop.h" |
| 21 #include "base/threading/simple_thread.h" | 21 #include "base/threading/simple_thread.h" |
| 22 #include "base/trace_event/trace_event.h" | |
| 22 #include "jni/ShellMain_jni.h" | 23 #include "jni/ShellMain_jni.h" |
| 23 #include "mojo/common/message_pump_mojo.h" | 24 #include "mojo/common/message_pump_mojo.h" |
| 24 #include "shell/android/android_handler_loader.h" | 25 #include "shell/android/android_handler_loader.h" |
| 25 #include "shell/android/background_application_loader.h" | 26 #include "shell/android/background_application_loader.h" |
| 26 #include "shell/android/native_viewport_application_loader.h" | 27 #include "shell/android/native_viewport_application_loader.h" |
| 27 #include "shell/android/ui_application_loader_android.h" | 28 #include "shell/android/ui_application_loader_android.h" |
| 28 #include "shell/application_manager/application_loader.h" | 29 #include "shell/application_manager/application_loader.h" |
| 29 #include "shell/command_line_util.h" | 30 #include "shell/command_line_util.h" |
| 30 #include "shell/context.h" | 31 #include "shell/context.h" |
| 31 #include "shell/init.h" | 32 #include "shell/init.h" |
| 33 #include "shell/switches.h" | |
| 34 #include "shell/tracer.h" | |
| 32 #include "ui/gl/gl_surface_egl.h" | 35 #include "ui/gl/gl_surface_egl.h" |
| 33 | 36 |
| 34 using base::LazyInstance; | 37 using base::LazyInstance; |
| 35 | 38 |
| 36 namespace mojo { | 39 namespace mojo { |
| 37 namespace shell { | 40 namespace shell { |
| 38 | 41 |
| 39 namespace { | 42 namespace { |
| 40 | 43 |
| 41 // Tag for logging. | 44 // Tag for logging. |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 62 LAZY_INSTANCE_INITIALIZER; | 65 LAZY_INSTANCE_INITIALIZER; |
| 63 | 66 |
| 64 LazyInstance<scoped_ptr<Context>> g_context = LAZY_INSTANCE_INITIALIZER; | 67 LazyInstance<scoped_ptr<Context>> g_context = LAZY_INSTANCE_INITIALIZER; |
| 65 | 68 |
| 66 LazyInstance<scoped_ptr<MojoShellRunner>> g_shell_runner = | 69 LazyInstance<scoped_ptr<MojoShellRunner>> g_shell_runner = |
| 67 LAZY_INSTANCE_INITIALIZER; | 70 LAZY_INSTANCE_INITIALIZER; |
| 68 | 71 |
| 69 LazyInstance<scoped_ptr<base::DelegateSimpleThread>> g_shell_thread = | 72 LazyInstance<scoped_ptr<base::DelegateSimpleThread>> g_shell_thread = |
| 70 LAZY_INSTANCE_INITIALIZER; | 73 LAZY_INSTANCE_INITIALIZER; |
| 71 | 74 |
| 72 LazyInstance<base::android::ScopedJavaGlobalRef<jobject>> g_main_activiy = | 75 LazyInstance<base::android::ScopedJavaGlobalRef<jobject>> g_main_activity = |
| 73 LAZY_INSTANCE_INITIALIZER; | 76 LAZY_INSTANCE_INITIALIZER; |
| 74 | 77 |
| 75 void ConfigureAndroidServices(Context* context) { | 78 void ConfigureAndroidServices(Context* context) { |
| 76 context->application_manager()->SetLoaderForURL( | 79 context->application_manager()->SetLoaderForURL( |
| 77 make_scoped_ptr(new UIApplicationLoader( | 80 make_scoped_ptr(new UIApplicationLoader( |
| 78 make_scoped_ptr(new NativeViewportApplicationLoader()), | 81 make_scoped_ptr(new NativeViewportApplicationLoader()), |
| 79 g_java_message_loop.Get().get())), | 82 g_java_message_loop.Get().get())), |
| 80 GURL("mojo:native_viewport_service")); | 83 GURL("mojo:native_viewport_service")); |
| 81 | 84 |
| 82 // Android handler is bundled with the Mojo shell, because it uses the | 85 // Android handler is bundled with the Mojo shell, because it uses the |
| 83 // MojoShell application as the JNI bridge to bootstrap execution of other | 86 // MojoShell application as the JNI bridge to bootstrap execution of other |
| 84 // Android Mojo apps that need JNI. | 87 // Android Mojo apps that need JNI. |
| 85 context->application_manager()->SetLoaderForURL( | 88 context->application_manager()->SetLoaderForURL( |
| 86 make_scoped_ptr(new BackgroundApplicationLoader( | 89 make_scoped_ptr(new BackgroundApplicationLoader( |
| 87 make_scoped_ptr(new AndroidHandlerLoader()), "android_handler", | 90 make_scoped_ptr(new AndroidHandlerLoader()), "android_handler", |
| 88 base::MessageLoop::TYPE_DEFAULT)), | 91 base::MessageLoop::TYPE_DEFAULT)), |
| 89 GURL("mojo:android_handler")); | 92 GURL("mojo:android_handler")); |
| 90 | 93 |
| 91 // By default, the keyboard is handled by the native_viewport_service. | 94 // By default, the keyboard is handled by the native_viewport_service. |
| 92 context->url_resolver()->AddURLMapping(GURL("mojo:keyboard"), | 95 context->url_resolver()->AddURLMapping(GURL("mojo:keyboard"), |
| 93 GURL("mojo:native_viewport_service")); | 96 GURL("mojo:native_viewport_service")); |
| 94 } | 97 } |
| 95 | 98 |
| 96 void QuitShellThread() { | 99 void QuitShellThread() { |
| 97 g_shell_thread.Get()->Join(); | 100 g_shell_thread.Get()->Join(); |
| 98 g_shell_thread.Pointer()->reset(); | 101 g_shell_thread.Pointer()->reset(); |
| 99 Java_ShellMain_finishActivity(base::android::AttachCurrentThread(), | 102 Java_ShellMain_finishActivity(base::android::AttachCurrentThread(), |
| 100 g_main_activiy.Get().obj()); | 103 g_main_activity.Get().obj()); |
| 101 exit(0); | 104 exit(0); |
| 102 } | 105 } |
| 103 | 106 |
| 104 void MojoShellRunner::Run() { | 107 void MojoShellRunner::Run() { |
| 105 base::MessageLoop loop(common::MessagePumpMojo::Create()); | 108 base::MessageLoop loop(common::MessagePumpMojo::Create()); |
| 106 Context* context = g_context.Pointer()->get(); | 109 Context* context = g_context.Pointer()->get(); |
| 107 ConfigureAndroidServices(context); | 110 ConfigureAndroidServices(context); |
| 108 context->Init(); | 111 context->Init(); |
| 109 | 112 |
| 110 for (auto& args : parameters_) | 113 for (auto& args : parameters_) |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 138 | 141 |
| 139 } // namespace | 142 } // namespace |
| 140 | 143 |
| 141 static void Init(JNIEnv* env, | 144 static void Init(JNIEnv* env, |
| 142 jclass clazz, | 145 jclass clazz, |
| 143 jobject activity, | 146 jobject activity, |
| 144 jstring mojo_shell_path, | 147 jstring mojo_shell_path, |
| 145 jobjectArray jparameters, | 148 jobjectArray jparameters, |
| 146 jstring j_local_apps_directory, | 149 jstring j_local_apps_directory, |
| 147 jstring j_tmp_dir) { | 150 jstring j_tmp_dir) { |
| 148 g_main_activiy.Get().Reset(env, activity); | 151 g_main_activity.Get().Reset(env, activity); |
| 149 | 152 |
| 153 std::string tmp_dir = base::android::ConvertJavaStringToUTF8(env, j_tmp_dir); | |
| 150 // Setting the TMPDIR environment variable so that applications can use it. | 154 // Setting the TMPDIR environment variable so that applications can use it. |
| 151 // TODO(qsr) We will need our subprocesses to inherit this. | 155 // TODO(qsr) We will need our subprocesses to inherit this. |
| 152 int return_value = | 156 int return_value = setenv("TMPDIR", tmp_dir.c_str(), 1); |
| 153 setenv("TMPDIR", | |
| 154 base::android::ConvertJavaStringToUTF8(env, j_tmp_dir).c_str(), 1); | |
| 155 DCHECK_EQ(return_value, 0); | 157 DCHECK_EQ(return_value, 0); |
| 156 | 158 |
| 157 base::android::ScopedJavaLocalRef<jobject> scoped_activity(env, activity); | 159 base::android::ScopedJavaLocalRef<jobject> scoped_activity(env, activity); |
| 158 base::android::InitApplicationContext(env, scoped_activity); | 160 base::android::InitApplicationContext(env, scoped_activity); |
| 159 | 161 |
| 160 std::vector<std::string> parameters; | 162 std::vector<std::string> parameters; |
| 161 parameters.push_back( | 163 parameters.push_back( |
| 162 base::android::ConvertJavaStringToUTF8(env, mojo_shell_path)); | 164 base::android::ConvertJavaStringToUTF8(env, mojo_shell_path)); |
| 163 base::android::AppendJavaStringArrayToStringVector(env, jparameters, | 165 base::android::AppendJavaStringArrayToStringVector(env, jparameters, |
| 164 ¶meters); | 166 ¶meters); |
| 165 base::CommandLine::Init(0, nullptr); | 167 base::CommandLine::Init(0, nullptr); |
| 166 base::CommandLine::ForCurrentProcess()->InitFromArgv(parameters); | 168 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 169 command_line->InitFromArgv(parameters); | |
| 170 Tracer tracer; | |
| 171 bool trace_startup = command_line->HasSwitch(switches::kTraceStartup); | |
| 172 if (trace_startup) | |
| 173 tracer.Start(command_line->GetSwitchValueASCII(switches::kTraceStartup)); | |
| 174 | |
| 167 g_shell_runner.Get().reset(new MojoShellRunner(parameters)); | 175 g_shell_runner.Get().reset(new MojoShellRunner(parameters)); |
| 168 | 176 |
| 169 InitializeLogging(); | 177 InitializeLogging(); |
| 170 | 178 |
| 171 InitializeRedirection(); | 179 InitializeRedirection(); |
| 172 | 180 |
| 173 // We want ~MessageLoop to happen prior to ~Context. Initializing | 181 // We want ~MessageLoop to happen prior to ~Context. Initializing |
| 174 // LazyInstances is akin to stack-allocating objects; their destructors | 182 // LazyInstances is akin to stack-allocating objects; their destructors |
| 175 // will be invoked first-in-last-out. | 183 // will be invoked first-in-last-out. |
| 176 Context* shell_context = new Context(); | 184 Context* shell_context = new Context(); |
| 177 shell_context->SetShellFileRoot(base::FilePath( | 185 shell_context->SetShellFileRoot(base::FilePath( |
| 178 base::android::ConvertJavaStringToUTF8(env, j_local_apps_directory))); | 186 base::android::ConvertJavaStringToUTF8(env, j_local_apps_directory))); |
| 179 g_context.Get().reset(shell_context); | 187 g_context.Get().reset(shell_context); |
| 180 | 188 |
| 181 g_java_message_loop.Get().reset(new base::MessageLoopForUI); | 189 g_java_message_loop.Get().reset(new base::MessageLoopForUI); |
| 182 base::MessageLoopForUI::current()->Start(); | 190 base::MessageLoopForUI::current()->Start(); |
| 183 | 191 |
| 192 if (trace_startup) { | |
| 193 g_java_message_loop.Get()->PostDelayedTask( | |
| 194 FROM_HERE, | |
| 195 base::Bind(&Tracer::StopAndFlushToFile, base::Unretained(&tracer), | |
|
qsr
2015/04/08 08:55:57
I am missing something here. tracer is a stack var
| |
| 196 tmp_dir + "/mojo_shell.trace"), | |
| 197 base::TimeDelta::FromSeconds(5)); | |
| 198 } | |
| 199 | |
| 184 // TODO(abarth): At which point should we switch to cross-platform | 200 // TODO(abarth): At which point should we switch to cross-platform |
| 185 // initialization? | 201 // initialization? |
| 186 | 202 |
| 187 gfx::GLSurface::InitializeOneOff(); | 203 gfx::GLSurface::InitializeOneOff(); |
| 188 } | 204 } |
| 189 | 205 |
| 190 static jboolean Start(JNIEnv* env, jclass clazz) { | 206 static jboolean Start(JNIEnv* env, jclass clazz) { |
| 191 if (!base::CommandLine::ForCurrentProcess()->GetArgs().size()) | 207 if (!base::CommandLine::ForCurrentProcess()->GetArgs().size()) |
| 192 return false; | 208 return false; |
| 193 | 209 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 207 base::CommandLine::ForCurrentProcess()->AppendArg( | 223 base::CommandLine::ForCurrentProcess()->AppendArg( |
| 208 base::android::ConvertJavaStringToUTF8(env, jurl)); | 224 base::android::ConvertJavaStringToUTF8(env, jurl)); |
| 209 } | 225 } |
| 210 | 226 |
| 211 bool RegisterShellMain(JNIEnv* env) { | 227 bool RegisterShellMain(JNIEnv* env) { |
| 212 return RegisterNativesImpl(env); | 228 return RegisterNativesImpl(env); |
| 213 } | 229 } |
| 214 | 230 |
| 215 } // namespace shell | 231 } // namespace shell |
| 216 } // namespace mojo | 232 } // namespace mojo |
| OLD | NEW |