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

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

Issue 10580038: Allow OpenSSLPrivateKeyStore and Android global app context leaking. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 3 months 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | net/base/openssl_private_key_store_android.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/atomicops.h" 11 #include "base/atomicops.h"
12 #include "base/lazy_instance.h" 12 #include "base/lazy_instance.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/threading/platform_thread.h" 14 #include "base/threading/platform_thread.h"
15 15
16 namespace { 16 namespace {
17 using base::android::GetClass; 17 using base::android::GetClass;
18 using base::android::GetMethodID; 18 using base::android::GetMethodID;
19 using base::android::ScopedJavaLocalRef; 19 using base::android::ScopedJavaLocalRef;
20 20
21 JavaVM* g_jvm = NULL;
22
23 base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> >
24 g_application_context = LAZY_INSTANCE_INITIALIZER;
25
26 struct MethodIdentifier { 21 struct MethodIdentifier {
27 const char* class_name; 22 const char* class_name;
28 const char* method; 23 const char* method;
29 const char* jni_signature; 24 const char* jni_signature;
30 25
31 bool operator<(const MethodIdentifier& other) const { 26 bool operator<(const MethodIdentifier& other) const {
32 int r = strcmp(class_name, other.class_name); 27 int r = strcmp(class_name, other.class_name);
33 if (r < 0) { 28 if (r < 0) {
34 return true; 29 return true;
35 } else if (r > 0) { 30 } else if (r > 0) {
36 return false; 31 return false;
37 } 32 }
38 33
39 r = strcmp(method, other.method); 34 r = strcmp(method, other.method);
40 if (r < 0) { 35 if (r < 0) {
41 return true; 36 return true;
42 } else if (r > 0) { 37 } else if (r > 0) {
43 return false; 38 return false;
44 } 39 }
45 40
46 return strcmp(jni_signature, other.jni_signature) < 0; 41 return strcmp(jni_signature, other.jni_signature) < 0;
47 } 42 }
48 }; 43 };
49 44
50 typedef std::map<MethodIdentifier, jmethodID> MethodIDMap; 45 typedef std::map<MethodIdentifier, jmethodID> MethodIDMap;
51 base::LazyInstance<MethodIDMap>::Leaky 46
52 g_method_id_map = LAZY_INSTANCE_INITIALIZER;
53 const base::subtle::AtomicWord kUnlocked = 0; 47 const base::subtle::AtomicWord kUnlocked = 0;
54 const base::subtle::AtomicWord kLocked = 1; 48 const base::subtle::AtomicWord kLocked = 1;
55 base::subtle::AtomicWord g_method_id_map_lock = kUnlocked; 49 base::subtle::AtomicWord g_method_id_map_lock = kUnlocked;
50 JavaVM* g_jvm = NULL;
51 // Leak the global app context, as it is used from a non-joinable worker thread
52 // that may still be running at shutdown. There is no harm in doing this.
53 base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> >::Leaky
54 g_application_context = LAZY_INSTANCE_INITIALIZER;
55 base::LazyInstance<MethodIDMap>::Leaky
56 g_method_id_map = LAZY_INSTANCE_INITIALIZER;
56 57
57 std::string GetJavaExceptionInfo(JNIEnv* env, jthrowable java_throwable) { 58 std::string GetJavaExceptionInfo(JNIEnv* env, jthrowable java_throwable) {
58 ScopedJavaLocalRef<jclass> throwable_clazz = 59 ScopedJavaLocalRef<jclass> throwable_clazz =
59 GetClass(env, "java/lang/Throwable"); 60 GetClass(env, "java/lang/Throwable");
60 jmethodID throwable_printstacktrace = 61 jmethodID throwable_printstacktrace =
61 GetMethodID(env, throwable_clazz, "printStackTrace", 62 GetMethodID(env, throwable_clazz, "printStackTrace",
62 "(Ljava/io/PrintStream;)V"); 63 "(Ljava/io/PrintStream;)V");
63 64
64 // Create an instance of ByteArrayOutputStream. 65 // Create an instance of ByteArrayOutputStream.
65 ScopedJavaLocalRef<jclass> bytearray_output_stream_clazz = 66 ScopedJavaLocalRef<jclass> bytearray_output_stream_clazz =
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 } 150 }
150 bool error = ClearException(env); 151 bool error = ClearException(env);
151 DCHECK(!error); 152 DCHECK(!error);
152 return true; 153 return true;
153 } 154 }
154 155
155 jmethodID GetMethodID(JNIEnv* env, 156 jmethodID GetMethodID(JNIEnv* env,
156 const JavaRef<jclass>& clazz, 157 const JavaRef<jclass>& clazz,
157 const char* method_name, 158 const char* method_name,
158 const char* jni_signature) { 159 const char* jni_signature) {
159 // We can't use clazz.env() as that may be from a different thread. 160 // clazz.env() can not be used as that may be from a different thread.
160 return GetMethodID(env, clazz.obj(), method_name, jni_signature); 161 return GetMethodID(env, clazz.obj(), method_name, jni_signature);
161 } 162 }
162 163
163 jmethodID GetMethodID(JNIEnv* env, 164 jmethodID GetMethodID(JNIEnv* env,
164 jclass clazz, 165 jclass clazz,
165 const char* method_name, 166 const char* method_name,
166 const char* jni_signature) { 167 const char* jni_signature) {
167 jmethodID method_id = 168 jmethodID method_id =
168 env->GetMethodID(clazz, method_name, jni_signature); 169 env->GetMethodID(clazz, method_name, jni_signature);
169 CHECK(method_id && !ClearException(env)) << "Failed to find method " << 170 CHECK(method_id && !ClearException(env)) << "Failed to find method " <<
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 if (!HasException(env)) 298 if (!HasException(env))
298 return false; 299 return false;
299 env->ExceptionDescribe(); 300 env->ExceptionDescribe();
300 env->ExceptionClear(); 301 env->ExceptionClear();
301 return true; 302 return true;
302 } 303 }
303 304
304 void CheckException(JNIEnv* env) { 305 void CheckException(JNIEnv* env) {
305 if (!HasException(env)) return; 306 if (!HasException(env)) return;
306 307
307 // Ugh, we are going to die, might as well tell breakpad about it. 308 // Exception has been found, might as well tell breakpad about it.
308 jthrowable java_throwable = env->ExceptionOccurred(); 309 jthrowable java_throwable = env->ExceptionOccurred();
309 if (!java_throwable) { 310 if (!java_throwable) {
310 // Nothing we can do. 311 // Do nothing but return false.
311 CHECK(false); 312 CHECK(false);
312 } 313 }
313 314
314 // Clear the pending exception, we do have a reference to it. 315 // Clear the pending exception, since a local reference is now held.
315 env->ExceptionClear(); 316 env->ExceptionClear();
316 317
317 // Set the exception_string in BuildInfo so that breakpad can read it. 318 // Set the exception_string in BuildInfo so that breakpad can read it.
318 // RVO should avoid any extra copies of the exception string. 319 // RVO should avoid any extra copies of the exception string.
319 base::android::BuildInfo::GetInstance()->set_java_exception_info( 320 base::android::BuildInfo::GetInstance()->set_java_exception_info(
320 GetJavaExceptionInfo(env, java_throwable)); 321 GetJavaExceptionInfo(env, java_throwable));
321 322
322 // Now, feel good about it and die. 323 // Now, feel good about it and die.
323 CHECK(false); 324 CHECK(false);
324 } 325 }
325 326
326 } // namespace android 327 } // namespace android
327 } // namespace base 328 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | net/base/openssl_private_key_store_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698