Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: compiled/storage/FilterStorage.cpp

Issue 29426559: Issue 5137 - [emscripten] Added basic filter storage implementation (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Left Patch Set: Turned FilterStorage into a singleton class Created May 10, 2017, 2:06 p.m.
Right Patch Set: Fixed bogus assert Created Aug. 31, 2017, 12:44 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « compiled/storage/FilterStorage.h ('k') | compiled/subscription/DownloadableSubscription.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 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++)
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
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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 ); 111 );
111 return true; 112 return true;
112 } 113 }
113 114
114 bool FilterStorage::MoveSubscription(Subscription* subscription, 115 bool FilterStorage::MoveSubscription(Subscription* subscription,
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;
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 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld