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: rename BuildReferrerChain to BuildFrameStructure Created Nov. 25, 2014, 12:40 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);
Wladimir Palant 2014/11/25 15:46:37 My understanding is that map::emplace won't replac
sergei 2014/11/26 10:34:43 Excellent point, I forget about it.
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::BuildFrameStructure( 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;
Wladimir Palant 2014/11/25 15:46:37 If you rename the function you should also rename
sergei 2014/11/26 10:34:43 renamed to `frames`.
48 if (url.empty())
49 {
50 return referrerChain;
Wladimir Palant 2014/11/25 15:46:37 Why is this special case necessary? An empty strin
sergei 2014/11/26 10:34:43 Because it's rather an invalid argument and we sho
Wladimir Palant 2014/12/09 21:20:03 I realized that I misunderstood how this method is
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.
Wladimir Palant 2014/11/25 15:46:37 This snarky comment seems completely pointless to
sergei 2014/11/26 10:34:43 removed
57 const int maxChainLength = 20;
Wladimir Palant 2014/11/25 15:46:37 Why increase this value? 10 should be enough for a
sergei 2014/11/26 10:34:43 It was needed for tests. removed.
58 referrerChain.reserve(maxChainLength);
Wladimir Palant 2014/11/25 15:46:37 I doubt that this makes sense. While we might get
sergei 2014/11/26 10:34:43 I've tested, it's indeed has only one frame in abo
Wladimir Palant 2014/12/09 21:20:03 Any sane allocation approach does, to minimize mem
sergei 2014/12/12 17:07:07 Actually on a lower level usual allocator rounds i
59 std::map<std::string, RequestInfo>::const_iterator currentEntry =
Wladimir Palant 2014/11/25 15:46:37 Given that you use std::map<std::string, RequestIn
sergei 2014/11/26 10:34:43 changed.
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.
Wladimir Palant 2014/11/25 15:46:37 I assume that the code this comment refers to will
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.
Wladimir Palant 2014/11/25 15:46:37 This special case is unnecessary. An empty referre
sergei 2014/11/26 10:34:43 Not clear which special case is mentioned, could y
Wladimir Palant 2014/12/09 21:20:03 I just realized that this was designed to work dif
sergei 2014/12/12 17:07:07 I also misunderstood it the same way initially and
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))
Wladimir Palant 2014/11/25 15:46:37 Please put the assignment to currentEntry at the e
sergei 2014/11/26 10:34:43 Not sure whether it's easier to read, because 'for
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);
Wladimir Palant 2014/11/25 15:46:37 This should be .insert(referrerChain.begin(), ...)
sergei 2014/11/26 10:34:43 In general it's inefficient, I don't understand wh
Wladimir Palant 2014/12/09 21:20:03 That's what I meant - for tiny lists like what we
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.
Wladimir Palant 2014/11/25 15:46:37 It should also be pretty pointless, with the refer
sergei 2014/11/26 10:34:43 The main reason of it was to use push_back and rev
99 reverse(referrerChain.begin(), referrerChain.end());
100 return referrerChain;
101 } 74 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld