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

Side by Side Diff: compiled/filter/Matcher.h

Issue 29556737: Issue 5141 - Convert filter match to C++ (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Fixed many issues. One test left out. Created Oct. 6, 2017, 1:45 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #pragma once
19
20 #include <vector>
21
22 #include "../debug.h"
23 #include "../bindings/runtime.h"
24 #include "../StringMap.h"
25 #include "Filter.h"
26
27 struct FilterKeyword
28 {
29 FilterKeyword()
30 {}
31 FilterKeyword(OwnedString&& keyword, Filter& filter)
32 : mKeyword(std::move(keyword)), mFilter(&filter)
33 {
34 }
35
36 explicit operator const String&() const
37 {
38 return mKeyword;
39 }
40 private:
41 OwnedString mKeyword;
Wladimir Palant 2017/10/09 08:39:47 A "keyword" is a substring of a filter text, so it
sergei 2017/10/09 15:27:53 The current way of obtaining of the keyword string
42 FilterPtr mFilter;
43 };
44
45 class Matcher : public ref_counted
46 {
47 private:
48 friend class CombinedMatcher;
49 StringMap<std::vector<FilterPtr>> mFilterByKeyword;
50 StringMap<FilterKeyword> mKeywordByFilter;
51 int mReId;
52 int mOptionsReId;
53 int mCandidatesReId;
54 int mMatchReId;
55 public:
56 static Matcher* BINDINGS_EXPORTED Create()
57 {
58 return new Matcher;
59 }
60 Matcher();
61 ~Matcher()
62 {
63 DeleteRegExp(mReId);
64 DeleteRegExp(mOptionsReId);
65 DeleteRegExp(mCandidatesReId);
66 DeleteRegExp(mMatchReId);
67 }
68
69 void BINDINGS_EXPORTED Add(Filter&);
70 void BINDINGS_EXPORTED Remove(Filter&);
71 void BINDINGS_EXPORTED Clear() ;
72 bool BINDINGS_EXPORTED HasFilter(const Filter&) const;
73 const String& BINDINGS_EXPORTED GetKeywordForFilter(const Filter& filter) cons t;
74 Filter* BINDINGS_EXPORTED MatchesAny(const String& location,
75 int typeMask, DependentString& docDomain, bool thirdParty,
76 const String& sitekey, bool specificOnly) const;
77 OwnedString BINDINGS_EXPORTED FindKeyword(const Filter&) const;
78
79 private:
80 FilterPtr CheckEntryMatch(const String& keyword,
81 const String& location,
82 int typeMask, DependentString& docDomain, bool thirdParty,
83 const String& sitekey, bool specificOnly) const;
84 };
85
86 // only needed because StringMap uses DependentString as keys.
87 struct CacheEntry
88 {
89 CacheEntry()
90 {}
91 CacheEntry(OwnedString&& key, FilterPtr filter)
92 : mKey(std::move(key)), mFilter(filter)
93 {}
94
95 const String& key() const
96 {
97 return mKey;
98 }
99 FilterPtr filter() const
100 {
101 return mFilter;
102 }
103 private:
104 OwnedString mKey;
105 FilterPtr mFilter;
106 };
107
108 class CombinedMatcher : public ref_counted
109 {
110 private:
111 static const size_t MAX_CACHE_ENTRIES;
112 StringMap<CacheEntry> mResultCache;
Wladimir Palant 2017/10/09 08:39:47 Before we copy the caching approach from the JS co
sergei 2017/10/09 15:27:53 It seems not trivial because it requires at least
Wladimir Palant 2017/10/10 07:39:05 You are getting it backwards. The caching shouldn'
113 Matcher mBlacklist;
114 Matcher mWhitelist;
115 int mMatchReId;
116
117 public:
118 static CombinedMatcher* BINDINGS_EXPORTED Create()
119 {
120 return new CombinedMatcher;
121 }
122
123 BINDINGS_EXPORTED CombinedMatcher();
124 ~CombinedMatcher()
125 {
126 DeleteRegExp(mMatchReId);
127 }
128
129 void BINDINGS_EXPORTED Add(Filter& filter);
130 void BINDINGS_EXPORTED Remove(Filter& filter);
131 void BINDINGS_EXPORTED Clear();
132 bool BINDINGS_EXPORTED HasFilter(const Filter& filter) const;
133 const String& BINDINGS_EXPORTED GetKeywordForFilter(const Filter& filter) cons t;
134 Filter* BINDINGS_EXPORTED MatchesAny(const String& location,
135 int typeMask, DependentString& docDomain, bool thirdParty,
136 const String& sitekey, bool specificOnly);
137 OwnedString BINDINGS_EXPORTED FindKeyword(const Filter& filter) const;
138
139 private:
140 Matcher& GetMatcher(const Filter& filter)
141 {
142 return filter.mType == Filter::Type::WHITELIST ? mWhitelist : mBlacklist;
143 }
144 const Matcher& GetMatcher(const Filter& filter) const
145 {
146 return filter.mType == Filter::Type::WHITELIST ? mWhitelist : mBlacklist;
147 }
148 void ResetCache();
149 FilterPtr MatchesAnyInternal(const String& location,
150 int typeMask, DependentString& docDomain, bool thirdParty,
151 const String& sitekey, bool specificOnly) const;
152 };
153
OLDNEW

Powered by Google App Engine
This is Rietveld