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

Powered by Google App Engine
This is Rietveld