| Index: src/FilterEngine.cpp |
| =================================================================== |
| --- a/src/FilterEngine.cpp |
| +++ b/src/FilterEngine.cpp |
| @@ -16,16 +16,17 @@ |
| */ |
| #include <algorithm> |
| #include <cctype> |
| #include <functional> |
| #include <string> |
| #include <AdblockPlus.h> |
| +#include "JsContext.h" |
| #include "Thread.h" |
| using namespace AdblockPlus; |
| extern std::string jsSources[]; |
| Filter::Filter(JsValuePtr value) |
| : JsValue(value) |
| @@ -133,18 +134,24 @@ bool Subscription::operator==(const Subs |
| return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsString(); |
| } |
| FilterEngine::FilterEngine(JsEnginePtr jsEngine) |
| : jsEngine(jsEngine), initialized(false), firstRun(false) |
| { |
| jsEngine->SetEventCallback("init", std::tr1::bind(&FilterEngine::InitDone, |
| this, std::tr1::placeholders::_1)); |
| - for (int i = 0; !jsSources[i].empty(); i += 2) |
| - jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); |
| + |
| + { |
| + // Lock the JS engine while we are loading scripts, no timeouts should fire |
| + // until we are done. |
| + const JsContext context(jsEngine); |
| + for (int i = 0; !jsSources[i].empty(); i += 2) |
| + jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); |
| + } |
| // TODO: This should really be implemented via a conditional variable |
| while (!initialized) |
| ::Sleep(10); |
| } |
| void FilterEngine::InitDone(JsValueList& params) |
| { |
| @@ -201,17 +208,17 @@ std::vector<SubscriptionPtr> FilterEngin |
| std::vector<SubscriptionPtr> result; |
| for (JsValueList::iterator it = values.begin(); it != values.end(); it++) |
| result.push_back(SubscriptionPtr(new Subscription(*it))); |
| return result; |
| } |
| AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, |
| const std::string& contentType, |
| - const std::string& documentUrl) |
| + const std::string& documentUrl) const |
| { |
| 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)); |
| JsValuePtr result = func->Call(params); |
| if (!result->IsNull()) |
| @@ -226,8 +233,25 @@ std::vector<std::string> FilterEngine::G |
| JsValueList params; |
| 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; |
| } |
| + |
| +JsValuePtr FilterEngine::GetPref(const std::string& pref) const |
| +{ |
| + JsValuePtr func = jsEngine->Evaluate("API.getPref"); |
| + JsValueList params; |
| + params.push_back(jsEngine->NewValue(pref)); |
| + return func->Call(params); |
| +} |
| + |
| +void FilterEngine::SetPref(const std::string& pref, JsValuePtr value) |
| +{ |
| + JsValuePtr func = jsEngine->Evaluate("API.setPref"); |
| + JsValueList params; |
| + params.push_back(jsEngine->NewValue(pref)); |
| + params.push_back(value); |
| + func->Call(params); |
| +} |