OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "mojo/shell/android/main.h" |
| 6 |
| 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/command_line_util.h" |
| 30 #include "mojo/shell/context.h" |
| 31 #include "mojo/shell/init.h" |
| 32 #include "ui/gl/gl_surface_egl.h" |
| 33 |
| 34 using base::LazyInstance; |
| 35 |
| 36 namespace mojo { |
| 37 namespace shell { |
| 38 |
| 39 namespace { |
| 40 |
| 41 // Tag for logging. |
| 42 const char kLogTag[] = "chromium"; |
| 43 |
| 44 // Command line argument for the communication fifo. |
| 45 const char kFifoPath[] = "fifo-path"; |
| 46 |
| 47 class MojoShellRunner : public base::DelegateSimpleThread::Delegate { |
| 48 public: |
| 49 MojoShellRunner(const std::vector<std::string>& parameters) |
| 50 : parameters_(parameters) {} |
| 51 ~MojoShellRunner() override {} |
| 52 |
| 53 private: |
| 54 void Run() override; |
| 55 |
| 56 std::vector<std::string> parameters_; |
| 57 |
| 58 DISALLOW_COPY_AND_ASSIGN(MojoShellRunner); |
| 59 }; |
| 60 |
| 61 LazyInstance<scoped_ptr<base::MessageLoop>> g_java_message_loop = |
| 62 LAZY_INSTANCE_INITIALIZER; |
| 63 |
| 64 LazyInstance<scoped_ptr<Context>> g_context = LAZY_INSTANCE_INITIALIZER; |
| 65 |
| 66 LazyInstance<scoped_ptr<MojoShellRunner>> g_shell_runner = |
| 67 LAZY_INSTANCE_INITIALIZER; |
| 68 |
| 69 LazyInstance<scoped_ptr<base::DelegateSimpleThread>> g_shell_thread = |
| 70 LAZY_INSTANCE_INITIALIZER; |
| 71 |
| 72 LazyInstance<base::android::ScopedJavaGlobalRef<jobject>> g_main_activiy = |
| 73 LAZY_INSTANCE_INITIALIZER; |
| 74 |
| 75 void ConfigureAndroidServices(Context* context) { |
| 76 context->application_manager()->SetLoaderForURL( |
| 77 make_scoped_ptr(new UIApplicationLoader( |
| 78 make_scoped_ptr(new NativeViewportApplicationLoader()), |
| 79 g_java_message_loop.Get().get())), |
| 80 GURL("mojo:native_viewport_service")); |
| 81 |
| 82 // Android handler is bundled with the Mojo shell, because it uses the |
| 83 // MojoShell application as the JNI bridge to bootstrap execution of other |
| 84 // Android Mojo apps that need JNI. |
| 85 context->application_manager()->SetLoaderForURL( |
| 86 make_scoped_ptr(new BackgroundApplicationLoader( |
| 87 make_scoped_ptr(new AndroidHandlerLoader()), "android_handler", |
| 88 base::MessageLoop::TYPE_DEFAULT)), |
| 89 GURL("mojo:android_handler")); |
| 90 |
| 91 // By default, the keyboard is handled by the native_viewport_service. |
| 92 context->url_resolver()->AddURLMapping(GURL("mojo:keyboard"), |
| 93 GURL("mojo:native_viewport_service")); |
| 94 } |
| 95 |
| 96 void QuitShellThread() { |
| 97 g_shell_thread.Get()->Join(); |
| 98 g_shell_thread.Pointer()->reset(); |
| 99 Java_ShellMain_finishActivity(base::android::AttachCurrentThread(), |
| 100 g_main_activiy.Get().obj()); |
| 101 exit(0); |
| 102 } |
| 103 |
| 104 void MojoShellRunner::Run() { |
| 105 base::MessageLoop loop(common::MessagePumpMojo::Create()); |
| 106 Context* context = g_context.Pointer()->get(); |
| 107 ConfigureAndroidServices(context); |
| 108 context->Init(); |
| 109 |
| 110 for (auto& args : parameters_) |
| 111 ApplyApplicationArgs(context, args); |
| 112 |
| 113 RunCommandLineApps(context); |
| 114 loop.Run(); |
| 115 |
| 116 g_java_message_loop.Pointer()->get()->PostTask(FROM_HERE, |
| 117 base::Bind(&QuitShellThread)); |
| 118 } |
| 119 |
| 120 // Initialize stdout redirection if the command line switch is present. |
| 121 void InitializeRedirection() { |
| 122 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(kFifoPath)) |
| 123 return; |
| 124 |
| 125 base::FilePath fifo_path = |
| 126 base::CommandLine::ForCurrentProcess()->GetSwitchValuePath(kFifoPath); |
| 127 base::FilePath directory = fifo_path.DirName(); |
| 128 CHECK(base::CreateDirectoryAndGetError(directory, nullptr)) |
| 129 << "Unable to create directory: " << directory.value(); |
| 130 unlink(fifo_path.value().c_str()); |
| 131 CHECK(base::android::CreateFIFO(fifo_path, 0666)) |
| 132 << "Unable to create fifo: " << fifo_path.value(); |
| 133 CHECK(base::android::RedirectStream(stdout, fifo_path, "w")) |
| 134 << "Failed to redirect stdout to file: " << fifo_path.value(); |
| 135 CHECK(dup2(STDOUT_FILENO, STDERR_FILENO) != -1) |
| 136 << "Unable to redirect stderr to stdout."; |
| 137 } |
| 138 |
| 139 } // namespace |
| 140 |
| 141 static void Init(JNIEnv* env, |
| 142 jclass clazz, |
| 143 jobject activity, |
| 144 jstring mojo_shell_path, |
| 145 jobjectArray jparameters, |
| 146 jstring j_local_apps_directory, |
| 147 jstring j_tmp_dir) { |
| 148 g_main_activiy.Get().Reset(env, activity); |
| 149 |
| 150 // Setting the TMPDIR environment variable so that applications can use it. |
| 151 // TODO(qsr) We will need our subprocesses to inherit this. |
| 152 int return_value = |
| 153 setenv("TMPDIR", |
| 154 base::android::ConvertJavaStringToUTF8(env, j_tmp_dir).c_str(), 1); |
| 155 DCHECK_EQ(return_value, 0); |
| 156 |
| 157 base::android::ScopedJavaLocalRef<jobject> scoped_activity(env, activity); |
| 158 base::android::InitApplicationContext(env, scoped_activity); |
| 159 |
| 160 std::vector<std::string> parameters; |
| 161 parameters.push_back( |
| 162 base::android::ConvertJavaStringToUTF8(env, mojo_shell_path)); |
| 163 base::android::AppendJavaStringArrayToStringVector(env, jparameters, |
| 164 ¶meters); |
| 165 base::CommandLine::Init(0, nullptr); |
| 166 base::CommandLine::ForCurrentProcess()->InitFromArgv(parameters); |
| 167 g_shell_runner.Get().reset(new MojoShellRunner(parameters)); |
| 168 |
| 169 InitializeLogging(); |
| 170 |
| 171 InitializeRedirection(); |
| 172 |
| 173 // We want ~MessageLoop to happen prior to ~Context. Initializing |
| 174 // LazyInstances is akin to stack-allocating objects; their destructors |
| 175 // will be invoked first-in-last-out. |
| 176 Context* shell_context = new Context(); |
| 177 shell_context->SetShellFileRoot(base::FilePath( |
| 178 base::android::ConvertJavaStringToUTF8(env, j_local_apps_directory))); |
| 179 g_context.Get().reset(shell_context); |
| 180 |
| 181 g_java_message_loop.Get().reset(new base::MessageLoopForUI); |
| 182 base::MessageLoopForUI::current()->Start(); |
| 183 |
| 184 // TODO(abarth): At which point should we switch to cross-platform |
| 185 // initialization? |
| 186 |
| 187 gfx::GLSurface::InitializeOneOff(); |
| 188 } |
| 189 |
| 190 static jboolean Start(JNIEnv* env, jclass clazz) { |
| 191 if (!base::CommandLine::ForCurrentProcess()->GetArgs().size()) |
| 192 return false; |
| 193 |
| 194 #if defined(MOJO_SHELL_DEBUG_URL) |
| 195 base::CommandLine::ForCurrentProcess()->AppendArg(MOJO_SHELL_DEBUG_URL); |
| 196 // Sleep for 5 seconds to give the debugger a chance to attach. |
| 197 sleep(5); |
| 198 #endif |
| 199 |
| 200 g_shell_thread.Get().reset(new base::DelegateSimpleThread( |
| 201 g_shell_runner.Get().get(), "ShellThread")); |
| 202 g_shell_thread.Get()->Start(); |
| 203 return true; |
| 204 } |
| 205 |
| 206 static void AddApplicationURL(JNIEnv* env, jclass clazz, jstring jurl) { |
| 207 base::CommandLine::ForCurrentProcess()->AppendArg( |
| 208 base::android::ConvertJavaStringToUTF8(env, jurl)); |
| 209 } |
| 210 |
| 211 bool RegisterShellMain(JNIEnv* env) { |
| 212 return RegisterNativesImpl(env); |
| 213 } |
| 214 |
| 215 } // namespace shell |
| 216 } // namespace mojo |
| 217 |
| 218 // TODO(vtl): Even though main() should never be called, mojo_shell fails to |
| 219 // link without it. Figure out if we can avoid this. |
| 220 int main(int argc, char** argv) { |
| 221 NOTREACHED(); |
| 222 } |
OLD | NEW |