Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: base/android/jni_android.cc

Issue 1407233017: Define a Java-side global application context. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Undo changes to ApplicationStatus Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/android/jni_android.h" 5 #include "base/android/jni_android.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/android/build_info.h" 9 #include "base/android/build_info.h"
10 #include "base/android/jni_string.h" 10 #include "base/android/jni_string.h"
11 #include "base/android/jni_utils.h" 11 #include "base/android/jni_utils.h"
12 #include "base/lazy_instance.h" 12 #include "base/lazy_instance.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 14
15 namespace { 15 namespace {
16 using base::android::GetClass; 16 using base::android::GetClass;
17 using base::android::MethodID; 17 using base::android::MethodID;
18 using base::android::ScopedJavaLocalRef; 18 using base::android::ScopedJavaLocalRef;
19 19
20 bool g_disable_manual_jni_registration = false; 20 bool g_disable_manual_jni_registration = false;
21 21
22 JavaVM* g_jvm = NULL; 22 JavaVM* g_jvm = NULL;
23 // Leak the global app context, as it is used from a non-joinable worker thread
24 // that may still be running at shutdown. There is no harm in doing this.
25 base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> >::Leaky
26 g_application_context = LAZY_INSTANCE_INITIALIZER;
27 base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> >::Leaky 23 base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> >::Leaky
28 g_class_loader = LAZY_INSTANCE_INITIALIZER; 24 g_class_loader = LAZY_INSTANCE_INITIALIZER;
29 jmethodID g_class_loader_load_class_method_id = 0; 25 jmethodID g_class_loader_load_class_method_id = 0;
30 26
31 } // namespace 27 } // namespace
32 28
33 namespace base { 29 namespace base {
34 namespace android { 30 namespace android {
35 31
36 bool IsManualJniRegistrationDisabled() { 32 bool IsManualJniRegistrationDisabled() {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 67
72 void InitVM(JavaVM* vm) { 68 void InitVM(JavaVM* vm) {
73 DCHECK(!g_jvm || g_jvm == vm); 69 DCHECK(!g_jvm || g_jvm == vm);
74 g_jvm = vm; 70 g_jvm = vm;
75 } 71 }
76 72
77 bool IsVMInitialized() { 73 bool IsVMInitialized() {
78 return g_jvm != NULL; 74 return g_jvm != NULL;
79 } 75 }
80 76
81 void InitApplicationContext(JNIEnv* env, const JavaRef<jobject>& context) {
82 if (env->IsSameObject(g_application_context.Get().obj(), context.obj())) {
83 // It's safe to set the context more than once if it's the same context.
84 return;
85 }
86 DCHECK(g_application_context.Get().is_null());
87 g_application_context.Get().Reset(context);
88 }
89
90 void InitReplacementClassLoader(JNIEnv* env, 77 void InitReplacementClassLoader(JNIEnv* env,
91 const JavaRef<jobject>& class_loader) { 78 const JavaRef<jobject>& class_loader) {
92 DCHECK(g_class_loader.Get().is_null()); 79 DCHECK(g_class_loader.Get().is_null());
93 DCHECK(!class_loader.is_null()); 80 DCHECK(!class_loader.is_null());
94 81
95 ScopedJavaLocalRef<jclass> class_loader_clazz = 82 ScopedJavaLocalRef<jclass> class_loader_clazz =
96 GetClass(env, "java/lang/ClassLoader"); 83 GetClass(env, "java/lang/ClassLoader");
97 CHECK(!ClearException(env)); 84 CHECK(!ClearException(env));
98 g_class_loader_load_class_method_id = 85 g_class_loader_load_class_method_id =
99 env->GetMethodID(class_loader_clazz.obj(), 86 env->GetMethodID(class_loader_clazz.obj(),
100 "loadClass", 87 "loadClass",
101 "(Ljava/lang/String;)Ljava/lang/Class;"); 88 "(Ljava/lang/String;)Ljava/lang/Class;");
102 CHECK(!ClearException(env)); 89 CHECK(!ClearException(env));
103 90
104 DCHECK(env->IsInstanceOf(class_loader.obj(), class_loader_clazz.obj())); 91 DCHECK(env->IsInstanceOf(class_loader.obj(), class_loader_clazz.obj()));
105 g_class_loader.Get().Reset(class_loader); 92 g_class_loader.Get().Reset(class_loader);
106 } 93 }
107 94
108 const jobject GetApplicationContext() {
109 DCHECK(!g_application_context.Get().is_null());
110 return g_application_context.Get().obj();
111 }
112
113 ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, const char* class_name) { 95 ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, const char* class_name) {
114 jclass clazz; 96 jclass clazz;
115 if (!g_class_loader.Get().is_null()) { 97 if (!g_class_loader.Get().is_null()) {
116 // ClassLoader.loadClass expects a classname with components separated by 98 // ClassLoader.loadClass expects a classname with components separated by
117 // dots instead of the slashes that JNIEnv::FindClass expects. The JNI 99 // dots instead of the slashes that JNIEnv::FindClass expects. The JNI
118 // generator generates names with slashes, so we have to replace them here. 100 // generator generates names with slashes, so we have to replace them here.
119 // TODO(torne): move to an approach where we always use ClassLoader except 101 // TODO(torne): move to an approach where we always use ClassLoader except
120 // for the special case of base::android::GetClassLoader(), and change the 102 // for the special case of base::android::GetClassLoader(), and change the
121 // JNI generator to generate dot-separated names. http://crbug.com/461773 103 // JNI generator to generate dot-separated names. http://crbug.com/461773
122 size_t bufsize = strlen(class_name) + 1; 104 size_t bufsize = strlen(class_name) + 1;
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 env, static_cast<jstring>( 273 env, static_cast<jstring>(
292 env->CallObjectMethod(bytearray_output_stream.obj(), 274 env->CallObjectMethod(bytearray_output_stream.obj(),
293 bytearray_output_stream_tostring))); 275 bytearray_output_stream_tostring)));
294 276
295 return ConvertJavaStringToUTF8(exception_string); 277 return ConvertJavaStringToUTF8(exception_string);
296 } 278 }
297 279
298 280
299 } // namespace android 281 } // namespace android
300 } // namespace base 282 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698