Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Unified Diff: net/cookies/evicted_domain_cookie_counter.cc

Issue 12805010: Implementing EvictedDomainCookieCounter to keep user metrics on cookie eviction and reinstatement. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleanups and comment fixes. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/cookies/evicted_domain_cookie_counter.cc
diff --git a/net/cookies/evicted_domain_cookie_counter.cc b/net/cookies/evicted_domain_cookie_counter.cc
new file mode 100644
index 0000000000000000000000000000000000000000..01126a68df96fc8830c887e0635c6908c2e92dc3
--- /dev/null
+++ b/net/cookies/evicted_domain_cookie_counter.cc
@@ -0,0 +1,226 @@
+// 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 "net/cookies/evicted_domain_cookie_counter.h"
+
+#include <queue>
+#include <vector>
+
+#include "base/metrics/histogram.h"
+#include "base/stl_util.h"
+#include "base/string_util.h"
+#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
+#include "net/cookies/canonical_cookie.h"
+
+namespace 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.
+ virtual void Report(const EvictedDomainCookieCounter::EvictedCookie& ec,
+ const Time& reinstatement_time) OVERRIDE;
+ virtual Time CurrentTime() OVERRIDE;
+};
+
+DelegateImpl::DelegateImpl() {}
+
+void DelegateImpl::Report(const EvictedDomainCookieCounter::EvictedCookie& ec,
+ const Time& reinstatement_time) {
+ TimeDelta reinstatement_delay(reinstatement_time - ec.eviction_time_);
+ // Need to duplicate HISTOGRAM_CUSTOM_TIMES(), since it is a macro that
+ // defines a static variable.
+ if (ec.is_google_) {
+ HISTOGRAM_CUSTOM_TIMES("Cookie.ReinstatedCookiesGoogle",
+ reinstatement_delay,
+ TimeDelta::FromSeconds(1),
+ TimeDelta::FromDays(7),
+ 50);
+ } else {
+ HISTOGRAM_CUSTOM_TIMES("Cookie.ReinstatedCookiesOther",
+ reinstatement_delay,
+ TimeDelta::FromSeconds(1),
+ TimeDelta::FromDays(7),
+ 50);
+ }
+}
+
+Time DelegateImpl::CurrentTime() {
+ return Time::Now();
+}
+
+} // namespace
+
+EvictedDomainCookieCounter::EvictedDomainCookieCounter(
+ scoped_refptr<CookieMonster::Delegate> next_cookie_monster_delegate)
+ : next_cookie_monster_delegate_(next_cookie_monster_delegate),
+ delegate_(new DelegateImpl),
+ max_size_(kMaxEvictedDomainCookies),
+ purge_count_(kPurgeEvictedDomainCookies) {
+}
+
+EvictedDomainCookieCounter::EvictedDomainCookieCounter(
+ scoped_refptr<CookieMonster::Delegate> next_cookie_monster_delegate,
+ scoped_ptr<Delegate> delegate,
+ size_t max_size,
+ size_t purge_count)
+ : next_cookie_monster_delegate_(next_cookie_monster_delegate),
+ delegate_(delegate.Pass()),
+ max_size_(max_size),
+ purge_count_(purge_count) {
+ DCHECK(delegate_);
+ DCHECK(purge_count < max_size_);
+}
+
+EvictedDomainCookieCounter::~EvictedDomainCookieCounter() {
+ STLDeleteContainerPairSecondPointers(evicted_cookies_.begin(),
+ evicted_cookies_.end());
+}
+
+// static
+// Simplified from google_util.cc: IsGoogleHostname().
+bool EvictedDomainCookieCounter::UnsafeIsGoogleCookie(
+ const CanonicalCookie& cc) {
+ const std::string& host(cc.Domain());
+ size_t tld_length =
+ RegistryControlledDomainService::GetRegistryLength(host, false);
+ if ((tld_length == 0) || (tld_length == std::string::npos))
+ return false;
+
+ std::string host_minus_tld(host, 0, host.length() - tld_length);
+ return LowerCaseEqualsASCII(host_minus_tld, "google.") ||
+ EndsWith(host_minus_tld, ".google.", false);
+}
+
+size_t EvictedDomainCookieCounter::GetStorageSize() const {
+ return evicted_cookies_.size();
+}
+
+void EvictedDomainCookieCounter::OnCookieChanged(const CanonicalCookie& cc,
+ bool removed,
+ ChangeCause cause) {
+ EvictedDomainCookieCounter::EvictedCookieKey key(GetKey(cc));
+ Time current(delegate_->CurrentTime());
+ if (removed) {
+ if (cause == CookieMonster::Delegate::CHANGE_COOKIE_EVICTED)
+ StoreEvictedCookie(key, cc, current);
+ else // Explicit deletion, so stop tracking.
+ FreeEvictedCookie(key);
erikwright (departed) 2013/03/26 16:38:13 On the surface, it would seem like this should nev
huangs 2013/03/27 00:40:26 Removed. I was being paranoid by assuming some sem
+ } else { // Includes adds or updates.
+ ProcessNewCookie(key, cc, current);
+ }
+
+ if (next_cookie_monster_delegate_)
+ next_cookie_monster_delegate_->OnCookieChanged(cc, removed, cause);
+}
+
+// static
+EvictedDomainCookieCounter::EvictedCookieKey
+ EvictedDomainCookieCounter::GetKey(const CanonicalCookie& cc) {
+ return cc.Domain() + ";" + cc.Path() + ";" + cc.Name();
+}
+
+void EvictedDomainCookieCounter::FreeEvictedCookie(
+ const EvictedCookieKey& key) {
+ EvictedCookieMap::iterator it = evicted_cookies_.find(key);
+ if (it != evicted_cookies_.end()) {
+ delete it->second;
+ evicted_cookies_.erase(it);
+ }
+}
+
+// |current| is passed by value, in case it came from a to-be-deleted element.
erikwright (departed) 2013/03/26 16:38:13 Since this is a private method and you know it doe
huangs 2013/03/27 00:40:26 Makes sense. Changed to const base::Time. Also,
+void EvictedDomainCookieCounter::FreeExpiredEvictedCookies(Time current) {
+ EvictedCookieMap::iterator it = evicted_cookies_.begin();
+ while (it != evicted_cookies_.end()) {
+ if (it->second->is_expired(current)) {
+ delete it->second;
+ evicted_cookies_.erase(it++); // Post-increment idiom.
+ } else {
+ ++it;
+ }
+ }
+}
+
+void EvictedDomainCookieCounter::GarbageCollect(const Time& current) {
+ if (evicted_cookies_.size() <= max_size_)
+ return;
+
+ // From |evicted_cookies_|, removed all expired cookies, and remove cookies
+ // with the oldest |eviction_time_| until |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(remove_quota > 0);
+
+ typedef EvictedDomainCookieCounter::EvictedCookie* EvictedCookiePtr;
+ // Comparator for a priority_queue<> whose top() element specifies the most
+ // recently evicted cookie.
+ struct EvictedCookieComparator {
+ bool operator() (EvictedCookiePtr ec1, EvictedCookiePtr ec2) {
+ return ec1->eviction_time_ < ec2->eviction_time_;
+ }
+ };
+
+ // Use priority queue to store oldest cookies, resize as necessary.
+ std::priority_queue<EvictedCookiePtr, std::vector<EvictedCookiePtr>,
+ EvictedCookieComparator> oldest_set;
+ for (EvictedCookieMap::iterator it = evicted_cookies_.begin();
+ it != evicted_cookies_.end(); ++it) {
+ if (it->second->is_expired(current)) // Will be removed, so decrease quota.
+ remove_quota = remove_quota ? remove_quota - 1 : 0; // Cap to 0 (size_t).
+ else
+ oldest_set.push(it->second);
+
+ if (oldest_set.size() > remove_quota)
+ oldest_set.pop(); // Spare the youngest element from deletion.
erikwright (departed) 2013/03/26 16:38:13 Alternatively, I suppose you could reverse the com
huangs 2013/03/27 00:40:26 The old method saves more memory, but it turns out
huangs 2013/03/27 00:40:26 Simulation: import math def lg(n): return math.l
huangs 2013/03/27 00:40:26 Instead of using priority_queue<>, may as well use
+ }
+ DCHECK(oldest_set.size() <= remove_quota);
+
+ // Mark oldest non-expired cookies as expired, for removal.
+ for (; !oldest_set.empty(); oldest_set.pop())
+ oldest_set.top()->set_expired();
erikwright (departed) 2013/03/26 16:38:13 Here (combined with FreeExpiredEvictedCookies) we
huangs 2013/03/27 00:40:26 Cool. Doing this, except I'm now using vector<T>!
+
+ FreeExpiredEvictedCookies(current);
+ // 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 CanonicalCookie& cc,
+ const Time& current) {
+ if (!cc.IsExpired(current)) {
erikwright (departed) 2013/03/26 16:38:13 This should also only happen if the cause is CHANG
huangs 2013/03/27 00:40:26 Removed.
+ FreeEvictedCookie(key); // Deallocate explicitly.
erikwright (departed) 2013/03/26 16:38:13 std::map::insert returns a pair of iterator/bool.
huangs 2013/03/27 00:40:26 Ah, neat. :) Done.
+
+ EvictedCookie* ec =
+ new EvictedCookie(current, cc.ExpiryDate(), UnsafeIsGoogleCookie(cc));
+ evicted_cookies_.insert(EvictedCookieMap::value_type(key, ec));
+
+ GarbageCollect(current);
+ }
+}
+
+void EvictedDomainCookieCounter::ProcessNewCookie(const EvictedCookieKey& key,
+ const CanonicalCookie& cc,
+ const Time& current) {
+ EvictedCookieMap::iterator it = evicted_cookies_.find(key);
+ if (it != evicted_cookies_.end()) {
+ if (!it->second->is_expired(current))
+ delegate_->Report(*it->second, current); // Reinstatement.
+ delete it->second;
+ evicted_cookies_.erase(it);
+ }
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698