| 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/context.h" | |
| 30 #include "mojo/shell/init.h" | |
| 31 #include "ui/gl/gl_surface_egl.h" | |
| 32 | |
| 33 using base::LazyInstance; | |
| 34 | |
| 35 namespace mojo { | |
| 36 namespace shell { | |
| 37 | |
| 38 namespace { | |
| 39 | |
| 40 // Tag for logging. | |
| 41 const char kLogTag[] = "chromium"; | |
| 42 | |
| 43 // Command line argument for the communication fifo. | |
| 44 const char kFifoPath[] = "fifo-path"; | |
| 45 | |
| 46 class MojoShellRunner : public base::DelegateSimpleThread::Delegate { | |
| 47 public: | |
| 48 MojoShellRunner(const std::vector<std::string>& parameters) {} | |
| 49 ~MojoShellRunner() override {} | |
| 50 | |
| 51 private: | |
| 52 void Run() override; | |
| 53 | |
| 54 DISALLOW_COPY_AND_ASSIGN(MojoShellRunner); | |
| 55 }; | |
| 56 | |
| 57 LazyInstance<scoped_ptr<base::MessageLoop>> g_java_message_loop = | |
| 58 LAZY_INSTANCE_INITIALIZER; | |
| 59 | |
| 60 LazyInstance<scoped_ptr<Context>> g_context = LAZY_INSTANCE_INITIALIZER; | |
| 61 | |
| 62 LazyInstance<scoped_ptr<MojoShellRunner>> g_shell_runner = | |
| 63 LAZY_INSTANCE_INITIALIZER; | |
| 64 | |
| 65 LazyInstance<scoped_ptr<base::DelegateSimpleThread>> g_shell_thread = | |
| 66 LAZY_INSTANCE_INITIALIZER; | |
| 67 | |
| 68 LazyInstance<base::android::ScopedJavaGlobalRef<jobject>> g_main_activiy = | |
| 69 LAZY_INSTANCE_INITIALIZER; | |
| 70 | |
| 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")); | |
| 77 | |
| 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")); | |
| 86 } | |
| 87 | |
| 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()); | |
| 93 exit(0); | |
| 94 } | |
| 95 | |
| 96 void MojoShellRunner::Run() { | |
| 97 base::MessageLoop loop(common::MessagePumpMojo::Create()); | |
| 98 Context* context = g_context.Pointer()->get(); | |
| 99 ConfigureAndroidServices(context); | |
| 100 context->Init(); | |
| 101 | |
| 102 context->Run(GURL("mojo:window_manager")); | |
| 103 loop.Run(); | |
| 104 | |
| 105 g_java_message_loop.Pointer()->get()->PostTask(FROM_HERE, | |
| 106 base::Bind(&QuitShellThread)); | |
| 107 } | |
| 108 | |
| 109 // Initialize stdout redirection if the command line switch is present. | |
| 110 void InitializeRedirection() { | |
| 111 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(kFifoPath)) | |
| 112 return; | |
| 113 | |
| 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."; | |
| 126 } | |
| 127 | |
| 128 } // namespace | |
| 129 | |
| 130 static void Init(JNIEnv* env, | |
| 131 jclass clazz, | |
| 132 jobject activity, | |
| 133 jstring mojo_shell_path, | |
| 134 jobjectArray jparameters, | |
| 135 jstring j_local_apps_directory, | |
| 136 jstring j_tmp_dir) { | |
| 137 g_main_activiy.Get().Reset(env, activity); | |
| 138 | |
| 139 // Setting the TMPDIR environment variable so that applications can use it. | |
| 140 // TODO(qsr) We will need our subprocesses to inherit this. | |
| 141 int return_value = | |
| 142 setenv("TMPDIR", | |
| 143 base::android::ConvertJavaStringToUTF8(env, j_tmp_dir).c_str(), 1); | |
| 144 DCHECK_EQ(return_value, 0); | |
| 145 | |
| 146 base::android::ScopedJavaLocalRef<jobject> scoped_activity(env, activity); | |
| 147 base::android::InitApplicationContext(env, scoped_activity); | |
| 148 | |
| 149 std::vector<std::string> parameters; | |
| 150 parameters.push_back( | |
| 151 base::android::ConvertJavaStringToUTF8(env, mojo_shell_path)); | |
| 152 base::android::AppendJavaStringArrayToStringVector(env, jparameters, | |
| 153 ¶meters); | |
| 154 base::CommandLine::Init(0, nullptr); | |
| 155 base::CommandLine::ForCurrentProcess()->InitFromArgv(parameters); | |
| 156 g_shell_runner.Get().reset(new MojoShellRunner(parameters)); | |
| 157 | |
| 158 InitializeLogging(); | |
| 159 | |
| 160 InitializeRedirection(); | |
| 161 | |
| 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); | |
| 169 | |
| 170 g_java_message_loop.Get().reset(new base::MessageLoopForUI); | |
| 171 base::MessageLoopForUI::current()->Start(); | |
| 172 | |
| 173 // TODO(abarth): At which point should we switch to cross-platform | |
| 174 // initialization? | |
| 175 | |
| 176 gfx::GLSurface::InitializeOneOff(); | |
| 177 } | |
| 178 | |
| 179 static jboolean Start(JNIEnv* env, jclass clazz) { | |
| 180 if (!base::CommandLine::ForCurrentProcess()->GetArgs().size()) | |
| 181 return false; | |
| 182 | |
| 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. | |
| 186 sleep(5); | |
| 187 #endif | |
| 188 | |
| 189 g_shell_thread.Get().reset(new base::DelegateSimpleThread( | |
| 190 g_shell_runner.Get().get(), "ShellThread")); | |
| 191 g_shell_thread.Get()->Start(); | |
| 192 return true; | |
| 193 } | |
| 194 | |
| 195 static void AddApplicationURL(JNIEnv* env, jclass clazz, jstring jurl) { | |
| 196 base::CommandLine::ForCurrentProcess()->AppendArg( | |
| 197 base::android::ConvertJavaStringToUTF8(env, jurl)); | |
| 198 } | |
| 199 | |
| 200 bool RegisterShellMain(JNIEnv* env) { | |
| 201 return RegisterNativesImpl(env); | |
| 202 } | |
| 203 | |
| 204 } // namespace shell | |
| 205 } // namespace mojo | |
| 206 | |
| 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) { | |
| 210 NOTREACHED(); | |
| 211 } | |
| OLD | NEW |