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 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 | |
27 class Matcher : public ref_counted | 45 class Matcher : public ref_counted |
28 { | 46 { |
29 private: | 47 private: |
30 friend class CombinedMatcher; | 48 friend class CombinedMatcher; |
31 StringMap<std::vector<FilterPtr>> mFilterByKeyword; | 49 StringMap<std::vector<FilterPtr>> mFilterByKeyword; |
32 StringMap<OwnedString> mKeywordByFilter; | 50 StringMap<FilterKeyword> mKeywordByFilter; |
51 int mReId; | |
52 int mOptionsReId; | |
33 int mCandidatesReId; | 53 int mCandidatesReId; |
54 int mMatchReId; | |
34 public: | 55 public: |
56 static Matcher* BINDINGS_EXPORTED Create() | |
57 { | |
58 return new Matcher; | |
59 } | |
35 Matcher(); | 60 Matcher(); |
36 ~Matcher(); | 61 ~Matcher() |
62 { | |
63 DeleteRegExp(mReId); | |
64 DeleteRegExp(mOptionsReId); | |
65 DeleteRegExp(mCandidatesReId); | |
66 DeleteRegExp(mMatchReId); | |
67 } | |
37 | 68 |
38 void BINDINGS_EXPORTED Add(const FilterPtr&); | 69 void BINDINGS_EXPORTED Add(Filter&); |
39 void BINDINGS_EXPORTED Remove(const FilterPtr&); | 70 void BINDINGS_EXPORTED Remove(Filter&); |
40 void BINDINGS_EXPORTED Clear() ; | 71 void BINDINGS_EXPORTED Clear() ; |
41 bool BINDINGS_EXPORTED HasFilter(const FilterPtr&) const; | 72 bool BINDINGS_EXPORTED HasFilter(const Filter&) const; |
42 const String& BINDINGS_EXPORTED GetKeywordForFilter(const FilterPtr& filter); | 73 const String& BINDINGS_EXPORTED GetKeywordForFilter(const Filter& filter) cons t; |
43 Filter* BINDINGS_EXPORTED MatchesAny(const String& location, | 74 Filter* BINDINGS_EXPORTED MatchesAny(const String& location, |
44 int typeMask, DependentString& docDomain, bool thirdParty, | 75 int typeMask, DependentString& docDomain, bool thirdParty, |
45 const String& sitekey, bool specificOnly); | 76 const String& sitekey, bool specificOnly) const; |
46 OwnedString BINDINGS_EXPORTED FindKeyword(const FilterPtr&); | 77 OwnedString BINDINGS_EXPORTED FindKeyword(const Filter&) const; |
78 | |
47 private: | 79 private: |
48 FilterPtr CheckEntryMatch(const String& keyword, | 80 FilterPtr CheckEntryMatch(const String& keyword, |
49 const String& location, | 81 const String& location, |
50 int typeMask, DependentString& docDomain, bool thirdParty, | 82 int typeMask, DependentString& docDomain, bool thirdParty, |
51 const String& sitekey, bool specificOnly); | 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; | |
52 }; | 106 }; |
53 | 107 |
54 class CombinedMatcher : public ref_counted | 108 class CombinedMatcher : public ref_counted |
55 { | 109 { |
56 private: | 110 private: |
57 static const size_t MAX_CACHE_ENTRIES; | 111 static const size_t MAX_CACHE_ENTRIES; |
58 StringMap<FilterPtr> mResultCache; | 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'
| |
59 int mMatchReId; | |
60 Matcher mBlacklist; | 113 Matcher mBlacklist; |
61 Matcher mWhitelist; | 114 Matcher mWhitelist; |
115 int mMatchReId; | |
62 | 116 |
63 public: | 117 public: |
64 CombinedMatcher(); | 118 static CombinedMatcher* BINDINGS_EXPORTED Create() |
65 ~CombinedMatcher(); | 119 { |
120 return new CombinedMatcher; | |
121 } | |
66 | 122 |
67 void BINDINGS_EXPORTED Add(const FilterPtr& filter); | 123 BINDINGS_EXPORTED CombinedMatcher(); |
68 void BINDINGS_EXPORTED Remove(const FilterPtr& filter); | 124 ~CombinedMatcher() |
125 { | |
126 DeleteRegExp(mMatchReId); | |
127 } | |
128 | |
129 void BINDINGS_EXPORTED Add(Filter& filter); | |
130 void BINDINGS_EXPORTED Remove(Filter& filter); | |
69 void BINDINGS_EXPORTED Clear(); | 131 void BINDINGS_EXPORTED Clear(); |
70 bool BINDINGS_EXPORTED HasFilter(const FilterPtr& filter) const; | 132 bool BINDINGS_EXPORTED HasFilter(const Filter& filter) const; |
71 const String& BINDINGS_EXPORTED GetKeywordForFilter(const FilterPtr& filter); | 133 const String& BINDINGS_EXPORTED GetKeywordForFilter(const Filter& filter) cons t; |
72 Filter* BINDINGS_EXPORTED MatchesAny(const String& location, | 134 Filter* BINDINGS_EXPORTED MatchesAny(const String& location, |
73 int typeMask, DependentString& docDomain, bool thirdParty, | 135 int typeMask, DependentString& docDomain, bool thirdParty, |
74 const String& sitekey, bool specificOnly); | 136 const String& sitekey, bool specificOnly); |
75 OwnedString BINDINGS_EXPORTED FindKeyword(const FilterPtr& filter); | 137 OwnedString BINDINGS_EXPORTED FindKeyword(const Filter& filter) const; |
76 | 138 |
77 private: | 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 } | |
78 void ResetCache(); | 148 void ResetCache(); |
79 FilterPtr MatchesAnyInternal(const String& location, | 149 FilterPtr MatchesAnyInternal(const String& location, |
80 int typeMask, DependentString& docDomain, bool thirdParty, | 150 int typeMask, DependentString& docDomain, bool thirdParty, |
81 const String& sitekey, bool specificOnly); | 151 const String& sitekey, bool specificOnly) const; |
82 }; | 152 }; |
153 | |
LEFT | RIGHT |