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: revert and change referrer map Created Nov. 25, 2014, 12:22 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
« no previous file with comments | « include/AdblockPlus/ReferrerMapping.h ('k') | test/ReferrerMapping.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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));
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::BuildReferrerChain(
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;
47 referrerChain.push_back(url); 48 if (url.empty())
49 {
50 return referrerChain;
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.
57 const int maxChainLength = 20;
58 referrerChain.reserve(maxChainLength);
59 std::map<std::string, RequestInfo>::const_iterator currentEntry =
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.
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);
83 }
84 for (int i = 0; i < maxChainLength && currentEntry != mapping.end(); i++,
85 currentEntry = mapping.find(currentEntry->second.referrer))
86 {
87 if (currentEntry->second.type == FilterEngine::ContentType::CONTENT_TYPE_SUB DOCUMENT)
88 {
89 referrerChain.push_back(currentEntry->first);
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.
99 reverse(referrerChain.begin(), referrerChain.end());
59 return referrerChain; 100 return referrerChain;
60 } 101 }
OLDNEW
« no previous file with comments | « include/AdblockPlus/ReferrerMapping.h ('k') | test/ReferrerMapping.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld