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 "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/native_viewport_application_loader.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 NativeViewportApplicationLoader()),
76 g_java_message_loop
.Get().get())),
77 GURL("mojo:native_viewport_service"));
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
);
103 context
->Run(GURL("mojo:window_manager"));
106 g_java_message_loop
.Pointer()->get()->PostTask(FROM_HERE
,
107 base::Bind(&QuitShellThread
));
110 // Initialize stdout redirection if the command line switch is present.
111 void InitializeRedirection() {
112 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(kFifoPath
))
115 base::FilePath fifo_path
=
116 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(kFifoPath
);
117 base::FilePath directory
= fifo_path
.DirName();
118 CHECK(base::CreateDirectoryAndGetError(directory
, nullptr))
119 << "Unable to create directory: " << directory
.value();
120 unlink(fifo_path
.value().c_str());
121 CHECK(base::android::CreateFIFO(fifo_path
, 0666))
122 << "Unable to create fifo: " << fifo_path
.value();
123 CHECK(base::android::RedirectStream(stdout
, fifo_path
, "w"))
124 << "Failed to redirect stdout to file: " << fifo_path
.value();
125 CHECK(dup2(STDOUT_FILENO
, STDERR_FILENO
) != -1)
126 << "Unable to redirect stderr to stdout.";
131 static void Init(JNIEnv
* env
,
134 jstring mojo_shell_path
,
135 jobjectArray jparameters
,
136 jstring j_local_apps_directory
,
138 g_main_activiy
.Get().Reset(env
, activity
);
140 // Setting the TMPDIR environment variable so that applications can use it.
141 // TODO(qsr) We will need our subprocesses to inherit this.
144 base::android::ConvertJavaStringToUTF8(env
, j_tmp_dir
).c_str(), 1);
145 DCHECK_EQ(return_value
, 0);
147 base::android::ScopedJavaLocalRef
<jobject
> scoped_activity(env
, activity
);
148 base::android::InitApplicationContext(env
, scoped_activity
);
150 std::vector
<std::string
> parameters
;
151 parameters
.push_back(
152 base::android::ConvertJavaStringToUTF8(env
, mojo_shell_path
));
153 base::android::AppendJavaStringArrayToStringVector(env
, jparameters
,
155 base::CommandLine::Init(0, nullptr);
156 base::CommandLine::ForCurrentProcess()->InitFromArgv(parameters
);
157 g_shell_runner
.Get().reset(new MojoShellRunner(parameters
));
160 mojo::runner::WaitForDebuggerIfNecessary();
162 InitializeRedirection();
164 // We want ~MessageLoop to happen prior to ~Context. Initializing
165 // LazyInstances is akin to stack-allocating objects; their destructors
166 // will be invoked first-in-last-out.
167 Context
* shell_context
= new Context();
168 shell_context
->SetShellFileRoot(base::FilePath(
169 base::android::ConvertJavaStringToUTF8(env
, j_local_apps_directory
)));
170 InitContext(shell_context
);
171 g_context
.Get().reset(shell_context
);
173 g_java_message_loop
.Get().reset(new base::MessageLoopForUI
);
174 base::MessageLoopForUI::current()->Start();
176 // TODO(abarth): At which point should we switch to cross-platform
179 gfx::GLSurface::InitializeOneOff();
182 static jboolean
Start(JNIEnv
* env
, jclass clazz
) {
183 if (!base::CommandLine::ForCurrentProcess()->GetArgs().size())
186 #if defined(MOJO_SHELL_DEBUG_URL)
187 base::CommandLine::ForCurrentProcess()->AppendArg(MOJO_SHELL_DEBUG_URL
);
188 // Sleep for 5 seconds to give the debugger a chance to attach.
192 g_shell_thread
.Get().reset(new base::DelegateSimpleThread(
193 g_shell_runner
.Get().get(), "ShellThread"));
194 g_shell_thread
.Get()->Start();
198 static void AddApplicationURL(JNIEnv
* env
, jclass clazz
, jstring jurl
) {
199 base::CommandLine::ForCurrentProcess()->AppendArg(
200 base::android::ConvertJavaStringToUTF8(env
, jurl
));
203 bool RegisterShellMain(JNIEnv
* env
) {
204 return RegisterNativesImpl(env
);
207 } // namespace runner
210 // TODO(vtl): Even though main() should never be called, mojo_shell fails to
211 // link without it. Figure out if we can avoid this.
212 int main(int argc
, char** argv
) {