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 #if !FILTER_ENGINE_STUBS | 9 #if !FILTER_ENGINE_STUBS |
10 extern const char* jsSources[]; | 10 extern const char* jsSources[]; |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 } | 180 } |
181 | 181 |
182 FilterEngine::FilterEngine(JsEngine& jsEngine) : jsEngine(jsEngine) | 182 FilterEngine::FilterEngine(JsEngine& jsEngine) : jsEngine(jsEngine) |
183 { | 183 { |
184 #if !FILTER_ENGINE_STUBS | 184 #if !FILTER_ENGINE_STUBS |
185 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2) | 185 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2) |
186 jsEngine.Evaluate(jsSources[i + 1], jsSources[i]); | 186 jsEngine.Evaluate(jsSources[i + 1], jsSources[i]); |
187 #endif | 187 #endif |
188 } | 188 } |
189 | 189 |
190 Filter& FilterEngine::GetFilter(const std::string& text) | 190 FilterPtr FilterEngine::GetFilter(const std::string& text) |
191 { | 191 { |
192 #if FILTER_ENGINE_STUBS | 192 #if FILTER_ENGINE_STUBS |
193 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st
dstring | 193 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st
dstring |
194 std::string trimmed(text); | 194 std::string trimmed(text); |
195 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st
d::not1(std::ptr_fun<int, int>(std::isspace)))); | 195 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st
d::not1(std::ptr_fun<int, int>(std::isspace)))); |
196 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std::pt
r_fun<int, int>(std::isspace))).base(), trimmed.end()); | 196 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std::pt
r_fun<int, int>(std::isspace))).base(), trimmed.end()); |
197 | 197 |
198 std::map<std::string, FilterPtr>::const_iterator it = knownFilters.find(trimme
d); | 198 std::map<std::string, FilterPtr>::const_iterator it = knownFilters.find(trimme
d); |
199 if (it != knownFilters.end()) | 199 if (it != knownFilters.end()) |
200 return *it->second; | 200 return it->second; |
201 | 201 |
202 FilterPtr result(new Filter(*this, trimmed)); | 202 FilterPtr result(new Filter(*this, trimmed)); |
203 knownFilters[trimmed] = result->shared_from_this(); | 203 knownFilters[trimmed] = result; |
204 return *result; | 204 return result; |
205 #endif | 205 #endif |
206 } | 206 } |
207 | 207 |
208 Subscription& FilterEngine::GetSubscription(const std::string& url) | 208 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url) |
209 { | 209 { |
210 #if FILTER_ENGINE_STUBS | 210 #if FILTER_ENGINE_STUBS |
211 std::map<std::string, SubscriptionPtr>::const_iterator it = knownSubscriptions
.find(url); | 211 std::map<std::string, SubscriptionPtr>::const_iterator it = knownSubscriptions
.find(url); |
212 if (it != knownSubscriptions.end()) | 212 if (it != knownSubscriptions.end()) |
213 return *it->second; | 213 return it->second; |
214 | 214 |
215 SubscriptionPtr result(new Subscription(*this, url)); | 215 SubscriptionPtr result(new Subscription(*this, url)); |
216 knownSubscriptions[url] = result->shared_from_this(); | 216 knownSubscriptions[url] = result; |
217 return *result; | 217 return result; |
218 #endif | 218 #endif |
219 } | 219 } |
220 | 220 |
221 const std::vector<FilterPtr>& FilterEngine::GetListedFilters() const | 221 const std::vector<FilterPtr>& FilterEngine::GetListedFilters() const |
222 { | 222 { |
223 #if FILTER_ENGINE_STUBS | 223 #if FILTER_ENGINE_STUBS |
224 return listedFilters; | 224 return listedFilters; |
225 #endif | 225 #endif |
226 } | 226 } |
227 | 227 |
228 const std::vector<SubscriptionPtr>& FilterEngine::GetListedSubscriptions() const | 228 const std::vector<SubscriptionPtr>& FilterEngine::GetListedSubscriptions() const |
229 { | 229 { |
230 #if FILTER_ENGINE_STUBS | 230 #if FILTER_ENGINE_STUBS |
231 return listedSubscriptions; | 231 return listedSubscriptions; |
232 #endif | 232 #endif |
233 } | 233 } |
234 | 234 |
235 void FilterEngine::FetchAvailableSubscriptions(SubscriptionsCallback callback) | 235 void FilterEngine::FetchAvailableSubscriptions(SubscriptionsCallback callback) |
236 { | 236 { |
237 #if FILTER_ENGINE_STUBS | 237 #if FILTER_ENGINE_STUBS |
238 std::vector<SubscriptionPtr> availableSubscriptions; | 238 std::vector<SubscriptionPtr> availableSubscriptions; |
239 | 239 |
240 Subscription& subscription1 = GetSubscription("https://easylist-downloads.adbl
ockplus.org/easylist.txt"); | 240 SubscriptionPtr subscription1 = GetSubscription("https://easylist-downloads.ad
blockplus.org/easylist.txt"); |
241 subscription1.SetProperty("title", "EasyList"); | 241 subscription1->SetProperty("title", "EasyList"); |
242 availableSubscriptions.push_back(subscription1.shared_from_this()); | 242 availableSubscriptions.push_back(subscription1); |
243 | 243 |
244 Subscription& subscription2 = GetSubscription("https://easylist-downloads.adbl
ockplus.org/easylistgermany+easylist.txt"); | 244 SubscriptionPtr subscription2 = GetSubscription("https://easylist-downloads.ad
blockplus.org/easylistgermany+easylist.txt"); |
245 subscription2.SetProperty("title", "EasyList Germany+EasyList"); | 245 subscription2->SetProperty("title", "EasyList Germany+EasyList"); |
246 availableSubscriptions.push_back(subscription2.shared_from_this()); | 246 availableSubscriptions.push_back(subscription2); |
247 | 247 |
248 callback(availableSubscriptions); | 248 callback(availableSubscriptions); |
249 #endif | 249 #endif |
250 } | 250 } |
251 | 251 |
252 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, | 252 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, |
253 const std::string& contentType, | 253 const std::string& contentType, |
254 const std::string& documentUrl) | 254 const std::string& documentUrl) |
255 { | 255 { |
256 #if FILTER_ENGINE_STUBS | 256 #if FILTER_ENGINE_STUBS |
257 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ | 257 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ |
258 if (url.find("adbanner.gif") != std::string::npos) | 258 if (url.find("adbanner.gif") != std::string::npos) |
259 return GetFilter("adbanner.gif").shared_from_this(); | 259 return GetFilter("adbanner.gif"); |
260 else if (url.find("notbanner.gif") != std::string::npos) | 260 else if (url.find("notbanner.gif") != std::string::npos) |
261 return GetFilter("@@notbanner.gif").shared_from_this(); | 261 return GetFilter("@@notbanner.gif"); |
262 else | 262 else |
263 return AdblockPlus::FilterPtr(); | 263 return AdblockPlus::FilterPtr(); |
264 #endif | 264 #endif |
265 } | 265 } |
266 | 266 |
267 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri
ng& domain) const | 267 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri
ng& domain) const |
268 { | 268 { |
269 #if FILTER_ENGINE_STUBS | 269 #if FILTER_ENGINE_STUBS |
270 std::vector<std::string> selectors; | 270 std::vector<std::string> selectors; |
271 selectors.push_back("#ad"); | 271 selectors.push_back("#ad"); |
272 selectors.push_back(".ad"); | 272 selectors.push_back(".ad"); |
273 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ | 273 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ |
274 if (domain == "simple-adblock.com") | 274 if (domain == "simple-adblock.com") |
275 selectors.push_back(".ad_300x250"); | 275 selectors.push_back(".ad_300x250"); |
276 return selectors; | 276 return selectors; |
277 #endif | 277 #endif |
278 } | 278 } |
OLD | NEW |