Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / mojo / shell / android / mojo_main.cc
index 214c553..832e0aa 100644 (file)
@@ -4,15 +4,20 @@
 
 #include "mojo/shell/android/mojo_main.h"
 
+#include "base/android/java_handler_thread.h"
 #include "base/android/jni_string.h"
 #include "base/at_exit.h"
 #include "base/bind.h"
 #include "base/command_line.h"
 #include "base/lazy_instance.h"
 #include "base/logging.h"
-#include "base/threading/thread.h"
+#include "base/macros.h"
+#include "base/message_loop/message_loop.h"
 #include "jni/MojoMain_jni.h"
-#include "mojo/shell/run.h"
+#include "mojo/application_manager/application_loader.h"
+#include "mojo/application_manager/application_manager.h"
+#include "mojo/shell/context.h"
+#include "mojo/shell/init.h"
 #include "ui/gl/gl_surface_egl.h"
 
 using base::LazyInstance;
@@ -21,63 +26,41 @@ namespace mojo {
 
 namespace {
 
-base::AtExitManager* g_at_exit = 0;
-
 LazyInstance<scoped_ptr<base::MessageLoop> > g_java_message_loop =
     LAZY_INSTANCE_INITIALIZER;
 
-LazyInstance<scoped_ptr<base::Thread> > g_shell_thread =
-    LAZY_INSTANCE_INITIALIZER;
-
 LazyInstance<scoped_ptr<shell::Context> > g_context =
     LAZY_INSTANCE_INITIALIZER;
 
-void InitializeLogging() {
-  logging::LoggingSettings settings;
-  settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
-  settings.dcheck_state =
-      logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS;
-  logging::InitLogging(settings);
-  // To view log output with IDs and timestamps use "adb logcat -v threadtime".
-  logging::SetLogItems(false,    // Process ID
-                       false,    // Thread ID
-                       false,    // Timestamp
-                       false);   // Tick count
-}
-
-struct ShellInit {
-  scoped_refptr<base::SingleThreadTaskRunner> java_runner;
-  base::android::ScopedJavaGlobalRef<jobject> activity;
-};
-
-void StartOnShellThread(ShellInit* init) {
-  shell::Context* context = new shell::Context();
-
-  context->set_activity(init->activity.obj());
-  context->task_runners()->set_java_runner(init->java_runner.get());
-  delete init;
+LazyInstance<scoped_ptr<base::android::JavaHandlerThread> > g_shell_thread =
+    LAZY_INSTANCE_INITIALIZER;
 
-  g_context.Get().reset(context);
-  shell::Run(context);
+void RunShell(std::vector<GURL> app_urls) {
+  shell::Context* context = g_context.Pointer()->get();
+  context->Init();
+  context->set_ui_loop(g_java_message_loop.Get().get());
+  for (std::vector<GURL>::const_iterator it = app_urls.begin();
+       it != app_urls.end(); ++it) {
+    context->Run(*it);
+  }
 }
 
-}  // namspace
+}  // namespace
 
 static void Init(JNIEnv* env, jclass clazz, jobject context) {
   base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context);
 
-  base::android::InitApplicationContext(scoped_context);
+  base::android::InitApplicationContext(env, scoped_context);
 
-  if (g_at_exit)
-    return;
-  g_at_exit = new base::AtExitManager();
-  // TODO(abarth): Currently we leak g_at_exit.
+  base::CommandLine::Init(0, 0);
+  mojo::shell::InitializeLogging();
 
-  CommandLine::Init(0, 0);
-  InitializeLogging();
-
-  g_java_message_loop.Get().reset(
-      new base::MessageLoop(base::MessageLoop::TYPE_UI));
+  // We want ~MessageLoop to happen prior to ~Context. Initializing
+  // LazyInstances is akin to stack-allocating objects; their destructors
+  // will be invoked first-in-last-out.
+  shell::Context* shell_context = new shell::Context();
+  g_context.Get().reset(shell_context);
+  g_java_message_loop.Get().reset(new base::MessageLoopForUI);
   base::MessageLoopForUI::current()->Start();
 
   // TODO(abarth): At which point should we switch to cross-platform
@@ -86,25 +69,22 @@ static void Init(JNIEnv* env, jclass clazz, jobject context) {
   gfx::GLSurface::InitializeOneOff();
 }
 
-static void Start(JNIEnv* env, jclass clazz, jobject context, jstring jurl) {
-  if (jurl) {
-    std::string app_url = base::android::ConvertJavaStringToUTF8(env, jurl);
-    std::vector<std::string> argv;
-    argv.push_back("mojo_shell");
-    argv.push_back("--app=" + app_url);
-    CommandLine::ForCurrentProcess()->InitFromArgv(argv);
-  }
-
-  ShellInit* init = new ShellInit();
-  init->java_runner = base::MessageLoopForUI::current()->message_loop_proxy();
-  init->activity.Reset(env, context);
-
-  g_shell_thread.Get().reset(new base::Thread("shell_thread"));
+static void Start(JNIEnv* env, jclass clazz, jstring jurl) {
+  std::vector<GURL> app_urls;
+#if defined(MOJO_SHELL_DEBUG_URL)
+  app_urls.push_back(GURL(MOJO_SHELL_DEBUG_URL));
+  // Sleep for 5 seconds to give the debugger a chance to attach.
+  sleep(5);
+#else
+  if (jurl)
+    app_urls.push_back(GURL(base::android::ConvertJavaStringToUTF8(env, jurl)));
+#endif
+
+  g_shell_thread.Get().reset(
+      new base::android::JavaHandlerThread("shell_thread"));
   g_shell_thread.Get()->Start();
-  g_shell_thread.Get()->message_loop()->PostTask(FROM_HERE,
-      base::Bind(StartOnShellThread, init));
-
-  // TODO(abarth): Currently we leak g_shell_thread.
+  g_shell_thread.Get()->message_loop()->PostTask(
+      FROM_HERE, base::Bind(&RunShell, app_urls));
 }
 
 bool RegisterMojoMain(JNIEnv* env) {