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 "base/threading/simple_thread.h"
22 #include "components/view_manager/android_loader.h"
23 #include "jni/ShellMain_jni.h"
24 #include "mojo/common/message_pump_mojo.h"
25 #include "mojo/runner/android/android_handler_loader.h"
26 #include "mojo/runner/android/background_application_loader.h"
27 #include "mojo/runner/android/context_init.h"
28 #include "mojo/runner/android/ui_application_loader_android.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 class MojoShellRunner
: public base::DelegateSimpleThread::Delegate
{
49 MojoShellRunner(const std::vector
<std::string
>& parameters
) {}
50 ~MojoShellRunner() override
{}
55 DISALLOW_COPY_AND_ASSIGN(MojoShellRunner
);
58 LazyInstance
<scoped_ptr
<base::MessageLoop
>> g_java_message_loop
=
59 LAZY_INSTANCE_INITIALIZER
;
61 LazyInstance
<scoped_ptr
<Context
>> g_context
= LAZY_INSTANCE_INITIALIZER
;
63 LazyInstance
<scoped_ptr
<MojoShellRunner
>> g_shell_runner
=
64 LAZY_INSTANCE_INITIALIZER
;
66 LazyInstance
<scoped_ptr
<base::DelegateSimpleThread
>> g_shell_thread
=
67 LAZY_INSTANCE_INITIALIZER
;
69 LazyInstance
<base::android::ScopedJavaGlobalRef
<jobject
>> g_main_activiy
=
70 LAZY_INSTANCE_INITIALIZER
;
72 void ConfigureAndroidServices(Context
* context
) {
73 context
->application_manager()->SetLoaderForURL(
74 make_scoped_ptr(new UIApplicationLoader(
75 make_scoped_ptr(new view_manager::AndroidLoader()),
76 g_java_message_loop
.Get().get())),
77 GURL("mojo:view_manager"));
79 // Android handler is bundled with the Mojo shell, because it uses the
80 // MojoShell application as the JNI bridge to bootstrap execution of other
81 // Android Mojo apps that need JNI.
82 context
->application_manager()->SetLoaderForURL(
83 make_scoped_ptr(new BackgroundApplicationLoader(
84 make_scoped_ptr(new AndroidHandlerLoader()), "android_handler",
85 base::MessageLoop::TYPE_DEFAULT
)),
86 GURL("mojo:android_handler"));
89 void QuitShellThread() {
90 g_shell_thread
.Get()->Join();
91 g_shell_thread
.Pointer()->reset();
92 Java_ShellMain_finishActivity(base::android::AttachCurrentThread(),
93 g_main_activiy
.Get().obj());
97 void MojoShellRunner::Run() {
98 base::MessageLoop
loop(common::MessagePumpMojo::Create());
99 Context
* context
= g_context
.Pointer()->get();
100 ConfigureAndroidServices(context
);
102 context
->RunCommandLineApplication();
105 g_java_message_loop
.Pointer()->get()->PostTask(FROM_HERE
,
106 base::Bind(&QuitShellThread
));
109 // Initialize stdout redirection if the command line switch is present.
110 void InitializeRedirection() {
111 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(kFifoPath
))
114 base::FilePath fifo_path
=
115 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(kFifoPath
);
116 base::FilePath directory
= fifo_path
.DirName();
117 CHECK(base::CreateDirectoryAndGetError(directory
, nullptr))
118 << "Unable to create directory: " << directory
.value();
119 unlink(fifo_path
.value().c_str());
120 CHECK(base::android::CreateFIFO(fifo_path
, 0666))
121 << "Unable to create fifo: " << fifo_path
.value();
122 CHECK(base::android::RedirectStream(stdout
, fifo_path
, "w"))
123 << "Failed to redirect stdout to file: " << fifo_path
.value();
124 CHECK(dup2(STDOUT_FILENO
, STDERR_FILENO
) != -1)
125 << "Unable to redirect stderr to stdout.";
130 static void Init(JNIEnv
* env
,
133 jstring mojo_shell_path
,
134 jobjectArray jparameters
,
135 jstring j_local_apps_directory
,
137 g_main_activiy
.Get().Reset(env
, activity
);
139 // Setting the TMPDIR environment variable so that applications can use it.
140 // TODO(qsr) We will need our subprocesses to inherit this.
143 base::android::ConvertJavaStringToUTF8(env
, j_tmp_dir
).c_str(), 1);
144 DCHECK_EQ(return_value
, 0);
146 base::android::ScopedJavaLocalRef
<jobject
> scoped_activity(env
, activity
);
147 base::android::InitApplicationContext(env
, scoped_activity
);
149 std::vector
<std::string
> parameters
;
150 parameters
.push_back(
151 base::android::ConvertJavaStringToUTF8(env
, mojo_shell_path
));
152 base::android::AppendJavaStringArrayToStringVector(env
, jparameters
,
154 base::CommandLine::Init(0, nullptr);
155 base::CommandLine::ForCurrentProcess()->InitFromArgv(parameters
);
156 g_shell_runner
.Get().reset(new MojoShellRunner(parameters
));
159 mojo::runner::WaitForDebuggerIfNecessary();
161 InitializeRedirection();
163 // We want ~MessageLoop to happen prior to ~Context. Initializing
164 // LazyInstances is akin to stack-allocating objects; their destructors
165 // will be invoked first-in-last-out.
166 Context
* shell_context
= new Context();
167 shell_context
->SetShellFileRoot(base::FilePath(
168 base::android::ConvertJavaStringToUTF8(env
, j_local_apps_directory
)));
169 g_context
.Get().reset(shell_context
);
171 g_java_message_loop
.Get().reset(new base::MessageLoopForUI
);
172 base::MessageLoopForUI::current()->Start();
174 // This is done after the main message loop is started since it may post
175 // tasks. This is consistent with the ordering from the desktop version of
176 // this file (../desktop/launcher_process.cc).
177 InitContext(shell_context
);
179 // TODO(abarth): At which point should we switch to cross-platform
182 gfx::GLSurface::InitializeOneOff();
185 static jboolean
Start(JNIEnv
* env
, jclass clazz
) {
186 if (!base::CommandLine::ForCurrentProcess()->GetArgs().size())
189 #if defined(MOJO_SHELL_DEBUG_URL)
190 base::CommandLine::ForCurrentProcess()->AppendArg(MOJO_SHELL_DEBUG_URL
);
191 // Sleep for 5 seconds to give the debugger a chance to attach.
195 g_shell_thread
.Get().reset(new base::DelegateSimpleThread(
196 g_shell_runner
.Get().get(), "ShellThread"));
197 g_shell_thread
.Get()->Start();
201 static void AddApplicationURL(JNIEnv
* env
, jclass clazz
, jstring jurl
) {
202 base::CommandLine::ForCurrentProcess()->AppendArg(
203 base::android::ConvertJavaStringToUTF8(env
, jurl
));
206 bool RegisterShellMain(JNIEnv
* env
) {
207 return RegisterNativesImpl(env
);
210 } // namespace runner
213 // TODO(vtl): Even though main() should never be called, mojo_shell fails to
214 // link without it. Figure out if we can avoid this.
215 int main(int argc
, char** argv
) {