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

Unified Diff: src/FilterEngine.cpp

Issue 10173031: Don`t use references to JsEngine to avoid use-after-free errors,switch to shared_ptr instead (Closed)
Patch Set: Created April 18, 2013, 4:15 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
@@ -140,30 +140,30 @@ bool Filter::IsListed()
for (std::vector<FilterPtr>::iterator it = filterEngine.listedFilters.begin();
it != filterEngine.listedFilters.end(); ++it)
{
if (it->get() == this)
return true;
}
return false;
#else
- JsValuePtr func = jsEngine.Evaluate("API.isListedFilter");
+ JsValuePtr func = jsEngine->Evaluate("API.isListedFilter");
JsValueList params;
params.push_back(shared_from_this());
return func->Call(params)->AsBool();
#endif
}
void Filter::AddToList()
{
#if FILTER_ENGINE_STUBS
if (!IsListed())
filterEngine.listedFilters.push_back(shared_from_this());
#else
- JsValuePtr func = jsEngine.Evaluate("API.addFilterToList");
+ JsValuePtr func = jsEngine->Evaluate("API.addFilterToList");
JsValueList params;
params.push_back(shared_from_this());
func->Call(params);
#endif
}
void Filter::RemoveFromList()
{
@@ -172,17 +172,17 @@ void Filter::RemoveFromList()
it != filterEngine.listedFilters.end();)
{
if (it->get() == this)
it = filterEngine.listedFilters.erase(it);
else
it++;
}
#else
- JsValuePtr func = jsEngine.Evaluate("API.removeFilterFromList");
+ JsValuePtr func = jsEngine->Evaluate("API.removeFilterFromList");
JsValueList params;
params.push_back(shared_from_this());
func->Call(params);
#endif
}
bool Filter::operator==(const Filter& filter) const
{
@@ -208,30 +208,30 @@ bool Subscription::IsListed()
for (std::vector<SubscriptionPtr>::iterator it = filterEngine.listedSubscriptions.begin();
it != filterEngine.listedSubscriptions.end(); ++it)
{
if (it->get() == this)
return true;
}
return false;
#else
- JsValuePtr func = jsEngine.Evaluate("API.isListedFilter");
+ JsValuePtr func = jsEngine->Evaluate("API.isListedFilter");
JsValueList params;
params.push_back(shared_from_this());
return func->Call(params)->AsBool();
#endif
}
void Subscription::AddToList()
{
#if FILTER_ENGINE_STUBS
if (!IsListed())
filterEngine.listedSubscriptions.push_back(shared_from_this());
#else
- JsValuePtr func = jsEngine.Evaluate("API.addSubscriptionToList");
+ JsValuePtr func = jsEngine->Evaluate("API.addSubscriptionToList");
JsValueList params;
params.push_back(shared_from_this());
func->Call(params);
#endif
}
void Subscription::RemoveFromList()
{
@@ -240,56 +240,56 @@ void Subscription::RemoveFromList()
it != filterEngine.listedSubscriptions.end();)
{
if (it->get() == this)
it = filterEngine.listedSubscriptions.erase(it);
else
it++;
}
#else
- JsValuePtr func = jsEngine.Evaluate("API.removeSubscriptionFromList");
+ JsValuePtr func = jsEngine->Evaluate("API.removeSubscriptionFromList");
JsValueList params;
params.push_back(shared_from_this());
func->Call(params);
#endif
}
void Subscription::UpdateFilters()
{
#if !FILTER_ENGINE_STUBS
- JsValuePtr func = jsEngine.Evaluate("API.updateSubscription");
+ JsValuePtr func = jsEngine->Evaluate("API.updateSubscription");
JsValueList params;
params.push_back(shared_from_this());
func->Call(params);
#endif
}
bool Subscription::IsUpdating()
{
#if FILTER_ENGINE_STUBS
return false;
#else
- JsValuePtr func = jsEngine.Evaluate("API.isSubscriptionUpdating");
+ JsValuePtr func = jsEngine->Evaluate("API.isSubscriptionUpdating");
JsValueList params;
params.push_back(shared_from_this());
JsValuePtr result = func->Call(params);
return result->AsBool();
#endif
}
bool Subscription::operator==(const Subscription& subscription) const
{
return GetProperty("url", "") == subscription.GetProperty("url", "");
}
-FilterEngine::FilterEngine(JsEngine& jsEngine) : jsEngine(jsEngine)
+FilterEngine::FilterEngine(JsEnginePtr jsEngine) : jsEngine(jsEngine)
{
#if !FILTER_ENGINE_STUBS
for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2)
- jsEngine.Evaluate(jsSources[i + 1], jsSources[i]);
+ jsEngine->Evaluate(jsSources[i + 1], jsSources[i]);
#endif
}
FilterPtr 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);
@@ -299,61 +299,61 @@ FilterPtr FilterEngine::GetFilter(const
std::map<std::string, FilterPtr>::const_iterator it = knownFilters.find(trimmed);
if (it != knownFilters.end())
return it->second;
FilterPtr result(new Filter(*this, trimmed));
knownFilters[trimmed] = result;
return result;
#else
- JsValuePtr func = jsEngine.Evaluate("API.getFilterFromText");
+ JsValuePtr func = jsEngine->Evaluate("API.getFilterFromText");
JsValueList params;
- params.push_back(jsEngine.NewValue(text));
+ params.push_back(jsEngine->NewValue(text));
return FilterPtr(new Filter(func->Call(params)));
#endif
}
SubscriptionPtr FilterEngine::GetSubscription(const std::string& url)
{
#if FILTER_ENGINE_STUBS
std::map<std::string, SubscriptionPtr>::const_iterator it = knownSubscriptions.find(url);
if (it != knownSubscriptions.end())
return it->second;
SubscriptionPtr result(new Subscription(*this, url));
knownSubscriptions[url] = result;
return result;
#else
- JsValuePtr func = jsEngine.Evaluate("API.getSubscriptionFromUrl");
+ JsValuePtr func = jsEngine->Evaluate("API.getSubscriptionFromUrl");
JsValueList params;
- params.push_back(jsEngine.NewValue(url));
+ params.push_back(jsEngine->NewValue(url));
return SubscriptionPtr(new Subscription(func->Call(params)));
#endif
}
const std::vector<FilterPtr> FilterEngine::GetListedFilters() const
{
#if FILTER_ENGINE_STUBS
return listedFilters;
#else
- JsValuePtr func = jsEngine.Evaluate("API.getListedFilters");
+ JsValuePtr func = jsEngine->Evaluate("API.getListedFilters");
JsValueList values = func->Call()->AsList();
std::vector<FilterPtr> result;
for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
result.push_back(FilterPtr(new Filter(*it)));
return result;
#endif
}
const std::vector<SubscriptionPtr> FilterEngine::GetListedSubscriptions() const
{
#if FILTER_ENGINE_STUBS
return listedSubscriptions;
#else
- JsValuePtr func = jsEngine.Evaluate("API.getListedSubscriptions");
+ JsValuePtr func = jsEngine->Evaluate("API.getListedSubscriptions");
JsValueList values = func->Call()->AsList();
std::vector<SubscriptionPtr> result;
for (JsValueList::iterator it = values.begin(); it != values.end(); it++)
result.push_back(SubscriptionPtr(new Subscription(*it)));
return result;
#endif
}
@@ -384,21 +384,21 @@ AdblockPlus::FilterPtr FilterEngine::Mat
//For test on http://simple-adblock.com/faq/testing-your-adblocker/
if (url.find("adbanner.gif") != std::string::npos)
return GetFilter("adbanner.gif");
else if (url.find("notbanner.gif") != std::string::npos)
return GetFilter("@@notbanner.gif");
else
return AdblockPlus::FilterPtr();
#else
- JsValuePtr func = jsEngine.Evaluate("API.checkFilterMatch");
+ JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch");
JsValueList params;
- params.push_back(jsEngine.NewValue(url));
- params.push_back(jsEngine.NewValue(contentType));
- params.push_back(jsEngine.NewValue(documentUrl));
+ params.push_back(jsEngine->NewValue(url));
+ params.push_back(jsEngine->NewValue(contentType));
+ params.push_back(jsEngine->NewValue(documentUrl));
JsValuePtr result = func->Call(params);
if (!result->IsNull())
return FilterPtr(new Filter(result));
else
return FilterPtr();
#endif
}
@@ -408,18 +408,18 @@ std::vector<std::string> FilterEngine::G
std::vector<std::string> selectors;
selectors.push_back("#ad");
selectors.push_back(".ad");
//For test on http://simple-adblock.com/faq/testing-your-adblocker/
if (domain == "simple-adblock.com")
selectors.push_back(".ad_300x250");
return selectors;
#else
- JsValuePtr func = jsEngine.Evaluate("API.getElementHidingSelectors");
+ JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors");
JsValueList params;
- params.push_back(jsEngine.NewValue(domain));
+ params.push_back(jsEngine->NewValue(domain));
JsValueList result = func->Call(params)->AsList();
std::vector<std::string> selectors;
for (JsValueList::iterator it = result.begin(); it != result.end(); ++it)
selectors.push_back((*it)->AsString());
return selectors;
#endif
}
« no previous file with comments | « src/FileSystemJsObject.cpp ('k') | src/GlobalJsObject.h » ('j') | src/JsEngine.cpp » ('J')

Powered by Google App Engine
This is Rietveld