Roll src/third_party/WebKit 3aea697:d9c6159 (svn 201973:201974)
[chromium-blink-merge.git] / mojo / runner / android / main.cc
blobb5717cca784c63495f2e58aa454a2378a6b21ba4
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.
5 #include "mojo/runner/android/main.h"
7 #include "base/android/fifo_utils.h"
8 #include "base/android/jni_android.h"
9 #include "base/android/jni_array.h"
10 #include "base/android/jni_string.h"
11 #include "base/at_exit.h"
12 #include "base/bind.h"
13 #include "base/command_line.h"
14 #include "base/debug/stack_trace.h"
15 #include "base/files/file_path.h"
16 #include "base/files/file_util.h"
17 #include "base/lazy_instance.h"
18 #include "base/logging.h"
19 #include "base/macros.h"
20 #include "base/message_loop/message_loop.h"
21 #include "base/run_loop.h"
22 #include "build/build_config.h"
23 #include "components/view_manager/android_loader.h"
24 #include "jni/ShellMain_jni.h"
25 #include "mojo/message_pump/message_pump_mojo.h"
26 #include "mojo/runner/android/android_handler_loader.h"
27 #include "mojo/runner/android/background_application_loader.h"
28 #include "mojo/runner/android/context_init.h"
29 #include "mojo/runner/android/ui_application_loader_android.h"
30 #include "mojo/runner/child_process.h"
31 #include "mojo/runner/context.h"
32 #include "mojo/runner/init.h"
33 #include "mojo/shell/application_loader.h"
34 #include "ui/gl/gl_surface_egl.h"
36 using base::LazyInstance;
38 namespace mojo {
39 namespace runner {
41 namespace {
43 // Tag for logging.
44 const char kLogTag[] = "chromium";
46 // Command line argument for the communication fifo.
47 const char kFifoPath[] = "fifo-path";
49 LazyInstance<scoped_ptr<base::MessageLoop>> g_java_message_loop =
50 LAZY_INSTANCE_INITIALIZER;
52 LazyInstance<scoped_ptr<Context>> g_context = LAZY_INSTANCE_INITIALIZER;
54 LazyInstance<base::android::ScopedJavaGlobalRef<jobject>> g_main_activiy =
55 LAZY_INSTANCE_INITIALIZER;
57 void ConfigureAndroidServices(Context* context) {
58 context->application_manager()->SetLoaderForURL(
59 make_scoped_ptr(new UIApplicationLoader(
60 make_scoped_ptr(new view_manager::AndroidLoader()),
61 g_java_message_loop.Get().get())),
62 GURL("mojo:view_manager"));
64 // Android handler is bundled with the Mojo shell, because it uses the
65 // MojoShell application as the JNI bridge to bootstrap execution of other
66 // Android Mojo apps that need JNI.
67 context->application_manager()->SetLoaderForURL(
68 make_scoped_ptr(new BackgroundApplicationLoader(
69 make_scoped_ptr(new AndroidHandlerLoader()), "android_handler",
70 base::MessageLoop::TYPE_DEFAULT)),
71 GURL("mojo:android_handler"));
74 void ExitShell() {
75 Java_ShellMain_finishActivity(base::android::AttachCurrentThread(),
76 g_main_activiy.Get().obj());
77 exit(0);
80 // Initialize stdout redirection if the command line switch is present.
81 void InitializeRedirection() {
82 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(kFifoPath))
83 return;
85 base::FilePath fifo_path =
86 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(kFifoPath);
87 base::FilePath directory = fifo_path.DirName();
88 CHECK(base::CreateDirectoryAndGetError(directory, nullptr))
89 << "Unable to create directory: " << directory.value();
90 unlink(fifo_path.value().c_str());
91 CHECK(base::android::CreateFIFO(fifo_path, 0666))
92 << "Unable to create fifo: " << fifo_path.value();
93 CHECK(base::android::RedirectStream(stdout, fifo_path, "w"))
94 << "Failed to redirect stdout to file: " << fifo_path.value();
95 CHECK(dup2(STDOUT_FILENO, STDERR_FILENO) != -1)
96 << "Unable to redirect stderr to stdout.";
99 } // namespace
101 static void Init(JNIEnv* env,
102 const JavaParamRef<jclass>& clazz,
103 const JavaParamRef<jobject>& activity,
104 const JavaParamRef<jstring>& mojo_shell_path,
105 const JavaParamRef<jobjectArray>& jparameters,
106 const JavaParamRef<jstring>& j_local_apps_directory,
107 const JavaParamRef<jstring>& j_tmp_dir) {
108 g_main_activiy.Get().Reset(env, activity);
110 // Setting the TMPDIR environment variable so that applications can use it.
111 // TODO(qsr) We will need our subprocesses to inherit this.
112 int return_value =
113 setenv("TMPDIR",
114 base::android::ConvertJavaStringToUTF8(env, j_tmp_dir).c_str(), 1);
115 DCHECK_EQ(return_value, 0);
117 base::android::ScopedJavaLocalRef<jobject> scoped_activity(env, activity);
118 base::android::InitApplicationContext(env, scoped_activity);
120 std::vector<std::string> parameters;
121 parameters.push_back(
122 base::android::ConvertJavaStringToUTF8(env, mojo_shell_path));
123 base::android::AppendJavaStringArrayToStringVector(env, jparameters,
124 &parameters);
125 base::CommandLine::Init(0, nullptr);
126 base::CommandLine::ForCurrentProcess()->InitFromArgv(parameters);
128 InitializeLogging();
129 mojo::runner::WaitForDebuggerIfNecessary();
131 InitializeRedirection();
133 // We want ~MessageLoop to happen prior to ~Context. Initializing
134 // LazyInstances is akin to stack-allocating objects; their destructors
135 // will be invoked first-in-last-out.
136 Context* shell_context = new Context();
137 shell_context->SetShellFileRoot(base::FilePath(
138 base::android::ConvertJavaStringToUTF8(env, j_local_apps_directory)));
139 g_context.Get().reset(shell_context);
141 g_java_message_loop.Get().reset(new base::MessageLoopForUI);
142 base::MessageLoopForUI::current()->Start();
144 ConfigureAndroidServices(shell_context);
145 shell_context->Init();
147 // This is done after the main message loop is started since it may post
148 // tasks. This is consistent with the ordering from the desktop version of
149 // this file (../desktop/launcher_process.cc).
150 InitContext(shell_context);
152 // TODO(abarth): At which point should we switch to cross-platform
153 // initialization?
155 gfx::GLSurface::InitializeOneOff();
158 static void Start(JNIEnv* env, const JavaParamRef<jclass>& clazz) {
159 #if defined(MOJO_SHELL_DEBUG_URL)
160 base::CommandLine::ForCurrentProcess()->AppendArg(MOJO_SHELL_DEBUG_URL);
161 // Sleep for 5 seconds to give the debugger a chance to attach.
162 sleep(5);
163 #endif
165 Context* context = g_context.Pointer()->get();
166 context->RunCommandLineApplication(base::Bind(ExitShell));
169 static void AddApplicationURL(JNIEnv* env,
170 const JavaParamRef<jclass>& clazz,
171 const JavaParamRef<jstring>& jurl) {
172 base::CommandLine::ForCurrentProcess()->AppendArg(
173 base::android::ConvertJavaStringToUTF8(env, jurl));
176 bool RegisterShellMain(JNIEnv* env) {
177 return RegisterNativesImpl(env);
180 Context* GetContext() {
181 return g_context.Get().get();
184 } // namespace runner
185 } // namespace mojo
187 int main(int argc, char** argv) {
188 base::AtExitManager at_exit;
189 base::CommandLine::Init(argc, argv);
191 #if !defined(OFFICIAL_BUILD)
192 base::debug::EnableInProcessStackDumping();
193 #endif
195 mojo::runner::InitializeLogging();
196 return mojo::runner::ChildProcessMain();