LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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 21 matching lines...) Expand all Loading... |
32 | 32 |
33 appInfo.developmentBuild = JniGetBooleanField(env, clazz, jAppInfo, "developme
ntBuild"); | 33 appInfo.developmentBuild = JniGetBooleanField(env, clazz, jAppInfo, "developme
ntBuild"); |
34 } | 34 } |
35 | 35 |
36 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject jAppInfo) | 36 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jobject jAppInfo) |
37 { | 37 { |
38 AdblockPlus::AppInfo appInfo; | 38 AdblockPlus::AppInfo appInfo; |
39 | 39 |
40 TransformAppInfo(env, jAppInfo, appInfo); | 40 TransformAppInfo(env, jAppInfo, appInfo); |
41 | 41 |
42 TRY | 42 try |
43 { | 43 { |
44 return JniPtrToLong(new AdblockPlus::JsEnginePtr(AdblockPlus::JsEngine::New(
appInfo))); | 44 return JniPtrToLong(new AdblockPlus::JsEnginePtr(AdblockPlus::JsEngine::New(
appInfo))); |
45 } | 45 } |
46 CATCH_THROW_AND_RETURN(env, 0) | 46 CATCH_THROW_AND_RETURN(env, 0) |
47 } | 47 } |
48 | 48 |
49 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) | 49 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) |
50 { | 50 { |
51 delete JniLongToTypePtr<AdblockPlus::JsEnginePtr>(ptr); | 51 delete JniLongToTypePtr<AdblockPlus::JsEnginePtr>(ptr); |
52 } | 52 } |
53 | 53 |
54 static void JNICALL JniSetEventCallback(JNIEnv* env, jclass clazz, jlong ptr, js
tring jEventName, jlong jCallbackPtr) | 54 static void JNICALL JniSetEventCallback(JNIEnv* env, jclass clazz, jlong ptr, js
tring jEventName, jlong jCallbackPtr) |
55 { | 55 { |
56 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 56 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
57 | 57 |
58 JniEventCallback* callback = JniLongToTypePtr<JniEventCallback>(jCallbackPtr); | 58 JniEventCallback* callback = JniLongToTypePtr<JniEventCallback>(jCallbackPtr); |
59 std::string eventName = JniJava2StdString(env, jEventName); | 59 std::string eventName = JniJavaToStdString(env, jEventName); |
60 AdblockPlus::JsEngine::EventCallback eCallback = std::tr1::bind(&JniEventCallb
ack::Callback, callback, std::tr1::placeholders::_1); | 60 AdblockPlus::JsEngine::EventCallback eCallback = std::tr1::bind(&JniEventCallb
ack::Callback, callback, std::tr1::placeholders::_1); |
61 | 61 |
62 TRY | 62 try |
63 { | 63 { |
64 engine->SetEventCallback(eventName, eCallback); | 64 engine->SetEventCallback(eventName, eCallback); |
65 } | 65 } |
66 CATCH_AND_THROW(env) | 66 CATCH_AND_THROW(env) |
67 } | 67 } |
68 | 68 |
69 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) |
70 { | 70 { |
71 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 71 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
72 | 72 |
73 std::string eventName = JniJava2StdString(env, jEventName); | 73 std::string eventName = JniJavaToStdString(env, jEventName); |
74 | 74 |
75 TRY | 75 try |
76 { | 76 { |
77 engine->RemoveEventCallback(eventName); | 77 engine->RemoveEventCallback(eventName); |
78 } | 78 } |
79 CATCH_AND_THROW(env) | 79 CATCH_AND_THROW(env) |
80 } | 80 } |
81 | 81 |
82 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) |
83 { | 83 { |
84 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 84 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
85 | 85 |
86 std::string source = JniJava2StdString(env, jSource); | 86 std::string source = JniJavaToStdString(env, jSource); |
87 std::string filename = JniJava2StdString(env, jFilename); | 87 std::string filename = JniJavaToStdString(env, jFilename); |
88 | 88 |
89 TRY | 89 try |
90 { | 90 { |
91 AdblockPlus::JsValuePtr jsValue = engine->Evaluate(source, filename); | 91 AdblockPlus::JsValuePtr jsValue = engine->Evaluate(source, filename); |
92 return NewJniJsValue(env, jsValue); | 92 return NewJniJsValue(env, jsValue); |
93 } | 93 } |
94 CATCH_THROW_AND_RETURN(env, 0) | 94 CATCH_THROW_AND_RETURN(env, 0) |
95 } | 95 } |
96 | 96 |
97 static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstrin
g jEventName, jarray jJsPtrs) | 97 static void JNICALL JniTriggerEvent(JNIEnv* env, jclass clazz, jlong ptr, jstrin
g jEventName, jarray jJsPtrs) |
98 { | 98 { |
99 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 99 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
100 std::string eventName = JniJava2StdString(env, jEventName); | 100 std::string eventName = JniJavaToStdString(env, jEventName); |
101 AdblockPlus::JsValueList args; | 101 AdblockPlus::JsValueList args; |
102 | 102 |
103 if (jJsPtrs) | 103 if (jJsPtrs) |
104 { | 104 { |
105 jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0); | 105 jlong* ptrs = (jlong*)env->GetPrimitiveArrayCritical(jJsPtrs, 0); |
106 | 106 |
107 jsize length = env->GetArrayLength(jJsPtrs); | 107 jsize length = env->GetArrayLength(jJsPtrs); |
108 | 108 |
109 for (jsize i = 0; i < length; i++) | 109 for (jsize i = 0; i < length; i++) |
110 { | 110 { |
111 args.push_back(JniGetJsValuePtr(ptrs[i])); | 111 args.push_back(JniGetJsValuePtr(ptrs[i])); |
112 } | 112 } |
113 | 113 |
114 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT); | 114 env->ReleasePrimitiveArrayCritical(jJsPtrs, ptrs, JNI_ABORT); |
115 } | 115 } |
116 | 116 |
117 TRY | 117 try |
118 { | 118 { |
119 engine->TriggerEvent(eventName, args); | 119 engine->TriggerEvent(eventName, args); |
120 } | 120 } |
121 CATCH_AND_THROW(env) | 121 CATCH_AND_THROW(env) |
122 } | 122 } |
123 | 123 |
124 static void JNICALL JniSetDefaultFileSystem(JNIEnv* env, jclass clazz, jlong ptr
, jstring jBasePath) | 124 static void JNICALL JniSetDefaultFileSystem(JNIEnv* env, jclass clazz, jlong ptr
, jstring jBasePath) |
125 { | 125 { |
126 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 126 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
127 | 127 |
128 TRY | 128 try |
129 { | 129 { |
130 AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSystem()); | 130 AdblockPlus::FileSystemPtr fileSystem(new AdblockPlus::DefaultFileSystem()); |
131 | 131 |
132 std::string basePath = JniJava2StdString(env, jBasePath); | 132 std::string basePath = JniJavaToStdString(env, jBasePath); |
133 reinterpret_cast<AdblockPlus::DefaultFileSystem*>(fileSystem.get())->SetBase
Path(basePath); | 133 reinterpret_cast<AdblockPlus::DefaultFileSystem*>(fileSystem.get())->SetBase
Path(basePath); |
134 | 134 |
135 engine->SetFileSystem(fileSystem); | 135 engine->SetFileSystem(fileSystem); |
136 } | 136 } |
137 CATCH_AND_THROW(env) | 137 CATCH_AND_THROW(env) |
138 } | 138 } |
139 | 139 |
140 static void JNICALL JniSetDefaultWebRequest(JNIEnv* env, jclass clazz, jlong ptr
) | 140 static void JNICALL JniSetDefaultWebRequest(JNIEnv* env, jclass clazz, jlong ptr
) |
141 { | 141 { |
142 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 142 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
143 | 143 |
144 TRY | 144 try |
145 { | 145 { |
146 AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest()); | 146 AdblockPlus::WebRequestPtr webRequest(new AdblockPlus::DefaultWebRequest()); |
147 | 147 |
148 engine->SetWebRequest(webRequest); | 148 engine->SetWebRequest(webRequest); |
149 } | 149 } |
150 CATCH_AND_THROW(env) | 150 CATCH_AND_THROW(env) |
151 } | 151 } |
152 | 152 |
153 static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr) | 153 static void JNICALL JniSetDefaultLogSystem(JNIEnv* env, jclass clazz, jlong ptr) |
154 { | 154 { |
155 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 155 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
156 | 156 |
157 TRY | 157 try |
158 { | 158 { |
159 AdblockPlus::LogSystemPtr logSystem(new AdblockPlus::DefaultLogSystem()); | 159 AdblockPlus::LogSystemPtr logSystem(new AdblockPlus::DefaultLogSystem()); |
160 | 160 |
161 engine->SetLogSystem(logSystem); | 161 engine->SetLogSystem(logSystem); |
162 } | 162 } |
163 CATCH_AND_THROW(env) | 163 CATCH_AND_THROW(env) |
164 } | 164 } |
165 | 165 |
166 static void JNICALL JniSetLogSystem(JNIEnv* env, jclass clazz, jlong ptr, jlong
logSystemPtr) | 166 static void JNICALL JniSetLogSystem(JNIEnv* env, jclass clazz, jlong ptr, jlong
logSystemPtr) |
167 { | 167 { |
168 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 168 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
169 | 169 |
170 TRY | 170 try |
171 { | 171 { |
172 AdblockPlus::LogSystemPtr logSystem(JniLongToTypePtr<JniLogSystemCallback>(l
ogSystemPtr)); | 172 AdblockPlus::LogSystemPtr logSystem(JniLongToTypePtr<JniLogSystemCallback>(l
ogSystemPtr)); |
173 | 173 |
174 engine->SetLogSystem(logSystem); | 174 engine->SetLogSystem(logSystem); |
175 } | 175 } |
176 CATCH_AND_THROW(env) | 176 CATCH_AND_THROW(env) |
177 } | 177 } |
178 | 178 |
179 static void JNICALL JniSetWebRequest(JNIEnv* env, jclass clazz, jlong ptr, jlong
webRequestPtr) | 179 static void JNICALL JniSetWebRequest(JNIEnv* env, jclass clazz, jlong ptr, jlong
webRequestPtr) |
180 { | 180 { |
181 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 181 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
182 | 182 |
183 TRY | 183 try |
184 { | 184 { |
185 AdblockPlus::WebRequestPtr& webRequest = *JniLongToTypePtr<AdblockPlus::WebR
equestPtr>(webRequestPtr); | 185 AdblockPlus::WebRequestPtr& webRequest = *JniLongToTypePtr<AdblockPlus::WebR
equestPtr>(webRequestPtr); |
186 | 186 |
187 engine->SetWebRequest(webRequest); | 187 engine->SetWebRequest(webRequest); |
188 } | 188 } |
189 CATCH_AND_THROW(env) | 189 CATCH_AND_THROW(env) |
190 } | 190 } |
191 | 191 |
192 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) |
193 { | 193 { |
194 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 194 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
195 | 195 |
196 TRY | 196 try |
197 { | 197 { |
198 AdblockPlus::JsValuePtr jsValue = engine->NewValue(static_cast<int64_t>(valu
e)); | 198 AdblockPlus::JsValuePtr jsValue = engine->NewValue(static_cast<int64_t>(valu
e)); |
199 return NewJniJsValue(env, jsValue); | 199 return NewJniJsValue(env, jsValue); |
200 } | 200 } |
201 CATCH_THROW_AND_RETURN(env, 0) | 201 CATCH_THROW_AND_RETURN(env, 0) |
202 } | 202 } |
203 | 203 |
204 static jobject JNICALL JniNewBooleanValue(JNIEnv* env, jclass clazz, jlong ptr,
jboolean value) | 204 static jobject JNICALL JniNewBooleanValue(JNIEnv* env, jclass clazz, jlong ptr,
jboolean value) |
205 { | 205 { |
206 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 206 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
207 | 207 |
208 TRY | 208 try |
209 { | 209 { |
210 AdblockPlus::JsValuePtr jsValue = engine->NewValue(value == JNI_TRUE ? true
: false); | 210 AdblockPlus::JsValuePtr jsValue = engine->NewValue(value == JNI_TRUE ? true
: false); |
211 return NewJniJsValue(env, jsValue); | 211 return NewJniJsValue(env, jsValue); |
212 } | 212 } |
213 CATCH_THROW_AND_RETURN(env, 0) | 213 CATCH_THROW_AND_RETURN(env, 0) |
214 } | 214 } |
215 | 215 |
216 static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, j
string value) | 216 static jobject JNICALL JniNewStringValue(JNIEnv* env, jclass clazz, jlong ptr, j
string value) |
217 { | 217 { |
218 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); | 218 AdblockPlus::JsEnginePtr& engine = *JniLongToTypePtr<AdblockPlus::JsEnginePtr>
(ptr); |
219 | 219 |
220 TRY | 220 try |
221 { | 221 { |
222 std::string strValue = JniJava2StdString(env, value); | 222 std::string strValue = JniJavaToStdString(env, value); |
223 AdblockPlus::JsValuePtr jsValue = engine->NewValue(strValue); | 223 AdblockPlus::JsValuePtr jsValue = engine->NewValue(strValue); |
224 return NewJniJsValue(env, jsValue); | 224 return NewJniJsValue(env, jsValue); |
225 } | 225 } |
226 CATCH_THROW_AND_RETURN(env, 0) | 226 CATCH_THROW_AND_RETURN(env, 0) |
227 } | 227 } |
228 | 228 |
| 229 // TODO: List of functions that lack JNI bindings |
229 //JsValuePtr NewObject(); | 230 //JsValuePtr NewObject(); |
230 //JsValuePtr NewCallback(v8::InvocationCallback callback); | 231 //JsValuePtr NewCallback(v8::InvocationCallback callback); |
231 //static JsEnginePtr FromArguments(const v8::Arguments& arguments); | 232 //static JsEnginePtr FromArguments(const v8::Arguments& arguments); |
232 //JsValueList ConvertArguments(const v8::Arguments& arguments); | 233 //JsValueList ConvertArguments(const v8::Arguments& arguments); |
233 | 234 |
234 static JNINativeMethod methods[] = | 235 static JNINativeMethod methods[] = |
235 { | 236 { |
236 { (char*)"ctor", (char*)"(" TYP("AppInfo") ")J", (void*)JniCtor }, | 237 { (char*)"ctor", (char*)"(" TYP("AppInfo") ")J", (void*)JniCtor }, |
237 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }, | 238 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }, |
238 | 239 |
239 { (char*)"setEventCallback", (char*)"(JLjava/lang/String;J)V", (void*)JniSetEv
entCallback }, | 240 { (char*)"setEventCallback", (char*)"(JLjava/lang/String;J)V", (void*)JniSetEv
entCallback }, |
240 { (char*)"removeEventCallback", (char*)"(JLjava/lang/String;)V", (void*)JniRem
oveEventCallback }, | 241 { (char*)"removeEventCallback", (char*)"(JLjava/lang/String;)V", (void*)JniRem
oveEventCallback }, |
241 { (char*)"triggerEvent", (char*)"(JLjava/lang/String;[J)V", (void*)JniTriggerE
vent }, | 242 { (char*)"triggerEvent", (char*)"(JLjava/lang/String;[J)V", (void*)JniTriggerE
vent }, |
242 | 243 |
243 { (char*)"evaluate", (char*)"(JLjava/lang/String;Ljava/lang/String;)" TYP("JsV
alue"), (void*)JniEvaluate }, | 244 { (char*)"evaluate", (char*)"(JLjava/lang/String;Ljava/lang/String;)" TYP("JsV
alue"), (void*)JniEvaluate }, |
244 | 245 |
245 { (char*)"setDefaultFileSystem", (char*)"(JLjava/lang/String;)V", (void*)JniSe
tDefaultFileSystem }, | 246 { (char*)"setDefaultFileSystem", (char*)"(JLjava/lang/String;)V", (void*)JniSe
tDefaultFileSystem }, |
246 { (char*)"setLogSystem", (char*)"(JJ)V", (void*)JniSetLogSystem }, | 247 { (char*)"setLogSystem", (char*)"(JJ)V", (void*)JniSetLogSystem }, |
247 { (char*)"setDefaultLogSystem", (char*)"(J)V", (void*)JniSetDefaultLogSystem }
, | 248 { (char*)"setDefaultLogSystem", (char*)"(J)V", (void*)JniSetDefaultLogSystem }
, |
248 { (char*)"setWebRequest", (char*)"(JJ)V", (void*)JniSetWebRequest }, | 249 { (char*)"setWebRequest", (char*)"(JJ)V", (void*)JniSetWebRequest }, |
249 { (char*)"setDefaultWebRequest", (char*)"(J)V", (void*)JniSetDefaultWebRequest
}, | 250 { (char*)"setDefaultWebRequest", (char*)"(J)V", (void*)JniSetDefaultWebRequest
}, |
250 | 251 |
251 { (char*)"newValue", (char*)"(JJ)"TYP("JsValue"), (void*)JniNewLongValue }, | 252 { (char*)"newValue", (char*)"(JJ)"TYP("JsValue"), (void*)JniNewLongValue }, |
252 { (char*)"newValue", (char*)"(JZ)"TYP("JsValue"), (void*)JniNewBooleanValue }, | 253 { (char*)"newValue", (char*)"(JZ)"TYP("JsValue"), (void*)JniNewBooleanValue }, |
253 { (char*)"newValue", (char*)"(JLjava/lang/String;)"TYP("JsValue"), (void*)JniN
ewStringValue } | 254 { (char*)"newValue", (char*)"(JLjava/lang/String;)"TYP("JsValue"), (void*)JniN
ewStringValue } |
254 }; | 255 }; |
255 | 256 |
256 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_android_api_JsEngine_regi
sterNatives(JNIEnv *env, jclass clazz) | 257 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsEngine_r
egisterNatives(JNIEnv *env, jclass clazz) |
257 { | 258 { |
258 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); | 259 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); |
259 } | 260 } |
LEFT | RIGHT |