| 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-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 #include <vector> | 18 #include <vector> | 
| 19 | 19 | 
| 20 #include "FilterStorage.h" | 20 #include "FilterStorage.h" | 
| 21 #include "../filter/Filter.h" | 21 #include "../filter/Filter.h" | 
| 22 #include "../subscription/UserDefinedSubscription.h" | 22 #include "../subscription/UserDefinedSubscription.h" | 
| 23 #include "../FilterNotifier.h" | 23 #include "../FilterNotifier.h" | 
| 24 | 24 | 
| 25 namespace | 25 FilterStorage* FilterStorage::mInstance = new FilterStorage(); | 
|  | 26 | 
|  | 27 FilterStorage::Subscriptions::size_type FilterStorage::GetSubscriptionCount() co
     nst | 
| 26 { | 28 { | 
| 27   std::vector<SubscriptionPtr> subscriptions; | 29   return mSubscriptions.size(); | 
| 28 } | 30 } | 
| 29 | 31 | 
| 30 unsigned int FilterStorage::GetSubscriptionCount() | 32 Subscription* FilterStorage::SubscriptionAt(FilterStorage::Subscriptions::size_t
     ype index) const | 
| 31 { | 33 { | 
| 32   return subscriptions.size(); | 34   if (index >= mSubscriptions.size()) | 
| 33 } |  | 
| 34 |  | 
| 35 Subscription* FilterStorage::SubscriptionAt(unsigned int index) |  | 
| 36 { |  | 
| 37   if (index >= subscriptions.size()) |  | 
| 38     return nullptr; | 35     return nullptr; | 
| 39 | 36 | 
| 40   SubscriptionPtr result(subscriptions[index]); | 37   SubscriptionPtr result(mSubscriptions[index]); | 
| 41   return result.release(); | 38   return result.release(); | 
| 42 } | 39 } | 
| 43 | 40 | 
| 44 int FilterStorage::IndexOfSubscription(const Subscription* subscription) | 41 int FilterStorage::IndexOfSubscription(const Subscription* subscription) const | 
| 45 { | 42 { | 
| 46   for (unsigned int i = 0; i < subscriptions.size(); i++) | 43   for (Subscriptions::size_type i = 0; i < mSubscriptions.size(); i++) | 
| 47     if (subscriptions[i] == subscription) | 44     if (mSubscriptions[i] == subscription) | 
| 48       return i; | 45       return i; | 
| 49   return -1; | 46   return -1; | 
| 50 } | 47 } | 
| 51 | 48 | 
| 52 Subscription* FilterStorage::GetSubscriptionForFilter(const Filter* filter) | 49 Subscription* FilterStorage::GetSubscriptionForFilter(const Filter* filter) cons
     t | 
