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

Unified Diff: src/FilterEngine.cpp

Issue 29417605: Issue 5034 - Part 3: Create plain JsValue instead of JsValuePtr (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: FIxed JsContext bug and a few others. Created April 19, 2017, 9:55 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
@@ -168,19 +168,19 @@
auto sync = std::make_shared<Sync>();
auto isConnectionAllowedCallback = params.isConnectionAllowedCallback;
if (isConnectionAllowedCallback)
jsEngine->SetIsConnectionAllowedCallback([sync, jsEngine]()->bool
{
sync->Wait();
return jsEngine->IsConnectionAllowed();
});
- jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated, sync, isConnectionAllowedCallback](const JsConstValueList& params)
+ jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated, sync, isConnectionAllowedCallback](const JsValueList& params)
{
- filterEngine->firstRun = params.size() && params[0]->AsBool();
+ filterEngine->firstRun = params.size() && params[0].AsBool();
if (isConnectionAllowedCallback)
{
std::weak_ptr<FilterEngine> weakFilterEngine = filterEngine;
jsEngine->SetIsConnectionAllowedCallback([weakFilterEngine, isConnectionAllowedCallback]()->bool
{
auto filterEngine = weakFilterEngine.lock();
if (!filterEngine)
return false;
@@ -192,23 +192,23 @@
jsEngine->RemoveEventCallback("_init");
});
// Lock the JS engine while we are loading scripts, no timeouts should fire
// until we are done.
const JsContext context(jsEngine);
// Set the preconfigured prefs
- JsValuePtr preconfiguredPrefsObject = jsEngine->NewObject();
+ auto preconfiguredPrefsObject = jsEngine->NewObject();
for (FilterEngine::Prefs::const_iterator it = params.preconfiguredPrefs.begin();
it != params.preconfiguredPrefs.end(); it++)
{
- preconfiguredPrefsObject->SetProperty(it->first, *it->second);
+ preconfiguredPrefsObject.SetProperty(it->first, it->second);
}
- jsEngine->SetGlobalProperty("_preconfiguredPrefs", *preconfiguredPrefsObject);
+ jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject);
// Load adblockplus scripts
for (int i = 0; !jsSources[i].empty(); i += 2)
jsEngine->Evaluate(jsSources[i + 1], jsSources[i]);
}
FilterEnginePtr FilterEngine::Create(const JsEnginePtr& jsEngine,
const FilterEngine::CreationParameters& params)
{
@@ -274,74 +274,74 @@
bool FilterEngine::IsFirstRun() const
{
return firstRun;
}
FilterPtr FilterEngine::GetFilter(const std::string& text) const
{
JsValuePtr func = jsEngine->Evaluate("API.getFilterFromText");
- return FilterPtr(new Filter(func->Call(*jsEngine->NewValue(text))));
+ return FilterPtr(new Filter(func->Call(jsEngine->NewValue(text))));
}
SubscriptionPtr FilterEngine::GetSubscription(const std::string& url) const
{
JsValuePtr func = jsEngine->Evaluate("API.getSubscriptionFromUrl");
- return SubscriptionPtr(new Subscription(func->Call(*jsEngine->NewValue(url))));
+ return SubscriptionPtr(new Subscription(func->Call(jsEngine->NewValue(url))));
}
std::vector<FilterPtr> FilterEngine::GetListedFilters() const
{
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(std::move(**it))));
+ result.push_back(FilterPtr(new Filter(std::move(*it))));
return result;
}
std::vector<SubscriptionPtr> FilterEngine::GetListedSubscriptions() const
{
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(std::move(**it))));
+ result.push_back(SubscriptionPtr(new Subscription(std::move(*it))));
return result;
}
std::vector<SubscriptionPtr> FilterEngine::FetchAvailableSubscriptions() const
{
JsValuePtr func = jsEngine->Evaluate("API.getRecommendedSubscriptions");
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(std::move(**it))));
+ result.push_back(SubscriptionPtr(new Subscription(std::move(*it))));
return result;
}
void FilterEngine::SetAAEnabled(bool enabled)
{
- jsEngine->Evaluate("API.setAASubscriptionEnabled")->Call(*jsEngine->NewValue(enabled));
+ jsEngine->Evaluate("API.setAASubscriptionEnabled")->Call(jsEngine->NewValue(enabled));
}
bool FilterEngine::IsAAEnabled() const
{
return jsEngine->Evaluate("API.isAASubscriptionEnabled()")->AsBool();
}
std::string FilterEngine::GetAAUrl() const
{
- return GetPref("subscriptions_exceptionsurl")->AsString();
+ return GetPref("subscriptions_exceptionsurl").AsString();
}
void FilterEngine::ShowNextNotification(const std::string& url) const
{
JsValuePtr func = jsEngine->Evaluate("API.showNextNotification");
- JsConstValueList params;
+ JsValueList params;
if (!url.empty())
{
params.push_back(jsEngine->NewValue(url));
}
func->Call(params);
}
void FilterEngine::SetShowNotificationCallback(const ShowNotificationCallback& value)
@@ -400,100 +400,99 @@
return !!GetWhitelistingFilter(url, CONTENT_TYPE_ELEMHIDE, documentUrls);
}
AdblockPlus::FilterPtr FilterEngine::CheckFilterMatch(const std::string& url,
ContentTypeMask contentTypeMask,
const std::string& documentUrl) const
{
JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch");
- JsConstValueList params;
+ JsValueList params;
params.push_back(jsEngine->NewValue(url));
params.push_back(jsEngine->NewValue(contentTypeMask));
params.push_back(jsEngine->NewValue(documentUrl));
JsValue result = func->Call(params);
if (!result.IsNull())
return FilterPtr(new Filter(std::move(result)));
else
return FilterPtr();
}
std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::string& domain) const
{
JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors");
- JsValueList result = func->Call(*jsEngine->NewValue(domain)).AsList();
+ JsValueList result = func->Call(jsEngine->NewValue(domain)).AsList();
std::vector<std::string> selectors;
for (const auto& r: result)
- selectors.push_back(r->AsString());
+ selectors.push_back(r.AsString());
return selectors;
}
-JsValuePtr FilterEngine::GetPref(const std::string& pref) const
+JsValue FilterEngine::GetPref(const std::string& pref) const
{
JsValuePtr func = jsEngine->Evaluate("API.getPref");
- return std::make_shared<JsValue>(func->Call(*jsEngine->NewValue(pref)));
+ return func->Call(jsEngine->NewValue(pref));
}
-void FilterEngine::SetPref(const std::string& pref, const JsValuePtr& value)
+void FilterEngine::SetPref(const std::string& pref, const JsValue& value)
{
JsValuePtr func = jsEngine->Evaluate("API.setPref");
- JsConstValueList params;
+ JsValueList params;
params.push_back(jsEngine->NewValue(pref));
- if (value)
- params.push_back(value);
+ params.push_back(value);
func->Call(params);
}
std::string FilterEngine::GetHostFromURL(const std::string& url) const
{
JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl");
- return func->Call(*jsEngine->NewValue(url)).AsString();
+ return func->Call(jsEngine->NewValue(url)).AsString();
}
void FilterEngine::SetUpdateAvailableCallback(
const FilterEngine::UpdateAvailableCallback& callback)
{
jsEngine->SetEventCallback("updateAvailable",
std::bind(&FilterEngine::UpdateAvailable, this, callback,
std::placeholders::_1));
}
void FilterEngine::RemoveUpdateAvailableCallback()
{
jsEngine->RemoveEventCallback("updateAvailable");
}
void FilterEngine::UpdateAvailable(
- const FilterEngine::UpdateAvailableCallback& callback, const JsConstValueList& params) const
+ const FilterEngine::UpdateAvailableCallback& callback, const JsValueList& params) const
{
- if (params.size() >= 1 && !params[0]->IsNull())
- callback(params[0]->AsString());
+ if (params.size() >= 1 && !params[0].IsNull())
+ callback(params[0].AsString());
}
void FilterEngine::ForceUpdateCheck(
const FilterEngine::UpdateCheckDoneCallback& callback)
{
JsValuePtr func = jsEngine->Evaluate("API.forceUpdateCheck");
- JsConstValueList params;
+ JsValueList params;
if (callback)
{
std::string eventName = "_updateCheckDone" + std::to_string(++updateCheckId);
jsEngine->SetEventCallback(eventName, std::bind(&FilterEngine::UpdateCheckDone,
this, eventName, callback, std::placeholders::_1));
params.push_back(jsEngine->NewValue(eventName));
}
func->Call(params);
}
void FilterEngine::UpdateCheckDone(const std::string& eventName,
- const FilterEngine::UpdateCheckDoneCallback& callback, const JsConstValueList& params)
+ const FilterEngine::UpdateCheckDoneCallback& callback, const JsValueList& params)
{
jsEngine->RemoveEventCallback(eventName);
- std::string error(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsString() : "");
+ std::string error(params.size() >= 1 && !params[0].IsNull() ? params[0].AsString() : "");
callback(error);
}
void FilterEngine::SetFilterChangeCallback(const FilterEngine::FilterChangeCallback& callback)
{
jsEngine->SetEventCallback("filterChange", std::bind(&FilterEngine::FilterChanged,
this, callback, std::placeholders::_1));
}
@@ -506,45 +505,45 @@
void FilterEngine::SetAllowedConnectionType(const std::string* value)
{
SetPref("allowed_connection_type", value ? jsEngine->NewValue(*value) : jsEngine->NewValue(""));
}
std::unique_ptr<std::string> FilterEngine::GetAllowedConnectionType() const
{
auto prefValue = GetPref("allowed_connection_type");
- if (prefValue->AsString().empty())
+ if (prefValue.AsString().empty())
return nullptr;
- return std::unique_ptr<std::string>(new std::string(prefValue->AsString()));
+ return std::unique_ptr<std::string>(new std::string(prefValue.AsString()));
}
-void FilterEngine::FilterChanged(const FilterEngine::FilterChangeCallback& callback, const JsConstValueList& params) const
+void FilterEngine::FilterChanged(const FilterEngine::FilterChangeCallback& callback, const JsValueList& params) const
{
- std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsString() : "");
- JsConstValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false));
- callback(action, *item);
+ std::string action(params.size() >= 1 && !params[0].IsNull() ? params[0].AsString() : "");
+ JsValue item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false));
+ callback(action, item);
}
void FilterEngine::ShowNotification(const ShowNotificationCallback& callback,
- const JsConstValueList& params) const
+ const JsValueList& params) const
{
if (params.size() < 1)
return;
- if (!params[0]->IsObject())
+ if (!params[0].IsObject())
{
return;
}
- callback(NotificationPtr(new Notification(params[0]->Clone())));
+ callback(NotificationPtr(new Notification(JsValue(params[0]))));
}
int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) const
{
- JsConstValueList params;
+ JsValueList params;
params.push_back(jsEngine->NewValue(v1));
params.push_back(jsEngine->NewValue(v2));
JsValuePtr func = jsEngine->Evaluate("API.compareVersions");
return func->Call(params).AsInt();
}
FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url,
ContentTypeMask contentTypeMask, const std::string& documentUrl) const
« no previous file with comments | « src/FileSystemJsObject.cpp ('k') | src/GlobalJsObject.cpp » ('j') | src/JsValue.cpp » ('J')

Powered by Google App Engine
This is Rietveld