OLD | NEW |
1 #include <algorithm> | 1 #include <algorithm> |
2 #include <cctype> | 2 #include <cctype> |
3 #include <functional> | 3 #include <functional> |
4 | 4 |
5 #include <AdblockPlus.h> | 5 #include <AdblockPlus.h> |
6 | 6 |
7 using namespace AdblockPlus; | 7 using namespace AdblockPlus; |
8 | 8 |
9 extern const char* jsSources[]; | 9 extern const char* jsSources[]; |
10 | 10 |
11 JsObject::JsObject(JsValuePtr value) | 11 Filter::Filter(JsValuePtr value) |
12 : JsValue(value->jsEngine, value->value) | 12 : JsValue(value) |
13 { | 13 { |
14 if (!IsObject()) | 14 if (!IsObject()) |
15 throw std::runtime_error("JavaScript value is not an object"); | 15 throw std::runtime_error("JavaScript value is not an object"); |
16 } | 16 } |
17 | 17 |
18 std::string JsObject::GetProperty(const std::string& name, const std::string& de
faultValue) const | 18 Filter::Type Filter::GetType() |
19 { | 19 { |
20 JsValuePtr value = JsValue::GetProperty(name); | 20 std::string className = GetClassName(); |
21 if (value->IsString()) | 21 if (className == "BlockingFilter") |
22 return value->AsString(); | 22 return TYPE_BLOCKING; |
| 23 else if (className == "WhitelistFilter") |
| 24 return TYPE_EXCEPTION; |
| 25 else if (className == "ElemHideFilter") |
| 26 return TYPE_ELEMHIDE; |
| 27 else if (className == "ElemHideException") |
| 28 return TYPE_ELEMHIDE_EXCEPTION; |
| 29 else if (className == "CommentFilter") |
| 30 return TYPE_COMMENT; |
23 else | 31 else |
24 return defaultValue; | 32 return TYPE_INVALID; |
25 } | |
26 | |
27 int64_t JsObject::GetProperty(const std::string& name, int64_t defaultValue) con
st | |
28 { | |
29 JsValuePtr value = JsValue::GetProperty(name); | |
30 if (value->IsNumber()) | |
31 return value->AsInt(); | |
32 else | |
33 return defaultValue; | |
34 } | |
35 | |
36 bool JsObject::GetProperty(const std::string& name, bool defaultValue) const | |
37 { | |
38 JsValuePtr value = JsValue::GetProperty(name); | |
39 if (value->IsBool()) | |
40 return value->AsBool(); | |
41 else | |
42 return defaultValue; | |
43 } | |
44 | |
45 Filter::Filter(JsValuePtr value) | |
46 : JsObject(value) | |
47 { | |
48 // Hack: set `type` property according to class name | |
49 std::string className = GetClassName(); | |
50 Type type; | |
51 if (className == "BlockingFilter") | |
52 type = TYPE_BLOCKING; | |
53 else if (className == "WhitelistFilter") | |
54 type = TYPE_EXCEPTION; | |
55 else if (className == "ElemHideFilter") | |
56 type = TYPE_ELEMHIDE; | |
57 else if (className == "ElemHideException") | |
58 type = TYPE_ELEMHIDE_EXCEPTION; | |
59 else if (className == "CommentFilter") | |
60 type = TYPE_COMMENT; | |
61 else | |
62 type = TYPE_INVALID; | |
63 SetProperty("type", type); | |
64 } | 33 } |
65 | 34 |
66 bool Filter::IsListed() | 35 bool Filter::IsListed() |
67 { | 36 { |
68 JsValuePtr func = jsEngine->Evaluate("API.isListedFilter"); | 37 JsValuePtr func = jsEngine->Evaluate("API.isListedFilter"); |
69 JsValueList params; | 38 JsValueList params; |
70 params.push_back(shared_from_this()); | 39 params.push_back(shared_from_this()); |
71 return func->Call(params)->AsBool(); | 40 return func->Call(params)->AsBool(); |
72 } | 41 } |
73 | 42 |
74 void Filter::AddToList() | 43 void Filter::AddToList() |
75 { | 44 { |
76 JsValuePtr func = jsEngine->Evaluate("API.addFilterToList"); | 45 JsValuePtr func = jsEngine->Evaluate("API.addFilterToList"); |
77 JsValueList params; | 46 JsValueList params; |
78 params.push_back(shared_from_this()); | 47 params.push_back(shared_from_this()); |
79 func->Call(params); | 48 func->Call(params); |
80 } | 49 } |
81 | 50 |
82 void Filter::RemoveFromList() | 51 void Filter::RemoveFromList() |
83 { | 52 { |
84 JsValuePtr func = jsEngine->Evaluate("API.removeFilterFromList"); | 53 JsValuePtr func = jsEngine->Evaluate("API.removeFilterFromList"); |
85 JsValueList params; | 54 JsValueList params; |
86 params.push_back(shared_from_this()); | 55 params.push_back(shared_from_this()); |
87 func->Call(params); | 56 func->Call(params); |
88 } | 57 } |
89 | 58 |
90 bool Filter::operator==(const Filter& filter) const | 59 bool Filter::operator==(const Filter& filter) const |
91 { | 60 { |
92 return GetProperty("text", "") == filter.GetProperty("text", ""); | 61 return GetProperty("text")->AsString() == filter.GetProperty("text")->AsString
(); |
93 } | 62 } |
94 | 63 |
95 Subscription::Subscription(JsValuePtr value) | 64 Subscription::Subscription(JsValuePtr value) |
96 : JsObject(value) | 65 : JsValue(value) |
97 { | 66 { |
| 67 if (!IsObject()) |
| 68 throw std::runtime_error("JavaScript value is not an object"); |
98 } | 69 } |
99 | 70 |
100 bool Subscription::IsListed() | 71 bool Subscription::IsListed() |
101 { | 72 { |
102 JsValuePtr func = jsEngine->Evaluate("API.isListedFilter"); | 73 JsValuePtr func = jsEngine->Evaluate("API.isListedFilter"); |
103 JsValueList params; | 74 JsValueList params; |
104 params.push_back(shared_from_this()); | 75 params.push_back(shared_from_this()); |
105 return func->Call(params)->AsBool(); | 76 return func->Call(params)->AsBool(); |
106 } | 77 } |
107 | 78 |
(...skipping 25 matching lines...) Expand all Loading... |
133 { | 104 { |
134 JsValuePtr func = jsEngine->Evaluate("API.isSubscriptionUpdating"); | 105 JsValuePtr func = jsEngine->Evaluate("API.isSubscriptionUpdating"); |
135 JsValueList params; | 106 JsValueList params; |
136 params.push_back(shared_from_this()); | 107 params.push_back(shared_from_this()); |
137 JsValuePtr result = func->Call(params); | 108 JsValuePtr result = func->Call(params); |
138 return result->AsBool(); | 109 return result->AsBool(); |
139 } | 110 } |
140 | 111 |
141 bool Subscription::operator==(const Subscription& subscription) const | 112 bool Subscription::operator==(const Subscription& subscription) const |
142 { | 113 { |
143 return GetProperty("url", "") == subscription.GetProperty("url", ""); | 114 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt
ring(); |
144 } | 115 } |
145 | 116 |
146 FilterEngine::FilterEngine(JsEnginePtr jsEngine) : jsEngine(jsEngine) | 117 FilterEngine::FilterEngine(JsEnginePtr jsEngine) : jsEngine(jsEngine) |
147 { | 118 { |
148 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2) | 119 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2) |
149 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); | 120 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); |
150 } | 121 } |
151 | 122 |
152 FilterPtr FilterEngine::GetFilter(const std::string& text) | 123 FilterPtr FilterEngine::GetFilter(const std::string& text) |
153 { | 124 { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 { | 186 { |
216 JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors"); | 187 JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors"); |
217 JsValueList params; | 188 JsValueList params; |
218 params.push_back(jsEngine->NewValue(domain)); | 189 params.push_back(jsEngine->NewValue(domain)); |
219 JsValueList result = func->Call(params)->AsList(); | 190 JsValueList result = func->Call(params)->AsList(); |
220 std::vector<std::string> selectors; | 191 std::vector<std::string> selectors; |
221 for (JsValueList::iterator it = result.begin(); it != result.end(); ++it) | 192 for (JsValueList::iterator it = result.begin(); it != result.end(); ++it) |
222 selectors.push_back((*it)->AsString()); | 193 selectors.push_back((*it)->AsString()); |
223 return selectors; | 194 return selectors; |
224 } | 195 } |
OLD | NEW |