| 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 /** | 18 /** | 
| 19 * @fileOverview Element hiding implementation. | 19 * @fileOverview Element hiding implementation. | 
| 20 */ | 20 */ | 
| 21 | 21 | 
| 22 #include <unordered_map> | 22 #pragma once | 
| 23 | |
| 23 #include <vector> | 24 #include <vector> | 
| 24 | 25 | 
| 25 #include "bindings/runtime.h" | 26 #include "bindings/runtime.h" | 
| 26 #include "intrusive_ptr.h" | 27 #include "intrusive_ptr.h" | 
| 27 #include "StringMap.h" | 28 #include "StringMap.h" | 
| 28 #include "filter/Filter.h" | 29 #include "filter/Filter.h" | 
| 29 #include "filter/ElemHideBase.h" | 30 #include "filter/ElemHideBase.h" | 
| 30 #include "filter/ElemHideException.h" | 31 #include "filter/ElemHideException.h" | 
| 31 | 32 | 
| 32 class _ElemHide_SelectorList : public ref_counted | 33 class ElemHide_SelectorList : public ref_counted | 
| 33 { | 34 { | 
| 34 std::vector<ElemHideBasePtr> mSelectors; | 35 std::vector<ElemHideBasePtr> mSelectors; | 
| 35 public: | 36 public: | 
| 36 size_t BINDINGS_EXPORTED GetSelectorCount() const | 37 size_t BINDINGS_EXPORTED GetSelectorCount() const | 
| 37 { | 38 { | 
| 38 return mSelectors.size(); | 39 return mSelectors.size(); | 
| 39 } | 40 } | 
| 40 DependentString BINDINGS_EXPORTED SelectorAt(size_t idx) const; | 41 OwnedString BINDINGS_EXPORTED SelectorAt(size_t idx) const; | 
| 41 const String& BINDINGS_EXPORTED FilterKeyAt(size_t idx) const; | 42 const String& BINDINGS_EXPORTED FilterKeyAt(size_t idx) const; | 
| 42 | 43 | 
| 43 void push_back(ElemHideBasePtr filter) | 44 void push_back(const ElemHideBasePtr& filter) | 
| 44 { | 45 { | 
| 45 mSelectors.push_back(filter); | 46 mSelectors.push_back(filter); | 
| 47 } | |
| 48 | |
| 49 void append(const ElemHide_SelectorList& list) | |
| 50 { | |
| 51 mSelectors.insert(mSelectors.end(), | |
| 52 list.mSelectors.cbegin(), list.mSelectors.cend()); | |
| 46 } | 53 } | 
| 47 }; | 54 }; | 
| 48 | 55 | 
| 49 class ElemHide : public ref_counted | 56 class ElemHide : public ref_counted | 
| 50 { | 57 { | 
| 51 // All filters. Key is filter text. Exception filters excluded. | 58 // All filters. Key is filter text. Exception filters excluded. | 
| 52 StringMap<ElemHideBasePtr> mFilters; | 59 StringMap<ElemHideBasePtr> mFilters; | 
| 53 // StringMap is non copyable. std::unordered_map<> it is | 60 // Filters by domain. Key is domain. | 
| 54 // Filters by domain. Key is domain. Subkey is filter text. | 61 // In value key is filter text, value is filter or NULL | 
| 55 StringMap<std::unordered_map<DependentString,ElemHideBasePtr,StringHash>> mFil tersByDomain; | 62 OwnedStringMap<OwnedStringMap<ElemHideBasePtr>> mFiltersByDomain; | 
| 
 
hub
2017/10/25 01:19:38
Using an std::unordered_map. Bu if we make StringM
 
sergei
2018/01/15 15:31:15
What is the overhead from the std::unordered_map?
 
hub
2018/01/19 02:11:01
File https://issues.adblockplus.org/ticket/6279 to
 
 | |
| 56 | 63 | 
| 57 // Exceptions. The key is the selector. | 64 // Exceptions. The key is the selector. | 
| 58 StringMap<std::vector<ElemHideExceptionPtr>> mExceptions; | 65 OwnedStringMap<std::vector<ElemHideExceptionPtr>> mExceptions; | 
| 59 // Known exceptions. Filter text as keys. | 66 // Known exceptions. Filter text as keys. | 
| 60 StringSet mKnownExceptions; | 67 StringSet mKnownExceptions; | 
| 61 | 68 | 
| 62 // Unconditional selectors. Filter text as key | 69 // Unconditional selectors. Filter selector as key. Filter as value. | 
| 63 StringSet mUnconditionalSelectors; | 70 OwnedStringMap<ElemHideBasePtr> mUnconditionalSelectors; | 
| 64 | 71 | 
| 65 static ElemHide* mInstance; | 72 mutable intrusive_ptr<ElemHide_SelectorList> mUnconditionalSelectorsCache; | 
| 73 | |
| 66 public: | 74 public: | 
| 75 static ElemHide* BINDINGS_EXPORTED Create() | |
| 76 { | |
| 77 return new ElemHide(); | |
| 78 } | |
| 79 // Used by GetSelectorsForDomain to narrow down selectors to return. | |
| 80 // Keep these value up to date in the ElemHide.js import script. | |
| 67 enum Criteria | 81 enum Criteria | 
| 68 { | 82 { | 
| 83 // Return all selectors applying to a particular hostname. | |
| 69 ALL_MATCHING = 0, | 84 ALL_MATCHING = 0, | 
| 85 // Exclude selectors which apply to all websites without exception. | |
| 70 NO_UNCONDITIONAL = 1, | 86 NO_UNCONDITIONAL = 1, | 
| 87 // Return only selectors for filters which specifically match the | |
| 88 // given host name. | |
| 71 SPECIFIC_ONLY = 2, | 89 SPECIFIC_ONLY = 2, | 
| 72 }; | 90 }; | 
| 73 | |
| 74 static ElemHide* BINDINGS_EXPORTED GetInstance() | |
| 75 { | |
| 76 return mInstance; | |
| 77 } | |
| 78 | 91 | 
| 79 void BINDINGS_EXPORTED Clear(); | 92 void BINDINGS_EXPORTED Clear(); | 
| 80 void BINDINGS_EXPORTED Add(ElemHideBase& filter); | 93 void BINDINGS_EXPORTED Add(ElemHideBase& filter); | 
| 81 void BINDINGS_EXPORTED Remove(ElemHideBase& filter); | 94 void BINDINGS_EXPORTED Remove(ElemHideBase& filter); | 
| 82 | 95 | 
| 83 _ElemHide_SelectorList* BINDINGS_EXPORTED GetSelectorsForDomain(const String& domain, Criteria criteria) const; | 96 ElemHide_SelectorList* BINDINGS_EXPORTED GetSelectorsForDomain(const String& d omain, | 
| 84 _ElemHide_SelectorList* BINDINGS_EXPORTED GetUnconditionalSelectors() const; | 97 Criteria criter ia) const; | 
| 98 ElemHide_SelectorList* BINDINGS_EXPORTED GetUnconditionalSelectors() const; | |
| 85 | 99 | 
| 86 ElemHideException* BINDINGS_EXPORTED GetException(const ElemHideBase& filter, | 100 ElemHideException* BINDINGS_EXPORTED GetException(const ElemHideBase& filter, | 
| 
 
hub
2017/10/25 01:19:38
This function is needed until ElemHideEmulation is
 
 | |
| 87 DependentString& docDomain) const; | 101 DependentString& docDomain) const; | 
| 88 | 102 | 
| 89 private: | 103 private: | 
| 90 void AddToFiltersByDomain(ElemHideBase& filter); | 104 void AddToFiltersByDomain(const ElemHideBasePtr & filter); | 
| 91 }; | 105 }; | 
| 92 | 106 | 
| LEFT | RIGHT |