OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2017 eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 #include <cstdlib> |
| 19 |
| 20 #include "UserDefinedSubscription.h" |
| 21 |
| 22 namespace |
| 23 { |
| 24 enum FilterCategory |
| 25 { |
| 26 WHITELIST = 1, |
| 27 BLOCKING = 2, |
| 28 ELEMHIDE = 4, |
| 29 }; |
| 30 |
| 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 } |
| 47 |
| 48 UserDefinedSubscription::UserDefinedSubscription(const String& id) |
| 49 : Subscription(Type::USERDEFINED, id), mDefaults(0) |
| 50 { |
| 51 } |
| 52 |
| 53 bool UserDefinedSubscription::IsDefaultFor(const Filter* filter) const |
| 54 { |
| 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]; |
| 61 } |
| 62 |
| 63 void UserDefinedSubscription::MakeDefaultFor(const Filter* filter) |
| 64 { |
| 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]; |
| 71 } |
| 72 |
| 73 OwnedString UserDefinedSubscription::Serialize() const |
| 74 { |
| 75 OwnedString result(Subscription::Serialize()); |
| 76 if (mDefaults) |
| 77 { |
| 78 result.append(u"defaults="_str); |
| 79 if (mDefaults & FilterCategory::BLOCKING) |
| 80 result.append(u" blocking"_str); |
| 81 if (mDefaults & FilterCategory::WHITELIST) |
| 82 result.append(u" whitelist"_str); |
| 83 if (mDefaults & FilterCategory::ELEMHIDE) |
| 84 result.append(u" elemhide"_str); |
| 85 result.append(u'\n'); |
| 86 } |
| 87 return result; |
| 88 } |
OLD | NEW |