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/location.h"
55 #include "base/logging.h"
56 #include "base/memory/scoped_ptr.h"
57 #include "base/metrics/field_trial.h"
58 #include "base/metrics/histogram.h"
59 #include "base/profiler/scoped_tracker.h"
60 #include "base/single_thread_task_runner.h"
61 #include "base/strings/string_util.h"
62 #include "base/strings/stringprintf.h"
63 #include "base/thread_task_runner_handle.h"
64 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
65 #include "net/cookies/canonical_cookie.h"
66 #include "net/cookies/cookie_util.h"
67 #include "net/cookies/parsed_cookie.h"
70 using base::TimeDelta
;
71 using base::TimeTicks
;
73 // In steady state, most cookie requests can be satisfied by the in memory
74 // cookie monster store. If the cookie request cannot be satisfied by the in
75 // memory store, the relevant cookies must be fetched from the persistent
76 // store. The task is queued in CookieMonster::tasks_pending_ if it requires
77 // all cookies to be loaded from the backend, or tasks_pending_for_key_ if it
78 // only requires all cookies associated with an eTLD+1.
80 // On the browser critical paths (e.g. for loading initial web pages in a
81 // session restore) it may take too long to wait for the full load. If a cookie
82 // request is for a specific URL, DoCookieTaskForURL is called, which triggers a
83 // priority load if the key is not loaded yet by calling PersistentCookieStore
84 // :: LoadCookiesForKey. The request is queued in
85 // CookieMonster::tasks_pending_for_key_ and executed upon receiving
86 // notification of key load completion via CookieMonster::OnKeyLoaded(). If
87 // multiple requests for the same eTLD+1 are received before key load
88 // completion, only the first request calls
89 // PersistentCookieStore::LoadCookiesForKey, all subsequent requests are queued
90 // in CookieMonster::tasks_pending_for_key_ and executed upon receiving
91 // notification of key load completion triggered by the first request for the
94 static const int kMinutesInTenYears
= 10 * 365 * 24 * 60;
98 const char kFetchWhenNecessaryName
[] = "FetchWhenNecessary";
99 const char kAlwaysFetchName
[] = "AlwaysFetch";
100 const char kCookieMonsterFetchStrategyName
[] = "CookieMonsterFetchStrategy";
106 // See comments at declaration of these variables in cookie_monster.h
108 const size_t CookieMonster::kDomainMaxCookies
= 180;
109 const size_t CookieMonster::kDomainPurgeCookies
= 30;
110 const size_t CookieMonster::kMaxCookies
= 3300;
111 const size_t CookieMonster::kPurgeCookies
= 300;
113 const size_t CookieMonster::kDomainCookiesQuotaLow
= 30;
114 const size_t CookieMonster::kDomainCookiesQuotaMedium
= 50;
115 const size_t CookieMonster::kDomainCookiesQuotaHigh
=
116 kDomainMaxCookies
- kDomainPurgeCookies
- kDomainCookiesQuotaLow
-
117 kDomainCookiesQuotaMedium
;
119 const int CookieMonster::kSafeFromGlobalPurgeDays
= 30;
123 bool ContainsControlCharacter(const std::string
& s
) {
124 for (std::string::const_iterator i
= s
.begin(); i
!= s
.end(); ++i
) {
125 if ((*i
>= 0) && (*i
<= 31))
132 typedef std::vector
<CanonicalCookie
*> CanonicalCookieVector
;
134 // Default minimum delay after updating a cookie's LastAccessDate before we
135 // will update it again.
136 const int kDefaultAccessUpdateThresholdSeconds
= 60;
138 // Comparator to sort cookies from highest creation date to lowest
140 struct OrderByCreationTimeDesc
{
141 bool operator()(const CookieMonster::CookieMap::iterator
& a
,
142 const CookieMonster::CookieMap::iterator
& b
) const {
143 return a
->second
->CreationDate() > b
->second
->CreationDate();
147 // Constants for use in VLOG
148 const int kVlogPerCookieMonster
= 1;
149 const int kVlogPeriodic
= 3;
150 const int kVlogGarbageCollection
= 5;
151 const int kVlogSetCookies
= 7;
152 const int kVlogGetCookies
= 9;
154 // Mozilla sorts on the path length (longest first), and then it
155 // sorts by creation time (oldest first).
156 // The RFC says the sort order for the domain attribute is undefined.
157 bool CookieSorter(CanonicalCookie
* cc1
, CanonicalCookie
* cc2
) {
158 if (cc1
->Path().length() == cc2
->Path().length())
159 return cc1
->CreationDate() < cc2
->CreationDate();
160 return cc1
->Path().length() > cc2
->Path().length();
163 bool LRACookieSorter(const CookieMonster::CookieMap::iterator
& it1
,
164 const CookieMonster::CookieMap::iterator
& it2
) {
165 // Cookies accessed less recently should be deleted first.
166 if (it1
->second
->LastAccessDate() != it2
->second
->LastAccessDate())
167 return it1
->second
->LastAccessDate() < it2
->second
->LastAccessDate();
169 // In rare cases we might have two cookies with identical last access times.
170 // To preserve the stability of the sort, in these cases prefer to delete
171 // older cookies over newer ones. CreationDate() is guaranteed to be unique.
172 return it1
->second
->CreationDate() < it2
->second
->CreationDate();
175 // Compare cookies using name, domain and path, so that "equivalent" cookies
176 // (per RFC 2965) are equal to each other.
177 bool PartialDiffCookieSorter(const CanonicalCookie
& a
,
178 const CanonicalCookie
& b
) {
179 return a
.PartialCompare(b
);
182 // This is a stricter ordering than PartialDiffCookieOrdering, where all fields
184 bool FullDiffCookieSorter(const CanonicalCookie
& a
, const CanonicalCookie
& b
) {
185 return a
.FullCompare(b
);
188 // Our strategy to find duplicates is:
189 // (1) Build a map from (cookiename, cookiepath) to
190 // {list of cookies with this signature, sorted by creation time}.
191 // (2) For each list with more than 1 entry, keep the cookie having the
192 // most recent creation time, and delete the others.
194 // Two cookies are considered equivalent if they have the same domain,
196 struct CookieSignature
{
198 CookieSignature(const std::string
& name
,
199 const std::string
& domain
,
200 const std::string
& path
)
201 : name(name
), domain(domain
), path(path
) {}
203 // To be a key for a map this class needs to be assignable, copyable,
204 // and have an operator<. The default assignment operator
205 // and copy constructor are exactly what we want.
207 bool operator<(const CookieSignature
& cs
) const {
208 // Name compare dominates, then domain, then path.
209 int diff
= name
.compare(cs
.name
);
213 diff
= domain
.compare(cs
.domain
);
217 return path
.compare(cs
.path
) < 0;
225 // For a CookieItVector iterator range [|it_begin|, |it_end|),
226 // sorts the first |num_sort| + 1 elements by LastAccessDate().
227 // The + 1 element exists so for any interval of length <= |num_sort| starting
228 // from |cookies_its_begin|, a LastAccessDate() bound can be found.
229 void SortLeastRecentlyAccessed(CookieMonster::CookieItVector::iterator it_begin
,
230 CookieMonster::CookieItVector::iterator it_end
,
232 DCHECK_LT(static_cast<int>(num_sort
), it_end
- it_begin
);
233 std::partial_sort(it_begin
, it_begin
+ num_sort
+ 1, it_end
, LRACookieSorter
);
236 // Predicate to support PartitionCookieByPriority().
237 struct CookiePriorityEqualsTo
238 : std::unary_function
<const CookieMonster::CookieMap::iterator
, bool> {
239 explicit CookiePriorityEqualsTo(CookiePriority priority
)
240 : priority_(priority
) {}
242 bool operator()(const CookieMonster::CookieMap::iterator it
) const {
243 return it
->second
->Priority() == priority_
;
246 const CookiePriority priority_
;
249 // For a CookieItVector iterator range [|it_begin|, |it_end|),
250 // moves all cookies with a given |priority| to the beginning of the list.
251 // Returns: An iterator in [it_begin, it_end) to the first element with
252 // priority != |priority|, or |it_end| if all have priority == |priority|.
253 CookieMonster::CookieItVector::iterator
PartitionCookieByPriority(
254 CookieMonster::CookieItVector::iterator it_begin
,
255 CookieMonster::CookieItVector::iterator it_end
,
256 CookiePriority priority
) {
257 return std::partition(it_begin
, it_end
, CookiePriorityEqualsTo(priority
));
260 bool LowerBoundAccessDateComparator(const CookieMonster::CookieMap::iterator it
,
261 const Time
& access_date
) {
262 return it
->second
->LastAccessDate() < access_date
;
265 // For a CookieItVector iterator range [|it_begin|, |it_end|)
266 // from a CookieItVector sorted by LastAccessDate(), returns the
267 // first iterator with access date >= |access_date|, or cookie_its_end if this
269 CookieMonster::CookieItVector::iterator
LowerBoundAccessDate(
270 const CookieMonster::CookieItVector::iterator its_begin
,
271 const CookieMonster::CookieItVector::iterator its_end
,
272 const Time
& access_date
) {
273 return std::lower_bound(its_begin
, its_end
, access_date
,
274 LowerBoundAccessDateComparator
);
277 // Mapping between DeletionCause and CookieMonsterDelegate::ChangeCause; the
278 // mapping also provides a boolean that specifies whether or not an
279 // OnCookieChanged notification ought to be generated.
280 typedef struct ChangeCausePair_struct
{
281 CookieMonsterDelegate::ChangeCause cause
;
284 ChangeCausePair ChangeCauseMapping
[] = {
285 // DELETE_COOKIE_EXPLICIT
286 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
, true},
287 // DELETE_COOKIE_OVERWRITE
288 {CookieMonsterDelegate::CHANGE_COOKIE_OVERWRITE
, true},
289 // DELETE_COOKIE_EXPIRED
290 {CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED
, true},
291 // DELETE_COOKIE_EVICTED
292 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
293 // DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE
294 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
, false},
295 // DELETE_COOKIE_DONT_RECORD
296 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
, false},
297 // DELETE_COOKIE_EVICTED_DOMAIN
298 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
299 // DELETE_COOKIE_EVICTED_GLOBAL
300 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
301 // DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
302 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
303 // DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
304 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
305 // DELETE_COOKIE_EXPIRED_OVERWRITE
306 {CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED_OVERWRITE
, true},
307 // DELETE_COOKIE_CONTROL_CHAR
308 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
309 // DELETE_COOKIE_LAST_ENTRY
310 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
, false}};
312 std::string
BuildCookieLine(const CanonicalCookieVector
& cookies
) {
313 std::string cookie_line
;
314 for (CanonicalCookieVector::const_iterator it
= cookies
.begin();
315 it
!= cookies
.end(); ++it
) {
316 if (it
!= cookies
.begin())
318 // In Mozilla if you set a cookie like AAAA, it will have an empty token
319 // and a value of AAAA. When it sends the cookie back, it will send AAAA,
320 // so we need to avoid sending =AAAA for a blank token value.
321 if (!(*it
)->Name().empty())
322 cookie_line
+= (*it
)->Name() + "=";
323 cookie_line
+= (*it
)->Value();
328 void RunAsync(scoped_refptr
<base::TaskRunner
> proxy
,
329 const CookieStore::CookieChangedCallback
& callback
,
330 const CanonicalCookie
& cookie
,
332 proxy
->PostTask(FROM_HERE
, base::Bind(callback
, cookie
, removed
));
337 CookieMonster::CookieMonster(PersistentCookieStore
* store
,
338 CookieMonsterDelegate
* delegate
)
339 : initialized_(false),
340 started_fetching_all_cookies_(false),
341 finished_fetching_all_cookies_(false),
342 fetch_strategy_(kUnknownFetch
),
344 last_access_threshold_(
345 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds
)),
347 last_statistic_record_time_(Time::Now()),
348 keep_expired_cookies_(false),
349 persist_session_cookies_(false) {
350 InitializeHistograms();
351 SetDefaultCookieableSchemes();
354 CookieMonster::CookieMonster(PersistentCookieStore
* store
,
355 CookieMonsterDelegate
* delegate
,
356 int last_access_threshold_milliseconds
)
357 : initialized_(false),
358 started_fetching_all_cookies_(false),
359 finished_fetching_all_cookies_(false),
360 fetch_strategy_(kUnknownFetch
),
362 last_access_threshold_(base::TimeDelta::FromMilliseconds(
363 last_access_threshold_milliseconds
)),
365 last_statistic_record_time_(base::Time::Now()),
366 keep_expired_cookies_(false),
367 persist_session_cookies_(false) {
368 InitializeHistograms();
369 SetDefaultCookieableSchemes();
372 // Task classes for queueing the coming request.
374 class CookieMonster::CookieMonsterTask
375 : public base::RefCountedThreadSafe
<CookieMonsterTask
> {
377 // Runs the task and invokes the client callback on the thread that
378 // originally constructed the task.
379 virtual void Run() = 0;
382 explicit CookieMonsterTask(CookieMonster
* cookie_monster
);
383 virtual ~CookieMonsterTask();
385 // Invokes the callback immediately, if the current thread is the one
386 // that originated the task, or queues the callback for execution on the
387 // appropriate thread. Maintains a reference to this CookieMonsterTask
388 // instance until the callback completes.
389 void InvokeCallback(base::Closure callback
);
391 CookieMonster
* cookie_monster() { return cookie_monster_
; }
394 friend class base::RefCountedThreadSafe
<CookieMonsterTask
>;
396 CookieMonster
* cookie_monster_
;
397 scoped_refptr
<base::SingleThreadTaskRunner
> thread_
;
399 DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask
);
402 CookieMonster::CookieMonsterTask::CookieMonsterTask(
403 CookieMonster
* cookie_monster
)
404 : cookie_monster_(cookie_monster
),
405 thread_(base::ThreadTaskRunnerHandle::Get()) {
408 CookieMonster::CookieMonsterTask::~CookieMonsterTask() {
411 // Unfortunately, one cannot re-bind a Callback with parameters into a closure.
412 // Therefore, the closure passed to InvokeCallback is a clumsy binding of
413 // Callback::Run on a wrapped Callback instance. Since Callback is not
414 // reference counted, we bind to an instance that is a member of the
415 // CookieMonsterTask subclass. Then, we cannot simply post the callback to a
416 // message loop because the underlying instance may be destroyed (along with the
417 // CookieMonsterTask instance) in the interim. Therefore, we post a callback
418 // bound to the CookieMonsterTask, which *is* reference counted (thus preventing
419 // destruction of the original callback), and which invokes the closure (which
420 // invokes the original callback with the returned data).
421 void CookieMonster::CookieMonsterTask::InvokeCallback(base::Closure callback
) {
422 if (thread_
->BelongsToCurrentThread()) {
425 thread_
->PostTask(FROM_HERE
, base::Bind(&CookieMonsterTask::InvokeCallback
,
430 // Task class for SetCookieWithDetails call.
431 class CookieMonster::SetCookieWithDetailsTask
: public CookieMonsterTask
{
433 SetCookieWithDetailsTask(CookieMonster
* cookie_monster
,
435 const std::string
& name
,
436 const std::string
& value
,
437 const std::string
& domain
,
438 const std::string
& path
,
439 const base::Time
& expiration_time
,
442 bool first_party_only
,
443 CookiePriority priority
,
444 const SetCookiesCallback
& callback
)
445 : CookieMonsterTask(cookie_monster
),
451 expiration_time_(expiration_time
),
453 http_only_(http_only
),
454 first_party_only_(first_party_only
),
456 callback_(callback
) {}
458 // CookieMonsterTask:
462 ~SetCookieWithDetailsTask() override
{}
470 base::Time expiration_time_
;
473 bool first_party_only_
;
474 CookiePriority priority_
;
475 SetCookiesCallback callback_
;
477 DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask
);
480 void CookieMonster::SetCookieWithDetailsTask::Run() {
481 bool success
= this->cookie_monster()->SetCookieWithDetails(
482 url_
, name_
, value_
, domain_
, path_
, expiration_time_
, secure_
,
483 http_only_
, first_party_only_
, priority_
);
484 if (!callback_
.is_null()) {
485 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run
,
486 base::Unretained(&callback_
), success
));
490 // Task class for GetAllCookies call.
491 class CookieMonster::GetAllCookiesTask
: public CookieMonsterTask
{
493 GetAllCookiesTask(CookieMonster
* cookie_monster
,
494 const GetCookieListCallback
& callback
)
495 : CookieMonsterTask(cookie_monster
), callback_(callback
) {}
501 ~GetAllCookiesTask() override
{}
504 GetCookieListCallback callback_
;
506 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask
);
509 void CookieMonster::GetAllCookiesTask::Run() {
510 if (!callback_
.is_null()) {
511 CookieList cookies
= this->cookie_monster()->GetAllCookies();
512 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run
,
513 base::Unretained(&callback_
), cookies
));
517 // Task class for GetAllCookiesForURLWithOptions call.
518 class CookieMonster::GetAllCookiesForURLWithOptionsTask
519 : public CookieMonsterTask
{
521 GetAllCookiesForURLWithOptionsTask(CookieMonster
* cookie_monster
,
523 const CookieOptions
& options
,
524 const GetCookieListCallback
& callback
)
525 : CookieMonsterTask(cookie_monster
),
528 callback_(callback
) {}
530 // CookieMonsterTask:
534 ~GetAllCookiesForURLWithOptionsTask() override
{}
538 CookieOptions options_
;
539 GetCookieListCallback callback_
;
541 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesForURLWithOptionsTask
);
544 void CookieMonster::GetAllCookiesForURLWithOptionsTask::Run() {
545 if (!callback_
.is_null()) {
547 this->cookie_monster()->GetAllCookiesForURLWithOptions(url_
, options_
);
548 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run
,
549 base::Unretained(&callback_
), cookies
));
553 template <typename Result
>
554 struct CallbackType
{
555 typedef base::Callback
<void(Result
)> Type
;
559 struct CallbackType
<void> {
560 typedef base::Closure Type
;
563 // Base task class for Delete*Task.
564 template <typename Result
>
565 class CookieMonster::DeleteTask
: public CookieMonsterTask
{
567 DeleteTask(CookieMonster
* cookie_monster
,
568 const typename CallbackType
<Result
>::Type
& callback
)
569 : CookieMonsterTask(cookie_monster
), callback_(callback
) {}
571 // CookieMonsterTask:
575 ~DeleteTask() override
;
578 // Runs the delete task and returns a result.
579 virtual Result
RunDeleteTask() = 0;
580 base::Closure
RunDeleteTaskAndBindCallback();
581 void FlushDone(const base::Closure
& callback
);
583 typename CallbackType
<Result
>::Type callback_
;
585 DISALLOW_COPY_AND_ASSIGN(DeleteTask
);
588 template <typename Result
>
589 CookieMonster::DeleteTask
<Result
>::~DeleteTask() {
592 template <typename Result
>
594 CookieMonster::DeleteTask
<Result
>::RunDeleteTaskAndBindCallback() {
595 Result result
= RunDeleteTask();
596 if (callback_
.is_null())
597 return base::Closure();
598 return base::Bind(callback_
, result
);
602 base::Closure
CookieMonster::DeleteTask
<void>::RunDeleteTaskAndBindCallback() {
607 template <typename Result
>
608 void CookieMonster::DeleteTask
<Result
>::Run() {
609 this->cookie_monster()->FlushStore(base::Bind(
610 &DeleteTask
<Result
>::FlushDone
, this, RunDeleteTaskAndBindCallback()));
613 template <typename Result
>
614 void CookieMonster::DeleteTask
<Result
>::FlushDone(
615 const base::Closure
& callback
) {
616 if (!callback
.is_null()) {
617 this->InvokeCallback(callback
);
621 // Task class for DeleteAll call.
622 class CookieMonster::DeleteAllTask
: public DeleteTask
<int> {
624 DeleteAllTask(CookieMonster
* cookie_monster
, const DeleteCallback
& callback
)
625 : DeleteTask
<int>(cookie_monster
, callback
) {}
628 int RunDeleteTask() override
;
631 ~DeleteAllTask() override
{}
634 DISALLOW_COPY_AND_ASSIGN(DeleteAllTask
);
637 int CookieMonster::DeleteAllTask::RunDeleteTask() {
638 return this->cookie_monster()->DeleteAll(true);
641 // Task class for DeleteAllCreatedBetween call.
642 class CookieMonster::DeleteAllCreatedBetweenTask
: public DeleteTask
<int> {
644 DeleteAllCreatedBetweenTask(CookieMonster
* cookie_monster
,
645 const Time
& delete_begin
,
646 const Time
& delete_end
,
647 const DeleteCallback
& callback
)
648 : DeleteTask
<int>(cookie_monster
, callback
),
649 delete_begin_(delete_begin
),
650 delete_end_(delete_end
) {}
653 int RunDeleteTask() override
;
656 ~DeleteAllCreatedBetweenTask() override
{}
662 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask
);
665 int CookieMonster::DeleteAllCreatedBetweenTask::RunDeleteTask() {
666 return this->cookie_monster()->DeleteAllCreatedBetween(delete_begin_
,
670 // Task class for DeleteAllForHost call.
671 class CookieMonster::DeleteAllForHostTask
: public DeleteTask
<int> {
673 DeleteAllForHostTask(CookieMonster
* cookie_monster
,
675 const DeleteCallback
& callback
)
676 : DeleteTask
<int>(cookie_monster
, callback
), url_(url
) {}
679 int RunDeleteTask() override
;
682 ~DeleteAllForHostTask() override
{}
687 DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask
);
690 int CookieMonster::DeleteAllForHostTask::RunDeleteTask() {
691 return this->cookie_monster()->DeleteAllForHost(url_
);
694 // Task class for DeleteAllCreatedBetweenForHost call.
695 class CookieMonster::DeleteAllCreatedBetweenForHostTask
696 : public DeleteTask
<int> {
698 DeleteAllCreatedBetweenForHostTask(CookieMonster
* cookie_monster
,
702 const DeleteCallback
& callback
)
703 : DeleteTask
<int>(cookie_monster
, callback
),
704 delete_begin_(delete_begin
),
705 delete_end_(delete_end
),
709 int RunDeleteTask() override
;
712 ~DeleteAllCreatedBetweenForHostTask() override
{}
719 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenForHostTask
);
722 int CookieMonster::DeleteAllCreatedBetweenForHostTask::RunDeleteTask() {
723 return this->cookie_monster()->DeleteAllCreatedBetweenForHost(
724 delete_begin_
, delete_end_
, url_
);
727 // Task class for DeleteCanonicalCookie call.
728 class CookieMonster::DeleteCanonicalCookieTask
: public DeleteTask
<bool> {
730 DeleteCanonicalCookieTask(CookieMonster
* cookie_monster
,
731 const CanonicalCookie
& cookie
,
732 const DeleteCookieCallback
& callback
)
733 : DeleteTask
<bool>(cookie_monster
, callback
), cookie_(cookie
) {}
736 bool RunDeleteTask() override
;
739 ~DeleteCanonicalCookieTask() override
{}
742 CanonicalCookie cookie_
;
744 DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask
);
747 bool CookieMonster::DeleteCanonicalCookieTask::RunDeleteTask() {
748 return this->cookie_monster()->DeleteCanonicalCookie(cookie_
);
751 // Task class for SetCookieWithOptions call.
752 class CookieMonster::SetCookieWithOptionsTask
: public CookieMonsterTask
{
754 SetCookieWithOptionsTask(CookieMonster
* cookie_monster
,
756 const std::string
& cookie_line
,
757 const CookieOptions
& options
,
758 const SetCookiesCallback
& callback
)
759 : CookieMonsterTask(cookie_monster
),
761 cookie_line_(cookie_line
),
763 callback_(callback
) {}
765 // CookieMonsterTask:
769 ~SetCookieWithOptionsTask() override
{}
773 std::string cookie_line_
;
774 CookieOptions options_
;
775 SetCookiesCallback callback_
;
777 DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask
);
780 void CookieMonster::SetCookieWithOptionsTask::Run() {
781 bool result
= this->cookie_monster()->SetCookieWithOptions(url_
, cookie_line_
,
783 if (!callback_
.is_null()) {
784 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run
,
785 base::Unretained(&callback_
), result
));
789 // Task class for SetAllCookies call.
790 class CookieMonster::SetAllCookiesTask
: public CookieMonsterTask
{
792 SetAllCookiesTask(CookieMonster
* cookie_monster
,
793 const CookieList
& list
,
794 const SetCookiesCallback
& callback
)
795 : CookieMonsterTask(cookie_monster
), list_(list
), callback_(callback
) {}
797 // CookieMonsterTask:
801 ~SetAllCookiesTask() override
{}
805 SetCookiesCallback callback_
;
807 DISALLOW_COPY_AND_ASSIGN(SetAllCookiesTask
);
810 void CookieMonster::SetAllCookiesTask::Run() {
811 CookieList positive_diff
;
812 CookieList negative_diff
;
813 CookieList old_cookies
= this->cookie_monster()->GetAllCookies();
814 this->cookie_monster()->ComputeCookieDiff(&old_cookies
, &list_
,
815 &positive_diff
, &negative_diff
);
817 for (CookieList::const_iterator it
= negative_diff
.begin();
818 it
!= negative_diff
.end(); ++it
) {
819 this->cookie_monster()->DeleteCanonicalCookie(*it
);
823 if (positive_diff
.size() > 0)
824 result
= this->cookie_monster()->SetCanonicalCookies(list_
);
826 if (!callback_
.is_null()) {
827 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run
,
828 base::Unretained(&callback_
), result
));
832 // Task class for GetCookiesWithOptions call.
833 class CookieMonster::GetCookiesWithOptionsTask
: public CookieMonsterTask
{
835 GetCookiesWithOptionsTask(CookieMonster
* cookie_monster
,
837 const CookieOptions
& options
,
838 const GetCookiesCallback
& callback
)
839 : CookieMonsterTask(cookie_monster
),
842 callback_(callback
) {}
844 // CookieMonsterTask:
848 ~GetCookiesWithOptionsTask() override
{}
852 CookieOptions options_
;
853 GetCookiesCallback callback_
;
855 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask
);
858 void CookieMonster::GetCookiesWithOptionsTask::Run() {
859 // TODO(mkwst): Remove ScopedTracker below once crbug.com/456373 is fixed.
860 tracked_objects::ScopedTracker
tracking_profile(
861 FROM_HERE_WITH_EXPLICIT_FUNCTION(
862 "456373 CookieMonster::GetCookiesWithOptionsTask::Run"));
864 this->cookie_monster()->GetCookiesWithOptions(url_
, options_
);
865 if (!callback_
.is_null()) {
866 this->InvokeCallback(base::Bind(&GetCookiesCallback::Run
,
867 base::Unretained(&callback_
), cookie
));
871 // Task class for DeleteCookie call.
872 class CookieMonster::DeleteCookieTask
: public DeleteTask
<void> {
874 DeleteCookieTask(CookieMonster
* cookie_monster
,
876 const std::string
& cookie_name
,
877 const base::Closure
& callback
)
878 : DeleteTask
<void>(cookie_monster
, callback
),
880 cookie_name_(cookie_name
) {}
883 void RunDeleteTask() override
;
886 ~DeleteCookieTask() override
{}
890 std::string cookie_name_
;
892 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask
);
895 void CookieMonster::DeleteCookieTask::RunDeleteTask() {
896 this->cookie_monster()->DeleteCookie(url_
, cookie_name_
);
899 // Task class for DeleteSessionCookies call.
900 class CookieMonster::DeleteSessionCookiesTask
: public DeleteTask
<int> {
902 DeleteSessionCookiesTask(CookieMonster
* cookie_monster
,
903 const DeleteCallback
& callback
)
904 : DeleteTask
<int>(cookie_monster
, callback
) {}
907 int RunDeleteTask() override
;
910 ~DeleteSessionCookiesTask() override
{}
913 DISALLOW_COPY_AND_ASSIGN(DeleteSessionCookiesTask
);
916 int CookieMonster::DeleteSessionCookiesTask::RunDeleteTask() {
917 return this->cookie_monster()->DeleteSessionCookies();
920 // Task class for HasCookiesForETLDP1Task call.
921 class CookieMonster::HasCookiesForETLDP1Task
: public CookieMonsterTask
{
923 HasCookiesForETLDP1Task(CookieMonster
* cookie_monster
,
924 const std::string
& etldp1
,
925 const HasCookiesForETLDP1Callback
& callback
)
926 : CookieMonsterTask(cookie_monster
),
928 callback_(callback
) {}
930 // CookieMonsterTask:
934 ~HasCookiesForETLDP1Task() override
{}
938 HasCookiesForETLDP1Callback callback_
;
940 DISALLOW_COPY_AND_ASSIGN(HasCookiesForETLDP1Task
);
943 void CookieMonster::HasCookiesForETLDP1Task::Run() {
944 bool result
= this->cookie_monster()->HasCookiesForETLDP1(etldp1_
);
945 if (!callback_
.is_null()) {
946 this->InvokeCallback(base::Bind(&HasCookiesForETLDP1Callback::Run
,
947 base::Unretained(&callback_
), result
));
951 // Asynchronous CookieMonster API
953 void CookieMonster::SetCookieWithDetailsAsync(
955 const std::string
& name
,
956 const std::string
& value
,
957 const std::string
& domain
,
958 const std::string
& path
,
959 const Time
& expiration_time
,
962 bool first_party_only
,
963 CookiePriority priority
,
964 const SetCookiesCallback
& callback
) {
965 scoped_refptr
<SetCookieWithDetailsTask
> task
= new SetCookieWithDetailsTask(
966 this, url
, name
, value
, domain
, path
, expiration_time
, secure
, http_only
,
967 first_party_only
, priority
, callback
);
968 DoCookieTaskForURL(task
, url
);
971 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback
& callback
) {
972 scoped_refptr
<GetAllCookiesTask
> task
= new GetAllCookiesTask(this, callback
);
977 void CookieMonster::GetAllCookiesForURLWithOptionsAsync(
979 const CookieOptions
& options
,
980 const GetCookieListCallback
& callback
) {
981 scoped_refptr
<GetAllCookiesForURLWithOptionsTask
> task
=
982 new GetAllCookiesForURLWithOptionsTask(this, url
, options
, callback
);
984 DoCookieTaskForURL(task
, url
);
987 void CookieMonster::GetAllCookiesForURLAsync(
989 const GetCookieListCallback
& callback
) {
990 CookieOptions options
;
991 options
.set_include_httponly();
992 options
.set_include_first_party_only();
993 scoped_refptr
<GetAllCookiesForURLWithOptionsTask
> task
=
994 new GetAllCookiesForURLWithOptionsTask(this, url
, options
, callback
);
996 DoCookieTaskForURL(task
, url
);
999 void CookieMonster::HasCookiesForETLDP1Async(
1000 const std::string
& etldp1
,
1001 const HasCookiesForETLDP1Callback
& callback
) {
1002 scoped_refptr
<HasCookiesForETLDP1Task
> task
=
1003 new HasCookiesForETLDP1Task(this, etldp1
, callback
);
1005 DoCookieTaskForURL(task
, GURL("http://" + etldp1
));
1008 void CookieMonster::DeleteAllAsync(const DeleteCallback
& callback
) {
1009 scoped_refptr
<DeleteAllTask
> task
= new DeleteAllTask(this, callback
);
1014 void CookieMonster::DeleteAllCreatedBetweenAsync(
1015 const Time
& delete_begin
,
1016 const Time
& delete_end
,
1017 const DeleteCallback
& callback
) {
1018 scoped_refptr
<DeleteAllCreatedBetweenTask
> task
=
1019 new DeleteAllCreatedBetweenTask(this, delete_begin
, delete_end
, callback
);
1024 void CookieMonster::DeleteAllCreatedBetweenForHostAsync(
1025 const Time delete_begin
,
1026 const Time delete_end
,
1028 const DeleteCallback
& callback
) {
1029 scoped_refptr
<DeleteAllCreatedBetweenForHostTask
> task
=
1030 new DeleteAllCreatedBetweenForHostTask(this, delete_begin
, delete_end
,
1033 DoCookieTaskForURL(task
, url
);
1036 void CookieMonster::DeleteAllForHostAsync(const GURL
& url
,
1037 const DeleteCallback
& callback
) {
1038 scoped_refptr
<DeleteAllForHostTask
> task
=
1039 new DeleteAllForHostTask(this, url
, callback
);
1041 DoCookieTaskForURL(task
, url
);
1044 void CookieMonster::DeleteCanonicalCookieAsync(
1045 const CanonicalCookie
& cookie
,
1046 const DeleteCookieCallback
& callback
) {
1047 scoped_refptr
<DeleteCanonicalCookieTask
> task
=
1048 new DeleteCanonicalCookieTask(this, cookie
, callback
);
1053 void CookieMonster::SetAllCookiesAsync(const CookieList
& list
,
1054 const SetCookiesCallback
& callback
) {
1055 scoped_refptr
<SetAllCookiesTask
> task
=
1056 new SetAllCookiesTask(this, list
, callback
);
1060 void CookieMonster::SetCookieWithOptionsAsync(
1062 const std::string
& cookie_line
,
1063 const CookieOptions
& options
,
1064 const SetCookiesCallback
& callback
) {
1065 scoped_refptr
<SetCookieWithOptionsTask
> task
=
1066 new SetCookieWithOptionsTask(this, url
, cookie_line
, options
, callback
);
1068 DoCookieTaskForURL(task
, url
);
1071 void CookieMonster::GetCookiesWithOptionsAsync(
1073 const CookieOptions
& options
,
1074 const GetCookiesCallback
& callback
) {
1075 scoped_refptr
<GetCookiesWithOptionsTask
> task
=
1076 new GetCookiesWithOptionsTask(this, url
, options
, callback
);
1078 DoCookieTaskForURL(task
, url
);
1081 void CookieMonster::DeleteCookieAsync(const GURL
& url
,
1082 const std::string
& cookie_name
,
1083 const base::Closure
& callback
) {
1084 scoped_refptr
<DeleteCookieTask
> task
=
1085 new DeleteCookieTask(this, url
, cookie_name
, callback
);
1087 DoCookieTaskForURL(task
, url
);
1090 void CookieMonster::DeleteSessionCookiesAsync(
1091 const CookieStore::DeleteCallback
& callback
) {
1092 scoped_refptr
<DeleteSessionCookiesTask
> task
=
1093 new DeleteSessionCookiesTask(this, callback
);
1098 void CookieMonster::DoCookieTask(
1099 const scoped_refptr
<CookieMonsterTask
>& task_item
) {
1101 base::AutoLock
autolock(lock_
);
1102 MarkCookieStoreAsInitialized();
1103 FetchAllCookiesIfNecessary();
1104 if (!finished_fetching_all_cookies_
&& store_
.get()) {
1105 tasks_pending_
.push(task_item
);
1113 void CookieMonster::DoCookieTaskForURL(
1114 const scoped_refptr
<CookieMonsterTask
>& task_item
,
1117 base::AutoLock
autolock(lock_
);
1118 MarkCookieStoreAsInitialized();
1119 if (ShouldFetchAllCookiesWhenFetchingAnyCookie())
1120 FetchAllCookiesIfNecessary();
1121 // If cookies for the requested domain key (eTLD+1) have been loaded from DB
1122 // then run the task, otherwise load from DB.
1123 if (!finished_fetching_all_cookies_
&& store_
.get()) {
1124 // Checks if the domain key has been loaded.
1126 cookie_util::GetEffectiveDomain(url
.scheme(), url
.host()));
1127 if (keys_loaded_
.find(key
) == keys_loaded_
.end()) {
1128 std::map
<std::string
,
1129 std::deque
<scoped_refptr
<CookieMonsterTask
>>>::iterator it
=
1130 tasks_pending_for_key_
.find(key
);
1131 if (it
== tasks_pending_for_key_
.end()) {
1132 store_
->LoadCookiesForKey(
1133 key
, base::Bind(&CookieMonster::OnKeyLoaded
, this, key
));
1134 it
= tasks_pending_for_key_
1135 .insert(std::make_pair(
1136 key
, std::deque
<scoped_refptr
<CookieMonsterTask
>>()))
1139 it
->second
.push_back(task_item
);
1147 bool CookieMonster::SetCookieWithDetails(const GURL
& url
,
1148 const std::string
& name
,
1149 const std::string
& value
,
1150 const std::string
& domain
,
1151 const std::string
& path
,
1152 const base::Time
& expiration_time
,
1155 bool first_party_only
,
1156 CookiePriority priority
) {
1157 base::AutoLock
autolock(lock_
);
1159 if (!HasCookieableScheme(url
))
1162 Time creation_time
= CurrentTime();
1163 last_time_seen_
= creation_time
;
1165 scoped_ptr
<CanonicalCookie
> cc
;
1166 cc
.reset(CanonicalCookie::Create(url
, name
, value
, domain
, path
,
1167 creation_time
, expiration_time
, secure
,
1168 http_only
, first_party_only
, priority
));
1173 CookieOptions options
;
1174 options
.set_include_httponly();
1175 options
.set_include_first_party_only();
1176 return SetCanonicalCookie(&cc
, creation_time
, options
);
1179 bool CookieMonster::ImportCookies(const CookieList
& list
) {
1180 base::AutoLock
autolock(lock_
);
1181 MarkCookieStoreAsInitialized();
1182 if (ShouldFetchAllCookiesWhenFetchingAnyCookie())
1183 FetchAllCookiesIfNecessary();
1184 for (CookieList::const_iterator iter
= list
.begin(); iter
!= list
.end();
1186 scoped_ptr
<CanonicalCookie
> cookie(new CanonicalCookie(*iter
));
1187 CookieOptions options
;
1188 options
.set_include_httponly();
1189 options
.set_include_first_party_only();
1190 if (!SetCanonicalCookie(&cookie
, cookie
->CreationDate(), options
))
1196 CookieList
CookieMonster::GetAllCookies() {
1197 base::AutoLock
autolock(lock_
);
1199 // This function is being called to scrape the cookie list for management UI
1200 // or similar. We shouldn't show expired cookies in this list since it will
1201 // just be confusing to users, and this function is called rarely enough (and
1202 // is already slow enough) that it's OK to take the time to garbage collect
1203 // the expired cookies now.
1205 // Note that this does not prune cookies to be below our limits (if we've
1206 // exceeded them) the way that calling GarbageCollect() would.
1207 GarbageCollectExpired(
1208 Time::Now(), CookieMapItPair(cookies_
.begin(), cookies_
.end()), NULL
);
1210 // Copy the CanonicalCookie pointers from the map so that we can use the same
1211 // sorter as elsewhere, then copy the result out.
1212 std::vector
<CanonicalCookie
*> cookie_ptrs
;
1213 cookie_ptrs
.reserve(cookies_
.size());
1214 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end(); ++it
)
1215 cookie_ptrs
.push_back(it
->second
);
1216 std::sort(cookie_ptrs
.begin(), cookie_ptrs
.end(), CookieSorter
);
1218 CookieList cookie_list
;
1219 cookie_list
.reserve(cookie_ptrs
.size());
1220 for (std::vector
<CanonicalCookie
*>::const_iterator it
= cookie_ptrs
.begin();
1221 it
!= cookie_ptrs
.end(); ++it
)
1222 cookie_list
.push_back(**it
);
1227 CookieList
CookieMonster::GetAllCookiesForURLWithOptions(
1229 const CookieOptions
& options
) {
1230 base::AutoLock
autolock(lock_
);
1232 std::vector
<CanonicalCookie
*> cookie_ptrs
;
1233 FindCookiesForHostAndDomain(url
, options
, false, &cookie_ptrs
);
1234 std::sort(cookie_ptrs
.begin(), cookie_ptrs
.end(), CookieSorter
);
1237 cookies
.reserve(cookie_ptrs
.size());
1238 for (std::vector
<CanonicalCookie
*>::const_iterator it
= cookie_ptrs
.begin();
1239 it
!= cookie_ptrs
.end(); it
++)
1240 cookies
.push_back(**it
);
1245 CookieList
CookieMonster::GetAllCookiesForURL(const GURL
& url
) {
1246 CookieOptions options
;
1247 options
.set_include_httponly();
1248 options
.set_first_party_url(url
);
1250 return GetAllCookiesForURLWithOptions(url
, options
);
1253 int CookieMonster::DeleteAll(bool sync_to_store
) {
1254 base::AutoLock
autolock(lock_
);
1256 int num_deleted
= 0;
1257 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end();) {
1258 CookieMap::iterator curit
= it
;
1260 InternalDeleteCookie(curit
, sync_to_store
,
1262 ? DELETE_COOKIE_EXPLICIT
1263 : DELETE_COOKIE_DONT_RECORD
/* Destruction. */);
1270 int CookieMonster::DeleteAllCreatedBetween(const Time
& delete_begin
,
1271 const Time
& delete_end
) {
1272 base::AutoLock
autolock(lock_
);
1274 int num_deleted
= 0;
1275 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end();) {
1276 CookieMap::iterator curit
= it
;
1277 CanonicalCookie
* cc
= curit
->second
;
1280 if (cc
->CreationDate() >= delete_begin
&&
1281 (delete_end
.is_null() || cc
->CreationDate() < delete_end
)) {
1282 InternalDeleteCookie(curit
, true, /*sync_to_store*/
1283 DELETE_COOKIE_EXPLICIT
);
1291 int CookieMonster::DeleteAllCreatedBetweenForHost(const Time delete_begin
,
1292 const Time delete_end
,
1294 base::AutoLock
autolock(lock_
);
1296 if (!HasCookieableScheme(url
))
1299 const std::string
host(url
.host());
1301 // We store host cookies in the store by their canonical host name;
1302 // domain cookies are stored with a leading ".". So this is a pretty
1303 // simple lookup and per-cookie delete.
1304 int num_deleted
= 0;
1305 for (CookieMapItPair its
= cookies_
.equal_range(GetKey(host
));
1306 its
.first
!= its
.second
;) {
1307 CookieMap::iterator curit
= its
.first
;
1310 const CanonicalCookie
* const cc
= curit
->second
;
1312 // Delete only on a match as a host cookie.
1313 if (cc
->IsHostCookie() && cc
->IsDomainMatch(host
) &&
1314 cc
->CreationDate() >= delete_begin
&&
1315 // The assumption that null |delete_end| is equivalent to
1316 // Time::Max() is confusing.
1317 (delete_end
.is_null() || cc
->CreationDate() < delete_end
)) {
1320 InternalDeleteCookie(curit
, true, DELETE_COOKIE_EXPLICIT
);
1326 int CookieMonster::DeleteAllForHost(const GURL
& url
) {
1327 return DeleteAllCreatedBetweenForHost(Time(), Time::Max(), url
);
1330 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie
& cookie
) {
1331 base::AutoLock
autolock(lock_
);
1333 for (CookieMapItPair its
= cookies_
.equal_range(GetKey(cookie
.Domain()));
1334 its
.first
!= its
.second
; ++its
.first
) {
1335 // The creation date acts as our unique index...
1336 if (its
.first
->second
->CreationDate() == cookie
.CreationDate()) {
1337 InternalDeleteCookie(its
.first
, true, DELETE_COOKIE_EXPLICIT
);
1344 void CookieMonster::SetCookieableSchemes(const char* const schemes
[],
1345 size_t num_schemes
) {
1346 base::AutoLock
autolock(lock_
);
1348 // Calls to this method will have no effect if made after a WebView or
1349 // CookieManager instance has been created.
1354 cookieable_schemes_
.clear();
1355 cookieable_schemes_
.insert(cookieable_schemes_
.end(), schemes
,
1356 schemes
+ num_schemes
);
1359 void CookieMonster::SetKeepExpiredCookies() {
1360 keep_expired_cookies_
= true;
1363 void CookieMonster::FlushStore(const base::Closure
& callback
) {
1364 base::AutoLock
autolock(lock_
);
1365 if (initialized_
&& store_
.get())
1366 store_
->Flush(callback
);
1367 else if (!callback
.is_null())
1368 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, callback
);
1371 bool CookieMonster::SetCookieWithOptions(const GURL
& url
,
1372 const std::string
& cookie_line
,
1373 const CookieOptions
& options
) {
1374 base::AutoLock
autolock(lock_
);
1376 if (!HasCookieableScheme(url
)) {
1380 return SetCookieWithCreationTimeAndOptions(url
, cookie_line
, Time(), options
);
1383 std::string
CookieMonster::GetCookiesWithOptions(const GURL
& url
,
1384 const CookieOptions
& options
) {
1385 base::AutoLock
autolock(lock_
);
1387 if (!HasCookieableScheme(url
))
1388 return std::string();
1390 std::vector
<CanonicalCookie
*> cookies
;
1391 FindCookiesForHostAndDomain(url
, options
, true, &cookies
);
1392 std::sort(cookies
.begin(), cookies
.end(), CookieSorter
);
1394 std::string cookie_line
= BuildCookieLine(cookies
);
1396 VLOG(kVlogGetCookies
) << "GetCookies() result: " << cookie_line
;
1401 void CookieMonster::DeleteCookie(const GURL
& url
,
1402 const std::string
& cookie_name
) {
1403 base::AutoLock
autolock(lock_
);
1405 if (!HasCookieableScheme(url
))
1408 CookieOptions options
;
1409 options
.set_include_httponly();
1410 options
.set_include_first_party_only();
1411 // Get the cookies for this host and its domain(s).
1412 std::vector
<CanonicalCookie
*> cookies
;
1413 FindCookiesForHostAndDomain(url
, options
, true, &cookies
);
1414 std::set
<CanonicalCookie
*> matching_cookies
;
1416 for (std::vector
<CanonicalCookie
*>::const_iterator it
= cookies
.begin();
1417 it
!= cookies
.end(); ++it
) {
1418 if ((*it
)->Name() != cookie_name
)
1420 if (url
.path().find((*it
)->Path()))
1422 matching_cookies
.insert(*it
);
1425 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end();) {
1426 CookieMap::iterator curit
= it
;
1428 if (matching_cookies
.find(curit
->second
) != matching_cookies
.end()) {
1429 InternalDeleteCookie(curit
, true, DELETE_COOKIE_EXPLICIT
);
1434 int CookieMonster::DeleteSessionCookies() {
1435 base::AutoLock
autolock(lock_
);
1437 int num_deleted
= 0;
1438 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end();) {
1439 CookieMap::iterator curit
= it
;
1440 CanonicalCookie
* cc
= curit
->second
;
1443 if (!cc
->IsPersistent()) {
1444 InternalDeleteCookie(curit
, true, /*sync_to_store*/
1445 DELETE_COOKIE_EXPIRED
);
1453 bool CookieMonster::HasCookiesForETLDP1(const std::string
& etldp1
) {
1454 base::AutoLock
autolock(lock_
);
1456 const std::string
key(GetKey(etldp1
));
1458 CookieMapItPair its
= cookies_
.equal_range(key
);
1459 return its
.first
!= its
.second
;
1462 CookieMonster
* CookieMonster::GetCookieMonster() {
1466 // This function must be called before the CookieMonster is used.
1467 void CookieMonster::SetPersistSessionCookies(bool persist_session_cookies
) {
1468 DCHECK(!initialized_
);
1469 persist_session_cookies_
= persist_session_cookies
;
1472 void CookieMonster::SetForceKeepSessionState() {
1474 store_
->SetForceKeepSessionState();
1478 CookieMonster::~CookieMonster() {
1482 bool CookieMonster::SetCookieWithCreationTime(const GURL
& url
,
1483 const std::string
& cookie_line
,
1484 const base::Time
& creation_time
) {
1485 DCHECK(!store_
.get()) << "This method is only to be used by unit-tests.";
1486 base::AutoLock
autolock(lock_
);
1488 if (!HasCookieableScheme(url
)) {
1492 MarkCookieStoreAsInitialized();
1493 if (ShouldFetchAllCookiesWhenFetchingAnyCookie())
1494 FetchAllCookiesIfNecessary();
1496 return SetCookieWithCreationTimeAndOptions(url
, cookie_line
, creation_time
,
1500 void CookieMonster::MarkCookieStoreAsInitialized() {
1501 initialized_
= true;
1504 void CookieMonster::FetchAllCookiesIfNecessary() {
1505 if (store_
.get() && !started_fetching_all_cookies_
) {
1506 started_fetching_all_cookies_
= true;
1511 bool CookieMonster::ShouldFetchAllCookiesWhenFetchingAnyCookie() {
1512 if (fetch_strategy_
== kUnknownFetch
) {
1513 const std::string group_name
=
1514 base::FieldTrialList::FindFullName(kCookieMonsterFetchStrategyName
);
1515 if (group_name
== kFetchWhenNecessaryName
) {
1516 fetch_strategy_
= kFetchWhenNecessary
;
1517 } else if (group_name
== kAlwaysFetchName
) {
1518 fetch_strategy_
= kAlwaysFetch
;
1520 // The logic in the conditional is redundant, but it makes trials of
1521 // the Finch experiment more explicit.
1522 fetch_strategy_
= kAlwaysFetch
;
1526 return fetch_strategy_
== kAlwaysFetch
;
1529 void CookieMonster::FetchAllCookies() {
1530 DCHECK(store_
.get()) << "Store must exist to initialize";
1531 DCHECK(!finished_fetching_all_cookies_
)
1532 << "All cookies have already been fetched.";
1534 // We bind in the current time so that we can report the wall-clock time for
1536 store_
->Load(base::Bind(&CookieMonster::OnLoaded
, this, TimeTicks::Now()));
1539 void CookieMonster::OnLoaded(TimeTicks beginning_time
,
1540 const std::vector
<CanonicalCookie
*>& cookies
) {
1541 StoreLoadedCookies(cookies
);
1542 histogram_time_blocked_on_load_
->AddTime(TimeTicks::Now() - beginning_time
);
1544 // Invoke the task queue of cookie request.
1548 void CookieMonster::OnKeyLoaded(const std::string
& key
,
1549 const std::vector
<CanonicalCookie
*>& cookies
) {
1550 // This function does its own separate locking.
1551 StoreLoadedCookies(cookies
);
1553 std::deque
<scoped_refptr
<CookieMonsterTask
>> tasks_pending_for_key
;
1555 // We need to do this repeatedly until no more tasks were added to the queue
1556 // during the period where we release the lock.
1559 base::AutoLock
autolock(lock_
);
1560 std::map
<std::string
,
1561 std::deque
<scoped_refptr
<CookieMonsterTask
>>>::iterator it
=
1562 tasks_pending_for_key_
.find(key
);
1563 if (it
== tasks_pending_for_key_
.end()) {
1564 keys_loaded_
.insert(key
);
1567 if (it
->second
.empty()) {
1568 keys_loaded_
.insert(key
);
1569 tasks_pending_for_key_
.erase(it
);
1572 it
->second
.swap(tasks_pending_for_key
);
1575 while (!tasks_pending_for_key
.empty()) {
1576 scoped_refptr
<CookieMonsterTask
> task
= tasks_pending_for_key
.front();
1578 tasks_pending_for_key
.pop_front();
1583 void CookieMonster::StoreLoadedCookies(
1584 const std::vector
<CanonicalCookie
*>& cookies
) {
1585 // TODO(erikwright): Remove ScopedTracker below once crbug.com/457528 is
1587 tracked_objects::ScopedTracker
tracking_profile(
1588 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1589 "457528 CookieMonster::StoreLoadedCookies"));
1591 // Initialize the store and sync in any saved persistent cookies. We don't
1592 // care if it's expired, insert it so it can be garbage collected, removed,
1594 base::AutoLock
autolock(lock_
);
1596 CookieItVector cookies_with_control_chars
;
1598 for (std::vector
<CanonicalCookie
*>::const_iterator it
= cookies
.begin();
1599 it
!= cookies
.end(); ++it
) {
1600 int64 cookie_creation_time
= (*it
)->CreationDate().ToInternalValue();
1602 if (creation_times_
.insert(cookie_creation_time
).second
) {
1603 CookieMap::iterator inserted
=
1604 InternalInsertCookie(GetKey((*it
)->Domain()), *it
, false);
1605 const Time
cookie_access_time((*it
)->LastAccessDate());
1606 if (earliest_access_time_
.is_null() ||
1607 cookie_access_time
< earliest_access_time_
)
1608 earliest_access_time_
= cookie_access_time
;
1610 if (ContainsControlCharacter((*it
)->Name()) ||
1611 ContainsControlCharacter((*it
)->Value())) {
1612 cookies_with_control_chars
.push_back(inserted
);
1615 LOG(ERROR
) << base::StringPrintf(
1616 "Found cookies with duplicate creation "
1617 "times in backing store: "
1618 "{name='%s', domain='%s', path='%s'}",
1619 (*it
)->Name().c_str(), (*it
)->Domain().c_str(),
1620 (*it
)->Path().c_str());
1621 // We've been given ownership of the cookie and are throwing it
1622 // away; reclaim the space.
1627 // Any cookies that contain control characters that we have loaded from the
1628 // persistent store should be deleted. See http://crbug.com/238041.
1629 for (CookieItVector::iterator it
= cookies_with_control_chars
.begin();
1630 it
!= cookies_with_control_chars
.end();) {
1631 CookieItVector::iterator curit
= it
;
1634 InternalDeleteCookie(*curit
, true, DELETE_COOKIE_CONTROL_CHAR
);
1637 // After importing cookies from the PersistentCookieStore, verify that
1638 // none of our other constraints are violated.
1639 // In particular, the backing store might have given us duplicate cookies.
1641 // This method could be called multiple times due to priority loading, thus
1642 // cookies loaded in previous runs will be validated again, but this is OK
1643 // since they are expected to be much fewer than total DB.
1644 EnsureCookiesMapIsValid();
1647 void CookieMonster::InvokeQueue() {
1649 scoped_refptr
<CookieMonsterTask
> request_task
;
1651 base::AutoLock
autolock(lock_
);
1652 if (tasks_pending_
.empty()) {
1653 finished_fetching_all_cookies_
= true;
1654 creation_times_
.clear();
1655 keys_loaded_
.clear();
1658 request_task
= tasks_pending_
.front();
1659 tasks_pending_
.pop();
1661 request_task
->Run();
1665 void CookieMonster::EnsureCookiesMapIsValid() {
1666 lock_
.AssertAcquired();
1668 int num_duplicates_trimmed
= 0;
1670 // Iterate through all the of the cookies, grouped by host.
1671 CookieMap::iterator prev_range_end
= cookies_
.begin();
1672 while (prev_range_end
!= cookies_
.end()) {
1673 CookieMap::iterator cur_range_begin
= prev_range_end
;
1674 const std::string key
= cur_range_begin
->first
; // Keep a copy.
1675 CookieMap::iterator cur_range_end
= cookies_
.upper_bound(key
);
1676 prev_range_end
= cur_range_end
;
1678 // Ensure no equivalent cookies for this host.
1679 num_duplicates_trimmed
+=
1680 TrimDuplicateCookiesForKey(key
, cur_range_begin
, cur_range_end
);
1683 // Record how many duplicates were found in the database.
1684 // See InitializeHistograms() for details.
1685 histogram_number_duplicate_db_cookies_
->Add(num_duplicates_trimmed
);
1688 int CookieMonster::TrimDuplicateCookiesForKey(const std::string
& key
,
1689 CookieMap::iterator begin
,
1690 CookieMap::iterator end
) {
1691 lock_
.AssertAcquired();
1693 // Set of cookies ordered by creation time.
1694 typedef std::set
<CookieMap::iterator
, OrderByCreationTimeDesc
> CookieSet
;
1696 // Helper map we populate to find the duplicates.
1697 typedef std::map
<CookieSignature
, CookieSet
> EquivalenceMap
;
1698 EquivalenceMap equivalent_cookies
;
1700 // The number of duplicate cookies that have been found.
1701 int num_duplicates
= 0;
1703 // Iterate through all of the cookies in our range, and insert them into
1704 // the equivalence map.
1705 for (CookieMap::iterator it
= begin
; it
!= end
; ++it
) {
1706 DCHECK_EQ(key
, it
->first
);
1707 CanonicalCookie
* cookie
= it
->second
;
1709 CookieSignature
signature(cookie
->Name(), cookie
->Domain(), cookie
->Path());
1710 CookieSet
& set
= equivalent_cookies
[signature
];
1712 // We found a duplicate!
1716 // We save the iterator into |cookies_| rather than the actual cookie
1717 // pointer, since we may need to delete it later.
1718 bool insert_success
= set
.insert(it
).second
;
1719 DCHECK(insert_success
)
1720 << "Duplicate creation times found in duplicate cookie name scan.";
1723 // If there were no duplicates, we are done!
1724 if (num_duplicates
== 0)
1727 // Make sure we find everything below that we did above.
1728 int num_duplicates_found
= 0;
1730 // Otherwise, delete all the duplicate cookies, both from our in-memory store
1731 // and from the backing store.
1732 for (EquivalenceMap::iterator it
= equivalent_cookies
.begin();
1733 it
!= equivalent_cookies
.end(); ++it
) {
1734 const CookieSignature
& signature
= it
->first
;
1735 CookieSet
& dupes
= it
->second
;
1737 if (dupes
.size() <= 1)
1738 continue; // This cookiename/path has no duplicates.
1739 num_duplicates_found
+= dupes
.size() - 1;
1741 // Since |dups| is sorted by creation time (descending), the first cookie
1742 // is the most recent one, so we will keep it. The rest are duplicates.
1743 dupes
.erase(dupes
.begin());
1745 LOG(ERROR
) << base::StringPrintf(
1746 "Found %d duplicate cookies for host='%s', "
1747 "with {name='%s', domain='%s', path='%s'}",
1748 static_cast<int>(dupes
.size()), key
.c_str(), signature
.name
.c_str(),
1749 signature
.domain
.c_str(), signature
.path
.c_str());
1751 // Remove all the cookies identified by |dupes|. It is valid to delete our
1752 // list of iterators one at a time, since |cookies_| is a multimap (they
1753 // don't invalidate existing iterators following deletion).
1754 for (CookieSet::iterator dupes_it
= dupes
.begin(); dupes_it
!= dupes
.end();
1756 InternalDeleteCookie(*dupes_it
, true,
1757 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE
);
1760 DCHECK_EQ(num_duplicates
, num_duplicates_found
);
1762 return num_duplicates
;
1765 // Note: file must be the last scheme.
1766 const char* const CookieMonster::kDefaultCookieableSchemes
[] = {"http",
1771 const int CookieMonster::kDefaultCookieableSchemesCount
=
1772 arraysize(kDefaultCookieableSchemes
);
1774 void CookieMonster::SetDefaultCookieableSchemes() {
1775 // Always disable file scheme unless SetEnableFileScheme(true) is called.
1776 SetCookieableSchemes(kDefaultCookieableSchemes
,
1777 kDefaultCookieableSchemesCount
- 1);
1780 void CookieMonster::FindCookiesForHostAndDomain(
1782 const CookieOptions
& options
,
1783 bool update_access_time
,
1784 std::vector
<CanonicalCookie
*>* cookies
) {
1785 lock_
.AssertAcquired();
1787 const Time
current_time(CurrentTime());
1789 // Probe to save statistics relatively frequently. We do it here rather
1790 // than in the set path as many websites won't set cookies, and we
1791 // want to collect statistics whenever the browser's being used.
1792 RecordPeriodicStats(current_time
);
1794 // Can just dispatch to FindCookiesForKey
1795 const std::string
key(GetKey(url
.host()));
1796 FindCookiesForKey(key
, url
, options
, current_time
, update_access_time
,
1800 void CookieMonster::FindCookiesForKey(const std::string
& key
,
1802 const CookieOptions
& options
,
1803 const Time
& current
,
1804 bool update_access_time
,
1805 std::vector
<CanonicalCookie
*>* cookies
) {
1806 lock_
.AssertAcquired();
1808 for (CookieMapItPair its
= cookies_
.equal_range(key
);
1809 its
.first
!= its
.second
;) {
1810 CookieMap::iterator curit
= its
.first
;
1811 CanonicalCookie
* cc
= curit
->second
;
1814 // If the cookie is expired, delete it.
1815 if (cc
->IsExpired(current
) && !keep_expired_cookies_
) {
1816 InternalDeleteCookie(curit
, true, DELETE_COOKIE_EXPIRED
);
1820 // Filter out cookies that should not be included for a request to the
1821 // given |url|. HTTP only cookies are filtered depending on the passed
1822 // cookie |options|.
1823 if (!cc
->IncludeForRequestURL(url
, options
))
1826 // Add this cookie to the set of matching cookies. Update the access
1827 // time if we've been requested to do so.
1828 if (update_access_time
) {
1829 InternalUpdateCookieAccessTime(cc
, current
);
1831 cookies
->push_back(cc
);
1835 bool CookieMonster::DeleteAnyEquivalentCookie(const std::string
& key
,
1836 const CanonicalCookie
& ecc
,
1838 bool already_expired
) {
1839 lock_
.AssertAcquired();
1841 bool found_equivalent_cookie
= false;
1842 bool skipped_httponly
= false;
1843 for (CookieMapItPair its
= cookies_
.equal_range(key
);
1844 its
.first
!= its
.second
;) {
1845 CookieMap::iterator curit
= its
.first
;
1846 CanonicalCookie
* cc
= curit
->second
;
1849 if (ecc
.IsEquivalent(*cc
)) {
1850 // We should never have more than one equivalent cookie, since they should
1851 // overwrite each other.
1852 CHECK(!found_equivalent_cookie
)
1853 << "Duplicate equivalent cookies found, cookie store is corrupted.";
1854 if (skip_httponly
&& cc
->IsHttpOnly()) {
1855 skipped_httponly
= true;
1857 InternalDeleteCookie(curit
, true, already_expired
1858 ? DELETE_COOKIE_EXPIRED_OVERWRITE
1859 : DELETE_COOKIE_OVERWRITE
);
1861 found_equivalent_cookie
= true;
1864 return skipped_httponly
;
1867 CookieMonster::CookieMap::iterator
CookieMonster::InternalInsertCookie(
1868 const std::string
& key
,
1869 CanonicalCookie
* cc
,
1870 bool sync_to_store
) {
1871 // TODO(mkwst): Remove ScopedTracker below once crbug.com/456373 is fixed.
1872 tracked_objects::ScopedTracker
tracking_profile(
1873 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1874 "456373 CookieMonster::InternalInsertCookie"));
1875 lock_
.AssertAcquired();
1877 if ((cc
->IsPersistent() || persist_session_cookies_
) && store_
.get() &&
1879 store_
->AddCookie(*cc
);
1880 CookieMap::iterator inserted
=
1881 cookies_
.insert(CookieMap::value_type(key
, cc
));
1882 if (delegate_
.get()) {
1883 delegate_
->OnCookieChanged(*cc
, false,
1884 CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
);
1887 // See InitializeHistograms() for details.
1888 int32_t sample
= cc
->IsFirstPartyOnly() ? 1 << COOKIE_TYPE_FIRSTPARTYONLY
: 0;
1889 sample
|= cc
->IsHttpOnly() ? 1 << COOKIE_TYPE_HTTPONLY
: 0;
1890 sample
|= cc
->IsSecure() ? 1 << COOKIE_TYPE_SECURE
: 0;
1891 histogram_cookie_type_
->Add(sample
);
1893 RunCallbacks(*cc
, false);
1898 bool CookieMonster::SetCookieWithCreationTimeAndOptions(
1900 const std::string
& cookie_line
,
1901 const Time
& creation_time_or_null
,
1902 const CookieOptions
& options
) {
1903 lock_
.AssertAcquired();
1905 VLOG(kVlogSetCookies
) << "SetCookie() line: " << cookie_line
;
1907 Time creation_time
= creation_time_or_null
;
1908 if (creation_time
.is_null()) {
1909 creation_time
= CurrentTime();
1910 last_time_seen_
= creation_time
;
1913 scoped_ptr
<CanonicalCookie
> cc(
1914 CanonicalCookie::Create(url
, cookie_line
, creation_time
, options
));
1917 VLOG(kVlogSetCookies
) << "WARNING: Failed to allocate CanonicalCookie";
1920 return SetCanonicalCookie(&cc
, creation_time
, options
);
1923 bool CookieMonster::SetCanonicalCookie(scoped_ptr
<CanonicalCookie
>* cc
,
1924 const Time
& creation_time
,
1925 const CookieOptions
& options
) {
1926 const std::string
key(GetKey((*cc
)->Domain()));
1927 bool already_expired
= (*cc
)->IsExpired(creation_time
);
1929 if (DeleteAnyEquivalentCookie(key
, **cc
, options
.exclude_httponly(),
1931 VLOG(kVlogSetCookies
) << "SetCookie() not clobbering httponly cookie";
1935 VLOG(kVlogSetCookies
) << "SetCookie() key: " << key
1936 << " cc: " << (*cc
)->DebugString();
1938 // Realize that we might be setting an expired cookie, and the only point
1939 // was to delete the cookie which we've already done.
1940 if (!already_expired
|| keep_expired_cookies_
) {
1941 // See InitializeHistograms() for details.
1942 if ((*cc
)->IsPersistent()) {
1943 histogram_expiration_duration_minutes_
->Add(
1944 ((*cc
)->ExpiryDate() - creation_time
).InMinutes());
1948 CanonicalCookie cookie
= *(cc
->get());
1949 InternalInsertCookie(key
, cc
->release(), true);
1952 VLOG(kVlogSetCookies
) << "SetCookie() not storing already expired cookie.";
1955 // We assume that hopefully setting a cookie will be less common than
1956 // querying a cookie. Since setting a cookie can put us over our limits,
1957 // make sure that we garbage collect... We can also make the assumption that
1958 // if a cookie was set, in the common case it will be used soon after,
1959 // and we will purge the expired cookies in GetCookies().
1960 GarbageCollect(creation_time
, key
);
1965 bool CookieMonster::SetCanonicalCookies(const CookieList
& list
) {
1966 base::AutoLock
autolock(lock_
);
1968 CookieOptions options
;
1969 options
.set_include_httponly();
1971 for (CookieList::const_iterator it
= list
.begin(); it
!= list
.end(); ++it
) {
1972 scoped_ptr
<CanonicalCookie
> canonical_cookie(new CanonicalCookie(*it
));
1973 if (!SetCanonicalCookie(&canonical_cookie
, it
->CreationDate(), options
))
1980 void CookieMonster::InternalUpdateCookieAccessTime(CanonicalCookie
* cc
,
1981 const Time
& current
) {
1982 lock_
.AssertAcquired();
1984 // Based off the Mozilla code. When a cookie has been accessed recently,
1985 // don't bother updating its access time again. This reduces the number of
1986 // updates we do during pageload, which in turn reduces the chance our storage
1987 // backend will hit its batch thresholds and be forced to update.
1988 if ((current
- cc
->LastAccessDate()) < last_access_threshold_
)
1991 // See InitializeHistograms() for details.
1992 histogram_between_access_interval_minutes_
->Add(
1993 (current
- cc
->LastAccessDate()).InMinutes());
1995 cc
->SetLastAccessDate(current
);
1996 if ((cc
->IsPersistent() || persist_session_cookies_
) && store_
.get())
1997 store_
->UpdateCookieAccessTime(*cc
);
2000 // InternalDeleteCookies must not invalidate iterators other than the one being
2002 void CookieMonster::InternalDeleteCookie(CookieMap::iterator it
,
2004 DeletionCause deletion_cause
) {
2005 lock_
.AssertAcquired();
2007 // Ideally, this would be asserted up where we define ChangeCauseMapping,
2008 // but DeletionCause's visibility (or lack thereof) forces us to make
2010 static_assert(arraysize(ChangeCauseMapping
) == DELETE_COOKIE_LAST_ENTRY
+ 1,
2011 "ChangeCauseMapping size should match DeletionCause size");
2013 // See InitializeHistograms() for details.
2014 if (deletion_cause
!= DELETE_COOKIE_DONT_RECORD
)
2015 histogram_cookie_deletion_cause_
->Add(deletion_cause
);
2017 CanonicalCookie
* cc
= it
->second
;
2018 VLOG(kVlogSetCookies
) << "InternalDeleteCookie()"
2019 << ", cause:" << deletion_cause
2020 << ", cc: " << cc
->DebugString();
2022 if ((cc
->IsPersistent() || persist_session_cookies_
) && store_
.get() &&
2024 store_
->DeleteCookie(*cc
);
2025 if (delegate_
.get()) {
2026 ChangeCausePair mapping
= ChangeCauseMapping
[deletion_cause
];
2029 delegate_
->OnCookieChanged(*cc
, true, mapping
.cause
);
2031 RunCallbacks(*cc
, true);
2036 // Domain expiry behavior is unchanged by key/expiry scheme (the
2037 // meaning of the key is different, but that's not visible to this routine).
2038 int CookieMonster::GarbageCollect(const Time
& current
, const std::string
& key
) {
2039 lock_
.AssertAcquired();
2041 int num_deleted
= 0;
2042 Time
safe_date(Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays
));
2044 // Collect garbage for this key, minding cookie priorities.
2045 if (cookies_
.count(key
) > kDomainMaxCookies
) {
2046 VLOG(kVlogGarbageCollection
) << "GarbageCollect() key: " << key
;
2048 CookieItVector cookie_its
;
2050 GarbageCollectExpired(current
, cookies_
.equal_range(key
), &cookie_its
);
2051 if (cookie_its
.size() > kDomainMaxCookies
) {
2052 VLOG(kVlogGarbageCollection
) << "Deep Garbage Collect domain.";
2054 cookie_its
.size() - (kDomainMaxCookies
- kDomainPurgeCookies
);
2055 DCHECK(purge_goal
> kDomainPurgeCookies
);
2057 // Boundary iterators into |cookie_its| for different priorities.
2058 CookieItVector::iterator it_bdd
[4];
2059 // Intialize |it_bdd| while sorting |cookie_its| by priorities.
2060 // Schematic: [MLLHMHHLMM] => [LLL|MMMM|HHH], with 4 boundaries.
2061 it_bdd
[0] = cookie_its
.begin();
2062 it_bdd
[3] = cookie_its
.end();
2064 PartitionCookieByPriority(it_bdd
[0], it_bdd
[3], COOKIE_PRIORITY_LOW
);
2065 it_bdd
[2] = PartitionCookieByPriority(it_bdd
[1], it_bdd
[3],
2066 COOKIE_PRIORITY_MEDIUM
);
2067 size_t quota
[3] = {kDomainCookiesQuotaLow
,
2068 kDomainCookiesQuotaMedium
,
2069 kDomainCookiesQuotaHigh
};
2071 // Purge domain cookies in 3 rounds.
2072 // Round 1: consider low-priority cookies only: evict least-recently
2073 // accessed, while protecting quota[0] of these from deletion.
2074 // Round 2: consider {low, medium}-priority cookies, evict least-recently
2075 // accessed, while protecting quota[0] + quota[1].
2076 // Round 3: consider all cookies, evict least-recently accessed.
2077 size_t accumulated_quota
= 0;
2078 CookieItVector::iterator it_purge_begin
= it_bdd
[0];
2079 for (int i
= 0; i
< 3 && purge_goal
> 0; ++i
) {
2080 accumulated_quota
+= quota
[i
];
2082 size_t num_considered
= it_bdd
[i
+ 1] - it_purge_begin
;
2083 if (num_considered
<= accumulated_quota
)
2086 // Number of cookies that will be purged in this round.
2088 std::min(purge_goal
, num_considered
- accumulated_quota
);
2089 purge_goal
-= round_goal
;
2091 SortLeastRecentlyAccessed(it_purge_begin
, it_bdd
[i
+ 1], round_goal
);
2092 // Cookies accessed on or after |safe_date| would have been safe from
2093 // global purge, and we want to keep track of this.
2094 CookieItVector::iterator it_purge_end
= it_purge_begin
+ round_goal
;
2095 CookieItVector::iterator it_purge_middle
=
2096 LowerBoundAccessDate(it_purge_begin
, it_purge_end
, safe_date
);
2097 // Delete cookies accessed before |safe_date|.
2098 num_deleted
+= GarbageCollectDeleteRange(
2099 current
, DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
, it_purge_begin
,
2101 // Delete cookies accessed on or after |safe_date|.
2102 num_deleted
+= GarbageCollectDeleteRange(
2103 current
, DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
, it_purge_middle
,
2105 it_purge_begin
= it_purge_end
;
2107 DCHECK_EQ(0U, purge_goal
);
2111 // Collect garbage for everything. With firefox style we want to preserve
2112 // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict.
2113 if (cookies_
.size() > kMaxCookies
&& earliest_access_time_
< safe_date
) {
2114 VLOG(kVlogGarbageCollection
) << "GarbageCollect() everything";
2115 CookieItVector cookie_its
;
2116 num_deleted
+= GarbageCollectExpired(
2117 current
, CookieMapItPair(cookies_
.begin(), cookies_
.end()),
2119 if (cookie_its
.size() > kMaxCookies
) {
2120 VLOG(kVlogGarbageCollection
) << "Deep Garbage Collect everything.";
2121 size_t purge_goal
= cookie_its
.size() - (kMaxCookies
- kPurgeCookies
);
2122 DCHECK(purge_goal
> kPurgeCookies
);
2123 // Sorts up to *and including* |cookie_its[purge_goal]|, so
2124 // |earliest_access_time| will be properly assigned even if
2125 // |global_purge_it| == |cookie_its.begin() + purge_goal|.
2126 SortLeastRecentlyAccessed(cookie_its
.begin(), cookie_its
.end(),
2128 // Find boundary to cookies older than safe_date.
2129 CookieItVector::iterator global_purge_it
= LowerBoundAccessDate(
2130 cookie_its
.begin(), cookie_its
.begin() + purge_goal
, safe_date
);
2131 // Only delete the old cookies.
2133 GarbageCollectDeleteRange(current
, DELETE_COOKIE_EVICTED_GLOBAL
,
2134 cookie_its
.begin(), global_purge_it
);
2135 // Set access day to the oldest cookie that wasn't deleted.
2136 earliest_access_time_
= (*global_purge_it
)->second
->LastAccessDate();
2143 int CookieMonster::GarbageCollectExpired(const Time
& current
,
2144 const CookieMapItPair
& itpair
,
2145 CookieItVector
* cookie_its
) {
2146 if (keep_expired_cookies_
)
2149 lock_
.AssertAcquired();
2151 int num_deleted
= 0;
2152 for (CookieMap::iterator it
= itpair
.first
, end
= itpair
.second
; it
!= end
;) {
2153 CookieMap::iterator curit
= it
;
2156 if (curit
->second
->IsExpired(current
)) {
2157 InternalDeleteCookie(curit
, true, DELETE_COOKIE_EXPIRED
);
2159 } else if (cookie_its
) {
2160 cookie_its
->push_back(curit
);
2167 int CookieMonster::GarbageCollectDeleteRange(const Time
& current
,
2168 DeletionCause cause
,
2169 CookieItVector::iterator it_begin
,
2170 CookieItVector::iterator it_end
) {
2171 for (CookieItVector::iterator it
= it_begin
; it
!= it_end
; it
++) {
2172 histogram_evicted_last_access_minutes_
->Add(
2173 (current
- (*it
)->second
->LastAccessDate()).InMinutes());
2174 InternalDeleteCookie((*it
), true, cause
);
2176 return it_end
- it_begin
;
2179 // A wrapper around registry_controlled_domains::GetDomainAndRegistry
2180 // to make clear we're creating a key for our local map. Here and
2181 // in FindCookiesForHostAndDomain() are the only two places where
2182 // we need to conditionalize based on key type.
2184 // Note that this key algorithm explicitly ignores the scheme. This is
2185 // because when we're entering cookies into the map from the backing store,
2186 // we in general won't have the scheme at that point.
2187 // In practical terms, this means that file cookies will be stored
2188 // in the map either by an empty string or by UNC name (and will be
2189 // limited by kMaxCookiesPerHost), and extension cookies will be stored
2190 // based on the single extension id, as the extension id won't have the
2191 // form of a DNS host and hence GetKey() will return it unchanged.
2193 // Arguably the right thing to do here is to make the key
2194 // algorithm dependent on the scheme, and make sure that the scheme is
2195 // available everywhere the key must be obtained (specfically at backing
2196 // store load time). This would require either changing the backing store
2197 // database schema to include the scheme (far more trouble than it's worth), or
2198 // separating out file cookies into their own CookieMonster instance and
2199 // thus restricting each scheme to a single cookie monster (which might
2200 // be worth it, but is still too much trouble to solve what is currently a
2202 std::string
CookieMonster::GetKey(const std::string
& domain
) const {
2203 std::string
effective_domain(
2204 registry_controlled_domains::GetDomainAndRegistry(
2205 domain
, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
));
2206 if (effective_domain
.empty())
2207 effective_domain
= domain
;
2209 if (!effective_domain
.empty() && effective_domain
[0] == '.')
2210 return effective_domain
.substr(1);
2211 return effective_domain
;
2214 bool CookieMonster::IsCookieableScheme(const std::string
& scheme
) {
2215 base::AutoLock
autolock(lock_
);
2217 return std::find(cookieable_schemes_
.begin(), cookieable_schemes_
.end(),
2218 scheme
) != cookieable_schemes_
.end();
2221 bool CookieMonster::HasCookieableScheme(const GURL
& url
) {
2222 lock_
.AssertAcquired();
2224 // Make sure the request is on a cookie-able url scheme.
2225 for (size_t i
= 0; i
< cookieable_schemes_
.size(); ++i
) {
2226 // We matched a scheme.
2227 if (url
.SchemeIs(cookieable_schemes_
[i
].c_str())) {
2228 // We've matched a supported scheme.
2233 // The scheme didn't match any in our whitelist.
2234 VLOG(kVlogPerCookieMonster
)
2235 << "WARNING: Unsupported cookie scheme: " << url
.scheme();
2239 // Test to see if stats should be recorded, and record them if so.
2240 // The goal here is to get sampling for the average browser-hour of
2241 // activity. We won't take samples when the web isn't being surfed,
2242 // and when the web is being surfed, we'll take samples about every
2243 // kRecordStatisticsIntervalSeconds.
2244 // last_statistic_record_time_ is initialized to Now() rather than null
2245 // in the constructor so that we won't take statistics right after
2246 // startup, to avoid bias from browsers that are started but not used.
2247 void CookieMonster::RecordPeriodicStats(const base::Time
& current_time
) {
2248 const base::TimeDelta
kRecordStatisticsIntervalTime(
2249 base::TimeDelta::FromSeconds(kRecordStatisticsIntervalSeconds
));
2251 // If we've taken statistics recently, return.
2252 if (current_time
- last_statistic_record_time_
<=
2253 kRecordStatisticsIntervalTime
) {
2257 // See InitializeHistograms() for details.
2258 histogram_count_
->Add(cookies_
.size());
2260 // More detailed statistics on cookie counts at different granularities.
2261 TimeTicks
beginning_of_time(TimeTicks::Now());
2263 for (CookieMap::const_iterator it_key
= cookies_
.begin();
2264 it_key
!= cookies_
.end();) {
2265 const std::string
& key(it_key
->first
);
2268 typedef std::map
<std::string
, unsigned int> DomainMap
;
2269 DomainMap domain_map
;
2270 CookieMapItPair its_cookies
= cookies_
.equal_range(key
);
2271 while (its_cookies
.first
!= its_cookies
.second
) {
2273 const std::string
& cookie_domain(its_cookies
.first
->second
->Domain());
2274 domain_map
[cookie_domain
]++;
2276 its_cookies
.first
++;
2278 histogram_etldp1_count_
->Add(key_count
);
2279 histogram_domain_per_etldp1_count_
->Add(domain_map
.size());
2280 for (DomainMap::const_iterator domain_map_it
= domain_map
.begin();
2281 domain_map_it
!= domain_map
.end(); domain_map_it
++)
2282 histogram_domain_count_
->Add(domain_map_it
->second
);
2284 it_key
= its_cookies
.second
;
2287 VLOG(kVlogPeriodic
) << "Time for recording cookie stats (us): "
2288 << (TimeTicks::Now() - beginning_of_time
)
2291 last_statistic_record_time_
= current_time
;
2294 // Initialize all histogram counter variables used in this class.
2296 // Normal histogram usage involves using the macros defined in
2297 // histogram.h, which automatically takes care of declaring these
2298 // variables (as statics), initializing them, and accumulating into
2299 // them, all from a single entry point. Unfortunately, that solution
2300 // doesn't work for the CookieMonster, as it's vulnerable to races between
2301 // separate threads executing the same functions and hence initializing the
2302 // same static variables. There isn't a race danger in the histogram
2303 // accumulation calls; they are written to be resilient to simultaneous
2304 // calls from multiple threads.
2306 // The solution taken here is to have per-CookieMonster instance
2307 // variables that are constructed during CookieMonster construction.
2308 // Note that these variables refer to the same underlying histogram,
2309 // so we still race (but safely) with other CookieMonster instances
2310 // for accumulation.
2312 // To do this we've expanded out the individual histogram macros calls,
2313 // with declarations of the variables in the class decl, initialization here
2314 // (done from the class constructor) and direct calls to the accumulation
2315 // methods where needed. The specific histogram macro calls on which the
2316 // initialization is based are included in comments below.
2317 void CookieMonster::InitializeHistograms() {
2318 // From UMA_HISTOGRAM_CUSTOM_COUNTS
2319 histogram_expiration_duration_minutes_
= base::Histogram::FactoryGet(
2320 "Cookie.ExpirationDurationMinutes", 1, kMinutesInTenYears
, 50,
2321 base::Histogram::kUmaTargetedHistogramFlag
);
2322 histogram_between_access_interval_minutes_
= base::Histogram::FactoryGet(
2323 "Cookie.BetweenAccessIntervalMinutes", 1, kMinutesInTenYears
, 50,
2324 base::Histogram::kUmaTargetedHistogramFlag
);
2325 histogram_evicted_last_access_minutes_
= base::Histogram::FactoryGet(
2326 "Cookie.EvictedLastAccessMinutes", 1, kMinutesInTenYears
, 50,
2327 base::Histogram::kUmaTargetedHistogramFlag
);
2328 histogram_count_
= base::Histogram::FactoryGet(
2329 "Cookie.Count", 1, 4000, 50, base::Histogram::kUmaTargetedHistogramFlag
);
2330 histogram_domain_count_
=
2331 base::Histogram::FactoryGet("Cookie.DomainCount", 1, 4000, 50,
2332 base::Histogram::kUmaTargetedHistogramFlag
);
2333 histogram_etldp1_count_
=
2334 base::Histogram::FactoryGet("Cookie.Etldp1Count", 1, 4000, 50,
2335 base::Histogram::kUmaTargetedHistogramFlag
);
2336 histogram_domain_per_etldp1_count_
=
2337 base::Histogram::FactoryGet("Cookie.DomainPerEtldp1Count", 1, 4000, 50,
2338 base::Histogram::kUmaTargetedHistogramFlag
);
2340 // From UMA_HISTOGRAM_COUNTS_10000 & UMA_HISTOGRAM_CUSTOM_COUNTS
2341 histogram_number_duplicate_db_cookies_
=
2342 base::Histogram::FactoryGet("Net.NumDuplicateCookiesInDb", 1, 10000, 50,
2343 base::Histogram::kUmaTargetedHistogramFlag
);
2345 // From UMA_HISTOGRAM_ENUMERATION
2346 histogram_cookie_deletion_cause_
= base::LinearHistogram::FactoryGet(
2347 "Cookie.DeletionCause", 1, DELETE_COOKIE_LAST_ENTRY
- 1,
2348 DELETE_COOKIE_LAST_ENTRY
, base::Histogram::kUmaTargetedHistogramFlag
);
2349 histogram_cookie_type_
= base::LinearHistogram::FactoryGet(
2350 "Cookie.Type", 1, (1 << COOKIE_TYPE_LAST_ENTRY
) - 1,
2351 1 << COOKIE_TYPE_LAST_ENTRY
, base::Histogram::kUmaTargetedHistogramFlag
);
2353 // From UMA_HISTOGRAM_{CUSTOM_,}TIMES
2354 histogram_time_blocked_on_load_
= base::Histogram::FactoryTimeGet(
2355 "Cookie.TimeBlockedOnLoad", base::TimeDelta::FromMilliseconds(1),
2356 base::TimeDelta::FromMinutes(1), 50,
2357 base::Histogram::kUmaTargetedHistogramFlag
);
2360 // The system resolution is not high enough, so we can have multiple
2361 // set cookies that result in the same system time. When this happens, we
2362 // increment by one Time unit. Let's hope computers don't get too fast.
2363 Time
CookieMonster::CurrentTime() {
2364 return std::max(Time::Now(), Time::FromInternalValue(
2365 last_time_seen_
.ToInternalValue() + 1));
2368 void CookieMonster::ComputeCookieDiff(CookieList
* old_cookies
,
2369 CookieList
* new_cookies
,
2370 CookieList
* cookies_to_add
,
2371 CookieList
* cookies_to_delete
) {
2372 DCHECK(old_cookies
);
2373 DCHECK(new_cookies
);
2374 DCHECK(cookies_to_add
);
2375 DCHECK(cookies_to_delete
);
2376 DCHECK(cookies_to_add
->empty());
2377 DCHECK(cookies_to_delete
->empty());
2380 // A set ordered by FullDiffCookieSorter is also ordered by
2381 // PartialDiffCookieSorter.
2382 std::sort(old_cookies
->begin(), old_cookies
->end(), FullDiffCookieSorter
);
2383 std::sort(new_cookies
->begin(), new_cookies
->end(), FullDiffCookieSorter
);
2385 // Select any old cookie for deletion if no new cookie has the same name,
2386 // domain, and path.
2387 std::set_difference(
2388 old_cookies
->begin(), old_cookies
->end(), new_cookies
->begin(),
2390 std::inserter(*cookies_to_delete
, cookies_to_delete
->begin()),
2391 PartialDiffCookieSorter
);
2393 // Select any new cookie for addition (or update) if no old cookie is exactly
2395 std::set_difference(new_cookies
->begin(), new_cookies
->end(),
2396 old_cookies
->begin(), old_cookies
->end(),
2397 std::inserter(*cookies_to_add
, cookies_to_add
->begin()),
2398 FullDiffCookieSorter
);
2401 scoped_ptr
<CookieStore::CookieChangedSubscription
>
2402 CookieMonster::AddCallbackForCookie(const GURL
& gurl
,
2403 const std::string
& name
,
2404 const CookieChangedCallback
& callback
) {
2405 base::AutoLock
autolock(lock_
);
2406 std::pair
<GURL
, std::string
> key(gurl
, name
);
2407 if (hook_map_
.count(key
) == 0)
2408 hook_map_
[key
] = make_linked_ptr(new CookieChangedCallbackList());
2409 return hook_map_
[key
]->Add(
2410 base::Bind(&RunAsync
, base::ThreadTaskRunnerHandle::Get(), callback
));
2413 #if defined(OS_ANDROID)
2414 void CookieMonster::SetEnableFileScheme(bool accept
) {
2415 // This assumes "file" is always at the end of the array. See the comment
2416 // above kDefaultCookieableSchemes.
2418 // TODO(mkwst): We're keeping this method around to support the
2419 // 'CookieManager::setAcceptFileSchemeCookies' method on Android's WebView;
2420 // if/when we can deprecate and remove that method, we can remove this one
2421 // as well. Until then, we'll just ensure that the method has no effect on
2422 // non-android systems.
2423 int num_schemes
= accept
? kDefaultCookieableSchemesCount
2424 : kDefaultCookieableSchemesCount
- 1;
2426 SetCookieableSchemes(kDefaultCookieableSchemes
, num_schemes
);
2430 void CookieMonster::RunCallbacks(const CanonicalCookie
& cookie
, bool removed
) {
2431 lock_
.AssertAcquired();
2433 opts
.set_include_httponly();
2434 opts
.set_include_first_party_only();
2435 // Note that the callbacks in hook_map_ are wrapped with MakeAsync(), so they
2436 // are guaranteed to not take long - they just post a RunAsync task back to
2437 // the appropriate thread's message loop and return. It is important that this
2438 // method not run user-supplied callbacks directly, since the CookieMonster
2439 // lock is held and it is easy to accidentally introduce deadlocks.
2440 for (CookieChangedHookMap::iterator it
= hook_map_
.begin();
2441 it
!= hook_map_
.end(); ++it
) {
2442 std::pair
<GURL
, std::string
> key
= it
->first
;
2443 if (cookie
.IncludeForRequestURL(key
.first
, opts
) &&
2444 cookie
.Name() == key
.second
) {
2445 it
->second
->Notify(cookie
, removed
);