Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / mojo / shell / android / mojo_main.cc
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/mojo_main.h"
6
7 #include "base/android/jni_string.h"
8 #include "base/at_exit.h"
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/message_loop/message_loop.h"
14 #include "jni/MojoMain_jni.h"
15 #include "mojo/public/shell/application.h"
16 #include "mojo/services/native_viewport/native_viewport_service.h"
17 #include "mojo/shell/context.h"
18 #include "mojo/shell/init.h"
19 #include "mojo/shell/run.h"
20 #include "mojo/shell/service_manager.h"
21 #include "ui/gl/gl_surface_egl.h"
22
23 using base::LazyInstance;
24
25 namespace mojo {
26
27 namespace {
28
29 LazyInstance<scoped_ptr<base::MessageLoop> > g_java_message_loop =
30     LAZY_INSTANCE_INITIALIZER;
31
32 LazyInstance<scoped_ptr<shell::Context> > g_context =
33     LAZY_INSTANCE_INITIALIZER;
34
35 class NativeViewportServiceLoader : public shell::ServiceManager::Loader {
36  public:
37   NativeViewportServiceLoader() {}
38   virtual ~NativeViewportServiceLoader() {}
39
40  private:
41   virtual void Load(const GURL& url,
42                     ScopedShellHandle service_handle)
43       MOJO_OVERRIDE {
44     app_.reset(CreateNativeViewportService(g_context.Get().get(),
45                                            service_handle.Pass()));
46   }
47   scoped_ptr<Application> app_;
48 };
49
50 LazyInstance<scoped_ptr<NativeViewportServiceLoader> >
51     g_viewport_service_loader = LAZY_INSTANCE_INITIALIZER;
52
53 }  // namspace
54
55 static void Init(JNIEnv* env, jclass clazz, jobject context) {
56   base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context);
57
58   base::android::InitApplicationContext(env, scoped_context);
59
60   CommandLine::Init(0, 0);
61   mojo::shell::InitializeLogging();
62
63   g_java_message_loop.Get().reset(new base::MessageLoopForUI);
64   base::MessageLoopForUI::current()->Start();
65
66   // TODO(abarth): At which point should we switch to cross-platform
67   // initialization?
68
69   gfx::GLSurface::InitializeOneOff();
70 }
71
72 static void Start(JNIEnv* env, jclass clazz, jobject context, jstring jurl) {
73   std::string app_url;
74 #if defined(MOJO_SHELL_DEBUG_URL)
75   app_url = MOJO_SHELL_DEBUG_URL;
76   // Sleep for 5 seconds to give the debugger a chance to attach.
77   sleep(5);
78 #else
79   if (jurl)
80     app_url = base::android::ConvertJavaStringToUTF8(env, jurl);
81 #endif
82   if (!app_url.empty()) {
83     std::vector<std::string> argv;
84     argv.push_back("mojo_shell");
85     argv.push_back(app_url);
86     CommandLine::ForCurrentProcess()->InitFromArgv(argv);
87   }
88
89   base::android::ScopedJavaGlobalRef<jobject> activity;
90   activity.Reset(env, context);
91
92   shell::Context* shell_context = new shell::Context();
93   shell_context->set_activity(activity.obj());
94   g_viewport_service_loader.Get().reset(new NativeViewportServiceLoader());
95   shell_context->service_manager()->SetLoaderForURL(
96       g_viewport_service_loader.Get().get(),
97       GURL("mojo:mojo_native_viewport_service"));
98
99   g_context.Get().reset(shell_context);
100   shell::Run(shell_context);
101 }
102
103 bool RegisterMojoMain(JNIEnv* env) {
104   return RegisterNativesImpl(env);
105 }
106
107 }  // namespace mojo