Index: src/FilterEngine.cpp |
=================================================================== |
--- a/src/FilterEngine.cpp |
+++ b/src/FilterEngine.cpp |
@@ -3,69 +3,38 @@ |
#include <functional> |
#include <AdblockPlus.h> |
using namespace AdblockPlus; |
extern const char* jsSources[]; |
-JsObject::JsObject(JsValuePtr value) |
- : JsValue(value->jsEngine, value->value) |
+Filter::Filter(JsValuePtr value) |
+ : JsValue(value) |
{ |
if (!IsObject()) |
throw std::runtime_error("JavaScript value is not an object"); |
} |
-std::string JsObject::GetProperty(const std::string& name, const std::string& defaultValue) const |
+Filter::Type Filter::GetType() |
{ |
- JsValuePtr value = JsValue::GetProperty(name); |
- if (value->IsString()) |
- return value->AsString(); |
+ std::string className = GetClassName(); |
+ if (className == "BlockingFilter") |
+ return TYPE_BLOCKING; |
+ else if (className == "WhitelistFilter") |
+ return TYPE_EXCEPTION; |
+ else if (className == "ElemHideFilter") |
+ return TYPE_ELEMHIDE; |
+ else if (className == "ElemHideException") |
+ return TYPE_ELEMHIDE_EXCEPTION; |
+ else if (className == "CommentFilter") |
+ return TYPE_COMMENT; |
else |
- return defaultValue; |
-} |
- |
-int64_t JsObject::GetProperty(const std::string& name, int64_t defaultValue) const |
-{ |
- JsValuePtr value = JsValue::GetProperty(name); |
- if (value->IsNumber()) |
- return value->AsInt(); |
- else |
- return defaultValue; |
-} |
- |
-bool JsObject::GetProperty(const std::string& name, bool defaultValue) const |
-{ |
- JsValuePtr value = JsValue::GetProperty(name); |
- if (value->IsBool()) |
- return value->AsBool(); |
- else |
- return defaultValue; |
-} |
- |
-Filter::Filter(JsValuePtr value) |
- : JsObject(value) |
-{ |
- // Hack: set `type` property according to class name |
- std::string className = GetClassName(); |
- Type type; |
- if (className == "BlockingFilter") |
- type = TYPE_BLOCKING; |
- else if (className == "WhitelistFilter") |
- type = TYPE_EXCEPTION; |
- else if (className == "ElemHideFilter") |
- type = TYPE_ELEMHIDE; |
- else if (className == "ElemHideException") |
- type = TYPE_ELEMHIDE_EXCEPTION; |
- else if (className == "CommentFilter") |
- type = TYPE_COMMENT; |
- else |
- type = TYPE_INVALID; |
- SetProperty("type", type); |
+ return TYPE_INVALID; |
} |
bool Filter::IsListed() |
{ |
JsValuePtr func = jsEngine->Evaluate("API.isListedFilter"); |
JsValueList params; |
params.push_back(shared_from_this()); |
return func->Call(params)->AsBool(); |
@@ -84,22 +53,24 @@ void Filter::RemoveFromList() |
JsValuePtr func = jsEngine->Evaluate("API.removeFilterFromList"); |
JsValueList params; |
params.push_back(shared_from_this()); |
func->Call(params); |
} |
bool Filter::operator==(const Filter& filter) const |
{ |
- return GetProperty("text", "") == filter.GetProperty("text", ""); |
+ return GetProperty("text")->AsString() == filter.GetProperty("text")->AsString(); |
} |
Subscription::Subscription(JsValuePtr value) |
- : JsObject(value) |
+ : JsValue(value) |
{ |
+ if (!IsObject()) |
+ throw std::runtime_error("JavaScript value is not an object"); |
} |
bool Subscription::IsListed() |
{ |
JsValuePtr func = jsEngine->Evaluate("API.isListedFilter"); |
JsValueList params; |
params.push_back(shared_from_this()); |
return func->Call(params)->AsBool(); |
@@ -135,17 +106,17 @@ bool Subscription::IsUpdating() |
JsValueList params; |
params.push_back(shared_from_this()); |
JsValuePtr result = func->Call(params); |
return result->AsBool(); |
} |
bool Subscription::operator==(const Subscription& subscription) const |
{ |
- return GetProperty("url", "") == subscription.GetProperty("url", ""); |
+ return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsString(); |
} |
FilterEngine::FilterEngine(JsEnginePtr jsEngine) : jsEngine(jsEngine) |
{ |
for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2) |
jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); |
} |