Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / blimp / client / android / blimp_library_loader.cc
blob495d9d18e88b9d6dc583d78896ca547d4601f218
1 // Copyright 2015 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 "blimp/client/android/blimp_library_loader.h"
7 #include <vector>
9 #include "base/android/base_jni_onload.h"
10 #include "base/android/base_jni_registrar.h"
11 #include "base/android/jni_android.h"
12 #include "base/android/library_loader/library_loader_hooks.h"
13 #include "base/bind.h"
14 #include "base/lazy_instance.h"
15 #include "base/logging.h"
16 #include "base/message_loop/message_loop.h"
17 #include "blimp/client/android/blimp_jni_registrar.h"
18 #include "jni/BlimpLibraryLoader_jni.h"
19 #include "ui/gl/gl_surface.h"
21 namespace {
23 base::LazyInstance<scoped_ptr<base::MessageLoopForUI>> g_main_message_loop =
24 LAZY_INSTANCE_INITIALIZER;
26 bool OnLibrariesLoaded(JNIEnv* env, jclass clazz) {
27 logging::LoggingSettings settings;
28 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
29 logging::InitLogging(settings);
31 // Disable process info prefixes on log lines. These can be obtained via "adb
32 // logcat -v threadtime".
33 logging::SetLogItems(false, // Process ID
34 false, // Thread ID
35 false, // Timestamp
36 false); // Tick count
37 VLOG(0) << "Chromium logging enabled: level = " << logging::GetMinLogLevel()
38 << ", default verbosity = " << logging::GetVlogVerbosity();
40 return true;
43 bool OnJniInitializationComplete() {
44 base::android::SetLibraryLoadedHook(&OnLibrariesLoaded);
45 return true;
48 bool RegisterJni(JNIEnv* env) {
49 if (!base::android::RegisterJni(env))
50 return false;
52 if (!blimp::RegisterBlimpJni(env))
53 return false;
55 return true;
58 } // namespace
60 namespace blimp {
62 static jboolean InitializeBlimp(JNIEnv* env,
63 const JavaParamRef<jclass>& clazz,
64 const JavaParamRef<jobject>& jcontext) {
65 base::android::InitApplicationContext(env, jcontext);
67 // TODO(dtrainor): Start the runner?
68 return true;
71 static jboolean StartBlimp(JNIEnv* env, const JavaParamRef<jclass>& clazz) {
72 // TODO(dtrainor): Initialize ICU?
74 if (!gfx::GLSurface::InitializeOneOff())
75 return false;
77 g_main_message_loop.Get().reset(new base::MessageLoopForUI);
78 base::MessageLoopForUI::current()->Start();
80 return true;
83 bool RegisterBlimpLibraryLoaderJni(JNIEnv* env) {
84 return RegisterNativesImpl(env);
87 } // namespace blimp
89 JNI_EXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
90 std::vector<base::android::RegisterCallback> register_callbacks;
91 register_callbacks.push_back(base::Bind(&RegisterJni));
93 std::vector<base::android::InitCallback> init_callbacks;
94 init_callbacks.push_back(base::Bind(&OnJniInitializationComplete));
96 // Although we only need to register JNI for base/ and blimp/, this follows
97 // the general Chrome for Android pattern, to be future-proof against future
98 // changes to JNI.
99 if (!base::android::OnJNIOnLoadRegisterJNI(vm, register_callbacks) ||
100 !base::android::OnJNIOnLoadInit(init_callbacks)) {
101 return -1;
104 return JNI_VERSION_1_4;