Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / mojo / shell / android / mojo_main.cc
index c265632..2f8924d 100644 (file)
@@ -4,6 +4,7 @@
 
 #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"
@@ -13,9 +14,8 @@
 #include "base/macros.h"
 #include "base/message_loop/message_loop.h"
 #include "jni/MojoMain_jni.h"
-#include "mojo/public/cpp/application/application.h"
-#include "mojo/service_manager/service_loader.h"
-#include "mojo/service_manager/service_manager.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 "mojo/shell/run.h"
@@ -33,6 +33,15 @@ LazyInstance<scoped_ptr<base::MessageLoop> > g_java_message_loop =
 LazyInstance<scoped_ptr<shell::Context> > g_context =
     LAZY_INSTANCE_INITIALIZER;
 
+LazyInstance<scoped_ptr<base::android::JavaHandlerThread> > g_shell_thread =
+    LAZY_INSTANCE_INITIALIZER;
+
+void RunShell(std::vector<GURL> app_urls) {
+  g_context.Get()->Init();
+  g_context.Get()->set_ui_loop(g_java_message_loop.Get().get());
+  shell::Run(g_context.Get().get(), app_urls);
+}
+
 }  // namespace
 
 static void Init(JNIEnv* env, jclass clazz, jobject context) {
@@ -43,6 +52,11 @@ static void Init(JNIEnv* env, jclass clazz, jobject context) {
   base::CommandLine::Init(0, 0);
   mojo::shell::InitializeLogging();
 
+  // 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();
 
@@ -52,7 +66,7 @@ static void Init(JNIEnv* env, jclass clazz, jobject context) {
   gfx::GLSurface::InitializeOneOff();
 }
 
-static void Start(JNIEnv* env, jclass clazz, jobject context, jstring jurl) {
+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));
@@ -63,14 +77,11 @@ static void Start(JNIEnv* env, jclass clazz, jobject context, jstring jurl) {
     app_urls.push_back(GURL(base::android::ConvertJavaStringToUTF8(env, jurl)));
 #endif
 
-  base::android::ScopedJavaGlobalRef<jobject> activity;
-  activity.Reset(env, context);
-
-  shell::Context* shell_context = new shell::Context();
-  shell_context->set_activity(activity.obj());
-
-  g_context.Get().reset(shell_context);
-  shell::Run(shell_context, app_urls);
+  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(&RunShell, app_urls));
 }
 
 bool RegisterMojoMain(JNIEnv* env) {