blob: 1f02bbd0be74c45a83a5462ad7971b7de9fae776 [file] [log] [blame]
[email protected]9fc44162012-01-23 22:56:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]61c86c62011-08-02 16:11:162// 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
avib30f2402015-12-24 03:43:287#include <stddef.h>
erikchen011ad3f2018-01-26 17:54:558#include <sys/prctl.h>
avib30f2402015-12-24 03:43:289
[email protected]96e7ade2011-12-05 14:42:0810#include <map>
11
Joshua Peraza7814da22018-07-10 21:37:5012#include "base/android/java_exception_reporter.h"
[email protected]1b46a532012-05-23 05:59:4913#include "base/android/jni_string.h"
Clark DuVallc131c502020-11-26 16:23:4914#include "base/android/jni_utils.h"
15#include "base/containers/flat_map.h"
Scott Violet44165792018-02-22 02:08:0816#include "base/debug/debugging_buildflags.h"
[email protected]96e7ade2011-12-05 14:42:0817#include "base/lazy_instance.h"
[email protected]61c86c62011-08-02 16:11:1618#include "base/logging.h"
Clark DuVallc131c502020-11-26 16:23:4919#include "base/no_destructor.h"
20#include "base/synchronization/lock.h"
dskibaa8c951e2016-10-25 23:39:1121#include "base/threading/thread_local.h"
[email protected]61c86c62011-08-02 16:11:1622
Clark DuVallc131c502020-11-26 16:23:4923namespace base {
24namespace android {
[email protected]61c86c62011-08-02 16:11:1625namespace {
[email protected]fe0f1ab2012-02-09 21:02:2726
[email protected]995a3ff2012-09-17 06:15:1027JavaVM* g_jvm = NULL;
Clark DuVallc131c502020-11-26 16:23:4928base::LazyInstance<ScopedJavaGlobalRef<jobject>>::Leaky g_class_loader =
29 LAZY_INSTANCE_INITIALIZER;
[email protected]d30dd6e2014-08-21 16:37:3230jmethodID g_class_loader_load_class_method_id = 0;
[email protected]96e7ade2011-12-05 14:42:0831
erikchenf7c8a0d2017-04-06 21:15:2732#if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS)
dskibaa8c951e2016-10-25 23:39:1133base::LazyInstance<base::ThreadLocalPointer<void>>::Leaky
34 g_stack_frame_pointer = LAZY_INSTANCE_INITIALIZER;
35#endif
36
Xianzhu Wangab2f3662018-02-09 22:02:2637bool g_fatal_exception_occurred = false;
38
Clark DuVallc131c502020-11-26 16:23:4939// Returns a ClassLoader instance which will be able to load classes from the
40// specified split.
41jobject 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]61c86c62011-08-02 16:11:1647
Clark DuVallc131c502020-11-26 16:23:4948 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;