| 53 { | 50 { | 
| 54   SubscriptionPtr fallback; | 51   SubscriptionPtr fallback; | 
| 55 | 52 | 
| 56   for (auto& subscription : subscriptions) | 53   for (Subscriptions::size_type i = 0; i < mSubscriptions.size(); i++) | 
| 57   { | 54   { | 
|  | 55     SubscriptionPtr subscription(mSubscriptions[i]); | 
| 58     UserDefinedSubscription* userDefinedSubscription = | 56     UserDefinedSubscription* userDefinedSubscription = | 
| 59         subscription->As<UserDefinedSubscription>(); | 57         subscription->As<UserDefinedSubscription>(); | 
| 60     if (userDefinedSubscription && !userDefinedSubscription->GetDisabled() && | 58     if (userDefinedSubscription && !userDefinedSubscription->GetDisabled() && | 
| 61         userDefinedSubscription->IsDefaultFor(filter)) | 59         userDefinedSubscription->IsDefaultFor(filter)) | 
| 62     { | 60     { | 
| 63       SubscriptionPtr result(subscription); | 61       SubscriptionPtr result(subscription); | 
| 64       return result.release(); | 62       return result.release(); | 
| 65     } | 63     } | 
| 66     else if (!fallback && userDefinedSubscription && | 64     else if (!fallback && userDefinedSubscription && | 
| 67              userDefinedSubscription->IsGeneric()) | 65              userDefinedSubscription->IsGeneric()) | 
| 68     { | 66     { | 
| 69       fallback = subscription; | 67       fallback = subscription; | 
| 70     } | 68     } | 
| 71   } | 69   } | 
| 72 | 70 | 
| 73   return fallback.release(); | 71   return fallback.release(); | 
| 74 } | 72 } | 
| 75 | 73 | 
| 76 bool FilterStorage::AddSubscription(Subscription* subscription) | 74 bool FilterStorage::AddSubscription(Subscription* subscription) | 
| 77 { | 75 { | 
| 78   assert(subscription, u"Attempt to add a null subscription"_str); | 76   assert(subscription, u"Attempt to add a null subscription"_str); | 
| 79 | 77 | 
| 80   if (!subscription || subscription->GetListed()) | 78   if (!subscription || subscription->GetListed()) | 
| 81     return false; | 79     return false; | 
| 82 | 80 | 
| 83   subscriptions.emplace_back(subscription); | 81   mSubscriptions.emplace_back(subscription); | 
| 84   subscription->SetListed(true); | 82   subscription->SetListed(true); | 
| 85 | 83 | 
| 86   FilterNotifier::SubscriptionChange( | 84   FilterNotifier::SubscriptionChange( | 
| 87     FilterNotifier::Topic::SUBSCRIPTION_ADDED, | 85     FilterNotifier::Topic::SUBSCRIPTION_ADDED, | 
| 88     subscription | 86     subscription | 
| 89   ); | 87   ); | 
| 90   return true; | 88   return true; | 
| 91 } | 89 } | 
| 92 | 90 | 
| 93 bool FilterStorage::RemoveSubscription(Subscription* subscription) | 91 bool FilterStorage::RemoveSubscription(Subscription* subscription) | 
| 94 { | 92 { | 
| 95   assert(subscription, u"Attempt to remove a null subscription"_str); | 93   assert(subscription, u"Attempt to remove a null subscription"_str); | 
| 96 | 94 | 
| 97   if (!subscription || !subscription->GetListed()) | 95   if (!subscription || !subscription->GetListed()) | 
| 98     return false; | 96     return false; | 
| 99 | 97 | 
| 100   for (auto it = subscriptions.begin(); it != subscriptions.end(); ++it) | 98   for (auto it = mSubscriptions.begin(); it != mSubscriptions.end(); ++it) | 
| 101   { | 99   { | 
| 102     if (*it == subscription) | 100     if (*it == subscription) | 
| 103     { | 101     { | 
| 104       subscriptions.erase(it); | 102       mSubscriptions.erase(it); | 
| 105       break; | 103       break; | 
| 106     } | 104     } | 
| 107   } | 105   } | 
| 108   subscription->SetListed(false); | 106   subscription->SetListed(false); | 
| 109 | 107 | 
| 110   FilterNotifier::SubscriptionChange( | 108   FilterNotifier::SubscriptionChange( | 
| 111     FilterNotifier::Topic::SUBSCRIPTION_REMOVED, | 109     FilterNotifier::Topic::SUBSCRIPTION_REMOVED, | 
| 112     subscription | 110     subscription | 
| 113   ); | 111   ); | 
| 114   return true; | 112   return true; | 
| 115 } | 113 } | 
| 116 | 114 | 
| 117 bool FilterStorage::MoveSubscription(Subscription* subscription, | 115 bool FilterStorage::MoveSubscription(Subscription* subscription, | 
| 118                                      const Subscription* insertBefore) | 116                                      const Subscription* insertBefore) | 
| 119 { | 117 { | 
| 120   assert(subscription, u"Attempt to move a null subscription"_str); | 118   assert(subscription, u"Attempt to move a null subscription"_str); | 
| 121 | 119 | 
| 122   int oldPos = IndexOfSubscription(subscription); | 120   int oldPos = IndexOfSubscription(subscription); | 
|  | 121   assert(oldPos >= 0, u"Attempt to move a subscription that is not in the list"_
     str); | 
| 123   if (oldPos == -1) | 122   if (oldPos == -1) | 
| 124     return false; | 123     return false; | 
| 125 | 124 | 
| 126   int newPos = -1; | 125   int newPos = -1; | 
| 127   if (insertBefore) | 126   if (insertBefore) | 
| 128     newPos = IndexOfSubscription(insertBefore); | 127     newPos = IndexOfSubscription(insertBefore); | 
| 129   if (newPos == -1) | 128   if (newPos == -1) | 
| 130     newPos = subscriptions.size(); | 129     newPos = mSubscriptions.size(); | 
| 131 | 130 | 
| 132   if (newPos > oldPos) | 131   if (newPos > oldPos) | 
| 133     newPos--; | 132     newPos--; | 
| 134 | 133 | 
| 135   if (newPos == oldPos) | 134   if (newPos == oldPos) | 
| 136     return false; | 135     return false; | 
| 137 | 136 | 
| 138   subscriptions.erase(subscriptions.begin() + oldPos); | 137   mSubscriptions.erase(mSubscriptions.begin() + oldPos); | 
| 139   subscriptions.emplace(subscriptions.begin() + newPos, subscription); | 138   mSubscriptions.emplace(mSubscriptions.begin() + newPos, subscription); | 
| 140 | 139 | 
| 141   FilterNotifier::SubscriptionChange( | 140   FilterNotifier::SubscriptionChange( | 
| 142     FilterNotifier::Topic::SUBSCRIPTION_MOVED, | 141     FilterNotifier::Topic::SUBSCRIPTION_MOVED, | 
| 143     subscription | 142     subscription | 
| 144   ); | 143   ); | 
| 145   return true; | 144   return true; | 
| 146 } | 145 } | 
| LEFT | RIGHT | 
|---|