mojo_main.cc revision f2477e01787aa58f445919b809d89e252beef54f
11e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
21e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
31e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// found in the LICENSE file.
41e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "mojo/shell/android/mojo_main.h"
61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
71e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/android/jni_string.h"
81e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/at_exit.h"
91e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/bind.h"
101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/command_line.h"
111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/lazy_instance.h"
121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/logging.h"
131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/threading/thread.h"
141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "jni/MojoMain_jni.h"
15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "mojo/shell/init.h"
161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "mojo/shell/run.h"
171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/gl/gl_surface_egl.h"
181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)using base::LazyInstance;
201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace mojo {
221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace {
241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)base::AtExitManager* g_at_exit = 0;
261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)LazyInstance<scoped_ptr<base::MessageLoop> > g_java_message_loop =
280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    LAZY_INSTANCE_INITIALIZER;
290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)LazyInstance<scoped_ptr<base::Thread> > g_shell_thread =
311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    LAZY_INSTANCE_INITIALIZER;
321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)LazyInstance<scoped_ptr<shell::Context> > g_context =
341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    LAZY_INSTANCE_INITIALIZER;
351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)struct ShellInit {
370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  scoped_refptr<base::SingleThreadTaskRunner> java_runner;
380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  base::android::ScopedJavaGlobalRef<jobject> activity;
390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)};
400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void StartOnShellThread(ShellInit* init) {
420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  shell::Context* context = new shell::Context();
430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  context->set_activity(init->activity.obj());
450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  context->task_runners()->set_java_runner(init->java_runner.get());
460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  delete init;
470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  g_context.Get().reset(context);
490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  shell::Run(context);
501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}  // namspace
531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)static void Init(JNIEnv* env, jclass clazz, jobject context) {
551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context);
561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
57f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  base::android::InitApplicationContext(env, scoped_context);
581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (g_at_exit)
601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return;
611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  g_at_exit = new base::AtExitManager();
621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // TODO(abarth): Currently we leak g_at_exit.
631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  CommandLine::Init(0, 0);
65f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  mojo::shell::InitializeLogging();
661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  g_java_message_loop.Get().reset(
680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      new base::MessageLoop(base::MessageLoop::TYPE_UI));
690f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  base::MessageLoopForUI::current()->Start();
700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // TODO(abarth): At which point should we switch to cross-platform
721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // initialization?
731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  gfx::GLSurface::InitializeOneOff();
751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)static void Start(JNIEnv* env, jclass clazz, jobject context, jstring jurl) {
781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (jurl) {
791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    std::string app_url = base::android::ConvertJavaStringToUTF8(env, jurl);
801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    std::vector<std::string> argv;
811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    argv.push_back("mojo_shell");
821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    argv.push_back("--app=" + app_url);
831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    CommandLine::ForCurrentProcess()->InitFromArgv(argv);
841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  ShellInit* init = new ShellInit();
870f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  init->java_runner = base::MessageLoopForUI::current()->message_loop_proxy();
880f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  init->activity.Reset(env, context);
890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  g_shell_thread.Get().reset(new base::Thread("shell_thread"));
911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  g_shell_thread.Get()->Start();
921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  g_shell_thread.Get()->message_loop()->PostTask(FROM_HERE,
930f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)      base::Bind(StartOnShellThread, init));
941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // TODO(abarth): Currently we leak g_shell_thread.
961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)bool RegisterMojoMain(JNIEnv* env) {
991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return RegisterNativesImpl(env);
1001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}  // namespace mojo
103