Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: compiled/Filter.cpp

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Minor improvements Created Feb. 6, 2016, 8:29 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « compiled/Filter.h ('k') | compiled/InvalidFilter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include "Filter.h"
2 #include "CommentFilter.h"
3 #include "InvalidFilter.h"
4 #include "RegExpFilter.h"
5 #include "WhitelistFilter.h"
6 #include "ElemHideBase.h"
7 #include "ElemHideFilter.h"
8 #include "ElemHideException.h"
9 #include "CSSPropertyFilter.h"
10 #include "StringMap.h"
11
12 namespace
13 {
14 StringMap<Filter*> knownFilters(8192);
15
16 void NormalizeWhitespace(DependentString& text)
17 {
18 String::size_type start = 0;
19 String::size_type end = text.length();
20
21 // Remove leading spaces and special characters like line breaks
22 for (; start < end; start++)
23 if (text[start] > ' ')
24 break;
25
26 // Now look for invalid characters inside the string
27 String::size_type pos;
28 for (pos = start; pos < end; pos++)
29 if (text[pos] < ' ')
30 break;
31
32 if (pos < end)
33 {
34 // Found invalid characters, copy all the valid characters while skipping
35 // the invalid ones.
36 String::size_type delta = 1;
37 for (pos = pos + 1; pos < end; pos++)
38 {
39 if (text[pos] < ' ')
40 delta++;
41 else
42 text[pos - delta] = text[pos];
43 }
44 end -= delta;
45 }
46
47 // Remove trailing spaces
48 for (; end > 0; end--)
49 if (text[end - 1] != ' ')
50 break;
51
52 // Set new string boundaries
53 text.reset(text, start, end - start);
54 }
55 }
56
57 Filter::Filter(const String& text)
58 : ref_counted(), mText(text)
59 {
60 annotate_address(this, "Filter");
61 }
62
63 Filter::~Filter()
64 {
65 // TODO: This should be removing from knownFilters
66 }
67
68 OwnedString Filter::Serialize() const
69 {
70 OwnedString result(u"[Filter]\ntext="_str);
71 result.append(mText);
72 result.append(u'\n');
73 return std::move(result);
74 }
75
76 Filter* Filter::FromText(DependentString& text)
77 {
78 NormalizeWhitespace(text);
79 if (text.empty())
80 return nullptr;
81
82 // Parsing also normalizes the filter text, so it has to be done before the
83 // lookup in knownFilters.
84 union
85 {
86 RegExpFilterData regexp;
87 ElemHideData elemhide;
88 } data;
89 OwnedString error;
90
91 Filter::Type type = CommentFilter::Parse(text);
92 if (type == Filter::Type::UNKNOWN)
93 type = ElemHideBase::Parse(text, data.elemhide);
94 if (type == Filter::Type::UNKNOWN)
95 type = RegExpFilter::Parse(text, error, data.regexp);
96
97 FilterPtr filter(GetKnownFilter(text));
98 if (filter)
99 return filter;
100
101 switch (type)
102 {
103 case Filter::Type::COMMENT:
104 filter = new CommentFilter(text);
105 break;
106 case Filter::Type::INVALID:
107 filter = new InvalidFilter(text, error);
108 break;
109 case Filter::Type::BLOCKING:
110 filter = new RegExpFilter(text, data.regexp);
111 break;
112 case Filter::Type::WHITELIST:
113 filter = new WhitelistFilter(text, data.regexp);
114 break;
115 case Filter::Type::ELEMHIDE:
116 filter = new ElemHideFilter(text, data.elemhide);
117 break;
118 case Filter::Type::ELEMHIDEEXCEPTION:
119 filter = new ElemHideException(text, data.elemhide);
120 break;
121 case Filter::Type::CSSPROPERTY:
122 filter = new CSSPropertyFilter(text, data.elemhide);
123 if (reinterpret_cast<CSSPropertyFilter*>(filter.get())->IsGeneric())
124 filter = new InvalidFilter(text,
125 u"No active domain specified for CSS property filter"_str);
126 break;
127 default:
128 // This should never happen but just in case
129 return nullptr;
130 }
131
132 enter_context("Adding to known filters");
133 knownFilters[filter->mText] = filter.get();
134 exit_context();
135
136 // TODO: We intentionally leak the filter here - currently it won't be used
137 // for anything and would be deleted immediately.
138 filter->AddRef();
139
140 return filter;
141 }
142
143 Filter* Filter::GetKnownFilter(const String& text)
144 {
145 auto it = knownFilters.find(text);
146 if (it != knownFilters.end())
147 return it->second;
148 else
149 return nullptr;
150 }
OLDNEW
« no previous file with comments | « compiled/Filter.h ('k') | compiled/InvalidFilter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld