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

Delta Between Two Patch Sets: src/FilterEngine.cpp

Issue 29366747: Issue 4657 - Add Acceptable Ads API (Closed)
Left Patch Set: Created Dec. 2, 2016, 4:27 p.m.
Right Patch Set: fix typo Created April 5, 2017, 4:53 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « lib/compat.js ('k') | test/FilterEngine.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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-2016 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <algorithm> 18 #include <algorithm>
19 #include <cctype> 19 #include <cctype>
20 #include <functional> 20 #include <functional>
21 #include <string> 21 #include <string>
22 22
23 #include <AdblockPlus.h> 23 #include <AdblockPlus.h>
24 #include "JsContext.h" 24 #include "JsContext.h"
25 #include "Thread.h" 25 #include "Thread.h"
26 #include <mutex>
27 #include <condition_variable>
26 28
27 using namespace AdblockPlus; 29 using namespace AdblockPlus;
28 30
29 extern std::string jsSources[]; 31 extern std::string jsSources[];
30 32
31 Filter::Filter(JsValue&& value) 33 Filter::Filter(JsValue&& value)
32 : JsValue(std::move(value)) 34 : JsValue(std::move(value))
33 { 35 {
34 if (!IsObject()) 36 if (!IsObject())
35 throw std::runtime_error("JavaScript value is not an object"); 37 throw std::runtime_error("JavaScript value is not an object");
36 } 38 }
37 39
38 Filter::Type Filter::GetType() 40 Filter::Type Filter::GetType() const
39 { 41 {
40 std::string className = GetClass(); 42 std::string className = GetClass();
41 if (className == "BlockingFilter") 43 if (className == "BlockingFilter")
42 return TYPE_BLOCKING; 44 return TYPE_BLOCKING;
43 else if (className == "WhitelistFilter") 45 else if (className == "WhitelistFilter")
44 return TYPE_EXCEPTION; 46 return TYPE_EXCEPTION;
45 else if (className == "ElemHideFilter") 47 else if (className == "ElemHideFilter")
46 return TYPE_ELEMHIDE; 48 return TYPE_ELEMHIDE;
47 else if (className == "ElemHideException") 49 else if (className == "ElemHideException")
48 return TYPE_ELEMHIDE_EXCEPTION; 50 return TYPE_ELEMHIDE_EXCEPTION;
49 else if (className == "CommentFilter") 51 else if (className == "CommentFilter")
50 return TYPE_COMMENT; 52 return TYPE_COMMENT;
51 else 53 else
52 return TYPE_INVALID; 54 return TYPE_INVALID;
53 } 55 }
54 56
55 bool Filter::IsListed() 57 bool Filter::IsListed() const
56 { 58 {
57 JsValuePtr func = jsEngine->Evaluate("API.isListedFilter"); 59 JsValuePtr func = jsEngine->Evaluate("API.isListedFilter");
58 JsValueList params; 60 return func->Call(*this).AsBool();
59 params.push_back(shared_from_this());
60 return func->Call(params)->AsBool();
61 } 61 }
62 62
63 void Filter::AddToList() 63 void Filter::AddToList()
64 { 64 {
65 JsValuePtr func = jsEngine->Evaluate("API.addFilterToList"); 65 JsValuePtr func = jsEngine->Evaluate("API.addFilterToList");
66 JsValueList params; 66 func->Call(*this);
67 params.push_back(shared_from_this());
68 func->Call(params);
69 } 67 }
70 68
71 void Filter::RemoveFromList() 69 void Filter::RemoveFromList()
72 { 70 {
73 JsValuePtr func = jsEngine->Evaluate("API.removeFilterFromList"); 71 JsValuePtr func = jsEngine->Evaluate("API.removeFilterFromList");
74 JsValueList params; 72 func->Call(*this);
75 params.push_back(shared_from_this());
76 func->Call(params);
77 } 73 }
78 74
79 bool Filter::operator==(const Filter& filter) const 75 bool Filter::operator==(const Filter& filter) const
80 { 76 {
81 return GetProperty("text")->AsString() == filter.GetProperty("text")->AsString (); 77 return GetProperty("text")->AsString() == filter.GetProperty("text")->AsString ();
82 } 78 }
83 79
84 Subscription::Subscription(JsValue&& value) 80 Subscription::Subscription(JsValue&& value)
85 : JsValue(std::move(value)) 81 : JsValue(std::move(value))
86 { 82 {
87 if (!IsObject()) 83 if (!IsObject())
88 throw std::runtime_error("JavaScript value is not an object"); 84 throw std::runtime_error("JavaScript value is not an object");
89 } 85 }
90 86
91 bool Subscription::IsListed() 87 bool Subscription::IsListed() const
92 { 88 {
93 JsValuePtr func = jsEngine->Evaluate("API.isListedSubscription"); 89 JsValuePtr func = jsEngine->Evaluate("API.isListedSubscription");
94 JsValueList params; 90 return func->Call(*this).AsBool();
95 params.push_back(shared_from_this());
96 return func->Call(params)->AsBool();
97 } 91 }
98 92
99 void Subscription::AddToList() 93 void Subscription::AddToList()
100 { 94 {
101 JsValuePtr func = jsEngine->Evaluate("API.addSubscriptionToList"); 95 JsValuePtr func = jsEngine->Evaluate("API.addSubscriptionToList");
102 JsValueList params; 96 func->Call(*this);
103 params.push_back(shared_from_this());
104 func->Call(params);
105 } 97 }
106 98
107 void Subscription::RemoveFromList() 99 void Subscription::RemoveFromList()
108 { 100 {
109 JsValuePtr func = jsEngine->Evaluate("API.removeSubscriptionFromList"); 101 JsValuePtr func = jsEngine->Evaluate("API.removeSubscriptionFromList");
110 JsValueList params; 102 func->Call(*this);
111 params.push_back(shared_from_this());
112 func->Call(params);
113 } 103 }
114 104
115 void Subscription::UpdateFilters() 105 void Subscription::UpdateFilters()
116 { 106 {
117 JsValuePtr func = jsEngine->Evaluate("API.updateSubscription"); 107 JsValuePtr func = jsEngine->Evaluate("API.updateSubscription");
118 JsValueList params; 108 func->Call(*this);
119 params.push_back(shared_from_this()); 109 }
120 func->Call(params); 110
121 } 111 bool Subscription::IsUpdating() const
122
123 bool Subscription::IsUpdating()
124 { 112 {
125 JsValuePtr func = jsEngine->Evaluate("API.isSubscriptionUpdating"); 113 JsValuePtr func = jsEngine->Evaluate("API.isSubscriptionUpdating");
126 JsValueList params; 114 return func->Call(*this).AsBool();
127 params.push_back(shared_from_this()); 115 }
128 JsValuePtr result = func->Call(params); 116
129 return result->AsBool(); 117 bool Subscription::IsAA() const
130 } 118 {
131 119 return jsEngine->Evaluate("API.isAASubscription")->Call(*this).AsBool();
132 bool Subscription::IsAA()
133 {
134 JsContext context(jsEngine);
135 return jsEngine->Evaluate("API.isAASubscription")->Call(*shared_from_this())-> AsBool();
136 } 120 }
137 121
138 bool Subscription::operator==(const Subscription& subscription) const 122 bool Subscription::operator==(const Subscription& subscription) const
139 { 123 {
140 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt ring(); 124 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt ring();
141 } 125 }
142 126
143 FilterEngine::FilterEngine(JsEnginePtr jsEngine, 127 namespace
144 const FilterEngine::Prefs& preconfiguredPrefs) 128 {
145 : jsEngine(jsEngine), initialized(false), firstRun(false), updateCheckId(0) 129 class Sync
146 { 130 {
147 jsEngine->SetEventCallback("_init", std::bind(&FilterEngine::InitDone, 131 public:
148 this, std::placeholders::_1)); 132 Sync()
149 133 :initialized(false)
150 {
151 // Lock the JS engine while we are loading scripts, no timeouts should fire
152 // until we are done.
153 const JsContext context(jsEngine);
154
155 // Set the preconfigured prefs
156 JsValuePtr preconfiguredPrefsObject = jsEngine->NewObject();
157 for (FilterEngine::Prefs::const_iterator it = preconfiguredPrefs.begin();
158 it != preconfiguredPrefs.end(); it++)
159 { 134 {
160 preconfiguredPrefsObject->SetProperty(it->first, it->second); 135
161 } 136 }
162 jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject) ; 137 void Wait()
163 // Load adblockplus scripts 138 {
164 for (int i = 0; !jsSources[i].empty(); i += 2) 139 std::unique_lock<std::mutex> lock(mutex);
165 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); 140 while (!initialized)
166 } 141 cv.wait(lock);
167 142 }
168 // TODO: This should really be implemented via a conditional variable 143 void Set()
169 while (!initialized) 144 {
170 ::Sleep(10); 145 {
146 std::unique_lock<std::mutex> lock(mutex);
147 initialized = true;
148 }
149 cv.notify_all();
150 }
151 private:
152 std::mutex mutex;
153 std::condition_variable cv;
154 bool initialized;
155 };
156 }
157
158 FilterEngine::FilterEngine(const JsEnginePtr& jsEngine)
159 : jsEngine(jsEngine), firstRun(false), updateCheckId(0)
160 {
161 }
162
163 void FilterEngine::CreateAsync(const JsEnginePtr& jsEngine,
164 const FilterEngine::OnCreatedCallback& onCreated,
165 const FilterEngine::CreationParameters& params)
166 {
167 FilterEnginePtr filterEngine(new FilterEngine(jsEngine));
168 auto sync = std::make_shared<Sync>();
169 auto isConnectionAllowedCallback = params.isConnectionAllowedCallback;
170 if (isConnectionAllowedCallback)
171 jsEngine->SetIsConnectionAllowedCallback([sync, jsEngine]()->bool
172 {
173 sync->Wait();
174 return jsEngine->IsConnectionAllowed();
175 });
176 jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated, sync, isConnectionAllowedCallback](JsValueList& params)
177 {
178 filterEngine->firstRun = params.size() && params[0]->AsBool();
179 if (isConnectionAllowedCallback)
180 {
181 std::weak_ptr<FilterEngine> weakFilterEngine = filterEngine;
182 jsEngine->SetIsConnectionAllowedCallback([weakFilterEngine, isConnectionAl lowedCallback]()->bool
183 {
184 auto filterEngine = weakFilterEngine.lock();
185 if (!filterEngine)
186 return false;
187 return isConnectionAllowedCallback(filterEngine->GetAllowedConnectionTyp e().get());
188 });
189 }
190 sync->Set();
191 onCreated(filterEngine);
192 jsEngine->RemoveEventCallback("_init");
193 });
194
195 // Lock the JS engine while we are loading scripts, no timeouts should fire
196 // until we are done.
197 const JsContext context(jsEngine);
198
199 // Set the preconfigured prefs
200 JsValuePtr preconfiguredPrefsObject = jsEngine->NewObject();
201 for (FilterEngine::Prefs::const_iterator it = params.preconfiguredPrefs.begin( );
202 it != params.preconfiguredPrefs.end(); it++)
203 {
204 preconfiguredPrefsObject->SetProperty(it->first, it->second);
205 }
206 jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject);
207 // Load adblockplus scripts
208 for (int i = 0; !jsSources[i].empty(); i += 2)
209 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]);
210 }
211
212 FilterEnginePtr FilterEngine::Create(const JsEnginePtr& jsEngine,
213 const FilterEngine::CreationParameters& params)
214 {
215 FilterEnginePtr retValue;
216 Sync sync;
217 CreateAsync(jsEngine, [&retValue, &sync](const FilterEnginePtr& filterEngine)
218 {
219 retValue = filterEngine;
220 sync.Set();
221 }, params);
222 sync.Wait();
223 return retValue;
171 } 224 }
172 225
173 namespace 226 namespace
174 { 227 {
175 typedef std::map<FilterEngine::ContentType, std::string> ContentTypeMap; 228 typedef std::map<FilterEngine::ContentType, std::string> ContentTypeMap;
176 229
177 ContentTypeMap CreateContentTypeMap() 230 ContentTypeMap CreateContentTypeMap()
178 { 231 {
179 ContentTypeMap contentTypes; 232 ContentTypeMap contentTypes;
180 contentTypes[FilterEngine::CONTENT_TYPE_OTHER] = "OTHER"; 233 contentTypes[FilterEngine::CONTENT_TYPE_OTHER] = "OTHER";
(...skipping 22 matching lines...) Expand all
203 ContentTypeMap::const_iterator it = contentTypes.find(contentType); 256 ContentTypeMap::const_iterator it = contentTypes.find(contentType);
204 if (it != contentTypes.end()) 257 if (it != contentTypes.end())
205 return it->second; 258 return it->second;
206 throw std::invalid_argument("Argument is not a valid ContentType"); 259 throw std::invalid_argument("Argument is not a valid ContentType");
207 } 260 }
208 261
209 FilterEngine::ContentType FilterEngine::StringToContentType(const std::string& c ontentType) 262 FilterEngine::ContentType FilterEngine::StringToContentType(const std::string& c ontentType)
210 { 263 {
211 std::string contentTypeUpper = contentType; 264 std::string contentTypeUpper = contentType;
212 std::transform(contentType.begin(), contentType.end(), contentTypeUpper.begin( ), ::toupper); 265 std::transform(contentType.begin(), contentType.end(), contentTypeUpper.begin( ), ::toupper);
213 for (ContentTypeMap::const_iterator it = contentTypes.begin(); 266 for (const auto& contentType : contentTypes)
214 it != contentTypes.end(); it++) 267 {
215 { 268 if (contentType.second == contentTypeUpper)
216 if (it->second == contentTypeUpper) 269 return contentType.first;
217 return it->first;
218 } 270 }
219 throw std::invalid_argument("Cannot convert argument to ContentType"); 271 throw std::invalid_argument("Cannot convert argument to ContentType");
220 } 272 }
221 273
222 void FilterEngine::InitDone(JsValueList& params)
223 {
224 jsEngine->RemoveEventCallback("_init");
225 initialized = true;
226 firstRun = params.size() && params[0]->AsBool();
227 }
228
229 bool FilterEngine::IsFirstRun() const 274 bool FilterEngine::IsFirstRun() const
230 { 275 {
231 return firstRun; 276 return firstRun;
232 } 277 }
233 278
234 FilterPtr FilterEngine::GetFilter(const std::string& text) 279 FilterPtr FilterEngine::GetFilter(const std::string& text) const
235 { 280 {
236 JsValuePtr func = jsEngine->Evaluate("API.getFilterFromText"); 281 JsValuePtr func = jsEngine->Evaluate("API.getFilterFromText");
237 JsValueList params; 282 return FilterPtr(new Filter(func->Call(*jsEngine->NewValue(text))));
238 params.push_back(jsEngine->NewValue(text)); 283 }
239 return FilterPtr(new Filter(std::move(*func->Call(params)))); 284
240 } 285 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url) const
241
242 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url)
243 { 286 {
244 JsValuePtr func = jsEngine->Evaluate("API.getSubscriptionFromUrl"); 287 JsValuePtr func = jsEngine->Evaluate("API.getSubscriptionFromUrl");
245 JsValueList params; 288 return SubscriptionPtr(new Subscription(func->Call(*jsEngine->NewValue(url)))) ;
246 params.push_back(jsEngine->NewValue(url));
247 return SubscriptionPtr(new Subscription(std::move(*func->Call(params))));
248 } 289 }
249 290
250 std::vector<FilterPtr> FilterEngine::GetListedFilters() const 291 std::vector<FilterPtr> FilterEngine::GetListedFilters() const
251 { 292 {
252 JsValuePtr func = jsEngine->Evaluate("API.getListedFilters"); 293 JsValuePtr func = jsEngine->Evaluate("API.getListedFilters");
253 JsValueList values = func->Call()->AsList(); 294 JsValueList values = func->Call().AsList();
254 std::vector<FilterPtr> result; 295 std::vector<FilterPtr> result;
255 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) 296 for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
256 result.push_back(FilterPtr(new Filter(std::move(**it)))); 297 result.push_back(FilterPtr(new Filter(std::move(**it))));
257 return result; 298 return result;
258 } 299 }
259 300
260 std::vector<SubscriptionPtr> FilterEngine::GetListedSubscriptions() const 301 std::vector<SubscriptionPtr> FilterEngine::GetListedSubscriptions() const
261 { 302 {
262 JsValuePtr func = jsEngine->Evaluate("API.getListedSubscriptions"); 303 JsValuePtr func = jsEngine->Evaluate("API.getListedSubscriptions");
263 JsValueList values = func->Call()->AsList(); 304 JsValueList values = func->Call().AsList();
264 std::vector<SubscriptionPtr> result; 305 std::vector<SubscriptionPtr> result;
265 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) 306 for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
266 result.push_back(SubscriptionPtr(new Subscription(std::move(**it)))); 307 result.push_back(SubscriptionPtr(new Subscription(std::move(**it))));
267 return result; 308 return result;
268 } 309 }
269 310
270 std::vector<SubscriptionPtr> FilterEngine::FetchAvailableSubscriptions() const 311 std::vector<SubscriptionPtr> FilterEngine::FetchAvailableSubscriptions() const
271 { 312 {
272 JsValuePtr func = jsEngine->Evaluate("API.getRecommendedSubscriptions"); 313 JsValuePtr func = jsEngine->Evaluate("API.getRecommendedSubscriptions");
273 JsValueList values = func->Call()->AsList(); 314 JsValueList values = func->Call().AsList();
274 std::vector<SubscriptionPtr> result; 315 std::vector<SubscriptionPtr> result;
275 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) 316 for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
276 result.push_back(SubscriptionPtr(new Subscription(std::move(**it)))); 317 result.push_back(SubscriptionPtr(new Subscription(std::move(**it))));
277 return result; 318 return result;
278 } 319 }
279 320
280 void FilterEngine::SetAAEnabled(bool enabled) 321 void FilterEngine::SetAAEnabled(bool enabled)
281 { 322 {
282 jsEngine->Evaluate("API.setAASubscriptionEnabled")->Call(*jsEngine->NewValue(e nabled)); 323 jsEngine->Evaluate("API.setAASubscriptionEnabled")->Call(*jsEngine->NewValue(e nabled));
Eric 2016/12/05 14:40:58 Why is there a context instantiated at line 134 bu
sergei 2017/03/17 15:55:25 JsContext context(jsEngine); is removed on line 13
283 } 324 }
284 325
285 bool FilterEngine::IsAAEnabled() const 326 bool FilterEngine::IsAAEnabled() const
286 { 327 {
287 return jsEngine->Evaluate("API.isAASubscriptionEnabled()")->AsBool(); 328 return jsEngine->Evaluate("API.isAASubscriptionEnabled()")->AsBool();
288 } 329 }
289 330
290 std::string FilterEngine::GetAAURL() const 331 std::string FilterEngine::GetAAUrl() const
291 { 332 {
292 return GetPref("subscriptions_exceptionsurl")->AsString(); 333 return GetPref("subscriptions_exceptionsurl")->AsString();
293 } 334 }
294 335
295 void FilterEngine::ShowNextNotification(const std::string& url) 336 void FilterEngine::ShowNextNotification(const std::string& url)
296 { 337 {
297 JsValuePtr func = jsEngine->Evaluate("API.showNextNotification"); 338 JsValuePtr func = jsEngine->Evaluate("API.showNextNotification");
298 JsValueList params; 339 JsConstValueList params;
299 if (!url.empty()) 340 if (!url.empty())
300 { 341 {
301 params.push_back(jsEngine->NewValue(url)); 342 params.push_back(jsEngine->NewValue(url));
302 } 343 }
303 func->Call(params); 344 func->Call(params);
304 } 345 }
305 346
306 void FilterEngine::SetShowNotificationCallback(const ShowNotificationCallback& v alue) 347 void FilterEngine::SetShowNotificationCallback(const ShowNotificationCallback& v alue)
307 { 348 {
308 if (!value) 349 if (!value)
(...skipping 19 matching lines...) Expand all
328 } 369 }
329 370
330 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, 371 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url,
331 ContentTypeMask contentTypeMask, 372 ContentTypeMask contentTypeMask,
332 const std::vector<std::string>& documentUrls) const 373 const std::vector<std::string>& documentUrls) const
333 { 374 {
334 if (documentUrls.empty()) 375 if (documentUrls.empty())
335 return CheckFilterMatch(url, contentTypeMask, ""); 376 return CheckFilterMatch(url, contentTypeMask, "");
336 377
337 std::string lastDocumentUrl = documentUrls.front(); 378 std::string lastDocumentUrl = documentUrls.front();
338 for (std::vector<std::string>::const_iterator it = documentUrls.begin(); 379 for (const auto& documentUrl : documentUrls) {
339 it != documentUrls.end(); it++) {
340 const std::string documentUrl = *it;
341 AdblockPlus::FilterPtr match = CheckFilterMatch(documentUrl, 380 AdblockPlus::FilterPtr match = CheckFilterMatch(documentUrl,
342 CONTENT_TYPE_DOCUMENT, 381 CONTENT_TYPE_DOCUMENT,
343 lastDocumentUrl); 382 lastDocumentUrl);
344 if (match && match->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION) 383 if (match && match->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION)
345 return match; 384 return match;
346 lastDocumentUrl = documentUrl; 385 lastDocumentUrl = documentUrl;
347 } 386 }
348 387
349 return CheckFilterMatch(url, contentTypeMask, lastDocumentUrl); 388 return CheckFilterMatch(url, contentTypeMask, lastDocumentUrl);
350 } 389 }
351 390
352 bool FilterEngine::IsDocumentWhitelisted(const std::string& url, 391 bool FilterEngine::IsDocumentWhitelisted(const std::string& url,
353 const std::vector<std::string>& documentUrls) const 392 const std::vector<std::string>& documentUrls) const
354 { 393 {
355 return !!GetWhitelistingFilter(url, CONTENT_TYPE_DOCUMENT, documentUrls); 394 return !!GetWhitelistingFilter(url, CONTENT_TYPE_DOCUMENT, documentUrls);
356 } 395 }
357 396
358 bool FilterEngine::IsElemhideWhitelisted(const std::string& url, 397 bool FilterEngine::IsElemhideWhitelisted(const std::string& url,
359 const std::vector<std::string>& documentUrls) const 398 const std::vector<std::string>& documentUrls) const
360 { 399 {
361 return !!GetWhitelistingFilter(url, CONTENT_TYPE_ELEMHIDE, documentUrls); 400 return !!GetWhitelistingFilter(url, CONTENT_TYPE_ELEMHIDE, documentUrls);
362 } 401 }
363 402
364 AdblockPlus::FilterPtr FilterEngine::CheckFilterMatch(const std::string& url, 403 AdblockPlus::FilterPtr FilterEngine::CheckFilterMatch(const std::string& url,
365 ContentTypeMask contentTypeMask, 404 ContentTypeMask contentTypeMask,
366 const std::string& documentUrl) const 405 const std::string& documentUrl) const
367 { 406 {
368 JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch"); 407 JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch");
369 JsValueList params; 408 JsConstValueList params;
370 params.push_back(jsEngine->NewValue(url)); 409 params.push_back(jsEngine->NewValue(url));
371 params.push_back(jsEngine->NewValue(contentTypeMask)); 410 params.push_back(jsEngine->NewValue(contentTypeMask));
372 params.push_back(jsEngine->NewValue(documentUrl)); 411 params.push_back(jsEngine->NewValue(documentUrl));
373 JsValuePtr result = func->Call(params); 412 JsValue result = func->Call(params);
374 if (!result->IsNull()) 413 if (!result.IsNull())
375 return FilterPtr(new Filter(std::move(*result))); 414 return FilterPtr(new Filter(std::move(result)));
376 else 415 else
377 return FilterPtr(); 416 return FilterPtr();
378 } 417 }
379 418
380 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri ng& domain) const 419 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri ng& domain) const
381 { 420 {
382 JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors"); 421 JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors");
383 JsValueList params; 422 JsValueList result = func->Call(*jsEngine->NewValue(domain)).AsList();
384 params.push_back(jsEngine->NewValue(domain));
385 JsValueList result = func->Call(params)->AsList();
386 std::vector<std::string> selectors; 423 std::vector<std::string> selectors;
387 for (JsValueList::iterator it = result.begin(); it != result.end(); ++it) 424 for (const auto& r: result)
388 selectors.push_back((*it)->AsString()); 425 selectors.push_back(r->AsString());
389 return selectors; 426 return selectors;
390 } 427 }
391 428
392 JsValuePtr FilterEngine::GetPref(const std::string& pref) const 429 JsValuePtr FilterEngine::GetPref(const std::string& pref) const
393 { 430 {
394 JsValuePtr func = jsEngine->Evaluate("API.getPref"); 431 JsValuePtr func = jsEngine->Evaluate("API.getPref");
395 JsValueList params; 432 return std::make_shared<JsValue>(func->Call(*jsEngine->NewValue(pref)));
433 }
434
435 void FilterEngine::SetPref(const std::string& pref, JsValuePtr value)
436 {
437 JsValuePtr func = jsEngine->Evaluate("API.setPref");
438 JsConstValueList params;
396 params.push_back(jsEngine->NewValue(pref)); 439 params.push_back(jsEngine->NewValue(pref));
397 return func->Call(params); 440 if (value)
398 } 441 params.push_back(value);
399
400 void FilterEngine::SetPref(const std::string& pref, JsValuePtr value)
401 {
402 JsValuePtr func = jsEngine->Evaluate("API.setPref");
403 JsValueList params;
404 params.push_back(jsEngine->NewValue(pref));
405 params.push_back(value);
406 func->Call(params); 442 func->Call(params);
407 } 443 }
408 444
409 std::string FilterEngine::GetHostFromURL(const std::string& url) 445 std::string FilterEngine::GetHostFromURL(const std::string& url) const
410 { 446 {
411 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl"); 447 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl");
412 JsValueList params; 448 return func->Call(*jsEngine->NewValue(url)).AsString();
413 params.push_back(jsEngine->NewValue(url));
414 return func->Call(params)->AsString();
415 } 449 }
416 450
417 void FilterEngine::SetUpdateAvailableCallback( 451 void FilterEngine::SetUpdateAvailableCallback(
418 FilterEngine::UpdateAvailableCallback callback) 452 FilterEngine::UpdateAvailableCallback callback)
419 { 453 {
420 jsEngine->SetEventCallback("updateAvailable", 454 jsEngine->SetEventCallback("updateAvailable",
421 std::bind(&FilterEngine::UpdateAvailable, this, callback, 455 std::bind(&FilterEngine::UpdateAvailable, this, callback,
422 std::placeholders::_1)); 456 std::placeholders::_1));
423 } 457 }
424 458
425 void FilterEngine::RemoveUpdateAvailableCallback() 459 void FilterEngine::RemoveUpdateAvailableCallback()
426 { 460 {
427 jsEngine->RemoveEventCallback("updateAvailable"); 461 jsEngine->RemoveEventCallback("updateAvailable");
428 } 462 }
429 463
430 void FilterEngine::UpdateAvailable( 464 void FilterEngine::UpdateAvailable(
431 FilterEngine::UpdateAvailableCallback callback, JsValueList& params) 465 FilterEngine::UpdateAvailableCallback callback, JsValueList& params)
432 { 466 {
433 if (params.size() >= 1 && !params[0]->IsNull()) 467 if (params.size() >= 1 && !params[0]->IsNull())
434 callback(params[0]->AsString()); 468 callback(params[0]->AsString());
435 } 469 }
436 470
437 void FilterEngine::ForceUpdateCheck( 471 void FilterEngine::ForceUpdateCheck(
438 FilterEngine::UpdateCheckDoneCallback callback) 472 const FilterEngine::UpdateCheckDoneCallback& callback)
439 { 473 {
440 std::string eventName = "_updateCheckDone"; 474 JsValuePtr func = jsEngine->Evaluate("API.forceUpdateCheck");
441 eventName += ++updateCheckId; 475 JsConstValueList params;
442 476 if (callback)
443 jsEngine->SetEventCallback(eventName, std::bind(&FilterEngine::UpdateCheckDone , 477 {
478 std::string eventName = "_updateCheckDone" + std::to_string(++updateCheckId) ;
479 jsEngine->SetEventCallback(eventName, std::bind(&FilterEngine::UpdateCheckDo ne,
444 this, eventName, callback, std::placeholders::_1)); 480 this, eventName, callback, std::placeholders::_1));
445 481 params.push_back(jsEngine->NewValue(eventName));
446 JsValuePtr func = jsEngine->Evaluate("API.forceUpdateCheck"); 482 }
447 JsValueList params;
448 params.push_back(jsEngine->NewValue(eventName));
449 func->Call(params); 483 func->Call(params);
450 } 484 }
451 485
452 void FilterEngine::UpdateCheckDone(const std::string& eventName, 486 void FilterEngine::UpdateCheckDone(const std::string& eventName,
453 FilterEngine::UpdateCheckDoneCallback callback, JsValueList& params) 487 FilterEngine::UpdateCheckDoneCallback callback, JsValueList& params)
454 { 488 {
455 jsEngine->RemoveEventCallback(eventName); 489 jsEngine->RemoveEventCallback(eventName);
456 490
457 std::string error(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsSt ring() : ""); 491 std::string error(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsSt ring() : "");
458 callback(error); 492 callback(error);
459 } 493 }
460 494
461 void FilterEngine::SetFilterChangeCallback(FilterEngine::FilterChangeCallback ca llback) 495 void FilterEngine::SetFilterChangeCallback(FilterEngine::FilterChangeCallback ca llback)
462 { 496 {
463 jsEngine->SetEventCallback("filterChange", std::bind(&FilterEngine::FilterChan ged, 497 jsEngine->SetEventCallback("filterChange", std::bind(&FilterEngine::FilterChan ged,
464 this, callback, std::placeholders::_1)); 498 this, callback, std::placeholders::_1));
465 } 499 }
466 500
467 void FilterEngine::RemoveFilterChangeCallback() 501 void FilterEngine::RemoveFilterChangeCallback()
468 { 502 {
469 jsEngine->RemoveEventCallback("filterChange"); 503 jsEngine->RemoveEventCallback("filterChange");
504 }
505
506 void FilterEngine::SetAllowedConnectionType(const std::string* value)
507 {
508 SetPref("allowed_connection_type", value ? jsEngine->NewValue(*value) : jsEngi ne->NewValue(""));
509 }
510
511 std::unique_ptr<std::string> FilterEngine::GetAllowedConnectionType() const
512 {
513 auto prefValue = GetPref("allowed_connection_type");
514 if (prefValue->AsString().empty())
515 return nullptr;
516 return std::unique_ptr<std::string>(new std::string(prefValue->AsString()));
470 } 517 }
471 518
472 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js ValueList& params) 519 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js ValueList& params)
473 { 520 {
474 std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsS tring() : ""); 521 std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsS tring() : "");
475 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false)); 522 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false));
476 callback(action, item); 523 callback(action, item);
477 } 524 }
478 525
479 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback, 526 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback,
480 const JsValueList& params) 527 const JsValueList& params)
481 { 528 {
482 if (params.size() < 1) 529 if (params.size() < 1)
483 return; 530 return;
484 531
485 if (!params[0]->IsObject()) 532 if (!params[0]->IsObject())
486 { 533 {
487 return; 534 return;
488 } 535 }
489 callback(NotificationPtr(new Notification(std::move(*params[0])))); 536 callback(NotificationPtr(new Notification(std::move(*params[0]))));
490 } 537 }
491 538
492 539
493 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) 540 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) const
494 { 541 {
495 JsValueList params; 542 JsConstValueList params;
496 params.push_back(jsEngine->NewValue(v1)); 543 params.push_back(jsEngine->NewValue(v1));
497 params.push_back(jsEngine->NewValue(v2)); 544 params.push_back(jsEngine->NewValue(v2));
498 JsValuePtr func = jsEngine->Evaluate("API.compareVersions"); 545 JsValuePtr func = jsEngine->Evaluate("API.compareVersions");
499 return func->Call(params)->AsInt(); 546 return func->Call(params).AsInt();
500 } 547 }
501 548
502 FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url, 549 FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url,
503 ContentTypeMask contentTypeMask, const std::string& documentUrl) const 550 ContentTypeMask contentTypeMask, const std::string& documentUrl) const
504 { 551 {
505 FilterPtr match = Matches(url, contentTypeMask, documentUrl); 552 FilterPtr match = Matches(url, contentTypeMask, documentUrl);
506 if (match && match->GetType() == Filter::TYPE_EXCEPTION) 553 if (match && match->GetType() == Filter::TYPE_EXCEPTION)
507 { 554 {
508 return match; 555 return match;
509 } 556 }
(...skipping 17 matching lines...) Expand all
527 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url); 574 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url);
528 if (filter) 575 if (filter)
529 { 576 {
530 return filter; 577 return filter;
531 } 578 }
532 currentUrl = parentUrl; 579 currentUrl = parentUrl;
533 } 580 }
534 while (urlIterator != documentUrls.end()); 581 while (urlIterator != documentUrls.end());
535 return FilterPtr(); 582 return FilterPtr();
536 } 583 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld