Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 #pragma once | 18 #pragma once |
19 | 19 |
20 #include <vector> | 20 #include <vector> |
21 | 21 |
22 #include "../debug.h" | 22 #include "../debug.h" |
23 #include "../bindings/runtime.h" | 23 #include "../bindings/runtime.h" |
24 #include "../StringMap.h" | 24 #include "../StringMap.h" |
25 #include "Filter.h" | 25 #include "Filter.h" |
26 | 26 |
27 class MatcherBase : public ref_counted | 27 struct FilterKeyword |
28 { | 28 { |
29 private: | 29 FilterKeyword() |
30 static MatcherBase* mInstance; | 30 {} |
31 | 31 FilterKeyword(OwnedString&& keyword, Filter& filter) |
32 public: | 32 : mKeyword(std::move(keyword)), mFilter(&filter) |
33 virtual ~MatcherBase() | |
34 { | 33 { |
35 } | 34 } |
36 | 35 |
37 static MatcherBase* BINDINGS_EXPORTED GetDefaultInstance() | 36 explicit operator const String&() const |
38 { | 37 { |
39 return mInstance; | 38 return mKeyword; |
40 } | 39 } |
41 virtual void BINDINGS_EXPORTED Add(const FilterPtr&) = 0; | 40 private: |
42 virtual void BINDINGS_EXPORTED Remove(const FilterPtr&) = 0; | 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
| |
43 virtual void BINDINGS_EXPORTED Clear() = 0; | 42 FilterPtr mFilter; |
44 virtual bool BINDINGS_EXPORTED HasFilter(const FilterPtr&) const = 0; | |
45 virtual const String& BINDINGS_EXPORTED GetKeywordForFilter(const FilterPtr& f ilter) = 0; | |
46 virtual Filter* BINDINGS_EXPORTED MatchesAny(const String& location, | |
47 int typeMask, DependentString& docDomain, bool thirdParty, | |
48 const String& sitekey, bool specificOnly) = 0; | |
49 protected: | |
50 virtual OwnedString FindKeyword(const FilterPtr&) = 0; | |
51 }; | 43 }; |
52 | 44 |
53 typedef intrusive_ptr<MatcherBase> MatcherPtr; | 45 class Matcher : public ref_counted |
54 | |
55 class Matcher : public MatcherBase | |
56 { | 46 { |
57 private: | 47 private: |
58 friend class CombinedMatcher; | 48 friend class CombinedMatcher; |
59 StringMap<std::vector<FilterPtr>> mFilterByKeyword; | 49 StringMap<std::vector<FilterPtr>> mFilterByKeyword; |
60 StringMap<OwnedString> mKeywordByFilter; | 50 StringMap<FilterKeyword> mKeywordByFilter; |
61 int mFilterReId; | 51 int mReId; |
62 int mOptionsReId; | 52 int mOptionsReId; |
63 int mCandidatesReId; | 53 int mCandidatesReId; |
54 int mMatchReId; | |
64 public: | 55 public: |
56 static Matcher* BINDINGS_EXPORTED Create() | |
57 { | |
58 return new Matcher; | |
59 } | |
65 Matcher(); | 60 Matcher(); |
66 ~Matcher(); | 61 ~Matcher() |
62 { | |
63 DeleteRegExp(mReId); | |
64 DeleteRegExp(mOptionsReId); | |
65 DeleteRegExp(mCandidatesReId); | |
66 DeleteRegExp(mMatchReId); | |
67 } | |
67 | 68 |
68 void BINDINGS_EXPORTED Add(const FilterPtr&) override; | 69 void BINDINGS_EXPORTED Add(Filter&); |
69 void BINDINGS_EXPORTED Remove(const FilterPtr&) override; | 70 void BINDINGS_EXPORTED Remove(Filter&); |
70 void BINDINGS_EXPORTED Clear() override ; | 71 void BINDINGS_EXPORTED Clear() ; |
71 bool BINDINGS_EXPORTED HasFilter(const FilterPtr&) const override; | 72 bool BINDINGS_EXPORTED HasFilter(const Filter&) const; |
72 const String& BINDINGS_EXPORTED GetKeywordForFilter(const FilterPtr& filter) o verride; | 73 const String& BINDINGS_EXPORTED GetKeywordForFilter(const Filter& filter) cons t; |
73 Filter* BINDINGS_EXPORTED MatchesAny(const String& location, | 74 Filter* BINDINGS_EXPORTED MatchesAny(const String& location, |
74 int typeMask, DependentString& docDomain, bool thirdParty, | 75 int typeMask, DependentString& docDomain, bool thirdParty, |
75 const String& sitekey, bool specificOnly) override; | 76 const String& sitekey, bool specificOnly) const; |
76 protected: | 77 OwnedString BINDINGS_EXPORTED FindKeyword(const Filter&) const; |
77 OwnedString FindKeyword(const FilterPtr&) override; | 78 |
78 private: | 79 private: |
79 Filter* CheckEntryMatch(const String& keyword, | 80 FilterPtr CheckEntryMatch(const String& keyword, |
80 const String& location, | 81 const String& location, |
81 int typeMask, DependentString& docDomain, bool thirdParty, | 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, | |
82 const String& sitekey, bool specificOnly); | 136 const String& sitekey, bool specificOnly); |
83 }; | 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 | |
LEFT | RIGHT |