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

Delta Between Two Patch Sets: src/ReferrerMapping.cpp

Issue 5768603836088320: Issue 1564-Fix FilterEngine::Matches for allowing request which is whitelisted in the ascendant node
Left Patch Set: revert and change referrer map Created Nov. 25, 2014, 12:22 p.m.
Right Patch Set: fix according to comments Created Dec. 12, 2014, 4:59 p.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 | « include/AdblockPlus/ReferrerMapping.h ('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 Url& url, const Url& referrer, FrameIndicator is Frame)
28 FilterEngine::ContentType requestType)
29 { 28 {
30 if (mapping.find(url) != mapping.end()) 29 if (mapping.find(url) != mapping.end())
31 cachedUrls.remove(url); 30 cachedUrls.remove(url);
32 cachedUrls.push_back(url); 31 cachedUrls.push_back(url);
33 mapping.emplace(url, RequestInfo(referrer, requestType)); 32 mapping[url] = RequestInfo(referrer, isFrame);
34 33
35 const int urlsToPop = cachedUrls.size() - maxCachedUrls; 34 const int urlsToPop = cachedUrls.size() - maxCachedUrls;
36 for (int i = 0; i < urlsToPop; i++) 35 for (int i = 0; i < urlsToPop; i++)
37 { 36 {
38 const std::string poppedUrl = cachedUrls.front(); 37 const std::string poppedUrl = cachedUrls.front();
39 cachedUrls.pop_front(); 38 cachedUrls.pop_front();
40 mapping.erase(poppedUrl); 39 mapping.erase(poppedUrl);
41 } 40 }
42 } 41 }
43 42
44 std::vector<std::string> ReferrerMapping::BuildReferrerChain( 43 ReferrerMapping::Urls ReferrerMapping::BuildFrameStructure(const Url& url) const
45 const std::string& url) const
46 { 44 {
47 std::vector<std::string> referrerChain; 45 Urls frames;
48 if (url.empty())
49 {
50 return referrerChain;
51 }
52 // 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
53 // if there's a referrer loop. 47 // if there's a referrer loop.
54 // Despite there is quite effecient algorithm to work with such loops, merely 48 const int maxChainLength = 10;
55 // limit the chain to some number works fine on practice and seems quiet 49 ReferrerMap::const_iterator currentEntry = url.empty() ? mapping.end() :
56 // elegant.
57 const int maxChainLength = 20;
58 referrerChain.reserve(maxChainLength);
59 std::map<std::string, RequestInfo>::const_iterator currentEntry =
60 mapping.find(url); 50 mapping.find(url);
61 // We need it to build [first, second] when there is only one entry in the 51 ReferrerMap::const_iterator prevEntry = mapping.end();
62 // `mapping` member, [{second, {first, AnyTypeOfSecond}}]. 52 for (int i = 0; i < maxChainLength && currentEntry != mapping.end(); i++)
63 // So, if we exit from the cycle not because of loop limit and the first is
64 // not empty then add it.
65 std::map<std::string, RequestInfo>::const_iterator prevEntry = mapping.end();
66
67 // process the current url beyond the loop below because we should add it
68 // even if `mapping` is empty.
69 if (currentEntry != mapping.end())
70 { 53 {
71 if (currentEntry->second.type == FilterEngine::ContentType::CONTENT_TYPE_SUB DOCUMENT) 54 if (currentEntry->second.IsFrame())
72 { 55 {
73 referrerChain.push_back(url); 56 frames.insert(frames.begin(), currentEntry->first);
74 } 57 }
75 prevEntry = currentEntry; 58 prevEntry = currentEntry;
76 currentEntry = mapping.find(currentEntry->second.referrer); 59 currentEntry = mapping.find(currentEntry->second.referrer);
77 } 60 }
78 else 61 if (prevEntry == mapping.end())
79 { 62 {
80 // If there is no information for the url in `mapping` then assume that 63 if (!url.empty())
81 // it's a frame, despite it's not necessary to be a frame. 64 {
82 referrerChain.push_back(url); 65 // No data, just assume that the url is a frame.
66 frames.push_back(url);
67 }
83 } 68 }
84 for (int i = 0; i < maxChainLength && currentEntry != mapping.end(); i++, 69 else if (!prevEntry->second.referrer.empty())
85 currentEntry = mapping.find(currentEntry->second.referrer))
86 { 70 {
87 if (currentEntry->second.type == FilterEngine::ContentType::CONTENT_TYPE_SUB DOCUMENT) 71 frames.insert(frames.begin(), prevEntry->second.referrer);
88 {
89 referrerChain.push_back(currentEntry->first);
90 }
91 prevEntry = currentEntry;
92 } 72 }
93 if (prevEntry != mapping.end() && !prevEntry->second.referrer.empty()) 73 return frames;
94 {
95 referrerChain.push_back(prevEntry->second.referrer);
96 }
97 // it should be slightly more efficient to reverse it at the end instead
98 // of copying the tail of the referrerChain on each adding.
99 reverse(referrerChain.begin(), referrerChain.end());
100 return referrerChain;
101 } 74 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld