| Index: src/ReferrerMapping.cpp |
| =================================================================== |
| --- a/src/ReferrerMapping.cpp |
| +++ b/src/ReferrerMapping.cpp |
| @@ -24,12 +24,13 @@ |
| { |
| } |
| -void ReferrerMapping::Add(const std::string& url, const std::string& referrer) |
| +void ReferrerMapping::Add(const std::string& url, const std::string& referrer, |
| + FilterEngine::ContentType requestType) |
| { |
| if (mapping.find(url) != mapping.end()) |
| cachedUrls.remove(url); |
| cachedUrls.push_back(url); |
| - mapping[url] = referrer; |
| + 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.
|
| const int urlsToPop = cachedUrls.size() - maxCachedUrls; |
| for (int i = 0; i < urlsToPop; i++) |
| @@ -40,21 +41,61 @@ |
| } |
| } |
| -std::vector<std::string> ReferrerMapping::BuildReferrerChain( |
| +std::vector<std::string> ReferrerMapping::BuildFrameStructure( |
| const std::string& url) const |
| { |
| 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`.
|
| - referrerChain.push_back(url); |
| + if (url.empty()) |
| + { |
| + 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
|
| + } |
| // We need to limit the chain length to ensure we don't block indefinitely |
| // if there's a referrer loop. |
| - const int maxChainLength = 10; |
| - std::map<std::string, std::string>::const_iterator currentEntry = |
| + // Despite there is quite effecient algorithm to work with such loops, merely |
| + // limit the chain to some number works fine on practice and seems quiet |
| + // elegant. |
|
Wladimir Palant
2014/11/25 15:46:37
This snarky comment seems completely pointless to
sergei
2014/11/26 10:34:43
removed
|
| + 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.
|
| + 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
|
| + 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.
|
| mapping.find(url); |
| - for (int i = 0; i < maxChainLength && currentEntry != mapping.end(); i++) |
| + // We need it to build [first, second] when there is only one entry in the |
| + // `mapping` member, [{second, {first, AnyTypeOfSecond}}]. |
| + // So, if we exit from the cycle not because of loop limit and the first is |
| + // not empty then add it. |
|
Wladimir Palant
2014/11/25 15:46:37
I assume that the code this comment refers to will
|
| + std::map<std::string, RequestInfo>::const_iterator prevEntry = mapping.end(); |
| + |
| + // process the current url beyond the loop below because we should add it |
| + // even if `mapping` is empty. |
| + if (currentEntry != mapping.end()) |
| { |
| - const std::string& currentUrl = currentEntry->second; |
| - referrerChain.insert(referrerChain.begin(), currentUrl); |
| - currentEntry = mapping.find(currentUrl); |
| + if (currentEntry->second.type == FilterEngine::ContentType::CONTENT_TYPE_SUBDOCUMENT) |
| + { |
| + referrerChain.push_back(url); |
| + } |
| + prevEntry = currentEntry; |
| + currentEntry = mapping.find(currentEntry->second.referrer); |
| } |
| + else |
| + { |
| + // If there is no information for the url in `mapping` then assume that |
| + // it's a frame, despite it's not necessary to be a frame. |
| + 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
|
| + } |
| + for (int i = 0; i < maxChainLength && currentEntry != mapping.end(); i++, |
| + 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
|
| + { |
| + if (currentEntry->second.type == FilterEngine::ContentType::CONTENT_TYPE_SUBDOCUMENT) |
| + { |
| + 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
|
| + } |
| + prevEntry = currentEntry; |
| + } |
| + if (prevEntry != mapping.end() && !prevEntry->second.referrer.empty()) |
| + { |
| + referrerChain.push_back(prevEntry->second.referrer); |
| + } |
| + // it should be slightly more efficient to reverse it at the end instead |
| + // 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
|
| + reverse(referrerChain.begin(), referrerChain.end()); |
| return referrerChain; |
| } |