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

Delta Between Two Patch Sets: src/FilterEngine.cpp

Issue 5598762307158016: Issue 1550 - Get rid of V8ValueHolder.h (Closed)
Left Patch Set: rebase Created Nov. 13, 2015, 1:54 p.m.
Right Patch Set: rebase Created May 20, 2016, 3:20 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 | « include/AdblockPlus/V8ValueHolder.h ('k') | src/JsContext.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-2015 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 *
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 params.push_back(shared_from_this()); 127 params.push_back(shared_from_this());
128 JsValuePtr result = func->Call(params); 128 JsValuePtr result = func->Call(params);
129 return result->AsBool(); 129 return result->AsBool();
130 } 130 }
131 131
132 bool Subscription::operator==(const Subscription& subscription) const 132 bool Subscription::operator==(const Subscription& subscription) const
133 { 133 {
134 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt ring(); 134 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt ring();
135 } 135 }
136 136
137 FilterEngine::FilterEngine(JsEnginePtr jsEngine, 137 FilterEngine::FilterEngine(JsEnginePtr jsEngine,
138 const FilterEngine::Prefs& preconfiguredPrefs) 138 const FilterEngine::Prefs& preconfiguredPrefs)
139 : jsEngine(jsEngine), initialized(false), firstRun(false), updateCheckId(0) 139 : jsEngine(jsEngine), initialized(false), firstRun(false), updateCheckId(0)
140 { 140 {
141 jsEngine->SetEventCallback("_init", std::bind(&FilterEngine::InitDone, 141 jsEngine->SetEventCallback("_init", std::bind(&FilterEngine::InitDone,
142 this, std::placeholders::_1)); 142 this, std::placeholders::_1));
143 143
144 { 144 {
145 // Lock the JS engine while we are loading scripts, no timeouts should fire 145 // Lock the JS engine while we are loading scripts, no timeouts should fire
146 // until we are done. 146 // until we are done.
147 const JsContext context(jsEngine); 147 const JsContext context(jsEngine);
(...skipping 23 matching lines...) Expand all
171 ContentTypeMap CreateContentTypeMap() 171 ContentTypeMap CreateContentTypeMap()
172 { 172 {
173 ContentTypeMap contentTypes; 173 ContentTypeMap contentTypes;
174 contentTypes[FilterEngine::CONTENT_TYPE_OTHER] = "OTHER"; 174 contentTypes[FilterEngine::CONTENT_TYPE_OTHER] = "OTHER";
175 contentTypes[FilterEngine::CONTENT_TYPE_SCRIPT] = "SCRIPT"; 175 contentTypes[FilterEngine::CONTENT_TYPE_SCRIPT] = "SCRIPT";
176 contentTypes[FilterEngine::CONTENT_TYPE_IMAGE] = "IMAGE"; 176 contentTypes[FilterEngine::CONTENT_TYPE_IMAGE] = "IMAGE";
177 contentTypes[FilterEngine::CONTENT_TYPE_STYLESHEET] = "STYLESHEET"; 177 contentTypes[FilterEngine::CONTENT_TYPE_STYLESHEET] = "STYLESHEET";
178 contentTypes[FilterEngine::CONTENT_TYPE_OBJECT] = "OBJECT"; 178 contentTypes[FilterEngine::CONTENT_TYPE_OBJECT] = "OBJECT";
179 contentTypes[FilterEngine::CONTENT_TYPE_SUBDOCUMENT] = "SUBDOCUMENT"; 179 contentTypes[FilterEngine::CONTENT_TYPE_SUBDOCUMENT] = "SUBDOCUMENT";
180 contentTypes[FilterEngine::CONTENT_TYPE_DOCUMENT] = "DOCUMENT"; 180 contentTypes[FilterEngine::CONTENT_TYPE_DOCUMENT] = "DOCUMENT";
181 contentTypes[FilterEngine::CONTENT_TYPE_PING] = "PING";
181 contentTypes[FilterEngine::CONTENT_TYPE_XMLHTTPREQUEST] = "XMLHTTPREQUEST"; 182 contentTypes[FilterEngine::CONTENT_TYPE_XMLHTTPREQUEST] = "XMLHTTPREQUEST";
182 contentTypes[FilterEngine::CONTENT_TYPE_OBJECT_SUBREQUEST] = "OBJECT_SUBREQU EST"; 183 contentTypes[FilterEngine::CONTENT_TYPE_OBJECT_SUBREQUEST] = "OBJECT_SUBREQU EST";
183 contentTypes[FilterEngine::CONTENT_TYPE_FONT] = "FONT"; 184 contentTypes[FilterEngine::CONTENT_TYPE_FONT] = "FONT";
184 contentTypes[FilterEngine::CONTENT_TYPE_MEDIA] = "MEDIA"; 185 contentTypes[FilterEngine::CONTENT_TYPE_MEDIA] = "MEDIA";
185 contentTypes[FilterEngine::CONTENT_TYPE_ELEMHIDE] = "ELEMHIDE"; 186 contentTypes[FilterEngine::CONTENT_TYPE_ELEMHIDE] = "ELEMHIDE";
187 contentTypes[FilterEngine::CONTENT_TYPE_GENERICBLOCK] = "GENERICBLOCK";
188 contentTypes[FilterEngine::CONTENT_TYPE_GENERICHIDE] = "GENERICHIDE";
186 return contentTypes; 189 return contentTypes;
187 } 190 }
188 } 191 }
189 192
190 const ContentTypeMap FilterEngine::contentTypes = CreateContentTypeMap(); 193 const ContentTypeMap FilterEngine::contentTypes = CreateContentTypeMap();
191 194
192 std::string FilterEngine::ContentTypeToString(ContentType contentType) 195 std::string FilterEngine::ContentTypeToString(ContentType contentType)
193 { 196 {
194 ContentTypeMap::const_iterator it = contentTypes.find(contentType); 197 ContentTypeMap::const_iterator it = contentTypes.find(contentType);
195 if (it != contentTypes.end()) 198 if (it != contentTypes.end())
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 std::bind(&FilterEngine::ShowNotification, this, value, 291 std::bind(&FilterEngine::ShowNotification, this, value,
289 std::placeholders::_1)); 292 std::placeholders::_1));
290 } 293 }
291 294
292 void FilterEngine::RemoveShowNotificationCallback() 295 void FilterEngine::RemoveShowNotificationCallback()
293 { 296 {
294 jsEngine->RemoveEventCallback("_showNotification"); 297 jsEngine->RemoveEventCallback("_showNotification");
295 } 298 }
296 299
297 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, 300 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url,
298 ContentType contentType, 301 ContentTypeMask contentTypeMask,
299 const std::string& documentUrl) const 302 const std::string& documentUrl) const
300 { 303 {
301 std::vector<std::string> documentUrls; 304 std::vector<std::string> documentUrls;
302 documentUrls.push_back(documentUrl); 305 documentUrls.push_back(documentUrl);
303 return Matches(url, contentType, documentUrls); 306 return Matches(url, contentTypeMask, documentUrls);
304 } 307 }
305 308
306 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, 309 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url,
307 ContentType contentType, 310 ContentTypeMask contentTypeMask,
308 const std::vector<std::string>& documentUrls) const 311 const std::vector<std::string>& documentUrls) const
309 { 312 {
310 if (documentUrls.empty()) 313 if (documentUrls.empty())
311 return CheckFilterMatch(url, contentType, ""); 314 return CheckFilterMatch(url, contentTypeMask, "");
312 315
313 std::string lastDocumentUrl = documentUrls.front(); 316 std::string lastDocumentUrl = documentUrls.front();
314 for (std::vector<std::string>::const_iterator it = documentUrls.begin(); 317 for (std::vector<std::string>::const_iterator it = documentUrls.begin();
315 it != documentUrls.end(); it++) { 318 it != documentUrls.end(); it++) {
316 const std::string documentUrl = *it; 319 const std::string documentUrl = *it;
317 AdblockPlus::FilterPtr match = CheckFilterMatch(documentUrl, 320 AdblockPlus::FilterPtr match = CheckFilterMatch(documentUrl,
318 CONTENT_TYPE_DOCUMENT, 321 CONTENT_TYPE_DOCUMENT,
319 lastDocumentUrl); 322 lastDocumentUrl);
320 if (match && match->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION) 323 if (match && match->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION)
321 return match; 324 return match;
322 lastDocumentUrl = documentUrl; 325 lastDocumentUrl = documentUrl;
323 } 326 }
324 327
325 return CheckFilterMatch(url, contentType, lastDocumentUrl); 328 return CheckFilterMatch(url, contentTypeMask, lastDocumentUrl);
329 }
330
331 bool FilterEngine::IsDocumentWhitelisted(const std::string& url,
332 const std::vector<std::string>& documentUrls) const
333 {
334 return !!GetWhitelistingFilter(url, CONTENT_TYPE_DOCUMENT, documentUrls);
335 }
336
337 bool FilterEngine::IsElemhideWhitelisted(const std::string& url,
338 const std::vector<std::string>& documentUrls) const
339 {
340 return !!GetWhitelistingFilter(url, CONTENT_TYPE_ELEMHIDE, documentUrls);
326 } 341 }
327 342
328 AdblockPlus::FilterPtr FilterEngine::CheckFilterMatch(const std::string& url, 343 AdblockPlus::FilterPtr FilterEngine::CheckFilterMatch(const std::string& url,
329 ContentType contentType, 344 ContentTypeMask contentTypeMask,
330 const std::string& documentUrl) const 345 const std::string& documentUrl) const
331 { 346 {
332 JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch"); 347 JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch");
333 JsValueList params; 348 JsValueList params;
334 params.push_back(jsEngine->NewValue(url)); 349 params.push_back(jsEngine->NewValue(url));
335 params.push_back(jsEngine->NewValue(ContentTypeToString(contentType))); 350 params.push_back(jsEngine->NewValue(contentTypeMask));
336 params.push_back(jsEngine->NewValue(documentUrl)); 351 params.push_back(jsEngine->NewValue(documentUrl));
337 JsValuePtr result = func->Call(params); 352 JsValuePtr result = func->Call(params);
338 if (!result->IsNull()) 353 if (!result->IsNull())
339 return FilterPtr(new Filter(std::move(*result))); 354 return FilterPtr(new Filter(std::move(*result)));
340 else 355 else
341 return FilterPtr(); 356 return FilterPtr();
342 } 357 }
343 358
344 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri ng& domain) const 359 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri ng& domain) const
345 { 360 {
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 470
456 471
457 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) 472 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2)
458 { 473 {
459 JsValueList params; 474 JsValueList params;
460 params.push_back(jsEngine->NewValue(v1)); 475 params.push_back(jsEngine->NewValue(v1));
461 params.push_back(jsEngine->NewValue(v2)); 476 params.push_back(jsEngine->NewValue(v2));
462 JsValuePtr func = jsEngine->Evaluate("API.compareVersions"); 477 JsValuePtr func = jsEngine->Evaluate("API.compareVersions");
463 return func->Call(params)->AsInt(); 478 return func->Call(params)->AsInt();
464 } 479 }
480
481 FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url,
482 ContentTypeMask contentTypeMask, const std::string& documentUrl) const
483 {
484 FilterPtr match = Matches(url, contentTypeMask, documentUrl);
485 if (match && match->GetType() == Filter::TYPE_EXCEPTION)
486 {
487 return match;
488 }
489 return FilterPtr();
490 }
491
492 FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url,
493 ContentTypeMask contentTypeMask,
494 const std::vector<std::string>& documentUrls) const
495 {
496 if (documentUrls.empty())
497 {
498 return GetWhitelistingFilter(url, contentTypeMask, "");
499 }
500
501 std::vector<std::string>::const_iterator urlIterator = documentUrls.begin();
502 std::string currentUrl = url;
503 do
504 {
505 std::string parentUrl = *urlIterator++;
506 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url);
507 if (filter)
508 {
509 return filter;
510 }
511 currentUrl = parentUrl;
512 }
513 while (urlIterator != documentUrls.end());
514 return FilterPtr();
515 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld