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