Left: | ||
Right: |
OLD | NEW |
---|---|
1 #include <AdblockPlus.h> | 1 #include <AdblockPlus.h> |
2 | 2 |
3 using namespace AdblockPlus; | 3 using namespace AdblockPlus; |
4 | 4 |
5 #if !FILTER_ENGINE_STUBS | 5 #if !FILTER_ENGINE_STUBS |
6 extern const char* jsSources[]; | 6 extern const char* jsSources[]; |
7 #endif | 7 #endif |
8 | 8 |
9 Subscription::Subscription(const std::string& url, const std::string& title) | 9 #if FILTER_ENGINE_STUBS |
10 : url(url), title(title) | 10 Subscription::Subscription(FilterEngine& filterEngine, const std::string& url) |
11 : filterEngine(filterEngine) | |
12 { | |
13 properties["url"] = url; | |
14 } | |
15 #else | |
16 Subscription::Subscription() | |
17 { | |
18 } | |
19 #endif | |
20 | |
21 const std::string Subscription::GetProperty(const std::string& name) const | |
22 { | |
23 #if FILTER_ENGINE_STUBS | |
24 std::map<std::string,std::string>::const_iterator it = properties.find(name); | |
25 if (it == properties.end()) | |
26 return ""; | |
27 else | |
28 return it->second; | |
29 #endif | |
30 } | |
31 | |
32 int Subscription::GetIntProperty(const std::string& name) 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 0; | |
38 else | |
39 return it->second; | |
40 #endif | |
41 } | |
42 | |
43 void Subscription::SetProperty(const std::string& name, const std::string& value ) | |
44 { | |
45 #if FILTER_ENGINE_STUBS | |
46 properties[name] = value; | |
47 #endif | |
48 } | |
49 | |
50 void Subscription::SetIntProperty(const std::string& name, int value) | |
51 { | |
52 #if FILTER_ENGINE_STUBS | |
53 intProperties[name] = value; | |
54 #endif | |
55 } | |
56 | |
57 bool Subscription::IsListed() const | |
58 { | |
59 #if FILTER_ENGINE_STUBS | |
60 for (std::vector<Subscription*>::iterator it = filterEngine.listedSubscription s.begin(); | |
61 it != filterEngine.listedSubscriptions.end(); ++it) | |
62 { | |
63 if (*it == this) | |
64 return true; | |
65 } | |
66 return false; | |
67 #endif | |
68 } | |
69 | |
70 void Subscription::AddToList() | |
71 { | |
72 #if FILTER_ENGINE_STUBS | |
73 if (!IsListed()) | |
74 filterEngine.listedSubscriptions.push_back(this); | |
75 #endif | |
76 } | |
77 | |
78 void Subscription::RemoveFromList() | |
79 { | |
80 for (std::vector<Subscription*>::iterator it = filterEngine.listedSubscription s.begin(); | |
81 it != filterEngine.listedSubscriptions.end();) | |
82 { | |
83 if (*it == this) | |
84 it = filterEngine.listedSubscriptions.erase(it); | |
85 else | |
86 it++; | |
87 } | |
88 } | |
89 | |
90 void Subscription::UpdateFilters() | |
11 { | 91 { |
12 } | 92 } |
13 | 93 |
14 FilterEngine::FilterEngine(JsEngine& jsEngine) : jsEngine(jsEngine) | 94 FilterEngine::FilterEngine(JsEngine& jsEngine) : jsEngine(jsEngine) |
15 { | 95 { |
16 #if !FILTER_ENGINE_STUBS | 96 #if !FILTER_ENGINE_STUBS |
17 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2) | 97 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2) |
18 jsEngine.Evaluate(jsSources[i + 1], jsSources[i]); | 98 jsEngine.Evaluate(jsSources[i + 1], jsSources[i]); |
19 #endif | 99 #endif |
20 } | 100 } |
21 | 101 |
22 void FilterEngine::AddSubscription(Subscription subscription) | 102 Subscription& FilterEngine::GetSubscription(const std::string& url) |
23 { | 103 { |
24 subscriptions.push_back(subscription); | 104 #if FILTER_ENGINE_STUBS |
105 std::map<std::string,Subscription*>::const_iterator it = knownSubscriptions.fi nd(url); | |
106 if (it != knownSubscriptions.end()) | |
107 return *it->second; | |
108 | |
109 Subscription* result = new Subscription(*this, url); | |
110 knownSubscriptions[url] = result; | |
111 return *result; | |
112 #endif | |
25 } | 113 } |
26 | 114 |
27 void FilterEngine::RemoveSubscription(const Subscription& subscription) | 115 const std::vector<Subscription*>& FilterEngine::GetListedSubscriptions() const |
28 { | 116 { |
29 for (std::vector<Subscription>::iterator it = subscriptions.begin(); | 117 #if FILTER_ENGINE_STUBS |
30 it != subscriptions.end();) | 118 return listedSubscriptions; |
31 if (it->url == subscription.url) | 119 #endif |
32 it = subscriptions.erase(it); | |
33 else | |
34 it++; | |
35 } | 120 } |
36 | 121 |
37 const Subscription* FilterEngine::FindSubscription(const std::string& url) const | 122 void FilterEngine::FetchAvailableSubscriptions(SubscriptionsCallback callback) |
38 { | 123 { |
39 for (std::vector<Subscription>::const_iterator it = subscriptions.begin(); | 124 #if FILTER_ENGINE_STUBS |
40 it != subscriptions.end(); it++) | 125 std::vector<Subscription*> availableSubscriptions; |
41 if (it->url == url) | |
42 return &(*it); | |
43 return 0; | |
44 } | |
45 | 126 |
46 const std::vector<Subscription>& FilterEngine::GetSubscriptions() const | 127 Subscription& subscription1 = GetSubscription("https://easylist-downloads.adbl ockplus.org/easylist.txt"); |
47 { | 128 subscription1.SetProperty("title", "EasyList"); |
48 return subscriptions; | 129 availableSubscriptions.push_back(&subscription1); |
49 } | |
50 | 130 |
51 void FilterEngine::UpdateSubscriptionFilters(const Subscription& subscription) | 131 Subscription& subscription2 = GetSubscription("https://easylist-downloads.adbl ockplus.org/easylistgermany+easylist.txt"); |
52 { | 132 subscription2.SetProperty("title", "EasyList Germany+EasyList"); |
53 } | 133 availableSubscriptions.push_back(&subscription2); |
54 | 134 |
55 std::vector<Subscription> FilterEngine::FetchAvailableSubscriptions() | 135 callback(availableSubscriptions); |
56 { | 136 #endif |
57 std::vector<Subscription> availableSubscriptions; | |
58 availableSubscriptions.push_back(Subscription("https://easylist-downloads.adbl ockplus.org/easylist.txt", "EasyList")); | |
59 availableSubscriptions.push_back(Subscription("https://easylist-downloads.adbl ockplus.org/easylistgermany+easylist.txt", "EasyList Germany+EasyList")); | |
60 return availableSubscriptions; | |
61 } | 137 } |
62 | 138 |
63 bool FilterEngine::Matches(const std::string& url, | 139 bool FilterEngine::Matches(const std::string& url, |
64 const std::string& contentType) const | 140 const std::string& contentType, |
141 const std::string& documentUrl) const | |
Oleksandr
2013/04/05 07:31:51
How about returning the filter definition rule tha
Wladimir Palant
2013/04/05 12:15:16
Yes, I want to do that in the next step, while imp
| |
65 { | 142 { |
143 #if FILTER_ENGINE_STUBS | |
66 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ | 144 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ |
67 return url.find("adbanner.gif") != std::string::npos; | 145 return url.find("adbanner.gif") != std::string::npos; |
146 #endif | |
68 } | 147 } |
69 | 148 |
70 std::vector<std::string> FilterEngine::GetElementHidingRules() const | 149 std::vector<std::string> FilterEngine::GetElementHidingRules() const |
71 { | 150 { |
151 #if FILTER_ENGINE_STUBS | |
72 std::vector<std::string> hidingRules; | 152 std::vector<std::string> hidingRules; |
73 hidingRules.push_back("###ad"); | 153 hidingRules.push_back("###ad"); |
74 hidingRules.push_back("##.ad"); | 154 hidingRules.push_back("##.ad"); |
75 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ | 155 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ |
76 hidingRules.push_back("##.ad_300x250"); | 156 hidingRules.push_back("##.ad_300x250"); |
77 return hidingRules; | 157 return hidingRules; |
158 #endif | |
78 } | 159 } |
OLD | NEW |