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 jlong JNICALL JniCtor(JNIEnv* env, jclass clazz, jlong enginePtr) |
| 23 { |
| 24 TRY |
| 25 { |
| 26 AdblockPlus::JsEnginePtr& jsEngine = *AdblockPlus::Android::JniLong2TypePtr<
AdblockPlus::JsEnginePtr>(enginePtr); |
| 27 return AdblockPlus::Android::JniPtr2Long(new AdblockPlus::FilterEngine(jsEng
ine)); |
| 28 } |
| 29 CATCH_THROW_AND_RETURN(env, 0) |
| 30 } |
| 31 |
| 32 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) |
| 33 { |
| 34 delete AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::FilterEngine>(ptr); |
| 35 } |
| 36 |
| 37 static jboolean JNICALL JniIsFirstRun(JNIEnv* env, jclass clazz, jlong ptr) |
| 38 { |
| 39 TRY |
| 40 { |
| 41 AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<Ad
blockPlus::FilterEngine>(ptr); |
| 42 |
| 43 return engine->IsFirstRun() ? JNI_TRUE : JNI_FALSE; |
| 44 } |
| 45 CATCH_THROW_AND_RETURN(env, JNI_FALSE); |
| 46 } |
| 47 |
| 48 static jobject JNICALL JniGetFilter(JNIEnv* env, jclass clazz, jlong ptr, jstrin
g jText) |
| 49 { |
| 50 AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<Adbl
ockPlus::FilterEngine>(ptr); |
| 51 std::string text = AdblockPlus::Android::JniJava2StdString(env, jText); |
| 52 |
| 53 TRY |
| 54 { |
| 55 AdblockPlus::FilterPtr filter = engine->GetFilter(text); |
| 56 |
| 57 return AdblockPlus::Android::NewJniFilter(env, filter); |
| 58 } |
| 59 CATCH_THROW_AND_RETURN(env, 0); |
| 60 } |
| 61 |
| 62 static jobject JNICALL JniGetListedFilters(JNIEnv* env, jclass clazz, jlong ptr) |
| 63 { |
| 64 AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<Adbl
ockPlus::FilterEngine>(ptr); |
| 65 |
| 66 TRY |
| 67 { |
| 68 std::vector<AdblockPlus::FilterPtr> filters = engine->GetListedFilters(); |
| 69 |
| 70 jobject list = AdblockPlus::Android::NewJniArrayList(env); |
| 71 |
| 72 for (std::vector<AdblockPlus::FilterPtr>::iterator it = filters.begin(), end
= filters.end(); it != end; it++) |
| 73 { |
| 74 AdblockPlus::Android::JniAddObjectToList(env, list, AdblockPlus::Android::
NewJniFilter(env, *it)); |
| 75 } |
| 76 |
| 77 return list; |
| 78 } |
| 79 CATCH_THROW_AND_RETURN(env, 0); |
| 80 } |
| 81 |
| 82 static jobject JNICALL JniGetSubscription(JNIEnv* env, jclass clazz, jlong ptr,
jstring jUrl) |
| 83 { |
| 84 AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<Adbl
ockPlus::FilterEngine>(ptr); |
| 85 std::string url = AdblockPlus::Android::JniJava2StdString(env, jUrl); |
| 86 |
| 87 TRY |
| 88 { |
| 89 AdblockPlus::SubscriptionPtr subscription = engine->GetSubscription(url); |
| 90 |
| 91 return AdblockPlus::Android::NewJniSubscription(env, subscription); |
| 92 } |
| 93 CATCH_THROW_AND_RETURN(env, 0); |
| 94 } |
| 95 |
| 96 static jobject JNICALL JniGetListedSubscriptions(JNIEnv* env, jclass clazz, jlon
g ptr) |
| 97 { |
| 98 AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<Adbl
ockPlus::FilterEngine>(ptr); |
| 99 |
| 100 TRY |
| 101 { |
| 102 std::vector<AdblockPlus::SubscriptionPtr> filters = engine->GetListedSubscri
ptions(); |
| 103 |
| 104 jobject list = AdblockPlus::Android::NewJniArrayList(env); |
| 105 |
| 106 for (std::vector<AdblockPlus::SubscriptionPtr>::iterator it = filters.begin(
), end = filters.end(); it != end; it++) |
| 107 { |
| 108 AdblockPlus::Android::JniAddObjectToList(env, list, AdblockPlus::Android::
NewJniSubscription(env, *it)); |
| 109 } |
| 110 |
| 111 return list; |
| 112 } |
| 113 CATCH_THROW_AND_RETURN(env, 0); |
| 114 } |
| 115 |
| 116 // FIXME: Code duplication |
| 117 static jobject JNICALL JniFetchAvailableSubscriptions(JNIEnv* env, jclass clazz,
jlong ptr) |
| 118 { |
| 119 AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<Adbl
ockPlus::FilterEngine>(ptr); |
| 120 |
| 121 TRY |
| 122 { |
| 123 std::vector<AdblockPlus::SubscriptionPtr> filters = engine->FetchAvailableSu
bscriptions(); |
| 124 |
| 125 jobject list = AdblockPlus::Android::NewJniArrayList(env); |
| 126 |
| 127 for (std::vector<AdblockPlus::SubscriptionPtr>::iterator it = filters.begin(
), end = filters.end(); it != end; it++) |
| 128 { |
| 129 AdblockPlus::Android::JniAddObjectToList(env, list, AdblockPlus::Android::
NewJniSubscription(env, *it)); |
| 130 } |
| 131 |
| 132 return list; |
| 133 } |
| 134 CATCH_THROW_AND_RETURN(env, 0); |
| 135 } |
| 136 |
| 137 static void JNICALL JniRemoveFilterChangeCallback(JNIEnv* env, jclass clazz, jlo
ng ptr) |
| 138 { |
| 139 AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<Adbl
ockPlus::FilterEngine>(ptr); |
| 140 |
| 141 TRY |
| 142 { |
| 143 engine->RemoveFilterChangeCallback(); |
| 144 } |
| 145 CATCH_AND_THROW(env) |
| 146 } |
| 147 |
| 148 static void JNICALL JniSetFilterChangeCallback(JNIEnv* env, jclass clazz, jlong
ptr, jlong filterPtr) |
| 149 { |
| 150 AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<Adbl
ockPlus::FilterEngine>(ptr); |
| 151 AdblockPlus::Android::JniFilterChangeCallback* callback = AdblockPlus::Android
::JniLong2TypePtr< |
| 152 AdblockPlus::Android::JniFilterChangeCallback>(filterPtr); |
| 153 |
| 154 AdblockPlus::FilterEngine::FilterChangeCallback filterCallback = std::tr1::bin
d(&AdblockPlus::Android::JniFilterChangeCallback::Callback, |
| 155 callback, std::tr1::placeholders::_1, std::tr1::placeholders::_2); |
| 156 |
| 157 TRY |
| 158 { |
| 159 engine->SetFilterChangeCallback(filterCallback); |
| 160 } |
| 161 CATCH_AND_THROW(env) |
| 162 } |
| 163 |
| 164 static void JNICALL JniForceUpdateCheck(JNIEnv* env, jclass clazz, jlong ptr, jl
ong updaterPtr) |
| 165 { |
| 166 AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<Adbl
ockPlus::FilterEngine>(ptr); |
| 167 AdblockPlus::Android::JniUpdaterCallback* callback = AdblockPlus::Android::Jni
Long2TypePtr<AdblockPlus::Android::JniUpdaterCallback>( |
| 168 updaterPtr); |
| 169 |
| 170 AdblockPlus::FilterEngine::UpdaterCallback updaterCallback = 0; |
| 171 |
| 172 if (updaterPtr) |
| 173 { |
| 174 updaterCallback = std::tr1::bind(&AdblockPlus::Android::JniUpdaterCallback::
Callback, callback, std::tr1::placeholders::_1); |
| 175 } |
| 176 |
| 177 TRY |
| 178 { |
| 179 engine->ForceUpdateCheck(updaterCallback); |
| 180 } |
| 181 CATCH_AND_THROW(env) |
| 182 } |
| 183 |
| 184 static jobject JNICALL JniGetElementHidingSelectors(JNIEnv* env, jclass clazz, j
long ptr, jstring jDomain) |
| 185 { |
| 186 AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<Adbl
ockPlus::FilterEngine>(ptr); |
| 187 |
| 188 std::string domain = AdblockPlus::Android::JniJava2StdString(env, jDomain); |
| 189 |
| 190 TRY |
| 191 { |
| 192 std::vector<std::string> selectors = engine->GetElementHidingSelectors(domai
n); |
| 193 |
| 194 jobject list = AdblockPlus::Android::NewJniArrayList(env); |
| 195 |
| 196 for (std::vector<std::string>::iterator it = selectors.begin(), end = select
ors.end(); it != end; it++) |
| 197 { |
| 198 AdblockPlus::Android::JniAddObjectToList(env, list, env->NewStringUTF(it->
c_str())); |
| 199 } |
| 200 |
| 201 return list; |
| 202 } |
| 203 CATCH_THROW_AND_RETURN(env, 0) |
| 204 } |
| 205 |
| 206 static jobject JNICALL JniMatches(JNIEnv* env, jclass clazz, jlong ptr, jstring
jUrl, jstring jContentType, jstring jDocumentUrl) |
| 207 { |
| 208 AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<Adbl
ockPlus::FilterEngine>(ptr); |
| 209 |
| 210 std::string url = AdblockPlus::Android::JniJava2StdString(env, jUrl); |
| 211 std::string contentType = AdblockPlus::Android::JniJava2StdString(env, jConten
tType); |
| 212 std::string documentUrl = AdblockPlus::Android::JniJava2StdString(env, jDocume
ntUrl); |
| 213 |
| 214 TRY |
| 215 { |
| 216 AdblockPlus::FilterPtr filter = engine->Matches(url, contentType, documentUr
l); |
| 217 |
| 218 return AdblockPlus::Android::NewJniFilter(env, filter); |
| 219 } |
| 220 CATCH_THROW_AND_RETURN(env, 0) |
| 221 } |
| 222 |
| 223 static jobject JNICALL JniMatchesMany(JNIEnv* env, jclass clazz, jlong ptr, jstr
ing jUrl, jstring jContentType, jobjectArray jDocumentUrls) |
| 224 { |
| 225 AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<Adbl
ockPlus::FilterEngine>(ptr); |
| 226 |
| 227 std::string url = AdblockPlus::Android::JniJava2StdString(env, jUrl); |
| 228 std::string contentType = AdblockPlus::Android::JniJava2StdString(env, jConten
tType); |
| 229 |
| 230 std::vector<std::string> documentUrls; |
| 231 |
| 232 jsize len = env->GetArrayLength(jDocumentUrls); |
| 233 |
| 234 for (jsize i = 0; i < len; i++) |
| 235 { |
| 236 documentUrls.push_back( |
| 237 AdblockPlus::Android::JniJava2StdString(env, static_cast<jstring>(env->G
etObjectArrayElement(jDocumentUrls, i)))); |
| 238 } |
| 239 |
| 240 TRY |
| 241 { |
| 242 AdblockPlus::FilterPtr filter = engine->Matches(url, contentType, documentUr
ls); |
| 243 |
| 244 return AdblockPlus::Android::NewJniFilter(env, filter); |
| 245 } |
| 246 CATCH_THROW_AND_RETURN(env, 0) |
| 247 } |
| 248 |
| 249 static jobject JNICALL JniGetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring
jPref) |
| 250 { |
| 251 AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<Adbl
ockPlus::FilterEngine>(ptr); |
| 252 |
| 253 std::string pref = AdblockPlus::Android::JniJava2StdString(env, jPref); |
| 254 |
| 255 TRY |
| 256 { |
| 257 AdblockPlus::JsValuePtr value = engine->GetPref(pref); |
| 258 |
| 259 return AdblockPlus::Android::NewJniJsValue(env, value); |
| 260 } |
| 261 CATCH_THROW_AND_RETURN(env, 0) |
| 262 } |
| 263 |
| 264 static void JNICALL JniSetPref(JNIEnv* env, jclass clazz, jlong ptr, jstring jPr
ef, jlong jsValue) |
| 265 { |
| 266 AdblockPlus::FilterEngine* engine = AdblockPlus::Android::JniLong2TypePtr<Adbl
ockPlus::FilterEngine>(ptr); |
| 267 |
| 268 std::string pref = AdblockPlus::Android::JniJava2StdString(env, jPref); |
| 269 AdblockPlus::JsValuePtr value = AdblockPlus::Android::JniGetJsValuePtr(jsValue
); |
| 270 |
| 271 TRY |
| 272 { |
| 273 engine->SetPref(pref, value); |
| 274 } |
| 275 CATCH_AND_THROW(env) |
| 276 } |
| 277 |
| 278 static JNINativeMethod methods[] = |
| 279 { |
| 280 { (char*)"ctor", (char*)"(J)J", (void*)JniCtor }, |
| 281 { (char*)"isFirstRun", (char*)"(J)Z", (void*)JniIsFirstRun }, |
| 282 { (char*)"getFilter", (char*)"(JLjava/lang/String;)" TYP("Filter"), (void*)JniGe
tFilter }, |
| 283 { (char*)"getListedFilters", (char*)"(J)Ljava/util/List;", (void*)JniGetListedFi
lters }, |
| 284 { (char*)"getSubscription", (char*)"(JLjava/lang/String;)" TYP("Subscription"),
(void*)JniGetSubscription }, |
| 285 { (char*)"getListedSubscriptions", (char*)"(J)Ljava/util/List;", (void*)JniGetLi
stedSubscriptions }, |
| 286 { (char*)"fetchAvailableSubscriptions", (char*)"(J)Ljava/util/List;", (void*)Jni
FetchAvailableSubscriptions }, |
| 287 { (char*)"setFilterChangeCallback", (char*)"(JJ)V", (void*)JniSetFilterChangeCal
lback }, |
| 288 { (char*)"removeFilterChangeCallback", (char*)"(J)V", (void*)JniRemoveFilterChan
geCallback }, |
| 289 { (char*)"forceUpdateCheck", (char*)"(JJ)V", (void*)JniForceUpdateCheck }, |
| 290 { (char*)"getElementHidingSelectors", (char*)"(JLjava/lang/String;)Ljava/util/Li
st;", (void*)JniGetElementHidingSelectors }, |
| 291 { (char*)"matches", (char*)"(JLjava/lang/String;Ljava/lang/String;Ljava/lang/Str
ing;)" TYP("Filter"), (void*)JniMatches }, |
| 292 { (char*)"matches", (char*)"(JLjava/lang/String;Ljava/lang/String;[Ljava/lang/St
ring;)" TYP("Filter"), (void*)JniMatchesMany }, |
| 293 { (char*)"getPref", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)JniGet
Pref }, |
| 294 { (char*)"setPref", (char*)"(JLjava/lang/String;J)V", (void*)JniSetPref }, |
| 295 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }, }; |
| 296 |
| 297 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_android_api_FilterEngine_
registerNatives(JNIEnv *env, jclass clazz) |
| 298 { |
| 299 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); |
| 300 } |
OLD | NEW |