1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Portions of this code based on Mozilla:
6 // (netwerk/cookie/src/nsCookieService.cpp)
7 /* ***** BEGIN LICENSE BLOCK *****
8 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
10 * The contents of this file are subject to the Mozilla Public License Version
11 * 1.1 (the "License"); you may not use this file except in compliance with
12 * the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS" basis,
16 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 * for the specific language governing rights and limitations under the
20 * The Original Code is mozilla.org code.
22 * The Initial Developer of the Original Code is
23 * Netscape Communications Corporation.
24 * Portions created by the Initial Developer are Copyright (C) 2003
25 * the Initial Developer. All Rights Reserved.
28 * Daniel Witte (dwitte@stanford.edu)
29 * Michiel van Leeuwen (mvl@exedo.nl)
31 * Alternatively, the contents of this file may be used under the terms of
32 * either the GNU General Public License Version 2 or later (the "GPL"), or
33 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
34 * in which case the provisions of the GPL or the LGPL are applicable instead
35 * of those above. If you wish to allow use of your version of this file only
36 * under the terms of either the GPL or the LGPL, and not to allow others to
37 * use your version of this file under the terms of the MPL, indicate your
38 * decision by deleting the provisions above and replace them with the notice
39 * and other provisions required by the GPL or the LGPL. If you do not delete
40 * the provisions above, a recipient may use your version of this file under
41 * the terms of any one of the MPL, the GPL or the LGPL.
43 * ***** END LICENSE BLOCK ***** */
45 #include "net/cookies/cookie_monster.h"
51 #include "base/basictypes.h"
52 #include "base/bind.h"
53 #include "base/callback.h"
54 #include "base/logging.h"
55 #include "base/memory/scoped_ptr.h"
56 #include "base/message_loop/message_loop.h"
57 #include "base/message_loop/message_loop_proxy.h"
58 #include "base/metrics/histogram.h"
59 #include "base/strings/string_util.h"
60 #include "base/strings/stringprintf.h"
61 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
62 #include "net/cookies/canonical_cookie.h"
63 #include "net/cookies/cookie_util.h"
64 #include "net/cookies/parsed_cookie.h"
68 using base::TimeDelta;
69 using base::TimeTicks;
71 // In steady state, most cookie requests can be satisfied by the in memory
72 // cookie monster store. However, if a request comes in during the initial
73 // cookie load, it must be delayed until that load completes. That is done by
74 // queueing it on CookieMonster::tasks_pending_ and running it when notification
75 // of cookie load completion is received via CookieMonster::OnLoaded. This
76 // callback is passed to the persistent store from CookieMonster::InitStore(),
77 // which is called on the first operation invoked on the CookieMonster.
79 // On the browser critical paths (e.g. for loading initial web pages in a
80 // session restore) it may take too long to wait for the full load. If a cookie
81 // request is for a specific URL, DoCookieTaskForURL is called, which triggers a
82 // priority load if the key is not loaded yet by calling PersistentCookieStore
83 // :: LoadCookiesForKey. The request is queued in
84 // CookieMonster::tasks_pending_for_key_ and executed upon receiving
85 // notification of key load completion via CookieMonster::OnKeyLoaded(). If
86 // multiple requests for the same eTLD+1 are received before key load
87 // completion, only the first request calls
88 // PersistentCookieStore::LoadCookiesForKey, all subsequent requests are queued
89 // in CookieMonster::tasks_pending_for_key_ and executed upon receiving
90 // notification of key load completion triggered by the first request for the
93 static const int kMinutesInTenYears = 10 * 365 * 24 * 60;
97 // See comments at declaration of these variables in cookie_monster.h
99 const size_t CookieMonster::kDomainMaxCookies = 180;
100 const size_t CookieMonster::kDomainPurgeCookies = 30;
101 const size_t CookieMonster::kMaxCookies = 3300;
102 const size_t CookieMonster::kPurgeCookies = 300;
104 const size_t CookieMonster::kDomainCookiesQuotaLow = 30;
105 const size_t CookieMonster::kDomainCookiesQuotaMedium = 50;
106 const size_t CookieMonster::kDomainCookiesQuotaHigh =
107 kDomainMaxCookies - kDomainPurgeCookies
108 - kDomainCookiesQuotaLow - kDomainCookiesQuotaMedium;
110 const int CookieMonster::kSafeFromGlobalPurgeDays = 30;
114 bool ContainsControlCharacter(const std::string& s) {
115 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {
116 if ((*i >= 0) && (*i <= 31))
123 typedef std::vector<CanonicalCookie*> CanonicalCookieVector;
125 // Default minimum delay after updating a cookie's LastAccessDate before we
126 // will update it again.
127 const int kDefaultAccessUpdateThresholdSeconds = 60;
129 // Comparator to sort cookies from highest creation date to lowest
131 struct OrderByCreationTimeDesc {
132 bool operator()(const CookieMonster::CookieMap::iterator& a,
133 const CookieMonster::CookieMap::iterator& b) const {
134 return a->second->CreationDate() > b->second->CreationDate();
138 // Constants for use in VLOG
139 const int kVlogPerCookieMonster = 1;
140 const int kVlogPeriodic = 3;
141 const int kVlogGarbageCollection = 5;
142 const int kVlogSetCookies = 7;
143 const int kVlogGetCookies = 9;
145 // Mozilla sorts on the path length (longest first), and then it
146 // sorts by creation time (oldest first).
147 // The RFC says the sort order for the domain attribute is undefined.
148 bool CookieSorter(CanonicalCookie* cc1, CanonicalCookie* cc2) {
149 if (cc1->Path().length() == cc2->Path().length())
150 return cc1->CreationDate() < cc2->CreationDate();
151 return cc1->Path().length() > cc2->Path().length();
154 bool LRACookieSorter(const CookieMonster::CookieMap::iterator& it1,
155 const CookieMonster::CookieMap::iterator& it2) {
156 // Cookies accessed less recently should be deleted first.
157 if (it1->second->LastAccessDate() != it2->second->LastAccessDate())
158 return it1->second->LastAccessDate() < it2->second->LastAccessDate();
160 // In rare cases we might have two cookies with identical last access times.
161 // To preserve the stability of the sort, in these cases prefer to delete
162 // older cookies over newer ones. CreationDate() is guaranteed to be unique.
163 return it1->second->CreationDate() < it2->second->CreationDate();
166 // Our strategy to find duplicates is:
167 // (1) Build a map from (cookiename, cookiepath) to
168 // {list of cookies with this signature, sorted by creation time}.
169 // (2) For each list with more than 1 entry, keep the cookie having the
170 // most recent creation time, and delete the others.
172 // Two cookies are considered equivalent if they have the same domain,
174 struct CookieSignature {
176 CookieSignature(const std::string& name,
177 const std::string& domain,
178 const std::string& path)
179 : name(name), domain(domain), path(path) {
182 // To be a key for a map this class needs to be assignable, copyable,
183 // and have an operator<. The default assignment operator
184 // and copy constructor are exactly what we want.
186 bool operator<(const CookieSignature& cs) const {
187 // Name compare dominates, then domain, then path.
188 int diff = name.compare(cs.name);
192 diff = domain.compare(cs.domain);
196 return path.compare(cs.path) < 0;
204 // Determine the cookie domain to use for setting the specified cookie.
205 bool GetCookieDomain(const GURL& url,
206 const ParsedCookie& pc,
207 std::string* result) {
208 std::string domain_string;
210 domain_string = pc.Domain();
211 return cookie_util::GetCookieDomainWithString(url, domain_string, result);
214 // For a CookieItVector iterator range [|it_begin|, |it_end|),
215 // sorts the first |num_sort| + 1 elements by LastAccessDate().
216 // The + 1 element exists so for any interval of length <= |num_sort| starting
217 // from |cookies_its_begin|, a LastAccessDate() bound can be found.
218 void SortLeastRecentlyAccessed(
219 CookieMonster::CookieItVector::iterator it_begin,
220 CookieMonster::CookieItVector::iterator it_end,
222 DCHECK_LT(static_cast<int>(num_sort), it_end - it_begin);
223 std::partial_sort(it_begin, it_begin + num_sort + 1, it_end, LRACookieSorter);
226 // Predicate to support PartitionCookieByPriority().
227 struct CookiePriorityEqualsTo
228 : std::unary_function<const CookieMonster::CookieMap::iterator, bool> {
229 CookiePriorityEqualsTo(CookiePriority priority)
230 : priority_(priority) {}
232 bool operator()(const CookieMonster::CookieMap::iterator it) const {
233 return it->second->Priority() == priority_;
236 const CookiePriority priority_;
239 // For a CookieItVector iterator range [|it_begin|, |it_end|),
240 // moves all cookies with a given |priority| to the beginning of the list.
241 // Returns: An iterator in [it_begin, it_end) to the first element with
242 // priority != |priority|, or |it_end| if all have priority == |priority|.
243 CookieMonster::CookieItVector::iterator PartitionCookieByPriority(
244 CookieMonster::CookieItVector::iterator it_begin,
245 CookieMonster::CookieItVector::iterator it_end,
246 CookiePriority priority) {
247 return std::partition(it_begin, it_end, CookiePriorityEqualsTo(priority));
250 bool LowerBoundAccessDateComparator(
251 const CookieMonster::CookieMap::iterator it, const Time& access_date) {
252 return it->second->LastAccessDate() < access_date;
255 // For a CookieItVector iterator range [|it_begin|, |it_end|)
256 // from a CookieItVector sorted by LastAccessDate(), returns the
257 // first iterator with access date >= |access_date|, or cookie_its_end if this
259 CookieMonster::CookieItVector::iterator LowerBoundAccessDate(
260 const CookieMonster::CookieItVector::iterator its_begin,
261 const CookieMonster::CookieItVector::iterator its_end,
262 const Time& access_date) {
263 return std::lower_bound(its_begin, its_end, access_date,
264 LowerBoundAccessDateComparator);
267 // Mapping between DeletionCause and Delegate::ChangeCause; the mapping also
268 // provides a boolean that specifies whether or not an OnCookieChanged
269 // notification ought to be generated.
270 typedef struct ChangeCausePair_struct {
271 CookieMonster::Delegate::ChangeCause cause;
274 ChangeCausePair ChangeCauseMapping[] = {
275 // DELETE_COOKIE_EXPLICIT
276 { CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT, true },
277 // DELETE_COOKIE_OVERWRITE
278 { CookieMonster::Delegate::CHANGE_COOKIE_OVERWRITE, true },
279 // DELETE_COOKIE_EXPIRED
280 { CookieMonster::Delegate::CHANGE_COOKIE_EXPIRED, true },
281 // DELETE_COOKIE_EVICTED
282 { CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true },
283 // DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE
284 { CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT, false },
285 // DELETE_COOKIE_DONT_RECORD
286 { CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT, false },
287 // DELETE_COOKIE_EVICTED_DOMAIN
288 { CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true },
289 // DELETE_COOKIE_EVICTED_GLOBAL
290 { CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true },
291 // DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
292 { CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true },
293 // DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
294 { CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true },
295 // DELETE_COOKIE_EXPIRED_OVERWRITE
296 { CookieMonster::Delegate::CHANGE_COOKIE_EXPIRED_OVERWRITE, true },
297 // DELETE_COOKIE_CONTROL_CHAR
298 { CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true},
299 // DELETE_COOKIE_LAST_ENTRY
300 { CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT, false }
303 std::string BuildCookieLine(const CanonicalCookieVector& cookies) {
304 std::string cookie_line;
305 for (CanonicalCookieVector::const_iterator it = cookies.begin();
306 it != cookies.end(); ++it) {
307 if (it != cookies.begin())
309 // In Mozilla if you set a cookie like AAAA, it will have an empty token
310 // and a value of AAAA. When it sends the cookie back, it will send AAAA,
311 // so we need to avoid sending =AAAA for a blank token value.
312 if (!(*it)->Name().empty())
313 cookie_line += (*it)->Name() + "=";
314 cookie_line += (*it)->Value();
322 bool CookieMonster::default_enable_file_scheme_ = false;
324 CookieMonster::CookieMonster(PersistentCookieStore* store, Delegate* delegate)
325 : initialized_(false),
328 last_access_threshold_(
329 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)),
331 last_statistic_record_time_(Time::Now()),
332 keep_expired_cookies_(false),
333 persist_session_cookies_(false) {
334 InitializeHistograms();
335 SetDefaultCookieableSchemes();
338 CookieMonster::CookieMonster(PersistentCookieStore* store,
340 int last_access_threshold_milliseconds)
341 : initialized_(false),
344 last_access_threshold_(base::TimeDelta::FromMilliseconds(
345 last_access_threshold_milliseconds)),
347 last_statistic_record_time_(base::Time::Now()),
348 keep_expired_cookies_(false),
349 persist_session_cookies_(false) {
350 InitializeHistograms();
351 SetDefaultCookieableSchemes();
355 // Task classes for queueing the coming request.
357 class CookieMonster::CookieMonsterTask
358 : public base::RefCountedThreadSafe<CookieMonsterTask> {
360 // Runs the task and invokes the client callback on the thread that
361 // originally constructed the task.
362 virtual void Run() = 0;
365 explicit CookieMonsterTask(CookieMonster* cookie_monster);
366 virtual ~CookieMonsterTask();
368 // Invokes the callback immediately, if the current thread is the one
369 // that originated the task, or queues the callback for execution on the
370 // appropriate thread. Maintains a reference to this CookieMonsterTask
371 // instance until the callback completes.
372 void InvokeCallback(base::Closure callback);
374 CookieMonster* cookie_monster() {
375 return cookie_monster_;
379 friend class base::RefCountedThreadSafe<CookieMonsterTask>;
381 CookieMonster* cookie_monster_;
382 scoped_refptr<base::MessageLoopProxy> thread_;
384 DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask);
387 CookieMonster::CookieMonsterTask::CookieMonsterTask(
388 CookieMonster* cookie_monster)
389 : cookie_monster_(cookie_monster),
390 thread_(base::MessageLoopProxy::current()) {
393 CookieMonster::CookieMonsterTask::~CookieMonsterTask() {}
395 // Unfortunately, one cannot re-bind a Callback with parameters into a closure.
396 // Therefore, the closure passed to InvokeCallback is a clumsy binding of
397 // Callback::Run on a wrapped Callback instance. Since Callback is not
398 // reference counted, we bind to an instance that is a member of the
399 // CookieMonsterTask subclass. Then, we cannot simply post the callback to a
400 // message loop because the underlying instance may be destroyed (along with the
401 // CookieMonsterTask instance) in the interim. Therefore, we post a callback
402 // bound to the CookieMonsterTask, which *is* reference counted (thus preventing
403 // destruction of the original callback), and which invokes the closure (which
404 // invokes the original callback with the returned data).
405 void CookieMonster::CookieMonsterTask::InvokeCallback(base::Closure callback) {
406 if (thread_->BelongsToCurrentThread()) {
409 thread_->PostTask(FROM_HERE, base::Bind(
410 &CookieMonsterTask::InvokeCallback, this, callback));
414 // Task class for SetCookieWithDetails call.
415 class CookieMonster::SetCookieWithDetailsTask : public CookieMonsterTask {
417 SetCookieWithDetailsTask(CookieMonster* cookie_monster,
419 const std::string& name,
420 const std::string& value,
421 const std::string& domain,
422 const std::string& path,
423 const base::Time& expiration_time,
426 CookiePriority priority,
427 const SetCookiesCallback& callback)
428 : CookieMonsterTask(cookie_monster),
434 expiration_time_(expiration_time),
436 http_only_(http_only),
438 callback_(callback) {
441 // CookieMonsterTask:
442 virtual void Run() OVERRIDE;
445 virtual ~SetCookieWithDetailsTask() {}
453 base::Time expiration_time_;
456 CookiePriority priority_;
457 SetCookiesCallback callback_;
459 DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask);
462 void CookieMonster::SetCookieWithDetailsTask::Run() {
463 bool success = this->cookie_monster()->
464 SetCookieWithDetails(url_, name_, value_, domain_, path_,
465 expiration_time_, secure_, http_only_, priority_);
466 if (!callback_.is_null()) {
467 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run,
468 base::Unretained(&callback_), success));
472 // Task class for GetAllCookies call.
473 class CookieMonster::GetAllCookiesTask : public CookieMonsterTask {
475 GetAllCookiesTask(CookieMonster* cookie_monster,
476 const GetCookieListCallback& callback)
477 : CookieMonsterTask(cookie_monster),
478 callback_(callback) {
482 virtual void Run() OVERRIDE;
485 virtual ~GetAllCookiesTask() {}
488 GetCookieListCallback callback_;
490 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask);
493 void CookieMonster::GetAllCookiesTask::Run() {
494 if (!callback_.is_null()) {
495 CookieList cookies = this->cookie_monster()->GetAllCookies();
496 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run,
497 base::Unretained(&callback_), cookies));
501 // Task class for GetAllCookiesForURLWithOptions call.
502 class CookieMonster::GetAllCookiesForURLWithOptionsTask
503 : public CookieMonsterTask {
505 GetAllCookiesForURLWithOptionsTask(
506 CookieMonster* cookie_monster,
508 const CookieOptions& options,
509 const GetCookieListCallback& callback)
510 : CookieMonsterTask(cookie_monster),
513 callback_(callback) {
516 // CookieMonsterTask:
517 virtual void Run() OVERRIDE;
520 virtual ~GetAllCookiesForURLWithOptionsTask() {}
524 CookieOptions options_;
525 GetCookieListCallback callback_;
527 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesForURLWithOptionsTask);
530 void CookieMonster::GetAllCookiesForURLWithOptionsTask::Run() {
531 if (!callback_.is_null()) {
532 CookieList cookies = this->cookie_monster()->
533 GetAllCookiesForURLWithOptions(url_, options_);
534 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run,
535 base::Unretained(&callback_), cookies));
539 template <typename Result> struct CallbackType {
540 typedef base::Callback<void(Result)> Type;
543 template <> struct CallbackType<void> {
544 typedef base::Closure Type;
547 // Base task class for Delete*Task.
548 template <typename Result>
549 class CookieMonster::DeleteTask : public CookieMonsterTask {
551 DeleteTask(CookieMonster* cookie_monster,
552 const typename CallbackType<Result>::Type& callback)
553 : CookieMonsterTask(cookie_monster),
554 callback_(callback) {
557 // CookieMonsterTask:
558 virtual void Run() OVERRIDE;
561 // Runs the delete task and returns a result.
562 virtual Result RunDeleteTask() = 0;
563 base::Closure RunDeleteTaskAndBindCallback();
564 void FlushDone(const base::Closure& callback);
566 typename CallbackType<Result>::Type callback_;
568 DISALLOW_COPY_AND_ASSIGN(DeleteTask);
571 template <typename Result>
572 base::Closure CookieMonster::DeleteTask<Result>::
573 RunDeleteTaskAndBindCallback() {
574 Result result = RunDeleteTask();
575 if (callback_.is_null())
576 return base::Closure();
577 return base::Bind(callback_, result);
581 base::Closure CookieMonster::DeleteTask<void>::RunDeleteTaskAndBindCallback() {
586 template <typename Result>
587 void CookieMonster::DeleteTask<Result>::Run() {
588 this->cookie_monster()->FlushStore(
589 base::Bind(&DeleteTask<Result>::FlushDone, this,
590 RunDeleteTaskAndBindCallback()));
593 template <typename Result>
594 void CookieMonster::DeleteTask<Result>::FlushDone(
595 const base::Closure& callback) {
596 if (!callback.is_null()) {
597 this->InvokeCallback(callback);
601 // Task class for DeleteAll call.
602 class CookieMonster::DeleteAllTask : public DeleteTask<int> {
604 DeleteAllTask(CookieMonster* cookie_monster,
605 const DeleteCallback& callback)
606 : DeleteTask(cookie_monster, callback) {
610 virtual int RunDeleteTask() OVERRIDE;
613 virtual ~DeleteAllTask() {}
616 DISALLOW_COPY_AND_ASSIGN(DeleteAllTask);
619 int CookieMonster::DeleteAllTask::RunDeleteTask() {
620 return this->cookie_monster()->DeleteAll(true);
623 // Task class for DeleteAllCreatedBetween call.
624 class CookieMonster::DeleteAllCreatedBetweenTask : public DeleteTask<int> {
626 DeleteAllCreatedBetweenTask(CookieMonster* cookie_monster,
627 const Time& delete_begin,
628 const Time& delete_end,
629 const DeleteCallback& callback)
630 : DeleteTask(cookie_monster, callback),
631 delete_begin_(delete_begin),
632 delete_end_(delete_end) {
636 virtual int RunDeleteTask() OVERRIDE;
639 virtual ~DeleteAllCreatedBetweenTask() {}
645 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask);
648 int CookieMonster::DeleteAllCreatedBetweenTask::RunDeleteTask() {
649 return this->cookie_monster()->
650 DeleteAllCreatedBetween(delete_begin_, delete_end_);
653 // Task class for DeleteAllForHost call.
654 class CookieMonster::DeleteAllForHostTask : public DeleteTask<int> {
656 DeleteAllForHostTask(CookieMonster* cookie_monster,
658 const DeleteCallback& callback)
659 : DeleteTask(cookie_monster, callback),
664 virtual int RunDeleteTask() OVERRIDE;
667 virtual ~DeleteAllForHostTask() {}
672 DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask);
675 int CookieMonster::DeleteAllForHostTask::RunDeleteTask() {
676 return this->cookie_monster()->DeleteAllForHost(url_);
679 // Task class for DeleteAllCreatedBetweenForHost call.
680 class CookieMonster::DeleteAllCreatedBetweenForHostTask
681 : public DeleteTask<int> {
683 DeleteAllCreatedBetweenForHostTask(
684 CookieMonster* cookie_monster,
688 const DeleteCallback& callback)
689 : DeleteTask(cookie_monster, callback),
690 delete_begin_(delete_begin),
691 delete_end_(delete_end),
696 virtual int RunDeleteTask() OVERRIDE;
699 virtual ~DeleteAllCreatedBetweenForHostTask() {}
706 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenForHostTask);
709 int CookieMonster::DeleteAllCreatedBetweenForHostTask::RunDeleteTask() {
710 return this->cookie_monster()->DeleteAllCreatedBetweenForHost(
711 delete_begin_, delete_end_, url_);
714 // Task class for DeleteCanonicalCookie call.
715 class CookieMonster::DeleteCanonicalCookieTask : public DeleteTask<bool> {
717 DeleteCanonicalCookieTask(CookieMonster* cookie_monster,
718 const CanonicalCookie& cookie,
719 const DeleteCookieCallback& callback)
720 : DeleteTask(cookie_monster, callback),
725 virtual bool RunDeleteTask() OVERRIDE;
728 virtual ~DeleteCanonicalCookieTask() {}
731 CanonicalCookie cookie_;
733 DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask);
736 bool CookieMonster::DeleteCanonicalCookieTask::RunDeleteTask() {
737 return this->cookie_monster()->DeleteCanonicalCookie(cookie_);
740 // Task class for SetCookieWithOptions call.
741 class CookieMonster::SetCookieWithOptionsTask : public CookieMonsterTask {
743 SetCookieWithOptionsTask(CookieMonster* cookie_monster,
745 const std::string& cookie_line,
746 const CookieOptions& options,
747 const SetCookiesCallback& callback)
748 : CookieMonsterTask(cookie_monster),
750 cookie_line_(cookie_line),
752 callback_(callback) {
755 // CookieMonsterTask:
756 virtual void Run() OVERRIDE;
759 virtual ~SetCookieWithOptionsTask() {}
763 std::string cookie_line_;
764 CookieOptions options_;
765 SetCookiesCallback callback_;
767 DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask);
770 void CookieMonster::SetCookieWithOptionsTask::Run() {
771 bool result = this->cookie_monster()->
772 SetCookieWithOptions(url_, cookie_line_, options_);
773 if (!callback_.is_null()) {
774 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run,
775 base::Unretained(&callback_), result));
779 // Task class for GetCookiesWithOptions call.
780 class CookieMonster::GetCookiesWithOptionsTask : public CookieMonsterTask {
782 GetCookiesWithOptionsTask(CookieMonster* cookie_monster,
784 const CookieOptions& options,
785 const GetCookiesCallback& callback)
786 : CookieMonsterTask(cookie_monster),
789 callback_(callback) {
792 // CookieMonsterTask:
793 virtual void Run() OVERRIDE;
796 virtual ~GetCookiesWithOptionsTask() {}
800 CookieOptions options_;
801 GetCookiesCallback callback_;
803 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask);
806 void CookieMonster::GetCookiesWithOptionsTask::Run() {
807 std::string cookie = this->cookie_monster()->
808 GetCookiesWithOptions(url_, options_);
809 if (!callback_.is_null()) {
810 this->InvokeCallback(base::Bind(&GetCookiesCallback::Run,
811 base::Unretained(&callback_), cookie));
815 // Task class for DeleteCookie call.
816 class CookieMonster::DeleteCookieTask : public DeleteTask<void> {
818 DeleteCookieTask(CookieMonster* cookie_monster,
820 const std::string& cookie_name,
821 const base::Closure& callback)
822 : DeleteTask(cookie_monster, callback),
824 cookie_name_(cookie_name) {
828 virtual void RunDeleteTask() OVERRIDE;
831 virtual ~DeleteCookieTask() {}
835 std::string cookie_name_;
837 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
840 void CookieMonster::DeleteCookieTask::RunDeleteTask() {
841 this->cookie_monster()->DeleteCookie(url_, cookie_name_);
844 // Task class for DeleteSessionCookies call.
845 class CookieMonster::DeleteSessionCookiesTask : public DeleteTask<int> {
847 DeleteSessionCookiesTask(CookieMonster* cookie_monster,
848 const DeleteCallback& callback)
849 : DeleteTask(cookie_monster, callback) {
853 virtual int RunDeleteTask() OVERRIDE;
856 virtual ~DeleteSessionCookiesTask() {}
860 DISALLOW_COPY_AND_ASSIGN(DeleteSessionCookiesTask);
863 int CookieMonster::DeleteSessionCookiesTask::RunDeleteTask() {
864 return this->cookie_monster()->DeleteSessionCookies();
867 // Task class for HasCookiesForETLDP1Task call.
868 class CookieMonster::HasCookiesForETLDP1Task : public CookieMonsterTask {
870 HasCookiesForETLDP1Task(
871 CookieMonster* cookie_monster,
872 const std::string& etldp1,
873 const HasCookiesForETLDP1Callback& callback)
874 : CookieMonsterTask(cookie_monster),
876 callback_(callback) {
879 // CookieMonsterTask:
880 virtual void Run() OVERRIDE;
883 virtual ~HasCookiesForETLDP1Task() {}
887 HasCookiesForETLDP1Callback callback_;
889 DISALLOW_COPY_AND_ASSIGN(HasCookiesForETLDP1Task);
892 void CookieMonster::HasCookiesForETLDP1Task::Run() {
893 bool result = this->cookie_monster()->HasCookiesForETLDP1(etldp1_);
894 if (!callback_.is_null()) {
895 this->InvokeCallback(
896 base::Bind(&HasCookiesForETLDP1Callback::Run,
897 base::Unretained(&callback_), result));
901 // Asynchronous CookieMonster API
903 void CookieMonster::SetCookieWithDetailsAsync(
905 const std::string& name,
906 const std::string& value,
907 const std::string& domain,
908 const std::string& path,
909 const Time& expiration_time,
912 CookiePriority priority,
913 const SetCookiesCallback& callback) {
914 scoped_refptr<SetCookieWithDetailsTask> task =
915 new SetCookieWithDetailsTask(this, url, name, value, domain, path,
916 expiration_time, secure, http_only, priority,
919 DoCookieTaskForURL(task, url);
922 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) {
923 scoped_refptr<GetAllCookiesTask> task =
924 new GetAllCookiesTask(this, callback);
930 void CookieMonster::GetAllCookiesForURLWithOptionsAsync(
932 const CookieOptions& options,
933 const GetCookieListCallback& callback) {
934 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
935 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
937 DoCookieTaskForURL(task, url);
940 void CookieMonster::GetAllCookiesForURLAsync(
941 const GURL& url, const GetCookieListCallback& callback) {
942 CookieOptions options;
943 options.set_include_httponly();
944 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
945 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
947 DoCookieTaskForURL(task, url);
950 void CookieMonster::HasCookiesForETLDP1Async(
951 const std::string& etldp1,
952 const HasCookiesForETLDP1Callback& callback) {
953 scoped_refptr<HasCookiesForETLDP1Task> task =
954 new HasCookiesForETLDP1Task(this, etldp1, callback);
956 DoCookieTaskForURL(task, GURL("http://" + etldp1));
959 void CookieMonster::DeleteAllAsync(const DeleteCallback& callback) {
960 scoped_refptr<DeleteAllTask> task =
961 new DeleteAllTask(this, callback);
966 void CookieMonster::DeleteAllCreatedBetweenAsync(
967 const Time& delete_begin, const Time& delete_end,
968 const DeleteCallback& callback) {
969 scoped_refptr<DeleteAllCreatedBetweenTask> task =
970 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end,
976 void CookieMonster::DeleteAllCreatedBetweenForHostAsync(
977 const Time delete_begin,
978 const Time delete_end,
980 const DeleteCallback& callback) {
981 scoped_refptr<DeleteAllCreatedBetweenForHostTask> task =
982 new DeleteAllCreatedBetweenForHostTask(
983 this, delete_begin, delete_end, url, callback);
985 DoCookieTaskForURL(task, url);
988 void CookieMonster::DeleteAllForHostAsync(
989 const GURL& url, const DeleteCallback& callback) {
990 scoped_refptr<DeleteAllForHostTask> task =
991 new DeleteAllForHostTask(this, url, callback);
993 DoCookieTaskForURL(task, url);
996 void CookieMonster::DeleteCanonicalCookieAsync(
997 const CanonicalCookie& cookie,
998 const DeleteCookieCallback& callback) {
999 scoped_refptr<DeleteCanonicalCookieTask> task =
1000 new DeleteCanonicalCookieTask(this, cookie, callback);
1005 void CookieMonster::SetCookieWithOptionsAsync(
1007 const std::string& cookie_line,
1008 const CookieOptions& options,
1009 const SetCookiesCallback& callback) {
1010 scoped_refptr<SetCookieWithOptionsTask> task =
1011 new SetCookieWithOptionsTask(this, url, cookie_line, options, callback);
1013 DoCookieTaskForURL(task, url);
1016 void CookieMonster::GetCookiesWithOptionsAsync(
1018 const CookieOptions& options,
1019 const GetCookiesCallback& callback) {
1020 scoped_refptr<GetCookiesWithOptionsTask> task =
1021 new GetCookiesWithOptionsTask(this, url, options, callback);
1023 DoCookieTaskForURL(task, url);
1026 void CookieMonster::DeleteCookieAsync(const GURL& url,
1027 const std::string& cookie_name,
1028 const base::Closure& callback) {
1029 scoped_refptr<DeleteCookieTask> task =
1030 new DeleteCookieTask(this, url, cookie_name, callback);
1032 DoCookieTaskForURL(task, url);
1035 void CookieMonster::DeleteSessionCookiesAsync(
1036 const CookieStore::DeleteCallback& callback) {
1037 scoped_refptr<DeleteSessionCookiesTask> task =
1038 new DeleteSessionCookiesTask(this, callback);
1043 void CookieMonster::DoCookieTask(
1044 const scoped_refptr<CookieMonsterTask>& task_item) {
1046 base::AutoLock autolock(lock_);
1049 tasks_pending_.push(task_item);
1057 void CookieMonster::DoCookieTaskForURL(
1058 const scoped_refptr<CookieMonsterTask>& task_item,
1061 base::AutoLock autolock(lock_);
1063 // If cookies for the requested domain key (eTLD+1) have been loaded from DB
1064 // then run the task, otherwise load from DB.
1066 // Checks if the domain key has been loaded.
1067 std::string key(cookie_util::GetEffectiveDomain(url.scheme(),
1069 if (keys_loaded_.find(key) == keys_loaded_.end()) {
1070 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > >
1071 ::iterator it = tasks_pending_for_key_.find(key);
1072 if (it == tasks_pending_for_key_.end()) {
1073 store_->LoadCookiesForKey(key,
1074 base::Bind(&CookieMonster::OnKeyLoaded, this, key));
1075 it = tasks_pending_for_key_.insert(std::make_pair(key,
1076 std::deque<scoped_refptr<CookieMonsterTask> >())).first;
1078 it->second.push_back(task_item);
1086 bool CookieMonster::SetCookieWithDetails(const GURL& url,
1087 const std::string& name,
1088 const std::string& value,
1089 const std::string& domain,
1090 const std::string& path,
1091 const base::Time& expiration_time,
1094 CookiePriority priority) {
1095 base::AutoLock autolock(lock_);
1097 if (!HasCookieableScheme(url))
1100 Time creation_time = CurrentTime();
1101 last_time_seen_ = creation_time;
1103 scoped_ptr<CanonicalCookie> cc;
1104 cc.reset(CanonicalCookie::Create(url, name, value, domain, path,
1105 creation_time, expiration_time,
1106 secure, http_only, priority));
1111 CookieOptions options;
1112 options.set_include_httponly();
1113 return SetCanonicalCookie(&cc, creation_time, options);
1116 bool CookieMonster::InitializeFrom(const CookieList& list) {
1117 base::AutoLock autolock(lock_);
1119 for (net::CookieList::const_iterator iter = list.begin();
1120 iter != list.end(); ++iter) {
1121 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie(*iter));
1122 net::CookieOptions options;
1123 options.set_include_httponly();
1124 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), options))
1130 CookieList CookieMonster::GetAllCookies() {
1131 base::AutoLock autolock(lock_);
1133 // This function is being called to scrape the cookie list for management UI
1134 // or similar. We shouldn't show expired cookies in this list since it will
1135 // just be confusing to users, and this function is called rarely enough (and
1136 // is already slow enough) that it's OK to take the time to garbage collect
1137 // the expired cookies now.
1139 // Note that this does not prune cookies to be below our limits (if we've
1140 // exceeded them) the way that calling GarbageCollect() would.
1141 GarbageCollectExpired(Time::Now(),
1142 CookieMapItPair(cookies_.begin(), cookies_.end()),
1145 // Copy the CanonicalCookie pointers from the map so that we can use the same
1146 // sorter as elsewhere, then copy the result out.
1147 std::vector<CanonicalCookie*> cookie_ptrs;
1148 cookie_ptrs.reserve(cookies_.size());
1149 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end(); ++it)
1150 cookie_ptrs.push_back(it->second);
1151 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
1153 CookieList cookie_list;
1154 cookie_list.reserve(cookie_ptrs.size());
1155 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
1156 it != cookie_ptrs.end(); ++it)
1157 cookie_list.push_back(**it);
1162 CookieList CookieMonster::GetAllCookiesForURLWithOptions(
1164 const CookieOptions& options) {
1165 base::AutoLock autolock(lock_);
1167 std::vector<CanonicalCookie*> cookie_ptrs;
1168 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs);
1169 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
1172 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
1173 it != cookie_ptrs.end(); it++)
1174 cookies.push_back(**it);
1179 CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) {
1180 CookieOptions options;
1181 options.set_include_httponly();
1183 return GetAllCookiesForURLWithOptions(url, options);
1186 int CookieMonster::DeleteAll(bool sync_to_store) {
1187 base::AutoLock autolock(lock_);
1189 int num_deleted = 0;
1190 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1191 CookieMap::iterator curit = it;
1193 InternalDeleteCookie(curit, sync_to_store,
1194 sync_to_store ? DELETE_COOKIE_EXPLICIT :
1195 DELETE_COOKIE_DONT_RECORD /* Destruction. */);
1202 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin,
1203 const Time& delete_end) {
1204 base::AutoLock autolock(lock_);
1206 int num_deleted = 0;
1207 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1208 CookieMap::iterator curit = it;
1209 CanonicalCookie* cc = curit->second;
1212 if (cc->CreationDate() >= delete_begin &&
1213 (delete_end.is_null() || cc->CreationDate() < delete_end)) {
1214 InternalDeleteCookie(curit,
1215 true, /*sync_to_store*/
1216 DELETE_COOKIE_EXPLICIT);
1224 int CookieMonster::DeleteAllCreatedBetweenForHost(const Time delete_begin,
1225 const Time delete_end,
1227 base::AutoLock autolock(lock_);
1229 if (!HasCookieableScheme(url))
1232 const std::string host(url.host());
1234 // We store host cookies in the store by their canonical host name;
1235 // domain cookies are stored with a leading ".". So this is a pretty
1236 // simple lookup and per-cookie delete.
1237 int num_deleted = 0;
1238 for (CookieMapItPair its = cookies_.equal_range(GetKey(host));
1239 its.first != its.second;) {
1240 CookieMap::iterator curit = its.first;
1243 const CanonicalCookie* const cc = curit->second;
1245 // Delete only on a match as a host cookie.
1246 if (cc->IsHostCookie() && cc->IsDomainMatch(host) &&
1247 cc->CreationDate() >= delete_begin &&
1248 // The assumption that null |delete_end| is equivalent to
1249 // Time::Max() is confusing.
1250 (delete_end.is_null() || cc->CreationDate() < delete_end)) {
1253 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT);
1259 int CookieMonster::DeleteAllForHost(const GURL& url) {
1260 return DeleteAllCreatedBetweenForHost(Time(), Time::Max(), url);
1264 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) {
1265 base::AutoLock autolock(lock_);
1267 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain()));
1268 its.first != its.second; ++its.first) {
1269 // The creation date acts as our unique index...
1270 if (its.first->second->CreationDate() == cookie.CreationDate()) {
1271 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT);
1278 void CookieMonster::SetCookieableSchemes(const char* schemes[],
1279 size_t num_schemes) {
1280 base::AutoLock autolock(lock_);
1282 // Cookieable Schemes must be set before first use of function.
1283 DCHECK(!initialized_);
1285 cookieable_schemes_.clear();
1286 cookieable_schemes_.insert(cookieable_schemes_.end(),
1287 schemes, schemes + num_schemes);
1290 void CookieMonster::SetEnableFileScheme(bool accept) {
1291 // This assumes "file" is always at the end of the array. See the comment
1292 // above kDefaultCookieableSchemes.
1293 int num_schemes = accept ? kDefaultCookieableSchemesCount :
1294 kDefaultCookieableSchemesCount - 1;
1295 SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes);
1298 void CookieMonster::SetKeepExpiredCookies() {
1299 keep_expired_cookies_ = true;
1303 void CookieMonster::EnableFileScheme() {
1304 default_enable_file_scheme_ = true;
1307 void CookieMonster::FlushStore(const base::Closure& callback) {
1308 base::AutoLock autolock(lock_);
1309 if (initialized_ && store_.get())
1310 store_->Flush(callback);
1311 else if (!callback.is_null())
1312 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
1315 bool CookieMonster::SetCookieWithOptions(const GURL& url,
1316 const std::string& cookie_line,
1317 const CookieOptions& options) {
1318 base::AutoLock autolock(lock_);
1320 if (!HasCookieableScheme(url)) {
1324 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options);
1327 std::string CookieMonster::GetCookiesWithOptions(const GURL& url,
1328 const CookieOptions& options) {
1329 base::AutoLock autolock(lock_);
1331 if (!HasCookieableScheme(url))
1332 return std::string();
1334 TimeTicks start_time(TimeTicks::Now());
1336 std::vector<CanonicalCookie*> cookies;
1337 FindCookiesForHostAndDomain(url, options, true, &cookies);
1338 std::sort(cookies.begin(), cookies.end(), CookieSorter);
1340 std::string cookie_line = BuildCookieLine(cookies);
1342 histogram_time_get_->AddTime(TimeTicks::Now() - start_time);
1344 VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line;
1349 void CookieMonster::DeleteCookie(const GURL& url,
1350 const std::string& cookie_name) {
1351 base::AutoLock autolock(lock_);
1353 if (!HasCookieableScheme(url))
1356 CookieOptions options;
1357 options.set_include_httponly();
1358 // Get the cookies for this host and its domain(s).
1359 std::vector<CanonicalCookie*> cookies;
1360 FindCookiesForHostAndDomain(url, options, true, &cookies);
1361 std::set<CanonicalCookie*> matching_cookies;
1363 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin();
1364 it != cookies.end(); ++it) {
1365 if ((*it)->Name() != cookie_name)
1367 if (url.path().find((*it)->Path()))
1369 matching_cookies.insert(*it);
1372 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1373 CookieMap::iterator curit = it;
1375 if (matching_cookies.find(curit->second) != matching_cookies.end()) {
1376 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT);
1381 int CookieMonster::DeleteSessionCookies() {
1382 base::AutoLock autolock(lock_);
1384 int num_deleted = 0;
1385 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1386 CookieMap::iterator curit = it;
1387 CanonicalCookie* cc = curit->second;
1390 if (!cc->IsPersistent()) {
1391 InternalDeleteCookie(curit,
1392 true, /*sync_to_store*/
1393 DELETE_COOKIE_EXPIRED);
1401 bool CookieMonster::HasCookiesForETLDP1(const std::string& etldp1) {
1402 base::AutoLock autolock(lock_);
1404 const std::string key(GetKey(etldp1));
1406 CookieMapItPair its = cookies_.equal_range(key);
1407 return its.first != its.second;
1410 CookieMonster* CookieMonster::GetCookieMonster() {
1414 // This function must be called before the CookieMonster is used.
1415 void CookieMonster::SetPersistSessionCookies(bool persist_session_cookies) {
1416 DCHECK(!initialized_);
1417 persist_session_cookies_ = persist_session_cookies;
1420 void CookieMonster::SetForceKeepSessionState() {
1422 store_->SetForceKeepSessionState();
1426 CookieMonster::~CookieMonster() {
1430 bool CookieMonster::SetCookieWithCreationTime(const GURL& url,
1431 const std::string& cookie_line,
1432 const base::Time& creation_time) {
1433 DCHECK(!store_.get()) << "This method is only to be used by unit-tests.";
1434 base::AutoLock autolock(lock_);
1436 if (!HasCookieableScheme(url)) {
1441 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time,
1445 void CookieMonster::InitStore() {
1446 DCHECK(store_.get()) << "Store must exist to initialize";
1448 // We bind in the current time so that we can report the wall-clock time for
1450 store_->Load(base::Bind(&CookieMonster::OnLoaded, this, TimeTicks::Now()));
1453 void CookieMonster::OnLoaded(TimeTicks beginning_time,
1454 const std::vector<CanonicalCookie*>& cookies) {
1455 StoreLoadedCookies(cookies);
1456 histogram_time_blocked_on_load_->AddTime(TimeTicks::Now() - beginning_time);
1458 // Invoke the task queue of cookie request.
1462 void CookieMonster::OnKeyLoaded(const std::string& key,
1463 const std::vector<CanonicalCookie*>& cookies) {
1464 // This function does its own separate locking.
1465 StoreLoadedCookies(cookies);
1467 std::deque<scoped_refptr<CookieMonsterTask> > tasks_pending_for_key;
1469 // We need to do this repeatedly until no more tasks were added to the queue
1470 // during the period where we release the lock.
1473 base::AutoLock autolock(lock_);
1474 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > >
1475 ::iterator it = tasks_pending_for_key_.find(key);
1476 if (it == tasks_pending_for_key_.end()) {
1477 keys_loaded_.insert(key);
1480 if (it->second.empty()) {
1481 keys_loaded_.insert(key);
1482 tasks_pending_for_key_.erase(it);
1485 it->second.swap(tasks_pending_for_key);
1488 while (!tasks_pending_for_key.empty()) {
1489 scoped_refptr<CookieMonsterTask> task = tasks_pending_for_key.front();
1491 tasks_pending_for_key.pop_front();
1496 void CookieMonster::StoreLoadedCookies(
1497 const std::vector<CanonicalCookie*>& cookies) {
1498 // Initialize the store and sync in any saved persistent cookies. We don't
1499 // care if it's expired, insert it so it can be garbage collected, removed,
1501 base::AutoLock autolock(lock_);
1503 CookieItVector cookies_with_control_chars;
1505 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin();
1506 it != cookies.end(); ++it) {
1507 int64 cookie_creation_time = (*it)->CreationDate().ToInternalValue();
1509 if (creation_times_.insert(cookie_creation_time).second) {
1510 CookieMap::iterator inserted =
1511 InternalInsertCookie(GetKey((*it)->Domain()), *it, false);
1512 const Time cookie_access_time((*it)->LastAccessDate());
1513 if (earliest_access_time_.is_null() ||
1514 cookie_access_time < earliest_access_time_)
1515 earliest_access_time_ = cookie_access_time;
1517 if (ContainsControlCharacter((*it)->Name()) ||
1518 ContainsControlCharacter((*it)->Value())) {
1519 cookies_with_control_chars.push_back(inserted);
1522 LOG(ERROR) << base::StringPrintf("Found cookies with duplicate creation "
1523 "times in backing store: "
1524 "{name='%s', domain='%s', path='%s'}",
1525 (*it)->Name().c_str(),
1526 (*it)->Domain().c_str(),
1527 (*it)->Path().c_str());
1528 // We've been given ownership of the cookie and are throwing it
1529 // away; reclaim the space.
1534 // Any cookies that contain control characters that we have loaded from the
1535 // persistent store should be deleted. See http://crbug.com/238041.
1536 for (CookieItVector::iterator it = cookies_with_control_chars.begin();
1537 it != cookies_with_control_chars.end();) {
1538 CookieItVector::iterator curit = it;
1541 InternalDeleteCookie(*curit, true, DELETE_COOKIE_CONTROL_CHAR);
1544 // After importing cookies from the PersistentCookieStore, verify that
1545 // none of our other constraints are violated.
1546 // In particular, the backing store might have given us duplicate cookies.
1548 // This method could be called multiple times due to priority loading, thus
1549 // cookies loaded in previous runs will be validated again, but this is OK
1550 // since they are expected to be much fewer than total DB.
1551 EnsureCookiesMapIsValid();
1554 void CookieMonster::InvokeQueue() {
1556 scoped_refptr<CookieMonsterTask> request_task;
1558 base::AutoLock autolock(lock_);
1559 if (tasks_pending_.empty()) {
1561 creation_times_.clear();
1562 keys_loaded_.clear();
1565 request_task = tasks_pending_.front();
1566 tasks_pending_.pop();
1568 request_task->Run();
1572 void CookieMonster::EnsureCookiesMapIsValid() {
1573 lock_.AssertAcquired();
1575 int num_duplicates_trimmed = 0;
1577 // Iterate through all the of the cookies, grouped by host.
1578 CookieMap::iterator prev_range_end = cookies_.begin();
1579 while (prev_range_end != cookies_.end()) {
1580 CookieMap::iterator cur_range_begin = prev_range_end;
1581 const std::string key = cur_range_begin->first; // Keep a copy.
1582 CookieMap::iterator cur_range_end = cookies_.upper_bound(key);
1583 prev_range_end = cur_range_end;
1585 // Ensure no equivalent cookies for this host.
1586 num_duplicates_trimmed +=
1587 TrimDuplicateCookiesForKey(key, cur_range_begin, cur_range_end);
1590 // Record how many duplicates were found in the database.
1591 // See InitializeHistograms() for details.
1592 histogram_cookie_deletion_cause_->Add(num_duplicates_trimmed);
1595 int CookieMonster::TrimDuplicateCookiesForKey(
1596 const std::string& key,
1597 CookieMap::iterator begin,
1598 CookieMap::iterator end) {
1599 lock_.AssertAcquired();
1601 // Set of cookies ordered by creation time.
1602 typedef std::set<CookieMap::iterator, OrderByCreationTimeDesc> CookieSet;
1604 // Helper map we populate to find the duplicates.
1605 typedef std::map<CookieSignature, CookieSet> EquivalenceMap;
1606 EquivalenceMap equivalent_cookies;
1608 // The number of duplicate cookies that have been found.
1609 int num_duplicates = 0;
1611 // Iterate through all of the cookies in our range, and insert them into
1612 // the equivalence map.
1613 for (CookieMap::iterator it = begin; it != end; ++it) {
1614 DCHECK_EQ(key, it->first);
1615 CanonicalCookie* cookie = it->second;
1617 CookieSignature signature(cookie->Name(), cookie->Domain(),
1619 CookieSet& set = equivalent_cookies[signature];
1621 // We found a duplicate!
1625 // We save the iterator into |cookies_| rather than the actual cookie
1626 // pointer, since we may need to delete it later.
1627 bool insert_success = set.insert(it).second;
1628 DCHECK(insert_success) <<
1629 "Duplicate creation times found in duplicate cookie name scan.";
1632 // If there were no duplicates, we are done!
1633 if (num_duplicates == 0)
1636 // Make sure we find everything below that we did above.
1637 int num_duplicates_found = 0;
1639 // Otherwise, delete all the duplicate cookies, both from our in-memory store
1640 // and from the backing store.
1641 for (EquivalenceMap::iterator it = equivalent_cookies.begin();
1642 it != equivalent_cookies.end();
1644 const CookieSignature& signature = it->first;
1645 CookieSet& dupes = it->second;
1647 if (dupes.size() <= 1)
1648 continue; // This cookiename/path has no duplicates.
1649 num_duplicates_found += dupes.size() - 1;
1651 // Since |dups| is sorted by creation time (descending), the first cookie
1652 // is the most recent one, so we will keep it. The rest are duplicates.
1653 dupes.erase(dupes.begin());
1655 LOG(ERROR) << base::StringPrintf(
1656 "Found %d duplicate cookies for host='%s', "
1657 "with {name='%s', domain='%s', path='%s'}",
1658 static_cast<int>(dupes.size()),
1660 signature.name.c_str(),
1661 signature.domain.c_str(),
1662 signature.path.c_str());
1664 // Remove all the cookies identified by |dupes|. It is valid to delete our
1665 // list of iterators one at a time, since |cookies_| is a multimap (they
1666 // don't invalidate existing iterators following deletion).
1667 for (CookieSet::iterator dupes_it = dupes.begin();
1668 dupes_it != dupes.end();
1670 InternalDeleteCookie(*dupes_it, true,
1671 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE);
1674 DCHECK_EQ(num_duplicates, num_duplicates_found);
1676 return num_duplicates;
1679 // Note: file must be the last scheme.
1680 const char* CookieMonster::kDefaultCookieableSchemes[] =
1681 { "http", "https", "file" };
1682 const int CookieMonster::kDefaultCookieableSchemesCount =
1683 arraysize(kDefaultCookieableSchemes);
1685 void CookieMonster::SetDefaultCookieableSchemes() {
1686 int num_schemes = default_enable_file_scheme_ ?
1687 kDefaultCookieableSchemesCount : kDefaultCookieableSchemesCount - 1;
1688 SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes);
1691 void CookieMonster::FindCookiesForHostAndDomain(
1693 const CookieOptions& options,
1694 bool update_access_time,
1695 std::vector<CanonicalCookie*>* cookies) {
1696 lock_.AssertAcquired();
1698 const Time current_time(CurrentTime());
1700 // Probe to save statistics relatively frequently. We do it here rather
1701 // than in the set path as many websites won't set cookies, and we
1702 // want to collect statistics whenever the browser's being used.
1703 RecordPeriodicStats(current_time);
1705 // Can just dispatch to FindCookiesForKey
1706 const std::string key(GetKey(url.host()));
1707 FindCookiesForKey(key, url, options, current_time,
1708 update_access_time, cookies);
1711 void CookieMonster::FindCookiesForKey(const std::string& key,
1713 const CookieOptions& options,
1714 const Time& current,
1715 bool update_access_time,
1716 std::vector<CanonicalCookie*>* cookies) {
1717 lock_.AssertAcquired();
1719 for (CookieMapItPair its = cookies_.equal_range(key);
1720 its.first != its.second; ) {
1721 CookieMap::iterator curit = its.first;
1722 CanonicalCookie* cc = curit->second;
1725 // If the cookie is expired, delete it.
1726 if (cc->IsExpired(current) && !keep_expired_cookies_) {
1727 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED);
1731 // Filter out cookies that should not be included for a request to the
1732 // given |url|. HTTP only cookies are filtered depending on the passed
1733 // cookie |options|.
1734 if (!cc->IncludeForRequestURL(url, options))
1737 // Add this cookie to the set of matching cookies. Update the access
1738 // time if we've been requested to do so.
1739 if (update_access_time) {
1740 InternalUpdateCookieAccessTime(cc, current);
1742 cookies->push_back(cc);
1746 bool CookieMonster::DeleteAnyEquivalentCookie(const std::string& key,
1747 const CanonicalCookie& ecc,
1749 bool already_expired) {
1750 lock_.AssertAcquired();
1752 bool found_equivalent_cookie = false;
1753 bool skipped_httponly = false;
1754 for (CookieMapItPair its = cookies_.equal_range(key);
1755 its.first != its.second; ) {
1756 CookieMap::iterator curit = its.first;
1757 CanonicalCookie* cc = curit->second;
1760 if (ecc.IsEquivalent(*cc)) {
1761 // We should never have more than one equivalent cookie, since they should
1762 // overwrite each other.
1763 CHECK(!found_equivalent_cookie) <<
1764 "Duplicate equivalent cookies found, cookie store is corrupted.";
1765 if (skip_httponly && cc->IsHttpOnly()) {
1766 skipped_httponly = true;
1768 InternalDeleteCookie(curit, true, already_expired ?
1769 DELETE_COOKIE_EXPIRED_OVERWRITE : DELETE_COOKIE_OVERWRITE);
1771 found_equivalent_cookie = true;
1774 return skipped_httponly;
1777 CookieMonster::CookieMap::iterator CookieMonster::InternalInsertCookie(
1778 const std::string& key,
1779 CanonicalCookie* cc,
1780 bool sync_to_store) {
1781 lock_.AssertAcquired();
1783 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() &&
1785 store_->AddCookie(*cc);
1786 CookieMap::iterator inserted =
1787 cookies_.insert(CookieMap::value_type(key, cc));
1788 if (delegate_.get()) {
1789 delegate_->OnCookieChanged(
1790 *cc, false, Delegate::CHANGE_COOKIE_EXPLICIT);
1796 bool CookieMonster::SetCookieWithCreationTimeAndOptions(
1798 const std::string& cookie_line,
1799 const Time& creation_time_or_null,
1800 const CookieOptions& options) {
1801 lock_.AssertAcquired();
1803 VLOG(kVlogSetCookies) << "SetCookie() line: " << cookie_line;
1805 Time creation_time = creation_time_or_null;
1806 if (creation_time.is_null()) {
1807 creation_time = CurrentTime();
1808 last_time_seen_ = creation_time;
1811 scoped_ptr<CanonicalCookie> cc(
1812 CanonicalCookie::Create(url, cookie_line, creation_time, options));
1815 VLOG(kVlogSetCookies) << "WARNING: Failed to allocate CanonicalCookie";
1818 return SetCanonicalCookie(&cc, creation_time, options);
1821 bool CookieMonster::SetCanonicalCookie(scoped_ptr<CanonicalCookie>* cc,
1822 const Time& creation_time,
1823 const CookieOptions& options) {
1824 const std::string key(GetKey((*cc)->Domain()));
1825 bool already_expired = (*cc)->IsExpired(creation_time);
1826 if (DeleteAnyEquivalentCookie(key, **cc, options.exclude_httponly(),
1828 VLOG(kVlogSetCookies) << "SetCookie() not clobbering httponly cookie";
1832 VLOG(kVlogSetCookies) << "SetCookie() key: " << key << " cc: "
1833 << (*cc)->DebugString();
1835 // Realize that we might be setting an expired cookie, and the only point
1836 // was to delete the cookie which we've already done.
1837 if (!already_expired || keep_expired_cookies_) {
1838 // See InitializeHistograms() for details.
1839 if ((*cc)->IsPersistent()) {
1840 histogram_expiration_duration_minutes_->Add(
1841 ((*cc)->ExpiryDate() - creation_time).InMinutes());
1844 InternalInsertCookie(key, cc->release(), true);
1846 VLOG(kVlogSetCookies) << "SetCookie() not storing already expired cookie.";
1849 // We assume that hopefully setting a cookie will be less common than
1850 // querying a cookie. Since setting a cookie can put us over our limits,
1851 // make sure that we garbage collect... We can also make the assumption that
1852 // if a cookie was set, in the common case it will be used soon after,
1853 // and we will purge the expired cookies in GetCookies().
1854 GarbageCollect(creation_time, key);
1859 void CookieMonster::InternalUpdateCookieAccessTime(CanonicalCookie* cc,
1860 const Time& current) {
1861 lock_.AssertAcquired();
1863 // Based off the Mozilla code. When a cookie has been accessed recently,
1864 // don't bother updating its access time again. This reduces the number of
1865 // updates we do during pageload, which in turn reduces the chance our storage
1866 // backend will hit its batch thresholds and be forced to update.
1867 if ((current - cc->LastAccessDate()) < last_access_threshold_)
1870 // See InitializeHistograms() for details.
1871 histogram_between_access_interval_minutes_->Add(
1872 (current - cc->LastAccessDate()).InMinutes());
1874 cc->SetLastAccessDate(current);
1875 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get())
1876 store_->UpdateCookieAccessTime(*cc);
1879 // InternalDeleteCookies must not invalidate iterators other than the one being
1881 void CookieMonster::InternalDeleteCookie(CookieMap::iterator it,
1883 DeletionCause deletion_cause) {
1884 lock_.AssertAcquired();
1886 // Ideally, this would be asserted up where we define ChangeCauseMapping,
1887 // but DeletionCause's visibility (or lack thereof) forces us to make
1889 COMPILE_ASSERT(arraysize(ChangeCauseMapping) == DELETE_COOKIE_LAST_ENTRY + 1,
1890 ChangeCauseMapping_size_not_eq_DeletionCause_enum_size);
1892 // See InitializeHistograms() for details.
1893 if (deletion_cause != DELETE_COOKIE_DONT_RECORD)
1894 histogram_cookie_deletion_cause_->Add(deletion_cause);
1896 CanonicalCookie* cc = it->second;
1897 VLOG(kVlogSetCookies) << "InternalDeleteCookie() cc: " << cc->DebugString();
1899 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() &&
1901 store_->DeleteCookie(*cc);
1902 if (delegate_.get()) {
1903 ChangeCausePair mapping = ChangeCauseMapping[deletion_cause];
1906 delegate_->OnCookieChanged(*cc, true, mapping.cause);
1912 // Domain expiry behavior is unchanged by key/expiry scheme (the
1913 // meaning of the key is different, but that's not visible to this routine).
1914 int CookieMonster::GarbageCollect(const Time& current,
1915 const std::string& key) {
1916 lock_.AssertAcquired();
1918 int num_deleted = 0;
1920 Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays));
1922 // Collect garbage for this key, minding cookie priorities.
1923 if (cookies_.count(key) > kDomainMaxCookies) {
1924 VLOG(kVlogGarbageCollection) << "GarbageCollect() key: " << key;
1926 CookieItVector cookie_its;
1927 num_deleted += GarbageCollectExpired(
1928 current, cookies_.equal_range(key), &cookie_its);
1929 if (cookie_its.size() > kDomainMaxCookies) {
1930 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect domain.";
1932 cookie_its.size() - (kDomainMaxCookies - kDomainPurgeCookies);
1933 DCHECK(purge_goal > kDomainPurgeCookies);
1935 // Boundary iterators into |cookie_its| for different priorities.
1936 CookieItVector::iterator it_bdd[4];
1937 // Intialize |it_bdd| while sorting |cookie_its| by priorities.
1938 // Schematic: [MLLHMHHLMM] => [LLL|MMMM|HHH], with 4 boundaries.
1939 it_bdd[0] = cookie_its.begin();
1940 it_bdd[3] = cookie_its.end();
1941 it_bdd[1] = PartitionCookieByPriority(it_bdd[0], it_bdd[3],
1942 COOKIE_PRIORITY_LOW);
1943 it_bdd[2] = PartitionCookieByPriority(it_bdd[1], it_bdd[3],
1944 COOKIE_PRIORITY_MEDIUM);
1946 kDomainCookiesQuotaLow,
1947 kDomainCookiesQuotaMedium,
1948 kDomainCookiesQuotaHigh
1951 // Purge domain cookies in 3 rounds.
1952 // Round 1: consider low-priority cookies only: evict least-recently
1953 // accessed, while protecting quota[0] of these from deletion.
1954 // Round 2: consider {low, medium}-priority cookies, evict least-recently
1955 // accessed, while protecting quota[0] + quota[1].
1956 // Round 3: consider all cookies, evict least-recently accessed.
1957 size_t accumulated_quota = 0;
1958 CookieItVector::iterator it_purge_begin = it_bdd[0];
1959 for (int i = 0; i < 3 && purge_goal > 0; ++i) {
1960 accumulated_quota += quota[i];
1962 size_t num_considered = it_bdd[i + 1] - it_purge_begin;
1963 if (num_considered <= accumulated_quota)
1966 // Number of cookies that will be purged in this round.
1968 std::min(purge_goal, num_considered - accumulated_quota);
1969 purge_goal -= round_goal;
1971 SortLeastRecentlyAccessed(it_purge_begin, it_bdd[i + 1], round_goal);
1972 // Cookies accessed on or after |safe_date| would have been safe from
1973 // global purge, and we want to keep track of this.
1974 CookieItVector::iterator it_purge_end = it_purge_begin + round_goal;
1975 CookieItVector::iterator it_purge_middle =
1976 LowerBoundAccessDate(it_purge_begin, it_purge_end, safe_date);
1977 // Delete cookies accessed before |safe_date|.
1978 num_deleted += GarbageCollectDeleteRange(
1980 DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE,
1983 // Delete cookies accessed on or after |safe_date|.
1984 num_deleted += GarbageCollectDeleteRange(
1986 DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE,
1989 it_purge_begin = it_purge_end;
1991 DCHECK_EQ(0U, purge_goal);
1995 // Collect garbage for everything. With firefox style we want to preserve
1996 // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict.
1997 if (cookies_.size() > kMaxCookies &&
1998 earliest_access_time_ < safe_date) {
1999 VLOG(kVlogGarbageCollection) << "GarbageCollect() everything";
2000 CookieItVector cookie_its;
2001 num_deleted += GarbageCollectExpired(
2002 current, CookieMapItPair(cookies_.begin(), cookies_.end()),
2004 if (cookie_its.size() > kMaxCookies) {
2005 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect everything.";
2006 size_t purge_goal = cookie_its.size() - (kMaxCookies - kPurgeCookies);
2007 DCHECK(purge_goal > kPurgeCookies);
2008 // Sorts up to *and including* |cookie_its[purge_goal]|, so
2009 // |earliest_access_time| will be properly assigned even if
2010 // |global_purge_it| == |cookie_its.begin() + purge_goal|.
2011 SortLeastRecentlyAccessed(cookie_its.begin(), cookie_its.end(),
2013 // Find boundary to cookies older than safe_date.
2014 CookieItVector::iterator global_purge_it =
2015 LowerBoundAccessDate(cookie_its.begin(),
2016 cookie_its.begin() + purge_goal,
2018 // Only delete the old cookies.
2019 num_deleted += GarbageCollectDeleteRange(
2021 DELETE_COOKIE_EVICTED_GLOBAL,
2024 // Set access day to the oldest cookie that wasn't deleted.
2025 earliest_access_time_ = (*global_purge_it)->second->LastAccessDate();
2032 int CookieMonster::GarbageCollectExpired(
2033 const Time& current,
2034 const CookieMapItPair& itpair,
2035 CookieItVector* cookie_its) {
2036 if (keep_expired_cookies_)
2039 lock_.AssertAcquired();
2041 int num_deleted = 0;
2042 for (CookieMap::iterator it = itpair.first, end = itpair.second; it != end;) {
2043 CookieMap::iterator curit = it;
2046 if (curit->second->IsExpired(current)) {
2047 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED);
2049 } else if (cookie_its) {
2050 cookie_its->push_back(curit);
2057 int CookieMonster::GarbageCollectDeleteRange(
2058 const Time& current,
2059 DeletionCause cause,
2060 CookieItVector::iterator it_begin,
2061 CookieItVector::iterator it_end) {
2062 for (CookieItVector::iterator it = it_begin; it != it_end; it++) {
2063 histogram_evicted_last_access_minutes_->Add(
2064 (current - (*it)->second->LastAccessDate()).InMinutes());
2065 InternalDeleteCookie((*it), true, cause);
2067 return it_end - it_begin;
2070 // A wrapper around registry_controlled_domains::GetDomainAndRegistry
2071 // to make clear we're creating a key for our local map. Here and
2072 // in FindCookiesForHostAndDomain() are the only two places where
2073 // we need to conditionalize based on key type.
2075 // Note that this key algorithm explicitly ignores the scheme. This is
2076 // because when we're entering cookies into the map from the backing store,
2077 // we in general won't have the scheme at that point.
2078 // In practical terms, this means that file cookies will be stored
2079 // in the map either by an empty string or by UNC name (and will be
2080 // limited by kMaxCookiesPerHost), and extension cookies will be stored
2081 // based on the single extension id, as the extension id won't have the
2082 // form of a DNS host and hence GetKey() will return it unchanged.
2084 // Arguably the right thing to do here is to make the key
2085 // algorithm dependent on the scheme, and make sure that the scheme is
2086 // available everywhere the key must be obtained (specfically at backing
2087 // store load time). This would require either changing the backing store
2088 // database schema to include the scheme (far more trouble than it's worth), or
2089 // separating out file cookies into their own CookieMonster instance and
2090 // thus restricting each scheme to a single cookie monster (which might
2091 // be worth it, but is still too much trouble to solve what is currently a
2093 std::string CookieMonster::GetKey(const std::string& domain) const {
2094 std::string effective_domain(
2095 registry_controlled_domains::GetDomainAndRegistry(
2096 domain, registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES));
2097 if (effective_domain.empty())
2098 effective_domain = domain;
2100 if (!effective_domain.empty() && effective_domain[0] == '.')
2101 return effective_domain.substr(1);
2102 return effective_domain;
2105 bool CookieMonster::IsCookieableScheme(const std::string& scheme) {
2106 base::AutoLock autolock(lock_);
2108 return std::find(cookieable_schemes_.begin(), cookieable_schemes_.end(),
2109 scheme) != cookieable_schemes_.end();
2112 bool CookieMonster::HasCookieableScheme(const GURL& url) {
2113 lock_.AssertAcquired();
2115 // Make sure the request is on a cookie-able url scheme.
2116 for (size_t i = 0; i < cookieable_schemes_.size(); ++i) {
2117 // We matched a scheme.
2118 if (url.SchemeIs(cookieable_schemes_[i].c_str())) {
2119 // We've matched a supported scheme.
2124 // The scheme didn't match any in our whitelist.
2125 VLOG(kVlogPerCookieMonster) << "WARNING: Unsupported cookie scheme: "
2130 // Test to see if stats should be recorded, and record them if so.
2131 // The goal here is to get sampling for the average browser-hour of
2132 // activity. We won't take samples when the web isn't being surfed,
2133 // and when the web is being surfed, we'll take samples about every
2134 // kRecordStatisticsIntervalSeconds.
2135 // last_statistic_record_time_ is initialized to Now() rather than null
2136 // in the constructor so that we won't take statistics right after
2137 // startup, to avoid bias from browsers that are started but not used.
2138 void CookieMonster::RecordPeriodicStats(const base::Time& current_time) {
2139 const base::TimeDelta kRecordStatisticsIntervalTime(
2140 base::TimeDelta::FromSeconds(kRecordStatisticsIntervalSeconds));
2142 // If we've taken statistics recently, return.
2143 if (current_time - last_statistic_record_time_ <=
2144 kRecordStatisticsIntervalTime) {
2148 // See InitializeHistograms() for details.
2149 histogram_count_->Add(cookies_.size());
2151 // More detailed statistics on cookie counts at different granularities.
2152 TimeTicks beginning_of_time(TimeTicks::Now());
2154 for (CookieMap::const_iterator it_key = cookies_.begin();
2155 it_key != cookies_.end(); ) {
2156 const std::string& key(it_key->first);
2159 typedef std::map<std::string, unsigned int> DomainMap;
2160 DomainMap domain_map;
2161 CookieMapItPair its_cookies = cookies_.equal_range(key);
2162 while (its_cookies.first != its_cookies.second) {
2164 const std::string& cookie_domain(its_cookies.first->second->Domain());
2165 domain_map[cookie_domain]++;
2167 its_cookies.first++;
2169 histogram_etldp1_count_->Add(key_count);
2170 histogram_domain_per_etldp1_count_->Add(domain_map.size());
2171 for (DomainMap::const_iterator domain_map_it = domain_map.begin();
2172 domain_map_it != domain_map.end(); domain_map_it++)
2173 histogram_domain_count_->Add(domain_map_it->second);
2175 it_key = its_cookies.second;
2179 << "Time for recording cookie stats (us): "
2180 << (TimeTicks::Now() - beginning_of_time).InMicroseconds();
2182 last_statistic_record_time_ = current_time;
2185 // Initialize all histogram counter variables used in this class.
2187 // Normal histogram usage involves using the macros defined in
2188 // histogram.h, which automatically takes care of declaring these
2189 // variables (as statics), initializing them, and accumulating into
2190 // them, all from a single entry point. Unfortunately, that solution
2191 // doesn't work for the CookieMonster, as it's vulnerable to races between
2192 // separate threads executing the same functions and hence initializing the
2193 // same static variables. There isn't a race danger in the histogram
2194 // accumulation calls; they are written to be resilient to simultaneous
2195 // calls from multiple threads.
2197 // The solution taken here is to have per-CookieMonster instance
2198 // variables that are constructed during CookieMonster construction.
2199 // Note that these variables refer to the same underlying histogram,
2200 // so we still race (but safely) with other CookieMonster instances
2201 // for accumulation.
2203 // To do this we've expanded out the individual histogram macros calls,
2204 // with declarations of the variables in the class decl, initialization here
2205 // (done from the class constructor) and direct calls to the accumulation
2206 // methods where needed. The specific histogram macro calls on which the
2207 // initialization is based are included in comments below.
2208 void CookieMonster::InitializeHistograms() {
2209 // From UMA_HISTOGRAM_CUSTOM_COUNTS
2210 histogram_expiration_duration_minutes_ = base::Histogram::FactoryGet(
2211 "Cookie.ExpirationDurationMinutes",
2212 1, kMinutesInTenYears, 50,
2213 base::Histogram::kUmaTargetedHistogramFlag);
2214 histogram_between_access_interval_minutes_ = base::Histogram::FactoryGet(
2215 "Cookie.BetweenAccessIntervalMinutes",
2216 1, kMinutesInTenYears, 50,
2217 base::Histogram::kUmaTargetedHistogramFlag);
2218 histogram_evicted_last_access_minutes_ = base::Histogram::FactoryGet(
2219 "Cookie.EvictedLastAccessMinutes",
2220 1, kMinutesInTenYears, 50,
2221 base::Histogram::kUmaTargetedHistogramFlag);
2222 histogram_count_ = base::Histogram::FactoryGet(
2223 "Cookie.Count", 1, 4000, 50,
2224 base::Histogram::kUmaTargetedHistogramFlag);
2225 histogram_domain_count_ = base::Histogram::FactoryGet(
2226 "Cookie.DomainCount", 1, 4000, 50,
2227 base::Histogram::kUmaTargetedHistogramFlag);
2228 histogram_etldp1_count_ = base::Histogram::FactoryGet(
2229 "Cookie.Etldp1Count", 1, 4000, 50,
2230 base::Histogram::kUmaTargetedHistogramFlag);
2231 histogram_domain_per_etldp1_count_ = base::Histogram::FactoryGet(
2232 "Cookie.DomainPerEtldp1Count", 1, 4000, 50,
2233 base::Histogram::kUmaTargetedHistogramFlag);
2235 // From UMA_HISTOGRAM_COUNTS_10000 & UMA_HISTOGRAM_CUSTOM_COUNTS
2236 histogram_number_duplicate_db_cookies_ = base::Histogram::FactoryGet(
2237 "Net.NumDuplicateCookiesInDb", 1, 10000, 50,
2238 base::Histogram::kUmaTargetedHistogramFlag);
2240 // From UMA_HISTOGRAM_ENUMERATION
2241 histogram_cookie_deletion_cause_ = base::LinearHistogram::FactoryGet(
2242 "Cookie.DeletionCause", 1,
2243 DELETE_COOKIE_LAST_ENTRY - 1, DELETE_COOKIE_LAST_ENTRY,
2244 base::Histogram::kUmaTargetedHistogramFlag);
2246 // From UMA_HISTOGRAM_{CUSTOM_,}TIMES
2247 histogram_time_get_ = base::Histogram::FactoryTimeGet("Cookie.TimeGet",
2248 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
2249 50, base::Histogram::kUmaTargetedHistogramFlag);
2250 histogram_time_blocked_on_load_ = base::Histogram::FactoryTimeGet(
2251 "Cookie.TimeBlockedOnLoad",
2252 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
2253 50, base::Histogram::kUmaTargetedHistogramFlag);
2257 // The system resolution is not high enough, so we can have multiple
2258 // set cookies that result in the same system time. When this happens, we
2259 // increment by one Time unit. Let's hope computers don't get too fast.
2260 Time CookieMonster::CurrentTime() {
2261 return std::max(Time::Now(),
2262 Time::FromInternalValue(last_time_seen_.ToInternalValue() + 1));