Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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-2017 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 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
50 return TYPE_ELEMHIDE_EXCEPTION; | 50 return TYPE_ELEMHIDE_EXCEPTION; |
51 else if (className == "CommentFilter") | 51 else if (className == "CommentFilter") |
52 return TYPE_COMMENT; | 52 return TYPE_COMMENT; |
53 else | 53 else |
54 return TYPE_INVALID; | 54 return TYPE_INVALID; |
55 } | 55 } |
56 | 56 |
57 bool Filter::IsListed() const | 57 bool Filter::IsListed() const |
58 { | 58 { |
59 JsValuePtr func = jsEngine->Evaluate("API.isListedFilter"); | 59 JsValuePtr func = jsEngine->Evaluate("API.isListedFilter"); |
60 JsConstValueList params; | 60 return func->Call(shared_from_this())->AsBool(); |
61 params.push_back(shared_from_this()); | |
62 return func->Call(params)->AsBool(); | |
sergei
2017/03/23 19:46:06
There is actually `JsValue::Call(const JsValue& ar
hub
2017/03/24 13:45:15
Now that I'm looking at it, I think that this func
sergei
2017/03/24 13:56:47
We don't need to reallocate new JsValue here.
Act
hub
2017/03/24 14:08:03
In JsValue::Call() we currently do:
params.push_b
sergei
2017/03/24 14:24:55
I think that we should simply change the implement
hub
2017/03/24 16:05:06
Agreed. Issue 3589?
sergei
2017/03/24 16:27:52
I think it should be another issue (https://issues
| |
63 } | 61 } |
64 | 62 |
65 void Filter::AddToList() | 63 void Filter::AddToList() |
66 { | 64 { |
67 JsValuePtr func = jsEngine->Evaluate("API.addFilterToList"); | 65 JsValuePtr func = jsEngine->Evaluate("API.addFilterToList"); |
68 JsConstValueList params; | 66 func->Call(shared_from_this()); |
69 params.push_back(shared_from_this()); | |
70 func->Call(params); | |
71 } | 67 } |
72 | 68 |
73 void Filter::RemoveFromList() | 69 void Filter::RemoveFromList() |
74 { | 70 { |
75 JsValuePtr func = jsEngine->Evaluate("API.removeFilterFromList"); | 71 JsValuePtr func = jsEngine->Evaluate("API.removeFilterFromList"); |
76 JsConstValueList params; | 72 func->Call(shared_from_this()); |
77 params.push_back(shared_from_this()); | |
78 func->Call(params); | |
79 } | 73 } |
80 | 74 |
81 bool Filter::operator==(const Filter& filter) const | 75 bool Filter::operator==(const Filter& filter) const |
82 { | 76 { |
83 return GetProperty("text")->AsString() == filter.GetProperty("text")->AsString (); | 77 return GetProperty("text")->AsString() == filter.GetProperty("text")->AsString (); |
84 } | 78 } |
85 | 79 |
86 Subscription::Subscription(JsValue&& value) | 80 Subscription::Subscription(JsValue&& value) |
87 : JsValue(std::move(value)) | 81 : JsValue(std::move(value)) |
88 { | 82 { |
89 if (!IsObject()) | 83 if (!IsObject()) |
90 throw std::runtime_error("JavaScript value is not an object"); | 84 throw std::runtime_error("JavaScript value is not an object"); |
91 } | 85 } |
92 | 86 |
93 bool Subscription::IsListed() const | 87 bool Subscription::IsListed() const |
94 { | 88 { |
95 JsValuePtr func = jsEngine->Evaluate("API.isListedSubscription"); | 89 JsValuePtr func = jsEngine->Evaluate("API.isListedSubscription"); |
96 JsConstValueList params; | 90 return func->Call(shared_from_this())->AsBool(); |
97 params.push_back(shared_from_this()); | |
98 return func->Call(params)->AsBool(); | |
99 } | 91 } |
100 | 92 |
101 void Subscription::AddToList() | 93 void Subscription::AddToList() |
102 { | 94 { |
103 JsValuePtr func = jsEngine->Evaluate("API.addSubscriptionToList"); | 95 JsValuePtr func = jsEngine->Evaluate("API.addSubscriptionToList"); |
104 JsConstValueList params; | 96 func->Call(shared_from_this()); |
105 params.push_back(shared_from_this()); | |
106 func->Call(params); | |
107 } | 97 } |
108 | 98 |
109 void Subscription::RemoveFromList() | 99 void Subscription::RemoveFromList() |
110 { | 100 { |
111 JsValuePtr func = jsEngine->Evaluate("API.removeSubscriptionFromList"); | 101 JsValuePtr func = jsEngine->Evaluate("API.removeSubscriptionFromList"); |
112 JsConstValueList params; | 102 func->Call(shared_from_this()); |
113 params.push_back(shared_from_this()); | |
114 func->Call(params); | |
115 } | 103 } |
116 | 104 |
117 void Subscription::UpdateFilters() | 105 void Subscription::UpdateFilters() |
118 { | 106 { |
119 JsValuePtr func = jsEngine->Evaluate("API.updateSubscription"); | 107 JsValuePtr func = jsEngine->Evaluate("API.updateSubscription"); |
120 JsConstValueList params; | 108 func->Call(shared_from_this()); |
121 params.push_back(shared_from_this()); | |
122 func->Call(params); | |
123 } | 109 } |
124 | 110 |
125 bool Subscription::IsUpdating() const | 111 bool Subscription::IsUpdating() const |
126 { | 112 { |
127 JsValuePtr func = jsEngine->Evaluate("API.isSubscriptionUpdating"); | 113 JsValuePtr func = jsEngine->Evaluate("API.isSubscriptionUpdating"); |
128 JsConstValueList params; | 114 JsValuePtr result = func->Call(shared_from_this()); |
129 params.push_back(shared_from_this()); | |
130 JsValuePtr result = func->Call(params); | |
131 return result->AsBool(); | 115 return result->AsBool(); |
132 } | 116 } |
133 | 117 |
134 bool Subscription::operator==(const Subscription& subscription) const | 118 bool Subscription::operator==(const Subscription& subscription) const |
135 { | 119 { |
136 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt ring(); | 120 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt ring(); |
137 } | 121 } |
138 | 122 |
139 namespace | 123 namespace |
140 { | 124 { |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
268 ContentTypeMap::const_iterator it = contentTypes.find(contentType); | 252 ContentTypeMap::const_iterator it = contentTypes.find(contentType); |
269 if (it != contentTypes.end()) | 253 if (it != contentTypes.end()) |
270 return it->second; | 254 return it->second; |
271 throw std::invalid_argument("Argument is not a valid ContentType"); | 255 throw std::invalid_argument("Argument is not a valid ContentType"); |
272 } | 256 } |
273 | 257 |
274 FilterEngine::ContentType FilterEngine::StringToContentType(const std::string& c ontentType) | 258 FilterEngine::ContentType FilterEngine::StringToContentType(const std::string& c ontentType) |
275 { | 259 { |
276 std::string contentTypeUpper = contentType; | 260 std::string contentTypeUpper = contentType; |
277 std::transform(contentType.begin(), contentType.end(), contentTypeUpper.begin( ), ::toupper); | 261 std::transform(contentType.begin(), contentType.end(), contentTypeUpper.begin( ), ::toupper); |
278 for (ContentTypeMap::const_iterator it = contentTypes.begin(); | 262 for (const auto& contentType : contentTypes) |
279 it != contentTypes.end(); it++) | 263 { |
280 { | 264 if (contentType.second == contentTypeUpper) |
281 if (it->second == contentTypeUpper) | 265 return contentType.first; |
282 return it->first; | |
283 } | 266 } |
284 throw std::invalid_argument("Cannot convert argument to ContentType"); | 267 throw std::invalid_argument("Cannot convert argument to ContentType"); |
285 } | 268 } |
286 | 269 |
287 bool FilterEngine::IsFirstRun() const | 270 bool FilterEngine::IsFirstRun() const |
288 { | 271 { |
289 return firstRun; | 272 return firstRun; |
290 } | 273 } |
291 | 274 |
292 FilterPtr FilterEngine::GetFilter(const std::string& text) const | 275 FilterPtr FilterEngine::GetFilter(const std::string& text) const |
293 { | 276 { |
294 JsValuePtr func = jsEngine->Evaluate("API.getFilterFromText"); | 277 JsValuePtr func = jsEngine->Evaluate("API.getFilterFromText"); |
295 JsConstValueList params; | 278 return FilterPtr(new Filter(std::move(*func->Call(jsEngine->NewValue(text))))) ; |
296 params.push_back(jsEngine->NewValue(text)); | |
297 return FilterPtr(new Filter(std::move(*func->Call(params)))); | |
298 } | 279 } |
299 | 280 |
300 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url) const | 281 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url) const |
301 { | 282 { |
302 JsValuePtr func = jsEngine->Evaluate("API.getSubscriptionFromUrl"); | 283 JsValuePtr func = jsEngine->Evaluate("API.getSubscriptionFromUrl"); |
303 JsConstValueList params; | 284 return SubscriptionPtr(new Subscription(std::move(*func->Call(jsEngine->NewVal ue(url))))); |
304 params.push_back(jsEngine->NewValue(url)); | |
305 return SubscriptionPtr(new Subscription(std::move(*func->Call(params)))); | |
306 } | 285 } |
307 | 286 |
308 std::vector<FilterPtr> FilterEngine::GetListedFilters() const | 287 std::vector<FilterPtr> FilterEngine::GetListedFilters() const |
309 { | 288 { |
310 JsValuePtr func = jsEngine->Evaluate("API.getListedFilters"); | 289 JsValuePtr func = jsEngine->Evaluate("API.getListedFilters"); |
311 JsValueList values = func->Call()->AsList(); | 290 JsValueList values = func->Call()->AsList(); |
312 std::vector<FilterPtr> result; | 291 std::vector<FilterPtr> result; |
313 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) | 292 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) |
314 result.push_back(FilterPtr(new Filter(std::move(**it)))); | 293 result.push_back(FilterPtr(new Filter(std::move(**it)))); |
315 return result; | 294 return result; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
371 } | 350 } |
372 | 351 |
373 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, | 352 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, |
374 ContentTypeMask contentTypeMask, | 353 ContentTypeMask contentTypeMask, |
375 const std::vector<std::string>& documentUrls) const | 354 const std::vector<std::string>& documentUrls) const |
376 { | 355 { |
377 if (documentUrls.empty()) | 356 if (documentUrls.empty()) |
378 return CheckFilterMatch(url, contentTypeMask, ""); | 357 return CheckFilterMatch(url, contentTypeMask, ""); |
379 | 358 |
380 std::string lastDocumentUrl = documentUrls.front(); | 359 std::string lastDocumentUrl = documentUrls.front(); |
381 for (std::vector<std::string>::const_iterator it = documentUrls.begin(); | 360 for (const auto& documentUrl : documentUrls) { |
382 it != documentUrls.end(); it++) { | |
383 const std::string documentUrl = *it; | |
384 AdblockPlus::FilterPtr match = CheckFilterMatch(documentUrl, | 361 AdblockPlus::FilterPtr match = CheckFilterMatch(documentUrl, |
385 CONTENT_TYPE_DOCUMENT, | 362 CONTENT_TYPE_DOCUMENT, |
386 lastDocumentUrl); | 363 lastDocumentUrl); |
387 if (match && match->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION) | 364 if (match && match->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION) |
388 return match; | 365 return match; |
389 lastDocumentUrl = documentUrl; | 366 lastDocumentUrl = documentUrl; |
390 } | 367 } |
391 | 368 |
392 return CheckFilterMatch(url, contentTypeMask, lastDocumentUrl); | 369 return CheckFilterMatch(url, contentTypeMask, lastDocumentUrl); |
393 } | 370 } |
(...skipping 22 matching lines...) Expand all Loading... | |
416 JsValuePtr result = func->Call(params); | 393 JsValuePtr result = func->Call(params); |
417 if (!result->IsNull()) | 394 if (!result->IsNull()) |
418 return FilterPtr(new Filter(std::move(*result))); | 395 return FilterPtr(new Filter(std::move(*result))); |
419 else | 396 else |
420 return FilterPtr(); | 397 return FilterPtr(); |
421 } | 398 } |
422 | 399 |
423 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri ng& domain) const | 400 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri ng& domain) const |
424 { | 401 { |
425 JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors"); | 402 JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors"); |
426 JsConstValueList params; | 403 JsValueList result = func->Call(jsEngine->NewValue(domain))->AsList(); |
427 params.push_back(jsEngine->NewValue(domain)); | |
428 JsValueList result = func->Call(params)->AsList(); | |
429 std::vector<std::string> selectors; | 404 std::vector<std::string> selectors; |
430 for (JsValueList::iterator it = result.begin(); it != result.end(); ++it) | 405 for (const auto& r: result) |
431 selectors.push_back((*it)->AsString()); | 406 selectors.push_back(r->AsString()); |
432 return selectors; | 407 return selectors; |
433 } | 408 } |
434 | 409 |
435 JsValuePtr FilterEngine::GetPref(const std::string& pref) const | 410 JsValuePtr FilterEngine::GetPref(const std::string& pref) const |
436 { | 411 { |
437 JsValuePtr func = jsEngine->Evaluate("API.getPref"); | 412 JsValuePtr func = jsEngine->Evaluate("API.getPref"); |
438 JsConstValueList params; | 413 return func->Call(jsEngine->NewValue(pref)); |
439 params.push_back(jsEngine->NewValue(pref)); | |
440 return func->Call(params); | |
441 } | 414 } |
442 | 415 |
443 void FilterEngine::SetPref(const std::string& pref, JsValuePtr value) | 416 void FilterEngine::SetPref(const std::string& pref, JsValuePtr value) |
444 { | 417 { |
445 JsValuePtr func = jsEngine->Evaluate("API.setPref"); | 418 JsValuePtr func = jsEngine->Evaluate("API.setPref"); |
446 JsConstValueList params; | 419 JsConstValueList params; |
447 params.push_back(jsEngine->NewValue(pref)); | 420 params.push_back(jsEngine->NewValue(pref)); |
448 if (value) | 421 if (value) |
449 params.push_back(value); | 422 params.push_back(value); |
450 func->Call(params); | 423 func->Call(params); |
451 } | 424 } |
452 | 425 |
453 std::string FilterEngine::GetHostFromURL(const std::string& url) const | 426 std::string FilterEngine::GetHostFromURL(const std::string& url) const |
454 { | 427 { |
455 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl"); | 428 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl"); |
456 JsConstValueList params; | 429 return func->Call(jsEngine->NewValue(url))->AsString(); |
457 params.push_back(jsEngine->NewValue(url)); | |
458 return func->Call(params)->AsString(); | |
459 } | 430 } |
460 | 431 |
461 void FilterEngine::SetUpdateAvailableCallback( | 432 void FilterEngine::SetUpdateAvailableCallback( |
462 FilterEngine::UpdateAvailableCallback callback) | 433 FilterEngine::UpdateAvailableCallback callback) |
463 { | 434 { |
464 jsEngine->SetEventCallback("updateAvailable", | 435 jsEngine->SetEventCallback("updateAvailable", |
465 std::bind(&FilterEngine::UpdateAvailable, this, callback, | 436 std::bind(&FilterEngine::UpdateAvailable, this, callback, |
466 std::placeholders::_1)); | 437 std::placeholders::_1)); |
467 } | 438 } |
468 | 439 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
584 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url); | 555 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url); |
585 if (filter) | 556 if (filter) |
586 { | 557 { |
587 return filter; | 558 return filter; |
588 } | 559 } |
589 currentUrl = parentUrl; | 560 currentUrl = parentUrl; |
590 } | 561 } |
591 while (urlIterator != documentUrls.end()); | 562 while (urlIterator != documentUrls.end()); |
592 return FilterPtr(); | 563 return FilterPtr(); |
593 } | 564 } |
LEFT | RIGHT |