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/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/lazy_instance.h"
17 #include "base/logging.h"
18 #include "base/macros.h"
19 #include "base/message_loop/message_loop.h"
20 #include "base/run_loop.h"
21 #include "components/view_manager/android_loader.h"
22 #include "jni/ShellMain_jni.h"
23 #include "mojo/common/message_pump_mojo.h"
24 #include "mojo/runner/android/android_handler_loader.h"
25 #include "mojo/runner/android/background_application_loader.h"
26 #include "mojo/runner/android/context_init.h"
27 #include "mojo/runner/android/ui_application_loader_android.h"
28 #include "mojo/runner/child_process.h"
29 #include "mojo/runner/context.h"
30 #include "mojo/runner/init.h"
31 #include "mojo/shell/application_loader.h"
32 #include "ui/gl/gl_surface_egl.h"
34 using base::LazyInstance
;
42 const char kLogTag
[] = "chromium";
44 // Command line argument for the communication fifo.
45 const char kFifoPath
[] = "fifo-path";
47 LazyInstance
<scoped_ptr
<base::MessageLoop
>> g_java_message_loop
=
48 LAZY_INSTANCE_INITIALIZER
;
50 LazyInstance
<scoped_ptr
<Context
>> g_context
= LAZY_INSTANCE_INITIALIZER
;
52 LazyInstance
<base::android::ScopedJavaGlobalRef
<jobject
>> g_main_activiy
=
53 LAZY_INSTANCE_INITIALIZER
;
55 void ConfigureAndroidServices(Context
* context
) {
56 context
->application_manager()->SetLoaderForURL(
57 make_scoped_ptr(new UIApplicationLoader(
58 make_scoped_ptr(new view_manager::AndroidLoader()),
59 g_java_message_loop
.Get().get())),
60 GURL("mojo:view_manager"));
62 // Android handler is bundled with the Mojo shell, because it uses the
63 // MojoShell application as the JNI bridge to bootstrap execution of other
64 // Android Mojo apps that need JNI.
65 context
->application_manager()->SetLoaderForURL(
66 make_scoped_ptr(new BackgroundApplicationLoader(
67 make_scoped_ptr(new AndroidHandlerLoader()), "android_handler",
68 base::MessageLoop::TYPE_DEFAULT
)),
69 GURL("mojo:android_handler"));
73 Java_ShellMain_finishActivity(base::android::AttachCurrentThread(),
74 g_main_activiy
.Get().obj());
78 // Initialize stdout redirection if the command line switch is present.
79 void InitializeRedirection() {
80 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(kFifoPath
))
83 base::FilePath fifo_path
=
84 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(kFifoPath
);
85 base::FilePath directory
= fifo_path
.DirName();
86 CHECK(base::CreateDirectoryAndGetError(directory
, nullptr))
87 << "Unable to create directory: " << directory
.value();
88 unlink(fifo_path
.value().c_str());
89 CHECK(base::android::CreateFIFO(fifo_path
, 0666))
90 << "Unable to create fifo: " << fifo_path
.value();
91 CHECK(base::android::RedirectStream(stdout
, fifo_path
, "w"))
92 << "Failed to redirect stdout to file: " << fifo_path
.value();
93 CHECK(dup2(STDOUT_FILENO
, STDERR_FILENO
) != -1)
94 << "Unable to redirect stderr to stdout.";
99 static void Init(JNIEnv
* env
,
102 jstring mojo_shell_path
,
103 jobjectArray jparameters
,
104 jstring j_local_apps_directory
,
106 g_main_activiy
.Get().Reset(env
, activity
);
108 // Setting the TMPDIR environment variable so that applications can use it.
109 // TODO(qsr) We will need our subprocesses to inherit this.
112 base::android::ConvertJavaStringToUTF8(env
, j_tmp_dir
).c_str(), 1);
113 DCHECK_EQ(return_value
, 0);
115 base::android::ScopedJavaLocalRef
<jobject
> scoped_activity(env
, activity
);
116 base::android::InitApplicationContext(env
, scoped_activity
);
118 std::vector
<std::string
> parameters
;
119 parameters
.push_back(
120 base::android::ConvertJavaStringToUTF8(env
, mojo_shell_path
));
121 base::android::AppendJavaStringArrayToStringVector(env
, jparameters
,
123 base::CommandLine::Init(0, nullptr);
124 base::CommandLine::ForCurrentProcess()->InitFromArgv(parameters
);
127 mojo::runner::WaitForDebuggerIfNecessary();
129 InitializeRedirection();
131 // We want ~MessageLoop to happen prior to ~Context. Initializing
132 // LazyInstances is akin to stack-allocating objects; their destructors
133 // will be invoked first-in-last-out.
134 Context
* shell_context
= new Context();
135 shell_context
->SetShellFileRoot(base::FilePath(
136 base::android::ConvertJavaStringToUTF8(env
, j_local_apps_directory
)));
137 g_context
.Get().reset(shell_context
);
139 g_java_message_loop
.Get().reset(new base::MessageLoopForUI
);
140 base::MessageLoopForUI::current()->Start();
142 ConfigureAndroidServices(shell_context
);
143 shell_context
->Init();
145 // This is done after the main message loop is started since it may post
146 // tasks. This is consistent with the ordering from the desktop version of
147 // this file (../desktop/launcher_process.cc).
148 InitContext(shell_context
);
150 // TODO(abarth): At which point should we switch to cross-platform
153 gfx::GLSurface::InitializeOneOff();
156 static void Start(JNIEnv
* env
, jclass clazz
) {
157 #if defined(MOJO_SHELL_DEBUG_URL)
158 base::CommandLine::ForCurrentProcess()->AppendArg(MOJO_SHELL_DEBUG_URL
);
159 // Sleep for 5 seconds to give the debugger a chance to attach.
163 Context
* context
= g_context
.Pointer()->get();
164 context
->RunCommandLineApplication(base::Bind(ExitShell
));
167 static void AddApplicationURL(JNIEnv
* env
, jclass clazz
, jstring jurl
) {
168 base::CommandLine::ForCurrentProcess()->AppendArg(
169 base::android::ConvertJavaStringToUTF8(env
, jurl
));
172 bool RegisterShellMain(JNIEnv
* env
) {
173 return RegisterNativesImpl(env
);
176 Context
* GetContext() {
177 return g_context
.Get().get();
180 } // namespace runner
183 int main(int argc
, char** argv
) {
184 base::AtExitManager at_exit
;
185 base::CommandLine::Init(argc
, argv
);
187 mojo::runner::InitializeLogging();
188 return mojo::runner::ChildProcessMain();