1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/net/evicted_domain_cookie_counter.h"
#include <algorithm>
#include <vector>
#include "base/metrics/histogram.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "components/google/core/browser/google_util.h"
#include "net/cookies/canonical_cookie.h"
namespace chrome_browser_net {
using base::Time;
using base::TimeDelta;
namespace {
const size_t kMaxEvictedDomainCookies = 500;
const size_t kPurgeEvictedDomainCookies = 100;
class DelegateImpl : public EvictedDomainCookieCounter::Delegate {
public:
DelegateImpl();
// EvictedDomainCookieCounter::Delegate implementation.
void Report(const EvictedDomainCookieCounter::EvictedCookie& evicted_cookie,
const Time& reinstatement_time) override;
Time CurrentTime() const override;
};
DelegateImpl::DelegateImpl() {}
void DelegateImpl::Report(
const EvictedDomainCookieCounter::EvictedCookie& evicted_cookie,
const Time& reinstatement_time) {
TimeDelta reinstatement_delay(
reinstatement_time - evicted_cookie.eviction_time);
// Need to duplicate UMA_HISTOGRAM_CUSTOM_TIMES(), since it is a macro that
// defines a static variable.
if (evicted_cookie.is_google) {
UMA_HISTOGRAM_CUSTOM_TIMES("Cookie.ReinstatedCookiesGoogle",
reinstatement_delay,
TimeDelta::FromSeconds(1),
TimeDelta::FromDays(7),
50);
} else {
UMA_HISTOGRAM_CUSTOM_TIMES("Cookie.ReinstatedCookiesOther",
reinstatement_delay,
TimeDelta::FromSeconds(1),
TimeDelta::FromDays(7),
50);
}
}
Time DelegateImpl::CurrentTime() const {
return Time::Now();
}
} // namespace
EvictedDomainCookieCounter::EvictedDomainCookieCounter(
scoped_refptr<net::CookieMonster::Delegate> next_cookie_monster_delegate)
: next_cookie_monster_delegate_(next_cookie_monster_delegate),
cookie_counter_delegate_(new DelegateImpl),
max_size_(kMaxEvictedDomainCookies),
purge_count_(kPurgeEvictedDomainCookies) {
}
EvictedDomainCookieCounter::EvictedDomainCookieCounter(
scoped_refptr<net::CookieMonster::Delegate> next_cookie_monster_delegate,
scoped_ptr<Delegate> cookie_counter_delegate,
size_t max_size,
size_t purge_count)
: next_cookie_monster_delegate_(next_cookie_monster_delegate),
cookie_counter_delegate_(cookie_counter_delegate.Pass()),
max_size_(max_size),
purge_count_(purge_count) {
DCHECK(cookie_counter_delegate_);
DCHECK_LT(purge_count, max_size_);
}
EvictedDomainCookieCounter::~EvictedDomainCookieCounter() {
STLDeleteContainerPairSecondPointers(evicted_cookies_.begin(),
evicted_cookies_.end());
}
size_t EvictedDomainCookieCounter::GetStorageSize() const {
return evicted_cookies_.size();
}
void EvictedDomainCookieCounter::OnCookieChanged(
const net::CanonicalCookie& cookie,
bool removed,
ChangeCause cause) {
EvictedDomainCookieCounter::EvictedCookieKey key(GetKey(cookie));
Time current_time(cookie_counter_delegate_->CurrentTime());
if (removed) {
if (cause == net::CookieMonster::Delegate::CHANGE_COOKIE_EVICTED)
StoreEvictedCookie(key, cookie, current_time);
} else { // Includes adds or updates.
ProcessNewCookie(key, cookie, current_time);
}
if (next_cookie_monster_delegate_.get())
next_cookie_monster_delegate_->OnCookieChanged(cookie, removed, cause);
}
void EvictedDomainCookieCounter::OnLoaded() {
if (next_cookie_monster_delegate_.get())
next_cookie_monster_delegate_->OnLoaded();
}
// static
EvictedDomainCookieCounter::EvictedCookieKey
EvictedDomainCookieCounter::GetKey(const net::CanonicalCookie& cookie) {
return cookie.Domain() + ";" + cookie.Path() + ";" + cookie.Name();
}
// static
bool EvictedDomainCookieCounter::CompareEvictedCookie(
const EvictedCookieMap::iterator evicted_cookie1,
const EvictedCookieMap::iterator evicted_cookie2) {
return evicted_cookie1->second->eviction_time
< evicted_cookie2->second->eviction_time;
}
void EvictedDomainCookieCounter::GarbageCollect(const Time& current_time) {
if (evicted_cookies_.size() <= max_size_)
return;
// From |evicted_cookies_|, removed all expired cookies, and remove cookies
// with the oldest |eviction_time| so that |size_goal| is attained.
size_t size_goal = max_size_ - purge_count_;
// Bound on number of non-expired cookies to remove.
size_t remove_quota = evicted_cookies_.size() - size_goal;
DCHECK_GT(remove_quota, 0u);
std::vector<EvictedCookieMap::iterator> remove_list;
remove_list.reserve(evicted_cookies_.size());
EvictedCookieMap::iterator it = evicted_cookies_.begin();
while (it != evicted_cookies_.end()) {
if (it->second->is_expired(current_time)) {
delete it->second;
evicted_cookies_.erase(it++); // Post-increment idiom for in-loop removal.
if (remove_quota)
--remove_quota;
} else {
if (remove_quota) // Don't bother storing if quota met.
remove_list.push_back(it);
++it;
}
}
// Free the oldest |remove_quota| non-expired cookies.
std::partial_sort(remove_list.begin(), remove_list.begin() + remove_quota,
remove_list.end(), CompareEvictedCookie);
for (size_t i = 0; i < remove_quota; ++i) {
delete remove_list[i]->second;
evicted_cookies_.erase(remove_list[i]);
}
// Apply stricter check if non-expired cookies were deleted.
DCHECK(remove_quota ? evicted_cookies_.size() == size_goal :
evicted_cookies_.size() <= size_goal);
}
void EvictedDomainCookieCounter::StoreEvictedCookie(
const EvictedCookieKey& key,
const net::CanonicalCookie& cookie,
const Time& current_time) {
bool is_google = google_util::IsGoogleHostname(
cookie.Domain(), google_util::ALLOW_SUBDOMAIN);
EvictedCookie* evicted_cookie =
new EvictedCookie(current_time, cookie.ExpiryDate(), is_google);
std::pair<EvictedCookieMap::iterator, bool> prev_entry =
evicted_cookies_.insert(
EvictedCookieMap::value_type(key, evicted_cookie));
if (!prev_entry.second) {
NOTREACHED();
delete prev_entry.first->second;
prev_entry.first->second = evicted_cookie;
}
GarbageCollect(current_time);
}
void EvictedDomainCookieCounter::ProcessNewCookie(
const EvictedCookieKey& key,
const net::CanonicalCookie& cc,
const Time& current_time) {
EvictedCookieMap::iterator it = evicted_cookies_.find(key);
if (it != evicted_cookies_.end()) {
if (!it->second->is_expired(current_time)) // Reinstatement.
cookie_counter_delegate_->Report(*it->second, current_time);
delete it->second;
evicted_cookies_.erase(it);
}
}
} // namespace chrome_browser_net
|