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

Side by Side Diff: src/ReferrerMapping.cpp

Issue 5768603836088320: Issue 1564-Fix FilterEngine::Matches for allowing request which is whitelisted in the ascendant node
Patch Set: fix according to comments and add some tests Created Nov. 26, 2014, 10:35 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 Url& url, const Url& referrer, FrameIndicator is Frame)
28 { 28 {
29 if (mapping.find(url) != mapping.end()) 29 if (mapping.find(url) != mapping.end())
30 cachedUrls.remove(url); 30 cachedUrls.remove(url);
31 cachedUrls.push_back(url); 31 cachedUrls.push_back(url);
32 mapping[url] = referrer; 32 mapping[url] = RequestInfo(referrer, isFrame);
33 33
34 const int urlsToPop = cachedUrls.size() - maxCachedUrls; 34 const int urlsToPop = cachedUrls.size() - maxCachedUrls;
35 for (int i = 0; i < urlsToPop; i++) 35 for (int i = 0; i < urlsToPop; i++)
36 { 36 {
37 const std::string poppedUrl = cachedUrls.front(); 37 const std::string poppedUrl = cachedUrls.front();
38 cachedUrls.pop_front(); 38 cachedUrls.pop_front();
39 mapping.erase(poppedUrl); 39 mapping.erase(poppedUrl);
40 } 40 }
41 } 41 }
42 42
43 std::vector<std::string> ReferrerMapping::BuildReferrerChain( 43 ReferrerMapping::Urls ReferrerMapping::BuildFrameStructure(const Url& url) const
44 const std::string& url) const
45 { 44 {
46 std::vector<std::string> referrerChain; 45 Urls frames;
47 referrerChain.push_back(url);
48 // We need to limit the chain length to ensure we don't block indefinitely 46 // We need to limit the chain length to ensure we don't block indefinitely
49 // if there's a referrer loop. 47 // if there's a referrer loop.
50 const int maxChainLength = 10; 48 const int maxChainLength = 10;
51 std::map<std::string, std::string>::const_iterator currentEntry = 49 ReferrerMap::const_iterator currentEntry = mapping.end();
52 mapping.find(url); 50 ReferrerMap::const_iterator prevEntry = mapping.end();
51 // process the current url beyond the loop below because we should add it
Wladimir Palant 2014/12/09 21:20:03 beyond => outside
52 // even if `mapping` is empty.
53 if (!url.empty())
54 {
55 currentEntry = mapping.find(url);
56 if (currentEntry != mapping.end())
57 {
58 if (currentEntry->second.IsFrame())
59 {
60 frames.push_back(url);
61 }
62 prevEntry = currentEntry;
63 currentEntry = mapping.find(currentEntry->second.referrer);
64 }
65 else
66 {
67 // If there is no information for the url in `mapping` then assume that
68 // it's a frame, despite it's not necessary to be a frame.
Wladimir Palant 2014/12/09 21:20:03 Nit: "despite it not necessarily being a frame"
69 frames.push_back(url);
70 }
71 }
Wladimir Palant 2014/12/09 21:20:03 Can't we check prevEntry after the loop instead of
sergei 2014/12/12 17:07:07 That does work, please check the updated version.
72
53 for (int i = 0; i < maxChainLength && currentEntry != mapping.end(); i++) 73 for (int i = 0; i < maxChainLength && currentEntry != mapping.end(); i++)
54 { 74 {
55 const std::string& currentUrl = currentEntry->second; 75 if (currentEntry->second.IsFrame())
56 referrerChain.insert(referrerChain.begin(), currentUrl); 76 {
57 currentEntry = mapping.find(currentUrl); 77 frames.insert(frames.begin(), currentEntry->first);
78 }
79 prevEntry = currentEntry;
80 currentEntry = mapping.find(currentEntry->second.referrer);
58 } 81 }
59 return referrerChain; 82 if (prevEntry != mapping.end() && !prevEntry->second.referrer.empty())
83 {
84 frames.insert(frames.begin(), prevEntry->second.referrer);
85 }
86 return frames;
60 } 87 }
OLDNEW

Powered by Google App Engine
This is Rietveld