Commit 693f7d3d authored by cfredric's avatar cfredric Committed by Chromium LUCI CQ

Apply some clangd suggestions in cookie_monster.cc.

The suggestions were to use range-based for-loops instead of C-style
for-loops. I also noticed some opportunities to use <algorithm> instead
of raw loops, and made those changes as well.

Change-Id: Ic5bf82090ef6f7b5a0a6908b0df4d4aa9ca9a959
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2584703
Commit-Queue: Chris Fredrickson <cfredric@chromium.org>
Reviewed-by: default avatarLily Chen <chlily@chromium.org>
Cr-Commit-Position: refs/heads/master@{#835883}
parent f5307871
...@@ -55,6 +55,7 @@ ...@@ -55,6 +55,7 @@
#include "base/metrics/field_trial.h" #include "base/metrics/field_trial.h"
#include "base/metrics/histogram.h" #include "base/metrics/histogram.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/ranges/algorithm.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/strings/string_piece.h" #include "base/strings/string_piece.h"
...@@ -158,12 +159,7 @@ const int CookieMonster::kSafeFromGlobalPurgeDays = 30; ...@@ -158,12 +159,7 @@ const int CookieMonster::kSafeFromGlobalPurgeDays = 30;
namespace { namespace {
bool ContainsControlCharacter(const std::string& s) { bool ContainsControlCharacter(const std::string& s) {
for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) { return base::ranges::any_of(s, [](char c) { return c >= 0 && c <= 31; });
if ((*i >= 0) && (*i <= 31))
return true;
}
return false;
} }
typedef std::vector<CanonicalCookie*> CanonicalCookieVector; typedef std::vector<CanonicalCookie*> CanonicalCookieVector;
...@@ -897,10 +893,10 @@ void CookieMonster::TrimDuplicateCookiesForKey(const std::string& key, ...@@ -897,10 +893,10 @@ void CookieMonster::TrimDuplicateCookiesForKey(const std::string& key,
// Otherwise, delete all the duplicate cookies, both from our in-memory store // Otherwise, delete all the duplicate cookies, both from our in-memory store
// and from the backing store. // and from the backing store.
for (auto it = equivalent_cookies.begin(); it != equivalent_cookies.end(); for (std::pair<const CanonicalCookie::UniqueCookieKey, CookieSet>&
++it) { equivalent_cookie : equivalent_cookies) {
const CanonicalCookie::UniqueCookieKey& signature = it->first; const CanonicalCookie::UniqueCookieKey& signature = equivalent_cookie.first;
CookieSet& dupes = it->second; CookieSet& dupes = equivalent_cookie.second;
if (dupes.size() <= 1) if (dupes.size() <= 1)
continue; // This cookiename/path has no duplicates. continue; // This cookiename/path has no duplicates.
...@@ -921,8 +917,8 @@ void CookieMonster::TrimDuplicateCookiesForKey(const std::string& key, ...@@ -921,8 +917,8 @@ void CookieMonster::TrimDuplicateCookiesForKey(const std::string& key,
// Remove all the cookies identified by |dupes|. It is valid to delete our // Remove all the cookies identified by |dupes|. It is valid to delete our
// list of iterators one at a time, since |cookies_| is a multimap (they // list of iterators one at a time, since |cookies_| is a multimap (they
// don't invalidate existing iterators following deletion). // don't invalidate existing iterators following deletion).
for (auto dupes_it = dupes.begin(); dupes_it != dupes.end(); ++dupes_it) { for (const CookieMap::iterator& dupe : dupes) {
InternalDeleteCookie(*dupes_it, true, InternalDeleteCookie(dupe, true,
DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE); DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE);
} }
} }
...@@ -972,24 +968,23 @@ void CookieMonster::FilterCookiesWithOptions( ...@@ -972,24 +968,23 @@ void CookieMonster::FilterCookiesWithOptions(
cookie_access_delegate() && cookie_access_delegate() &&
cookie_access_delegate()->ShouldTreatUrlAsTrustworthy(url); cookie_access_delegate()->ShouldTreatUrlAsTrustworthy(url);
for (std::vector<CanonicalCookie*>::iterator it = cookie_ptrs->begin(); for (CanonicalCookie* cookie_ptr : *cookie_ptrs) {
it != cookie_ptrs->end(); it++) {
// Filter out cookies that should not be included for a request to the // Filter out cookies that should not be included for a request to the
// given |url|. HTTP only cookies are filtered depending on the passed // given |url|. HTTP only cookies are filtered depending on the passed
// cookie |options|. // cookie |options|.
CookieAccessResult access_result = (*it)->IncludeForRequestURL( CookieAccessResult access_result = cookie_ptr->IncludeForRequestURL(
url, options, url, options,
CookieAccessParams{GetAccessSemanticsForCookie(**it), CookieAccessParams{GetAccessSemanticsForCookie(*cookie_ptr),
delegate_treats_url_as_trustworthy}); delegate_treats_url_as_trustworthy});
if (!access_result.status.IsInclude()) { if (!access_result.status.IsInclude()) {
if (options.return_excluded_cookies()) if (options.return_excluded_cookies())
excluded_cookies->push_back({**it, access_result}); excluded_cookies->push_back({*cookie_ptr, access_result});
continue; continue;
} }
if (options.update_access_time()) if (options.update_access_time())
InternalUpdateCookieAccessTime(*it, current_time); InternalUpdateCookieAccessTime(cookie_ptr, current_time);
int destination_port = url.EffectiveIntPort(); int destination_port = url.EffectiveIntPort();
...@@ -999,26 +994,26 @@ void CookieMonster::FilterCookiesWithOptions( ...@@ -999,26 +994,26 @@ void CookieMonster::FilterCookiesWithOptions(
ReducePortRangeForCookieHistogram(destination_port)); ReducePortRangeForCookieHistogram(destination_port));
UMA_HISTOGRAM_ENUMERATION( UMA_HISTOGRAM_ENUMERATION(
"Cookie.Port.ReadDiffersFromSet.Localhost", "Cookie.Port.ReadDiffersFromSet.Localhost",
IsCookieSentToSamePortThatSetIt(url, (*it)->SourcePort(), IsCookieSentToSamePortThatSetIt(url, cookie_ptr->SourcePort(),
(*it)->SourceScheme())); cookie_ptr->SourceScheme()));
} else { } else {
UMA_HISTOGRAM_ENUMERATION( UMA_HISTOGRAM_ENUMERATION(
"Cookie.Port.Read.RemoteHost", "Cookie.Port.Read.RemoteHost",
ReducePortRangeForCookieHistogram(destination_port)); ReducePortRangeForCookieHistogram(destination_port));
UMA_HISTOGRAM_ENUMERATION( UMA_HISTOGRAM_ENUMERATION(
"Cookie.Port.ReadDiffersFromSet.RemoteHost", "Cookie.Port.ReadDiffersFromSet.RemoteHost",
IsCookieSentToSamePortThatSetIt(url, (*it)->SourcePort(), IsCookieSentToSamePortThatSetIt(url, cookie_ptr->SourcePort(),
(*it)->SourceScheme())); cookie_ptr->SourceScheme()));
} }
if ((*it)->IsDomainCookie()) { if (cookie_ptr->IsDomainCookie()) {
UMA_HISTOGRAM_ENUMERATION( UMA_HISTOGRAM_ENUMERATION(
"Cookie.Port.ReadDiffersFromSet.DomainSet", "Cookie.Port.ReadDiffersFromSet.DomainSet",
IsCookieSentToSamePortThatSetIt(url, (*it)->SourcePort(), IsCookieSentToSamePortThatSetIt(url, cookie_ptr->SourcePort(),
(*it)->SourceScheme())); cookie_ptr->SourceScheme()));
} }
included_cookies->push_back({**it, access_result}); included_cookies->push_back({*cookie_ptr, access_result});
} }
} }
...@@ -1738,18 +1733,17 @@ bool CookieMonster::HasCookieableScheme(const GURL& url) { ...@@ -1738,18 +1733,17 @@ bool CookieMonster::HasCookieableScheme(const GURL& url) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
// Make sure the request is on a cookie-able url scheme. // Make sure the request is on a cookie-able url scheme.
for (size_t i = 0; i < cookieable_schemes_.size(); ++i) { bool is_cookieable = base::ranges::any_of(
// We matched a scheme. cookieable_schemes_, [&url](const std::string& cookieable_scheme) {
if (url.SchemeIs(cookieable_schemes_[i].c_str())) { return url.SchemeIs(cookieable_scheme.c_str());
// We've matched a supported scheme. });
return true;
} if (!is_cookieable) {
// The scheme didn't match any in our allowed list.
DVLOG(net::cookie_util::kVlogPerCookieMonster)
<< "WARNING: Unsupported cookie scheme: " << url.scheme();
} }
return is_cookieable;
// The scheme didn't match any in our allowed list.
DVLOG(net::cookie_util::kVlogPerCookieMonster)
<< "WARNING: Unsupported cookie scheme: " << url.scheme();
return false;
} }
CookieAccessSemantics CookieMonster::GetAccessSemanticsForCookie( CookieAccessSemantics CookieMonster::GetAccessSemanticsForCookie(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment