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