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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 #include <cstdlib> |
| 19 |
18 #include "UserDefinedSubscription.h" | 20 #include "UserDefinedSubscription.h" |
19 | 21 |
20 int UserDefinedSubscription::filterTypeToDefaults[] = { | 22 namespace |
21 UserDefinedSubscription::Defaults::BLOCKING, // UNKNOWN | 23 { |
22 UserDefinedSubscription::Defaults::BLOCKING, // INVALID | 24 enum FilterCategory |
23 UserDefinedSubscription::Defaults::BLOCKING, // COMMENT | 25 { |
24 UserDefinedSubscription::Defaults::BLOCKING, // BLOCKING | 26 WHITELIST = 1, |
25 UserDefinedSubscription::Defaults::WHITELIST, // WHITELIST | 27 BLOCKING = 2, |
26 UserDefinedSubscription::Defaults::ELEMHIDE, // ELEMHIDE | 28 ELEMHIDE = 4, |
27 UserDefinedSubscription::Defaults::ELEMHIDE, // ELEMHIDEEXCEPTION | 29 }; |
28 UserDefinedSubscription::Defaults::ELEMHIDE, // ELEMHIDEEMULATION | 30 |
29 }; | 31 const FilterCategory filterTypeToCategory[] = { |
| 32 FilterCategory::BLOCKING, // UNKNOWN |
| 33 FilterCategory::BLOCKING, // INVALID |
| 34 FilterCategory::BLOCKING, // COMMENT |
| 35 FilterCategory::BLOCKING, // BLOCKING |
| 36 FilterCategory::WHITELIST, // WHITELIST |
| 37 FilterCategory::ELEMHIDE, // ELEMHIDE |
| 38 FilterCategory::ELEMHIDE, // ELEMHIDEEXCEPTION |
| 39 FilterCategory::ELEMHIDE, // ELEMHIDEEMULATION |
| 40 }; |
| 41 |
| 42 static_assert( |
| 43 sizeof(filterTypeToCategory) / sizeof(filterTypeToCategory[0]) == Filter::Ty
pe::VALUE_COUNT, |
| 44 "Unexpected number of filter types, was a new type added?" |
| 45 ); |
| 46 } |
30 | 47 |
31 UserDefinedSubscription::UserDefinedSubscription(const String& id) | 48 UserDefinedSubscription::UserDefinedSubscription(const String& id) |
32 : Subscription(Type::USERDEFINED, id), mDefaults(0) | 49 : Subscription(Type::USERDEFINED, id), mDefaults(0) |
33 { | 50 { |
34 } | 51 } |
35 | 52 |
36 bool UserDefinedSubscription::IsDefaultFor(const Filter* filter) const | 53 bool UserDefinedSubscription::IsDefaultFor(const Filter* filter) const |
37 { | 54 { |
38 return mDefaults & filterTypeToDefaults[filter->mType]; | 55 if (filter->mType >= Filter::Type::VALUE_COUNT) |
| 56 { |
| 57 assert(false, "Filter type exceeds valid range"); |
| 58 abort(); |
| 59 } |
| 60 return mDefaults & filterTypeToCategory[filter->mType]; |
39 } | 61 } |
40 | 62 |
41 void UserDefinedSubscription::MakeDefaultFor(const Filter* filter) | 63 void UserDefinedSubscription::MakeDefaultFor(const Filter* filter) |
42 { | 64 { |
43 mDefaults |= filterTypeToDefaults[filter->mType]; | 65 if (filter->mType >= Filter::Type::VALUE_COUNT) |
| 66 { |
| 67 assert(false, "Filter type exceeds valid range"); |
| 68 abort(); |
| 69 } |
| 70 mDefaults |= filterTypeToCategory[filter->mType]; |
44 } | 71 } |
45 | 72 |
46 void UserDefinedSubscription::InsertFilterAt(Filter* filter, unsigned pos) | 73 void UserDefinedSubscription::InsertFilterAt(Filter* filter, unsigned pos) |
47 { | 74 { |
48 if (pos >= mFilters.size()) | 75 if (pos >= mFilters.size()) |
49 mFilters.emplace_back(filter); | 76 mFilters.emplace_back(filter); |
50 else | 77 else |
51 mFilters.emplace(mFilters.begin() + pos, filter); | 78 mFilters.emplace(mFilters.begin() + pos, filter); |
52 } | 79 } |
53 | 80 |
54 bool UserDefinedSubscription::RemoveFilterAt(unsigned pos) | 81 bool UserDefinedSubscription::RemoveFilterAt(unsigned pos) |
55 { | 82 { |
56 if (pos >= mFilters.size()) | 83 if (pos >= mFilters.size()) |
57 return false; | 84 return false; |
58 | 85 |
59 mFilters.erase(mFilters.begin() + pos); | 86 mFilters.erase(mFilters.begin() + pos); |
60 return true; | 87 return true; |
61 } | 88 } |
62 | 89 |
63 OwnedString UserDefinedSubscription::Serialize() const | 90 OwnedString UserDefinedSubscription::Serialize() const |
64 { | 91 { |
65 OwnedString result(Subscription::Serialize()); | 92 OwnedString result(Subscription::Serialize()); |
66 if (mDefaults) | 93 if (mDefaults) |
67 { | 94 { |
68 result.append(u"defaults="_str); | 95 result.append(u"defaults="_str); |
69 if (mDefaults & Defaults::BLOCKING) | 96 if (mDefaults & FilterCategory::BLOCKING) |
70 result.append(u" blocking"_str); | 97 result.append(u" blocking"_str); |
71 if (mDefaults & Defaults::WHITELIST) | 98 if (mDefaults & FilterCategory::WHITELIST) |
72 result.append(u" whitelist"_str); | 99 result.append(u" whitelist"_str); |
73 if (mDefaults & Defaults::ELEMHIDE) | 100 if (mDefaults & FilterCategory::ELEMHIDE) |
74 result.append(u" elemhide"_str); | 101 result.append(u" elemhide"_str); |
75 result.append(u'\n'); | 102 result.append(u'\n'); |
76 } | 103 } |
77 return result; | 104 return result; |
78 } | 105 } |
LEFT | RIGHT |