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

Unified Diff: compiled/storage/FilterStorage.cpp

Issue 29426559: Issue 5137 - [emscripten] Added basic filter storage implementation (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore
Patch Set: Turned FilterStorage into a singleton class Created May 10, 2017, 2:06 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: compiled/storage/FilterStorage.cpp
===================================================================
new file mode 100644
--- /dev/null
+++ b/compiled/storage/FilterStorage.cpp
@@ -0,0 +1,143 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2017 eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <vector>
+
+#include "FilterStorage.h"
+#include "../filter/Filter.h"
+#include "../subscription/UserDefinedSubscription.h"
+#include "../FilterNotifier.h"
+
+FilterStorage* FilterStorage::mInstance = new FilterStorage();
+
+unsigned int FilterStorage::GetSubscriptionCount() const
+{
+ return mSubscriptions.size();
+}
+
+Subscription* FilterStorage::SubscriptionAt(unsigned int index) const
+{
+ if (index >= mSubscriptions.size())
+ return nullptr;
+
+ SubscriptionPtr result(mSubscriptions[index]);
+ return result.release();
+}
+
+int FilterStorage::IndexOfSubscription(const Subscription* subscription) const
+{
+ for (unsigned int i = 0; i < mSubscriptions.size(); i++)
+ if (mSubscriptions[i] == subscription)
+ return i;
+ return -1;
+}
+
+Subscription* FilterStorage::GetSubscriptionForFilter(const Filter* filter)
+{
+ SubscriptionPtr fallback;
+
+ for (auto& subscription : mSubscriptions)
+ {
+ UserDefinedSubscription* userDefinedSubscription =
+ subscription->As<UserDefinedSubscription>();
+ if (userDefinedSubscription && !userDefinedSubscription->GetDisabled() &&
+ userDefinedSubscription->IsDefaultFor(filter))
+ {
+ SubscriptionPtr result(subscription);
+ return result.release();
+ }
+ else if (!fallback && userDefinedSubscription &&
+ userDefinedSubscription->IsGeneric())
+ {
+ fallback = subscription;
+ }
+ }
+
+ return fallback.release();
+}
+
+bool FilterStorage::AddSubscription(Subscription* subscription)
+{
+ assert(subscription, u"Attempt to add a null subscription"_str);
+
+ if (!subscription || subscription->GetListed())
+ return false;
+
+ mSubscriptions.emplace_back(subscription);
+ subscription->SetListed(true);
+
+ FilterNotifier::SubscriptionChange(
+ FilterNotifier::Topic::SUBSCRIPTION_ADDED,
+ subscription
+ );
+ return true;
+}
+
+bool FilterStorage::RemoveSubscription(Subscription* subscription)
+{
+ assert(subscription, u"Attempt to remove a null subscription"_str);
+
+ if (!subscription || !subscription->GetListed())
+ return false;
+
+ for (auto it = mSubscriptions.begin(); it != mSubscriptions.end(); ++it)
+ {
+ if (*it == subscription)
+ {
+ mSubscriptions.erase(it);
+ break;
+ }
+ }
+ subscription->SetListed(false);
+
+ FilterNotifier::SubscriptionChange(
+ FilterNotifier::Topic::SUBSCRIPTION_REMOVED,
+ subscription
+ );
+ return true;
+}
+
+bool FilterStorage::MoveSubscription(Subscription* subscription,
+ const Subscription* insertBefore)
+{
+ assert(subscription, u"Attempt to move a null subscription"_str);
+
+ int oldPos = IndexOfSubscription(subscription);
+ if (oldPos == -1)
+ return false;
+
+ int newPos = -1;
+ if (insertBefore)
+ newPos = IndexOfSubscription(insertBefore);
+ if (newPos == -1)
+ newPos = mSubscriptions.size();
+
+ if (newPos > oldPos)
+ newPos--;
+
+ if (newPos == oldPos)
+ return false;
+
+ mSubscriptions.erase(mSubscriptions.begin() + oldPos);
+ mSubscriptions.emplace(mSubscriptions.begin() + newPos, subscription);
+
+ FilterNotifier::SubscriptionChange(
+ FilterNotifier::Topic::SUBSCRIPTION_MOVED,
+ subscription
+ );
+ return true;
+}

Powered by Google App Engine
This is Rietveld