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/shell/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/shell/android/android_handler_loader.h"
25 #include "mojo/shell/android/background_application_loader.h"
26 #include "mojo/shell/android/native_viewport_application_loader.h"
27 #include "mojo/shell/android/ui_application_loader_android.h"
28 #include "mojo/shell/application_manager/application_loader.h"
29 #include "mojo/shell/context.h"
30 #include "mojo/shell/init.h"
31 #include "ui/gl/gl_surface_egl.h"
33 using base::LazyInstance
;
41 const char kLogTag
[] = "chromium";
43 // Command line argument for the communication fifo.
44 const char kFifoPath
[] = "fifo-path";
46 class MojoShellRunner
: public base::DelegateSimpleThread::Delegate
{
48 MojoShellRunner(const std::vector
<std::string
>& parameters
) {}
49 ~MojoShellRunner() override
{}
54 DISALLOW_COPY_AND_ASSIGN(MojoShellRunner
);
57 LazyInstance
<scoped_ptr
<base::MessageLoop
>> g_java_message_loop
=
58 LAZY_INSTANCE_INITIALIZER
;
60 LazyInstance
<scoped_ptr
<Context
>> g_context
= LAZY_INSTANCE_INITIALIZER
;
62 LazyInstance
<scoped_ptr
<MojoShellRunner
>> g_shell_runner
=
63 LAZY_INSTANCE_INITIALIZER
;
65 LazyInstance
<scoped_ptr
<base::DelegateSimpleThread
>> g_shell_thread
=
66 LAZY_INSTANCE_INITIALIZER
;
68 LazyInstance
<base::android::ScopedJavaGlobalRef
<jobject
>> g_main_activiy
=
69 LAZY_INSTANCE_INITIALIZER
;
71 void ConfigureAndroidServices(Context
* context
) {
72 context
->application_manager()->SetLoaderForURL(
73 make_scoped_ptr(new UIApplicationLoader(
74 make_scoped_ptr(new NativeViewportApplicationLoader()),
75 g_java_message_loop
.Get().get())),
76 GURL("mojo:native_viewport_service"));
78 // Android handler is bundled with the Mojo shell, because it uses the
79 // MojoShell application as the JNI bridge to bootstrap execution of other
80 // Android Mojo apps that need JNI.
81 context
->application_manager()->SetLoaderForURL(
82 make_scoped_ptr(new BackgroundApplicationLoader(
83 make_scoped_ptr(new AndroidHandlerLoader()), "android_handler",
84 base::MessageLoop::TYPE_DEFAULT
)),
85 GURL("mojo:android_handler"));
88 void QuitShellThread() {
89 g_shell_thread
.Get()->Join();
90 g_shell_thread
.Pointer()->reset();
91 Java_ShellMain_finishActivity(base::android::AttachCurrentThread(),
92 g_main_activiy
.Get().obj());
96 void MojoShellRunner::Run() {
97 base::MessageLoop
loop(common::MessagePumpMojo::Create());
98 Context
* context
= g_context
.Pointer()->get();
99 ConfigureAndroidServices(context
);
102 context
->Run(GURL("mojo:window_manager"));
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
));
160 InitializeRedirection();
162 // We want ~MessageLoop to happen prior to ~Context. Initializing
163 // LazyInstances is akin to stack-allocating objects; their destructors
164 // will be invoked first-in-last-out.
165 Context
* shell_context
= new Context();
166 shell_context
->SetShellFileRoot(base::FilePath(
167 base::android::ConvertJavaStringToUTF8(env
, j_local_apps_directory
)));
168 g_context
.Get().reset(shell_context
);
170 g_java_message_loop
.Get().reset(new base::MessageLoopForUI
);
171 base::MessageLoopForUI::current()->Start();
173 // TODO(abarth): At which point should we switch to cross-platform
176 gfx::GLSurface::InitializeOneOff();
179 static jboolean
Start(JNIEnv
* env
, jclass clazz
) {
180 if (!base::CommandLine::ForCurrentProcess()->GetArgs().size())
183 #if defined(MOJO_SHELL_DEBUG_URL)
184 base::CommandLine::ForCurrentProcess()->AppendArg(MOJO_SHELL_DEBUG_URL
);
185 // Sleep for 5 seconds to give the debugger a chance to attach.
189 g_shell_thread
.Get().reset(new base::DelegateSimpleThread(
190 g_shell_runner
.Get().get(), "ShellThread"));
191 g_shell_thread
.Get()->Start();
195 static void AddApplicationURL(JNIEnv
* env
, jclass clazz
, jstring jurl
) {
196 base::CommandLine::ForCurrentProcess()->AppendArg(
197 base::android::ConvertJavaStringToUTF8(env
, jurl
));
200 bool RegisterShellMain(JNIEnv
* env
) {
201 return RegisterNativesImpl(env
);
207 // TODO(vtl): Even though main() should never be called, mojo_shell fails to
208 // link without it. Figure out if we can avoid this.
209 int main(int argc
, char** argv
) {