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.
5 #include "mojo/shell/android/mojo_main.h"
7 #include "base/android/jni_string.h"
8 #include "base/at_exit.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"
23 using base::LazyInstance;
29 LazyInstance<scoped_ptr<base::MessageLoop> > g_java_message_loop =
30 LAZY_INSTANCE_INITIALIZER;
32 LazyInstance<scoped_ptr<shell::Context> > g_context =
33 LAZY_INSTANCE_INITIALIZER;
35 class NativeViewportServiceLoader : public shell::ServiceManager::Loader {
37 NativeViewportServiceLoader() {}
38 virtual ~NativeViewportServiceLoader() {}
41 virtual void Load(const GURL& url,
42 ScopedShellHandle service_handle)
44 app_.reset(CreateNativeViewportService(g_context.Get().get(),
45 service_handle.Pass()));
47 scoped_ptr<Application> app_;
50 LazyInstance<scoped_ptr<NativeViewportServiceLoader> >
51 g_viewport_service_loader = LAZY_INSTANCE_INITIALIZER;
55 static void Init(JNIEnv* env, jclass clazz, jobject context) {
56 base::android::ScopedJavaLocalRef<jobject> scoped_context(env, context);
58 base::android::InitApplicationContext(env, scoped_context);
60 CommandLine::Init(0, 0);
61 mojo::shell::InitializeLogging();
63 g_java_message_loop.Get().reset(new base::MessageLoopForUI);
64 base::MessageLoopForUI::current()->Start();
66 // TODO(abarth): At which point should we switch to cross-platform
69 gfx::GLSurface::InitializeOneOff();
72 static void Start(JNIEnv* env, jclass clazz, jobject context, jstring jurl) {
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.
80 app_url = base::android::ConvertJavaStringToUTF8(env, jurl);
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);
89 base::android::ScopedJavaGlobalRef<jobject> activity;
90 activity.Reset(env, context);
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"));
99 g_context.Get().reset(shell_context);
100 shell::Run(shell_context);
103 bool RegisterMojoMain(JNIEnv* env) {
104 return RegisterNativesImpl(env);