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/profiler/scoped_tracker.h"
60 #include "base/strings/string_util.h"
61 #include "base/strings/stringprintf.h"
62 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
63 #include "net/cookies/canonical_cookie.h"
64 #include "net/cookies/cookie_util.h"
65 #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
- kDomainCookiesQuotaLow
-
108 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 // Compare cookies using name, domain and path, so that "equivalent" cookies
167 // (per RFC 2965) are equal to each other.
168 bool PartialDiffCookieSorter(const CanonicalCookie
& a
,
169 const CanonicalCookie
& b
) {
170 return a
.PartialCompare(b
);
173 // This is a stricter ordering than PartialDiffCookieOrdering, where all fields
175 bool FullDiffCookieSorter(const CanonicalCookie
& a
, const CanonicalCookie
& b
) {
176 return a
.FullCompare(b
);
179 // Our strategy to find duplicates is:
180 // (1) Build a map from (cookiename, cookiepath) to
181 // {list of cookies with this signature, sorted by creation time}.
182 // (2) For each list with more than 1 entry, keep the cookie having the
183 // most recent creation time, and delete the others.
185 // Two cookies are considered equivalent if they have the same domain,
187 struct CookieSignature
{
189 CookieSignature(const std::string
& name
,
190 const std::string
& domain
,
191 const std::string
& path
)
192 : name(name
), domain(domain
), path(path
) {}
194 // To be a key for a map this class needs to be assignable, copyable,
195 // and have an operator<. The default assignment operator
196 // and copy constructor are exactly what we want.
198 bool operator<(const CookieSignature
& cs
) const {
199 // Name compare dominates, then domain, then path.
200 int diff
= name
.compare(cs
.name
);
204 diff
= domain
.compare(cs
.domain
);
208 return path
.compare(cs
.path
) < 0;
216 // For a CookieItVector iterator range [|it_begin|, |it_end|),
217 // sorts the first |num_sort| + 1 elements by LastAccessDate().
218 // The + 1 element exists so for any interval of length <= |num_sort| starting
219 // from |cookies_its_begin|, a LastAccessDate() bound can be found.
220 void SortLeastRecentlyAccessed(CookieMonster::CookieItVector::iterator it_begin
,
221 CookieMonster::CookieItVector::iterator it_end
,
223 DCHECK_LT(static_cast<int>(num_sort
), it_end
- it_begin
);
224 std::partial_sort(it_begin
, it_begin
+ num_sort
+ 1, it_end
, LRACookieSorter
);
227 // Predicate to support PartitionCookieByPriority().
228 struct CookiePriorityEqualsTo
229 : std::unary_function
<const CookieMonster::CookieMap::iterator
, bool> {
230 explicit CookiePriorityEqualsTo(CookiePriority priority
)
231 : priority_(priority
) {}
233 bool operator()(const CookieMonster::CookieMap::iterator it
) const {
234 return it
->second
->Priority() == priority_
;
237 const CookiePriority priority_
;
240 // For a CookieItVector iterator range [|it_begin|, |it_end|),
241 // moves all cookies with a given |priority| to the beginning of the list.
242 // Returns: An iterator in [it_begin, it_end) to the first element with
243 // priority != |priority|, or |it_end| if all have priority == |priority|.
244 CookieMonster::CookieItVector::iterator
PartitionCookieByPriority(
245 CookieMonster::CookieItVector::iterator it_begin
,
246 CookieMonster::CookieItVector::iterator it_end
,
247 CookiePriority priority
) {
248 return std::partition(it_begin
, it_end
, CookiePriorityEqualsTo(priority
));
251 bool LowerBoundAccessDateComparator(const CookieMonster::CookieMap::iterator it
,
252 const Time
& access_date
) {
253 return it
->second
->LastAccessDate() < access_date
;
256 // For a CookieItVector iterator range [|it_begin|, |it_end|)
257 // from a CookieItVector sorted by LastAccessDate(), returns the
258 // first iterator with access date >= |access_date|, or cookie_its_end if this
260 CookieMonster::CookieItVector::iterator
LowerBoundAccessDate(
261 const CookieMonster::CookieItVector::iterator its_begin
,
262 const CookieMonster::CookieItVector::iterator its_end
,
263 const Time
& access_date
) {
264 return std::lower_bound(its_begin
, its_end
, access_date
,
265 LowerBoundAccessDateComparator
);
268 // Mapping between DeletionCause and CookieMonsterDelegate::ChangeCause; the
269 // mapping also provides a boolean that specifies whether or not an
270 // OnCookieChanged notification ought to be generated.
271 typedef struct ChangeCausePair_struct
{
272 CookieMonsterDelegate::ChangeCause cause
;
275 ChangeCausePair ChangeCauseMapping
[] = {
276 // DELETE_COOKIE_EXPLICIT
277 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
, true},
278 // DELETE_COOKIE_OVERWRITE
279 {CookieMonsterDelegate::CHANGE_COOKIE_OVERWRITE
, true},
280 // DELETE_COOKIE_EXPIRED
281 {CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED
, true},
282 // DELETE_COOKIE_EVICTED
283 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
284 // DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE
285 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
, false},
286 // DELETE_COOKIE_DONT_RECORD
287 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
, false},
288 // DELETE_COOKIE_EVICTED_DOMAIN
289 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
290 // DELETE_COOKIE_EVICTED_GLOBAL
291 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
292 // DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
293 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
294 // DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
295 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
296 // DELETE_COOKIE_EXPIRED_OVERWRITE
297 {CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED_OVERWRITE
, true},
298 // DELETE_COOKIE_CONTROL_CHAR
299 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
300 // DELETE_COOKIE_LAST_ENTRY
301 {CookieMonsterDelegate::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();
319 void RunAsync(scoped_refptr
<base::TaskRunner
> proxy
,
320 const CookieStore::CookieChangedCallback
& callback
,
321 const CanonicalCookie
& cookie
,
323 proxy
->PostTask(FROM_HERE
, base::Bind(callback
, cookie
, removed
));
328 CookieMonster::CookieMonster(PersistentCookieStore
* store
,
329 CookieMonsterDelegate
* delegate
)
330 : initialized_(false),
333 last_access_threshold_(
334 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds
)),
336 last_statistic_record_time_(Time::Now()),
337 keep_expired_cookies_(false),
338 persist_session_cookies_(false) {
339 InitializeHistograms();
340 SetDefaultCookieableSchemes();
343 CookieMonster::CookieMonster(PersistentCookieStore
* store
,
344 CookieMonsterDelegate
* delegate
,
345 int last_access_threshold_milliseconds
)
346 : initialized_(false),
349 last_access_threshold_(base::TimeDelta::FromMilliseconds(
350 last_access_threshold_milliseconds
)),
352 last_statistic_record_time_(base::Time::Now()),
353 keep_expired_cookies_(false),
354 persist_session_cookies_(false) {
355 InitializeHistograms();
356 SetDefaultCookieableSchemes();
359 // Task classes for queueing the coming request.
361 class CookieMonster::CookieMonsterTask
362 : public base::RefCountedThreadSafe
<CookieMonsterTask
> {
364 // Runs the task and invokes the client callback on the thread that
365 // originally constructed the task.
366 virtual void Run() = 0;
369 explicit CookieMonsterTask(CookieMonster
* cookie_monster
);
370 virtual ~CookieMonsterTask();
372 // Invokes the callback immediately, if the current thread is the one
373 // that originated the task, or queues the callback for execution on the
374 // appropriate thread. Maintains a reference to this CookieMonsterTask
375 // instance until the callback completes.
376 void InvokeCallback(base::Closure callback
);
378 CookieMonster
* cookie_monster() { return cookie_monster_
; }
381 friend class base::RefCountedThreadSafe
<CookieMonsterTask
>;
383 CookieMonster
* cookie_monster_
;
384 scoped_refptr
<base::MessageLoopProxy
> thread_
;
386 DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask
);
389 CookieMonster::CookieMonsterTask::CookieMonsterTask(
390 CookieMonster
* cookie_monster
)
391 : cookie_monster_(cookie_monster
),
392 thread_(base::MessageLoopProxy::current()) {
395 CookieMonster::CookieMonsterTask::~CookieMonsterTask() {
398 // Unfortunately, one cannot re-bind a Callback with parameters into a closure.
399 // Therefore, the closure passed to InvokeCallback is a clumsy binding of
400 // Callback::Run on a wrapped Callback instance. Since Callback is not
401 // reference counted, we bind to an instance that is a member of the
402 // CookieMonsterTask subclass. Then, we cannot simply post the callback to a
403 // message loop because the underlying instance may be destroyed (along with the
404 // CookieMonsterTask instance) in the interim. Therefore, we post a callback
405 // bound to the CookieMonsterTask, which *is* reference counted (thus preventing
406 // destruction of the original callback), and which invokes the closure (which
407 // invokes the original callback with the returned data).
408 void CookieMonster::CookieMonsterTask::InvokeCallback(base::Closure callback
) {
409 if (thread_
->BelongsToCurrentThread()) {
412 thread_
->PostTask(FROM_HERE
, base::Bind(&CookieMonsterTask::InvokeCallback
,
417 // Task class for SetCookieWithDetails call.
418 class CookieMonster::SetCookieWithDetailsTask
: public CookieMonsterTask
{
420 SetCookieWithDetailsTask(CookieMonster
* cookie_monster
,
422 const std::string
& name
,
423 const std::string
& value
,
424 const std::string
& domain
,
425 const std::string
& path
,
426 const base::Time
& expiration_time
,
429 bool first_party_only
,
430 CookiePriority priority
,
431 const SetCookiesCallback
& callback
)
432 : CookieMonsterTask(cookie_monster
),
438 expiration_time_(expiration_time
),
440 http_only_(http_only
),
441 first_party_only_(first_party_only
),
443 callback_(callback
) {}
445 // CookieMonsterTask:
449 ~SetCookieWithDetailsTask() override
{}
457 base::Time expiration_time_
;
460 bool first_party_only_
;
461 CookiePriority priority_
;
462 SetCookiesCallback callback_
;
464 DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask
);
467 void CookieMonster::SetCookieWithDetailsTask::Run() {
468 bool success
= this->cookie_monster()->SetCookieWithDetails(
469 url_
, name_
, value_
, domain_
, path_
, expiration_time_
, secure_
,
470 http_only_
, first_party_only_
, priority_
);
471 if (!callback_
.is_null()) {
472 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run
,
473 base::Unretained(&callback_
), success
));
477 // Task class for GetAllCookies call.
478 class CookieMonster::GetAllCookiesTask
: public CookieMonsterTask
{
480 GetAllCookiesTask(CookieMonster
* cookie_monster
,
481 const GetCookieListCallback
& callback
)
482 : CookieMonsterTask(cookie_monster
), callback_(callback
) {}
488 ~GetAllCookiesTask() override
{}
491 GetCookieListCallback callback_
;
493 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask
);
496 void CookieMonster::GetAllCookiesTask::Run() {
497 if (!callback_
.is_null()) {
498 CookieList cookies
= this->cookie_monster()->GetAllCookies();
499 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run
,
500 base::Unretained(&callback_
), cookies
));
504 // Task class for GetAllCookiesForURLWithOptions call.
505 class CookieMonster::GetAllCookiesForURLWithOptionsTask
506 : public CookieMonsterTask
{
508 GetAllCookiesForURLWithOptionsTask(CookieMonster
* cookie_monster
,
510 const CookieOptions
& options
,
511 const GetCookieListCallback
& callback
)
512 : CookieMonsterTask(cookie_monster
),
515 callback_(callback
) {}
517 // CookieMonsterTask:
521 ~GetAllCookiesForURLWithOptionsTask() override
{}
525 CookieOptions options_
;
526 GetCookieListCallback callback_
;
528 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesForURLWithOptionsTask
);
531 void CookieMonster::GetAllCookiesForURLWithOptionsTask::Run() {
532 if (!callback_
.is_null()) {
534 this->cookie_monster()->GetAllCookiesForURLWithOptions(url_
, options_
);
535 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run
,
536 base::Unretained(&callback_
), cookies
));
540 template <typename Result
>
541 struct CallbackType
{
542 typedef base::Callback
<void(Result
)> Type
;
546 struct CallbackType
<void> {
547 typedef base::Closure Type
;
550 // Base task class for Delete*Task.
551 template <typename Result
>
552 class CookieMonster::DeleteTask
: public CookieMonsterTask
{
554 DeleteTask(CookieMonster
* cookie_monster
,
555 const typename CallbackType
<Result
>::Type
& callback
)
556 : CookieMonsterTask(cookie_monster
), callback_(callback
) {}
558 // CookieMonsterTask:
562 ~DeleteTask() override
;
565 // Runs the delete task and returns a result.
566 virtual Result
RunDeleteTask() = 0;
567 base::Closure
RunDeleteTaskAndBindCallback();
568 void FlushDone(const base::Closure
& callback
);
570 typename CallbackType
<Result
>::Type callback_
;
572 DISALLOW_COPY_AND_ASSIGN(DeleteTask
);
575 template <typename Result
>
576 CookieMonster::DeleteTask
<Result
>::~DeleteTask() {
579 template <typename Result
>
581 CookieMonster::DeleteTask
<Result
>::RunDeleteTaskAndBindCallback() {
582 Result result
= RunDeleteTask();
583 if (callback_
.is_null())
584 return base::Closure();
585 return base::Bind(callback_
, result
);
589 base::Closure
CookieMonster::DeleteTask
<void>::RunDeleteTaskAndBindCallback() {
594 template <typename Result
>
595 void CookieMonster::DeleteTask
<Result
>::Run() {
596 this->cookie_monster()->FlushStore(base::Bind(
597 &DeleteTask
<Result
>::FlushDone
, this, RunDeleteTaskAndBindCallback()));
600 template <typename Result
>
601 void CookieMonster::DeleteTask
<Result
>::FlushDone(
602 const base::Closure
& callback
) {
603 if (!callback
.is_null()) {
604 this->InvokeCallback(callback
);
608 // Task class for DeleteAll call.
609 class CookieMonster::DeleteAllTask
: public DeleteTask
<int> {
611 DeleteAllTask(CookieMonster
* cookie_monster
, const DeleteCallback
& callback
)
612 : DeleteTask
<int>(cookie_monster
, callback
) {}
615 int RunDeleteTask() override
;
618 ~DeleteAllTask() override
{}
621 DISALLOW_COPY_AND_ASSIGN(DeleteAllTask
);
624 int CookieMonster::DeleteAllTask::RunDeleteTask() {
625 return this->cookie_monster()->DeleteAll(true);
628 // Task class for DeleteAllCreatedBetween call.
629 class CookieMonster::DeleteAllCreatedBetweenTask
: public DeleteTask
<int> {
631 DeleteAllCreatedBetweenTask(CookieMonster
* cookie_monster
,
632 const Time
& delete_begin
,
633 const Time
& delete_end
,
634 const DeleteCallback
& callback
)
635 : DeleteTask
<int>(cookie_monster
, callback
),
636 delete_begin_(delete_begin
),
637 delete_end_(delete_end
) {}
640 int RunDeleteTask() override
;
643 ~DeleteAllCreatedBetweenTask() override
{}
649 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask
);
652 int CookieMonster::DeleteAllCreatedBetweenTask::RunDeleteTask() {
653 return this->cookie_monster()->DeleteAllCreatedBetween(delete_begin_
,
657 // Task class for DeleteAllForHost call.
658 class CookieMonster::DeleteAllForHostTask
: public DeleteTask
<int> {
660 DeleteAllForHostTask(CookieMonster
* cookie_monster
,
662 const DeleteCallback
& callback
)
663 : DeleteTask
<int>(cookie_monster
, callback
), url_(url
) {}
666 int RunDeleteTask() override
;
669 ~DeleteAllForHostTask() override
{}
674 DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask
);
677 int CookieMonster::DeleteAllForHostTask::RunDeleteTask() {
678 return this->cookie_monster()->DeleteAllForHost(url_
);
681 // Task class for DeleteAllCreatedBetweenForHost call.
682 class CookieMonster::DeleteAllCreatedBetweenForHostTask
683 : public DeleteTask
<int> {
685 DeleteAllCreatedBetweenForHostTask(CookieMonster
* cookie_monster
,
689 const DeleteCallback
& callback
)
690 : DeleteTask
<int>(cookie_monster
, callback
),
691 delete_begin_(delete_begin
),
692 delete_end_(delete_end
),
696 int RunDeleteTask() override
;
699 ~DeleteAllCreatedBetweenForHostTask() override
{}
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
<bool>(cookie_monster
, callback
), cookie_(cookie
) {}
723 bool RunDeleteTask() override
;
726 ~DeleteCanonicalCookieTask() override
{}
729 CanonicalCookie cookie_
;
731 DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask
);
734 bool CookieMonster::DeleteCanonicalCookieTask::RunDeleteTask() {
735 return this->cookie_monster()->DeleteCanonicalCookie(cookie_
);
738 // Task class for SetCookieWithOptions call.
739 class CookieMonster::SetCookieWithOptionsTask
: public CookieMonsterTask
{
741 SetCookieWithOptionsTask(CookieMonster
* cookie_monster
,
743 const std::string
& cookie_line
,
744 const CookieOptions
& options
,
745 const SetCookiesCallback
& callback
)
746 : CookieMonsterTask(cookie_monster
),
748 cookie_line_(cookie_line
),
750 callback_(callback
) {}
752 // CookieMonsterTask:
756 ~SetCookieWithOptionsTask() override
{}
760 std::string cookie_line_
;
761 CookieOptions options_
;
762 SetCookiesCallback callback_
;
764 DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask
);
767 void CookieMonster::SetCookieWithOptionsTask::Run() {
768 bool result
= this->cookie_monster()->SetCookieWithOptions(url_
, cookie_line_
,
770 if (!callback_
.is_null()) {
771 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run
,
772 base::Unretained(&callback_
), result
));
776 // Task class for SetAllCookies call.
777 class CookieMonster::SetAllCookiesTask
: public CookieMonsterTask
{
779 SetAllCookiesTask(CookieMonster
* cookie_monster
,
780 const CookieList
& list
,
781 const SetCookiesCallback
& callback
)
782 : CookieMonsterTask(cookie_monster
), list_(list
), callback_(callback
) {}
784 // CookieMonsterTask:
788 ~SetAllCookiesTask() override
{}
792 SetCookiesCallback callback_
;
794 DISALLOW_COPY_AND_ASSIGN(SetAllCookiesTask
);
797 void CookieMonster::SetAllCookiesTask::Run() {
798 CookieList positive_diff
;
799 CookieList negative_diff
;
800 CookieList old_cookies
= this->cookie_monster()->GetAllCookies();
801 this->cookie_monster()->ComputeCookieDiff(&old_cookies
, &list_
,
802 &positive_diff
, &negative_diff
);
804 for (CookieList::const_iterator it
= negative_diff
.begin();
805 it
!= negative_diff
.end(); ++it
) {
806 this->cookie_monster()->DeleteCanonicalCookie(*it
);
810 if (positive_diff
.size() > 0)
811 result
= this->cookie_monster()->SetCanonicalCookies(list_
);
813 if (!callback_
.is_null()) {
814 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run
,
815 base::Unretained(&callback_
), result
));
819 // Task class for GetCookiesWithOptions call.
820 class CookieMonster::GetCookiesWithOptionsTask
: public CookieMonsterTask
{
822 GetCookiesWithOptionsTask(CookieMonster
* cookie_monster
,
824 const CookieOptions
& options
,
825 const GetCookiesCallback
& callback
)
826 : CookieMonsterTask(cookie_monster
),
829 callback_(callback
) {}
831 // CookieMonsterTask:
835 ~GetCookiesWithOptionsTask() override
{}
839 CookieOptions options_
;
840 GetCookiesCallback callback_
;
842 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask
);
845 void CookieMonster::GetCookiesWithOptionsTask::Run() {
846 // TODO(mkwst): Remove ScopedTracker below once crbug.com/456373 is fixed.
847 tracked_objects::ScopedTracker
tracking_profile(
848 FROM_HERE_WITH_EXPLICIT_FUNCTION(
849 "456373 CookieMonster::GetCookiesWithOptionsTask::Run"));
851 this->cookie_monster()->GetCookiesWithOptions(url_
, options_
);
852 if (!callback_
.is_null()) {
853 this->InvokeCallback(base::Bind(&GetCookiesCallback::Run
,
854 base::Unretained(&callback_
), cookie
));
858 // Task class for DeleteCookie call.
859 class CookieMonster::DeleteCookieTask
: public DeleteTask
<void> {
861 DeleteCookieTask(CookieMonster
* cookie_monster
,
863 const std::string
& cookie_name
,
864 const base::Closure
& callback
)
865 : DeleteTask
<void>(cookie_monster
, callback
),
867 cookie_name_(cookie_name
) {}
870 void RunDeleteTask() override
;
873 ~DeleteCookieTask() override
{}
877 std::string cookie_name_
;
879 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask
);
882 void CookieMonster::DeleteCookieTask::RunDeleteTask() {
883 this->cookie_monster()->DeleteCookie(url_
, cookie_name_
);
886 // Task class for DeleteSessionCookies call.
887 class CookieMonster::DeleteSessionCookiesTask
: public DeleteTask
<int> {
889 DeleteSessionCookiesTask(CookieMonster
* cookie_monster
,
890 const DeleteCallback
& callback
)
891 : DeleteTask
<int>(cookie_monster
, callback
) {}
894 int RunDeleteTask() override
;
897 ~DeleteSessionCookiesTask() override
{}
900 DISALLOW_COPY_AND_ASSIGN(DeleteSessionCookiesTask
);
903 int CookieMonster::DeleteSessionCookiesTask::RunDeleteTask() {
904 return this->cookie_monster()->DeleteSessionCookies();
907 // Task class for HasCookiesForETLDP1Task call.
908 class CookieMonster::HasCookiesForETLDP1Task
: public CookieMonsterTask
{
910 HasCookiesForETLDP1Task(CookieMonster
* cookie_monster
,
911 const std::string
& etldp1
,
912 const HasCookiesForETLDP1Callback
& callback
)
913 : CookieMonsterTask(cookie_monster
),
915 callback_(callback
) {}
917 // CookieMonsterTask:
921 ~HasCookiesForETLDP1Task() override
{}
925 HasCookiesForETLDP1Callback callback_
;
927 DISALLOW_COPY_AND_ASSIGN(HasCookiesForETLDP1Task
);
930 void CookieMonster::HasCookiesForETLDP1Task::Run() {
931 bool result
= this->cookie_monster()->HasCookiesForETLDP1(etldp1_
);
932 if (!callback_
.is_null()) {
933 this->InvokeCallback(base::Bind(&HasCookiesForETLDP1Callback::Run
,
934 base::Unretained(&callback_
), result
));
938 // Asynchronous CookieMonster API
940 void CookieMonster::SetCookieWithDetailsAsync(
942 const std::string
& name
,
943 const std::string
& value
,
944 const std::string
& domain
,
945 const std::string
& path
,
946 const Time
& expiration_time
,
949 bool first_party_only
,
950 CookiePriority priority
,
951 const SetCookiesCallback
& callback
) {
952 scoped_refptr
<SetCookieWithDetailsTask
> task
= new SetCookieWithDetailsTask(
953 this, url
, name
, value
, domain
, path
, expiration_time
, secure
, http_only
,
954 first_party_only
, priority
, callback
);
955 DoCookieTaskForURL(task
, url
);
958 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback
& callback
) {
959 scoped_refptr
<GetAllCookiesTask
> task
= new GetAllCookiesTask(this, callback
);
964 void CookieMonster::GetAllCookiesForURLWithOptionsAsync(
966 const CookieOptions
& options
,
967 const GetCookieListCallback
& callback
) {
968 scoped_refptr
<GetAllCookiesForURLWithOptionsTask
> task
=
969 new GetAllCookiesForURLWithOptionsTask(this, url
, options
, callback
);
971 DoCookieTaskForURL(task
, url
);
974 void CookieMonster::GetAllCookiesForURLAsync(
976 const GetCookieListCallback
& callback
) {
977 CookieOptions options
;
978 options
.set_include_httponly();
979 options
.set_include_first_party_only();
980 scoped_refptr
<GetAllCookiesForURLWithOptionsTask
> task
=
981 new GetAllCookiesForURLWithOptionsTask(this, url
, options
, callback
);
983 DoCookieTaskForURL(task
, url
);
986 void CookieMonster::HasCookiesForETLDP1Async(
987 const std::string
& etldp1
,
988 const HasCookiesForETLDP1Callback
& callback
) {
989 scoped_refptr
<HasCookiesForETLDP1Task
> task
=
990 new HasCookiesForETLDP1Task(this, etldp1
, callback
);
992 DoCookieTaskForURL(task
, GURL("http://" + etldp1
));
995 void CookieMonster::DeleteAllAsync(const DeleteCallback
& callback
) {
996 scoped_refptr
<DeleteAllTask
> task
= new DeleteAllTask(this, callback
);
1001 void CookieMonster::DeleteAllCreatedBetweenAsync(
1002 const Time
& delete_begin
,
1003 const Time
& delete_end
,
1004 const DeleteCallback
& callback
) {
1005 scoped_refptr
<DeleteAllCreatedBetweenTask
> task
=
1006 new DeleteAllCreatedBetweenTask(this, delete_begin
, delete_end
, callback
);
1011 void CookieMonster::DeleteAllCreatedBetweenForHostAsync(
1012 const Time delete_begin
,
1013 const Time delete_end
,
1015 const DeleteCallback
& callback
) {
1016 scoped_refptr
<DeleteAllCreatedBetweenForHostTask
> task
=
1017 new DeleteAllCreatedBetweenForHostTask(this, delete_begin
, delete_end
,
1020 DoCookieTaskForURL(task
, url
);
1023 void CookieMonster::DeleteAllForHostAsync(const GURL
& url
,
1024 const DeleteCallback
& callback
) {
1025 scoped_refptr
<DeleteAllForHostTask
> task
=
1026 new DeleteAllForHostTask(this, url
, callback
);
1028 DoCookieTaskForURL(task
, url
);
1031 void CookieMonster::DeleteCanonicalCookieAsync(
1032 const CanonicalCookie
& cookie
,
1033 const DeleteCookieCallback
& callback
) {
1034 scoped_refptr
<DeleteCanonicalCookieTask
> task
=
1035 new DeleteCanonicalCookieTask(this, cookie
, callback
);
1040 void CookieMonster::SetAllCookiesAsync(const CookieList
& list
,
1041 const SetCookiesCallback
& callback
) {
1042 scoped_refptr
<SetAllCookiesTask
> task
=
1043 new SetAllCookiesTask(this, list
, callback
);
1047 void CookieMonster::SetCookieWithOptionsAsync(
1049 const std::string
& cookie_line
,
1050 const CookieOptions
& options
,
1051 const SetCookiesCallback
& callback
) {
1052 scoped_refptr
<SetCookieWithOptionsTask
> task
=
1053 new SetCookieWithOptionsTask(this, url
, cookie_line
, options
, callback
);
1055 DoCookieTaskForURL(task
, url
);
1058 void CookieMonster::GetCookiesWithOptionsAsync(
1060 const CookieOptions
& options
,
1061 const GetCookiesCallback
& callback
) {
1062 scoped_refptr
<GetCookiesWithOptionsTask
> task
=
1063 new GetCookiesWithOptionsTask(this, url
, options
, callback
);
1065 DoCookieTaskForURL(task
, url
);
1068 void CookieMonster::DeleteCookieAsync(const GURL
& url
,
1069 const std::string
& cookie_name
,
1070 const base::Closure
& callback
) {
1071 scoped_refptr
<DeleteCookieTask
> task
=
1072 new DeleteCookieTask(this, url
, cookie_name
, callback
);
1074 DoCookieTaskForURL(task
, url
);
1077 void CookieMonster::DeleteSessionCookiesAsync(
1078 const CookieStore::DeleteCallback
& callback
) {
1079 scoped_refptr
<DeleteSessionCookiesTask
> task
=
1080 new DeleteSessionCookiesTask(this, callback
);
1085 void CookieMonster::DoCookieTask(
1086 const scoped_refptr
<CookieMonsterTask
>& task_item
) {
1088 base::AutoLock
autolock(lock_
);
1091 tasks_pending_
.push(task_item
);
1099 void CookieMonster::DoCookieTaskForURL(
1100 const scoped_refptr
<CookieMonsterTask
>& task_item
,
1103 base::AutoLock
autolock(lock_
);
1105 // If cookies for the requested domain key (eTLD+1) have been loaded from DB
1106 // then run the task, otherwise load from DB.
1108 // Checks if the domain key has been loaded.
1110 cookie_util::GetEffectiveDomain(url
.scheme(), url
.host()));
1111 if (keys_loaded_
.find(key
) == keys_loaded_
.end()) {
1112 std::map
<std::string
,
1113 std::deque
<scoped_refptr
<CookieMonsterTask
>>>::iterator it
=
1114 tasks_pending_for_key_
.find(key
);
1115 if (it
== tasks_pending_for_key_
.end()) {
1116 store_
->LoadCookiesForKey(
1117 key
, base::Bind(&CookieMonster::OnKeyLoaded
, this, key
));
1118 it
= tasks_pending_for_key_
1119 .insert(std::make_pair(
1120 key
, std::deque
<scoped_refptr
<CookieMonsterTask
>>()))
1123 it
->second
.push_back(task_item
);
1131 bool CookieMonster::SetCookieWithDetails(const GURL
& url
,
1132 const std::string
& name
,
1133 const std::string
& value
,
1134 const std::string
& domain
,
1135 const std::string
& path
,
1136 const base::Time
& expiration_time
,
1139 bool first_party_only
,
1140 CookiePriority priority
) {
1141 base::AutoLock
autolock(lock_
);
1143 if (!HasCookieableScheme(url
))
1146 Time creation_time
= CurrentTime();
1147 last_time_seen_
= creation_time
;
1149 scoped_ptr
<CanonicalCookie
> cc
;
1150 cc
.reset(CanonicalCookie::Create(url
, name
, value
, domain
, path
,
1151 creation_time
, expiration_time
, secure
,
1152 http_only
, first_party_only
, priority
));
1157 CookieOptions options
;
1158 options
.set_include_httponly();
1159 options
.set_include_first_party_only();
1160 return SetCanonicalCookie(&cc
, creation_time
, options
);
1163 bool CookieMonster::ImportCookies(const CookieList
& list
) {
1164 base::AutoLock
autolock(lock_
);
1166 for (CookieList::const_iterator iter
= list
.begin(); iter
!= list
.end();
1168 scoped_ptr
<CanonicalCookie
> cookie(new CanonicalCookie(*iter
));
1169 CookieOptions options
;
1170 options
.set_include_httponly();
1171 options
.set_include_first_party_only();
1172 if (!SetCanonicalCookie(&cookie
, cookie
->CreationDate(), options
))
1178 CookieList
CookieMonster::GetAllCookies() {
1179 base::AutoLock
autolock(lock_
);
1181 // This function is being called to scrape the cookie list for management UI
1182 // or similar. We shouldn't show expired cookies in this list since it will
1183 // just be confusing to users, and this function is called rarely enough (and
1184 // is already slow enough) that it's OK to take the time to garbage collect
1185 // the expired cookies now.
1187 // Note that this does not prune cookies to be below our limits (if we've
1188 // exceeded them) the way that calling GarbageCollect() would.
1189 GarbageCollectExpired(
1190 Time::Now(), CookieMapItPair(cookies_
.begin(), cookies_
.end()), NULL
);
1192 // Copy the CanonicalCookie pointers from the map so that we can use the same
1193 // sorter as elsewhere, then copy the result out.
1194 std::vector
<CanonicalCookie
*> cookie_ptrs
;
1195 cookie_ptrs
.reserve(cookies_
.size());
1196 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end(); ++it
)
1197 cookie_ptrs
.push_back(it
->second
);
1198 std::sort(cookie_ptrs
.begin(), cookie_ptrs
.end(), CookieSorter
);
1200 CookieList cookie_list
;
1201 cookie_list
.reserve(cookie_ptrs
.size());
1202 for (std::vector
<CanonicalCookie
*>::const_iterator it
= cookie_ptrs
.begin();
1203 it
!= cookie_ptrs
.end(); ++it
)
1204 cookie_list
.push_back(**it
);
1209 CookieList
CookieMonster::GetAllCookiesForURLWithOptions(
1211 const CookieOptions
& options
) {
1212 base::AutoLock
autolock(lock_
);
1214 std::vector
<CanonicalCookie
*> cookie_ptrs
;
1215 FindCookiesForHostAndDomain(url
, options
, false, &cookie_ptrs
);
1216 std::sort(cookie_ptrs
.begin(), cookie_ptrs
.end(), CookieSorter
);
1219 cookies
.reserve(cookie_ptrs
.size());
1220 for (std::vector
<CanonicalCookie
*>::const_iterator it
= cookie_ptrs
.begin();
1221 it
!= cookie_ptrs
.end(); it
++)
1222 cookies
.push_back(**it
);
1227 CookieList
CookieMonster::GetAllCookiesForURL(const GURL
& url
) {
1228 CookieOptions options
;
1229 options
.set_include_httponly();
1230 options
.set_first_party_url(url
);
1232 return GetAllCookiesForURLWithOptions(url
, options
);
1235 int CookieMonster::DeleteAll(bool sync_to_store
) {
1236 // TODO(xiyuan): Remove the log after http://crbug.com/449816.
1237 VLOG(kVlogSetCookies
) << "CookieMonster::DeleteAll, sync_to_store="
1240 base::AutoLock
autolock(lock_
);
1242 int num_deleted
= 0;
1243 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end();) {
1244 CookieMap::iterator curit
= it
;
1246 InternalDeleteCookie(curit
, sync_to_store
,
1248 ? DELETE_COOKIE_EXPLICIT
1249 : DELETE_COOKIE_DONT_RECORD
/* Destruction. */);
1256 int CookieMonster::DeleteAllCreatedBetween(const Time
& delete_begin
,
1257 const Time
& delete_end
) {
1258 // TODO(xiyuan): Remove the log after http://crbug.com/449816.
1259 VLOG(kVlogSetCookies
) << "CookieMonster::DeleteAllCreatedBetween";
1261 base::AutoLock
autolock(lock_
);
1263 int num_deleted
= 0;
1264 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end();) {
1265 CookieMap::iterator curit
= it
;
1266 CanonicalCookie
* cc
= curit
->second
;
1269 if (cc
->CreationDate() >= delete_begin
&&
1270 (delete_end
.is_null() || cc
->CreationDate() < delete_end
)) {
1271 InternalDeleteCookie(curit
, true, /*sync_to_store*/
1272 DELETE_COOKIE_EXPLICIT
);
1280 int CookieMonster::DeleteAllCreatedBetweenForHost(const Time delete_begin
,
1281 const Time delete_end
,
1283 // TODO(xiyuan): Remove the log after http://crbug.com/449816.
1284 VLOG(kVlogSetCookies
) << "CookieMonster::DeleteAllCreatedBetweenForHost";
1286 base::AutoLock
autolock(lock_
);
1288 if (!HasCookieableScheme(url
))
1291 const std::string
host(url
.host());
1293 // We store host cookies in the store by their canonical host name;
1294 // domain cookies are stored with a leading ".". So this is a pretty
1295 // simple lookup and per-cookie delete.
1296 int num_deleted
= 0;
1297 for (CookieMapItPair its
= cookies_
.equal_range(GetKey(host
));
1298 its
.first
!= its
.second
;) {
1299 CookieMap::iterator curit
= its
.first
;
1302 const CanonicalCookie
* const cc
= curit
->second
;
1304 // Delete only on a match as a host cookie.
1305 if (cc
->IsHostCookie() && cc
->IsDomainMatch(host
) &&
1306 cc
->CreationDate() >= delete_begin
&&
1307 // The assumption that null |delete_end| is equivalent to
1308 // Time::Max() is confusing.
1309 (delete_end
.is_null() || cc
->CreationDate() < delete_end
)) {
1312 InternalDeleteCookie(curit
, true, DELETE_COOKIE_EXPLICIT
);
1318 int CookieMonster::DeleteAllForHost(const GURL
& url
) {
1319 return DeleteAllCreatedBetweenForHost(Time(), Time::Max(), url
);
1322 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie
& cookie
) {
1323 // TODO(xiyuan): Remove the log after http://crbug.com/449816.
1324 VLOG(kVlogSetCookies
) << "CookieMonster::DeleteCanonicalCookie";
1326 base::AutoLock
autolock(lock_
);
1328 for (CookieMapItPair its
= cookies_
.equal_range(GetKey(cookie
.Domain()));
1329 its
.first
!= its
.second
; ++its
.first
) {
1330 // The creation date acts as our unique index...
1331 if (its
.first
->second
->CreationDate() == cookie
.CreationDate()) {
1332 InternalDeleteCookie(its
.first
, true, DELETE_COOKIE_EXPLICIT
);
1339 void CookieMonster::SetCookieableSchemes(const char* const schemes
[],
1340 size_t num_schemes
) {
1341 base::AutoLock
autolock(lock_
);
1343 // Cookieable Schemes must be set before first use of function.
1344 DCHECK(!initialized_
);
1346 cookieable_schemes_
.clear();
1347 cookieable_schemes_
.insert(cookieable_schemes_
.end(), schemes
,
1348 schemes
+ num_schemes
);
1351 void CookieMonster::SetKeepExpiredCookies() {
1352 keep_expired_cookies_
= true;
1355 void CookieMonster::FlushStore(const base::Closure
& callback
) {
1356 base::AutoLock
autolock(lock_
);
1357 if (initialized_
&& store_
.get())
1358 store_
->Flush(callback
);
1359 else if (!callback
.is_null())
1360 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
1363 bool CookieMonster::SetCookieWithOptions(const GURL
& url
,
1364 const std::string
& cookie_line
,
1365 const CookieOptions
& options
) {
1366 base::AutoLock
autolock(lock_
);
1368 if (!HasCookieableScheme(url
)) {
1372 return SetCookieWithCreationTimeAndOptions(url
, cookie_line
, Time(), options
);
1375 std::string
CookieMonster::GetCookiesWithOptions(const GURL
& url
,
1376 const CookieOptions
& options
) {
1377 base::AutoLock
autolock(lock_
);
1379 if (!HasCookieableScheme(url
))
1380 return std::string();
1382 std::vector
<CanonicalCookie
*> cookies
;
1383 FindCookiesForHostAndDomain(url
, options
, true, &cookies
);
1384 std::sort(cookies
.begin(), cookies
.end(), CookieSorter
);
1386 std::string cookie_line
= BuildCookieLine(cookies
);
1388 VLOG(kVlogGetCookies
) << "GetCookies() result: " << cookie_line
;
1393 void CookieMonster::DeleteCookie(const GURL
& url
,
1394 const std::string
& cookie_name
) {
1395 // TODO(xiyuan): Remove the log after http://crbug.com/449816.
1396 VLOG(kVlogSetCookies
) << "CookieMonster::DeleteCookie";
1398 base::AutoLock
autolock(lock_
);
1400 if (!HasCookieableScheme(url
))
1403 CookieOptions options
;
1404 options
.set_include_httponly();
1405 options
.set_include_first_party_only();
1406 // Get the cookies for this host and its domain(s).
1407 std::vector
<CanonicalCookie
*> cookies
;
1408 FindCookiesForHostAndDomain(url
, options
, true, &cookies
);
1409 std::set
<CanonicalCookie
*> matching_cookies
;
1411 for (std::vector
<CanonicalCookie
*>::const_iterator it
= cookies
.begin();
1412 it
!= cookies
.end(); ++it
) {
1413 if ((*it
)->Name() != cookie_name
)
1415 if (url
.path().find((*it
)->Path()))
1417 matching_cookies
.insert(*it
);
1420 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end();) {
1421 CookieMap::iterator curit
= it
;
1423 if (matching_cookies
.find(curit
->second
) != matching_cookies
.end()) {
1424 InternalDeleteCookie(curit
, true, DELETE_COOKIE_EXPLICIT
);
1429 int CookieMonster::DeleteSessionCookies() {
1430 // TODO(xiyuan): Remove the log after http://crbug.com/449816.
1431 VLOG(kVlogSetCookies
) << "CookieMonster::DeleteSessionCookies";
1433 base::AutoLock
autolock(lock_
);
1435 int num_deleted
= 0;
1436 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end();) {
1437 CookieMap::iterator curit
= it
;
1438 CanonicalCookie
* cc
= curit
->second
;
1441 if (!cc
->IsPersistent()) {
1442 InternalDeleteCookie(curit
, true, /*sync_to_store*/
1443 DELETE_COOKIE_EXPIRED
);
1451 bool CookieMonster::HasCookiesForETLDP1(const std::string
& etldp1
) {
1452 base::AutoLock
autolock(lock_
);
1454 const std::string
key(GetKey(etldp1
));
1456 CookieMapItPair its
= cookies_
.equal_range(key
);
1457 return its
.first
!= its
.second
;
1460 CookieMonster
* CookieMonster::GetCookieMonster() {
1464 // This function must be called before the CookieMonster is used.
1465 void CookieMonster::SetPersistSessionCookies(bool persist_session_cookies
) {
1466 DCHECK(!initialized_
);
1467 persist_session_cookies_
= persist_session_cookies
;
1470 void CookieMonster::SetForceKeepSessionState() {
1472 store_
->SetForceKeepSessionState();
1476 CookieMonster::~CookieMonster() {
1480 bool CookieMonster::SetCookieWithCreationTime(const GURL
& url
,
1481 const std::string
& cookie_line
,
1482 const base::Time
& creation_time
) {
1483 DCHECK(!store_
.get()) << "This method is only to be used by unit-tests.";
1484 base::AutoLock
autolock(lock_
);
1486 if (!HasCookieableScheme(url
)) {
1491 return SetCookieWithCreationTimeAndOptions(url
, cookie_line
, creation_time
,
1495 void CookieMonster::InitStore() {
1496 DCHECK(store_
.get()) << "Store must exist to initialize";
1498 // We bind in the current time so that we can report the wall-clock time for
1500 store_
->Load(base::Bind(&CookieMonster::OnLoaded
, this, TimeTicks::Now()));
1503 void CookieMonster::OnLoaded(TimeTicks beginning_time
,
1504 const std::vector
<CanonicalCookie
*>& cookies
) {
1505 // TODO(pkasting): Remove ScopedTracker below once crbug.com/457528 is fixed.
1506 tracked_objects::ScopedTracker
tracking_profile1(
1507 FROM_HERE_WITH_EXPLICIT_FUNCTION("457528 CookieMonster::OnLoaded 1"));
1508 StoreLoadedCookies(cookies
);
1509 histogram_time_blocked_on_load_
->AddTime(TimeTicks::Now() - beginning_time
);
1511 // TODO(pkasting): Remove ScopedTracker below once crbug.com/457528 is fixed.
1512 tracked_objects::ScopedTracker
tracking_profile2(
1513 FROM_HERE_WITH_EXPLICIT_FUNCTION("457528 CookieMonster::OnLoaded 2"));
1514 // Invoke the task queue of cookie request.
1518 void CookieMonster::OnKeyLoaded(const std::string
& key
,
1519 const std::vector
<CanonicalCookie
*>& cookies
) {
1520 // This function does its own separate locking.
1521 StoreLoadedCookies(cookies
);
1523 std::deque
<scoped_refptr
<CookieMonsterTask
>> tasks_pending_for_key
;
1525 // We need to do this repeatedly until no more tasks were added to the queue
1526 // during the period where we release the lock.
1529 base::AutoLock
autolock(lock_
);
1530 std::map
<std::string
,
1531 std::deque
<scoped_refptr
<CookieMonsterTask
>>>::iterator it
=
1532 tasks_pending_for_key_
.find(key
);
1533 if (it
== tasks_pending_for_key_
.end()) {
1534 keys_loaded_
.insert(key
);
1537 if (it
->second
.empty()) {
1538 keys_loaded_
.insert(key
);
1539 tasks_pending_for_key_
.erase(it
);
1542 it
->second
.swap(tasks_pending_for_key
);
1545 while (!tasks_pending_for_key
.empty()) {
1546 scoped_refptr
<CookieMonsterTask
> task
= tasks_pending_for_key
.front();
1548 tasks_pending_for_key
.pop_front();
1553 void CookieMonster::StoreLoadedCookies(
1554 const std::vector
<CanonicalCookie
*>& cookies
) {
1555 // Initialize the store and sync in any saved persistent cookies. We don't
1556 // care if it's expired, insert it so it can be garbage collected, removed,
1558 base::AutoLock
autolock(lock_
);
1560 CookieItVector cookies_with_control_chars
;
1562 for (std::vector
<CanonicalCookie
*>::const_iterator it
= cookies
.begin();
1563 it
!= cookies
.end(); ++it
) {
1564 int64 cookie_creation_time
= (*it
)->CreationDate().ToInternalValue();
1566 if (creation_times_
.insert(cookie_creation_time
).second
) {
1567 CookieMap::iterator inserted
=
1568 InternalInsertCookie(GetKey((*it
)->Domain()), *it
, false);
1569 const Time
cookie_access_time((*it
)->LastAccessDate());
1570 if (earliest_access_time_
.is_null() ||
1571 cookie_access_time
< earliest_access_time_
)
1572 earliest_access_time_
= cookie_access_time
;
1574 if (ContainsControlCharacter((*it
)->Name()) ||
1575 ContainsControlCharacter((*it
)->Value())) {
1576 cookies_with_control_chars
.push_back(inserted
);
1579 LOG(ERROR
) << base::StringPrintf(
1580 "Found cookies with duplicate creation "
1581 "times in backing store: "
1582 "{name='%s', domain='%s', path='%s'}",
1583 (*it
)->Name().c_str(), (*it
)->Domain().c_str(),
1584 (*it
)->Path().c_str());
1585 // We've been given ownership of the cookie and are throwing it
1586 // away; reclaim the space.
1591 // Any cookies that contain control characters that we have loaded from the
1592 // persistent store should be deleted. See http://crbug.com/238041.
1593 for (CookieItVector::iterator it
= cookies_with_control_chars
.begin();
1594 it
!= cookies_with_control_chars
.end();) {
1595 CookieItVector::iterator curit
= it
;
1598 InternalDeleteCookie(*curit
, true, DELETE_COOKIE_CONTROL_CHAR
);
1601 // After importing cookies from the PersistentCookieStore, verify that
1602 // none of our other constraints are violated.
1603 // In particular, the backing store might have given us duplicate cookies.
1605 // This method could be called multiple times due to priority loading, thus
1606 // cookies loaded in previous runs will be validated again, but this is OK
1607 // since they are expected to be much fewer than total DB.
1608 EnsureCookiesMapIsValid();
1611 void CookieMonster::InvokeQueue() {
1613 scoped_refptr
<CookieMonsterTask
> request_task
;
1615 base::AutoLock
autolock(lock_
);
1616 if (tasks_pending_
.empty()) {
1618 creation_times_
.clear();
1619 keys_loaded_
.clear();
1622 request_task
= tasks_pending_
.front();
1623 tasks_pending_
.pop();
1625 request_task
->Run();
1629 void CookieMonster::EnsureCookiesMapIsValid() {
1630 lock_
.AssertAcquired();
1632 int num_duplicates_trimmed
= 0;
1634 // Iterate through all the of the cookies, grouped by host.
1635 CookieMap::iterator prev_range_end
= cookies_
.begin();
1636 while (prev_range_end
!= cookies_
.end()) {
1637 CookieMap::iterator cur_range_begin
= prev_range_end
;
1638 const std::string key
= cur_range_begin
->first
; // Keep a copy.
1639 CookieMap::iterator cur_range_end
= cookies_
.upper_bound(key
);
1640 prev_range_end
= cur_range_end
;
1642 // Ensure no equivalent cookies for this host.
1643 num_duplicates_trimmed
+=
1644 TrimDuplicateCookiesForKey(key
, cur_range_begin
, cur_range_end
);
1647 // Record how many duplicates were found in the database.
1648 // See InitializeHistograms() for details.
1649 histogram_number_duplicate_db_cookies_
->Add(num_duplicates_trimmed
);
1652 int CookieMonster::TrimDuplicateCookiesForKey(const std::string
& key
,
1653 CookieMap::iterator begin
,
1654 CookieMap::iterator end
) {
1655 lock_
.AssertAcquired();
1657 // Set of cookies ordered by creation time.
1658 typedef std::set
<CookieMap::iterator
, OrderByCreationTimeDesc
> CookieSet
;
1660 // Helper map we populate to find the duplicates.
1661 typedef std::map
<CookieSignature
, CookieSet
> EquivalenceMap
;
1662 EquivalenceMap equivalent_cookies
;
1664 // The number of duplicate cookies that have been found.
1665 int num_duplicates
= 0;
1667 // Iterate through all of the cookies in our range, and insert them into
1668 // the equivalence map.
1669 for (CookieMap::iterator it
= begin
; it
!= end
; ++it
) {
1670 DCHECK_EQ(key
, it
->first
);
1671 CanonicalCookie
* cookie
= it
->second
;
1673 CookieSignature
signature(cookie
->Name(), cookie
->Domain(), cookie
->Path());
1674 CookieSet
& set
= equivalent_cookies
[signature
];
1676 // We found a duplicate!
1680 // We save the iterator into |cookies_| rather than the actual cookie
1681 // pointer, since we may need to delete it later.
1682 bool insert_success
= set
.insert(it
).second
;
1683 DCHECK(insert_success
)
1684 << "Duplicate creation times found in duplicate cookie name scan.";
1687 // If there were no duplicates, we are done!
1688 if (num_duplicates
== 0)
1691 // Make sure we find everything below that we did above.
1692 int num_duplicates_found
= 0;
1694 // Otherwise, delete all the duplicate cookies, both from our in-memory store
1695 // and from the backing store.
1696 for (EquivalenceMap::iterator it
= equivalent_cookies
.begin();
1697 it
!= equivalent_cookies
.end(); ++it
) {
1698 const CookieSignature
& signature
= it
->first
;
1699 CookieSet
& dupes
= it
->second
;
1701 if (dupes
.size() <= 1)
1702 continue; // This cookiename/path has no duplicates.
1703 num_duplicates_found
+= dupes
.size() - 1;
1705 // Since |dups| is sorted by creation time (descending), the first cookie
1706 // is the most recent one, so we will keep it. The rest are duplicates.
1707 dupes
.erase(dupes
.begin());
1709 LOG(ERROR
) << base::StringPrintf(
1710 "Found %d duplicate cookies for host='%s', "
1711 "with {name='%s', domain='%s', path='%s'}",
1712 static_cast<int>(dupes
.size()), key
.c_str(), signature
.name
.c_str(),
1713 signature
.domain
.c_str(), signature
.path
.c_str());
1715 // Remove all the cookies identified by |dupes|. It is valid to delete our
1716 // list of iterators one at a time, since |cookies_| is a multimap (they
1717 // don't invalidate existing iterators following deletion).
1718 for (CookieSet::iterator dupes_it
= dupes
.begin(); dupes_it
!= dupes
.end();
1720 InternalDeleteCookie(*dupes_it
, true,
1721 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE
);
1724 DCHECK_EQ(num_duplicates
, num_duplicates_found
);
1726 return num_duplicates
;
1729 // Note: file must be the last scheme.
1730 const char* const CookieMonster::kDefaultCookieableSchemes
[] = {"http",
1735 const int CookieMonster::kDefaultCookieableSchemesCount
=
1736 arraysize(kDefaultCookieableSchemes
);
1738 void CookieMonster::SetDefaultCookieableSchemes() {
1739 // Always disable file scheme unless SetEnableFileScheme(true) is called.
1740 SetCookieableSchemes(kDefaultCookieableSchemes
,
1741 kDefaultCookieableSchemesCount
- 1);
1744 void CookieMonster::FindCookiesForHostAndDomain(
1746 const CookieOptions
& options
,
1747 bool update_access_time
,
1748 std::vector
<CanonicalCookie
*>* cookies
) {
1749 lock_
.AssertAcquired();
1751 const Time
current_time(CurrentTime());
1753 // Probe to save statistics relatively frequently. We do it here rather
1754 // than in the set path as many websites won't set cookies, and we
1755 // want to collect statistics whenever the browser's being used.
1756 RecordPeriodicStats(current_time
);
1758 // Can just dispatch to FindCookiesForKey
1759 const std::string
key(GetKey(url
.host()));
1760 FindCookiesForKey(key
, url
, options
, current_time
, update_access_time
,
1764 void CookieMonster::FindCookiesForKey(const std::string
& key
,
1766 const CookieOptions
& options
,
1767 const Time
& current
,
1768 bool update_access_time
,
1769 std::vector
<CanonicalCookie
*>* cookies
) {
1770 lock_
.AssertAcquired();
1772 for (CookieMapItPair its
= cookies_
.equal_range(key
);
1773 its
.first
!= its
.second
;) {
1774 CookieMap::iterator curit
= its
.first
;
1775 CanonicalCookie
* cc
= curit
->second
;
1778 // If the cookie is expired, delete it.
1779 if (cc
->IsExpired(current
) && !keep_expired_cookies_
) {
1780 InternalDeleteCookie(curit
, true, DELETE_COOKIE_EXPIRED
);
1784 // Filter out cookies that should not be included for a request to the
1785 // given |url|. HTTP only cookies are filtered depending on the passed
1786 // cookie |options|.
1787 if (!cc
->IncludeForRequestURL(url
, options
))
1790 // Add this cookie to the set of matching cookies. Update the access
1791 // time if we've been requested to do so.
1792 if (update_access_time
) {
1793 InternalUpdateCookieAccessTime(cc
, current
);
1795 cookies
->push_back(cc
);
1799 bool CookieMonster::DeleteAnyEquivalentCookie(const std::string
& key
,
1800 const CanonicalCookie
& ecc
,
1802 bool already_expired
) {
1803 lock_
.AssertAcquired();
1805 bool found_equivalent_cookie
= false;
1806 bool skipped_httponly
= false;
1807 for (CookieMapItPair its
= cookies_
.equal_range(key
);
1808 its
.first
!= its
.second
;) {
1809 CookieMap::iterator curit
= its
.first
;
1810 CanonicalCookie
* cc
= curit
->second
;
1813 if (ecc
.IsEquivalent(*cc
)) {
1814 // We should never have more than one equivalent cookie, since they should
1815 // overwrite each other.
1816 CHECK(!found_equivalent_cookie
)
1817 << "Duplicate equivalent cookies found, cookie store is corrupted.";
1818 if (skip_httponly
&& cc
->IsHttpOnly()) {
1819 skipped_httponly
= true;
1821 InternalDeleteCookie(curit
, true, already_expired
1822 ? DELETE_COOKIE_EXPIRED_OVERWRITE
1823 : DELETE_COOKIE_OVERWRITE
);
1825 found_equivalent_cookie
= true;
1828 return skipped_httponly
;
1831 CookieMonster::CookieMap::iterator
CookieMonster::InternalInsertCookie(
1832 const std::string
& key
,
1833 CanonicalCookie
* cc
,
1834 bool sync_to_store
) {
1835 // TODO(mkwst): Remove ScopedTracker below once crbug.com/456373 is fixed.
1836 tracked_objects::ScopedTracker
tracking_profile(
1837 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1838 "456373 CookieMonster::InternalInsertCookie"));
1839 lock_
.AssertAcquired();
1841 if ((cc
->IsPersistent() || persist_session_cookies_
) && store_
.get() &&
1843 store_
->AddCookie(*cc
);
1844 CookieMap::iterator inserted
=
1845 cookies_
.insert(CookieMap::value_type(key
, cc
));
1846 if (delegate_
.get()) {
1847 delegate_
->OnCookieChanged(*cc
, false,
1848 CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
);
1851 // See InitializeHistograms() for details.
1852 int32_t sample
= cc
->IsFirstPartyOnly() ? 1 << COOKIE_TYPE_FIRSTPARTYONLY
: 0;
1853 sample
|= cc
->IsHttpOnly() ? 1 << COOKIE_TYPE_HTTPONLY
: 0;
1854 sample
|= cc
->IsSecure() ? 1 << COOKIE_TYPE_SECURE
: 0;
1855 histogram_cookie_type_
->Add(sample
);
1857 RunCallbacks(*cc
, false);
1862 bool CookieMonster::SetCookieWithCreationTimeAndOptions(
1864 const std::string
& cookie_line
,
1865 const Time
& creation_time_or_null
,
1866 const CookieOptions
& options
) {
1867 lock_
.AssertAcquired();
1869 VLOG(kVlogSetCookies
) << "SetCookie() line: " << cookie_line
;
1871 Time creation_time
= creation_time_or_null
;
1872 if (creation_time
.is_null()) {
1873 creation_time
= CurrentTime();
1874 last_time_seen_
= creation_time
;
1877 scoped_ptr
<CanonicalCookie
> cc(
1878 CanonicalCookie::Create(url
, cookie_line
, creation_time
, options
));
1881 VLOG(kVlogSetCookies
) << "WARNING: Failed to allocate CanonicalCookie";
1884 return SetCanonicalCookie(&cc
, creation_time
, options
);
1887 bool CookieMonster::SetCanonicalCookie(scoped_ptr
<CanonicalCookie
>* cc
,
1888 const Time
& creation_time
,
1889 const CookieOptions
& options
) {
1890 const std::string
key(GetKey((*cc
)->Domain()));
1891 bool already_expired
= (*cc
)->IsExpired(creation_time
);
1893 if (DeleteAnyEquivalentCookie(key
, **cc
, options
.exclude_httponly(),
1895 VLOG(kVlogSetCookies
) << "SetCookie() not clobbering httponly cookie";
1899 VLOG(kVlogSetCookies
) << "SetCookie() key: " << key
1900 << " cc: " << (*cc
)->DebugString();
1902 // Realize that we might be setting an expired cookie, and the only point
1903 // was to delete the cookie which we've already done.
1904 if (!already_expired
|| keep_expired_cookies_
) {
1905 // See InitializeHistograms() for details.
1906 if ((*cc
)->IsPersistent()) {
1907 histogram_expiration_duration_minutes_
->Add(
1908 ((*cc
)->ExpiryDate() - creation_time
).InMinutes());
1912 CanonicalCookie cookie
= *(cc
->get());
1913 InternalInsertCookie(key
, cc
->release(), true);
1916 VLOG(kVlogSetCookies
) << "SetCookie() not storing already expired cookie.";
1919 // We assume that hopefully setting a cookie will be less common than
1920 // querying a cookie. Since setting a cookie can put us over our limits,
1921 // make sure that we garbage collect... We can also make the assumption that
1922 // if a cookie was set, in the common case it will be used soon after,
1923 // and we will purge the expired cookies in GetCookies().
1924 GarbageCollect(creation_time
, key
);
1929 bool CookieMonster::SetCanonicalCookies(const CookieList
& list
) {
1930 base::AutoLock
autolock(lock_
);
1932 CookieOptions options
;
1933 options
.set_include_httponly();
1935 for (CookieList::const_iterator it
= list
.begin(); it
!= list
.end(); ++it
) {
1936 scoped_ptr
<CanonicalCookie
> canonical_cookie(new CanonicalCookie(*it
));
1937 if (!SetCanonicalCookie(&canonical_cookie
, it
->CreationDate(), options
))
1944 void CookieMonster::InternalUpdateCookieAccessTime(CanonicalCookie
* cc
,
1945 const Time
& current
) {
1946 lock_
.AssertAcquired();
1948 // Based off the Mozilla code. When a cookie has been accessed recently,
1949 // don't bother updating its access time again. This reduces the number of
1950 // updates we do during pageload, which in turn reduces the chance our storage
1951 // backend will hit its batch thresholds and be forced to update.
1952 if ((current
- cc
->LastAccessDate()) < last_access_threshold_
)
1955 // See InitializeHistograms() for details.
1956 histogram_between_access_interval_minutes_
->Add(
1957 (current
- cc
->LastAccessDate()).InMinutes());
1959 cc
->SetLastAccessDate(current
);
1960 if ((cc
->IsPersistent() || persist_session_cookies_
) && store_
.get())
1961 store_
->UpdateCookieAccessTime(*cc
);
1964 // InternalDeleteCookies must not invalidate iterators other than the one being
1966 void CookieMonster::InternalDeleteCookie(CookieMap::iterator it
,
1968 DeletionCause deletion_cause
) {
1969 lock_
.AssertAcquired();
1971 // Ideally, this would be asserted up where we define ChangeCauseMapping,
1972 // but DeletionCause's visibility (or lack thereof) forces us to make
1974 static_assert(arraysize(ChangeCauseMapping
) == DELETE_COOKIE_LAST_ENTRY
+ 1,
1975 "ChangeCauseMapping size should match DeletionCause size");
1977 // See InitializeHistograms() for details.
1978 if (deletion_cause
!= DELETE_COOKIE_DONT_RECORD
)
1979 histogram_cookie_deletion_cause_
->Add(deletion_cause
);
1981 CanonicalCookie
* cc
= it
->second
;
1982 VLOG(kVlogSetCookies
) << "InternalDeleteCookie()"
1983 << ", cause:" << deletion_cause
1984 << ", cc: " << cc
->DebugString();
1986 if ((cc
->IsPersistent() || persist_session_cookies_
) && store_
.get() &&
1988 store_
->DeleteCookie(*cc
);
1989 if (delegate_
.get()) {
1990 ChangeCausePair mapping
= ChangeCauseMapping
[deletion_cause
];
1993 delegate_
->OnCookieChanged(*cc
, true, mapping
.cause
);
1995 RunCallbacks(*cc
, true);
2000 // Domain expiry behavior is unchanged by key/expiry scheme (the
2001 // meaning of the key is different, but that's not visible to this routine).
2002 int CookieMonster::GarbageCollect(const Time
& current
, const std::string
& key
) {
2003 lock_
.AssertAcquired();
2005 int num_deleted
= 0;
2006 Time
safe_date(Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays
));
2008 // Collect garbage for this key, minding cookie priorities.
2009 if (cookies_
.count(key
) > kDomainMaxCookies
) {
2010 VLOG(kVlogGarbageCollection
) << "GarbageCollect() key: " << key
;
2012 CookieItVector cookie_its
;
2014 GarbageCollectExpired(current
, cookies_
.equal_range(key
), &cookie_its
);
2015 if (cookie_its
.size() > kDomainMaxCookies
) {
2016 VLOG(kVlogGarbageCollection
) << "Deep Garbage Collect domain.";
2018 cookie_its
.size() - (kDomainMaxCookies
- kDomainPurgeCookies
);
2019 DCHECK(purge_goal
> kDomainPurgeCookies
);
2021 // Boundary iterators into |cookie_its| for different priorities.
2022 CookieItVector::iterator it_bdd
[4];
2023 // Intialize |it_bdd| while sorting |cookie_its| by priorities.
2024 // Schematic: [MLLHMHHLMM] => [LLL|MMMM|HHH], with 4 boundaries.
2025 it_bdd
[0] = cookie_its
.begin();
2026 it_bdd
[3] = cookie_its
.end();
2028 PartitionCookieByPriority(it_bdd
[0], it_bdd
[3], COOKIE_PRIORITY_LOW
);
2029 it_bdd
[2] = PartitionCookieByPriority(it_bdd
[1], it_bdd
[3],
2030 COOKIE_PRIORITY_MEDIUM
);
2031 size_t quota
[3] = {kDomainCookiesQuotaLow
,
2032 kDomainCookiesQuotaMedium
,
2033 kDomainCookiesQuotaHigh
};
2035 // Purge domain cookies in 3 rounds.
2036 // Round 1: consider low-priority cookies only: evict least-recently
2037 // accessed, while protecting quota[0] of these from deletion.
2038 // Round 2: consider {low, medium}-priority cookies, evict least-recently
2039 // accessed, while protecting quota[0] + quota[1].
2040 // Round 3: consider all cookies, evict least-recently accessed.
2041 size_t accumulated_quota
= 0;
2042 CookieItVector::iterator it_purge_begin
= it_bdd
[0];
2043 for (int i
= 0; i
< 3 && purge_goal
> 0; ++i
) {
2044 accumulated_quota
+= quota
[i
];
2046 size_t num_considered
= it_bdd
[i
+ 1] - it_purge_begin
;
2047 if (num_considered
<= accumulated_quota
)
2050 // Number of cookies that will be purged in this round.
2052 std::min(purge_goal
, num_considered
- accumulated_quota
);
2053 purge_goal
-= round_goal
;
2055 SortLeastRecentlyAccessed(it_purge_begin
, it_bdd
[i
+ 1], round_goal
);
2056 // Cookies accessed on or after |safe_date| would have been safe from
2057 // global purge, and we want to keep track of this.
2058 CookieItVector::iterator it_purge_end
= it_purge_begin
+ round_goal
;
2059 CookieItVector::iterator it_purge_middle
=
2060 LowerBoundAccessDate(it_purge_begin
, it_purge_end
, safe_date
);
2061 // Delete cookies accessed before |safe_date|.
2062 num_deleted
+= GarbageCollectDeleteRange(
2063 current
, DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
, it_purge_begin
,
2065 // Delete cookies accessed on or after |safe_date|.
2066 num_deleted
+= GarbageCollectDeleteRange(
2067 current
, DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
, it_purge_middle
,
2069 it_purge_begin
= it_purge_end
;
2071 DCHECK_EQ(0U, purge_goal
);
2075 // Collect garbage for everything. With firefox style we want to preserve
2076 // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict.
2077 if (cookies_
.size() > kMaxCookies
&& earliest_access_time_
< safe_date
) {
2078 VLOG(kVlogGarbageCollection
) << "GarbageCollect() everything";
2079 CookieItVector cookie_its
;
2080 num_deleted
+= GarbageCollectExpired(
2081 current
, CookieMapItPair(cookies_
.begin(), cookies_
.end()),
2083 if (cookie_its
.size() > kMaxCookies
) {
2084 VLOG(kVlogGarbageCollection
) << "Deep Garbage Collect everything.";
2085 size_t purge_goal
= cookie_its
.size() - (kMaxCookies
- kPurgeCookies
);
2086 DCHECK(purge_goal
> kPurgeCookies
);
2087 // Sorts up to *and including* |cookie_its[purge_goal]|, so
2088 // |earliest_access_time| will be properly assigned even if
2089 // |global_purge_it| == |cookie_its.begin() + purge_goal|.
2090 SortLeastRecentlyAccessed(cookie_its
.begin(), cookie_its
.end(),
2092 // Find boundary to cookies older than safe_date.
2093 CookieItVector::iterator global_purge_it
= LowerBoundAccessDate(
2094 cookie_its
.begin(), cookie_its
.begin() + purge_goal
, safe_date
);
2095 // Only delete the old cookies.
2097 GarbageCollectDeleteRange(current
, DELETE_COOKIE_EVICTED_GLOBAL
,
2098 cookie_its
.begin(), global_purge_it
);
2099 // Set access day to the oldest cookie that wasn't deleted.
2100 earliest_access_time_
= (*global_purge_it
)->second
->LastAccessDate();
2107 int CookieMonster::GarbageCollectExpired(const Time
& current
,
2108 const CookieMapItPair
& itpair
,
2109 CookieItVector
* cookie_its
) {
2110 // TODO(xiyuan): Remove the log after http://crbug.com/449816.
2111 VLOG(kVlogSetCookies
) << "CookieMonster::GarbageCollectExpired";
2113 if (keep_expired_cookies_
)
2116 lock_
.AssertAcquired();
2118 int num_deleted
= 0;
2119 for (CookieMap::iterator it
= itpair
.first
, end
= itpair
.second
; it
!= end
;) {
2120 CookieMap::iterator curit
= it
;
2123 if (curit
->second
->IsExpired(current
)) {
2124 InternalDeleteCookie(curit
, true, DELETE_COOKIE_EXPIRED
);
2126 } else if (cookie_its
) {
2127 cookie_its
->push_back(curit
);
2134 int CookieMonster::GarbageCollectDeleteRange(const Time
& current
,
2135 DeletionCause cause
,
2136 CookieItVector::iterator it_begin
,
2137 CookieItVector::iterator it_end
) {
2138 // TODO(xiyuan): Remove the log after http://crbug.com/449816.
2139 VLOG(kVlogSetCookies
) << "CookieMonster::GarbageCollectDeleteRange";
2141 for (CookieItVector::iterator it
= it_begin
; it
!= it_end
; it
++) {
2142 histogram_evicted_last_access_minutes_
->Add(
2143 (current
- (*it
)->second
->LastAccessDate()).InMinutes());
2144 InternalDeleteCookie((*it
), true, cause
);
2146 return it_end
- it_begin
;
2149 // A wrapper around registry_controlled_domains::GetDomainAndRegistry
2150 // to make clear we're creating a key for our local map. Here and
2151 // in FindCookiesForHostAndDomain() are the only two places where
2152 // we need to conditionalize based on key type.
2154 // Note that this key algorithm explicitly ignores the scheme. This is
2155 // because when we're entering cookies into the map from the backing store,
2156 // we in general won't have the scheme at that point.
2157 // In practical terms, this means that file cookies will be stored
2158 // in the map either by an empty string or by UNC name (and will be
2159 // limited by kMaxCookiesPerHost), and extension cookies will be stored
2160 // based on the single extension id, as the extension id won't have the
2161 // form of a DNS host and hence GetKey() will return it unchanged.
2163 // Arguably the right thing to do here is to make the key
2164 // algorithm dependent on the scheme, and make sure that the scheme is
2165 // available everywhere the key must be obtained (specfically at backing
2166 // store load time). This would require either changing the backing store
2167 // database schema to include the scheme (far more trouble than it's worth), or
2168 // separating out file cookies into their own CookieMonster instance and
2169 // thus restricting each scheme to a single cookie monster (which might
2170 // be worth it, but is still too much trouble to solve what is currently a
2172 std::string
CookieMonster::GetKey(const std::string
& domain
) const {
2173 std::string
effective_domain(
2174 registry_controlled_domains::GetDomainAndRegistry(
2175 domain
, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
));
2176 if (effective_domain
.empty())
2177 effective_domain
= domain
;
2179 if (!effective_domain
.empty() && effective_domain
[0] == '.')
2180 return effective_domain
.substr(1);
2181 return effective_domain
;
2184 bool CookieMonster::IsCookieableScheme(const std::string
& scheme
) {
2185 base::AutoLock
autolock(lock_
);
2187 return std::find(cookieable_schemes_
.begin(), cookieable_schemes_
.end(),
2188 scheme
) != cookieable_schemes_
.end();
2191 bool CookieMonster::HasCookieableScheme(const GURL
& url
) {
2192 lock_
.AssertAcquired();
2194 // Make sure the request is on a cookie-able url scheme.
2195 for (size_t i
= 0; i
< cookieable_schemes_
.size(); ++i
) {
2196 // We matched a scheme.
2197 if (url
.SchemeIs(cookieable_schemes_
[i
].c_str())) {
2198 // We've matched a supported scheme.
2203 // The scheme didn't match any in our whitelist.
2204 VLOG(kVlogPerCookieMonster
)
2205 << "WARNING: Unsupported cookie scheme: " << url
.scheme();
2209 // Test to see if stats should be recorded, and record them if so.
2210 // The goal here is to get sampling for the average browser-hour of
2211 // activity. We won't take samples when the web isn't being surfed,
2212 // and when the web is being surfed, we'll take samples about every
2213 // kRecordStatisticsIntervalSeconds.
2214 // last_statistic_record_time_ is initialized to Now() rather than null
2215 // in the constructor so that we won't take statistics right after
2216 // startup, to avoid bias from browsers that are started but not used.
2217 void CookieMonster::RecordPeriodicStats(const base::Time
& current_time
) {
2218 const base::TimeDelta
kRecordStatisticsIntervalTime(
2219 base::TimeDelta::FromSeconds(kRecordStatisticsIntervalSeconds
));
2221 // If we've taken statistics recently, return.
2222 if (current_time
- last_statistic_record_time_
<=
2223 kRecordStatisticsIntervalTime
) {
2227 // See InitializeHistograms() for details.
2228 histogram_count_
->Add(cookies_
.size());
2230 // More detailed statistics on cookie counts at different granularities.
2231 TimeTicks
beginning_of_time(TimeTicks::Now());
2233 for (CookieMap::const_iterator it_key
= cookies_
.begin();
2234 it_key
!= cookies_
.end();) {
2235 const std::string
& key(it_key
->first
);
2238 typedef std::map
<std::string
, unsigned int> DomainMap
;
2239 DomainMap domain_map
;
2240 CookieMapItPair its_cookies
= cookies_
.equal_range(key
);
2241 while (its_cookies
.first
!= its_cookies
.second
) {
2243 const std::string
& cookie_domain(its_cookies
.first
->second
->Domain());
2244 domain_map
[cookie_domain
]++;
2246 its_cookies
.first
++;
2248 histogram_etldp1_count_
->Add(key_count
);
2249 histogram_domain_per_etldp1_count_
->Add(domain_map
.size());
2250 for (DomainMap::const_iterator domain_map_it
= domain_map
.begin();
2251 domain_map_it
!= domain_map
.end(); domain_map_it
++)
2252 histogram_domain_count_
->Add(domain_map_it
->second
);
2254 it_key
= its_cookies
.second
;
2257 VLOG(kVlogPeriodic
) << "Time for recording cookie stats (us): "
2258 << (TimeTicks::Now() - beginning_of_time
)
2261 last_statistic_record_time_
= current_time
;
2264 // Initialize all histogram counter variables used in this class.
2266 // Normal histogram usage involves using the macros defined in
2267 // histogram.h, which automatically takes care of declaring these
2268 // variables (as statics), initializing them, and accumulating into
2269 // them, all from a single entry point. Unfortunately, that solution
2270 // doesn't work for the CookieMonster, as it's vulnerable to races between
2271 // separate threads executing the same functions and hence initializing the
2272 // same static variables. There isn't a race danger in the histogram
2273 // accumulation calls; they are written to be resilient to simultaneous
2274 // calls from multiple threads.
2276 // The solution taken here is to have per-CookieMonster instance
2277 // variables that are constructed during CookieMonster construction.
2278 // Note that these variables refer to the same underlying histogram,
2279 // so we still race (but safely) with other CookieMonster instances
2280 // for accumulation.
2282 // To do this we've expanded out the individual histogram macros calls,
2283 // with declarations of the variables in the class decl, initialization here
2284 // (done from the class constructor) and direct calls to the accumulation
2285 // methods where needed. The specific histogram macro calls on which the
2286 // initialization is based are included in comments below.
2287 void CookieMonster::InitializeHistograms() {
2288 // From UMA_HISTOGRAM_CUSTOM_COUNTS
2289 histogram_expiration_duration_minutes_
= base::Histogram::FactoryGet(
2290 "Cookie.ExpirationDurationMinutes", 1, kMinutesInTenYears
, 50,
2291 base::Histogram::kUmaTargetedHistogramFlag
);
2292 histogram_between_access_interval_minutes_
= base::Histogram::FactoryGet(
2293 "Cookie.BetweenAccessIntervalMinutes", 1, kMinutesInTenYears
, 50,
2294 base::Histogram::kUmaTargetedHistogramFlag
);
2295 histogram_evicted_last_access_minutes_
= base::Histogram::FactoryGet(
2296 "Cookie.EvictedLastAccessMinutes", 1, kMinutesInTenYears
, 50,
2297 base::Histogram::kUmaTargetedHistogramFlag
);
2298 histogram_count_
= base::Histogram::FactoryGet(
2299 "Cookie.Count", 1, 4000, 50, base::Histogram::kUmaTargetedHistogramFlag
);
2300 histogram_domain_count_
=
2301 base::Histogram::FactoryGet("Cookie.DomainCount", 1, 4000, 50,
2302 base::Histogram::kUmaTargetedHistogramFlag
);
2303 histogram_etldp1_count_
=
2304 base::Histogram::FactoryGet("Cookie.Etldp1Count", 1, 4000, 50,
2305 base::Histogram::kUmaTargetedHistogramFlag
);
2306 histogram_domain_per_etldp1_count_
=
2307 base::Histogram::FactoryGet("Cookie.DomainPerEtldp1Count", 1, 4000, 50,
2308 base::Histogram::kUmaTargetedHistogramFlag
);
2310 // From UMA_HISTOGRAM_COUNTS_10000 & UMA_HISTOGRAM_CUSTOM_COUNTS
2311 histogram_number_duplicate_db_cookies_
=
2312 base::Histogram::FactoryGet("Net.NumDuplicateCookiesInDb", 1, 10000, 50,
2313 base::Histogram::kUmaTargetedHistogramFlag
);
2315 // From UMA_HISTOGRAM_ENUMERATION
2316 histogram_cookie_deletion_cause_
= base::LinearHistogram::FactoryGet(
2317 "Cookie.DeletionCause", 1, DELETE_COOKIE_LAST_ENTRY
- 1,
2318 DELETE_COOKIE_LAST_ENTRY
, base::Histogram::kUmaTargetedHistogramFlag
);
2319 histogram_cookie_type_
= base::LinearHistogram::FactoryGet(
2320 "Cookie.Type", 1, (1 << COOKIE_TYPE_LAST_ENTRY
) - 1,
2321 1 << COOKIE_TYPE_LAST_ENTRY
, base::Histogram::kUmaTargetedHistogramFlag
);
2323 // From UMA_HISTOGRAM_{CUSTOM_,}TIMES
2324 histogram_time_blocked_on_load_
= base::Histogram::FactoryTimeGet(
2325 "Cookie.TimeBlockedOnLoad", base::TimeDelta::FromMilliseconds(1),
2326 base::TimeDelta::FromMinutes(1), 50,
2327 base::Histogram::kUmaTargetedHistogramFlag
);
2330 // The system resolution is not high enough, so we can have multiple
2331 // set cookies that result in the same system time. When this happens, we
2332 // increment by one Time unit. Let's hope computers don't get too fast.
2333 Time
CookieMonster::CurrentTime() {
2334 return std::max(Time::Now(), Time::FromInternalValue(
2335 last_time_seen_
.ToInternalValue() + 1));
2338 void CookieMonster::ComputeCookieDiff(CookieList
* old_cookies
,
2339 CookieList
* new_cookies
,
2340 CookieList
* cookies_to_add
,
2341 CookieList
* cookies_to_delete
) {
2342 DCHECK(old_cookies
);
2343 DCHECK(new_cookies
);
2344 DCHECK(cookies_to_add
);
2345 DCHECK(cookies_to_delete
);
2346 DCHECK(cookies_to_add
->empty());
2347 DCHECK(cookies_to_delete
->empty());
2350 // A set ordered by FullDiffCookieSorter is also ordered by
2351 // PartialDiffCookieSorter.
2352 std::sort(old_cookies
->begin(), old_cookies
->end(), FullDiffCookieSorter
);
2353 std::sort(new_cookies
->begin(), new_cookies
->end(), FullDiffCookieSorter
);
2355 // Select any old cookie for deletion if no new cookie has the same name,
2356 // domain, and path.
2357 std::set_difference(
2358 old_cookies
->begin(), old_cookies
->end(), new_cookies
->begin(),
2360 std::inserter(*cookies_to_delete
, cookies_to_delete
->begin()),
2361 PartialDiffCookieSorter
);
2363 // Select any new cookie for addition (or update) if no old cookie is exactly
2365 std::set_difference(new_cookies
->begin(), new_cookies
->end(),
2366 old_cookies
->begin(), old_cookies
->end(),
2367 std::inserter(*cookies_to_add
, cookies_to_add
->begin()),
2368 FullDiffCookieSorter
);
2371 scoped_ptr
<CookieStore::CookieChangedSubscription
>
2372 CookieMonster::AddCallbackForCookie(const GURL
& gurl
,
2373 const std::string
& name
,
2374 const CookieChangedCallback
& callback
) {
2375 base::AutoLock
autolock(lock_
);
2376 std::pair
<GURL
, std::string
> key(gurl
, name
);
2377 if (hook_map_
.count(key
) == 0)
2378 hook_map_
[key
] = make_linked_ptr(new CookieChangedCallbackList());
2379 return hook_map_
[key
]->Add(
2380 base::Bind(&RunAsync
, base::MessageLoopProxy::current(), callback
));
2383 #if defined(OS_ANDROID)
2384 void CookieMonster::SetEnableFileScheme(bool accept
) {
2385 // This assumes "file" is always at the end of the array. See the comment
2386 // above kDefaultCookieableSchemes.
2388 // TODO(mkwst): We're keeping this method around to support the
2389 // 'CookieManager::setAcceptFileSchemeCookies' method on Android's WebView;
2390 // if/when we can deprecate and remove that method, we can remove this one
2391 // as well. Until then, we'll just ensure that the method has no effect on
2392 // non-android systems.
2393 int num_schemes
= accept
? kDefaultCookieableSchemesCount
2394 : kDefaultCookieableSchemesCount
- 1;
2396 SetCookieableSchemes(kDefaultCookieableSchemes
, num_schemes
);
2400 void CookieMonster::RunCallbacks(const CanonicalCookie
& cookie
, bool removed
) {
2401 lock_
.AssertAcquired();
2403 opts
.set_include_httponly();
2404 opts
.set_include_first_party_only();
2405 // Note that the callbacks in hook_map_ are wrapped with MakeAsync(), so they
2406 // are guaranteed to not take long - they just post a RunAsync task back to
2407 // the appropriate thread's message loop and return. It is important that this
2408 // method not run user-supplied callbacks directly, since the CookieMonster
2409 // lock is held and it is easy to accidentally introduce deadlocks.
2410 for (CookieChangedHookMap::iterator it
= hook_map_
.begin();
2411 it
!= hook_map_
.end(); ++it
) {
2412 std::pair
<GURL
, std::string
> key
= it
->first
;
2413 if (cookie
.IncludeForRequestURL(key
.first
, opts
) &&
2414 cookie
.Name() == key
.second
) {
2415 it
->second
->Notify(cookie
, removed
);