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

Unified Diff: src/ReferrerMapping.cpp

Issue 6307944991817728: Issue 984 - port referrer mapping from Android (Closed)
Patch Set: Created July 16, 2014, 10:32 a.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
« no previous file with comments | « libadblockplus.gyp ('k') | test/ReferrerMapping.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/ReferrerMapping.cpp
===================================================================
new file mode 100644
--- /dev/null
+++ b/src/ReferrerMapping.cpp
@@ -0,0 +1,60 @@
+/*
+ * This file is part of Adblock Plus <http://adblockplus.org/>,
+ * Copyright (C) 2006-2014 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 <AdblockPlus/ReferrerMapping.h>
+
+using namespace AdblockPlus;
+
+ReferrerMapping::ReferrerMapping(const int maxCachedUrls)
+ : maxCachedUrls(maxCachedUrls)
+{
+}
+
+void ReferrerMapping::Add(const std::string& url, const std::string& referrer)
+{
+ if (mapping.find(url) != mapping.end())
+ cachedUrls.remove(url);
+ cachedUrls.push_back(url);
+ mapping[url] = referrer;
+
+ const int urlsToPop = cachedUrls.size() - maxCachedUrls;
+ for (int i = 0; i < urlsToPop; i++)
+ {
+ const std::string poppedUrl = cachedUrls.front();
+ cachedUrls.pop_front();
+ mapping.erase(poppedUrl);
+ }
+}
+
+std::vector<std::string> ReferrerMapping::BuildReferrerChain(
+ const std::string& url) const
+{
+ std::vector<std::string> referrerChain;
+ referrerChain.push_back(url);
+ // We need to limit the chain length to ensure we don't block indefinitely
+ // if there's a referrer loop.
+ const int maxChainLength = 10;
+ std::map<std::string, std::string>::const_iterator currentEntry =
+ mapping.find(url);
+ for (int i = 0; i < maxChainLength && currentEntry != mapping.end(); i++)
+ {
+ const std::string& currentUrl = currentEntry->second;
+ referrerChain.insert(referrerChain.begin(), currentUrl);
+ currentEntry = mapping.find(currentUrl);
+ }
+ return referrerChain;
+}
« no previous file with comments | « libadblockplus.gyp ('k') | test/ReferrerMapping.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld