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