 Issue 29333474:
  Issue 4125 - [emscripten] Convert filter classes to C++  (Closed)
    
  
    Issue 29333474:
  Issue 4125 - [emscripten] Convert filter classes to C++  (Closed) 
  | Left: | ||
| Right: | 
| LEFT | RIGHT | 
|---|---|
| 1 #include "Filter.h" | 1 #include "Filter.h" | 
| 2 #include "CommentFilter.h" | 2 #include "CommentFilter.h" | 
| 3 #include "InvalidFilter.h" | 3 #include "InvalidFilter.h" | 
| 4 #include "RegExpFilter.h" | 4 #include "RegExpFilter.h" | 
| 5 #include "BlockingFilter.h" | 5 #include "BlockingFilter.h" | 
| 6 #include "WhitelistFilter.h" | 6 #include "WhitelistFilter.h" | 
| 7 #include "ElemHideBase.h" | 7 #include "ElemHideBase.h" | 
| 8 #include "ElemHideFilter.h" | 8 #include "ElemHideFilter.h" | 
| 9 #include "ElemHideException.h" | 9 #include "ElemHideException.h" | 
| 10 #include "CSSPropertyFilter.h" | 10 #include "CSSPropertyFilter.h" | 
| 11 #include "StringMap.h" | 11 #include "StringMap.h" | 
| 12 | 12 | 
| 13 namespace | 13 namespace | 
| 14 { | 14 { | 
| 15 StringMap<Filter*> knownFilters(8192); | 15 StringMap<Filter*> knownFilters(8192); | 
| 
sergei
2016/06/16 21:16:28
What about having another object which holds known
 
sergei
2016/06/16 21:16:31
Does it add a big overhead to store intrusive_ptr
 
Wladimir Palant
2016/12/06 10:47:22
Sure, we can probably do that - but at the moment
 | |
| 16 | 16 | 
| 17 void NormalizeWhitespace(DependentString& text) | 17 void NormalizeWhitespace(DependentString& text) | 
| 18 { | 18 { | 
| 19 String::size_type start = 0; | 19 String::size_type start = 0; | 
| 20 String::size_type end = text.length(); | 20 String::size_type end = text.length(); | 
| 21 | 21 | 
| 22 // Remove leading spaces and special characters like line breaks | 22 // Remove leading spaces and special characters like line breaks | 
| 23 for (; start < end; start++) | 23 for (; start < end; start++) | 
| 24 if (text[start] > ' ') | 24 if (text[start] > ' ') | 
| 25 break; | 25 break; | 
| (...skipping 22 matching lines...) Expand all Loading... | |
| 48 // Remove trailing spaces | 48 // Remove trailing spaces | 
| 49 for (; end > 0; end--) | 49 for (; end > 0; end--) | 
| 50 if (text[end - 1] != ' ') | 50 if (text[end - 1] != ' ') | 
| 51 break; | 51 break; | 
| 52 | 52 | 
| 53 // Set new string boundaries | 53 // Set new string boundaries | 
| 54 text.reset(text, start, end - start); | 54 text.reset(text, start, end - start); | 
| 55 } | 55 } | 
| 56 } | 56 } | 
| 57 | 57 | 
| 58 Filter::Filter(const String& text) | 58 Filter::Filter(Type type, const String& text) | 
| 59 : mText(text) | 59 : mType(type), mText(text) | 
| 60 { | 60 { | 
| 61 annotate_address(this, "Filter"); | 61 annotate_address(this, "Filter"); | 
| 62 } | 62 } | 
| 63 | 63 | 
| 64 Filter::~Filter() | 64 Filter::~Filter() | 
| 65 { | 65 { | 
| 66 // TODO: This should be removing from knownFilters | 66 knownFilters.erase(mText); | 
| 
Wladimir Palant
2016/12/06 10:47:24
I addressed this TODO comment so that we can stop
 | |
| 67 } | 67 } | 
| 68 | 68 | 
| 69 OwnedString Filter::Serialize() const | 69 OwnedString Filter::Serialize() const | 
| 70 { | 70 { | 
| 71 OwnedString result(u"[Filter]\ntext="_str); | 71 OwnedString result(u"[Filter]\ntext="_str); | 
| 72 result.append(mText); | 72 result.append(mText); | 
| 73 result.append(u'\n'); | 73 result.append(u'\n'); | 
| 74 return result; | 74 return result; | 
| 75 } | 75 } | 
| 76 | 76 | 
| (...skipping 13 matching lines...) Expand all Loading... | |
| 90 DependentString error; | 90 DependentString error; | 
| 91 | 91 | 
| 92 Filter::Type type = CommentFilter::Parse(text); | 92 Filter::Type type = CommentFilter::Parse(text); | 
| 93 if (type == Filter::Type::UNKNOWN) | 93 if (type == Filter::Type::UNKNOWN) | 
| 94 type = ElemHideBase::Parse(text, data.elemhide); | 94 type = ElemHideBase::Parse(text, data.elemhide); | 
| 95 if (type == Filter::Type::UNKNOWN) | 95 if (type == Filter::Type::UNKNOWN) | 
| 96 type = RegExpFilter::Parse(text, error, data.regexp); | 96 type = RegExpFilter::Parse(text, error, data.regexp); | 
| 97 | 97 | 
| 98 auto knownFilter = knownFilters.find(text); | 98 auto knownFilter = knownFilters.find(text); | 
| 99 if (knownFilter) | 99 if (knownFilter) | 
| 100 { | |
| 101 knownFilter->second->AddRef(); | |
| 100 return knownFilter->second; | 102 return knownFilter->second; | 
| 103 } | |
| 101 | 104 | 
| 102 FilterPtr filter; | 105 FilterPtr filter; | 
| 103 switch (type) | 106 switch (type) | 
| 104 { | 107 { | 
| 105 case Filter::Type::COMMENT: | 108 case Filter::Type::COMMENT: | 
| 106 filter = new CommentFilter(text); | 109 filter = new CommentFilter(text); | 
| 107 break; | 110 break; | 
| 108 case Filter::Type::INVALID: | 111 case Filter::Type::INVALID: | 
| 109 filter = new InvalidFilter(text, error); | 112 filter = new InvalidFilter(text, error); | 
| 110 break; | 113 break; | 
| (...skipping 19 matching lines...) Expand all Loading... | |
| 130 return nullptr; | 133 return nullptr; | 
| 131 } | 134 } | 
| 132 | 135 | 
| 133 // This is a hack: we looked up the entry using text but create it using | 136 // 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, | 137 // filter->mText. This works because both are equal at this point. However, | 
| 135 // text refers to a temporary buffer which will go away. | 138 // text refers to a temporary buffer which will go away. | 
| 136 enter_context("Adding to known filters"); | 139 enter_context("Adding to known filters"); | 
| 137 knownFilter.assign(filter->mText, filter.get()); | 140 knownFilter.assign(filter->mText, filter.get()); | 
| 138 exit_context(); | 141 exit_context(); | 
| 139 | 142 | 
| 140 // TODO: We intentionally leak the filter here - currently it won't be used | 143 return filter.release(); | 
| 141 // for anything and would be deleted immediately. | |
| 
sergei
2016/06/16 21:16:33
Actually, we should have a convention that when we
 
Wladimir Palant
2016/12/06 10:47:20
Ok, let's implement proper semantics.
 | |
| 142 filter->AddRef(); | |
| 143 | |
| 144 return filter; | |
| 145 } | 144 } | 
| 146 | |
| 147 Filter* Filter::GetKnownFilter(const String& text) | |
| 
sergei
2016/06/16 21:16:30
It seems this method is not used, do we really nee
 
Wladimir Palant
2016/12/06 10:47:18
It isn't used in this form of course - the origina
 | |
| 148 { | |
| 149 auto it = knownFilters.find(text); | |
| 150 if (it) | |
| 151 return it->second; | |
| 152 else | |
| 153 return nullptr; | |
| 154 } | |
| LEFT | RIGHT |