Left: | ||
Right: |
OLD | NEW |
---|---|
1 #include <algorithm> | |
1 #include <AdblockPlus.h> | 2 #include <AdblockPlus.h> |
2 | 3 |
3 using namespace AdblockPlus; | 4 using namespace AdblockPlus; |
4 | 5 |
5 #if !FILTER_ENGINE_STUBS | 6 #if !FILTER_ENGINE_STUBS |
6 extern const char* jsSources[]; | 7 extern const char* jsSources[]; |
7 #endif | 8 #endif |
8 | 9 |
9 Subscription::Subscription(const std::string& url, const std::string& title) | 10 #if FILTER_ENGINE_STUBS |
10 : url(url), title(title) | 11 JSObject::JSObject(FilterEngine& filterEngine) |
12 : filterEngine(filterEngine) | |
13 { | |
14 } | |
15 #else | |
16 JSObject::JSObject() | |
17 { | |
18 } | |
19 #endif | |
20 | |
21 std::string JSObject::GetProperty(const std::string& name, const std::string& de faultValue) const | |
22 { | |
23 #if FILTER_ENGINE_STUBS | |
24 std::map<std::string, std::string>::const_iterator it = stringProperties.find( name); | |
25 if (it == stringProperties.end()) | |
26 return defaultValue; | |
27 else | |
28 return it->second; | |
29 #endif | |
30 } | |
31 | |
32 int JSObject::GetProperty(const std::string& name, int defaultValue) const | |
33 { | |
34 #if FILTER_ENGINE_STUBS | |
35 std::map<std::string, int>::const_iterator it = intProperties.find(name); | |
36 if (it == intProperties.end()) | |
37 return defaultValue; | |
38 else | |
39 return it->second; | |
40 #endif | |
41 } | |
42 | |
43 bool JSObject::GetProperty(const std::string& name, bool defaultValue) const | |
44 { | |
45 #if FILTER_ENGINE_STUBS | |
46 std::map<std::string, bool>::const_iterator it = boolProperties.find(name); | |
47 if (it == boolProperties.end()) | |
48 return defaultValue; | |
49 else | |
50 return it->second; | |
51 #endif | |
52 } | |
53 | |
54 void JSObject::SetProperty(const std::string& name, const std::string& value) | |
55 { | |
56 #if FILTER_ENGINE_STUBS | |
57 stringProperties[name] = value; | |
58 #endif | |
59 } | |
60 | |
61 void JSObject::SetProperty(const std::string& name, int value) | |
62 { | |
63 #if FILTER_ENGINE_STUBS | |
64 intProperties[name] = value; | |
65 #endif | |
66 } | |
67 | |
68 void JSObject::SetProperty(const std::string& name, bool value) | |
69 { | |
70 #if FILTER_ENGINE_STUBS | |
71 boolProperties[name] = value; | |
72 #endif | |
73 } | |
74 | |
75 #if FILTER_ENGINE_STUBS | |
76 Filter::Filter(FilterEngine& filterEngine, const std::string& text) | |
77 : JSObject(filterEngine) | |
78 { | |
79 SetProperty("text", text); | |
80 if (text.find("!") == 0) | |
81 SetProperty("type", "comment"); | |
Felix Dahlke
2013/04/06 05:17:05
Have you considered having an enum for the type? W
| |
82 else if (text.find("@@") == 0) | |
83 SetProperty("type", "exception"); | |
84 else if (text.find("#@") != std::string::npos) | |
85 SetProperty("type", "elemhideexception"); | |
86 else if (text.find("#") != std::string::npos) | |
87 SetProperty("type", "elemhide"); | |
88 else | |
89 SetProperty("type", "blocking"); | |
90 } | |
91 #else | |
92 Filter::Filter() | |
93 { | |
94 } | |
95 #endif | |
96 | |
97 bool Filter::IsListed() const | |
98 { | |
99 #if FILTER_ENGINE_STUBS | |
100 for (std::vector<Filter*>::iterator it = filterEngine.listedFilters.begin(); | |
101 it != filterEngine.listedFilters.end(); ++it) | |
102 { | |
103 if (*it == this) | |
104 return true; | |
105 } | |
106 return false; | |
107 #endif | |
108 } | |
109 | |
110 void Filter::AddToList() | |
111 { | |
112 #if FILTER_ENGINE_STUBS | |
113 if (!IsListed()) | |
114 filterEngine.listedFilters.push_back(this); | |
115 #endif | |
116 } | |
117 | |
118 void Filter::RemoveFromList() | |
119 { | |
120 for (std::vector<Filter*>::iterator it = filterEngine.listedFilters.begin(); | |
121 it != filterEngine.listedFilters.end();) | |
122 { | |
123 if (*it == this) | |
124 it = filterEngine.listedFilters.erase(it); | |
125 else | |
126 it++; | |
127 } | |
128 } | |
129 | |
130 #if FILTER_ENGINE_STUBS | |
131 Subscription::Subscription(FilterEngine& filterEngine, const std::string& url) | |
132 : JSObject(filterEngine) | |
133 { | |
134 SetProperty("url", url); | |
135 } | |
136 #else | |
137 Subscription::Subscription() | |
138 { | |
139 } | |
140 #endif | |
141 | |
142 bool Subscription::IsListed() const | |
143 { | |
144 #if FILTER_ENGINE_STUBS | |
145 for (std::vector<Subscription*>::iterator it = filterEngine.listedSubscription s.begin(); | |
146 it != filterEngine.listedSubscriptions.end(); ++it) | |
147 { | |
148 if (*it == this) | |
149 return true; | |
150 } | |
151 return false; | |
152 #endif | |
153 } | |
154 | |
155 void Subscription::AddToList() | |
156 { | |
157 #if FILTER_ENGINE_STUBS | |
158 if (!IsListed()) | |
159 filterEngine.listedSubscriptions.push_back(this); | |
160 #endif | |
161 } | |
162 | |
163 void Subscription::RemoveFromList() | |
164 { | |
165 for (std::vector<Subscription*>::iterator it = filterEngine.listedSubscription s.begin(); | |
166 it != filterEngine.listedSubscriptions.end();) | |
167 { | |
168 if (*it == this) | |
169 it = filterEngine.listedSubscriptions.erase(it); | |
170 else | |
171 it++; | |
172 } | |
173 } | |
174 | |
175 void Subscription::UpdateFilters() | |
11 { | 176 { |
12 } | 177 } |
13 | 178 |
14 FilterEngine::FilterEngine(JsEngine& jsEngine) : jsEngine(jsEngine) | 179 FilterEngine::FilterEngine(JsEngine& jsEngine) : jsEngine(jsEngine) |
15 { | 180 { |
16 #if !FILTER_ENGINE_STUBS | 181 #if !FILTER_ENGINE_STUBS |
17 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2) | 182 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2) |
18 jsEngine.Evaluate(jsSources[i + 1], jsSources[i]); | 183 jsEngine.Evaluate(jsSources[i + 1], jsSources[i]); |
19 #endif | 184 #endif |
20 } | 185 } |
21 | 186 |
22 void FilterEngine::AddSubscription(Subscription subscription) | 187 Filter& FilterEngine::GetFilter(const std::string& text) |
23 { | 188 { |
24 subscriptions.push_back(subscription); | 189 #if FILTER_ENGINE_STUBS |
25 } | 190 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring |
26 | 191 std::string trimmed(text); |
27 void FilterEngine::RemoveSubscription(const Subscription& subscription) | 192 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st d::not1(std::ptr_fun<int, int>(std::isspace)))); |
28 { | 193 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std::pt r_fun<int, int>(std::isspace))).base(), trimmed.end()); |
29 for (std::vector<Subscription>::iterator it = subscriptions.begin(); | 194 |
30 it != subscriptions.end();) | 195 std::map<std::string, Filter*>::const_iterator it = knownFilters.find(trimmed) ; |
31 if (it->url == subscription.url) | 196 if (it != knownFilters.end()) |
32 it = subscriptions.erase(it); | 197 return *it->second; |
33 else | 198 |
34 it++; | 199 Filter* result = new Filter(*this, trimmed); |
35 } | 200 knownFilters[trimmed] = result; |
Oleksandr
2013/04/05 14:09:07
I was not able to understand who and when is doing
Wladimir Palant
2013/04/05 14:31:45
The C++ wrappers are cached for the entire life ti
Oleksandr
2013/04/05 15:14:12
So I take it as no one deletes it - it is just cle
Felix Dahlke
2013/04/06 05:17:05
I'm with Oleksandr here, would prefer it if Filter
Wladimir Palant
2013/04/08 13:51:47
Now that this map stores shared_ptr instances clea
| |
36 | 201 return *result; |
37 const Subscription* FilterEngine::FindSubscription(const std::string& url) const | 202 #endif |
38 { | 203 } |
39 for (std::vector<Subscription>::const_iterator it = subscriptions.begin(); | 204 |
40 it != subscriptions.end(); it++) | 205 Subscription& FilterEngine::GetSubscription(const std::string& url) |
41 if (it->url == url) | 206 { |
42 return &(*it); | 207 #if FILTER_ENGINE_STUBS |
43 return 0; | 208 std::map<std::string, Subscription*>::const_iterator it = knownSubscriptions.f ind(url); |
44 } | 209 if (it != knownSubscriptions.end()) |
45 | 210 return *it->second; |
46 const std::vector<Subscription>& FilterEngine::GetSubscriptions() const | 211 |
47 { | 212 Subscription* result = new Subscription(*this, url); |
48 return subscriptions; | 213 knownSubscriptions[url] = result; |
49 } | 214 return *result; |
50 | 215 #endif |
51 void FilterEngine::UpdateSubscriptionFilters(const Subscription& subscription) | 216 } |
52 { | 217 |
53 } | 218 const std::vector<Filter*>& FilterEngine::GetListedFilters() const |
54 | 219 { |
55 std::vector<Subscription> FilterEngine::FetchAvailableSubscriptions() | 220 #if FILTER_ENGINE_STUBS |
56 { | 221 return listedFilters; |
57 std::vector<Subscription> availableSubscriptions; | 222 #endif |
58 availableSubscriptions.push_back(Subscription("https://easylist-downloads.adbl ockplus.org/easylist.txt", "EasyList")); | 223 } |
59 availableSubscriptions.push_back(Subscription("https://easylist-downloads.adbl ockplus.org/easylistgermany+easylist.txt", "EasyList Germany+EasyList")); | 224 |
60 return availableSubscriptions; | 225 const std::vector<Subscription*>& FilterEngine::GetListedSubscriptions() const |
61 } | 226 { |
62 | 227 #if FILTER_ENGINE_STUBS |
63 bool FilterEngine::Matches(const std::string& url, | 228 return listedSubscriptions; |
64 const std::string& contentType) const | 229 #endif |
65 { | 230 } |
231 | |
232 void FilterEngine::FetchAvailableSubscriptions(SubscriptionsCallback callback) | |
233 { | |
234 #if FILTER_ENGINE_STUBS | |
235 std::vector<Subscription*> availableSubscriptions; | |
236 | |
237 Subscription& subscription1 = GetSubscription("https://easylist-downloads.adbl ockplus.org/easylist.txt"); | |
238 subscription1.SetProperty("title", "EasyList"); | |
239 availableSubscriptions.push_back(&subscription1); | |
240 | |
241 Subscription& subscription2 = GetSubscription("https://easylist-downloads.adbl ockplus.org/easylistgermany+easylist.txt"); | |
242 subscription2.SetProperty("title", "EasyList Germany+EasyList"); | |
243 availableSubscriptions.push_back(&subscription2); | |
244 | |
245 callback(availableSubscriptions); | |
246 #endif | |
247 } | |
248 | |
249 Filter* FilterEngine::Matches(const std::string& url, | |
250 const std::string& contentType, | |
251 const std::string& documentUrl) | |
252 { | |
253 #if FILTER_ENGINE_STUBS | |
66 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ | 254 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ |
67 return url.find("adbanner.gif") != std::string::npos; | 255 if (url.find("adbanner.gif") != std::string::npos) |
68 } | 256 return &GetFilter("adbanner.gif"); |
69 | 257 else if (url.find("notbanner.gif") != std::string::npos) |
70 std::vector<std::string> FilterEngine::GetElementHidingRules() const | 258 return &GetFilter("@@notbanner.gif"); |
71 { | 259 else |
72 std::vector<std::string> hidingRules; | 260 return 0; |
73 hidingRules.push_back("###ad"); | 261 #endif |
74 hidingRules.push_back("##.ad"); | 262 } |
263 | |
264 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri ng& domain) const | |
265 { | |
266 #if FILTER_ENGINE_STUBS | |
267 std::vector<std::string> selectors; | |
268 selectors.push_back("#ad"); | |
269 selectors.push_back(".ad"); | |
75 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ | 270 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ |
76 hidingRules.push_back("##.ad_300x250"); | 271 if (domain == "simple-adblock.com") |
77 return hidingRules; | 272 selectors.push_back(".ad_300x250"); |
78 } | 273 return selectors; |
274 #endif | |
275 } | |
OLD | NEW |