Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: src/FilterEngine.cpp

Issue 10100009: FilterEngine API improvements (Closed)
Left Patch Set: Created April 4, 2013, 5:04 p.m.
Right Patch Set: Changed filter type enum Created April 9, 2013, 5:55 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « shell/src/FiltersCommand.cpp ('k') | test/FilterEngineStubs.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 #if FILTER_ENGINE_STUBS 10 #if FILTER_ENGINE_STUBS
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", TYPE_COMMENT);
82 else if (text.find("@@") == 0)
83 SetProperty("type", TYPE_EXCEPTION);
84 else if (text.find("#@") != std::string::npos)
85 SetProperty("type", TYPE_ELEMHIDE_EXCEPTION);
86 else if (text.find("#") != std::string::npos)
87 SetProperty("type", TYPE_ELEMHIDE);
88 else
89 SetProperty("type", 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<FilterPtr>::iterator it = filterEngine.listedFilters.begin();
101 it != filterEngine.listedFilters.end(); ++it)
102 {
103 if (it->get() == 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(shared_from_this());
115 #endif
116 }
117
118 void Filter::RemoveFromList()
119 {
120 for (std::vector<FilterPtr>::iterator it = filterEngine.listedFilters.begin();
121 it != filterEngine.listedFilters.end();)
122 {
123 if (it->get() == this)
124 it = filterEngine.listedFilters.erase(it);
125 else
126 it++;
127 }
128 }
129
130 #if FILTER_ENGINE_STUBS
10 Subscription::Subscription(FilterEngine& filterEngine, const std::string& url) 131 Subscription::Subscription(FilterEngine& filterEngine, const std::string& url)
11 : filterEngine(filterEngine) 132 : JsObject(filterEngine)
12 { 133 {
13 properties["url"] = url; 134 SetProperty("url", url);
14 } 135 }
15 #else 136 #else
16 Subscription::Subscription() 137 Subscription::Subscription()
17 { 138 {
18 } 139 }
19 #endif 140 #endif
20 141
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 142 bool Subscription::IsListed() const
58 { 143 {
59 #if FILTER_ENGINE_STUBS 144 #if FILTER_ENGINE_STUBS
60 for (std::vector<Subscription*>::iterator it = filterEngine.listedSubscription s.begin(); 145 for (std::vector<SubscriptionPtr>::iterator it = filterEngine.listedSubscripti ons.begin();
61 it != filterEngine.listedSubscriptions.end(); ++it) 146 it != filterEngine.listedSubscriptions.end(); ++it)
62 { 147 {
63 if (*it == this) 148 if (it->get() == this)
64 return true; 149 return true;
65 } 150 }
66 return false; 151 return false;
67 #endif 152 #endif
68 } 153 }
69 154
70 void Subscription::AddToList() 155 void Subscription::AddToList()
71 { 156 {
72 #if FILTER_ENGINE_STUBS 157 #if FILTER_ENGINE_STUBS
73 if (!IsListed()) 158 if (!IsListed())
74 filterEngine.listedSubscriptions.push_back(this); 159 filterEngine.listedSubscriptions.push_back(shared_from_this());
75 #endif 160 #endif
76 } 161 }
77 162
78 void Subscription::RemoveFromList() 163 void Subscription::RemoveFromList()
79 { 164 {
80 for (std::vector<Subscription*>::iterator it = filterEngine.listedSubscription s.begin(); 165 for (std::vector<SubscriptionPtr>::iterator it = filterEngine.listedSubscripti ons.begin();
81 it != filterEngine.listedSubscriptions.end();) 166 it != filterEngine.listedSubscriptions.end();)
82 { 167 {
83 if (*it == this) 168 if (it->get() == this)
84 it = filterEngine.listedSubscriptions.erase(it); 169 it = filterEngine.listedSubscriptions.erase(it);
85 else 170 else
86 it++; 171 it++;
87 } 172 }
88 } 173 }
89 174
90 void Subscription::UpdateFilters() 175 void Subscription::UpdateFilters()
91 { 176 {
92 } 177 }
93 178
94 FilterEngine::FilterEngine(JsEngine& jsEngine) : jsEngine(jsEngine) 179 FilterEngine::FilterEngine(JsEngine& jsEngine) : jsEngine(jsEngine)
95 { 180 {
96 #if !FILTER_ENGINE_STUBS 181 #if !FILTER_ENGINE_STUBS
97 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2) 182 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2)
98 jsEngine.Evaluate(jsSources[i + 1], jsSources[i]); 183 jsEngine.Evaluate(jsSources[i + 1], jsSources[i]);
99 #endif 184 #endif
100 } 185 }
101 186
187 Filter& FilterEngine::GetFilter(const std::string& text)
188 {
189 #if FILTER_ENGINE_STUBS
190 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring
191 std::string trimmed(text);
192 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st d::not1(std::ptr_fun<int, int>(std::isspace))));
193 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(std::pt r_fun<int, int>(std::isspace))).base(), trimmed.end());
194
195 std::map<std::string, FilterPtr>::const_iterator it = knownFilters.find(trimme d);
196 if (it != knownFilters.end())
197 return *it->second;
198
199 FilterPtr result(new Filter(*this, trimmed));
200 knownFilters[trimmed] = result->shared_from_this();
201 return *result;
202 #endif
203 }
204
102 Subscription& FilterEngine::GetSubscription(const std::string& url) 205 Subscription& FilterEngine::GetSubscription(const std::string& url)
103 { 206 {
104 #if FILTER_ENGINE_STUBS 207 #if FILTER_ENGINE_STUBS
105 std::map<std::string,Subscription*>::const_iterator it = knownSubscriptions.fi nd(url); 208 std::map<std::string, SubscriptionPtr>::const_iterator it = knownSubscriptions .find(url);
106 if (it != knownSubscriptions.end()) 209 if (it != knownSubscriptions.end())
107 return *it->second; 210 return *it->second;
108 211
109 Subscription* result = new Subscription(*this, url); 212 SubscriptionPtr result(new Subscription(*this, url));
110 knownSubscriptions[url] = result; 213 knownSubscriptions[url] = result->shared_from_this();
111 return *result; 214 return *result;
112 #endif 215 #endif
113 } 216 }
114 217
115 const std::vector<Subscription*>& FilterEngine::GetListedSubscriptions() const 218 const std::vector<FilterPtr>& FilterEngine::GetListedFilters() const
219 {
220 #if FILTER_ENGINE_STUBS
221 return listedFilters;
222 #endif
223 }
224
225 const std::vector<SubscriptionPtr>& FilterEngine::GetListedSubscriptions() const
116 { 226 {
117 #if FILTER_ENGINE_STUBS 227 #if FILTER_ENGINE_STUBS
118 return listedSubscriptions; 228 return listedSubscriptions;
119 #endif 229 #endif
120 } 230 }
121 231
122 void FilterEngine::FetchAvailableSubscriptions(SubscriptionsCallback callback) 232 void FilterEngine::FetchAvailableSubscriptions(SubscriptionsCallback callback)
123 { 233 {
124 #if FILTER_ENGINE_STUBS 234 #if FILTER_ENGINE_STUBS
125 std::vector<Subscription*> availableSubscriptions; 235 std::vector<SubscriptionPtr> availableSubscriptions;
126 236
127 Subscription& subscription1 = GetSubscription("https://easylist-downloads.adbl ockplus.org/easylist.txt"); 237 Subscription& subscription1 = GetSubscription("https://easylist-downloads.adbl ockplus.org/easylist.txt");
128 subscription1.SetProperty("title", "EasyList"); 238 subscription1.SetProperty("title", "EasyList");
129 availableSubscriptions.push_back(&subscription1); 239 availableSubscriptions.push_back(subscription1.shared_from_this());
130 240
131 Subscription& subscription2 = GetSubscription("https://easylist-downloads.adbl ockplus.org/easylistgermany+easylist.txt"); 241 Subscription& subscription2 = GetSubscription("https://easylist-downloads.adbl ockplus.org/easylistgermany+easylist.txt");
132 subscription2.SetProperty("title", "EasyList Germany+EasyList"); 242 subscription2.SetProperty("title", "EasyList Germany+EasyList");
133 availableSubscriptions.push_back(&subscription2); 243 availableSubscriptions.push_back(subscription2.shared_from_this());
134 244
135 callback(availableSubscriptions); 245 callback(availableSubscriptions);
136 #endif 246 #endif
137 } 247 }
138 248
139 bool FilterEngine::Matches(const std::string& url, 249 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url,
140 const std::string& contentType, 250 const std::string& contentType,
141 const std::string& documentUrl) const 251 const std::string& documentUrl)
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
142 { 252 {
143 #if FILTER_ENGINE_STUBS 253 #if FILTER_ENGINE_STUBS
144 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ 254 //For test on http://simple-adblock.com/faq/testing-your-adblocker/
145 return url.find("adbanner.gif") != std::string::npos; 255 if (url.find("adbanner.gif") != std::string::npos)
146 #endif 256 return GetFilter("adbanner.gif").shared_from_this();
147 } 257 else if (url.find("notbanner.gif") != std::string::npos)
148 258 return GetFilter("@@notbanner.gif").shared_from_this();
149 std::vector<std::string> FilterEngine::GetElementHidingRules() const 259 else
150 { 260 return AdblockPlus::FilterPtr();
151 #if FILTER_ENGINE_STUBS 261 #endif
152 std::vector<std::string> hidingRules; 262 }
153 hidingRules.push_back("###ad"); 263
154 hidingRules.push_back("##.ad"); 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");
155 //For test on http://simple-adblock.com/faq/testing-your-adblocker/ 270 //For test on http://simple-adblock.com/faq/testing-your-adblocker/
156 hidingRules.push_back("##.ad_300x250"); 271 if (domain == "simple-adblock.com")
157 return hidingRules; 272 selectors.push_back(".ad_300x250");
158 #endif 273 return selectors;
159 } 274 #endif
275 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld