CookieMonster: Add histograms for total keys, keys with too many cookies

This CL adds two histograms to CookieMonster.

The first records the number of keys (eTLD+1's) which have undergone
eviction due to reaching the per-registrable-domain cookie limit.

The second records the number of keys (eTLD+1's) present in the store
which have at least 1 cookie (which may or may not be expired).

These are both logged periodically, once every 10 minutes of active
browsing time.

Bug: 1087156
Change-Id: I710e3bf6b16033fa1dea3401a7a43e0cf9ff2b5b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2219157
Commit-Queue: Lily Chen <[email protected]>
Reviewed-by: Maksim Orlovich <[email protected]>
Reviewed-by: Charlie Harrison <[email protected]>
Cr-Commit-Position: refs/heads/master@{#773709}
diff --git a/net/cookies/cookie_monster.h b/net/cookies/cookie_monster.h
index 2299054..0e4fa9e20 100644
--- a/net/cookies/cookie_monster.h
+++ b/net/cookies/cookie_monster.h
@@ -124,6 +124,9 @@
   static const size_t kMaxCookies;
   static const size_t kPurgeCookies;
 
+  // Max number of keys to store for domains that have been purged.
+  static const size_t kMaxDomainPurgedKeys;
+
   // Quota for cookies with {low, medium, high} priorities within a domain.
   static const size_t kDomainCookiesQuotaLow;
   static const size_t kDomainCookiesQuotaMedium;
@@ -204,6 +207,10 @@
   // before the CookieMap typedef.
   static std::string GetKey(base::StringPiece domain);
 
+  // Triggers immediate recording of stats that are typically reported
+  // periodically.
+  bool DoRecordPeriodicStatsForTesting() { return DoRecordPeriodicStats(); }
+
  private:
   // For garbage collection constants.
   FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection);
@@ -563,7 +570,11 @@
   // statistics if a sufficient time period has passed.
   void RecordPeriodicStats(const base::Time& current_time);
 
-  // Initialize the above variables; should only be called from
+  // Records the aforementioned stats if we have already finished loading all
+  // cookies. Returns whether stats were recorded.
+  bool DoRecordPeriodicStats();
+
+  // Initialize the histogram_* variables below; should only be called from
   // the constructor.
   void InitializeHistograms();
 
@@ -589,6 +600,15 @@
   base::HistogramBase* histogram_cookie_source_scheme_;
   base::HistogramBase* histogram_time_blocked_on_load_;
 
+  // Set of keys (eTLD+1's) for which non-expired cookies have
+  // been evicted for hitting the per-domain max. The size of this set is
+  // histogrammed periodically. The size is limited to |kMaxDomainPurgedKeys|.
+  std::set<std::string> domain_purged_keys_;
+
+  // The number of distinct keys (eTLD+1's) currently present in the |cookies_|
+  // multimap. This is histogrammed periodically.
+  size_t num_keys_;
+
   CookieMap cookies_;
 
   CookieMonsterChangeDispatcher change_dispatcher_;