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, 9:05 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,58 @@
+/*
+ * 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)
+{
+ mapping[url] = referrer;
+ if (std::find(cachedUrls.begin(), cachedUrls.end(), url) != cachedUrls.end())
+ cachedUrls.remove(url);
+ cachedUrls.push_back(url);
+ while (cachedUrls.size() > maxCachedUrls)
Wladimir Palant 2014/07/16 09:51:11 I'm already unhappy with std::find() having to go
Felix Dahlke 2014/07/16 10:15:48 Done. We shouldn't really rely on implementation d
Felix Dahlke 2014/07/16 10:18:39 Note that we can optimise the std::find above away
Wladimir Palant 2014/07/16 10:26:01 Overlooked this comment - sure, sounds like a good
Felix Dahlke 2014/07/16 10:34:24 Done.
+ {
+ 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