OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 #include <AdblockPlus/ReferrerMapping.h> |
| 19 |
| 20 using namespace AdblockPlus; |
| 21 |
| 22 ReferrerMapping::ReferrerMapping(const int maxCachedUrls) |
| 23 : maxCachedUrls(maxCachedUrls) |
| 24 { |
| 25 } |
| 26 |
| 27 void ReferrerMapping::Add(const std::string& url, const std::string& referrer) |
| 28 { |
| 29 if (mapping.find(url) != mapping.end()) |
| 30 cachedUrls.remove(url); |
| 31 cachedUrls.push_back(url); |
| 32 mapping[url] = referrer; |
| 33 |
| 34 const int urlsToPop = cachedUrls.size() - maxCachedUrls; |
| 35 for (int i = 0; i < urlsToPop; i++) |
| 36 { |
| 37 const std::string poppedUrl = cachedUrls.front(); |
| 38 cachedUrls.pop_front(); |
| 39 mapping.erase(poppedUrl); |
| 40 } |
| 41 } |
| 42 |
| 43 std::vector<std::string> ReferrerMapping::BuildReferrerChain( |
| 44 const std::string& url) const |
| 45 { |
| 46 std::vector<std::string> referrerChain; |
| 47 referrerChain.push_back(url); |
| 48 // We need to limit the chain length to ensure we don't block indefinitely |
| 49 // if there's a referrer loop. |
| 50 const int maxChainLength = 10; |
| 51 std::map<std::string, std::string>::const_iterator currentEntry = |
| 52 mapping.find(url); |
| 53 for (int i = 0; i < maxChainLength && currentEntry != mapping.end(); i++) |
| 54 { |
| 55 const std::string& currentUrl = currentEntry->second; |
| 56 referrerChain.insert(referrerChain.begin(), currentUrl); |
| 57 currentEntry = mapping.find(currentUrl); |
| 58 } |
| 59 return referrerChain; |
| 60 } |
OLD | NEW |