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: rename BuildReferrerChain to BuildFrameStructure Created Nov. 25, 2014, 12:40 p.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 std::string& url, const std::string& referrer,
28 FilterEngine::ContentType requestType)
28 { 29 {
29 if (mapping.find(url) != mapping.end()) 30 if (mapping.find(url) != mapping.end())
30 cachedUrls.remove(url); 31 cachedUrls.remove(url);
31 cachedUrls.push_back(url); 32 cachedUrls.push_back(url);
32 mapping[url] = referrer; 33 mapping.emplace(url, RequestInfo(referrer, requestType));
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.
33 34
34 const int urlsToPop = cachedUrls.size() - maxCachedUrls; 35 const int urlsToPop = cachedUrls.size() - maxCachedUrls;
35 for (int i = 0; i < urlsToPop; i++) 36 for (int i = 0; i < urlsToPop; i++)
36 { 37 {
37 const std::string poppedUrl = cachedUrls.front(); 38 const std::string poppedUrl = cachedUrls.front();
38 cachedUrls.pop_front(); 39 cachedUrls.pop_front();
39 mapping.erase(poppedUrl); 40 mapping.erase(poppedUrl);
40 } 41 }
41 } 42 }
42 43
43 std::vector<std::string> ReferrerMapping::BuildReferrerChain( 44 std::vector<std::string> ReferrerMapping::BuildFrameStructure(
44 const std::string& url) const 45 const std::string& url) const
45 { 46 {
46 std::vector<std::string> referrerChain; 47 std::vector<std::string> referrerChain;
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`.
47 referrerChain.push_back(url); 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 }
48 // We need to limit the chain length to ensure we don't block indefinitely 52 // We need to limit the chain length to ensure we don't block indefinitely
49 // if there's a referrer loop. 53 // if there's a referrer loop.
50 const int maxChainLength = 10; 54 // Despite there is quite effecient algorithm to work with such loops, merely
51 std::map<std::string, std::string>::const_iterator currentEntry = 55 // limit the chain to some number works fine on practice and seems quiet
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.
52 mapping.find(url); 60 mapping.find(url);
53 for (int i = 0; i < maxChainLength && currentEntry != mapping.end(); i++) 61 // We need it to build [first, second] when there is only one entry in the
62 // `mapping` member, [{second, {first, AnyTypeOfSecond}}].
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())
54 { 70 {
55 const std::string& currentUrl = currentEntry->second; 71 if (currentEntry->second.type == FilterEngine::ContentType::CONTENT_TYPE_SUB DOCUMENT)
56 referrerChain.insert(referrerChain.begin(), currentUrl); 72 {
57 currentEntry = mapping.find(currentUrl); 73 referrerChain.push_back(url);
74 }
75 prevEntry = currentEntry;
76 currentEntry = mapping.find(currentEntry->second.referrer);
58 } 77 }
78 else
79 {
80 // If there is no information for the url in `mapping` then assume that
81 // it's a frame, despite it's not necessary to be a frame.
82 referrerChain.push_back(url);
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
83 }
84 for (int i = 0; i < maxChainLength && currentEntry != mapping.end(); i++,
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 {
87 if (currentEntry->second.type == FilterEngine::ContentType::CONTENT_TYPE_SUB DOCUMENT)
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 }
93 if (prevEntry != mapping.end() && !prevEntry->second.referrer.empty())
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());
59 return referrerChain; 100 return referrerChain;
60 } 101 }
OLDNEW

Powered by Google App Engine
This is Rietveld