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: address comment Created March 17, 2017, 10:09 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 *
(...skipping 16 matching lines...) Expand all
30 30
31 extern std::string jsSources[]; 31 extern std::string jsSources[];
32 32
33 Filter::Filter(JsValue&& value) 33 Filter::Filter(JsValue&& value)
34 : JsValue(std::move(value)) 34 : JsValue(std::move(value))
35 { 35 {
36 if (!IsObject()) 36 if (!IsObject())
37 throw std::runtime_error("JavaScript value is not an object"); 37 throw std::runtime_error("JavaScript value is not an object");
38 } 38 }
39 39
40 Filter::Type Filter::GetType() 40 Filter::Type Filter::GetType() const
41 { 41 {
42 std::string className = GetClass(); 42 std::string className = GetClass();
43 if (className == "BlockingFilter") 43 if (className == "BlockingFilter")
44 return TYPE_BLOCKING; 44 return TYPE_BLOCKING;
45 else if (className == "WhitelistFilter") 45 else if (className == "WhitelistFilter")
46 return TYPE_EXCEPTION; 46 return TYPE_EXCEPTION;
47 else if (className == "ElemHideFilter") 47 else if (className == "ElemHideFilter")
48 return TYPE_ELEMHIDE; 48 return TYPE_ELEMHIDE;
49 else if (className == "ElemHideException") 49 else if (className == "ElemHideException")
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() 57 bool Filter::IsListed() const
58 { 58 {
59 JsValuePtr func = jsEngine->Evaluate("API.isListedFilter"); 59 JsValuePtr func = jsEngine->Evaluate("API.isListedFilter");
60 JsValueList params; 60 return func->Call(*this).AsBool();
61 params.push_back(shared_from_this());
62 return func->Call(params)->AsBool();
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 JsValueList params; 66 func->Call(*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 JsValueList params; 72 func->Call(*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() 87 bool Subscription::IsListed() const
94 { 88 {
95 JsValuePtr func = jsEngine->Evaluate("API.isListedSubscription"); 89 JsValuePtr func = jsEngine->Evaluate("API.isListedSubscription");
96 JsValueList params; 90 return func->Call(*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 JsValueList params; 96 func->Call(*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 JsValueList params; 102 func->Call(*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 JsValueList params; 108 func->Call(*this);
121 params.push_back(shared_from_this()); 109 }
122 func->Call(params); 110
123 } 111 bool Subscription::IsUpdating() const
124
125 bool Subscription::IsUpdating()
126 { 112 {
127 JsValuePtr func = jsEngine->Evaluate("API.isSubscriptionUpdating"); 113 JsValuePtr func = jsEngine->Evaluate("API.isSubscriptionUpdating");
128 JsValueList params; 114 return func->Call(*this).AsBool();
129 params.push_back(shared_from_this()); 115 }
130 JsValuePtr result = func->Call(params); 116
131 return result->AsBool(); 117 bool Subscription::IsAA() const
132 } 118 {
133 119 return jsEngine->Evaluate("API.isAASubscription")->Call(*this).AsBool();
134 bool Subscription::IsAA()
135 {
136 return jsEngine->Evaluate("API.isAASubscription")->Call(*shared_from_this())-> AsBool();
137 } 120 }
138 121
139 bool Subscription::operator==(const Subscription& subscription) const 122 bool Subscription::operator==(const Subscription& subscription) const
140 { 123 {
141 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt ring(); 124 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt ring();
142 } 125 }
143 126
144 namespace 127 namespace
145 { 128 {
146 class Sync 129 class Sync
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 ContentTypeMap::const_iterator it = contentTypes.find(contentType); 256 ContentTypeMap::const_iterator it = contentTypes.find(contentType);
274 if (it != contentTypes.end()) 257 if (it != contentTypes.end())
275 return it->second; 258 return it->second;
276 throw std::invalid_argument("Argument is not a valid ContentType"); 259 throw std::invalid_argument("Argument is not a valid ContentType");
277 } 260 }
278 261
279 FilterEngine::ContentType FilterEngine::StringToContentType(const std::string& c ontentType) 262 FilterEngine::ContentType FilterEngine::StringToContentType(const std::string& c ontentType)
280 { 263 {
281 std::string contentTypeUpper = contentType; 264 std::string contentTypeUpper = contentType;
282 std::transform(contentType.begin(), contentType.end(), contentTypeUpper.begin( ), ::toupper); 265 std::transform(contentType.begin(), contentType.end(), contentTypeUpper.begin( ), ::toupper);
283 for (ContentTypeMap::const_iterator it = contentTypes.begin(); 266 for (const auto& contentType : contentTypes)
284 it != contentTypes.end(); it++) 267 {
285 { 268 if (contentType.second == contentTypeUpper)
286 if (it->second == contentTypeUpper) 269 return contentType.first;
287 return it->first;
288 } 270 }
289 throw std::invalid_argument("Cannot convert argument to ContentType"); 271 throw std::invalid_argument("Cannot convert argument to ContentType");
290 } 272 }
291 273
292 bool FilterEngine::IsFirstRun() const 274 bool FilterEngine::IsFirstRun() const
293 { 275 {
294 return firstRun; 276 return firstRun;
295 } 277 }
296 278
297 FilterPtr FilterEngine::GetFilter(const std::string& text) 279 FilterPtr FilterEngine::GetFilter(const std::string& text) const
298 { 280 {
299 JsValuePtr func = jsEngine->Evaluate("API.getFilterFromText"); 281 JsValuePtr func = jsEngine->Evaluate("API.getFilterFromText");
300 JsValueList params; 282 return FilterPtr(new Filter(func->Call(*jsEngine->NewValue(text))));
301 params.push_back(jsEngine->NewValue(text)); 283 }
302 return FilterPtr(new Filter(std::move(*func->Call(params)))); 284
303 } 285 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url) const
304
305 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url)
306 { 286 {
307 JsValuePtr func = jsEngine->Evaluate("API.getSubscriptionFromUrl"); 287 JsValuePtr func = jsEngine->Evaluate("API.getSubscriptionFromUrl");
308 JsValueList params; 288 return SubscriptionPtr(new Subscription(func->Call(*jsEngine->NewValue(url)))) ;
309 params.push_back(jsEngine->NewValue(url));
310 return SubscriptionPtr(new Subscription(std::move(*func->Call(params))));
311 } 289 }
312 290
313 std::vector<FilterPtr> FilterEngine::GetListedFilters() const 291 std::vector<FilterPtr> FilterEngine::GetListedFilters() const
314 { 292 {
315 JsValuePtr func = jsEngine->Evaluate("API.getListedFilters"); 293 JsValuePtr func = jsEngine->Evaluate("API.getListedFilters");
316 JsValueList values = func->Call()->AsList(); 294 JsValueList values = func->Call().AsList();
317 std::vector<FilterPtr> result; 295 std::vector<FilterPtr> result;
318 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) 296 for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
319 result.push_back(FilterPtr(new Filter(std::move(**it)))); 297 result.push_back(FilterPtr(new Filter(std::move(**it))));
320 return result; 298 return result;
321 } 299 }
322 300
323 std::vector<SubscriptionPtr> FilterEngine::GetListedSubscriptions() const 301 std::vector<SubscriptionPtr> FilterEngine::GetListedSubscriptions() const
324 { 302 {
325 JsValuePtr func = jsEngine->Evaluate("API.getListedSubscriptions"); 303 JsValuePtr func = jsEngine->Evaluate("API.getListedSubscriptions");
326 JsValueList values = func->Call()->AsList(); 304 JsValueList values = func->Call().AsList();
327 std::vector<SubscriptionPtr> result; 305 std::vector<SubscriptionPtr> result;
328 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) 306 for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
329 result.push_back(SubscriptionPtr(new Subscription(std::move(**it)))); 307 result.push_back(SubscriptionPtr(new Subscription(std::move(**it))));
330 return result; 308 return result;
331 } 309 }
332 310
333 std::vector<SubscriptionPtr> FilterEngine::FetchAvailableSubscriptions() const 311 std::vector<SubscriptionPtr> FilterEngine::FetchAvailableSubscriptions() const
334 { 312 {
335 JsValuePtr func = jsEngine->Evaluate("API.getRecommendedSubscriptions"); 313 JsValuePtr func = jsEngine->Evaluate("API.getRecommendedSubscriptions");
336 JsValueList values = func->Call()->AsList(); 314 JsValueList values = func->Call().AsList();
337 std::vector<SubscriptionPtr> result; 315 std::vector<SubscriptionPtr> result;
338 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) 316 for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
339 result.push_back(SubscriptionPtr(new Subscription(std::move(**it)))); 317 result.push_back(SubscriptionPtr(new Subscription(std::move(**it))));
340 return result; 318 return result;
341 } 319 }
342 320
343 void FilterEngine::SetAAEnabled(bool enabled) 321 void FilterEngine::SetAAEnabled(bool enabled)
344 { 322 {
345 jsEngine->Evaluate("API.setAASubscriptionEnabled")->Call(*jsEngine->NewValue(e nabled)); 323 jsEngine->Evaluate("API.setAASubscriptionEnabled")->Call(*jsEngine->NewValue(e nabled));
346 } 324 }
347 325
348 bool FilterEngine::IsAAEnabled() const 326 bool FilterEngine::IsAAEnabled() const
349 { 327 {
350 return jsEngine->Evaluate("API.isAASubscriptionEnabled()")->AsBool(); 328 return jsEngine->Evaluate("API.isAASubscriptionEnabled()")->AsBool();
351 } 329 }
352 330
353 std::string FilterEngine::GetAAUrl() const 331 std::string FilterEngine::GetAAUrl() const
354 { 332 {
355 return GetPref("subscriptions_exceptionsurl")->AsString(); 333 return GetPref("subscriptions_exceptionsurl")->AsString();
356 } 334 }
357 335
358 void FilterEngine::ShowNextNotification(const std::string& url) 336 void FilterEngine::ShowNextNotification(const std::string& url)
359 { 337 {
360 JsValuePtr func = jsEngine->Evaluate("API.showNextNotification"); 338 JsValuePtr func = jsEngine->Evaluate("API.showNextNotification");
361 JsValueList params; 339 JsConstValueList params;
362 if (!url.empty()) 340 if (!url.empty())
363 { 341 {
364 params.push_back(jsEngine->NewValue(url)); 342 params.push_back(jsEngine->NewValue(url));
365 } 343 }
366 func->Call(params); 344 func->Call(params);
367 } 345 }
368 346
369 void FilterEngine::SetShowNotificationCallback(const ShowNotificationCallback& v alue) 347 void FilterEngine::SetShowNotificationCallback(const ShowNotificationCallback& v alue)
370 { 348 {
371 if (!value) 349 if (!value)
(...skipping 19 matching lines...) Expand all
391 } 369 }
392 370
393 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, 371 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url,
394 ContentTypeMask contentTypeMask, 372 ContentTypeMask contentTypeMask,
395 const std::vector<std::string>& documentUrls) const 373 const std::vector<std::string>& documentUrls) const
396 { 374 {
397 if (documentUrls.empty()) 375 if (documentUrls.empty())
398 return CheckFilterMatch(url, contentTypeMask, ""); 376 return CheckFilterMatch(url, contentTypeMask, "");
399 377
400 std::string lastDocumentUrl = documentUrls.front(); 378 std::string lastDocumentUrl = documentUrls.front();
401 for (std::vector<std::string>::const_iterator it = documentUrls.begin(); 379 for (const auto& documentUrl : documentUrls) {
402 it != documentUrls.end(); it++) {
403 const std::string documentUrl = *it;
404 AdblockPlus::FilterPtr match = CheckFilterMatch(documentUrl, 380 AdblockPlus::FilterPtr match = CheckFilterMatch(documentUrl,
405 CONTENT_TYPE_DOCUMENT, 381 CONTENT_TYPE_DOCUMENT,
406 lastDocumentUrl); 382 lastDocumentUrl);
407 if (match && match->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION) 383 if (match && match->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION)
408 return match; 384 return match;
409 lastDocumentUrl = documentUrl; 385 lastDocumentUrl = documentUrl;
410 } 386 }
411 387
412 return CheckFilterMatch(url, contentTypeMask, lastDocumentUrl); 388 return CheckFilterMatch(url, contentTypeMask, lastDocumentUrl);
413 } 389 }
414 390
415 bool FilterEngine::IsDocumentWhitelisted(const std::string& url, 391 bool FilterEngine::IsDocumentWhitelisted(const std::string& url,
416 const std::vector<std::string>& documentUrls) const 392 const std::vector<std::string>& documentUrls) const
417 { 393 {
418 return !!GetWhitelistingFilter(url, CONTENT_TYPE_DOCUMENT, documentUrls); 394 return !!GetWhitelistingFilter(url, CONTENT_TYPE_DOCUMENT, documentUrls);
419 } 395 }
420 396
421 bool FilterEngine::IsElemhideWhitelisted(const std::string& url, 397 bool FilterEngine::IsElemhideWhitelisted(const std::string& url,
422 const std::vector<std::string>& documentUrls) const 398 const std::vector<std::string>& documentUrls) const
423 { 399 {
424 return !!GetWhitelistingFilter(url, CONTENT_TYPE_ELEMHIDE, documentUrls); 400 return !!GetWhitelistingFilter(url, CONTENT_TYPE_ELEMHIDE, documentUrls);
425 } 401 }
426 402
427 AdblockPlus::FilterPtr FilterEngine::CheckFilterMatch(const std::string& url, 403 AdblockPlus::FilterPtr FilterEngine::CheckFilterMatch(const std::string& url,
428 ContentTypeMask contentTypeMask, 404 ContentTypeMask contentTypeMask,
429 const std::string& documentUrl) const 405 const std::string& documentUrl) const
430 { 406 {
431 JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch"); 407 JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch");
432 JsValueList params; 408 JsConstValueList params;
433 params.push_back(jsEngine->NewValue(url)); 409 params.push_back(jsEngine->NewValue(url));
434 params.push_back(jsEngine->NewValue(contentTypeMask)); 410 params.push_back(jsEngine->NewValue(contentTypeMask));
435 params.push_back(jsEngine->NewValue(documentUrl)); 411 params.push_back(jsEngine->NewValue(documentUrl));
436 JsValuePtr result = func->Call(params); 412 JsValue result = func->Call(params);
437 if (!result->IsNull()) 413 if (!result.IsNull())
438 return FilterPtr(new Filter(std::move(*result))); 414 return FilterPtr(new Filter(std::move(result)));
439 else 415 else
440 return FilterPtr(); 416 return FilterPtr();
441 } 417 }
442 418
443 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
444 { 420 {
445 JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors"); 421 JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors");
446 JsValueList params; 422 JsValueList result = func->Call(*jsEngine->NewValue(domain)).AsList();
447 params.push_back(jsEngine->NewValue(domain));
448 JsValueList result = func->Call(params)->AsList();
449 std::vector<std::string> selectors; 423 std::vector<std::string> selectors;
450 for (JsValueList::iterator it = result.begin(); it != result.end(); ++it) 424 for (const auto& r: result)
451 selectors.push_back((*it)->AsString()); 425 selectors.push_back(r->AsString());
452 return selectors; 426 return selectors;
453 } 427 }
454 428
455 JsValuePtr FilterEngine::GetPref(const std::string& pref) const 429 JsValuePtr FilterEngine::GetPref(const std::string& pref) const
456 { 430 {
457 JsValuePtr func = jsEngine->Evaluate("API.getPref"); 431 JsValuePtr func = jsEngine->Evaluate("API.getPref");
458 JsValueList params; 432 return std::make_shared<JsValue>(func->Call(*jsEngine->NewValue(pref)));
459 params.push_back(jsEngine->NewValue(pref));
460 return func->Call(params);
461 } 433 }
462 434
463 void FilterEngine::SetPref(const std::string& pref, JsValuePtr value) 435 void FilterEngine::SetPref(const std::string& pref, JsValuePtr value)
464 { 436 {
465 JsValuePtr func = jsEngine->Evaluate("API.setPref"); 437 JsValuePtr func = jsEngine->Evaluate("API.setPref");
466 JsValueList params; 438 JsConstValueList params;
467 params.push_back(jsEngine->NewValue(pref)); 439 params.push_back(jsEngine->NewValue(pref));
468 if (value) 440 if (value)
469 params.push_back(value); 441 params.push_back(value);
470 func->Call(params); 442 func->Call(params);
471 } 443 }
472 444
473 std::string FilterEngine::GetHostFromURL(const std::string& url) 445 std::string FilterEngine::GetHostFromURL(const std::string& url) const
474 { 446 {
475 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl"); 447 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl");
476 JsValueList params; 448 return func->Call(*jsEngine->NewValue(url)).AsString();
477 params.push_back(jsEngine->NewValue(url));
478 return func->Call(params)->AsString();
479 } 449 }
480 450
481 void FilterEngine::SetUpdateAvailableCallback( 451 void FilterEngine::SetUpdateAvailableCallback(
482 FilterEngine::UpdateAvailableCallback callback) 452 FilterEngine::UpdateAvailableCallback callback)
483 { 453 {
484 jsEngine->SetEventCallback("updateAvailable", 454 jsEngine->SetEventCallback("updateAvailable",
485 std::bind(&FilterEngine::UpdateAvailable, this, callback, 455 std::bind(&FilterEngine::UpdateAvailable, this, callback,
486 std::placeholders::_1)); 456 std::placeholders::_1));
487 } 457 }
488 458
489 void FilterEngine::RemoveUpdateAvailableCallback() 459 void FilterEngine::RemoveUpdateAvailableCallback()
490 { 460 {
491 jsEngine->RemoveEventCallback("updateAvailable"); 461 jsEngine->RemoveEventCallback("updateAvailable");
492 } 462 }
493 463
494 void FilterEngine::UpdateAvailable( 464 void FilterEngine::UpdateAvailable(
495 FilterEngine::UpdateAvailableCallback callback, JsValueList& params) 465 FilterEngine::UpdateAvailableCallback callback, JsValueList& params)
496 { 466 {
497 if (params.size() >= 1 && !params[0]->IsNull()) 467 if (params.size() >= 1 && !params[0]->IsNull())
498 callback(params[0]->AsString()); 468 callback(params[0]->AsString());
499 } 469 }
500 470
501 void FilterEngine::ForceUpdateCheck( 471 void FilterEngine::ForceUpdateCheck(
502 FilterEngine::UpdateCheckDoneCallback callback) 472 const FilterEngine::UpdateCheckDoneCallback& callback)
503 { 473 {
504 std::string eventName = "_updateCheckDone"; 474 JsValuePtr func = jsEngine->Evaluate("API.forceUpdateCheck");
505 eventName += ++updateCheckId; 475 JsConstValueList params;
506 476 if (callback)
507 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,
508 this, eventName, callback, std::placeholders::_1)); 480 this, eventName, callback, std::placeholders::_1));
509 481 params.push_back(jsEngine->NewValue(eventName));
510 JsValuePtr func = jsEngine->Evaluate("API.forceUpdateCheck"); 482 }
511 JsValueList params;
512 params.push_back(jsEngine->NewValue(eventName));
513 func->Call(params); 483 func->Call(params);
514 } 484 }
515 485
516 void FilterEngine::UpdateCheckDone(const std::string& eventName, 486 void FilterEngine::UpdateCheckDone(const std::string& eventName,
517 FilterEngine::UpdateCheckDoneCallback callback, JsValueList& params) 487 FilterEngine::UpdateCheckDoneCallback callback, JsValueList& params)
518 { 488 {
519 jsEngine->RemoveEventCallback(eventName); 489 jsEngine->RemoveEventCallback(eventName);
520 490
521 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() : "");
522 callback(error); 492 callback(error);
523 } 493 }
524 494
525 void FilterEngine::SetFilterChangeCallback(FilterEngine::FilterChangeCallback ca llback) 495 void FilterEngine::SetFilterChangeCallback(FilterEngine::FilterChangeCallback ca llback)
526 { 496 {
527 jsEngine->SetEventCallback("filterChange", std::bind(&FilterEngine::FilterChan ged, 497 jsEngine->SetEventCallback("filterChange", std::bind(&FilterEngine::FilterChan ged,
528 this, callback, std::placeholders::_1)); 498 this, callback, std::placeholders::_1));
529 } 499 }
530 500
531 void FilterEngine::RemoveFilterChangeCallback() 501 void FilterEngine::RemoveFilterChangeCallback()
532 { 502 {
533 jsEngine->RemoveEventCallback("filterChange"); 503 jsEngine->RemoveEventCallback("filterChange");
534 } 504 }
535 505
536 void FilterEngine::SetAllowedConnectionType(const std::string* value) 506 void FilterEngine::SetAllowedConnectionType(const std::string* value)
537 { 507 {
538 SetPref("allowed_connection_type", value ? jsEngine->NewValue(*value) : nullpt r); 508 SetPref("allowed_connection_type", value ? jsEngine->NewValue(*value) : jsEngi ne->NewValue(""));
539 } 509 }
540 510
541 std::unique_ptr<std::string> FilterEngine::GetAllowedConnectionType() 511 std::unique_ptr<std::string> FilterEngine::GetAllowedConnectionType() const
542 { 512 {
543 auto prefValue = GetPref("allowed_connection_type"); 513 auto prefValue = GetPref("allowed_connection_type");
544 if (prefValue->IsUndefined()) 514 if (prefValue->AsString().empty())
545 return nullptr; 515 return nullptr;
546 return std::unique_ptr<std::string>(new std::string(prefValue->AsString())); 516 return std::unique_ptr<std::string>(new std::string(prefValue->AsString()));
547 } 517 }
548 518
549 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js ValueList& params) 519 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js ValueList& params)
550 { 520 {
551 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() : "");
552 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false)); 522 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false));
553 callback(action, item); 523 callback(action, item);
554 } 524 }
555 525
556 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback, 526 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback,
557 const JsValueList& params) 527 const JsValueList& params)
558 { 528 {
559 if (params.size() < 1) 529 if (params.size() < 1)
560 return; 530 return;
561 531
562 if (!params[0]->IsObject()) 532 if (!params[0]->IsObject())
563 { 533 {
564 return; 534 return;
565 } 535 }
566 callback(NotificationPtr(new Notification(std::move(*params[0])))); 536 callback(NotificationPtr(new Notification(std::move(*params[0]))));
567 } 537 }
568 538
569 539
570 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) 540 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) const
571 { 541 {
572 JsValueList params; 542 JsConstValueList params;
573 params.push_back(jsEngine->NewValue(v1)); 543 params.push_back(jsEngine->NewValue(v1));
574 params.push_back(jsEngine->NewValue(v2)); 544 params.push_back(jsEngine->NewValue(v2));
575 JsValuePtr func = jsEngine->Evaluate("API.compareVersions"); 545 JsValuePtr func = jsEngine->Evaluate("API.compareVersions");
576 return func->Call(params)->AsInt(); 546 return func->Call(params).AsInt();
577 } 547 }
578 548
579 FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url, 549 FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url,
580 ContentTypeMask contentTypeMask, const std::string& documentUrl) const 550 ContentTypeMask contentTypeMask, const std::string& documentUrl) const
581 { 551 {
582 FilterPtr match = Matches(url, contentTypeMask, documentUrl); 552 FilterPtr match = Matches(url, contentTypeMask, documentUrl);
583 if (match && match->GetType() == Filter::TYPE_EXCEPTION) 553 if (match && match->GetType() == Filter::TYPE_EXCEPTION)
584 { 554 {
585 return match; 555 return match;
586 } 556 }
(...skipping 17 matching lines...) Expand all
604 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url); 574 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url);
605 if (filter) 575 if (filter)
606 { 576 {
607 return filter; 577 return filter;
608 } 578 }
609 currentUrl = parentUrl; 579 currentUrl = parentUrl;
610 } 580 }
611 while (urlIterator != documentUrls.end()); 581 while (urlIterator != documentUrls.end());
612 return FilterPtr(); 582 return FilterPtr();
613 } 583 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld