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

Delta Between Two Patch Sets: src/ReferrerMapping.cpp

Issue 6307944991817728: Issue 984 - port referrer mapping from Android (Closed)
Left Patch Set: Created July 16, 2014, 9:05 a.m.
Right Patch Set: Created July 16, 2014, 10:32 a.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 | « libadblockplus.gyp ('k') | test/ReferrerMapping.cpp » ('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 <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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 <AdblockPlus/ReferrerMapping.h> 18 #include <AdblockPlus/ReferrerMapping.h>
19 19
20 using namespace AdblockPlus; 20 using namespace AdblockPlus;
21 21
22 ReferrerMapping::ReferrerMapping(const int maxCachedUrls) 22 ReferrerMapping::ReferrerMapping(const int maxCachedUrls)
23 : maxCachedUrls(maxCachedUrls) 23 : maxCachedUrls(maxCachedUrls)
24 { 24 {
25 } 25 }
26 26
27 void ReferrerMapping::Add(const std::string& url, const std::string& referrer) 27 void ReferrerMapping::Add(const std::string& url, const std::string& referrer)
28 { 28 {
29 mapping[url] = referrer; 29 if (mapping.find(url) != mapping.end())
30 if (std::find(cachedUrls.begin(), cachedUrls.end(), url) != cachedUrls.end())
31 cachedUrls.remove(url); 30 cachedUrls.remove(url);
32 cachedUrls.push_back(url); 31 cachedUrls.push_back(url);
33 while (cachedUrls.size() > maxCachedUrls) 32 mapping[url] = referrer;
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.
33
34 const int urlsToPop = cachedUrls.size() - maxCachedUrls;
35 for (int i = 0; i < urlsToPop; i++)
34 { 36 {
35 const std::string poppedUrl = cachedUrls.front(); 37 const std::string poppedUrl = cachedUrls.front();
36 cachedUrls.pop_front(); 38 cachedUrls.pop_front();
37 mapping.erase(poppedUrl); 39 mapping.erase(poppedUrl);
38 } 40 }
39 } 41 }
40 42
41 std::vector<std::string> ReferrerMapping::BuildReferrerChain( 43 std::vector<std::string> ReferrerMapping::BuildReferrerChain(
42 const std::string& url) const 44 const std::string& url) const
43 { 45 {
44 std::vector<std::string> referrerChain; 46 std::vector<std::string> referrerChain;
45 referrerChain.push_back(url); 47 referrerChain.push_back(url);
46 // We need to limit the chain length to ensure we don't block indefinitely 48 // We need to limit the chain length to ensure we don't block indefinitely
47 // if there's a referrer loop. 49 // if there's a referrer loop.
48 const int maxChainLength = 10; 50 const int maxChainLength = 10;
49 std::map<std::string, std::string>::const_iterator currentEntry = 51 std::map<std::string, std::string>::const_iterator currentEntry =
50 mapping.find(url); 52 mapping.find(url);
51 for (int i = 0; i < maxChainLength && currentEntry != mapping.end(); i++) 53 for (int i = 0; i < maxChainLength && currentEntry != mapping.end(); i++)
52 { 54 {
53 const std::string& currentUrl = currentEntry->second; 55 const std::string& currentUrl = currentEntry->second;
54 referrerChain.insert(referrerChain.begin(), currentUrl); 56 referrerChain.insert(referrerChain.begin(), currentUrl);
55 currentEntry = mapping.find(currentUrl); 57 currentEntry = mapping.find(currentUrl);
56 } 58 }
57 return referrerChain; 59 return referrerChain;
58 } 60 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld