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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: compiled/filter/Matcher.h
===================================================================
new file mode 100644
--- /dev/null
+++ b/compiled/filter/Matcher.h
@@ -0,0 +1,153 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-present eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <vector>
+
+#include "../debug.h"
+#include "../bindings/runtime.h"
+#include "../StringMap.h"
+#include "Filter.h"
+
+struct FilterKeyword
+{
+ FilterKeyword()
+ {}
+ FilterKeyword(OwnedString&& keyword, Filter& filter)
+ : mKeyword(std::move(keyword)), mFilter(&filter)
+ {
+ }
+
+ explicit operator const String&() const
+ {
+ return mKeyword;
+ }
+private:
+ 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
+ FilterPtr mFilter;
+};
+
+class Matcher : public ref_counted
+{
+private:
+ friend class CombinedMatcher;
+ StringMap<std::vector<FilterPtr>> mFilterByKeyword;
+ StringMap<FilterKeyword> mKeywordByFilter;
+ int mReId;
+ int mOptionsReId;
+ int mCandidatesReId;
+ int mMatchReId;
+public:
+ static Matcher* BINDINGS_EXPORTED Create()
+ {
+ return new Matcher;
+ }
+ Matcher();
+ ~Matcher()
+ {
+ DeleteRegExp(mReId);
+ DeleteRegExp(mOptionsReId);
+ DeleteRegExp(mCandidatesReId);
+ DeleteRegExp(mMatchReId);
+ }
+
+ void BINDINGS_EXPORTED Add(Filter&);
+ void BINDINGS_EXPORTED Remove(Filter&);
+ void BINDINGS_EXPORTED Clear() ;
+ bool BINDINGS_EXPORTED HasFilter(const Filter&) const;
+ const String& BINDINGS_EXPORTED GetKeywordForFilter(const Filter& filter) const;
+ Filter* BINDINGS_EXPORTED MatchesAny(const String& location,
+ int typeMask, DependentString& docDomain, bool thirdParty,
+ const String& sitekey, bool specificOnly) const;
+ OwnedString BINDINGS_EXPORTED FindKeyword(const Filter&) const;
+
+private:
+ FilterPtr CheckEntryMatch(const String& keyword,
+ const String& location,
+ int typeMask, DependentString& docDomain, bool thirdParty,
+ const String& sitekey, bool specificOnly) const;
+};
+
+// only needed because StringMap uses DependentString as keys.
+struct CacheEntry
+{
+ CacheEntry()
+ {}
+ CacheEntry(OwnedString&& key, FilterPtr filter)
+ : mKey(std::move(key)), mFilter(filter)
+ {}
+
+ const String& key() const
+ {
+ return mKey;
+ }
+ FilterPtr filter() const
+ {
+ return mFilter;
+ }
+private:
+ OwnedString mKey;
+ FilterPtr mFilter;
+};
+
+class CombinedMatcher : public ref_counted
+{
+private:
+ static const size_t MAX_CACHE_ENTRIES;
+ 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'
+ Matcher mBlacklist;
+ Matcher mWhitelist;
+ int mMatchReId;
+
+public:
+ static CombinedMatcher* BINDINGS_EXPORTED Create()
+ {
+ return new CombinedMatcher;
+ }
+
+ BINDINGS_EXPORTED CombinedMatcher();
+ ~CombinedMatcher()
+ {
+ DeleteRegExp(mMatchReId);
+ }
+
+ void BINDINGS_EXPORTED Add(Filter& filter);
+ void BINDINGS_EXPORTED Remove(Filter& filter);
+ void BINDINGS_EXPORTED Clear();
+ bool BINDINGS_EXPORTED HasFilter(const Filter& filter) const;
+ const String& BINDINGS_EXPORTED GetKeywordForFilter(const Filter& filter) const;
+ Filter* BINDINGS_EXPORTED MatchesAny(const String& location,
+ int typeMask, DependentString& docDomain, bool thirdParty,
+ const String& sitekey, bool specificOnly);
+ OwnedString BINDINGS_EXPORTED FindKeyword(const Filter& filter) const;
+
+private:
+ Matcher& GetMatcher(const Filter& filter)
+ {
+ return filter.mType == Filter::Type::WHITELIST ? mWhitelist : mBlacklist;
+ }
+ const Matcher& GetMatcher(const Filter& filter) const
+ {
+ return filter.mType == Filter::Type::WHITELIST ? mWhitelist : mBlacklist;
+ }
+ void ResetCache();
+ FilterPtr MatchesAnyInternal(const String& location,
+ int typeMask, DependentString& docDomain, bool thirdParty,
+ const String& sitekey, bool specificOnly) const;
+};
+

Powered by Google App Engine
This is Rietveld