Left: | ||
Right: |
OLD | NEW |
---|---|
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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
49 { | 49 { |
50 delete JniLongToTypePtr<AdblockPlus::JsEnginePtr>(ptr); | 50 delete JniLongToTypePtr<AdblockPlus::JsEnginePtr>(ptr); |
51 } | 51 } |
52 | 52 |
53 static void JNICALL JniSetEventCallback(JNIEnv* env, jclass clazz, jlong ptr, js tring jEventName, jlong jCallbackPtr) | 53 static void JNICALL JniSetEventCallback(JNIEnv* env, jclass clazz, jlong ptr, js tring jEventName, jlong jCallbackPtr) |
54 { | 54 { |
55 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); | 55 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); |
56 | 56 |
57 JniEventCallback* callback = JniLongToTypePtr<JniEventCallback>(jCallbackPtr); | 57 JniEventCallback* callback = JniLongToTypePtr<JniEventCallback>(jCallbackPtr); |
58 std::string eventName = JniJavaToStdString(env, jEventName); | 58 std::string eventName = JniJavaToStdString(env, jEventName); |
59 AdblockPlus::JsEngine::EventCallback eCallback = std::bind(&JniEventCallback:: Callback, callback, std::placeholders::_1); | 59 AdblockPlus::JsEngine::EventCallback eCallback = |
60 std::bind(&JniEventCallback::Callback, callback, std::placeholders::_1); | |
60 | 61 |
61 try | 62 try |
62 { | 63 { |
63 engine->SetEventCallback(eventName, eCallback); | 64 engine->SetEventCallback(eventName, eCallback); |
64 } | 65 } |
65 CATCH_AND_THROW(env) | 66 CATCH_AND_THROW(env) |
66 } | 67 } |
67 | 68 |
68 static void JNICALL JniRemoveEventCallback(JNIEnv* env, jclass clazz, jlong ptr, jstring jEventName) | 69 static void JNICALL JniRemoveEventCallback(JNIEnv* env, jclass clazz, jlong ptr, jstring jEventName) |
69 { | 70 { |
(...skipping 10 matching lines...) Expand all Loading... | |
80 | 81 |
81 static jobject JNICALL JniEvaluate(JNIEnv* env, jclass clazz, jlong ptr, jstring jSource, jstring jFilename) | 82 static jobject JNICALL JniEvaluate(JNIEnv* env, jclass clazz, jlong ptr, jstring jSource, jstring jFilename) |
82 { | 83 { |
83 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); | 84 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); |
84 | 85 |
85 std::string source = JniJavaToStdString(env, jSource); | 86 std::string source = JniJavaToStdString(env, jSource); |
86 std::string filename = JniJavaToStdString(env, jFilename); | 87 std::string filename = JniJavaToStdString(env, jFilename); |
87 | 88 |
88 try | 89 try |
89 { | 90 { |
90 AdblockPlus::JsValuePtr jsValue = engine->Evaluate(source, filename); | 91 return NewJniJsValue(env, engine->Evaluate(source, filename)); |
91 return NewJniJsValue(env, jsValue); | |
92 } | 92 } |
93 CATCH_THROW_AND_RETURN(env, 0) | 93 CATCH_THROW_AND_RETURN(env, 0) |
94 } | 94 } |
95 | 95 |
96 static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jEventName, jarray jJsPtrs) | 96 static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jEventName, jarray jJsPtrs) |
97 { | 97 { |
98 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); | 98 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); |
99 std::string eventName = JniJavaToStdString(env, jEventName); | 99 std::string eventName = JniJavaToStdString(env, jEventName); |
100 AdblockPlus::JsValueList args; | 100 AdblockPlus::JsValueList args; |
101 | 101 |
102 if (jJsPtrs) | 102 if (jJsPtrs) |
103 { | 103 { |
104 jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0); | 104 jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0); |
105 | 105 |
106 jsize length = env->GetArrayLength(jJsPtrs); | 106 jsize length = env->GetArrayLength(jJsPtrs); |
107 | 107 |
108 for (jsize i = 0; i < length; i++) | 108 for (jsize i = 0; i < length; i++) |
109 { | 109 { |
110 args.push_back(JniGetJsValuePtr(ptrs[i])); | 110 args.push_back(JniGetJsValue(ptrs[i])); |
111 } | 111 } |
112 | 112 |
113 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT); | 113 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT); |
114 } | 114 } |
115 | 115 |
116 try | 116 try |
117 { | 117 { |
118 engine->TriggerEvent(eventName, args); | 118 engine->TriggerEvent(eventName, args); |
119 } | 119 } |
120 CATCH_AND_THROW(env) | 120 CATCH_AND_THROW(env) |
(...skipping 15 matching lines...) Expand all Loading... | |
136 CATCH_AND_THROW(env) | 136 CATCH_AND_THROW(env) |
137 } | 137 } |
138 | 138 |
139 static void JNICALL JniSetDefaultWebRequest(JNIEnv* env, jclass clazz, jlong ptr ) | 139 static void JNICALL JniSetDefaultWebRequest(JNIEnv* env, jclass clazz, jlong ptr ) |
140 { | 140 { |
141 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); | 141 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); |
142 | 142 |
143 try | 143 try |
144 { | 144 { |
145 AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest()); | 145 AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest()); |
146 | |
147 engine->SetWebRequest(webRequest); | 146 engine->SetWebRequest(webRequest); |
148 } | 147 } |
149 CATCH_AND_THROW(env) | 148 CATCH_AND_THROW(env) |
150 } | 149 } |
151 | 150 |
152 static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr) | 151 static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr) |
153 { | 152 { |
154 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); | 153 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); |
155 | 154 |
156 try | 155 try |
157 { | 156 { |
158 AdblockPlus::LogSystemPtr logSystem(new AdblockPlus::DefaultLogSystem()); | 157 AdblockPlus::LogSystemPtr logSystem(new AdblockPlus::DefaultLogSystem()); |
159 | 158 |
160 engine->SetLogSystem(logSystem); | 159 engine->SetLogSystem(logSystem); |
161 } | 160 } |
162 CATCH_AND_THROW(env) | 161 CATCH_AND_THROW(env) |
163 } | 162 } |
164 | 163 |
165 static void JNICALL JniSetLogSystem(JNIEnv* env, jclass clazz, jlong ptr, jlong logSystemPtr) | 164 static void JNICALL JniSetLogSystem(JNIEnv* env, jclass clazz, jlong ptr, jlong logSystemPtr) |
166 { | 165 { |
167 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); | 166 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); |
168 | 167 |
169 try | 168 try |
170 { | 169 { |
171 AdblockPlus::LogSystemPtr logSystem = *JniLongToTypePtr<AdblockPlus::LogSyst emPtr>(logSystemPtr); | 170 AdblockPlus::LogSystemPtr logSystem = |
sergei
2017/04/26 14:12:56
it seems these changes are unrelated, do you mind
anton
2017/04/27 06:21:01
Acknowledged.
| |
171 *JniLongToTypePtr<AdblockPlus::LogSystemPtr>(logSystemPtr); | |
172 | 172 |
173 engine->SetLogSystem(logSystem); | 173 engine->SetLogSystem(logSystem); |
174 } | 174 } |
175 CATCH_AND_THROW(env) | 175 CATCH_AND_THROW(env) |
176 } | 176 } |
177 | 177 |
178 static void JNICALL JniSetWebRequest(JNIEnv* env, jclass clazz, jlong ptr, jlong webRequestPtr) | 178 static void JNICALL JniSetWebRequest(JNIEnv* env, jclass clazz, jlong ptr, jlong webRequestPtr) |
179 { | 179 { |
180 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); | 180 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); |
181 | 181 |
182 try | 182 try |
183 { | 183 { |
184 AdblockPlus::WebRequestPtr& webRequest = *JniLongToTypePtr<AdblockPlus::WebR equestPtr>(webRequestPtr); | 184 AdblockPlus::WebRequestPtr& webRequest = |
185 *JniLongToTypePtr<AdblockPlus::WebRequestPtr>(webRequestPtr); | |
185 | 186 |
186 engine->SetWebRequest(webRequest); | 187 engine->SetWebRequest(webRequest); |
187 } | 188 } |
188 CATCH_AND_THROW(env) | 189 CATCH_AND_THROW(env) |
189 } | 190 } |
190 | 191 |
191 static jobject JNICALL JniNewLongValue(JNIEnv* env, jclass clazz, jlong ptr, jlo ng value) | 192 static jobject JNICALL JniNewLongValue(JNIEnv* env, jclass clazz, jlong ptr, jlo ng value) |
192 { | 193 { |
193 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); | 194 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); |
194 | 195 |
195 try | 196 try |
196 { | 197 { |
197 AdblockPlus::JsValuePtr jsValue = engine->NewValue(static_cast<int64_t>(valu e)); | 198 return NewJniJsValue(env, engine->NewValue(static_cast<int64_t>(value))); |
198 return NewJniJsValue(env, jsValue); | |
199 } | 199 } |
200 CATCH_THROW_AND_RETURN(env, 0) | 200 CATCH_THROW_AND_RETURN(env, 0) |
201 } | 201 } |
202 | 202 |
203 static jobject JNICALL JniNewBooleanValue(JNIEnv* env, jclass clazz, jlong ptr, jboolean value) | 203 static jobject JNICALL JniNewBooleanValue(JNIEnv* env, jclass clazz, jlong ptr, jboolean value) |
204 { | 204 { |
205 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); | 205 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); |
206 | 206 |
207 try | 207 try |
208 { | 208 { |
209 AdblockPlus::JsValuePtr jsValue = engine->NewValue(value == JNI_TRUE ? true : false); | 209 return NewJniJsValue(env, engine->NewValue(value == JNI_TRUE ? true : false) ); |
210 return NewJniJsValue(env, jsValue); | |
211 } | 210 } |
212 CATCH_THROW_AND_RETURN(env, 0) | 211 CATCH_THROW_AND_RETURN(env, 0) |
213 } | 212 } |
214 | 213 |
215 static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, j string value) | 214 static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, j string value) |
216 { | 215 { |
217 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); | 216 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr> (ptr); |
218 | 217 |
219 try | 218 try |
220 { | 219 { |
221 std::string strValue = JniJavaToStdString(env, value); | 220 std::string strValue = JniJavaToStdString(env, value); |
222 AdblockPlus::JsValuePtr jsValue = engine->NewValue(strValue); | 221 return NewJniJsValue(env, engine->NewValue(strValue)); |
223 return NewJniJsValue(env, jsValue); | |
224 } | 222 } |
225 CATCH_THROW_AND_RETURN(env, 0) | 223 CATCH_THROW_AND_RETURN(env, 0) |
226 } | 224 } |
227 | 225 |
228 // TODO: List of functions that lack JNI bindings | 226 // TODO: List of functions that lack JNI bindings |
229 //JsValuePtr NewObject(); | 227 //JsValue NewObject(); |
230 //JsValuePtr NewCallback(v8::InvocationCallback callback); | 228 //JsValue NewCallback(v8::InvocationCallback callback); |
231 //static JsEnginePtr FromArguments(const v8::Arguments& arguments); | 229 //static JsEnginePtr FromArguments(const v8::Arguments& arguments); |
232 //JsValueList ConvertArguments(const v8::Arguments& arguments); | 230 //JsValueList ConvertArguments(const v8::Arguments& arguments); |
233 | 231 |
234 static JNINativeMethod methods[] = | 232 static JNINativeMethod methods[] = |
235 { | 233 { |
236 { (char*)"ctor", (char*)"(" TYP("AppInfo") ")J", (void*)JniCtor }, | 234 { (char*)"ctor", (char*)"(" TYP("AppInfo") ")J", (void*)JniCtor }, |
237 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }, | 235 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }, |
238 | 236 |
239 { (char*)"setEventCallback", (char*)"(JLjava/lang/String;J)V", (void*)JniSetEv entCallback }, | 237 { (char*)"setEventCallback", (char*)"(JLjava/lang/String;J)V", (void*)JniSetEv entCallback }, |
240 { (char*)"removeEventCallback", (char*)"(JLjava/lang/String;)V", (void*)JniRem oveEventCallback }, | 238 { (char*)"removeEventCallback", (char*)"(JLjava/lang/String;)V", (void*)JniRem oveEventCallback }, |
241 { (char*)"triggerEvent", (char*)"(JLjava/lang/String;[J)V", (void*)JniTriggerE vent }, | 239 { (char*)"triggerEvent", (char*)"(JLjava/lang/String;[J)V", (void*)JniTriggerE vent }, |
242 | 240 |
243 { (char*)"evaluate", (char*)"(JLjava/lang/String;Ljava/lang/String;)" TYP("JsV alue"), (void*)JniEvaluate }, | 241 { (char*)"evaluate", (char*)"(JLjava/lang/String;Ljava/lang/String;)" TYP("JsV alue"), (void*)JniEvaluate }, |
244 | 242 |
245 { (char*)"setDefaultFileSystem", (char*)"(JLjava/lang/String;)V", (void*)JniSe tDefaultFileSystem }, | 243 { (char*)"setDefaultFileSystem", (char*)"(JLjava/lang/String;)V", (void*)JniSe tDefaultFileSystem }, |
246 { (char*)"setLogSystem", (char*)"(JJ)V", (void*)JniSetLogSystem }, | 244 { (char*)"setLogSystem", (char*)"(JJ)V", (void*)JniSetLogSystem }, |
247 { (char*)"setDefaultLogSystem", (char*)"(J)V", (void*)JniSetDefaultLogSystem } , | 245 { (char*)"setDefaultLogSystem", (char*)"(J)V", (void*)JniSetDefaultLogSystem } , |
248 { (char*)"setWebRequest", (char*)"(JJ)V", (void*)JniSetWebRequest }, | 246 { (char*)"setWebRequest", (char*)"(JJ)V", (void*)JniSetWebRequest }, |
249 { (char*)"setDefaultWebRequest", (char*)"(J)V", (void*)JniSetDefaultWebRequest }, | 247 { (char*)"setDefaultWebRequest", (char*)"(J)V", (void*)JniSetDefaultWebRequest }, |
250 | 248 |
251 { (char*)"newValue", (char*)"(JJ)" TYP("JsValue"), (void*)JniNewLongValue }, | 249 { (char*)"newValue", (char*)"(JJ)" TYP("JsValue"), (void*)JniNewLongValue }, |
252 { (char*)"newValue", (char*)"(JZ)" TYP("JsValue"), (void*)JniNewBooleanValue } , | 250 { (char*)"newValue", (char*)"(JZ)" TYP("JsValue"), (void*)JniNewBooleanValue } , |
253 { (char*)"newValue", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)Jni NewStringValue } | 251 { (char*)"newValue", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)Jni NewStringValue } |
254 }; | 252 }; |
255 | 253 |
256 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r egisterNatives(JNIEnv *env, jclass clazz) | 254 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r egisterNatives(JNIEnv *env, jclass clazz) |
257 { | 255 { |
258 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); | 256 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); |
259 } | 257 } |
OLD | NEW |