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

Delta Between Two Patch Sets: compiled/ActiveFilter.cpp

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Left Patch Set: Back to manual approach for API Created Jan. 18, 2016, 12:41 p.m.
Right Patch Set: Addressed comments from Patch Set 28 Created March 21, 2017, 10:04 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 | « compiled/ActiveFilter.h ('k') | compiled/BlockingFilter.h » ('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 <cstdio>
2
1 #include "ActiveFilter.h" 3 #include "ActiveFilter.h"
4 #include "StringScanner.h"
2 5
3 ActiveFilter::ActiveFilter(const std::u16string& text) 6 namespace
4 : Filter(text), disabled(false), hitCount(0), lastHit(0) 7 {
8 const DependentString DEFAULT_DOMAIN(u""_str);
9
10 OwnedString to_string(unsigned int i)
11 {
12 char buffer[11];
13 int len = sprintf(buffer, "%u", i);
14
15 OwnedString result(len);
16 for (String::size_type i = 0; i < len; i++)
17 result[i] = buffer[i];
18 return result;
19 }
20 }
21
22 ActiveFilter::ActiveFilter(Type type, const String& text, bool ignoreTrailingDot )
23 : Filter(type, text), mDisabled(false), mHitCount(0), mLastHit(0),
24 mIgnoreTrailingDot(ignoreTrailingDot)
5 { 25 {
6 } 26 }
27
28 ActiveFilter::DomainMap* ActiveFilter::GetDomains() const
29 {
30 return mDomains.get();
31 }
32
33 ActiveFilter::SitekeySet* ActiveFilter::GetSitekeys() const
34 {
35 return mSitekeys.get();
36 }
37
38 void ActiveFilter::ParseDomains(const String& domains,
39 String::value_type separator) const
40 {
41 DomainMap::size_type count = 2;
42 for (String::size_type i = 0; i < domains.length(); i++)
43 if (domains[i] == separator)
44 count++;
45
46 mDomains.reset(new DomainMap(count));
47 annotate_address(mDomains.get(), "DomainMap");
48
49 StringScanner scanner(domains, 0, separator);
50 String::size_type start = 0;
51 bool reverse = false;
52 bool hasIncludes = false;
53 bool done = scanner.done();
54 while (!done)
55 {
56 done = scanner.done();
57 String::value_type currChar = scanner.next();
58 if (currChar == u'~' && scanner.position() == start)
59 {
60 start++;
61 reverse = true;
62 }
63 else if (currChar == separator)
64 {
65 String::size_type len = scanner.position() - start;
66 if (len > 0 && mIgnoreTrailingDot && domains[start + len - 1] == '.')
67 len--;
68 if (len > 0)
69 {
70 enter_context("Adding to ActiveFilter.mDomains");
71 (*mDomains)[DependentString(domains, start, len)] = !reverse;
72 exit_context();
73
74 if (!reverse)
75 hasIncludes = true;
76 }
77 start = scanner.position() + 1;
78 reverse = false;
79 }
80 }
81 enter_context("Adding to ActiveFilter.mDomains");
82 (*mDomains)[DEFAULT_DOMAIN] = !hasIncludes;
83 exit_context();
84 }
85
86 void ActiveFilter::AddSitekey(const String& sitekey) const
87 {
88 if (!mSitekeys)
89 {
90 mSitekeys.reset(new SitekeySet());
91 annotate_address(mSitekeys.get(), "SitekeySet");
92 }
93
94 enter_context("Adding to ActiveFilter.mSitekeys");
95 mSitekeys->insert(sitekey);
96 exit_context();
97 }
98
99 bool ActiveFilter::IsActiveOnDomain(DependentString& docDomain, const String& si tekey) const
100 {
101 auto sitekeys = GetSitekeys();
102 if (sitekeys && !sitekeys->find(sitekey))
103 return false;
104
105 // If no domains are set the rule matches everywhere
106 auto domains = GetDomains();
107 if (!domains)
108 return true;
109
110 // If the document has no host name, match only if the filter isn't restricted
111 // to specific domains
112 if (docDomain.empty())
113 return (*domains)[DEFAULT_DOMAIN];
114
115 docDomain.toLower();
116
117 String::size_type len = docDomain.length();
118 if (len > 0 && mIgnoreTrailingDot && docDomain[len - 1] == '.')
119 docDomain.reset(docDomain, 0, len - 1);
120 while (true)
121 {
122 auto it = domains->find(docDomain);
123 if (it)
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 (*domains)[DEFAULT_DOMAIN];
132 }
133
134 bool ActiveFilter::IsActiveOnlyOnDomain(DependentString& docDomain) const
135 {
136 auto domains = GetDomains();
137 if (!domains || docDomain.empty() || (*domains)[DEFAULT_DOMAIN])
138 return false;
139
140 docDomain.toLower();
141
142 String::size_type len = docDomain.length();
143 if (len > 0 && mIgnoreTrailingDot && docDomain[len - 1] == '.')
144 docDomain.reset(docDomain, 0, len - 1);
145 for (auto it = domains->begin(); it != domains->end(); ++it)
146 {
147 if (!it->second || it->first.equals(docDomain))
148 continue;
149
150 size_t len1 = it->first.length();
151 size_t len2 = docDomain.length();
152 if (len1 > len2 &&
153 DependentString(it->first, len1 - len2).equals(docDomain) &&
154 it->first[len1 - len2 - 1] == u'.')
155 {
156 continue;
157 }
158
159 return false;
160 }
161 return true;
162 }
163
164 bool ActiveFilter::IsGeneric() const
165 {
166 auto sitekeys = GetSitekeys();
167 auto domains = GetDomains();
168 return !sitekeys && (!domains || (*domains)[DEFAULT_DOMAIN]);
169 }
170
171 OwnedString ActiveFilter::Serialize() const
172 {
173 /* TODO this is very inefficient */
174 OwnedString result(Filter::Serialize());
175 if (mDisabled)
176 result.append(u"disabled=true\n"_str);
177 if (mHitCount)
178 {
179 result.append(u"hitCount="_str);
180 result.append(to_string(mHitCount));
181 result.append(u'\n');
182 }
183 if (mLastHit)
184 {
185 result.append(u"lastHit="_str);
186 result.append(to_string(mLastHit));
187 result.append(u'\n');
188 }
189 return result;
190 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld