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

Side by Side Diff: compiled/ActiveFilter.cpp

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Fixed annotation for hash buffers Created Jan. 28, 2016, 5:07 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « compiled/ActiveFilter.h ('k') | compiled/CSSPropertyFilter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include <cstdio>
2
3 #include "ActiveFilter.h"
4 #include "StringScanner.h"
5
6 namespace
7 {
8 void ProcessDomain(String& docDomain, bool ignoreTrailingDot)
9 {
10 String::size_type len = docDomain.length();
11 if (ignoreTrailingDot)
12 {
13 for (; len > 0; --len)
14 {
15 if (docDomain[len - 1] != u'.')
16 break;
17 }
18 }
19
20 for (String::size_type i = 0; i < len; ++i)
21 {
22 String::value_type currChar = docDomain[i];
23 // TODO: This needs to work for non-ASCII as well
24 if (currChar >= u'A' && currChar <= u'Z')
25 docDomain[i] = currChar + u'a' - u'A';
26 }
27
28 docDomain.reset(docDomain, 0, len);
29 }
30
31 String to_string(unsigned int i)
32 {
33 char buffer[11];
34 int len = sprintf(buffer, "%u", i);
35 return std::move(String(buffer, len));
36 }
37 }
38
39 ActiveFilter::ActiveFilter(const String& text, bool ignoreTrailingDot)
40 : Filter(text), mDisabled(false), mHitCount(0), mLastHit(0),
41 mIgnoreTrailingDot(ignoreTrailingDot)
42 {
43 }
44
45 void ActiveFilter::ParseDomains(const String& str, String::value_type separator)
46 {
47 DomainMap::size_type count = 2;
48 for (String::size_type i = 0; i < str.length(); i++)
49 if (str[i] == separator)
50 count++;
51
52 mDomains.reset(new DomainMap(count));
53 annotate_address(mDomains.get(), "DomainMap");
54
55 StringScanner scanner(str, separator);
56 String::size_type start = 0;
57 bool reverse = false;
58 bool hasIncludes = false;
59 bool done = false;
60 while (!done)
61 {
62 done = scanner.done();
63 String::value_type currChar = scanner.next();
64 if (currChar == u'~' && scanner.position() == start)
65 {
66 start++;
67 reverse = true;
68 }
69 else if (currChar == separator)
70 {
71 if (scanner.position() > start)
72 {
73 String domain(str, start, scanner.position() - start);
74 ProcessDomain(domain, mIgnoreTrailingDot);
75
76 enter_context("Adding to ActiveFilter.mDomains");
77 (*mDomains)[domain] = !reverse;
78 exit_context();
79
80 if (!reverse)
81 hasIncludes = true;
82 }
83 start = scanner.position() + 1;
84 reverse = false;
85 }
86 }
87 enter_context("Adding to ActiveFilter.mDomains");
88 (*mDomains)[u""_str] = !hasIncludes;
89 exit_context();
90 }
91
92 void ActiveFilter::AddSitekey(const String& sitekey)
93 {
94 if (!mSitekeys)
95 {
96 mSitekeys.reset(new SitekeySet());
97 annotate_address(mSitekeys.get(), "SitekeySet");
98 }
99
100 enter_context("Adding to ActiveFilter.mSitekeys");
101 mSitekeys->insert(sitekey);
102 exit_context();
103 }
104
105 bool ActiveFilter::IsActiveOnDomain(String& docDomain, const String& sitekey)
106 {
107 if (mSitekeys && mSitekeys->find(sitekey) == mSitekeys->end())
108 return false;
109
110 // If no domains are set the rule matches everywhere
111 if (!mDomains)
112 return true;
113
114 // If the document has no host name, match only if the filter isn't restricted
115 // to specific domains
116 if (docDomain.empty())
117 return (*mDomains)[u""_str];
118
119 ProcessDomain(docDomain, mIgnoreTrailingDot);
120 while (true)
121 {
122 auto it = mDomains->find(docDomain);
123 if (it != mDomains->end())
124 return it->second;
125
126 String::size_type nextDot = docDomain.find(u'.');
127 if (nextDot == docDomain.npos)
128 break;
129 docDomain.reset(docDomain, nextDot + 1);
130 }
131 return (*mDomains)[u""_str];
132 }
133
134 bool ActiveFilter::IsActiveOnlyOnDomain(String& docDomain)
135 {
136 if (!mDomains || docDomain.empty() || (*mDomains)[u""_str])
137 return false;
138
139 ProcessDomain(docDomain, mIgnoreTrailingDot);
140 for (auto it = mDomains->begin(); it != mDomains->end(); ++it)
141 {
142 if (!it->second || it->first.equals(docDomain))
143 continue;
144
145 size_t len1 = it->first.length();
146 size_t len2 = docDomain.length();
147 if (len1 > len2 &&
148 String(it->first, len1 - len2).equals(docDomain) &&
149 it->first[len1 - len2 - 1] == u'.')
150 {
151 continue;
152 }
153
154 return false;
155 }
156 return true;
157 }
158
159 bool ActiveFilter::IsGeneric()
160 {
161 return !mSitekeys && (!mDomains || (*mDomains)[u""_str]);
162 }
163
164 String ActiveFilter::Serialize()
165 {
166 /* TODO this is very inefficient */
167 String result(Filter::Serialize());
168 if (mDisabled)
169 result.append(u"disabled=true\n"_str);
170 if (mHitCount)
171 {
172 result.append(u"hitCount="_str);
173 result.append(to_string(mHitCount));
174 result.append(u'\n');
175 }
176 if (mLastHit)
177 {
178 result.append(u"lastHit="_str);
179 result.append(to_string(mLastHit));
180 result.append(u'\n');
181 }
182 return std::move(result);
183 }
OLDNEW
« no previous file with comments | « compiled/ActiveFilter.h ('k') | compiled/CSSPropertyFilter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld