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 net::CanonicalCookie
& a
,
169 const net::CanonicalCookie
& b
) {
170 return a
.PartialCompare(b
);
173 // This is a stricter ordering than PartialDiffCookieOrdering, where all fields
175 bool FullDiffCookieSorter(const net::CanonicalCookie
& a
,
176 const net::CanonicalCookie
& b
) {
177 return a
.FullCompare(b
);
180 // Our strategy to find duplicates is:
181 // (1) Build a map from (cookiename, cookiepath) to
182 // {list of cookies with this signature, sorted by creation time}.
183 // (2) For each list with more than 1 entry, keep the cookie having the
184 // most recent creation time, and delete the others.
186 // Two cookies are considered equivalent if they have the same domain,
188 struct CookieSignature
{
190 CookieSignature(const std::string
& name
,
191 const std::string
& domain
,
192 const std::string
& path
)
193 : name(name
), domain(domain
), path(path
) {}
195 // To be a key for a map this class needs to be assignable, copyable,
196 // and have an operator<. The default assignment operator
197 // and copy constructor are exactly what we want.
199 bool operator<(const CookieSignature
& cs
) const {
200 // Name compare dominates, then domain, then path.
201 int diff
= name
.compare(cs
.name
);
205 diff
= domain
.compare(cs
.domain
);
209 return path
.compare(cs
.path
) < 0;
217 // For a CookieItVector iterator range [|it_begin|, |it_end|),
218 // sorts the first |num_sort| + 1 elements by LastAccessDate().
219 // The + 1 element exists so for any interval of length <= |num_sort| starting
220 // from |cookies_its_begin|, a LastAccessDate() bound can be found.
221 void SortLeastRecentlyAccessed(CookieMonster::CookieItVector::iterator it_begin
,
222 CookieMonster::CookieItVector::iterator it_end
,
224 DCHECK_LT(static_cast<int>(num_sort
), it_end
- it_begin
);
225 std::partial_sort(it_begin
, it_begin
+ num_sort
+ 1, it_end
, LRACookieSorter
);
228 // Predicate to support PartitionCookieByPriority().
229 struct CookiePriorityEqualsTo
230 : std::unary_function
<const CookieMonster::CookieMap::iterator
, bool> {
231 explicit CookiePriorityEqualsTo(CookiePriority priority
)
232 : priority_(priority
) {}
234 bool operator()(const CookieMonster::CookieMap::iterator it
) const {
235 return it
->second
->Priority() == priority_
;
238 const CookiePriority priority_
;
241 // For a CookieItVector iterator range [|it_begin|, |it_end|),
242 // moves all cookies with a given |priority| to the beginning of the list.
243 // Returns: An iterator in [it_begin, it_end) to the first element with
244 // priority != |priority|, or |it_end| if all have priority == |priority|.
245 CookieMonster::CookieItVector::iterator
PartitionCookieByPriority(
246 CookieMonster::CookieItVector::iterator it_begin
,
247 CookieMonster::CookieItVector::iterator it_end
,
248 CookiePriority priority
) {
249 return std::partition(it_begin
, it_end
, CookiePriorityEqualsTo(priority
));
252 bool LowerBoundAccessDateComparator(const CookieMonster::CookieMap::iterator it
,
253 const Time
& access_date
) {
254 return it
->second
->LastAccessDate() < access_date
;
257 // For a CookieItVector iterator range [|it_begin|, |it_end|)
258 // from a CookieItVector sorted by LastAccessDate(), returns the
259 // first iterator with access date >= |access_date|, or cookie_its_end if this
261 CookieMonster::CookieItVector::iterator
LowerBoundAccessDate(
262 const CookieMonster::CookieItVector::iterator its_begin
,
263 const CookieMonster::CookieItVector::iterator its_end
,
264 const Time
& access_date
) {
265 return std::lower_bound(its_begin
, its_end
, access_date
,
266 LowerBoundAccessDateComparator
);
269 // Mapping between DeletionCause and CookieMonsterDelegate::ChangeCause; the
270 // mapping also provides a boolean that specifies whether or not an
271 // OnCookieChanged notification ought to be generated.
272 typedef struct ChangeCausePair_struct
{
273 CookieMonsterDelegate::ChangeCause cause
;
276 ChangeCausePair ChangeCauseMapping
[] = {
277 // DELETE_COOKIE_EXPLICIT
278 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
, true},
279 // DELETE_COOKIE_OVERWRITE
280 {CookieMonsterDelegate::CHANGE_COOKIE_OVERWRITE
, true},
281 // DELETE_COOKIE_EXPIRED
282 {CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED
, true},
283 // DELETE_COOKIE_EVICTED
284 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
285 // DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE
286 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
, false},
287 // DELETE_COOKIE_DONT_RECORD
288 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
, false},
289 // DELETE_COOKIE_EVICTED_DOMAIN
290 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
291 // DELETE_COOKIE_EVICTED_GLOBAL
292 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
293 // DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
294 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
295 // DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
296 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
297 // DELETE_COOKIE_EXPIRED_OVERWRITE
298 {CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED_OVERWRITE
, true},
299 // DELETE_COOKIE_CONTROL_CHAR
300 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
301 // DELETE_COOKIE_LAST_ENTRY
302 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
, false}};
304 std::string
BuildCookieLine(const CanonicalCookieVector
& cookies
) {
305 std::string cookie_line
;
306 for (CanonicalCookieVector::const_iterator it
= cookies
.begin();
307 it
!= cookies
.end(); ++it
) {
308 if (it
!= cookies
.begin())
310 // In Mozilla if you set a cookie like AAAA, it will have an empty token
311 // and a value of AAAA. When it sends the cookie back, it will send AAAA,
312 // so we need to avoid sending =AAAA for a blank token value.
313 if (!(*it
)->Name().empty())
314 cookie_line
+= (*it
)->Name() + "=";
315 cookie_line
+= (*it
)->Value();
320 void RunAsync(scoped_refptr
<base::TaskRunner
> proxy
,
321 const CookieStore::CookieChangedCallback
& callback
,
322 const CanonicalCookie
& cookie
,
324 proxy
->PostTask(FROM_HERE
, base::Bind(callback
, cookie
, removed
));
329 CookieMonster::CookieMonster(PersistentCookieStore
* store
,
330 CookieMonsterDelegate
* delegate
)
331 : initialized_(false),
334 last_access_threshold_(
335 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds
)),
337 last_statistic_record_time_(Time::Now()),
338 keep_expired_cookies_(false),
339 persist_session_cookies_(false) {
340 InitializeHistograms();
341 SetDefaultCookieableSchemes();
344 CookieMonster::CookieMonster(PersistentCookieStore
* store
,
345 CookieMonsterDelegate
* delegate
,
346 int last_access_threshold_milliseconds
)
347 : initialized_(false),
350 last_access_threshold_(base::TimeDelta::FromMilliseconds(
351 last_access_threshold_milliseconds
)),
353 last_statistic_record_time_(base::Time::Now()),
354 keep_expired_cookies_(false),
355 persist_session_cookies_(false) {
356 InitializeHistograms();
357 SetDefaultCookieableSchemes();
360 // Task classes for queueing the coming request.
362 class CookieMonster::CookieMonsterTask
363 : public base::RefCountedThreadSafe
<CookieMonsterTask
> {
365 // Runs the task and invokes the client callback on the thread that
366 // originally constructed the task.
367 virtual void Run() = 0;
370 explicit CookieMonsterTask(CookieMonster
* cookie_monster
);
371 virtual ~CookieMonsterTask();
373 // Invokes the callback immediately, if the current thread is the one
374 // that originated the task, or queues the callback for execution on the
375 // appropriate thread. Maintains a reference to this CookieMonsterTask
376 // instance until the callback completes.
377 void InvokeCallback(base::Closure callback
);
379 CookieMonster
* cookie_monster() { return cookie_monster_
; }
382 friend class base::RefCountedThreadSafe
<CookieMonsterTask
>;
384 CookieMonster
* cookie_monster_
;
385 scoped_refptr
<base::MessageLoopProxy
> thread_
;
387 DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask
);
390 CookieMonster::CookieMonsterTask::CookieMonsterTask(
391 CookieMonster
* cookie_monster
)
392 : cookie_monster_(cookie_monster
),
393 thread_(base::MessageLoopProxy::current()) {
396 CookieMonster::CookieMonsterTask::~CookieMonsterTask() {
399 // Unfortunately, one cannot re-bind a Callback with parameters into a closure.
400 // Therefore, the closure passed to InvokeCallback is a clumsy binding of
401 // Callback::Run on a wrapped Callback instance. Since Callback is not
402 // reference counted, we bind to an instance that is a member of the
403 // CookieMonsterTask subclass. Then, we cannot simply post the callback to a
404 // message loop because the underlying instance may be destroyed (along with the
405 // CookieMonsterTask instance) in the interim. Therefore, we post a callback
406 // bound to the CookieMonsterTask, which *is* reference counted (thus preventing
407 // destruction of the original callback), and which invokes the closure (which
408 // invokes the original callback with the returned data).
409 void CookieMonster::CookieMonsterTask::InvokeCallback(base::Closure callback
) {
410 if (thread_
->BelongsToCurrentThread()) {
413 thread_
->PostTask(FROM_HERE
, base::Bind(&CookieMonsterTask::InvokeCallback
,
418 // Task class for SetCookieWithDetails call.
419 class CookieMonster::SetCookieWithDetailsTask
: public CookieMonsterTask
{
421 SetCookieWithDetailsTask(CookieMonster
* cookie_monster
,
423 const std::string
& name
,
424 const std::string
& value
,
425 const std::string
& domain
,
426 const std::string
& path
,
427 const base::Time
& expiration_time
,
430 bool first_party_only
,
431 CookiePriority priority
,
432 const SetCookiesCallback
& callback
)
433 : CookieMonsterTask(cookie_monster
),
439 expiration_time_(expiration_time
),
441 http_only_(http_only
),
442 first_party_only_(first_party_only
),
444 callback_(callback
) {}
446 // CookieMonsterTask:
450 ~SetCookieWithDetailsTask() override
{}
458 base::Time expiration_time_
;
461 bool first_party_only_
;
462 CookiePriority priority_
;
463 SetCookiesCallback callback_
;
465 DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask
);
468 void CookieMonster::SetCookieWithDetailsTask::Run() {
469 bool success
= this->cookie_monster()->SetCookieWithDetails(
470 url_
, name_
, value_
, domain_
, path_
, expiration_time_
, secure_
,
471 http_only_
, first_party_only_
, priority_
);
472 if (!callback_
.is_null()) {
473 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run
,
474 base::Unretained(&callback_
), success
));
478 // Task class for GetAllCookies call.
479 class CookieMonster::GetAllCookiesTask
: public CookieMonsterTask
{
481 GetAllCookiesTask(CookieMonster
* cookie_monster
,
482 const GetCookieListCallback
& callback
)
483 : CookieMonsterTask(cookie_monster
), callback_(callback
) {}
489 ~GetAllCookiesTask() override
{}
492 GetCookieListCallback callback_
;
494 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask
);
497 void CookieMonster::GetAllCookiesTask::Run() {
498 if (!callback_
.is_null()) {
499 CookieList cookies
= this->cookie_monster()->GetAllCookies();
500 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run
,
501 base::Unretained(&callback_
), cookies
));
505 // Task class for GetAllCookiesForURLWithOptions call.
506 class CookieMonster::GetAllCookiesForURLWithOptionsTask
507 : public CookieMonsterTask
{
509 GetAllCookiesForURLWithOptionsTask(CookieMonster
* cookie_monster
,
511 const CookieOptions
& options
,
512 const GetCookieListCallback
& callback
)
513 : CookieMonsterTask(cookie_monster
),
516 callback_(callback
) {}
518 // CookieMonsterTask:
522 ~GetAllCookiesForURLWithOptionsTask() override
{}
526 CookieOptions options_
;
527 GetCookieListCallback callback_
;
529 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesForURLWithOptionsTask
);
532 void CookieMonster::GetAllCookiesForURLWithOptionsTask::Run() {
533 if (!callback_
.is_null()) {
535 this->cookie_monster()->GetAllCookiesForURLWithOptions(url_
, options_
);
536 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run
,
537 base::Unretained(&callback_
), cookies
));
541 template <typename Result
>
542 struct CallbackType
{
543 typedef base::Callback
<void(Result
)> Type
;
547 struct CallbackType
<void> {
548 typedef base::Closure Type
;
551 // Base task class for Delete*Task.
552 template <typename Result
>
553 class CookieMonster::DeleteTask
: public CookieMonsterTask
{
555 DeleteTask(CookieMonster
* cookie_monster
,
556 const typename CallbackType
<Result
>::Type
& callback
)
557 : CookieMonsterTask(cookie_monster
), callback_(callback
) {}
559 // CookieMonsterTask:
560 virtual void Run() override
;
563 ~DeleteTask() override
;
566 // Runs the delete task and returns a result.
567 virtual Result
RunDeleteTask() = 0;
568 base::Closure
RunDeleteTaskAndBindCallback();
569 void FlushDone(const base::Closure
& callback
);
571 typename CallbackType
<Result
>::Type callback_
;
573 DISALLOW_COPY_AND_ASSIGN(DeleteTask
);
576 template <typename Result
>
577 CookieMonster::DeleteTask
<Result
>::~DeleteTask() {
580 template <typename Result
>
582 CookieMonster::DeleteTask
<Result
>::RunDeleteTaskAndBindCallback() {
583 Result result
= RunDeleteTask();
584 if (callback_
.is_null())
585 return base::Closure();
586 return base::Bind(callback_
, result
);
590 base::Closure
CookieMonster::DeleteTask
<void>::RunDeleteTaskAndBindCallback() {
595 template <typename Result
>
596 void CookieMonster::DeleteTask
<Result
>::Run() {
597 this->cookie_monster()->FlushStore(base::Bind(
598 &DeleteTask
<Result
>::FlushDone
, this, RunDeleteTaskAndBindCallback()));
601 template <typename Result
>
602 void CookieMonster::DeleteTask
<Result
>::FlushDone(
603 const base::Closure
& callback
) {
604 if (!callback
.is_null()) {
605 this->InvokeCallback(callback
);
609 // Task class for DeleteAll call.
610 class CookieMonster::DeleteAllTask
: public DeleteTask
<int> {
612 DeleteAllTask(CookieMonster
* cookie_monster
, const DeleteCallback
& callback
)
613 : DeleteTask
<int>(cookie_monster
, callback
) {}
616 int RunDeleteTask() override
;
619 ~DeleteAllTask() override
{}
622 DISALLOW_COPY_AND_ASSIGN(DeleteAllTask
);
625 int CookieMonster::DeleteAllTask::RunDeleteTask() {
626 return this->cookie_monster()->DeleteAll(true);
629 // Task class for DeleteAllCreatedBetween call.
630 class CookieMonster::DeleteAllCreatedBetweenTask
: public DeleteTask
<int> {
632 DeleteAllCreatedBetweenTask(CookieMonster
* cookie_monster
,
633 const Time
& delete_begin
,
634 const Time
& delete_end
,
635 const DeleteCallback
& callback
)
636 : DeleteTask
<int>(cookie_monster
, callback
),
637 delete_begin_(delete_begin
),
638 delete_end_(delete_end
) {}
641 int RunDeleteTask() override
;
644 ~DeleteAllCreatedBetweenTask() override
{}
650 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask
);
653 int CookieMonster::DeleteAllCreatedBetweenTask::RunDeleteTask() {
654 return this->cookie_monster()->DeleteAllCreatedBetween(delete_begin_
,
658 // Task class for DeleteAllForHost call.
659 class CookieMonster::DeleteAllForHostTask
: public DeleteTask
<int> {
661 DeleteAllForHostTask(CookieMonster
* cookie_monster
,
663 const DeleteCallback
& callback
)
664 : DeleteTask
<int>(cookie_monster
, callback
), url_(url
) {}
667 int RunDeleteTask() override
;
670 ~DeleteAllForHostTask() override
{}
675 DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask
);
678 int CookieMonster::DeleteAllForHostTask::RunDeleteTask() {
679 return this->cookie_monster()->DeleteAllForHost(url_
);
682 // Task class for DeleteAllCreatedBetweenForHost call.
683 class CookieMonster::DeleteAllCreatedBetweenForHostTask
684 : public DeleteTask
<int> {
686 DeleteAllCreatedBetweenForHostTask(CookieMonster
* cookie_monster
,
690 const DeleteCallback
& callback
)
691 : DeleteTask
<int>(cookie_monster
, callback
),
692 delete_begin_(delete_begin
),
693 delete_end_(delete_end
),
697 int RunDeleteTask() override
;
700 ~DeleteAllCreatedBetweenForHostTask() override
{}
707 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenForHostTask
);
710 int CookieMonster::DeleteAllCreatedBetweenForHostTask::RunDeleteTask() {
711 return this->cookie_monster()->DeleteAllCreatedBetweenForHost(
712 delete_begin_
, delete_end_
, url_
);
715 // Task class for DeleteCanonicalCookie call.
716 class CookieMonster::DeleteCanonicalCookieTask
: public DeleteTask
<bool> {
718 DeleteCanonicalCookieTask(CookieMonster
* cookie_monster
,
719 const CanonicalCookie
& cookie
,
720 const DeleteCookieCallback
& callback
)
721 : DeleteTask
<bool>(cookie_monster
, callback
), cookie_(cookie
) {}
724 bool RunDeleteTask() override
;
727 ~DeleteCanonicalCookieTask() override
{}
730 CanonicalCookie cookie_
;
732 DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask
);
735 bool CookieMonster::DeleteCanonicalCookieTask::RunDeleteTask() {
736 return this->cookie_monster()->DeleteCanonicalCookie(cookie_
);
739 // Task class for SetCookieWithOptions call.
740 class CookieMonster::SetCookieWithOptionsTask
: public CookieMonsterTask
{
742 SetCookieWithOptionsTask(CookieMonster
* cookie_monster
,
744 const std::string
& cookie_line
,
745 const CookieOptions
& options
,
746 const SetCookiesCallback
& callback
)
747 : CookieMonsterTask(cookie_monster
),
749 cookie_line_(cookie_line
),
751 callback_(callback
) {}
753 // CookieMonsterTask:
757 ~SetCookieWithOptionsTask() override
{}
761 std::string cookie_line_
;
762 CookieOptions options_
;
763 SetCookiesCallback callback_
;
765 DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask
);
768 void CookieMonster::SetCookieWithOptionsTask::Run() {
769 // TODO(pkasting): Remove ScopedTracker below once crbug.com/456373 is fixed.
770 tracked_objects::ScopedTracker
tracking_profile(
771 FROM_HERE_WITH_EXPLICIT_FUNCTION(
772 "456373 CookieMonster::SetCookieWithOptionsTask::Run"));
773 bool result
= this->cookie_monster()->SetCookieWithOptions(url_
, cookie_line_
,
775 if (!callback_
.is_null()) {
776 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run
,
777 base::Unretained(&callback_
), result
));
781 // Task class for SetAllCookies call.
782 class CookieMonster::SetAllCookiesTask
: public CookieMonsterTask
{
784 SetAllCookiesTask(CookieMonster
* cookie_monster
,
785 const CookieList
& list
,
786 const SetCookiesCallback
& callback
)
787 : CookieMonsterTask(cookie_monster
), list_(list
), callback_(callback
) {}
789 // CookieMonsterTask:
793 ~SetAllCookiesTask() override
{}
797 SetCookiesCallback callback_
;
799 DISALLOW_COPY_AND_ASSIGN(SetAllCookiesTask
);
802 void CookieMonster::SetAllCookiesTask::Run() {
803 CookieList positive_diff
;
804 CookieList negative_diff
;
805 CookieList old_cookies
= this->cookie_monster()->GetAllCookies();
806 this->cookie_monster()->ComputeCookieDiff(&old_cookies
, &list_
,
807 &positive_diff
, &negative_diff
);
809 for (CookieList::const_iterator it
= negative_diff
.begin();
810 it
!= negative_diff
.end(); ++it
) {
811 this->cookie_monster()->DeleteCanonicalCookie(*it
);
815 if (positive_diff
.size() > 0)
816 result
= this->cookie_monster()->SetCanonicalCookies(list_
);
818 if (!callback_
.is_null()) {
819 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run
,
820 base::Unretained(&callback_
), result
));
824 // Task class for GetCookiesWithOptions call.
825 class CookieMonster::GetCookiesWithOptionsTask
: public CookieMonsterTask
{
827 GetCookiesWithOptionsTask(CookieMonster
* cookie_monster
,
829 const CookieOptions
& options
,
830 const GetCookiesCallback
& callback
)
831 : CookieMonsterTask(cookie_monster
),
834 callback_(callback
) {}
836 // CookieMonsterTask:
840 ~GetCookiesWithOptionsTask() override
{}
844 CookieOptions options_
;
845 GetCookiesCallback callback_
;
847 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask
);
850 void CookieMonster::GetCookiesWithOptionsTask::Run() {
851 // TODO(pkasting): Remove ScopedTracker below once crbug.com/456373 is fixed.
852 tracked_objects::ScopedTracker
tracking_profile(
853 FROM_HERE_WITH_EXPLICIT_FUNCTION(
854 "456373 CookieMonster::GetCookiesWithOptionsTask::Run"));
856 this->cookie_monster()->GetCookiesWithOptions(url_
, options_
);
857 if (!callback_
.is_null()) {
858 this->InvokeCallback(base::Bind(&GetCookiesCallback::Run
,
859 base::Unretained(&callback_
), cookie
));
863 // Task class for DeleteCookie call.
864 class CookieMonster::DeleteCookieTask
: public DeleteTask
<void> {
866 DeleteCookieTask(CookieMonster
* cookie_monster
,
868 const std::string
& cookie_name
,
869 const base::Closure
& callback
)
870 : DeleteTask
<void>(cookie_monster
, callback
),
872 cookie_name_(cookie_name
) {}
875 void RunDeleteTask() override
;
878 ~DeleteCookieTask() override
{}
882 std::string cookie_name_
;
884 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask
);
887 void CookieMonster::DeleteCookieTask::RunDeleteTask() {
888 this->cookie_monster()->DeleteCookie(url_
, cookie_name_
);
891 // Task class for DeleteSessionCookies call.
892 class CookieMonster::DeleteSessionCookiesTask
: public DeleteTask
<int> {
894 DeleteSessionCookiesTask(CookieMonster
* cookie_monster
,
895 const DeleteCallback
& callback
)
896 : DeleteTask
<int>(cookie_monster
, callback
) {}
899 int RunDeleteTask() override
;
902 ~DeleteSessionCookiesTask() override
{}
905 DISALLOW_COPY_AND_ASSIGN(DeleteSessionCookiesTask
);
908 int CookieMonster::DeleteSessionCookiesTask::RunDeleteTask() {
909 return this->cookie_monster()->DeleteSessionCookies();
912 // Task class for HasCookiesForETLDP1Task call.
913 class CookieMonster::HasCookiesForETLDP1Task
: public CookieMonsterTask
{
915 HasCookiesForETLDP1Task(CookieMonster
* cookie_monster
,
916 const std::string
& etldp1
,
917 const HasCookiesForETLDP1Callback
& callback
)
918 : CookieMonsterTask(cookie_monster
),
920 callback_(callback
) {}
922 // CookieMonsterTask:
926 ~HasCookiesForETLDP1Task() override
{}
930 HasCookiesForETLDP1Callback callback_
;
932 DISALLOW_COPY_AND_ASSIGN(HasCookiesForETLDP1Task
);
935 void CookieMonster::HasCookiesForETLDP1Task::Run() {
936 bool result
= this->cookie_monster()->HasCookiesForETLDP1(etldp1_
);
937 if (!callback_
.is_null()) {
938 this->InvokeCallback(base::Bind(&HasCookiesForETLDP1Callback::Run
,
939 base::Unretained(&callback_
), result
));
943 // Asynchronous CookieMonster API
945 void CookieMonster::SetCookieWithDetailsAsync(
947 const std::string
& name
,
948 const std::string
& value
,
949 const std::string
& domain
,
950 const std::string
& path
,
951 const Time
& expiration_time
,
954 bool first_party_only
,
955 CookiePriority priority
,
956 const SetCookiesCallback
& callback
) {
957 scoped_refptr
<SetCookieWithDetailsTask
> task
= new SetCookieWithDetailsTask(
958 this, url
, name
, value
, domain
, path
, expiration_time
, secure
, http_only
,
959 first_party_only
, priority
, callback
);
960 DoCookieTaskForURL(task
, url
);
963 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback
& callback
) {
964 scoped_refptr
<GetAllCookiesTask
> task
= new GetAllCookiesTask(this, callback
);
969 void CookieMonster::GetAllCookiesForURLWithOptionsAsync(
971 const CookieOptions
& options
,
972 const GetCookieListCallback
& callback
) {
973 scoped_refptr
<GetAllCookiesForURLWithOptionsTask
> task
=
974 new GetAllCookiesForURLWithOptionsTask(this, url
, options
, callback
);
976 DoCookieTaskForURL(task
, url
);
979 void CookieMonster::GetAllCookiesForURLAsync(
981 const GetCookieListCallback
& callback
) {
982 CookieOptions options
;
983 options
.set_include_httponly();
984 options
.set_include_first_party_only();
985 scoped_refptr
<GetAllCookiesForURLWithOptionsTask
> task
=
986 new GetAllCookiesForURLWithOptionsTask(this, url
, options
, callback
);
988 DoCookieTaskForURL(task
, url
);
991 void CookieMonster::HasCookiesForETLDP1Async(
992 const std::string
& etldp1
,
993 const HasCookiesForETLDP1Callback
& callback
) {
994 scoped_refptr
<HasCookiesForETLDP1Task
> task
=
995 new HasCookiesForETLDP1Task(this, etldp1
, callback
);
997 DoCookieTaskForURL(task
, GURL("http://" + etldp1
));
1000 void CookieMonster::DeleteAllAsync(const DeleteCallback
& callback
) {
1001 scoped_refptr
<DeleteAllTask
> task
= new DeleteAllTask(this, callback
);
1006 void CookieMonster::DeleteAllCreatedBetweenAsync(
1007 const Time
& delete_begin
,
1008 const Time
& delete_end
,
1009 const DeleteCallback
& callback
) {
1010 scoped_refptr
<DeleteAllCreatedBetweenTask
> task
=
1011 new DeleteAllCreatedBetweenTask(this, delete_begin
, delete_end
, callback
);
1016 void CookieMonster::DeleteAllCreatedBetweenForHostAsync(
1017 const Time delete_begin
,
1018 const Time delete_end
,
1020 const DeleteCallback
& callback
) {
1021 scoped_refptr
<DeleteAllCreatedBetweenForHostTask
> task
=
1022 new DeleteAllCreatedBetweenForHostTask(this, delete_begin
, delete_end
,
1025 DoCookieTaskForURL(task
, url
);
1028 void CookieMonster::DeleteAllForHostAsync(const GURL
& url
,
1029 const DeleteCallback
& callback
) {
1030 scoped_refptr
<DeleteAllForHostTask
> task
=
1031 new DeleteAllForHostTask(this, url
, callback
);
1033 DoCookieTaskForURL(task
, url
);
1036 void CookieMonster::DeleteCanonicalCookieAsync(
1037 const CanonicalCookie
& cookie
,
1038 const DeleteCookieCallback
& callback
) {
1039 scoped_refptr
<DeleteCanonicalCookieTask
> task
=
1040 new DeleteCanonicalCookieTask(this, cookie
, callback
);
1045 void CookieMonster::SetAllCookiesAsync(const CookieList
& list
,
1046 const SetCookiesCallback
& callback
) {
1047 scoped_refptr
<SetAllCookiesTask
> task
=
1048 new SetAllCookiesTask(this, list
, callback
);
1052 void CookieMonster::SetCookieWithOptionsAsync(
1054 const std::string
& cookie_line
,
1055 const CookieOptions
& options
,
1056 const SetCookiesCallback
& callback
) {
1057 scoped_refptr
<SetCookieWithOptionsTask
> task
=
1058 new SetCookieWithOptionsTask(this, url
, cookie_line
, options
, callback
);
1060 DoCookieTaskForURL(task
, url
);
1063 void CookieMonster::GetCookiesWithOptionsAsync(
1065 const CookieOptions
& options
,
1066 const GetCookiesCallback
& callback
) {
1067 scoped_refptr
<GetCookiesWithOptionsTask
> task
=
1068 new GetCookiesWithOptionsTask(this, url
, options
, callback
);
1070 DoCookieTaskForURL(task
, url
);
1073 void CookieMonster::DeleteCookieAsync(const GURL
& url
,
1074 const std::string
& cookie_name
,
1075 const base::Closure
& callback
) {
1076 scoped_refptr
<DeleteCookieTask
> task
=
1077 new DeleteCookieTask(this, url
, cookie_name
, callback
);
1079 DoCookieTaskForURL(task
, url
);
1082 void CookieMonster::DeleteSessionCookiesAsync(
1083 const CookieStore::DeleteCallback
& callback
) {
1084 scoped_refptr
<DeleteSessionCookiesTask
> task
=
1085 new DeleteSessionCookiesTask(this, callback
);
1090 void CookieMonster::DoCookieTask(
1091 const scoped_refptr
<CookieMonsterTask
>& task_item
) {
1093 base::AutoLock
autolock(lock_
);
1096 tasks_pending_
.push(task_item
);
1104 void CookieMonster::DoCookieTaskForURL(
1105 const scoped_refptr
<CookieMonsterTask
>& task_item
,
1108 base::AutoLock
autolock(lock_
);
1110 // If cookies for the requested domain key (eTLD+1) have been loaded from DB
1111 // then run the task, otherwise load from DB.
1113 // Checks if the domain key has been loaded.
1115 cookie_util::GetEffectiveDomain(url
.scheme(), url
.host()));
1116 if (keys_loaded_
.find(key
) == keys_loaded_
.end()) {
1117 std::map
<std::string
,
1118 std::deque
<scoped_refptr
<CookieMonsterTask
>>>::iterator it
=
1119 tasks_pending_for_key_
.find(key
);
1120 if (it
== tasks_pending_for_key_
.end()) {
1121 store_
->LoadCookiesForKey(
1122 key
, base::Bind(&CookieMonster::OnKeyLoaded
, this, key
));
1123 it
= tasks_pending_for_key_
1124 .insert(std::make_pair(
1125 key
, std::deque
<scoped_refptr
<CookieMonsterTask
>>()))
1128 it
->second
.push_back(task_item
);
1136 bool CookieMonster::SetCookieWithDetails(const GURL
& url
,
1137 const std::string
& name
,
1138 const std::string
& value
,
1139 const std::string
& domain
,
1140 const std::string
& path
,
1141 const base::Time
& expiration_time
,
1144 bool first_party_only
,
1145 CookiePriority priority
) {
1146 base::AutoLock
autolock(lock_
);
1148 if (!HasCookieableScheme(url
))
1151 Time creation_time
= CurrentTime();
1152 last_time_seen_
= creation_time
;
1154 scoped_ptr
<CanonicalCookie
> cc
;
1155 cc
.reset(CanonicalCookie::Create(url
, name
, value
, domain
, path
,
1156 creation_time
, expiration_time
, secure
,
1157 http_only
, first_party_only
, priority
));
1162 CookieOptions options
;
1163 options
.set_include_httponly();
1164 options
.set_include_first_party_only();
1165 return SetCanonicalCookie(&cc
, creation_time
, options
);
1168 bool CookieMonster::ImportCookies(const CookieList
& list
) {
1169 base::AutoLock
autolock(lock_
);
1171 for (net::CookieList::const_iterator iter
= list
.begin(); iter
!= list
.end();
1173 scoped_ptr
<CanonicalCookie
> cookie(new CanonicalCookie(*iter
));
1174 net::CookieOptions options
;
1175 options
.set_include_httponly();
1176 options
.set_include_first_party_only();
1177 if (!SetCanonicalCookie(&cookie
, cookie
->CreationDate(), options
))
1183 CookieList
CookieMonster::GetAllCookies() {
1184 base::AutoLock
autolock(lock_
);
1186 // This function is being called to scrape the cookie list for management UI
1187 // or similar. We shouldn't show expired cookies in this list since it will
1188 // just be confusing to users, and this function is called rarely enough (and
1189 // is already slow enough) that it's OK to take the time to garbage collect
1190 // the expired cookies now.
1192 // Note that this does not prune cookies to be below our limits (if we've
1193 // exceeded them) the way that calling GarbageCollect() would.
1194 GarbageCollectExpired(
1195 Time::Now(), CookieMapItPair(cookies_
.begin(), cookies_
.end()), NULL
);
1197 // Copy the CanonicalCookie pointers from the map so that we can use the same
1198 // sorter as elsewhere, then copy the result out.
1199 std::vector
<CanonicalCookie
*> cookie_ptrs
;
1200 cookie_ptrs
.reserve(cookies_
.size());
1201 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end(); ++it
)
1202 cookie_ptrs
.push_back(it
->second
);
1203 std::sort(cookie_ptrs
.begin(), cookie_ptrs
.end(), CookieSorter
);
1205 CookieList cookie_list
;
1206 cookie_list
.reserve(cookie_ptrs
.size());
1207 for (std::vector
<CanonicalCookie
*>::const_iterator it
= cookie_ptrs
.begin();
1208 it
!= cookie_ptrs
.end(); ++it
)
1209 cookie_list
.push_back(**it
);
1214 CookieList
CookieMonster::GetAllCookiesForURLWithOptions(
1216 const CookieOptions
& options
) {
1217 base::AutoLock
autolock(lock_
);
1219 std::vector
<CanonicalCookie
*> cookie_ptrs
;
1220 FindCookiesForHostAndDomain(url
, options
, false, &cookie_ptrs
);
1221 std::sort(cookie_ptrs
.begin(), cookie_ptrs
.end(), CookieSorter
);
1224 cookies
.reserve(cookie_ptrs
.size());
1225 for (std::vector
<CanonicalCookie
*>::const_iterator it
= cookie_ptrs
.begin();
1226 it
!= cookie_ptrs
.end(); it
++)
1227 cookies
.push_back(**it
);
1232 CookieList
CookieMonster::GetAllCookiesForURL(const GURL
& url
) {
1233 CookieOptions options
;
1234 options
.set_include_httponly();
1235 options
.set_first_party_url(url
);
1237 return GetAllCookiesForURLWithOptions(url
, options
);
1240 int CookieMonster::DeleteAll(bool sync_to_store
) {
1241 base::AutoLock
autolock(lock_
);
1243 int num_deleted
= 0;
1244 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end();) {
1245 CookieMap::iterator curit
= it
;
1247 InternalDeleteCookie(curit
, sync_to_store
,
1249 ? DELETE_COOKIE_EXPLICIT
1250 : DELETE_COOKIE_DONT_RECORD
/* Destruction. */);
1257 int CookieMonster::DeleteAllCreatedBetween(const Time
& delete_begin
,
1258 const Time
& delete_end
) {
1259 base::AutoLock
autolock(lock_
);
1261 int num_deleted
= 0;
1262 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end();) {
1263 CookieMap::iterator curit
= it
;
1264 CanonicalCookie
* cc
= curit
->second
;
1267 if (cc
->CreationDate() >= delete_begin
&&
1268 (delete_end
.is_null() || cc
->CreationDate() < delete_end
)) {
1269 InternalDeleteCookie(curit
, true, /*sync_to_store*/
1270 DELETE_COOKIE_EXPLICIT
);
1278 int CookieMonster::DeleteAllCreatedBetweenForHost(const Time delete_begin
,
1279 const Time delete_end
,
1281 base::AutoLock
autolock(lock_
);
1283 if (!HasCookieableScheme(url
))
1286 const std::string
host(url
.host());
1288 // We store host cookies in the store by their canonical host name;
1289 // domain cookies are stored with a leading ".". So this is a pretty
1290 // simple lookup and per-cookie delete.
1291 int num_deleted
= 0;
1292 for (CookieMapItPair its
= cookies_
.equal_range(GetKey(host
));
1293 its
.first
!= its
.second
;) {
1294 CookieMap::iterator curit
= its
.first
;
1297 const CanonicalCookie
* const cc
= curit
->second
;
1299 // Delete only on a match as a host cookie.
1300 if (cc
->IsHostCookie() && cc
->IsDomainMatch(host
) &&
1301 cc
->CreationDate() >= delete_begin
&&
1302 // The assumption that null |delete_end| is equivalent to
1303 // Time::Max() is confusing.
1304 (delete_end
.is_null() || cc
->CreationDate() < delete_end
)) {
1307 InternalDeleteCookie(curit
, true, DELETE_COOKIE_EXPLICIT
);
1313 int CookieMonster::DeleteAllForHost(const GURL
& url
) {
1314 return DeleteAllCreatedBetweenForHost(Time(), Time::Max(), url
);
1317 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie
& cookie
) {
1318 base::AutoLock
autolock(lock_
);
1320 for (CookieMapItPair its
= cookies_
.equal_range(GetKey(cookie
.Domain()));
1321 its
.first
!= its
.second
; ++its
.first
) {
1322 // The creation date acts as our unique index...
1323 if (its
.first
->second
->CreationDate() == cookie
.CreationDate()) {
1324 InternalDeleteCookie(its
.first
, true, DELETE_COOKIE_EXPLICIT
);
1331 void CookieMonster::SetCookieableSchemes(const char* const schemes
[],
1332 size_t num_schemes
) {
1333 base::AutoLock
autolock(lock_
);
1335 // Cookieable Schemes must be set before first use of function.
1336 DCHECK(!initialized_
);
1338 cookieable_schemes_
.clear();
1339 cookieable_schemes_
.insert(cookieable_schemes_
.end(), schemes
,
1340 schemes
+ num_schemes
);
1343 void CookieMonster::SetKeepExpiredCookies() {
1344 keep_expired_cookies_
= true;
1347 void CookieMonster::FlushStore(const base::Closure
& callback
) {
1348 base::AutoLock
autolock(lock_
);
1349 if (initialized_
&& store_
.get())
1350 store_
->Flush(callback
);
1351 else if (!callback
.is_null())
1352 base::MessageLoop::current()->PostTask(FROM_HERE
, callback
);
1355 bool CookieMonster::SetCookieWithOptions(const GURL
& url
,
1356 const std::string
& cookie_line
,
1357 const CookieOptions
& options
) {
1358 base::AutoLock
autolock(lock_
);
1360 if (!HasCookieableScheme(url
)) {
1364 return SetCookieWithCreationTimeAndOptions(url
, cookie_line
, Time(), options
);
1367 std::string
CookieMonster::GetCookiesWithOptions(const GURL
& url
,
1368 const CookieOptions
& options
) {
1369 base::AutoLock
autolock(lock_
);
1371 if (!HasCookieableScheme(url
))
1372 return std::string();
1374 std::vector
<CanonicalCookie
*> cookies
;
1375 FindCookiesForHostAndDomain(url
, options
, true, &cookies
);
1376 std::sort(cookies
.begin(), cookies
.end(), CookieSorter
);
1378 std::string cookie_line
= BuildCookieLine(cookies
);
1380 VLOG(kVlogGetCookies
) << "GetCookies() result: " << cookie_line
;
1385 void CookieMonster::DeleteCookie(const GURL
& url
,
1386 const std::string
& cookie_name
) {
1387 base::AutoLock
autolock(lock_
);
1389 if (!HasCookieableScheme(url
))
1392 CookieOptions options
;
1393 options
.set_include_httponly();
1394 options
.set_include_first_party_only();
1395 // Get the cookies for this host and its domain(s).
1396 std::vector
<CanonicalCookie
*> cookies
;
1397 FindCookiesForHostAndDomain(url
, options
, true, &cookies
);
1398 std::set
<CanonicalCookie
*> matching_cookies
;
1400 for (std::vector
<CanonicalCookie
*>::const_iterator it
= cookies
.begin();
1401 it
!= cookies
.end(); ++it
) {
1402 if ((*it
)->Name() != cookie_name
)
1404 if (url
.path().find((*it
)->Path()))
1406 matching_cookies
.insert(*it
);
1409 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end();) {
1410 CookieMap::iterator curit
= it
;
1412 if (matching_cookies
.find(curit
->second
) != matching_cookies
.end()) {
1413 InternalDeleteCookie(curit
, true, DELETE_COOKIE_EXPLICIT
);
1418 int CookieMonster::DeleteSessionCookies() {
1419 base::AutoLock
autolock(lock_
);
1421 int num_deleted
= 0;
1422 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end();) {
1423 CookieMap::iterator curit
= it
;
1424 CanonicalCookie
* cc
= curit
->second
;
1427 if (!cc
->IsPersistent()) {
1428 InternalDeleteCookie(curit
, true, /*sync_to_store*/
1429 DELETE_COOKIE_EXPIRED
);
1437 bool CookieMonster::HasCookiesForETLDP1(const std::string
& etldp1
) {
1438 base::AutoLock
autolock(lock_
);
1440 const std::string
key(GetKey(etldp1
));
1442 CookieMapItPair its
= cookies_
.equal_range(key
);
1443 return its
.first
!= its
.second
;
1446 CookieMonster
* CookieMonster::GetCookieMonster() {
1450 // This function must be called before the CookieMonster is used.
1451 void CookieMonster::SetPersistSessionCookies(bool persist_session_cookies
) {
1452 DCHECK(!initialized_
);
1453 persist_session_cookies_
= persist_session_cookies
;
1456 void CookieMonster::SetForceKeepSessionState() {
1458 store_
->SetForceKeepSessionState();
1462 CookieMonster::~CookieMonster() {
1466 bool CookieMonster::SetCookieWithCreationTime(const GURL
& url
,
1467 const std::string
& cookie_line
,
1468 const base::Time
& creation_time
) {
1469 DCHECK(!store_
.get()) << "This method is only to be used by unit-tests.";
1470 base::AutoLock
autolock(lock_
);
1472 if (!HasCookieableScheme(url
)) {
1477 return SetCookieWithCreationTimeAndOptions(url
, cookie_line
, creation_time
,
1481 void CookieMonster::InitStore() {
1482 DCHECK(store_
.get()) << "Store must exist to initialize";
1484 // We bind in the current time so that we can report the wall-clock time for
1486 store_
->Load(base::Bind(&CookieMonster::OnLoaded
, this, TimeTicks::Now()));
1489 void CookieMonster::OnLoaded(TimeTicks beginning_time
,
1490 const std::vector
<CanonicalCookie
*>& cookies
) {
1491 StoreLoadedCookies(cookies
);
1492 histogram_time_blocked_on_load_
->AddTime(TimeTicks::Now() - beginning_time
);
1494 // Invoke the task queue of cookie request.
1498 void CookieMonster::OnKeyLoaded(const std::string
& key
,
1499 const std::vector
<CanonicalCookie
*>& cookies
) {
1500 // This function does its own separate locking.
1501 StoreLoadedCookies(cookies
);
1503 std::deque
<scoped_refptr
<CookieMonsterTask
>> tasks_pending_for_key
;
1505 // We need to do this repeatedly until no more tasks were added to the queue
1506 // during the period where we release the lock.
1509 base::AutoLock
autolock(lock_
);
1510 std::map
<std::string
,
1511 std::deque
<scoped_refptr
<CookieMonsterTask
>>>::iterator it
=
1512 tasks_pending_for_key_
.find(key
);
1513 if (it
== tasks_pending_for_key_
.end()) {
1514 keys_loaded_
.insert(key
);
1517 if (it
->second
.empty()) {
1518 keys_loaded_
.insert(key
);
1519 tasks_pending_for_key_
.erase(it
);
1522 it
->second
.swap(tasks_pending_for_key
);
1525 while (!tasks_pending_for_key
.empty()) {
1526 scoped_refptr
<CookieMonsterTask
> task
= tasks_pending_for_key
.front();
1528 tasks_pending_for_key
.pop_front();
1533 void CookieMonster::StoreLoadedCookies(
1534 const std::vector
<CanonicalCookie
*>& cookies
) {
1535 // Initialize the store and sync in any saved persistent cookies. We don't
1536 // care if it's expired, insert it so it can be garbage collected, removed,
1538 base::AutoLock
autolock(lock_
);
1540 CookieItVector cookies_with_control_chars
;
1542 for (std::vector
<CanonicalCookie
*>::const_iterator it
= cookies
.begin();
1543 it
!= cookies
.end(); ++it
) {
1544 int64 cookie_creation_time
= (*it
)->CreationDate().ToInternalValue();
1546 if (creation_times_
.insert(cookie_creation_time
).second
) {
1547 CookieMap::iterator inserted
=
1548 InternalInsertCookie(GetKey((*it
)->Domain()), *it
, false);
1549 const Time
cookie_access_time((*it
)->LastAccessDate());
1550 if (earliest_access_time_
.is_null() ||
1551 cookie_access_time
< earliest_access_time_
)
1552 earliest_access_time_
= cookie_access_time
;
1554 if (ContainsControlCharacter((*it
)->Name()) ||
1555 ContainsControlCharacter((*it
)->Value())) {
1556 cookies_with_control_chars
.push_back(inserted
);
1559 LOG(ERROR
) << base::StringPrintf(
1560 "Found cookies with duplicate creation "
1561 "times in backing store: "
1562 "{name='%s', domain='%s', path='%s'}",
1563 (*it
)->Name().c_str(), (*it
)->Domain().c_str(),
1564 (*it
)->Path().c_str());
1565 // We've been given ownership of the cookie and are throwing it
1566 // away; reclaim the space.
1571 // Any cookies that contain control characters that we have loaded from the
1572 // persistent store should be deleted. See http://crbug.com/238041.
1573 for (CookieItVector::iterator it
= cookies_with_control_chars
.begin();
1574 it
!= cookies_with_control_chars
.end();) {
1575 CookieItVector::iterator curit
= it
;
1578 InternalDeleteCookie(*curit
, true, DELETE_COOKIE_CONTROL_CHAR
);
1581 // After importing cookies from the PersistentCookieStore, verify that
1582 // none of our other constraints are violated.
1583 // In particular, the backing store might have given us duplicate cookies.
1585 // This method could be called multiple times due to priority loading, thus
1586 // cookies loaded in previous runs will be validated again, but this is OK
1587 // since they are expected to be much fewer than total DB.
1588 EnsureCookiesMapIsValid();
1591 void CookieMonster::InvokeQueue() {
1593 scoped_refptr
<CookieMonsterTask
> request_task
;
1595 base::AutoLock
autolock(lock_
);
1596 if (tasks_pending_
.empty()) {
1598 creation_times_
.clear();
1599 keys_loaded_
.clear();
1602 request_task
= tasks_pending_
.front();
1603 tasks_pending_
.pop();
1605 request_task
->Run();
1609 void CookieMonster::EnsureCookiesMapIsValid() {
1610 lock_
.AssertAcquired();
1612 int num_duplicates_trimmed
= 0;
1614 // Iterate through all the of the cookies, grouped by host.
1615 CookieMap::iterator prev_range_end
= cookies_
.begin();
1616 while (prev_range_end
!= cookies_
.end()) {
1617 CookieMap::iterator cur_range_begin
= prev_range_end
;
1618 const std::string key
= cur_range_begin
->first
; // Keep a copy.
1619 CookieMap::iterator cur_range_end
= cookies_
.upper_bound(key
);
1620 prev_range_end
= cur_range_end
;
1622 // Ensure no equivalent cookies for this host.
1623 num_duplicates_trimmed
+=
1624 TrimDuplicateCookiesForKey(key
, cur_range_begin
, cur_range_end
);
1627 // Record how many duplicates were found in the database.
1628 // See InitializeHistograms() for details.
1629 histogram_number_duplicate_db_cookies_
->Add(num_duplicates_trimmed
);
1632 int CookieMonster::TrimDuplicateCookiesForKey(const std::string
& key
,
1633 CookieMap::iterator begin
,
1634 CookieMap::iterator end
) {
1635 lock_
.AssertAcquired();
1637 // Set of cookies ordered by creation time.
1638 typedef std::set
<CookieMap::iterator
, OrderByCreationTimeDesc
> CookieSet
;
1640 // Helper map we populate to find the duplicates.
1641 typedef std::map
<CookieSignature
, CookieSet
> EquivalenceMap
;
1642 EquivalenceMap equivalent_cookies
;
1644 // The number of duplicate cookies that have been found.
1645 int num_duplicates
= 0;
1647 // Iterate through all of the cookies in our range, and insert them into
1648 // the equivalence map.
1649 for (CookieMap::iterator it
= begin
; it
!= end
; ++it
) {
1650 DCHECK_EQ(key
, it
->first
);
1651 CanonicalCookie
* cookie
= it
->second
;
1653 CookieSignature
signature(cookie
->Name(), cookie
->Domain(), cookie
->Path());
1654 CookieSet
& set
= equivalent_cookies
[signature
];
1656 // We found a duplicate!
1660 // We save the iterator into |cookies_| rather than the actual cookie
1661 // pointer, since we may need to delete it later.
1662 bool insert_success
= set
.insert(it
).second
;
1663 DCHECK(insert_success
)
1664 << "Duplicate creation times found in duplicate cookie name scan.";
1667 // If there were no duplicates, we are done!
1668 if (num_duplicates
== 0)
1671 // Make sure we find everything below that we did above.
1672 int num_duplicates_found
= 0;
1674 // Otherwise, delete all the duplicate cookies, both from our in-memory store
1675 // and from the backing store.
1676 for (EquivalenceMap::iterator it
= equivalent_cookies
.begin();
1677 it
!= equivalent_cookies
.end(); ++it
) {
1678 const CookieSignature
& signature
= it
->first
;
1679 CookieSet
& dupes
= it
->second
;
1681 if (dupes
.size() <= 1)
1682 continue; // This cookiename/path has no duplicates.
1683 num_duplicates_found
+= dupes
.size() - 1;
1685 // Since |dups| is sorted by creation time (descending), the first cookie
1686 // is the most recent one, so we will keep it. The rest are duplicates.
1687 dupes
.erase(dupes
.begin());
1689 LOG(ERROR
) << base::StringPrintf(
1690 "Found %d duplicate cookies for host='%s', "
1691 "with {name='%s', domain='%s', path='%s'}",
1692 static_cast<int>(dupes
.size()), key
.c_str(), signature
.name
.c_str(),
1693 signature
.domain
.c_str(), signature
.path
.c_str());
1695 // Remove all the cookies identified by |dupes|. It is valid to delete our
1696 // list of iterators one at a time, since |cookies_| is a multimap (they
1697 // don't invalidate existing iterators following deletion).
1698 for (CookieSet::iterator dupes_it
= dupes
.begin(); dupes_it
!= dupes
.end();
1700 InternalDeleteCookie(*dupes_it
, true,
1701 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE
);
1704 DCHECK_EQ(num_duplicates
, num_duplicates_found
);
1706 return num_duplicates
;
1709 // Note: file must be the last scheme.
1710 const char* const CookieMonster::kDefaultCookieableSchemes
[] = {"http",
1715 const int CookieMonster::kDefaultCookieableSchemesCount
=
1716 arraysize(kDefaultCookieableSchemes
);
1718 void CookieMonster::SetDefaultCookieableSchemes() {
1719 // Always disable file scheme unless SetEnableFileScheme(true) is called.
1720 SetCookieableSchemes(kDefaultCookieableSchemes
,
1721 kDefaultCookieableSchemesCount
- 1);
1724 void CookieMonster::FindCookiesForHostAndDomain(
1726 const CookieOptions
& options
,
1727 bool update_access_time
,
1728 std::vector
<CanonicalCookie
*>* cookies
) {
1729 lock_
.AssertAcquired();
1731 const Time
current_time(CurrentTime());
1733 // Probe to save statistics relatively frequently. We do it here rather
1734 // than in the set path as many websites won't set cookies, and we
1735 // want to collect statistics whenever the browser's being used.
1736 RecordPeriodicStats(current_time
);
1738 // Can just dispatch to FindCookiesForKey
1739 const std::string
key(GetKey(url
.host()));
1740 FindCookiesForKey(key
, url
, options
, current_time
, update_access_time
,
1744 void CookieMonster::FindCookiesForKey(const std::string
& key
,
1746 const CookieOptions
& options
,
1747 const Time
& current
,
1748 bool update_access_time
,
1749 std::vector
<CanonicalCookie
*>* cookies
) {
1750 lock_
.AssertAcquired();
1752 for (CookieMapItPair its
= cookies_
.equal_range(key
);
1753 its
.first
!= its
.second
;) {
1754 CookieMap::iterator curit
= its
.first
;
1755 CanonicalCookie
* cc
= curit
->second
;
1758 // If the cookie is expired, delete it.
1759 if (cc
->IsExpired(current
) && !keep_expired_cookies_
) {
1760 InternalDeleteCookie(curit
, true, DELETE_COOKIE_EXPIRED
);
1764 // Filter out cookies that should not be included for a request to the
1765 // given |url|. HTTP only cookies are filtered depending on the passed
1766 // cookie |options|.
1767 if (!cc
->IncludeForRequestURL(url
, options
))
1770 // Add this cookie to the set of matching cookies. Update the access
1771 // time if we've been requested to do so.
1772 if (update_access_time
) {
1773 InternalUpdateCookieAccessTime(cc
, current
);
1775 cookies
->push_back(cc
);
1779 bool CookieMonster::DeleteAnyEquivalentCookie(const std::string
& key
,
1780 const CanonicalCookie
& ecc
,
1782 bool already_expired
) {
1783 lock_
.AssertAcquired();
1785 bool found_equivalent_cookie
= false;
1786 bool skipped_httponly
= false;
1787 for (CookieMapItPair its
= cookies_
.equal_range(key
);
1788 its
.first
!= its
.second
;) {
1789 CookieMap::iterator curit
= its
.first
;
1790 CanonicalCookie
* cc
= curit
->second
;
1793 if (ecc
.IsEquivalent(*cc
)) {
1794 // We should never have more than one equivalent cookie, since they should
1795 // overwrite each other.
1796 CHECK(!found_equivalent_cookie
)
1797 << "Duplicate equivalent cookies found, cookie store is corrupted.";
1798 if (skip_httponly
&& cc
->IsHttpOnly()) {
1799 skipped_httponly
= true;
1801 InternalDeleteCookie(curit
, true, already_expired
1802 ? DELETE_COOKIE_EXPIRED_OVERWRITE
1803 : DELETE_COOKIE_OVERWRITE
);
1805 found_equivalent_cookie
= true;
1808 return skipped_httponly
;
1811 CookieMonster::CookieMap::iterator
CookieMonster::InternalInsertCookie(
1812 const std::string
& key
,
1813 CanonicalCookie
* cc
,
1814 bool sync_to_store
) {
1815 // TODO(pkasting): Remove ScopedTracker below once crbug.com/456373 is fixed.
1816 tracked_objects::ScopedTracker
tracking_profile(
1817 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1818 "456373 CookieMonster::InternalInsertCookie"));
1819 lock_
.AssertAcquired();
1821 if ((cc
->IsPersistent() || persist_session_cookies_
) && store_
.get() &&
1823 store_
->AddCookie(*cc
);
1824 CookieMap::iterator inserted
=
1825 cookies_
.insert(CookieMap::value_type(key
, cc
));
1826 if (delegate_
.get()) {
1827 delegate_
->OnCookieChanged(*cc
, false,
1828 CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
);
1831 // See InitializeHistograms() for details.
1832 int32_t sample
= cc
->IsFirstPartyOnly() ? 1 << COOKIE_TYPE_FIRSTPARTYONLY
: 0;
1833 sample
|= cc
->IsHttpOnly() ? 1 << COOKIE_TYPE_HTTPONLY
: 0;
1834 sample
|= cc
->IsSecure() ? 1 << COOKIE_TYPE_SECURE
: 0;
1835 histogram_cookie_type_
->Add(sample
);
1837 RunCallbacks(*cc
, false);
1842 bool CookieMonster::SetCookieWithCreationTimeAndOptions(
1844 const std::string
& cookie_line
,
1845 const Time
& creation_time_or_null
,
1846 const CookieOptions
& options
) {
1847 lock_
.AssertAcquired();
1849 VLOG(kVlogSetCookies
) << "SetCookie() line: " << cookie_line
;
1851 Time creation_time
= creation_time_or_null
;
1852 if (creation_time
.is_null()) {
1853 creation_time
= CurrentTime();
1854 last_time_seen_
= creation_time
;
1857 scoped_ptr
<CanonicalCookie
> cc(
1858 CanonicalCookie::Create(url
, cookie_line
, creation_time
, options
));
1861 VLOG(kVlogSetCookies
) << "WARNING: Failed to allocate CanonicalCookie";
1864 return SetCanonicalCookie(&cc
, creation_time
, options
);
1867 bool CookieMonster::SetCanonicalCookie(scoped_ptr
<CanonicalCookie
>* cc
,
1868 const Time
& creation_time
,
1869 const CookieOptions
& options
) {
1870 const std::string
key(GetKey((*cc
)->Domain()));
1871 bool already_expired
= (*cc
)->IsExpired(creation_time
);
1873 if (DeleteAnyEquivalentCookie(key
, **cc
, options
.exclude_httponly(),
1875 VLOG(kVlogSetCookies
) << "SetCookie() not clobbering httponly cookie";
1879 VLOG(kVlogSetCookies
) << "SetCookie() key: " << key
1880 << " cc: " << (*cc
)->DebugString();
1882 // Realize that we might be setting an expired cookie, and the only point
1883 // was to delete the cookie which we've already done.
1884 if (!already_expired
|| keep_expired_cookies_
) {
1885 // See InitializeHistograms() for details.
1886 if ((*cc
)->IsPersistent()) {
1887 histogram_expiration_duration_minutes_
->Add(
1888 ((*cc
)->ExpiryDate() - creation_time
).InMinutes());
1892 CanonicalCookie cookie
= *(cc
->get());
1893 InternalInsertCookie(key
, cc
->release(), true);
1896 VLOG(kVlogSetCookies
) << "SetCookie() not storing already expired cookie.";
1899 // We assume that hopefully setting a cookie will be less common than
1900 // querying a cookie. Since setting a cookie can put us over our limits,
1901 // make sure that we garbage collect... We can also make the assumption that
1902 // if a cookie was set, in the common case it will be used soon after,
1903 // and we will purge the expired cookies in GetCookies().
1904 GarbageCollect(creation_time
, key
);
1909 bool CookieMonster::SetCanonicalCookies(const CookieList
& list
) {
1910 base::AutoLock
autolock(lock_
);
1912 net::CookieOptions options
;
1913 options
.set_include_httponly();
1915 for (CookieList::const_iterator it
= list
.begin(); it
!= list
.end(); ++it
) {
1916 scoped_ptr
<CanonicalCookie
> canonical_cookie(new CanonicalCookie(*it
));
1917 if (!SetCanonicalCookie(&canonical_cookie
, it
->CreationDate(), options
))
1924 void CookieMonster::InternalUpdateCookieAccessTime(CanonicalCookie
* cc
,
1925 const Time
& current
) {
1926 lock_
.AssertAcquired();
1928 // Based off the Mozilla code. When a cookie has been accessed recently,
1929 // don't bother updating its access time again. This reduces the number of
1930 // updates we do during pageload, which in turn reduces the chance our storage
1931 // backend will hit its batch thresholds and be forced to update.
1932 if ((current
- cc
->LastAccessDate()) < last_access_threshold_
)
1935 // See InitializeHistograms() for details.
1936 histogram_between_access_interval_minutes_
->Add(
1937 (current
- cc
->LastAccessDate()).InMinutes());
1939 cc
->SetLastAccessDate(current
);
1940 if ((cc
->IsPersistent() || persist_session_cookies_
) && store_
.get())
1941 store_
->UpdateCookieAccessTime(*cc
);
1944 // InternalDeleteCookies must not invalidate iterators other than the one being
1946 void CookieMonster::InternalDeleteCookie(CookieMap::iterator it
,
1948 DeletionCause deletion_cause
) {
1949 lock_
.AssertAcquired();
1951 // Ideally, this would be asserted up where we define ChangeCauseMapping,
1952 // but DeletionCause's visibility (or lack thereof) forces us to make
1954 static_assert(arraysize(ChangeCauseMapping
) == DELETE_COOKIE_LAST_ENTRY
+ 1,
1955 "ChangeCauseMapping size should match DeletionCause size");
1957 // See InitializeHistograms() for details.
1958 if (deletion_cause
!= DELETE_COOKIE_DONT_RECORD
)
1959 histogram_cookie_deletion_cause_
->Add(deletion_cause
);
1961 CanonicalCookie
* cc
= it
->second
;
1962 VLOG(kVlogSetCookies
) << "InternalDeleteCookie() cc: " << cc
->DebugString();
1964 if ((cc
->IsPersistent() || persist_session_cookies_
) && store_
.get() &&
1966 store_
->DeleteCookie(*cc
);
1967 if (delegate_
.get()) {
1968 ChangeCausePair mapping
= ChangeCauseMapping
[deletion_cause
];
1971 delegate_
->OnCookieChanged(*cc
, true, mapping
.cause
);
1973 RunCallbacks(*cc
, true);
1978 // Domain expiry behavior is unchanged by key/expiry scheme (the
1979 // meaning of the key is different, but that's not visible to this routine).
1980 int CookieMonster::GarbageCollect(const Time
& current
, const std::string
& key
) {
1981 lock_
.AssertAcquired();
1983 int num_deleted
= 0;
1984 Time
safe_date(Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays
));
1986 // Collect garbage for this key, minding cookie priorities.
1987 if (cookies_
.count(key
) > kDomainMaxCookies
) {
1988 VLOG(kVlogGarbageCollection
) << "GarbageCollect() key: " << key
;
1990 CookieItVector cookie_its
;
1992 GarbageCollectExpired(current
, cookies_
.equal_range(key
), &cookie_its
);
1993 if (cookie_its
.size() > kDomainMaxCookies
) {
1994 VLOG(kVlogGarbageCollection
) << "Deep Garbage Collect domain.";
1996 cookie_its
.size() - (kDomainMaxCookies
- kDomainPurgeCookies
);
1997 DCHECK(purge_goal
> kDomainPurgeCookies
);
1999 // Boundary iterators into |cookie_its| for different priorities.
2000 CookieItVector::iterator it_bdd
[4];
2001 // Intialize |it_bdd| while sorting |cookie_its| by priorities.
2002 // Schematic: [MLLHMHHLMM] => [LLL|MMMM|HHH], with 4 boundaries.
2003 it_bdd
[0] = cookie_its
.begin();
2004 it_bdd
[3] = cookie_its
.end();
2006 PartitionCookieByPriority(it_bdd
[0], it_bdd
[3], COOKIE_PRIORITY_LOW
);
2007 it_bdd
[2] = PartitionCookieByPriority(it_bdd
[1], it_bdd
[3],
2008 COOKIE_PRIORITY_MEDIUM
);
2009 size_t quota
[3] = {kDomainCookiesQuotaLow
,
2010 kDomainCookiesQuotaMedium
,
2011 kDomainCookiesQuotaHigh
};
2013 // Purge domain cookies in 3 rounds.
2014 // Round 1: consider low-priority cookies only: evict least-recently
2015 // accessed, while protecting quota[0] of these from deletion.
2016 // Round 2: consider {low, medium}-priority cookies, evict least-recently
2017 // accessed, while protecting quota[0] + quota[1].
2018 // Round 3: consider all cookies, evict least-recently accessed.
2019 size_t accumulated_quota
= 0;
2020 CookieItVector::iterator it_purge_begin
= it_bdd
[0];
2021 for (int i
= 0; i
< 3 && purge_goal
> 0; ++i
) {
2022 accumulated_quota
+= quota
[i
];
2024 size_t num_considered
= it_bdd
[i
+ 1] - it_purge_begin
;
2025 if (num_considered
<= accumulated_quota
)
2028 // Number of cookies that will be purged in this round.
2030 std::min(purge_goal
, num_considered
- accumulated_quota
);
2031 purge_goal
-= round_goal
;
2033 SortLeastRecentlyAccessed(it_purge_begin
, it_bdd
[i
+ 1], round_goal
);
2034 // Cookies accessed on or after |safe_date| would have been safe from
2035 // global purge, and we want to keep track of this.
2036 CookieItVector::iterator it_purge_end
= it_purge_begin
+ round_goal
;
2037 CookieItVector::iterator it_purge_middle
=
2038 LowerBoundAccessDate(it_purge_begin
, it_purge_end
, safe_date
);
2039 // Delete cookies accessed before |safe_date|.
2040 num_deleted
+= GarbageCollectDeleteRange(
2041 current
, DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
, it_purge_begin
,
2043 // Delete cookies accessed on or after |safe_date|.
2044 num_deleted
+= GarbageCollectDeleteRange(
2045 current
, DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
, it_purge_middle
,
2047 it_purge_begin
= it_purge_end
;
2049 DCHECK_EQ(0U, purge_goal
);
2053 // Collect garbage for everything. With firefox style we want to preserve
2054 // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict.
2055 if (cookies_
.size() > kMaxCookies
&& earliest_access_time_
< safe_date
) {
2056 VLOG(kVlogGarbageCollection
) << "GarbageCollect() everything";
2057 CookieItVector cookie_its
;
2058 num_deleted
+= GarbageCollectExpired(
2059 current
, CookieMapItPair(cookies_
.begin(), cookies_
.end()),
2061 if (cookie_its
.size() > kMaxCookies
) {
2062 VLOG(kVlogGarbageCollection
) << "Deep Garbage Collect everything.";
2063 size_t purge_goal
= cookie_its
.size() - (kMaxCookies
- kPurgeCookies
);
2064 DCHECK(purge_goal
> kPurgeCookies
);
2065 // Sorts up to *and including* |cookie_its[purge_goal]|, so
2066 // |earliest_access_time| will be properly assigned even if
2067 // |global_purge_it| == |cookie_its.begin() + purge_goal|.
2068 SortLeastRecentlyAccessed(cookie_its
.begin(), cookie_its
.end(),
2070 // Find boundary to cookies older than safe_date.
2071 CookieItVector::iterator global_purge_it
= LowerBoundAccessDate(
2072 cookie_its
.begin(), cookie_its
.begin() + purge_goal
, safe_date
);
2073 // Only delete the old cookies.
2075 GarbageCollectDeleteRange(current
, DELETE_COOKIE_EVICTED_GLOBAL
,
2076 cookie_its
.begin(), global_purge_it
);
2077 // Set access day to the oldest cookie that wasn't deleted.
2078 earliest_access_time_
= (*global_purge_it
)->second
->LastAccessDate();
2085 int CookieMonster::GarbageCollectExpired(const Time
& current
,
2086 const CookieMapItPair
& itpair
,
2087 CookieItVector
* cookie_its
) {
2088 if (keep_expired_cookies_
)
2091 lock_
.AssertAcquired();
2093 int num_deleted
= 0;
2094 for (CookieMap::iterator it
= itpair
.first
, end
= itpair
.second
; it
!= end
;) {
2095 CookieMap::iterator curit
= it
;
2098 if (curit
->second
->IsExpired(current
)) {
2099 InternalDeleteCookie(curit
, true, DELETE_COOKIE_EXPIRED
);
2101 } else if (cookie_its
) {
2102 cookie_its
->push_back(curit
);
2109 int CookieMonster::GarbageCollectDeleteRange(const Time
& current
,
2110 DeletionCause cause
,
2111 CookieItVector::iterator it_begin
,
2112 CookieItVector::iterator it_end
) {
2113 for (CookieItVector::iterator it
= it_begin
; it
!= it_end
; it
++) {
2114 histogram_evicted_last_access_minutes_
->Add(
2115 (current
- (*it
)->second
->LastAccessDate()).InMinutes());
2116 InternalDeleteCookie((*it
), true, cause
);
2118 return it_end
- it_begin
;
2121 // A wrapper around registry_controlled_domains::GetDomainAndRegistry
2122 // to make clear we're creating a key for our local map. Here and
2123 // in FindCookiesForHostAndDomain() are the only two places where
2124 // we need to conditionalize based on key type.
2126 // Note that this key algorithm explicitly ignores the scheme. This is
2127 // because when we're entering cookies into the map from the backing store,
2128 // we in general won't have the scheme at that point.
2129 // In practical terms, this means that file cookies will be stored
2130 // in the map either by an empty string or by UNC name (and will be
2131 // limited by kMaxCookiesPerHost), and extension cookies will be stored
2132 // based on the single extension id, as the extension id won't have the
2133 // form of a DNS host and hence GetKey() will return it unchanged.
2135 // Arguably the right thing to do here is to make the key
2136 // algorithm dependent on the scheme, and make sure that the scheme is
2137 // available everywhere the key must be obtained (specfically at backing
2138 // store load time). This would require either changing the backing store
2139 // database schema to include the scheme (far more trouble than it's worth), or
2140 // separating out file cookies into their own CookieMonster instance and
2141 // thus restricting each scheme to a single cookie monster (which might
2142 // be worth it, but is still too much trouble to solve what is currently a
2144 std::string
CookieMonster::GetKey(const std::string
& domain
) const {
2145 std::string
effective_domain(
2146 registry_controlled_domains::GetDomainAndRegistry(
2147 domain
, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
));
2148 if (effective_domain
.empty())
2149 effective_domain
= domain
;
2151 if (!effective_domain
.empty() && effective_domain
[0] == '.')
2152 return effective_domain
.substr(1);
2153 return effective_domain
;
2156 bool CookieMonster::IsCookieableScheme(const std::string
& scheme
) {
2157 base::AutoLock
autolock(lock_
);
2159 return std::find(cookieable_schemes_
.begin(), cookieable_schemes_
.end(),
2160 scheme
) != cookieable_schemes_
.end();
2163 bool CookieMonster::HasCookieableScheme(const GURL
& url
) {
2164 lock_
.AssertAcquired();
2166 // Make sure the request is on a cookie-able url scheme.
2167 for (size_t i
= 0; i
< cookieable_schemes_
.size(); ++i
) {
2168 // We matched a scheme.
2169 if (url
.SchemeIs(cookieable_schemes_
[i
].c_str())) {
2170 // We've matched a supported scheme.
2175 // The scheme didn't match any in our whitelist.
2176 VLOG(kVlogPerCookieMonster
)
2177 << "WARNING: Unsupported cookie scheme: " << url
.scheme();
2181 // Test to see if stats should be recorded, and record them if so.
2182 // The goal here is to get sampling for the average browser-hour of
2183 // activity. We won't take samples when the web isn't being surfed,
2184 // and when the web is being surfed, we'll take samples about every
2185 // kRecordStatisticsIntervalSeconds.
2186 // last_statistic_record_time_ is initialized to Now() rather than null
2187 // in the constructor so that we won't take statistics right after
2188 // startup, to avoid bias from browsers that are started but not used.
2189 void CookieMonster::RecordPeriodicStats(const base::Time
& current_time
) {
2190 const base::TimeDelta
kRecordStatisticsIntervalTime(
2191 base::TimeDelta::FromSeconds(kRecordStatisticsIntervalSeconds
));
2193 // If we've taken statistics recently, return.
2194 if (current_time
- last_statistic_record_time_
<=
2195 kRecordStatisticsIntervalTime
) {
2199 // See InitializeHistograms() for details.
2200 histogram_count_
->Add(cookies_
.size());
2202 // More detailed statistics on cookie counts at different granularities.
2203 TimeTicks
beginning_of_time(TimeTicks::Now());
2205 for (CookieMap::const_iterator it_key
= cookies_
.begin();
2206 it_key
!= cookies_
.end();) {
2207 const std::string
& key(it_key
->first
);
2210 typedef std::map
<std::string
, unsigned int> DomainMap
;
2211 DomainMap domain_map
;
2212 CookieMapItPair its_cookies
= cookies_
.equal_range(key
);
2213 while (its_cookies
.first
!= its_cookies
.second
) {
2215 const std::string
& cookie_domain(its_cookies
.first
->second
->Domain());
2216 domain_map
[cookie_domain
]++;
2218 its_cookies
.first
++;
2220 histogram_etldp1_count_
->Add(key_count
);
2221 histogram_domain_per_etldp1_count_
->Add(domain_map
.size());
2222 for (DomainMap::const_iterator domain_map_it
= domain_map
.begin();
2223 domain_map_it
!= domain_map
.end(); domain_map_it
++)
2224 histogram_domain_count_
->Add(domain_map_it
->second
);
2226 it_key
= its_cookies
.second
;
2229 VLOG(kVlogPeriodic
) << "Time for recording cookie stats (us): "
2230 << (TimeTicks::Now() - beginning_of_time
)
2233 last_statistic_record_time_
= current_time
;
2236 // Initialize all histogram counter variables used in this class.
2238 // Normal histogram usage involves using the macros defined in
2239 // histogram.h, which automatically takes care of declaring these
2240 // variables (as statics), initializing them, and accumulating into
2241 // them, all from a single entry point. Unfortunately, that solution
2242 // doesn't work for the CookieMonster, as it's vulnerable to races between
2243 // separate threads executing the same functions and hence initializing the
2244 // same static variables. There isn't a race danger in the histogram
2245 // accumulation calls; they are written to be resilient to simultaneous
2246 // calls from multiple threads.
2248 // The solution taken here is to have per-CookieMonster instance
2249 // variables that are constructed during CookieMonster construction.
2250 // Note that these variables refer to the same underlying histogram,
2251 // so we still race (but safely) with other CookieMonster instances
2252 // for accumulation.
2254 // To do this we've expanded out the individual histogram macros calls,
2255 // with declarations of the variables in the class decl, initialization here
2256 // (done from the class constructor) and direct calls to the accumulation
2257 // methods where needed. The specific histogram macro calls on which the
2258 // initialization is based are included in comments below.
2259 void CookieMonster::InitializeHistograms() {
2260 // From UMA_HISTOGRAM_CUSTOM_COUNTS
2261 histogram_expiration_duration_minutes_
= base::Histogram::FactoryGet(
2262 "Cookie.ExpirationDurationMinutes", 1, kMinutesInTenYears
, 50,
2263 base::Histogram::kUmaTargetedHistogramFlag
);
2264 histogram_between_access_interval_minutes_
= base::Histogram::FactoryGet(
2265 "Cookie.BetweenAccessIntervalMinutes", 1, kMinutesInTenYears
, 50,
2266 base::Histogram::kUmaTargetedHistogramFlag
);
2267 histogram_evicted_last_access_minutes_
= base::Histogram::FactoryGet(
2268 "Cookie.EvictedLastAccessMinutes", 1, kMinutesInTenYears
, 50,
2269 base::Histogram::kUmaTargetedHistogramFlag
);
2270 histogram_count_
= base::Histogram::FactoryGet(
2271 "Cookie.Count", 1, 4000, 50, base::Histogram::kUmaTargetedHistogramFlag
);
2272 histogram_domain_count_
=
2273 base::Histogram::FactoryGet("Cookie.DomainCount", 1, 4000, 50,
2274 base::Histogram::kUmaTargetedHistogramFlag
);
2275 histogram_etldp1_count_
=
2276 base::Histogram::FactoryGet("Cookie.Etldp1Count", 1, 4000, 50,
2277 base::Histogram::kUmaTargetedHistogramFlag
);
2278 histogram_domain_per_etldp1_count_
=
2279 base::Histogram::FactoryGet("Cookie.DomainPerEtldp1Count", 1, 4000, 50,
2280 base::Histogram::kUmaTargetedHistogramFlag
);
2282 // From UMA_HISTOGRAM_COUNTS_10000 & UMA_HISTOGRAM_CUSTOM_COUNTS
2283 histogram_number_duplicate_db_cookies_
=
2284 base::Histogram::FactoryGet("Net.NumDuplicateCookiesInDb", 1, 10000, 50,
2285 base::Histogram::kUmaTargetedHistogramFlag
);
2287 // From UMA_HISTOGRAM_ENUMERATION
2288 histogram_cookie_deletion_cause_
= base::LinearHistogram::FactoryGet(
2289 "Cookie.DeletionCause", 1, DELETE_COOKIE_LAST_ENTRY
- 1,
2290 DELETE_COOKIE_LAST_ENTRY
, base::Histogram::kUmaTargetedHistogramFlag
);
2291 histogram_cookie_type_
= base::LinearHistogram::FactoryGet(
2292 "Cookie.Type", 1, COOKIE_TYPE_LAST_ENTRY
- 1, COOKIE_TYPE_LAST_ENTRY
,
2293 base::Histogram::kUmaTargetedHistogramFlag
);
2295 // From UMA_HISTOGRAM_{CUSTOM_,}TIMES
2296 histogram_time_blocked_on_load_
= base::Histogram::FactoryTimeGet(
2297 "Cookie.TimeBlockedOnLoad", base::TimeDelta::FromMilliseconds(1),
2298 base::TimeDelta::FromMinutes(1), 50,
2299 base::Histogram::kUmaTargetedHistogramFlag
);
2302 // The system resolution is not high enough, so we can have multiple
2303 // set cookies that result in the same system time. When this happens, we
2304 // increment by one Time unit. Let's hope computers don't get too fast.
2305 Time
CookieMonster::CurrentTime() {
2306 return std::max(Time::Now(), Time::FromInternalValue(
2307 last_time_seen_
.ToInternalValue() + 1));
2310 void CookieMonster::ComputeCookieDiff(CookieList
* old_cookies
,
2311 CookieList
* new_cookies
,
2312 CookieList
* cookies_to_add
,
2313 CookieList
* cookies_to_delete
) {
2314 DCHECK(old_cookies
);
2315 DCHECK(new_cookies
);
2316 DCHECK(cookies_to_add
);
2317 DCHECK(cookies_to_delete
);
2318 DCHECK(cookies_to_add
->empty());
2319 DCHECK(cookies_to_delete
->empty());
2322 // A set ordered by FullDiffCookieSorter is also ordered by
2323 // PartialDiffCookieSorter.
2324 std::sort(old_cookies
->begin(), old_cookies
->end(), FullDiffCookieSorter
);
2325 std::sort(new_cookies
->begin(), new_cookies
->end(), FullDiffCookieSorter
);
2327 // Select any old cookie for deletion if no new cookie has the same name,
2328 // domain, and path.
2329 std::set_difference(
2330 old_cookies
->begin(), old_cookies
->end(), new_cookies
->begin(),
2332 std::inserter(*cookies_to_delete
, cookies_to_delete
->begin()),
2333 PartialDiffCookieSorter
);
2335 // Select any new cookie for addition (or update) if no old cookie is exactly
2337 std::set_difference(new_cookies
->begin(), new_cookies
->end(),
2338 old_cookies
->begin(), old_cookies
->end(),
2339 std::inserter(*cookies_to_add
, cookies_to_add
->begin()),
2340 FullDiffCookieSorter
);
2343 scoped_ptr
<CookieStore::CookieChangedSubscription
>
2344 CookieMonster::AddCallbackForCookie(const GURL
& gurl
,
2345 const std::string
& name
,
2346 const CookieChangedCallback
& callback
) {
2347 base::AutoLock
autolock(lock_
);
2348 std::pair
<GURL
, std::string
> key(gurl
, name
);
2349 if (hook_map_
.count(key
) == 0)
2350 hook_map_
[key
] = make_linked_ptr(new CookieChangedCallbackList());
2351 return hook_map_
[key
]->Add(
2352 base::Bind(&RunAsync
, base::MessageLoopProxy::current(), callback
));
2355 #if defined(OS_ANDROID)
2356 void CookieMonster::SetEnableFileScheme(bool accept
) {
2357 // This assumes "file" is always at the end of the array. See the comment
2358 // above kDefaultCookieableSchemes.
2360 // TODO(mkwst): We're keeping this method around to support the
2361 // 'CookieManager::setAcceptFileSchemeCookies' method on Android's WebView;
2362 // if/when we can deprecate and remove that method, we can remove this one
2363 // as well. Until then, we'll just ensure that the method has no effect on
2364 // non-android systems.
2365 int num_schemes
= accept
? kDefaultCookieableSchemesCount
2366 : kDefaultCookieableSchemesCount
- 1;
2368 SetCookieableSchemes(kDefaultCookieableSchemes
, num_schemes
);
2372 void CookieMonster::RunCallbacks(const CanonicalCookie
& cookie
, bool removed
) {
2373 lock_
.AssertAcquired();
2375 opts
.set_include_httponly();
2376 opts
.set_include_first_party_only();
2377 // Note that the callbacks in hook_map_ are wrapped with MakeAsync(), so they
2378 // are guaranteed to not take long - they just post a RunAsync task back to
2379 // the appropriate thread's message loop and return. It is important that this
2380 // method not run user-supplied callbacks directly, since the CookieMonster
2381 // lock is held and it is easy to accidentally introduce deadlocks.
2382 for (CookieChangedHookMap::iterator it
= hook_map_
.begin();
2383 it
!= hook_map_
.end(); ++it
) {
2384 std::pair
<GURL
, std::string
> key
= it
->first
;
2385 if (cookie
.IncludeForRequestURL(key
.first
, opts
) &&
2386 cookie
.Name() == key
.second
) {
2387 it
->second
->Notify(cookie
, removed
);