Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: libadblockplus-android/jni/Utils.cpp

Issue 29354787: Issue 4442 - Cache class and ctors references in JNI_Load/UnLoad (Closed)
Patch Set: reverted to NewTuple Created Nov. 21, 2016, 8:25 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « libadblockplus-android/jni/Utils.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <string> 18 #include <string>
19 19
20 #include "Utils.h" 20 #include "Utils.h"
21 21
22 // precached in JNI_OnLoad and released in JNI_OnUnload
23 JniGlobalReference<jclass>* arrayListClass;
24 jmethodID arrayListCtor;
25
26 JniGlobalReference<jclass>* filterClass;
27 jmethodID filterCtor;
28
29 JniGlobalReference<jclass>* subscriptionClass;
30 jmethodID subscriptionCtor;
31
32 JniGlobalReference<jclass>* notificationClass;
33 jmethodID notificationCtor;
34
35 JniGlobalReference<jclass>* exceptionClass;
36
37 void JniUtils_OnLoad(JavaVM* vm, JNIEnv* env, void* reserved)
38 {
39 arrayListClass = new JniGlobalReference<jclass>(env, env->FindClass("java/util /ArrayList"));
40 arrayListCtor = env->GetMethodID(arrayListClass->Get(), "<init>", "()V");
41
42 filterClass = new JniGlobalReference<jclass>(env, env->FindClass(PKG("Filter") ));
43 filterCtor = env->GetMethodID(filterClass->Get(), "<init>", "(J)V");
44
45 subscriptionClass = new JniGlobalReference<jclass>(env, env->FindClass(PKG("Su bscription")));
46 subscriptionCtor = env->GetMethodID(subscriptionClass->Get(), "<init>", "(J)V" );
47
48 notificationClass = new JniGlobalReference<jclass>(env, env->FindClass(PKG("No tification")));
49 notificationCtor = env->GetMethodID(notificationClass->Get(), "<init>", "(J)V" );
50
51 exceptionClass = new JniGlobalReference<jclass>(env, env->FindClass(PKG("Adblo ckPlusException")));
52 }
53
54 void JniUtils_OnUnload(JavaVM* vm, JNIEnv* env, void* reserved)
55 {
56 if (arrayListClass)
57 {
58 delete arrayListClass;
59 arrayListClass = NULL;
60 }
61
62 if (filterClass)
63 {
64 delete filterClass;
65 filterClass = NULL;
66 }
67
68 if (subscriptionClass)
69 {
70 delete subscriptionClass;
71 subscriptionClass = NULL;
72 }
73
74 if (notificationClass)
75 {
76 delete notificationClass;
77 notificationClass = NULL;
78 }
79
80 if (exceptionClass)
81 {
82 delete exceptionClass;
83 exceptionClass = NULL;
84 }
85 }
86
22 std::string JniJavaToStdString(JNIEnv* env, jstring str) 87 std::string JniJavaToStdString(JNIEnv* env, jstring str)
23 { 88 {
24 if (!str) 89 if (!str)
25 { 90 {
26 return std::string(); 91 return std::string();
27 } 92 }
28 93
29 const char* cStr = env->GetStringUTFChars(str, 0); 94 const char* cStr = env->GetStringUTFChars(str, 0);
30 std::string ret(cStr); 95 std::string ret(cStr);
31 env->ReleaseStringUTFChars(str, cStr); 96 env->ReleaseStringUTFChars(str, cStr);
32 97
33 return ret; 98 return ret;
34 } 99 }
35 100
36 jobject NewJniArrayList(JNIEnv* env) 101 jobject NewJniArrayList(JNIEnv* env)
37 { 102 {
38 JniLocalReference<jclass> clazz(env, env->FindClass("java/util/ArrayList")); 103 return env->NewObject(arrayListClass->Get(), arrayListCtor);
39 jmethodID ctor = env->GetMethodID(*clazz, "<init>", "()V"); 104 }
40 return env->NewObject(*clazz, ctor); 105
106 jmethodID JniGetAddToListMethod(JNIEnv* env, jobject list)
107 {
108 JniLocalReference<jclass> clazz(env, env->GetObjectClass(list));
109 return env->GetMethodID(*clazz, "add", "(Ljava/lang/Object;)Z");
110 }
111
112 void JniAddObjectToList(JNIEnv* env, jobject list, jmethodID addMethod, jobject value)
113 {
114 env->CallBooleanMethod(list, addMethod, value);
41 } 115 }
42 116
43 void JniAddObjectToList(JNIEnv* env, jobject list, jobject value) 117 void JniAddObjectToList(JNIEnv* env, jobject list, jobject value)
44 { 118 {
45 JniLocalReference<jclass> clazz(env, env->GetObjectClass(list)); 119 jmethodID addMethod = JniGetAddToListMethod(env, list);
46 jmethodID add = env->GetMethodID(*clazz, "add", "(Ljava/lang/Object;)Z"); 120 JniAddObjectToList(env, list, addMethod, value);
47 env->CallBooleanMethod(list, add, value);
48 } 121 }
49 122
50 void JniThrowException(JNIEnv* env, const std::string& message) 123 void JniThrowException(JNIEnv* env, const std::string& message)
51 { 124 {
52 JniLocalReference<jclass> clazz(env, 125 env->ThrowNew(exceptionClass->Get(), message.c_str());
53 env->FindClass(PKG("AdblockPlusException")));
54 env->ThrowNew(*clazz, message.c_str());
55 } 126 }
56 127
57 void JniThrowException(JNIEnv* env, const std::exception& e) 128 void JniThrowException(JNIEnv* env, const std::exception& e)
58 { 129 {
59 JniThrowException(env, e.what()); 130 JniThrowException(env, e.what());
60 } 131 }
61 132
62 void JniThrowException(JNIEnv* env) 133 void JniThrowException(JNIEnv* env)
63 { 134 {
64 JniThrowException(env, "Unknown exception from libadblockplus"); 135 JniThrowException(env, "Unknown exception from libadblockplus");
(...skipping 16 matching lines...) Expand all
81 152
82 JNIEnvAcquire::~JNIEnvAcquire() 153 JNIEnvAcquire::~JNIEnvAcquire()
83 { 154 {
84 if (attachmentStatus == JNI_EDETACHED) 155 if (attachmentStatus == JNI_EDETACHED)
85 { 156 {
86 javaVM->DetachCurrentThread(); 157 javaVM->DetachCurrentThread();
87 } 158 }
88 } 159 }
89 160
90 template<typename T> 161 template<typename T>
91 static jobject NewJniObject(JNIEnv* env, const T& value, const char* javaClass) 162 static jobject NewJniObject(JNIEnv* env, const T& value, jclass clazz, jmethodID ctor)
92 { 163 {
93 if (!value.get()) 164 if (!value.get())
94 { 165 {
95 return 0; 166 return 0;
96 } 167 }
97 168
98 JniLocalReference<jclass> clazz( 169 return env->NewObject(clazz, ctor, JniPtrToLong(new T(value)));
99 env, 170 }
100 env->FindClass(javaClass));
101 jmethodID method = env->GetMethodID(*clazz, "<init>", "(J)V");
102 171
103 return env->NewObject( 172 template<typename T>
104 *clazz, 173 static jobject NewJniObject(JNIEnv* env, const T& value, const char* javaClass)
105 method, 174 {
106 JniPtrToLong(new T(value))); 175 JniLocalReference<jclass> clazz( env, env->FindClass(javaClass));
176 jmethodID ctor = env->GetMethodID(*clazz, "<init>", "(J)V");
177 return NewJniObject(env, value, *clazz, ctor);
107 } 178 }
108 179
109 jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter) 180 jobject NewJniFilter(JNIEnv* env, const AdblockPlus::FilterPtr& filter)
110 { 181 {
111 return NewJniObject(env, filter, PKG("Filter")); 182 return NewJniObject(env, filter, filterClass->Get(), filterCtor);
112 } 183 }
113 184
114 jobject NewJniSubscription(JNIEnv* env, 185 jobject NewJniSubscription(JNIEnv* env,
115 const AdblockPlus::SubscriptionPtr& subscription) 186 const AdblockPlus::SubscriptionPtr& subscription)
116 { 187 {
117 return NewJniObject(env, subscription, PKG("Subscription")); 188 return NewJniObject(env, subscription, subscriptionClass->Get(), subscriptionC tor);
118 } 189 }
119 190
120 jobject NewJniNotification(JNIEnv* env, 191 jobject NewJniNotification(JNIEnv* env,
121 const AdblockPlus::NotificationPtr& notification) 192 const AdblockPlus::NotificationPtr& notification)
122 { 193 {
123 return NewJniObject(env, notification, PKG("Notification")); 194 return NewJniObject(env, notification, notificationClass->Get(), notificationC tor);
124 } 195 }
OLDNEW
« no previous file with comments | « libadblockplus-android/jni/Utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld