 Issue 29426559:
  Issue 5137 - [emscripten] Added basic filter storage implementation  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluscore
    
  
    Issue 29426559:
  Issue 5137 - [emscripten] Added basic filter storage implementation  (Closed) 
  Base URL: https://hg.adblockplus.org/adblockpluscore| 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 #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 FilterStorage* FilterStorage::mInstance = new FilterStorage(); | 25 FilterStorage* FilterStorage::mInstance = new FilterStorage(); | 
| 26 | 26 | 
| 27 unsigned int FilterStorage::GetSubscriptionCount() const | 27 FilterStorage::Subscriptions::size_type FilterStorage::GetSubscriptionCount() co nst | 
| 28 { | 28 { | 
| 29 return mSubscriptions.size(); | 29 return mSubscriptions.size(); | 
| 30 } | 30 } | 
| 31 | 31 | 
| 32 Subscription* FilterStorage::SubscriptionAt(unsigned int index) const | 32 Subscription* FilterStorage::SubscriptionAt(FilterStorage::Subscriptions::size_t ype index) const | 
| 33 { | 33 { | 
| 34 if (index >= mSubscriptions.size()) | 34 if (index >= mSubscriptions.size()) | 
| 35 return nullptr; | 35 return nullptr; | 
| 36 | 36 | 
| 37 SubscriptionPtr result(mSubscriptions[index]); | 37 SubscriptionPtr result(mSubscriptions[index]); | 
| 38 return result.release(); | 38 return result.release(); | 
| 39 } | 39 } | 
| 40 | 40 | 
| 41 int FilterStorage::IndexOfSubscription(const Subscription* subscription) const | 41 int FilterStorage::IndexOfSubscription(const Subscription* subscription) const | 
| 42 { | 42 { | 
| 43 for (unsigned int i = 0; i < mSubscriptions.size(); i++) | 43 for (Subscriptions::size_type i = 0; i < mSubscriptions.size(); i++) | 
| 
sergei
2017/08/24 13:32:06
Despite there are strangely no warnings from compi
 
Wladimir Palant
2017/08/31 11:32:35
Done.
 | |
| 44 if (mSubscriptions[i] == subscription) | 44 if (mSubscriptions[i] == subscription) | 
| 45 return i; | 45 return i; | 
| 46 return -1; | 46 return -1; | 
| 47 } | 47 } | 
| 48 | 48 | 
| 49 Subscription* FilterStorage::GetSubscriptionForFilter(const Filter* filter) | 49 Subscription* FilterStorage::GetSubscriptionForFilter(const Filter* filter) cons t | 
| 
sergei
2017/08/24 13:32:05
It seems that this method should be const.
 
Wladimir Palant
2017/08/31 11:32:36
Managed to do this with somewhat counterintuitive
 
sergei
2017/08/31 12:43:38
Yeah, sorry, maybe it was a bad idea to turn it in
 | |
| 50 { | 50 { | 
| 51 SubscriptionPtr fallback; | 51 SubscriptionPtr fallback; | 
| 52 | 52 | 
| 53 for (auto& subscription : mSubscriptions) | 53 for (Subscriptions::size_type i = 0; i < mSubscriptions.size(); i++) | 
| 54 { | 54 { | 
| 55 SubscriptionPtr subscription(mSubscriptions[i]); | |
| 55 UserDefinedSubscription* userDefinedSubscription = | 56 UserDefinedSubscription* userDefinedSubscription = | 
| 56 subscription->As<UserDefinedSubscription>(); | 57 subscription->As<UserDefinedSubscription>(); | 
| 57 if (userDefinedSubscription && !userDefinedSubscription->GetDisabled() && | 58 if (userDefinedSubscription && !userDefinedSubscription->GetDisabled() && | 
| 58 userDefinedSubscription->IsDefaultFor(filter)) | 59 userDefinedSubscription->IsDefaultFor(filter)) | 
| 59 { | 60 { | 
| 60 SubscriptionPtr result(subscription); | 61 SubscriptionPtr result(subscription); | 
| 61 return result.release(); | 62 return result.release(); | 
| 62 } | 63 } | 
| 63 else if (!fallback && userDefinedSubscription && | 64 else if (!fallback && userDefinedSubscription && | 
| 64 userDefinedSubscription->IsGeneric()) | 65 userDefinedSubscription->IsGeneric()) | 
| (...skipping 15 matching lines...) Expand all Loading... | |
| 80 mSubscriptions.emplace_back(subscription); | 81 mSubscriptions.emplace_back(subscription); | 
| 81 subscription->SetListed(true); | 82 subscription->SetListed(true); | 
| 82 | 83 | 
| 83 FilterNotifier::SubscriptionChange( | 84 FilterNotifier::SubscriptionChange( | 
| 84 FilterNotifier::Topic::SUBSCRIPTION_ADDED, | 85 FilterNotifier::Topic::SUBSCRIPTION_ADDED, | 
| 85 subscription | 86 subscription | 
| 86 ); | 87 ); | 
| 87 return true; | 88 return true; | 
| 88 } | 89 } | 
| 89 | 90 | 
| 90 bool FilterStorage::RemoveSubscription(Subscription* subscription) | 91 bool FilterStorage::RemoveSubscription(Subscription* subscription) | 
| 
sergei
2017/08/24 13:32:06
It seems so far there is no way to make the argume
 
Wladimir Palant
2017/08/31 11:32:35
For AddSubscription() the argument shouldn't be co
 
sergei
2017/08/31 12:43:38
Acknowledged.
 | |
| 91 { | 92 { | 
| 92 assert(subscription, u"Attempt to remove a null subscription"_str); | 93 assert(subscription, u"Attempt to remove a null subscription"_str); | 
| 93 | 94 | 
| 94 if (!subscription || !subscription->GetListed()) | 95 if (!subscription || !subscription->GetListed()) | 
| 95 return false; | 96 return false; | 
| 96 | 97 | 
| 97 for (auto it = mSubscriptions.begin(); it != mSubscriptions.end(); ++it) | 98 for (auto it = mSubscriptions.begin(); it != mSubscriptions.end(); ++it) | 
| 98 { | 99 { | 
| 99 if (*it == subscription) | 100 if (*it == subscription) | 
| 100 { | 101 { | 
| 101 mSubscriptions.erase(it); | 102 mSubscriptions.erase(it); | 
| 102 break; | 103 break; | 
| 103 } | 104 } | 
| 104 } | 105 } | 
| 105 subscription->SetListed(false); | 106 subscription->SetListed(false); | 
| 106 | 107 | 
| 107 FilterNotifier::SubscriptionChange( | 108 FilterNotifier::SubscriptionChange( | 
| 108 FilterNotifier::Topic::SUBSCRIPTION_REMOVED, | 109 FilterNotifier::Topic::SUBSCRIPTION_REMOVED, | 
| 109 subscription | 110 subscription | 
| 110 ); | 111 ); | 
| 111 return true; | 112 return true; | 
| 112 } | 113 } | 
| 113 | 114 | 
| 114 bool FilterStorage::MoveSubscription(Subscription* subscription, | 115 bool FilterStorage::MoveSubscription(Subscription* subscription, | 
| 
sergei
2017/08/24 13:32:05
should `subscription` argument be a const pointer?
 
Wladimir Palant
2017/08/31 11:32:35
It cannot be. Even if we make changes to the refer
 
sergei
2017/08/31 12:43:38
Acknowledged.
 | |
| 115 const Subscription* insertBefore) | 116 const Subscription* insertBefore) | 
| 116 { | 117 { | 
| 117 assert(subscription, u"Attempt to move a null subscription"_str); | 118 assert(subscription, u"Attempt to move a null subscription"_str); | 
| 118 | 119 | 
| 119 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); | |
| 120 if (oldPos == -1) | 122 if (oldPos == -1) | 
| 121 return false; | 123 return false; | 
| 
sergei
2017/08/24 13:32:05
should there be an assert? it's seems undesired wh
 
Wladimir Palant
2017/08/31 11:32:34
Done.
 | |
| 122 | 124 | 
| 123 int newPos = -1; | 125 int newPos = -1; | 
| 124 if (insertBefore) | 126 if (insertBefore) | 
| 125 newPos = IndexOfSubscription(insertBefore); | 127 newPos = IndexOfSubscription(insertBefore); | 
| 126 if (newPos == -1) | 128 if (newPos == -1) | 
| 127 newPos = mSubscriptions.size(); | 129 newPos = mSubscriptions.size(); | 
| 128 | 130 | 
| 129 if (newPos > oldPos) | 131 if (newPos > oldPos) | 
| 130 newPos--; | 132 newPos--; | 
| 131 | 133 | 
| 132 if (newPos == oldPos) | 134 if (newPos == oldPos) | 
| 133 return false; | 135 return false; | 
| 134 | 136 | 
| 135 mSubscriptions.erase(mSubscriptions.begin() + oldPos); | 137 mSubscriptions.erase(mSubscriptions.begin() + oldPos); | 
| 136 mSubscriptions.emplace(mSubscriptions.begin() + newPos, subscription); | 138 mSubscriptions.emplace(mSubscriptions.begin() + newPos, subscription); | 
| 137 | 139 | 
| 138 FilterNotifier::SubscriptionChange( | 140 FilterNotifier::SubscriptionChange( | 
| 139 FilterNotifier::Topic::SUBSCRIPTION_MOVED, | 141 FilterNotifier::Topic::SUBSCRIPTION_MOVED, | 
| 140 subscription | 142 subscription | 
| 141 ); | 143 ); | 
| 142 return true; | 144 return true; | 
| 143 } | 145 } | 
| LEFT | RIGHT |