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

Side by Side Diff: jni/abpEngine.cpp

Issue 11172036: ABP/Android libadblockplus integration (Closed)
Patch Set: Created July 22, 2013, 8:52 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
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 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 <jni.h>
19 #include <AdblockPlus.h>
20 #include "AndroidLogSystem.h"
21 #include "AndroidWebRequest.h"
22 #include "Utils.h"
23 #include "debug.h"
24
25 JavaVM* globalJvm;
26 AdblockPlus::FilterEngine* filterEngine;
Wladimir Palant 2013/09/12 11:31:14 Please use a smart pointer here, raw pointers shou
27 jobject jniObject;
28
29 extern "C"
30 {
31 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_initialize(JNIEn v *pEnv, jobject, jstring basepath);
32 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_release(JNIEnv * pEnv, jobject);
33 JNIEXPORT jboolean JNICALL Java_org_adblockplus_android_ABPEngine_isFirstRun(J NIEnv *pEnv, jobject);
34 JNIEXPORT jobjectArray JNICALL Java_org_adblockplus_android_ABPEngine_getListe dSubscriptions(JNIEnv *pEnv, jobject);
35 JNIEXPORT jobjectArray JNICALL Java_org_adblockplus_android_ABPEngine_getRecom mendedSubscriptions(JNIEnv *pEnv, jobject);
36 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_addSubscription( JNIEnv *pEnv, jobject, jstring url);
37 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_removeSubscripti on(JNIEnv *pEnv, jobject, jstring url);
38 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_refreshSubscript ion(JNIEnv *pEnv, jobject, jstring url);
39 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_actualizeSubscri ptionStatus(JNIEnv *pEnv, jobject, jstring url);
40 JNIEXPORT jboolean JNICALL Java_org_adblockplus_android_ABPEngine_matches(JNIE nv *pEnv, jobject, jstring url, jstring contentType, jstring documentUrl);
41 JNIEXPORT jobjectArray JNICALL Java_org_adblockplus_android_ABPEngine_getSelec torsForDomain(JNIEnv *pEnv, jobject, jstring domain);
42 };
43
44 jobjectArray subscriptionsAsJavaArray(JNIEnv *pEnv, std::vector<AdblockPlus::Sub scriptionPtr> subscriptions)
45 {
46 D(D_WARN, "subscriptionsAsJavaArray()");
47 static jclass cls = reinterpret_cast<jclass>(pEnv->NewGlobalRef(pEnv->FindClas s("org/adblockplus/android/Subscription")));
48 static jmethodID cid = pEnv->GetMethodID(cls, "<init>", "()V");
49 static jfieldID ftitle = pEnv->GetFieldID(cls, "title", "Ljava/lang/String;");
50 static jfieldID furl = pEnv->GetFieldID(cls, "url", "Ljava/lang/String;");
51
52 D(D_WARN, "Subscriptions: %d", subscriptions.size());
53 const jobjectArray ret = (jobjectArray) pEnv->NewObjectArray(subscriptions.siz e(), cls, NULL);
Wladimir Palant 2013/09/12 11:31:14 The const modifier is somewhat misleading here and
54
55 int i = 0;
56 for (std::vector<AdblockPlus::SubscriptionPtr>::const_iterator it = subscripti ons.begin();
57 it != subscriptions.end(); it++)
58 {
59 jobject subscription = pEnv->NewObject(cls, cid);
60 pEnv->SetObjectField(subscription, ftitle, pEnv->NewStringUTF((*it)->GetProp erty("title")->AsString().c_str()));
61 pEnv->SetObjectField(subscription, furl, pEnv->NewStringUTF((*it)->GetProper ty("url")->AsString().c_str()));
62 pEnv->SetObjectArrayElement(ret, i, subscription);
63 i++;
64 }
65
66 return ret;
67 }
68
69 void UpdateSubscriptionStatus(const std::string& url)
70 {
71 D(D_WARN, "UpdateSubscriptionStatus()");
72
73 AdblockPlus::SubscriptionPtr subscription = filterEngine->GetSubscription(url) ;
74 std::string downloadStatus = subscription->GetProperty("downloadStatus")->IsNu ll() ? "" : subscription->GetProperty("downloadStatus")->AsString();
75 int64_t lastDownload = subscription->GetProperty("lastDownload")->AsInt();
76
77 std::string status = "synchronize_never";
78 int64_t time = 0;
79
80 if (subscription->IsUpdating())
81 {
82 status = "synchronize_in_progress";
83 }
84 else if (!downloadStatus.empty() && downloadStatus != "synchronize_ok")
85 {
86 status = downloadStatus;
87 }
88 else if (lastDownload > 0)
89 {
90 time = lastDownload;
91 status = "synchronize_last_at";
92 }
93
94 JNIEnv* jniEnv = NULL;
95 int stat = globalJvm->GetEnv((void **)&jniEnv, JNI_VERSION_1_6);
96 if (stat == JNI_EDETACHED)
97 {
98 if (globalJvm->AttachCurrentThread(&jniEnv, NULL) != 0)
99 throw std::runtime_error("Failed to get JNI environment");
100 }
101
102 jstring jUrl = jniEnv->NewStringUTF(url.c_str());
103 jstring jStatus = jniEnv->NewStringUTF(status.c_str());
104 jlong jTime = time * 1000;
105
106 static jclass cls = jniEnv->GetObjectClass(jniObject);
107 static jmethodID mid = jniEnv->GetMethodID(cls, "onFilterChanged", "(Ljava/lan g/String;Ljava/lang/String;J)V");
108 if (mid)
109 jniEnv->CallVoidMethod(jniObject, mid, jUrl, jStatus, jTime);
110 jniEnv->DeleteLocalRef(jUrl);
111 jniEnv->DeleteLocalRef(jStatus);
112
113 if (stat == JNI_EDETACHED)
114 globalJvm->DetachCurrentThread();
115 }
116
117 void FilterChangedCallback(const std::string& action, const std::string& url)
Wladimir Palant 2013/09/12 11:31:14 I think that in the final revision of your libadbl
118 {
119 D(D_WARN, "FilterChangedCallback()");
120
121 if (action == "subscription.lastDownload" || action == "subscription.downloadS tatus")
122 {
123 UpdateSubscriptionStatus(url);
Wladimir Palant 2013/09/12 11:31:14 Nit: Indentation is off here, need to convert a ta
124 }
125 }
126
127 void UpdateAvailableCallback(AdblockPlus::JsValueList& params)
128 {
129 //TODO Notify Java UI about available update
130 }
131
132 jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
133 {
134 return JNI_VERSION_1_6;
Wladimir Palant 2013/09/12 11:31:14 I don't think that we should hardcode the JNI vers
135 }
136
137 void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved)
138 {
139 }
140
141 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_initialize(JNIEnv *pEnv, jobject pObject, jstring basepath)
142 {
143 D(D_WARN, "nativeInitialize()");
144 int status = pEnv->GetJavaVM(&globalJvm);
145
146 jniObject = pEnv->NewGlobalRef(pObject);
147
148 const std::string path = GetString(pEnv, basepath);
149
150 AdblockPlus::AppInfo appInfo;
151 // FIXME Should be taken from Manifest
Wladimir Palant 2013/09/12 11:31:14 Please use "TODO" instead of "FIXME", this is what
152 appInfo.version = "1.1.2";
153 appInfo.name = "adblockplusandroid";
154 appInfo.platform = "android";
Wladimir Palant 2013/09/12 11:31:14 Please note that this class changed in the current
155
156 AdblockPlus::JsEnginePtr jsEngine(AdblockPlus::JsEngine::New(appInfo));
157
158 AdblockPlus::DefaultFileSystem* defaultFileSystem = new AdblockPlus::DefaultFi leSystem();
159 AndroidLogSystem* androidLogSystem = new AndroidLogSystem();
160 AndroidWebRequest* androidWebRequest = new AndroidWebRequest(globalJvm);
Wladimir Palant 2013/09/12 11:31:14 I think your should assign to a shared pointer imm
161
162 defaultFileSystem->SetBasePath(path);
Wladimir Palant 2013/09/12 11:31:14 You initialize the path variable 15 lines before i
163 jsEngine->SetLogSystem(AdblockPlus::LogSystemPtr(androidLogSystem));
164 jsEngine->SetFileSystem(AdblockPlus::FileSystemPtr(defaultFileSystem));
165 jsEngine->SetWebRequest(AdblockPlus::WebRequestPtr(androidWebRequest));
166 jsEngine->SetEventCallback("updateAvailable", std::tr1::bind(&UpdateAvailableC allback, std::tr1::placeholders::_1));
Wladimir Palant 2013/09/12 11:31:14 Why use std::tr1::bind() here instead of simply pa
167
168 filterEngine = new AdblockPlus::FilterEngine(jsEngine);
169 filterEngine->SetFilterChangeCallback(&FilterChangedCallback);
170 }
171
172 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_release(JNIEnv *pE nv, jobject)
173 {
174 D(D_WARN, "nativeRelease()");
175 AdblockPlus::JsEnginePtr jsEngine = filterEngine->GetJsEngine();
176 jsEngine->RemoveEventCallback("updateAvailable");
177 filterEngine->RemoveFilterChangeCallback();
Wladimir Palant 2013/09/12 11:31:14 I think that you can rely on FilterEngine and JsEn
178 delete filterEngine;
179 pEnv->DeleteGlobalRef(jniObject);
180 jniObject = NULL;
181 globalJvm = NULL;
182 }
183
184 JNIEXPORT jboolean JNICALL Java_org_adblockplus_android_ABPEngine_isFirstRun(JNI Env *pEnv, jobject)
185 {
186 return filterEngine->IsFirstRun() ? JNI_TRUE : JNI_FALSE;
187 }
188
189 JNIEXPORT jobjectArray JNICALL Java_org_adblockplus_android_ABPEngine_getListedS ubscriptions(JNIEnv *pEnv, jobject)
190 {
191 D(D_WARN, "getListedSubscriptions()");
192 const std::vector<AdblockPlus::SubscriptionPtr> subscriptions = filterEngine-> GetListedSubscriptions();
Wladimir Palant 2013/09/12 11:31:14 Why are you catching exceptions in the GetRecommen
193 return subscriptionsAsJavaArray(pEnv, subscriptions);
194 }
195
196 JNIEXPORT jobjectArray JNICALL Java_org_adblockplus_android_ABPEngine_getRecomme ndedSubscriptions(JNIEnv *pEnv, jobject)
197 {
198 D(D_WARN, "getRecommendedSubscriptions()");
199 try
200 {
201 const std::vector<AdblockPlus::SubscriptionPtr> subscriptions = filterEngine ->FetchAvailableSubscriptions();
202 return subscriptionsAsJavaArray(pEnv, subscriptions);
203 }
204 catch (const std::exception& e)
205 {
206 D(D_ERROR, "Exception: %s", e.what());
207 }
208 catch (...)
209 {
210 D(D_ERROR, "Unknown exception");
211 }
212 return NULL;
Wladimir Palant 2013/09/12 11:31:14 I don't think that returning a null pointer makes
213 }
214
215 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_addSubscription(JN IEnv *pEnv, jobject, jstring url)
216 {
217 D(D_WARN, "addSubscription()");
218 const std::string surl = GetString(pEnv, url);
219 AdblockPlus::SubscriptionPtr subscription = filterEngine->GetSubscription(surl );
220 subscription->AddToList();
221 }
222
223 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_removeSubscription (JNIEnv *pEnv, jobject, jstring url)
224 {
225 D(D_WARN, "removeSubscription()");
226 const std::string surl = GetString(pEnv, url);
227 AdblockPlus::SubscriptionPtr subscription = filterEngine->GetSubscription(surl );
228 if (subscription->IsListed())
Wladimir Palant 2013/09/12 11:31:14 Calling IsListed() is unnecessary, that duplicates
229 {
230 subscription->RemoveFromList();
231 }
232 }
233
234 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_refreshSubscriptio n(JNIEnv *pEnv, jobject, jstring url)
235 {
236 D(D_WARN, "refreshSubscription()");
237 const std::string surl = GetString(pEnv, url);
238 AdblockPlus::SubscriptionPtr subscription = filterEngine->GetSubscription(surl );
239 subscription->UpdateFilters();
240 }
241
242 JNIEXPORT void JNICALL Java_org_adblockplus_android_ABPEngine_actualizeSubscript ionStatus(JNIEnv *pEnv, jobject, jstring url)
Wladimir Palant 2013/09/12 11:31:14 Nit: I don't think that "actualize" is really what
243 {
244 D(D_WARN, "actualizeSubscriptionStatus()");
245 const std::string surl = GetString(pEnv, url);
246 UpdateSubscriptionStatus(surl);
247 }
248
249 JNIEXPORT jboolean JNICALL Java_org_adblockplus_android_ABPEngine_matches(JNIEnv *pEnv, jobject, jstring url, jstring contentType, jstring documentUrl)
250 {
251 const std::string surl = GetString(pEnv, url);
252 const std::string stype = GetString(pEnv, contentType);
253 const std::string sdoc = GetString(pEnv, documentUrl);
254
255 AdblockPlus::FilterPtr filter = filterEngine->Matches(surl, stype, sdoc);
256
257 if (! filter)
258 return JNI_FALSE;
259
260 // hack: if there is no referrer block only if filter is domain-specific
261 // (to re-enable in-app ads blocking, proposed on 12.11.2012 Monday meeting)
262 // documentUrl contains the referrer (Android application special case)
263 if (sdoc.empty() && (filter->GetProperty("text")->AsString()).find("||") != st d::string::npos)
Wladimir Palant 2013/09/12 11:31:14 Nit: the parentheses around filter->GetProperty("t
Felix Dahlke 2013/11/07 18:27:23 I don't really get what this is supposed to do - w
Wladimir Palant 2013/11/08 06:16:30 As the comment says, it looks for domain-specific
264 return JNI_FALSE;
265
266 return filter->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION ? JNI_FALSE : JNI_TRUE;
267 }
268
269 JNIEXPORT jobjectArray JNICALL Java_org_adblockplus_android_ABPEngine_getSelecto rsForDomain(JNIEnv *pEnv, jobject, jstring domain)
270 {
271 const std::string sdomain = GetString(pEnv, domain);
272 const std::vector<std::string> selectors = filterEngine->GetElementHidingSelec tors(sdomain);
273
274 static jclass cls = reinterpret_cast<jclass>(pEnv->NewGlobalRef(pEnv->FindClas s("java/lang/String")));
275
276 D(D_WARN, "Selectors: %d", selectors.size());
277 const jobjectArray ret = (jobjectArray) pEnv->NewObjectArray(selectors.size(), cls, NULL);
278
279 int i = 0;
280 for (std::vector<std::string>::const_iterator it = selectors.begin();
281 it != selectors.end(); it++)
282 {
283 jstring selector = pEnv->NewStringUTF((*it).c_str());
284 pEnv->SetObjectArrayElement(ret, i, selector);
285 pEnv->DeleteLocalRef(selector);
286 i++;
287 }
288
289 return ret;
290 }
291
292
OLDNEW

Powered by Google App Engine
This is Rietveld