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

Unified Diff: src/FilterEngine.cpp

Issue 10100009: FilterEngine API improvements (Closed)
Patch Set: Addressed review comments Created April 8, 2013, 1:51 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/FilterEngine.cpp
===================================================================
--- a/src/FilterEngine.cpp
+++ b/src/FilterEngine.cpp
@@ -3,174 +3,174 @@
using namespace AdblockPlus;
#if !FILTER_ENGINE_STUBS
extern const char* jsSources[];
#endif
#if FILTER_ENGINE_STUBS
-JSObject::JSObject(FilterEngine& filterEngine)
+JsObject::JsObject(FilterEngine& filterEngine)
: filterEngine(filterEngine)
{
}
#else
-JSObject::JSObject()
+JsObject::JsObject()
{
}
#endif
-std::string JSObject::GetProperty(const std::string& name, const std::string& defaultValue) const
+std::string JsObject::GetProperty(const std::string& name, const std::string& defaultValue) const
{
#if FILTER_ENGINE_STUBS
std::map<std::string, std::string>::const_iterator it = stringProperties.find(name);
if (it == stringProperties.end())
return defaultValue;
else
return it->second;
#endif
}
-int JSObject::GetProperty(const std::string& name, int defaultValue) const
+int JsObject::GetProperty(const std::string& name, int defaultValue) const
{
#if FILTER_ENGINE_STUBS
std::map<std::string, int>::const_iterator it = intProperties.find(name);
if (it == intProperties.end())
return defaultValue;
else
return it->second;
#endif
}
-bool JSObject::GetProperty(const std::string& name, bool defaultValue) const
+bool JsObject::GetProperty(const std::string& name, bool defaultValue) const
{
#if FILTER_ENGINE_STUBS
std::map<std::string, bool>::const_iterator it = boolProperties.find(name);
if (it == boolProperties.end())
return defaultValue;
else
return it->second;
#endif
}
-void JSObject::SetProperty(const std::string& name, const std::string& value)
+void JsObject::SetProperty(const std::string& name, const std::string& value)
{
#if FILTER_ENGINE_STUBS
stringProperties[name] = value;
#endif
}
-void JSObject::SetProperty(const std::string& name, int value)
+void JsObject::SetProperty(const std::string& name, int value)
{
#if FILTER_ENGINE_STUBS
intProperties[name] = value;
#endif
}
-void JSObject::SetProperty(const std::string& name, bool value)
+void JsObject::SetProperty(const std::string& name, bool value)
{
#if FILTER_ENGINE_STUBS
boolProperties[name] = value;
#endif
}
#if FILTER_ENGINE_STUBS
Filter::Filter(FilterEngine& filterEngine, const std::string& text)
- : JSObject(filterEngine)
+ : JsObject(filterEngine)
{
SetProperty("text", text);
if (text.find("!") == 0)
- SetProperty("type", "comment");
+ SetProperty("type", COMMENT_RULE);
else if (text.find("@@") == 0)
- SetProperty("type", "exception");
+ SetProperty("type", EXCEPTION_RULE);
else if (text.find("#@") != std::string::npos)
- SetProperty("type", "elemhideexception");
+ SetProperty("type", ELEMHIDE_EXCEPTION_RULE);
else if (text.find("#") != std::string::npos)
- SetProperty("type", "elemhide");
+ SetProperty("type", ELEMHIDE_RULE);
else
- SetProperty("type", "blocking");
+ SetProperty("type", BLOCKING_RULE);
}
#else
Filter::Filter()
{
}
#endif
bool Filter::IsListed() const
{
#if FILTER_ENGINE_STUBS
- for (std::vector<Filter*>::iterator it = filterEngine.listedFilters.begin();
+ for (std::vector<FilterPtr>::iterator it = filterEngine.listedFilters.begin();
it != filterEngine.listedFilters.end(); ++it)
{
- if (*it == this)
+ if (it->get() == this)
return true;
}
return false;
#endif
}
void Filter::AddToList()
{
#if FILTER_ENGINE_STUBS
if (!IsListed())
- filterEngine.listedFilters.push_back(this);
+ filterEngine.listedFilters.push_back(shared_from_this());
#endif
}
void Filter::RemoveFromList()
{
- for (std::vector<Filter*>::iterator it = filterEngine.listedFilters.begin();
+ for (std::vector<FilterPtr>::iterator it = filterEngine.listedFilters.begin();
it != filterEngine.listedFilters.end();)
{
- if (*it == this)
+ if (it->get() == this)
it = filterEngine.listedFilters.erase(it);
else
it++;
}
}
#if FILTER_ENGINE_STUBS
Subscription::Subscription(FilterEngine& filterEngine, const std::string& url)
- : JSObject(filterEngine)
+ : JsObject(filterEngine)
{
SetProperty("url", url);
}
#else
Subscription::Subscription()
{
}
#endif
bool Subscription::IsListed() const
{
#if FILTER_ENGINE_STUBS
- for (std::vector<Subscription*>::iterator it = filterEngine.listedSubscriptions.begin();
+ for (std::vector<SubscriptionPtr>::iterator it = filterEngine.listedSubscriptions.begin();
it != filterEngine.listedSubscriptions.end(); ++it)
{
- if (*it == this)
+ if (it->get() == this)
return true;
}
return false;
#endif
}
void Subscription::AddToList()
{
#if FILTER_ENGINE_STUBS
if (!IsListed())
- filterEngine.listedSubscriptions.push_back(this);
+ filterEngine.listedSubscriptions.push_back(shared_from_this());
#endif
}
void Subscription::RemoveFromList()
{
- for (std::vector<Subscription*>::iterator it = filterEngine.listedSubscriptions.begin();
+ for (std::vector<SubscriptionPtr>::iterator it = filterEngine.listedSubscriptions.begin();
it != filterEngine.listedSubscriptions.end();)
{
- if (*it == this)
+ if (it->get() == this)
it = filterEngine.listedSubscriptions.erase(it);
else
it++;
}
}
void Subscription::UpdateFilters()
{
@@ -187,82 +187,82 @@ FilterEngine::FilterEngine(JsEngine& jsE
Filter& FilterEngine::GetFilter(const std::string& text)
{
#if FILTER_ENGINE_STUBS
// Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
std::string trimmed(text);
trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), trimmed.end());
- std::map<std::string, Filter*>::const_iterator it = knownFilters.find(trimmed);
+ std::map<std::string, FilterPtr>::const_iterator it = knownFilters.find(trimmed);
if (it != knownFilters.end())
return *it->second;
- Filter* result = new Filter(*this, trimmed);
- knownFilters[trimmed] = result;
+ FilterPtr result(new Filter(*this, trimmed));
+ knownFilters[trimmed] = result->shared_from_this();
return *result;
#endif
}
Subscription& FilterEngine::GetSubscription(const std::string& url)
{
#if FILTER_ENGINE_STUBS
- std::map<std::string, Subscription*>::const_iterator it = knownSubscriptions.find(url);
+ std::map<std::string, SubscriptionPtr>::const_iterator it = knownSubscriptions.find(url);
if (it != knownSubscriptions.end())
return *it->second;
- Subscription* result = new Subscription(*this, url);
- knownSubscriptions[url] = result;
+ SubscriptionPtr result(new Subscription(*this, url));
+ knownSubscriptions[url] = result->shared_from_this();
return *result;
#endif
}
-const std::vector<Filter*>& FilterEngine::GetListedFilters() const
+const std::vector<FilterPtr>& FilterEngine::GetListedFilters() const
{
#if FILTER_ENGINE_STUBS
return listedFilters;
#endif
}
-const std::vector<Subscription*>& FilterEngine::GetListedSubscriptions() const
+const std::vector<SubscriptionPtr>& FilterEngine::GetListedSubscriptions() const
{
#if FILTER_ENGINE_STUBS
return listedSubscriptions;
#endif
}
void FilterEngine::FetchAvailableSubscriptions(SubscriptionsCallback callback)
{
#if FILTER_ENGINE_STUBS
- std::vector<Subscription*> availableSubscriptions;
+ std::vector<SubscriptionPtr> availableSubscriptions;
Subscription& subscription1 = GetSubscription("https://easylist-downloads.adblockplus.org/easylist.txt");
subscription1.SetProperty("title", "EasyList");
- availableSubscriptions.push_back(&subscription1);
+ availableSubscriptions.push_back(subscription1.shared_from_this());
Subscription& subscription2 = GetSubscription("https://easylist-downloads.adblockplus.org/easylistgermany+easylist.txt");
subscription2.SetProperty("title", "EasyList Germany+EasyList");
- availableSubscriptions.push_back(&subscription2);
+ availableSubscriptions.push_back(subscription2.shared_from_this());
callback(availableSubscriptions);
#endif
}
-Filter* FilterEngine::Matches(const std::string& url,
- const std::string& contentType,
- const std::string& documentUrl)
+AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url,
+ const std::string& contentType,
+ const std::string& documentUrl)
{
#if FILTER_ENGINE_STUBS
//For test on http://simple-adblock.com/faq/testing-your-adblocker/
if (url.find("adbanner.gif") != std::string::npos)
- return &GetFilter("adbanner.gif");
+ return GetFilter("adbanner.gif").shared_from_this();
else if (url.find("notbanner.gif") != std::string::npos)
- return &GetFilter("@@notbanner.gif");
+ return GetFilter("@@notbanner.gif").shared_from_this();
else
- return 0;
+ return AdblockPlus::FilterPtr();
#endif
}
std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::string& domain) const
{
#if FILTER_ENGINE_STUBS
std::vector<std::string> selectors;
selectors.push_back("#ad");

Powered by Google App Engine
This is Rietveld