blob: 58beec5c14d5663148e6fee6d0d97b9c477da2b2 [file] [log] [blame]
Brian Carlstromb2acfa22013-06-19 15:09:28 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef JNI_INVOCATION_H_included
18#define JNI_INVOCATION_H_included
19
20#include <jni.h>
21
22// JniInvocation adds a layer of indirection for applications using
23// the JNI invocation API to allow the JNI implementation to be
24// selected dynamically. Apps can specify a specific implementation to
25// be used by calling InitJniInvocation. If this is not done, the
26// library will chosen based on the value of Android system property
Brian Carlstromdf034942013-06-30 13:12:55 -070027// persist.sys.dalvik.vm.lib on the device, and otherwise fall back to
28// a hard-coded default implementation.
Brian Carlstromb2acfa22013-06-19 15:09:28 -070029class JniInvocation {
30 public:
31 JniInvocation();
32
33 ~JniInvocation();
34
35 // Initialize JNI invocation API. library should specifiy a valid
36 // shared library for opening via dlopen providing a JNI invocation
Brian Carlstromdf034942013-06-30 13:12:55 -070037 // implementation, or null to allow defaulting via
38 // persist.sys.dalvik.vm.lib.
Brian Carlstromb2acfa22013-06-19 15:09:28 -070039 bool Init(const char* library);
40
Ningsheng Jian88b84ec2014-09-17 13:34:09 +080041 // Exposes which library is actually loaded from the given name. The
42 // buffer of size PROPERTY_VALUE_MAX will be used to load the system
43 // property for the default library, if necessary. If no buffer is
44 // provided, the fallback value will be used.
45 static const char* GetLibrary(const char* library, char* buffer);
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070046
Brian Carlstromb2acfa22013-06-19 15:09:28 -070047 private:
Tom Cherry3c461712017-12-14 00:00:15 -080048 static const char* GetLibrary(const char* library, char* buffer, bool (*is_debuggable)(),
49 int (*get_library_system_property)(char* buffer));
Brian Carlstromb2acfa22013-06-19 15:09:28 -070050
51 bool FindSymbol(void** pointer, const char* symbol);
52
53 static JniInvocation& GetJniInvocation();
54
55 jint JNI_GetDefaultJavaVMInitArgs(void* vmargs);
56 jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args);
57 jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize size, jsize* vm_count);
58
59 static JniInvocation* jni_invocation_;
60
61 void* handle_;
62 jint (*JNI_GetDefaultJavaVMInitArgs_)(void*);
63 jint (*JNI_CreateJavaVM_)(JavaVM**, JNIEnv**, void*);
64 jint (*JNI_GetCreatedJavaVMs_)(JavaVM**, jsize, jsize*);
65
66 friend jint JNI_GetDefaultJavaVMInitArgs(void* vm_args);
67 friend jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args);
68 friend jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize size, jsize* vm_count);
Tom Cherry3c461712017-12-14 00:00:15 -080069 friend class JNIInvocation_Debuggable_Test;
70 friend class JNIInvocation_NonDebuggable_Test;
Brian Carlstromb2acfa22013-06-19 15:09:28 -070071};
72
73#endif // JNI_INVOCATION_H_included