| OLD | NEW | 
| (Empty) |  | 
 |    1 #include "Filter.h" | 
 |    2 #include "CommentFilter.h" | 
 |    3 #include "InvalidFilter.h" | 
 |    4 #include "RegExpFilter.h" | 
 |    5 #include "BlockingFilter.h" | 
 |    6 #include "WhitelistFilter.h" | 
 |    7 #include "ElemHideBase.h" | 
 |    8 #include "ElemHideFilter.h" | 
 |    9 #include "ElemHideException.h" | 
 |   10 #include "CSSPropertyFilter.h" | 
 |   11 #include "StringMap.h" | 
 |   12  | 
 |   13 namespace | 
 |   14 { | 
 |   15   StringMap<Filter*> knownFilters(8192); | 
 |   16  | 
 |   17   void NormalizeWhitespace(DependentString& text) | 
 |   18   { | 
 |   19     String::size_type start = 0; | 
 |   20     String::size_type end = text.length(); | 
 |   21  | 
 |   22     // Remove leading spaces and special characters like line breaks | 
 |   23     for (; start < end; start++) | 
 |   24       if (text[start] > ' ') | 
 |   25         break; | 
 |   26  | 
 |   27     // Now look for invalid characters inside the string | 
 |   28     String::size_type pos; | 
 |   29     for (pos = start; pos < end; pos++) | 
 |   30       if (text[pos] < ' ') | 
 |   31         break; | 
 |   32  | 
 |   33     if (pos < end) | 
 |   34     { | 
 |   35       // Found invalid characters, copy all the valid characters while skipping | 
 |   36       // the invalid ones. | 
 |   37       String::size_type delta = 1; | 
 |   38       for (pos = pos + 1; pos < end; pos++) | 
 |   39       { | 
 |   40         if (text[pos] < ' ') | 
 |   41           delta++; | 
 |   42         else | 
 |   43           text[pos - delta] = text[pos]; | 
 |   44       } | 
 |   45       end -= delta; | 
 |   46     } | 
 |   47  | 
 |   48     // Remove trailing spaces | 
 |   49     for (; end > 0; end--) | 
 |   50       if (text[end - 1] != ' ') | 
 |   51         break; | 
 |   52  | 
 |   53     // Set new string boundaries | 
 |   54     text.reset(text, start, end - start); | 
 |   55   } | 
 |   56 } | 
 |   57  | 
 |   58 Filter::Filter(const String& text) | 
 |   59     : mText(text) | 
 |   60 { | 
 |   61   annotate_address(this, "Filter"); | 
 |   62 } | 
 |   63  | 
 |   64 Filter::~Filter() | 
 |   65 { | 
 |   66   // TODO: This should be removing from knownFilters | 
 |   67 } | 
 |   68  | 
 |   69 OwnedString Filter::Serialize() const | 
 |   70 { | 
 |   71   OwnedString result(u"[Filter]\ntext="_str); | 
 |   72   result.append(mText); | 
 |   73   result.append(u'\n'); | 
 |   74   return result; | 
 |   75 } | 
 |   76  | 
 |   77 Filter* Filter::FromText(DependentString& text) | 
 |   78 { | 
 |   79   NormalizeWhitespace(text); | 
 |   80   if (text.empty()) | 
 |   81     return nullptr; | 
 |   82  | 
 |   83   // Parsing also normalizes the filter text, so it has to be done before the | 
 |   84   // lookup in knownFilters. | 
 |   85   union | 
 |   86   { | 
 |   87     RegExpFilterData regexp; | 
 |   88     ElemHideData elemhide; | 
 |   89   } data; | 
 |   90   DependentString error; | 
 |   91  | 
 |   92   Filter::Type type = CommentFilter::Parse(text); | 
 |   93   if (type == Filter::Type::UNKNOWN) | 
 |   94     type = ElemHideBase::Parse(text, data.elemhide); | 
 |   95   if (type == Filter::Type::UNKNOWN) | 
 |   96     type = RegExpFilter::Parse(text, error, data.regexp); | 
 |   97  | 
 |   98   auto knownFilter = knownFilters.find(text); | 
 |   99   if (knownFilter) | 
 |  100     return knownFilter->second; | 
 |  101  | 
 |  102   FilterPtr filter; | 
 |  103   switch (type) | 
 |  104   { | 
 |  105     case Filter::Type::COMMENT: | 
 |  106       filter = new CommentFilter(text); | 
 |  107       break; | 
 |  108     case Filter::Type::INVALID: | 
 |  109       filter = new InvalidFilter(text, error); | 
 |  110       break; | 
 |  111     case Filter::Type::BLOCKING: | 
 |  112       filter = new BlockingFilter(text, data.regexp); | 
 |  113       break; | 
 |  114     case Filter::Type::WHITELIST: | 
 |  115       filter = new WhitelistFilter(text, data.regexp); | 
 |  116       break; | 
 |  117     case Filter::Type::ELEMHIDE: | 
 |  118       filter = new ElemHideFilter(text, data.elemhide); | 
 |  119       break; | 
 |  120     case Filter::Type::ELEMHIDEEXCEPTION: | 
 |  121       filter = new ElemHideException(text, data.elemhide); | 
 |  122       break; | 
 |  123     case Filter::Type::CSSPROPERTY: | 
 |  124       filter = new CSSPropertyFilter(text, data.elemhide); | 
 |  125       if (static_cast<CSSPropertyFilter*>(filter.get())->IsGeneric()) | 
 |  126         filter = new InvalidFilter(text, u"filter_cssproperty_nodomain"_str); | 
 |  127       break; | 
 |  128     default: | 
 |  129       // This should never happen but just in case | 
 |  130       return nullptr; | 
 |  131   } | 
 |  132  | 
 |  133   // This is a hack: we looked up the entry using text but create it using | 
 |  134   // filter->mText. This works because both are equal at this point. However, | 
 |  135   // text refers to a temporary buffer which will go away. | 
 |  136   enter_context("Adding to known filters"); | 
 |  137   knownFilter.assign(filter->mText, filter.get()); | 
 |  138   exit_context(); | 
 |  139  | 
 |  140   // TODO: We intentionally leak the filter here - currently it won't be used | 
 |  141   // for anything and would be deleted immediately. | 
 |  142   filter->AddRef(); | 
 |  143  | 
 |  144   return filter; | 
 |  145 } | 
 |  146  | 
 |  147 Filter* Filter::GetKnownFilter(const String& text) | 
 |  148 { | 
 |  149   auto it = knownFilters.find(text); | 
 |  150   if (it) | 
 |  151     return it->second; | 
 |  152   else | 
 |  153     return nullptr; | 
 |  154 } | 
| OLD | NEW |