Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | |
3 * Copyright (C) 2006-2014 Eyeo GmbH | |
4 * | |
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 | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
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/>. | |
16 */ | |
17 | |
18 #include <AdblockPlus.h> | |
19 #include "Utils.h" | |
20 #include "JniCallbacks.h" | |
21 | |
22 static jobject SubscriptionsToArrayList(JNIEnv* env, std::vector<AdblockPlus::Su bscriptionPtr>& subscriptions) | |
23 { | |
24 jobject list = NewJniArrayList(env); | |
25 | |
26 for (std::vector<AdblockPlus::SubscriptionPtr>::iterator it = subscriptions.be gin(), end = subscriptions.end(); it != end; it++) | |
27 { | |
28 JniAddObjectToList(env, list, NewJniSubscription(env, *it)); | |
29 } | |
30 | |
31 return list; | |
32 } | |
33 | |
34 static jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jlong enginePtr) | |
35 { | |
36 TRY | |
37 { | |
38 AdblockPlus::JsEnginePtr& jsEngine = *JniLongToTypePtr<AdblockPlus::JsEngine Ptr>(enginePtr); | |
39 return JniPtrToLong(new AdblockPlus::FilterEngine(jsEngine)); | |
40 } | |
41 CATCH_THROW_AND_RETURN(env, 0) | |
42 } | |
43 | |
44 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) | |
45 { | |
46 delete JniLongToTypePtr<AdblockPlus::FilterEngine>(ptr); | |
47 } | |
48 | |
49 static jboolean JNICALL JniIsFirstRun(JNIEnv* env, jclass clazz, jlong ptr) | |
50 { | |
51 TRY | |
52 { | |
53 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngi ne>(ptr); | |
54 | |
55 return engine->IsFirstRun() ? JNI_TRUE : JNI_FALSE; | |
56 } | |
57 CATCH_THROW_AND_RETURN(env, JNI_FALSE); | |
58 } | |
59 | |
60 static jobject JNICALL JniGetFilter(JNIEnv* env, jclass clazz, jlong ptr, jstrin g jText) | |
61 { | |
62 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); | |
63 std::string text = JniJavaToStdString(env, jText); | |
64 | |
65 TRY | |
66 { | |
67 AdblockPlus::FilterPtr filter = engine->GetFilter(text); | |
68 | |
69 return NewJniFilter(env, filter); | |
70 } | |
71 CATCH_THROW_AND_RETURN(env, 0); | |
72 } | |
73 | |
74 static jobject JNICALL JniGetListedFilters(JNIEnv* env, jclass clazz, jlong ptr) | |
75 { | |
76 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); | |
77 | |
78 TRY | |
79 { | |
80 std::vector<AdblockPlus::FilterPtr> filters = engine->GetListedFilters(); | |
81 | |
82 jobject list = NewJniArrayList(env); | |
83 | |
84 for (std::vector<AdblockPlus::FilterPtr>::iterator it = filters.begin(), end = filters.end(); it != end; it++) | |
85 { | |
86 JniAddObjectToList(env, list, NewJniFilter(env, *it)); | |
87 } | |
88 | |
89 return list; | |
90 } | |
91 CATCH_THROW_AND_RETURN(env, 0); | |
92 } | |
93 | |
94 static jobject JNICALL JniGetSubscription(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl) | |
95 { | |
96 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); | |
97 std::string url = JniJavaToStdString(env, jUrl); | |
98 | |
99 TRY | |
100 { | |
101 AdblockPlus::SubscriptionPtr subscription = engine->GetSubscription(url); | |
102 | |
103 return NewJniSubscription(env, subscription); | |
104 } | |
105 CATCH_THROW_AND_RETURN(env, 0); | |
106 } | |
107 | |
108 static jobject JNICALL JniGetListedSubscriptions(JNIEnv* env, jclass clazz, jlon g ptr) | |
109 { | |
110 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); | |
111 | |
112 TRY | |
113 { | |
114 std::vector<AdblockPlus::SubscriptionPtr> subscriptions = engine->GetListedS ubscriptions(); | |
115 | |
116 return SubscriptionsToArrayList(env, subscriptions); | |
117 } | |
118 CATCH_THROW_AND_RETURN(env, 0); | |
119 } | |
120 | |
121 static jobject JNICALL JniFetchAvailableSubscriptions(JNIEnv* env, jclass clazz, jlong ptr) | |
122 { | |
123 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); | |
124 | |
125 TRY | |
126 { | |
127 std::vector<AdblockPlus::SubscriptionPtr> subscriptions = engine->FetchAvail ableSubscriptions(); | |
128 | |
129 return SubscriptionsToArrayList(env, subscriptions); | |
130 } | |
131 CATCH_THROW_AND_RETURN(env, 0); | |
132 } | |
133 | |
134 static void JNICALL JniRemoveFilterChangeCallback(JNIEnv* env, jclass clazz, jlo ng ptr) | |
135 { | |
136 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); | |
137 | |
138 TRY | |
139 { | |
140 engine->RemoveFilterChangeCallback(); | |
141 } | |
142 CATCH_AND_THROW(env) | |
143 } | |
144 | |
145 static void JNICALL JniSetFilterChangeCallback(JNIEnv* env, jclass clazz, jlong ptr, jlong filterPtr) | |
146 { | |
147 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); | |
148 JniFilterChangeCallback* callback = JniLongToTypePtr<JniFilterChangeCallback>( filterPtr); | |
149 | |
150 AdblockPlus::FilterEngine::FilterChangeCallback filterCallback = | |
151 std::tr1::bind(&JniFilterChangeCallback::Callback, callback, std::tr1::pla ceholders::_1, std::tr1::placeholders::_2); | |
152 | |
153 TRY | |
154 { | |
155 engine->SetFilterChangeCallback(filterCallback); | |
156 } | |
157 CATCH_AND_THROW(env) | |
158 } | |
159 | |
160 static void JNICALL JniForceUpdateCheck(JNIEnv* env, jclass clazz, jlong ptr, jl ong updaterPtr) | |
161 { | |
162 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); | |
163 JniUpdaterCallback* callback = JniLongToTypePtr<JniUpdaterCallback>(updaterPtr ); | |
164 | |
165 AdblockPlus::FilterEngine::UpdaterCallback updaterCallback = 0; | |
166 | |
167 if (updaterPtr) | |
168 { | |
169 updaterCallback = std::tr1::bind(&JniUpdaterCallback::Callback, callback, st d::tr1::placeholders::_1); | |
170 } | |
171 | |
172 TRY | |
173 { | |
174 engine->ForceUpdateCheck(updaterCallback); | |
175 } | |
176 CATCH_AND_THROW(env) | |
177 } | |
178 | |
179 static jobject JNICALL JniGetElementHidingSelectors(JNIEnv* env, jclass clazz, j long ptr, jstring jDomain) | |
180 { | |
181 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); | |
182 | |
183 std::string domain = JniJavaToStdString(env, jDomain); | |
184 | |
185 TRY | |
186 { | |
187 std::vector<std::string> selectors = engine->GetElementHidingSelectors(domai n); | |
188 | |
189 jobject list = NewJniArrayList(env); | |
190 | |
191 for (std::vector<std::string>::iterator it = selectors.begin(), end = select ors.end(); it != end; it++) | |
192 { | |
193 JniAddObjectToList(env, list, env->NewStringUTF(it->c_str())); | |
194 } | |
195 | |
196 return list; | |
197 } | |
198 CATCH_THROW_AND_RETURN(env, 0) | |
199 } | |
200 | |
201 static jobject JNICALL JniMatches(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl, jstring jContentType, jstring jDocumentUrl) | |
202 { | |
203 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); | |
204 | |
205 std::string url = JniJavaToStdString(env, jUrl); | |
206 std::string contentType = JniJavaToStdString(env, jContentType); | |
207 std::string documentUrl = JniJavaToStdString(env, jDocumentUrl); | |
208 | |
209 TRY | |
210 { | |
211 AdblockPlus::FilterPtr filter = engine->Matches(url, contentType, documentUr l); | |
212 | |
213 return NewJniFilter(env, filter); | |
214 } | |
215 CATCH_THROW_AND_RETURN(env, 0) | |
216 } | |
217 | |
218 static jobject JNICALL JniMatchesMany(JNIEnv* env, jclass clazz, jlong ptr, jstr ing jUrl, jstring jContentType, jobjectArray jDocumentUrls) | |
219 { | |
220 AdblockPlus::FilterEngine* engine = JniLongToTypePtr<AdblockPlus::FilterEngine >(ptr); | |
221 | |
222 std::string url = JniJavaToStdString(env, jUrl); | |
223 std::string contentType = JniJavaToStdString(env, jContentType); | |
224 | |
225 std::vector<std::string> documentUrls; | |
226 | |
227 jsize len = env->GetArrayLength(jDocumentUrls); | |
228 | |
229 for (jsize i = 0; i < len; i++) | |
230 { | |
231 documentUrls.push_back(JniJavaToStdString(env, static_cast<jstring>(env->Get ObjectArrayElement(jDocumentUrls, i)))); | |
232 } | |
233 | |
234 TRY | |
235 { | |
236 AdblockPlus::FilterPtr filter = engine->Matches(url, contentType, documentUr ls); | |
237 | |
238 return NewJniFilter(env, filter); | |
239 } | |
240 CATCH_THROW_AND_RETURN(env, 0) | |
241 } | |
242 | |
243 static jobject JNICALL JniGetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPref) | |
244 { | |
245 AdblockPlus::FilterEngine* engine = JniLongToTypePtr < AdblockPlus::FilterEngi ne > (ptr); | |
Felix Dahlke
2014/03/28 17:28:41
No whitespace around < or >
René Jeschke
2014/04/11 12:25:53
Done.
| |
246 | |
247 std::string pref = JniJavaToStdString(env, jPref); | |
248 | |
249 TRY | |
250 { | |
251 AdblockPlus::JsValuePtr value = engine->GetPref(pref); | |
252 | |
253 return NewJniJsValue(env, value); | |
254 } | |
255 CATCH_THROW_AND_RETURN(env, 0) | |
256 } | |
257 | |
258 static void JNICALL JniSetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPr ef, jlong jsValue) | |
259 { | |
260 AdblockPlus::FilterEngine* engine = JniLongToTypePtr < AdblockPlus::FilterEngi ne > (ptr); | |
261 | |
262 std::string pref = JniJavaToStdString(env, jPref); | |
263 AdblockPlus::JsValuePtr value = JniGetJsValuePtr(jsValue); | |
264 | |
265 TRY | |
266 { | |
267 engine->SetPref(pref, value); | |
268 } | |
269 CATCH_AND_THROW(env) | |
270 } | |
271 | |
272 static JNINativeMethod methods[] = | |
273 { | |
274 { (char*)"ctor", (char*)"(J)J", (void*)JniCtor }, | |
275 { (char*)"isFirstRun", (char*)"(J)Z", (void*)JniIsFirstRun }, | |
276 { (char*)"getFilter", (char*)"(JLjava/lang/String;)" TYP("Filter"), (void*)Jni GetFilter }, | |
277 { (char*)"getListedFilters", (char*)"(J)Ljava/util/List;", (void*)JniGetListed Filters }, | |
278 { (char*)"getSubscription", (char*)"(JLjava/lang/String;)" TYP("Subscription") , (void*)JniGetSubscription }, | |
279 { (char*)"getListedSubscriptions", (char*)"(J)Ljava/util/List;", (void*)JniGet ListedSubscriptions }, | |
280 { (char*)"fetchAvailableSubscriptions", (char*)"(J)Ljava/util/List;", (void*)J niFetchAvailableSubscriptions }, | |
281 { (char*)"setFilterChangeCallback", (char*)"(JJ)V", (void*)JniSetFilterChangeC allback }, | |
282 { (char*)"removeFilterChangeCallback", (char*)"(J)V", (void*)JniRemoveFilterCh angeCallback }, | |
283 { (char*)"forceUpdateCheck", (char*)"(JJ)V", (void*)JniForceUpdateCheck }, | |
284 { (char*)"getElementHidingSelectors", (char*)"(JLjava/lang/String;)Ljava/util/ List;", (void*)JniGetElementHidingSelectors }, | |
285 { (char*)"matches", (char*)"(JLjava/lang/String;Ljava/lang/String;Ljava/lang/S tring;)" TYP("Filter"), (void*)JniMatches }, | |
286 { (char*)"matches", (char*)"(JLjava/lang/String;Ljava/lang/String;[Ljava/lang/ String;)" TYP("Filter"), (void*)JniMatchesMany }, | |
287 { (char*)"getPref", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)JniG etPref }, | |
288 { (char*)"setPref", (char*)"(JLjava/lang/String;J)V", (void*)JniSetPref }, | |
289 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } | |
290 }; | |
291 | |
292 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_android_api_FilterEngine_ registerNatives(JNIEnv *env, jclass clazz) | |
293 { | |
294 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); | |
295 } | |
OLD | NEW |