[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 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 "base/android/jni_android.h" | ||||
6 | |||||
avi | b30f240 | 2015-12-24 03:43:28 | [diff] [blame] | 7 | #include <stddef.h> |
erikchen | 011ad3f | 2018-01-26 17:54:55 | [diff] [blame] | 8 | #include <sys/prctl.h> |
avi | b30f240 | 2015-12-24 03:43:28 | [diff] [blame] | 9 | |
[email protected] | 96e7ade | 2011-12-05 14:42:08 | [diff] [blame] | 10 | #include <map> |
11 | |||||
Joshua Peraza | 7814da2 | 2018-07-10 21:37:50 | [diff] [blame] | 12 | #include "base/android/java_exception_reporter.h" |
[email protected] | 1b46a53 | 2012-05-23 05:59:49 | [diff] [blame] | 13 | #include "base/android/jni_string.h" |
Clark DuVall | c131c50 | 2020-11-26 16:23:49 | [diff] [blame^] | 14 | #include "base/android/jni_utils.h" |
15 | #include "base/containers/flat_map.h" | ||||
Scott Violet | 4416579 | 2018-02-22 02:08:08 | [diff] [blame] | 16 | #include "base/debug/debugging_buildflags.h" |
[email protected] | 96e7ade | 2011-12-05 14:42:08 | [diff] [blame] | 17 | #include "base/lazy_instance.h" |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 18 | #include "base/logging.h" |
Clark DuVall | c131c50 | 2020-11-26 16:23:49 | [diff] [blame^] | 19 | #include "base/no_destructor.h" |
20 | #include "base/synchronization/lock.h" | ||||
dskiba | a8c951e | 2016-10-25 23:39:11 | [diff] [blame] | 21 | #include "base/threading/thread_local.h" |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 22 | |
Clark DuVall | c131c50 | 2020-11-26 16:23:49 | [diff] [blame^] | 23 | namespace base { |
24 | namespace android { | ||||
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 25 | namespace { |
[email protected] | fe0f1ab | 2012-02-09 21:02:27 | [diff] [blame] | 26 | |
[email protected] | 995a3ff | 2012-09-17 06:15:10 | [diff] [blame] | 27 | JavaVM* g_jvm = NULL; |
Clark DuVall | c131c50 | 2020-11-26 16:23:49 | [diff] [blame^] | 28 | base::LazyInstance<ScopedJavaGlobalRef<jobject>>::Leaky g_class_loader = |
29 | LAZY_INSTANCE_INITIALIZER; | ||||
[email protected] | d30dd6e | 2014-08-21 16:37:32 | [diff] [blame] | 30 | jmethodID g_class_loader_load_class_method_id = 0; |
[email protected] | 96e7ade | 2011-12-05 14:42:08 | [diff] [blame] | 31 | |
erikchen | f7c8a0d | 2017-04-06 21:15:27 | [diff] [blame] | 32 | #if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
dskiba | a8c951e | 2016-10-25 23:39:11 | [diff] [blame] | 33 | base::LazyInstance<base::ThreadLocalPointer<void>>::Leaky |
34 | g_stack_frame_pointer = LAZY_INSTANCE_INITIALIZER; | ||||
35 | #endif | ||||
36 | |||||
Xianzhu Wang | ab2f366 | 2018-02-09 22:02:26 | [diff] [blame] | 37 | bool g_fatal_exception_occurred = false; |
38 | |||||
Clark DuVall | c131c50 | 2020-11-26 16:23:49 | [diff] [blame^] | 39 | // Returns a ClassLoader instance which will be able to load classes from the |
40 | // specified split. | ||||
41 | jobject GetCachedClassLoader(JNIEnv* env, const std::string& split_name) { | ||||
42 | DCHECK(!split_name.empty()); | ||||
43 | static base::NoDestructor<base::Lock> lock; | ||||
44 | static base::NoDestructor< | ||||
45 | base::flat_map<std::string, ScopedJavaGlobalRef<jobject>>> | ||||
46 | split_class_loader_map; | ||||
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 47 | |
Clark DuVall | c131c50 | 2020-11-26 16:23:49 | [diff] [blame^] | 48 | base::AutoLock guard(*lock); |
49 | auto it = split_class_loader_map->find(split_name); | ||||
50 | if (it != split_class_loader_map->end()) { | ||||
51 | return it->second.obj(); | ||||
52 | } | ||||
53 | |||||
54 | ScopedJavaGlobalRef<jobject> class_loader( | ||||
55 | GetSplitClassLoader(env, split_name)); | ||||
56 | jobject class_loader_obj = class_loader.obj(); | ||||
57 | split_class_loader_map->insert({split_name, std::move(class_loader)}); | ||||
58 | return class_loader_obj; | ||||