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