• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #include "jni.h"
18 #include "core_jni_helpers.h"
19 
20 #include <sstream>
21 #include <iostream>
22 #include <unicode/putil.h>
23 #include <unordered_map>
24 #include <vector>
25 
26 using namespace std;
27 
28 /*
29  * This is responsible for setting up the JNI environment for communication between
30  * the Java and native parts of layoutlib, including registering native methods.
31  * This is mostly achieved by copying the way it is done in the platform
32  * (see AndroidRuntime.cpp).
33  */
34 
35 static JavaVM* javaVM;
36 
37 namespace android {
38 
39 extern int register_android_animation_PropertyValuesHolder(JNIEnv *env);
40 extern int register_android_content_AssetManager(JNIEnv* env);
41 extern int register_android_content_StringBlock(JNIEnv* env);
42 extern int register_android_content_XmlBlock(JNIEnv* env);
43 extern int register_android_content_res_ApkAssets(JNIEnv* env);
44 extern int register_android_os_FileObserver(JNIEnv* env);
45 extern int register_android_os_MessageQueue(JNIEnv* env);
46 extern int register_android_os_SystemClock(JNIEnv* env);
47 extern int register_android_os_SystemProperties(JNIEnv* env);
48 extern int register_android_os_Trace(JNIEnv* env);
49 extern int register_android_text_AndroidCharacter(JNIEnv* env);
50 extern int register_android_util_EventLog(JNIEnv* env);
51 extern int register_android_util_Log(JNIEnv* env);
52 extern int register_android_util_jar_StrictJarFile(JNIEnv* env);
53 extern int register_com_android_internal_util_VirtualRefBasePtr(JNIEnv *env);
54 
55 #define REG_JNI(name)      { name }
56 struct RegJNIRec {
57     int (*mProc)(JNIEnv*);
58 };
59 
60 // Map of all possible class names to register to their corresponding JNI registration function pointer
61 // The actual list of registered classes will be determined at runtime via the 'native_classes' System property
62 static const std::unordered_map<std::string, RegJNIRec> gRegJNIMap = {
63         {"android.animation.PropertyValuesHolder",
64          REG_JNI(register_android_animation_PropertyValuesHolder)},
65 #ifdef __linux__
66         {"android.content.res.ApkAssets", REG_JNI(register_android_content_res_ApkAssets)},
67         {"android.content.res.AssetManager", REG_JNI(register_android_content_AssetManager)},
68 #endif
69         {"android.content.res.StringBlock", REG_JNI(register_android_content_StringBlock)},
70         {"android.content.res.XmlBlock", REG_JNI(register_android_content_XmlBlock)},
71 #ifdef __linux__
72         {"android.os.FileObserver", REG_JNI(register_android_os_FileObserver)},
73         {"android.os.MessageQueue", REG_JNI(register_android_os_MessageQueue)},
74 #endif
75         {"android.os.SystemClock", REG_JNI(register_android_os_SystemClock)},
76         {"android.os.SystemProperties", REG_JNI(register_android_os_SystemProperties)},
77         {"android.os.Trace", REG_JNI(register_android_os_Trace)},
78         {"android.text.AndroidCharacter", REG_JNI(register_android_text_AndroidCharacter)},
79         {"android.util.EventLog", REG_JNI(register_android_util_EventLog)},
80         {"android.util.Log", REG_JNI(register_android_util_Log)},
81         {"android.util.jar.StrictJarFile", REG_JNI(register_android_util_jar_StrictJarFile)},
82         {"com.android.internal.util.VirtualRefBasePtr",
83          REG_JNI(register_com_android_internal_util_VirtualRefBasePtr)},
84 };
85 // Vector to store the names of classes that need delegates of their native methods
86 static vector<string> classesToDelegate;
87 
register_jni_procs(const std::unordered_map<std::string,RegJNIRec> & jniRegMap,const vector<string> & classesToRegister,JNIEnv * env)88 static int register_jni_procs(const std::unordered_map<std::string, RegJNIRec>& jniRegMap,
89         const vector<string>& classesToRegister, JNIEnv* env) {
90 
91     for (const string& className : classesToRegister) {
92         if (jniRegMap.at(className).mProc(env) < 0) {
93             return -1;
94         }
95     }
96     return 0;
97 }
98 
registerNativeMethods(JNIEnv * env,const char * className,const JNINativeMethod * gMethods,int numMethods)99 int AndroidRuntime::registerNativeMethods(JNIEnv* env,
100         const char* className, const JNINativeMethod* gMethods, int numMethods) {
101     string classNameString = string(className);
102     if (find(classesToDelegate.begin(), classesToDelegate.end(), classNameString)
103             != classesToDelegate.end()) {
104         // Register native methods to the delegate class <classNameString>_NativeDelegate
105         // by adding _Original to the name of each method.
106         replace(classNameString.begin(), classNameString.end(), '$', '_');
107         string delegateClassName = classNameString + "_NativeDelegate";
108         jclass clazz = env->FindClass(delegateClassName.c_str());
109         JNINativeMethod gTypefaceDelegateMethods[numMethods];
110         for (int i = 0; i < numMethods; i++) {
111             JNINativeMethod gTypefaceMethod = gMethods[i];
112             string newName = string(gTypefaceMethod.name) + "_Original";
113             gTypefaceDelegateMethods[i].name = strdup(newName.c_str());
114             gTypefaceDelegateMethods[i].signature = gTypefaceMethod.signature;
115             gTypefaceDelegateMethods[i].fnPtr = gTypefaceMethod.fnPtr;
116         }
117         int result = env->RegisterNatives(clazz, gTypefaceDelegateMethods, numMethods);
118         for (int i = 0; i < numMethods; i++) {
119             free((char*)gTypefaceDelegateMethods[i].name);
120         }
121         return result;
122     }
123 
124     jclass clazz = env->FindClass(className);
125     return env->RegisterNatives(clazz, gMethods, numMethods);
126 }
127 
getJNIEnv()128 JNIEnv* AndroidRuntime::getJNIEnv() {
129     JNIEnv* env;
130     if (javaVM->GetEnv((void**) &env, JNI_VERSION_1_6) != JNI_OK)
131         return nullptr;
132     return env;
133 }
134 
getJavaVM()135 JavaVM* AndroidRuntime::getJavaVM() {
136     return javaVM;
137 }
138 
parseCsv(const string & csvString)139 static vector<string> parseCsv(const string& csvString) {
140     vector<string>   result;
141     istringstream stream(csvString);
142     string segment;
143     while(getline(stream, segment, ','))
144     {
145         result.push_back(segment);
146     }
147     return result;
148 }
149 
parseCsv(JNIEnv * env,jstring csvJString)150 static vector<string> parseCsv(JNIEnv* env, jstring csvJString) {
151     const char* charArray = env->GetStringUTFChars(csvJString, 0);
152     string csvString(charArray);
153     vector<string> result = parseCsv(csvString);
154     env->ReleaseStringUTFChars(csvJString, charArray);
155     return result;
156 }
157 
158 } // namespace android
159 
160 using namespace android;
161 
JNI_OnLoad(JavaVM * vm,void *)162 JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) {
163     javaVM = vm;
164     JNIEnv* env = nullptr;
165     if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
166         return JNI_ERR;
167     }
168 
169     // Configuration is stored as java System properties.
170     // Get a reference to System.getProperty
171     jclass system = FindClassOrDie(env, "java/lang/System");
172     jmethodID getPropertyMethod = GetStaticMethodIDOrDie(env, system, "getProperty",
173                                                          "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
174 
175     // Get the names of classes that have to delegate their native methods
176     auto delegateNativesToNativesString =
177             (jstring) env->CallStaticObjectMethod(system,
178                     getPropertyMethod, env->NewStringUTF("delegate_natives_to_natives"),
179                     env->NewStringUTF(""));
180     classesToDelegate = parseCsv(env, delegateNativesToNativesString);
181 
182     // Get the names of classes that need to register their native methods
183     auto nativesClassesJString =
184             (jstring) env->CallStaticObjectMethod(system,
185                                                   getPropertyMethod, env->NewStringUTF("native_classes"),
186                                                   env->NewStringUTF(""));
187     vector<string> classesToRegister = parseCsv(env, nativesClassesJString);
188 
189     if (register_jni_procs(gRegJNIMap, classesToRegister, env) < 0) {
190         return JNI_ERR;
191     }
192 
193     // Set the location of ICU data
194     auto stringPath = (jstring) env->CallStaticObjectMethod(system,
195         getPropertyMethod, env->NewStringUTF("icu.dir"),
196         env->NewStringUTF(""));
197     const char* path = env->GetStringUTFChars(stringPath, 0);
198     u_setDataDirectory(path);
199     env->ReleaseStringUTFChars(stringPath, path);
200 
201 
202     return JNI_VERSION_1_6;
203 }
204 
205