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 kVlogGarbageCollection
= 5;
150 const int kVlogSetCookies
= 7;
151 const int kVlogGetCookies
= 9;
153 // Mozilla sorts on the path length (longest first), and then it
154 // sorts by creation time (oldest first).
155 // The RFC says the sort order for the domain attribute is undefined.
156 bool CookieSorter(CanonicalCookie
* cc1
, CanonicalCookie
* cc2
) {
157 if (cc1
->Path().length() == cc2
->Path().length())
158 return cc1
->CreationDate() < cc2
->CreationDate();
159 return cc1
->Path().length() > cc2
->Path().length();
162 bool LRACookieSorter(const CookieMonster::CookieMap::iterator
& it1
,
163 const CookieMonster::CookieMap::iterator
& it2
) {
164 // Cookies accessed less recently should be deleted first.
165 if (it1
->second
->LastAccessDate() != it2
->second
->LastAccessDate())
166 return it1
->second
->LastAccessDate() < it2
->second
->LastAccessDate();
168 // In rare cases we might have two cookies with identical last access times.
169 // To preserve the stability of the sort, in these cases prefer to delete
170 // older cookies over newer ones. CreationDate() is guaranteed to be unique.
171 return it1
->second
->CreationDate() < it2
->second
->CreationDate();
174 // Compare cookies using name, domain and path, so that "equivalent" cookies
175 // (per RFC 2965) are equal to each other.
176 bool PartialDiffCookieSorter(const CanonicalCookie
& a
,
177 const CanonicalCookie
& b
) {
178 return a
.PartialCompare(b
);
181 // This is a stricter ordering than PartialDiffCookieOrdering, where all fields
183 bool FullDiffCookieSorter(const CanonicalCookie
& a
, const CanonicalCookie
& b
) {
184 return a
.FullCompare(b
);
187 // Our strategy to find duplicates is:
188 // (1) Build a map from (cookiename, cookiepath) to
189 // {list of cookies with this signature, sorted by creation time}.
190 // (2) For each list with more than 1 entry, keep the cookie having the
191 // most recent creation time, and delete the others.
193 // Two cookies are considered equivalent if they have the same domain,
195 struct CookieSignature
{
197 CookieSignature(const std::string
& name
,
198 const std::string
& domain
,
199 const std::string
& path
)
200 : name(name
), domain(domain
), path(path
) {}
202 // To be a key for a map this class needs to be assignable, copyable,
203 // and have an operator<. The default assignment operator
204 // and copy constructor are exactly what we want.
206 bool operator<(const CookieSignature
& cs
) const {
207 // Name compare dominates, then domain, then path.
208 int diff
= name
.compare(cs
.name
);
212 diff
= domain
.compare(cs
.domain
);
216 return path
.compare(cs
.path
) < 0;
224 // For a CookieItVector iterator range [|it_begin|, |it_end|),
225 // sorts the first |num_sort| + 1 elements by LastAccessDate().
226 // The + 1 element exists so for any interval of length <= |num_sort| starting
227 // from |cookies_its_begin|, a LastAccessDate() bound can be found.
228 void SortLeastRecentlyAccessed(CookieMonster::CookieItVector::iterator it_begin
,
229 CookieMonster::CookieItVector::iterator it_end
,
231 DCHECK_LT(static_cast<int>(num_sort
), it_end
- it_begin
);
232 std::partial_sort(it_begin
, it_begin
+ num_sort
+ 1, it_end
, LRACookieSorter
);
235 // Predicate to support PartitionCookieByPriority().
236 struct CookiePriorityEqualsTo
237 : std::unary_function
<const CookieMonster::CookieMap::iterator
, bool> {
238 explicit CookiePriorityEqualsTo(CookiePriority priority
)
239 : priority_(priority
) {}
241 bool operator()(const CookieMonster::CookieMap::iterator it
) const {
242 return it
->second
->Priority() == priority_
;
245 const CookiePriority priority_
;
248 // For a CookieItVector iterator range [|it_begin|, |it_end|),
249 // moves all cookies with a given |priority| to the beginning of the list.
250 // Returns: An iterator in [it_begin, it_end) to the first element with
251 // priority != |priority|, or |it_end| if all have priority == |priority|.
252 CookieMonster::CookieItVector::iterator
PartitionCookieByPriority(
253 CookieMonster::CookieItVector::iterator it_begin
,
254 CookieMonster::CookieItVector::iterator it_end
,
255 CookiePriority priority
) {
256 return std::partition(it_begin
, it_end
, CookiePriorityEqualsTo(priority
));
259 bool LowerBoundAccessDateComparator(const CookieMonster::CookieMap::iterator it
,
260 const Time
& access_date
) {
261 return it
->second
->LastAccessDate() < access_date
;
264 // For a CookieItVector iterator range [|it_begin|, |it_end|)
265 // from a CookieItVector sorted by LastAccessDate(), returns the
266 // first iterator with access date >= |access_date|, or cookie_its_end if this
268 CookieMonster::CookieItVector::iterator
LowerBoundAccessDate(
269 const CookieMonster::CookieItVector::iterator its_begin
,
270 const CookieMonster::CookieItVector::iterator its_end
,
271 const Time
& access_date
) {
272 return std::lower_bound(its_begin
, its_end
, access_date
,
273 LowerBoundAccessDateComparator
);
276 // Mapping between DeletionCause and CookieMonsterDelegate::ChangeCause; the
277 // mapping also provides a boolean that specifies whether or not an
278 // OnCookieChanged notification ought to be generated.
279 typedef struct ChangeCausePair_struct
{
280 CookieMonsterDelegate::ChangeCause cause
;
283 ChangeCausePair ChangeCauseMapping
[] = {
284 // DELETE_COOKIE_EXPLICIT
285 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
, true},
286 // DELETE_COOKIE_OVERWRITE
287 {CookieMonsterDelegate::CHANGE_COOKIE_OVERWRITE
, true},
288 // DELETE_COOKIE_EXPIRED
289 {CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED
, true},
290 // DELETE_COOKIE_EVICTED
291 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
292 // DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE
293 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
, false},
294 // DELETE_COOKIE_DONT_RECORD
295 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
, false},
296 // DELETE_COOKIE_EVICTED_DOMAIN
297 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
298 // DELETE_COOKIE_EVICTED_GLOBAL
299 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
300 // DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
301 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
302 // DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
303 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
304 // DELETE_COOKIE_EXPIRED_OVERWRITE
305 {CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED_OVERWRITE
, true},
306 // DELETE_COOKIE_CONTROL_CHAR
307 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED
, true},
308 // DELETE_COOKIE_LAST_ENTRY
309 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
, false}};
311 std::string
BuildCookieLine(const CanonicalCookieVector
& cookies
) {
312 std::string cookie_line
;
313 for (CanonicalCookieVector::const_iterator it
= cookies
.begin();
314 it
!= cookies
.end(); ++it
) {
315 if (it
!= cookies
.begin())
317 // In Mozilla if you set a cookie like AAAA, it will have an empty token
318 // and a value of AAAA. When it sends the cookie back, it will send AAAA,
319 // so we need to avoid sending =AAAA for a blank token value.
320 if (!(*it
)->Name().empty())
321 cookie_line
+= (*it
)->Name() + "=";
322 cookie_line
+= (*it
)->Value();
327 void RunAsync(scoped_refptr
<base::TaskRunner
> proxy
,
328 const CookieStore::CookieChangedCallback
& callback
,
329 const CanonicalCookie
& cookie
,
331 proxy
->PostTask(FROM_HERE
, base::Bind(callback
, cookie
, removed
));
336 CookieMonster::CookieMonster(PersistentCookieStore
* store
,
337 CookieMonsterDelegate
* delegate
)
338 : initialized_(false),
339 started_fetching_all_cookies_(false),
340 finished_fetching_all_cookies_(false),
341 fetch_strategy_(kUnknownFetch
),
343 last_access_threshold_(
344 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds
)),
346 last_statistic_record_time_(Time::Now()),
347 keep_expired_cookies_(false),
348 persist_session_cookies_(false) {
349 InitializeHistograms();
350 SetDefaultCookieableSchemes();
353 CookieMonster::CookieMonster(PersistentCookieStore
* store
,
354 CookieMonsterDelegate
* delegate
,
355 int last_access_threshold_milliseconds
)
356 : initialized_(false),
357 started_fetching_all_cookies_(false),
358 finished_fetching_all_cookies_(false),
359 fetch_strategy_(kUnknownFetch
),
361 last_access_threshold_(base::TimeDelta::FromMilliseconds(
362 last_access_threshold_milliseconds
)),
364 last_statistic_record_time_(base::Time::Now()),
365 keep_expired_cookies_(false),
366 persist_session_cookies_(false) {
367 InitializeHistograms();
368 SetDefaultCookieableSchemes();
371 // Task classes for queueing the coming request.
373 class CookieMonster::CookieMonsterTask
374 : public base::RefCountedThreadSafe
<CookieMonsterTask
> {
376 // Runs the task and invokes the client callback on the thread that
377 // originally constructed the task.
378 virtual void Run() = 0;
381 explicit CookieMonsterTask(CookieMonster
* cookie_monster
);
382 virtual ~CookieMonsterTask();
384 // Invokes the callback immediately, if the current thread is the one
385 // that originated the task, or queues the callback for execution on the
386 // appropriate thread. Maintains a reference to this CookieMonsterTask
387 // instance until the callback completes.
388 void InvokeCallback(base::Closure callback
);
390 CookieMonster
* cookie_monster() { return cookie_monster_
; }
393 friend class base::RefCountedThreadSafe
<CookieMonsterTask
>;
395 CookieMonster
* cookie_monster_
;
396 scoped_refptr
<base::SingleThreadTaskRunner
> thread_
;
398 DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask
);
401 CookieMonster::CookieMonsterTask::CookieMonsterTask(
402 CookieMonster
* cookie_monster
)
403 : cookie_monster_(cookie_monster
),
404 thread_(base::ThreadTaskRunnerHandle::Get()) {
407 CookieMonster::CookieMonsterTask::~CookieMonsterTask() {
410 // Unfortunately, one cannot re-bind a Callback with parameters into a closure.
411 // Therefore, the closure passed to InvokeCallback is a clumsy binding of
412 // Callback::Run on a wrapped Callback instance. Since Callback is not
413 // reference counted, we bind to an instance that is a member of the
414 // CookieMonsterTask subclass. Then, we cannot simply post the callback to a
415 // message loop because the underlying instance may be destroyed (along with the
416 // CookieMonsterTask instance) in the interim. Therefore, we post a callback
417 // bound to the CookieMonsterTask, which *is* reference counted (thus preventing
418 // destruction of the original callback), and which invokes the closure (which
419 // invokes the original callback with the returned data).
420 void CookieMonster::CookieMonsterTask::InvokeCallback(base::Closure callback
) {
421 if (thread_
->BelongsToCurrentThread()) {
424 thread_
->PostTask(FROM_HERE
, base::Bind(&CookieMonsterTask::InvokeCallback
,
429 // Task class for SetCookieWithDetails call.
430 class CookieMonster::SetCookieWithDetailsTask
: public CookieMonsterTask
{
432 SetCookieWithDetailsTask(CookieMonster
* cookie_monster
,
434 const std::string
& name
,
435 const std::string
& value
,
436 const std::string
& domain
,
437 const std::string
& path
,
438 const base::Time
& expiration_time
,
441 bool first_party_only
,
442 CookiePriority priority
,
443 const SetCookiesCallback
& callback
)
444 : CookieMonsterTask(cookie_monster
),
450 expiration_time_(expiration_time
),
452 http_only_(http_only
),
453 first_party_only_(first_party_only
),
455 callback_(callback
) {}
457 // CookieMonsterTask:
461 ~SetCookieWithDetailsTask() override
{}
469 base::Time expiration_time_
;
472 bool first_party_only_
;
473 CookiePriority priority_
;
474 SetCookiesCallback callback_
;
476 DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask
);
479 void CookieMonster::SetCookieWithDetailsTask::Run() {
480 bool success
= this->cookie_monster()->SetCookieWithDetails(
481 url_
, name_
, value_
, domain_
, path_
, expiration_time_
, secure_
,
482 http_only_
, first_party_only_
, priority_
);
483 if (!callback_
.is_null()) {
484 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run
,
485 base::Unretained(&callback_
), success
));
489 // Task class for GetAllCookies call.
490 class CookieMonster::GetAllCookiesTask
: public CookieMonsterTask
{
492 GetAllCookiesTask(CookieMonster
* cookie_monster
,
493 const GetCookieListCallback
& callback
)
494 : CookieMonsterTask(cookie_monster
), callback_(callback
) {}
500 ~GetAllCookiesTask() override
{}
503 GetCookieListCallback callback_
;
505 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask
);
508 void CookieMonster::GetAllCookiesTask::Run() {
509 if (!callback_
.is_null()) {
510 CookieList cookies
= this->cookie_monster()->GetAllCookies();
511 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run
,
512 base::Unretained(&callback_
), cookies
));
516 // Task class for GetAllCookiesForURLWithOptions call.
517 class CookieMonster::GetAllCookiesForURLWithOptionsTask
518 : public CookieMonsterTask
{
520 GetAllCookiesForURLWithOptionsTask(CookieMonster
* cookie_monster
,
522 const CookieOptions
& options
,
523 const GetCookieListCallback
& callback
)
524 : CookieMonsterTask(cookie_monster
),
527 callback_(callback
) {}
529 // CookieMonsterTask:
533 ~GetAllCookiesForURLWithOptionsTask() override
{}
537 CookieOptions options_
;
538 GetCookieListCallback callback_
;
540 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesForURLWithOptionsTask
);
543 void CookieMonster::GetAllCookiesForURLWithOptionsTask::Run() {
544 if (!callback_
.is_null()) {
546 this->cookie_monster()->GetAllCookiesForURLWithOptions(url_
, options_
);
547 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run
,
548 base::Unretained(&callback_
), cookies
));
552 template <typename Result
>
553 struct CallbackType
{
554 typedef base::Callback
<void(Result
)> Type
;
558 struct CallbackType
<void> {
559 typedef base::Closure Type
;
562 // Base task class for Delete*Task.
563 template <typename Result
>
564 class CookieMonster::DeleteTask
: public CookieMonsterTask
{
566 DeleteTask(CookieMonster
* cookie_monster
,
567 const typename CallbackType
<Result
>::Type
& callback
)
568 : CookieMonsterTask(cookie_monster
), callback_(callback
) {}
570 // CookieMonsterTask:
574 ~DeleteTask() override
;
577 // Runs the delete task and returns a result.
578 virtual Result
RunDeleteTask() = 0;
579 base::Closure
RunDeleteTaskAndBindCallback();
580 void FlushDone(const base::Closure
& callback
);
582 typename CallbackType
<Result
>::Type callback_
;
584 DISALLOW_COPY_AND_ASSIGN(DeleteTask
);
587 template <typename Result
>
588 CookieMonster::DeleteTask
<Result
>::~DeleteTask() {
591 template <typename Result
>
593 CookieMonster::DeleteTask
<Result
>::RunDeleteTaskAndBindCallback() {
594 Result result
= RunDeleteTask();
595 if (callback_
.is_null())
596 return base::Closure();
597 return base::Bind(callback_
, result
);
601 base::Closure
CookieMonster::DeleteTask
<void>::RunDeleteTaskAndBindCallback() {
606 template <typename Result
>
607 void CookieMonster::DeleteTask
<Result
>::Run() {
608 this->cookie_monster()->FlushStore(base::Bind(
609 &DeleteTask
<Result
>::FlushDone
, this, RunDeleteTaskAndBindCallback()));
612 template <typename Result
>
613 void CookieMonster::DeleteTask
<Result
>::FlushDone(
614 const base::Closure
& callback
) {
615 if (!callback
.is_null()) {
616 this->InvokeCallback(callback
);
620 // Task class for DeleteAll call.
621 class CookieMonster::DeleteAllTask
: public DeleteTask
<int> {
623 DeleteAllTask(CookieMonster
* cookie_monster
, const DeleteCallback
& callback
)
624 : DeleteTask
<int>(cookie_monster
, callback
) {}
627 int RunDeleteTask() override
;
630 ~DeleteAllTask() override
{}
633 DISALLOW_COPY_AND_ASSIGN(DeleteAllTask
);
636 int CookieMonster::DeleteAllTask::RunDeleteTask() {
637 return this->cookie_monster()->DeleteAll(true);
640 // Task class for DeleteAllCreatedBetween call.
641 class CookieMonster::DeleteAllCreatedBetweenTask
: public DeleteTask
<int> {
643 DeleteAllCreatedBetweenTask(CookieMonster
* cookie_monster
,
644 const Time
& delete_begin
,
645 const Time
& delete_end
,
646 const DeleteCallback
& callback
)
647 : DeleteTask
<int>(cookie_monster
, callback
),
648 delete_begin_(delete_begin
),
649 delete_end_(delete_end
) {}
652 int RunDeleteTask() override
;
655 ~DeleteAllCreatedBetweenTask() override
{}
661 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask
);
664 int CookieMonster::DeleteAllCreatedBetweenTask::RunDeleteTask() {
665 return this->cookie_monster()->DeleteAllCreatedBetween(delete_begin_
,
669 // Task class for DeleteAllForHost call.
670 class CookieMonster::DeleteAllForHostTask
: public DeleteTask
<int> {
672 DeleteAllForHostTask(CookieMonster
* cookie_monster
,
674 const DeleteCallback
& callback
)
675 : DeleteTask
<int>(cookie_monster
, callback
), url_(url
) {}
678 int RunDeleteTask() override
;
681 ~DeleteAllForHostTask() override
{}
686 DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask
);
689 int CookieMonster::DeleteAllForHostTask::RunDeleteTask() {
690 return this->cookie_monster()->DeleteAllForHost(url_
);
693 // Task class for DeleteAllCreatedBetweenForHost call.
694 class CookieMonster::DeleteAllCreatedBetweenForHostTask
695 : public DeleteTask
<int> {
697 DeleteAllCreatedBetweenForHostTask(CookieMonster
* cookie_monster
,
701 const DeleteCallback
& callback
)
702 : DeleteTask
<int>(cookie_monster
, callback
),
703 delete_begin_(delete_begin
),
704 delete_end_(delete_end
),
708 int RunDeleteTask() override
;
711 ~DeleteAllCreatedBetweenForHostTask() override
{}
718 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenForHostTask
);
721 int CookieMonster::DeleteAllCreatedBetweenForHostTask::RunDeleteTask() {
722 return this->cookie_monster()->DeleteAllCreatedBetweenForHost(
723 delete_begin_
, delete_end_
, url_
);
726 // Task class for DeleteCanonicalCookie call.
727 class CookieMonster::DeleteCanonicalCookieTask
: public DeleteTask
<bool> {
729 DeleteCanonicalCookieTask(CookieMonster
* cookie_monster
,
730 const CanonicalCookie
& cookie
,
731 const DeleteCookieCallback
& callback
)
732 : DeleteTask
<bool>(cookie_monster
, callback
), cookie_(cookie
) {}
735 bool RunDeleteTask() override
;
738 ~DeleteCanonicalCookieTask() override
{}
741 CanonicalCookie cookie_
;
743 DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask
);
746 bool CookieMonster::DeleteCanonicalCookieTask::RunDeleteTask() {
747 return this->cookie_monster()->DeleteCanonicalCookie(cookie_
);
750 // Task class for SetCookieWithOptions call.
751 class CookieMonster::SetCookieWithOptionsTask
: public CookieMonsterTask
{
753 SetCookieWithOptionsTask(CookieMonster
* cookie_monster
,
755 const std::string
& cookie_line
,
756 const CookieOptions
& options
,
757 const SetCookiesCallback
& callback
)
758 : CookieMonsterTask(cookie_monster
),
760 cookie_line_(cookie_line
),
762 callback_(callback
) {}
764 // CookieMonsterTask:
768 ~SetCookieWithOptionsTask() override
{}
772 std::string cookie_line_
;
773 CookieOptions options_
;
774 SetCookiesCallback callback_
;
776 DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask
);
779 void CookieMonster::SetCookieWithOptionsTask::Run() {
780 bool result
= this->cookie_monster()->SetCookieWithOptions(url_
, cookie_line_
,
782 if (!callback_
.is_null()) {
783 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run
,
784 base::Unretained(&callback_
), result
));
788 // Task class for SetAllCookies call.
789 class CookieMonster::SetAllCookiesTask
: public CookieMonsterTask
{
791 SetAllCookiesTask(CookieMonster
* cookie_monster
,
792 const CookieList
& list
,
793 const SetCookiesCallback
& callback
)
794 : CookieMonsterTask(cookie_monster
), list_(list
), callback_(callback
) {}
796 // CookieMonsterTask:
800 ~SetAllCookiesTask() override
{}
804 SetCookiesCallback callback_
;
806 DISALLOW_COPY_AND_ASSIGN(SetAllCookiesTask
);
809 void CookieMonster::SetAllCookiesTask::Run() {
810 CookieList positive_diff
;
811 CookieList negative_diff
;
812 CookieList old_cookies
= this->cookie_monster()->GetAllCookies();
813 this->cookie_monster()->ComputeCookieDiff(&old_cookies
, &list_
,
814 &positive_diff
, &negative_diff
);
816 for (CookieList::const_iterator it
= negative_diff
.begin();
817 it
!= negative_diff
.end(); ++it
) {
818 this->cookie_monster()->DeleteCanonicalCookie(*it
);
822 if (positive_diff
.size() > 0)
823 result
= this->cookie_monster()->SetCanonicalCookies(list_
);
825 if (!callback_
.is_null()) {
826 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run
,
827 base::Unretained(&callback_
), result
));
831 // Task class for GetCookiesWithOptions call.
832 class CookieMonster::GetCookiesWithOptionsTask
: public CookieMonsterTask
{
834 GetCookiesWithOptionsTask(CookieMonster
* cookie_monster
,
836 const CookieOptions
& options
,
837 const GetCookiesCallback
& callback
)
838 : CookieMonsterTask(cookie_monster
),
841 callback_(callback
) {}
843 // CookieMonsterTask:
847 ~GetCookiesWithOptionsTask() override
{}
851 CookieOptions options_
;
852 GetCookiesCallback callback_
;
854 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask
);
857 void CookieMonster::GetCookiesWithOptionsTask::Run() {
858 // TODO(mkwst): Remove ScopedTracker below once crbug.com/456373 is fixed.
859 tracked_objects::ScopedTracker
tracking_profile(
860 FROM_HERE_WITH_EXPLICIT_FUNCTION(
861 "456373 CookieMonster::GetCookiesWithOptionsTask::Run"));
863 this->cookie_monster()->GetCookiesWithOptions(url_
, options_
);
864 if (!callback_
.is_null()) {
865 this->InvokeCallback(base::Bind(&GetCookiesCallback::Run
,
866 base::Unretained(&callback_
), cookie
));
870 // Task class for DeleteCookie call.
871 class CookieMonster::DeleteCookieTask
: public DeleteTask
<void> {
873 DeleteCookieTask(CookieMonster
* cookie_monster
,
875 const std::string
& cookie_name
,
876 const base::Closure
& callback
)
877 : DeleteTask
<void>(cookie_monster
, callback
),
879 cookie_name_(cookie_name
) {}
882 void RunDeleteTask() override
;
885 ~DeleteCookieTask() override
{}
889 std::string cookie_name_
;
891 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask
);
894 void CookieMonster::DeleteCookieTask::RunDeleteTask() {
895 this->cookie_monster()->DeleteCookie(url_
, cookie_name_
);
898 // Task class for DeleteSessionCookies call.
899 class CookieMonster::DeleteSessionCookiesTask
: public DeleteTask
<int> {
901 DeleteSessionCookiesTask(CookieMonster
* cookie_monster
,
902 const DeleteCallback
& callback
)
903 : DeleteTask
<int>(cookie_monster
, callback
) {}
906 int RunDeleteTask() override
;
909 ~DeleteSessionCookiesTask() override
{}
912 DISALLOW_COPY_AND_ASSIGN(DeleteSessionCookiesTask
);
915 int CookieMonster::DeleteSessionCookiesTask::RunDeleteTask() {
916 return this->cookie_monster()->DeleteSessionCookies();
919 // Asynchronous CookieMonster API
921 void CookieMonster::SetCookieWithDetailsAsync(
923 const std::string
& name
,
924 const std::string
& value
,
925 const std::string
& domain
,
926 const std::string
& path
,
927 const Time
& expiration_time
,
930 bool first_party_only
,
931 CookiePriority priority
,
932 const SetCookiesCallback
& callback
) {
933 scoped_refptr
<SetCookieWithDetailsTask
> task
= new SetCookieWithDetailsTask(
934 this, url
, name
, value
, domain
, path
, expiration_time
, secure
, http_only
,
935 first_party_only
, priority
, callback
);
936 DoCookieTaskForURL(task
, url
);
939 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback
& callback
) {
940 scoped_refptr
<GetAllCookiesTask
> task
= new GetAllCookiesTask(this, callback
);
945 void CookieMonster::GetAllCookiesForURLWithOptionsAsync(
947 const CookieOptions
& options
,
948 const GetCookieListCallback
& callback
) {
949 scoped_refptr
<GetAllCookiesForURLWithOptionsTask
> task
=
950 new GetAllCookiesForURLWithOptionsTask(this, url
, options
, callback
);
952 DoCookieTaskForURL(task
, url
);
955 void CookieMonster::GetAllCookiesForURLAsync(
957 const GetCookieListCallback
& callback
) {
958 CookieOptions options
;
959 options
.set_include_httponly();
960 options
.set_include_first_party_only();
961 scoped_refptr
<GetAllCookiesForURLWithOptionsTask
> task
=
962 new GetAllCookiesForURLWithOptionsTask(this, url
, options
, callback
);
964 DoCookieTaskForURL(task
, url
);
967 void CookieMonster::DeleteAllAsync(const DeleteCallback
& callback
) {
968 scoped_refptr
<DeleteAllTask
> task
= new DeleteAllTask(this, callback
);
973 void CookieMonster::DeleteAllCreatedBetweenAsync(
974 const Time
& delete_begin
,
975 const Time
& delete_end
,
976 const DeleteCallback
& callback
) {
977 scoped_refptr
<DeleteAllCreatedBetweenTask
> task
=
978 new DeleteAllCreatedBetweenTask(this, delete_begin
, delete_end
, callback
);
983 void CookieMonster::DeleteAllCreatedBetweenForHostAsync(
984 const Time delete_begin
,
985 const Time delete_end
,
987 const DeleteCallback
& callback
) {
988 scoped_refptr
<DeleteAllCreatedBetweenForHostTask
> task
=
989 new DeleteAllCreatedBetweenForHostTask(this, delete_begin
, delete_end
,
992 DoCookieTaskForURL(task
, url
);
995 void CookieMonster::DeleteAllForHostAsync(const GURL
& url
,
996 const DeleteCallback
& callback
) {
997 scoped_refptr
<DeleteAllForHostTask
> task
=
998 new DeleteAllForHostTask(this, url
, callback
);
1000 DoCookieTaskForURL(task
, url
);
1003 void CookieMonster::DeleteCanonicalCookieAsync(
1004 const CanonicalCookie
& cookie
,
1005 const DeleteCookieCallback
& callback
) {
1006 scoped_refptr
<DeleteCanonicalCookieTask
> task
=
1007 new DeleteCanonicalCookieTask(this, cookie
, callback
);
1012 void CookieMonster::SetAllCookiesAsync(const CookieList
& list
,
1013 const SetCookiesCallback
& callback
) {
1014 scoped_refptr
<SetAllCookiesTask
> task
=
1015 new SetAllCookiesTask(this, list
, callback
);
1019 void CookieMonster::SetCookieWithOptionsAsync(
1021 const std::string
& cookie_line
,
1022 const CookieOptions
& options
,
1023 const SetCookiesCallback
& callback
) {
1024 scoped_refptr
<SetCookieWithOptionsTask
> task
=
1025 new SetCookieWithOptionsTask(this, url
, cookie_line
, options
, callback
);
1027 DoCookieTaskForURL(task
, url
);
1030 void CookieMonster::GetCookiesWithOptionsAsync(
1032 const CookieOptions
& options
,
1033 const GetCookiesCallback
& callback
) {
1034 scoped_refptr
<GetCookiesWithOptionsTask
> task
=
1035 new GetCookiesWithOptionsTask(this, url
, options
, callback
);
1037 DoCookieTaskForURL(task
, url
);
1040 void CookieMonster::DeleteCookieAsync(const GURL
& url
,
1041 const std::string
& cookie_name
,
1042 const base::Closure
& callback
) {
1043 scoped_refptr
<DeleteCookieTask
> task
=
1044 new DeleteCookieTask(this, url
, cookie_name
, callback
);
1046 DoCookieTaskForURL(task
, url
);
1049 void CookieMonster::DeleteSessionCookiesAsync(
1050 const CookieStore::DeleteCallback
& callback
) {
1051 scoped_refptr
<DeleteSessionCookiesTask
> task
=
1052 new DeleteSessionCookiesTask(this, callback
);
1057 void CookieMonster::DoCookieTask(
1058 const scoped_refptr
<CookieMonsterTask
>& task_item
) {
1060 base::AutoLock
autolock(lock_
);
1061 MarkCookieStoreAsInitialized();
1062 FetchAllCookiesIfNecessary();
1063 if (!finished_fetching_all_cookies_
&& store_
.get()) {
1064 tasks_pending_
.push(task_item
);
1072 void CookieMonster::DoCookieTaskForURL(
1073 const scoped_refptr
<CookieMonsterTask
>& task_item
,
1076 base::AutoLock
autolock(lock_
);
1077 MarkCookieStoreAsInitialized();
1078 if (ShouldFetchAllCookiesWhenFetchingAnyCookie())
1079 FetchAllCookiesIfNecessary();
1080 // If cookies for the requested domain key (eTLD+1) have been loaded from DB
1081 // then run the task, otherwise load from DB.
1082 if (!finished_fetching_all_cookies_
&& store_
.get()) {
1083 // Checks if the domain key has been loaded.
1085 cookie_util::GetEffectiveDomain(url
.scheme(), url
.host()));
1086 if (keys_loaded_
.find(key
) == keys_loaded_
.end()) {
1087 std::map
<std::string
,
1088 std::deque
<scoped_refptr
<CookieMonsterTask
>>>::iterator it
=
1089 tasks_pending_for_key_
.find(key
);
1090 if (it
== tasks_pending_for_key_
.end()) {
1091 store_
->LoadCookiesForKey(
1092 key
, base::Bind(&CookieMonster::OnKeyLoaded
, this, key
));
1093 it
= tasks_pending_for_key_
1094 .insert(std::make_pair(
1095 key
, std::deque
<scoped_refptr
<CookieMonsterTask
>>()))
1098 it
->second
.push_back(task_item
);
1106 bool CookieMonster::SetCookieWithDetails(const GURL
& url
,
1107 const std::string
& name
,
1108 const std::string
& value
,
1109 const std::string
& domain
,
1110 const std::string
& path
,
1111 const base::Time
& expiration_time
,
1114 bool first_party_only
,
1115 CookiePriority priority
) {
1116 base::AutoLock
autolock(lock_
);
1118 if (!HasCookieableScheme(url
))
1121 Time creation_time
= CurrentTime();
1122 last_time_seen_
= creation_time
;
1124 scoped_ptr
<CanonicalCookie
> cc
;
1125 cc
.reset(CanonicalCookie::Create(url
, name
, value
, domain
, path
,
1126 creation_time
, expiration_time
, secure
,
1127 http_only
, first_party_only
, priority
));
1132 CookieOptions options
;
1133 options
.set_include_httponly();
1134 options
.set_include_first_party_only();
1135 return SetCanonicalCookie(&cc
, creation_time
, options
);
1138 bool CookieMonster::ImportCookies(const CookieList
& list
) {
1139 base::AutoLock
autolock(lock_
);
1140 MarkCookieStoreAsInitialized();
1141 if (ShouldFetchAllCookiesWhenFetchingAnyCookie())
1142 FetchAllCookiesIfNecessary();
1143 for (CookieList::const_iterator iter
= list
.begin(); iter
!= list
.end();
1145 scoped_ptr
<CanonicalCookie
> cookie(new CanonicalCookie(*iter
));
1146 CookieOptions options
;
1147 options
.set_include_httponly();
1148 options
.set_include_first_party_only();
1149 if (!SetCanonicalCookie(&cookie
, cookie
->CreationDate(), options
))
1155 CookieList
CookieMonster::GetAllCookies() {
1156 base::AutoLock
autolock(lock_
);
1158 // This function is being called to scrape the cookie list for management UI
1159 // or similar. We shouldn't show expired cookies in this list since it will
1160 // just be confusing to users, and this function is called rarely enough (and
1161 // is already slow enough) that it's OK to take the time to garbage collect
1162 // the expired cookies now.
1164 // Note that this does not prune cookies to be below our limits (if we've
1165 // exceeded them) the way that calling GarbageCollect() would.
1166 GarbageCollectExpired(
1167 Time::Now(), CookieMapItPair(cookies_
.begin(), cookies_
.end()), NULL
);
1169 // Copy the CanonicalCookie pointers from the map so that we can use the same
1170 // sorter as elsewhere, then copy the result out.
1171 std::vector
<CanonicalCookie
*> cookie_ptrs
;
1172 cookie_ptrs
.reserve(cookies_
.size());
1173 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end(); ++it
)
1174 cookie_ptrs
.push_back(it
->second
);
1175 std::sort(cookie_ptrs
.begin(), cookie_ptrs
.end(), CookieSorter
);
1177 CookieList cookie_list
;
1178 cookie_list
.reserve(cookie_ptrs
.size());
1179 for (std::vector
<CanonicalCookie
*>::const_iterator it
= cookie_ptrs
.begin();
1180 it
!= cookie_ptrs
.end(); ++it
)
1181 cookie_list
.push_back(**it
);
1186 CookieList
CookieMonster::GetAllCookiesForURLWithOptions(
1188 const CookieOptions
& options
) {
1189 base::AutoLock
autolock(lock_
);
1191 std::vector
<CanonicalCookie
*> cookie_ptrs
;
1192 FindCookiesForHostAndDomain(url
, options
, false, &cookie_ptrs
);
1193 std::sort(cookie_ptrs
.begin(), cookie_ptrs
.end(), CookieSorter
);
1196 cookies
.reserve(cookie_ptrs
.size());
1197 for (std::vector
<CanonicalCookie
*>::const_iterator it
= cookie_ptrs
.begin();
1198 it
!= cookie_ptrs
.end(); it
++)
1199 cookies
.push_back(**it
);
1204 CookieList
CookieMonster::GetAllCookiesForURL(const GURL
& url
) {
1205 CookieOptions options
;
1206 options
.set_include_httponly();
1207 options
.set_first_party_url(url
);
1209 return GetAllCookiesForURLWithOptions(url
, options
);
1212 int CookieMonster::DeleteAll(bool sync_to_store
) {
1213 base::AutoLock
autolock(lock_
);
1215 int num_deleted
= 0;
1216 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end();) {
1217 CookieMap::iterator curit
= it
;
1219 InternalDeleteCookie(curit
, sync_to_store
,
1221 ? DELETE_COOKIE_EXPLICIT
1222 : DELETE_COOKIE_DONT_RECORD
/* Destruction. */);
1229 int CookieMonster::DeleteAllCreatedBetween(const Time
& delete_begin
,
1230 const Time
& delete_end
) {
1231 base::AutoLock
autolock(lock_
);
1233 int num_deleted
= 0;
1234 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end();) {
1235 CookieMap::iterator curit
= it
;
1236 CanonicalCookie
* cc
= curit
->second
;
1239 if (cc
->CreationDate() >= delete_begin
&&
1240 (delete_end
.is_null() || cc
->CreationDate() < delete_end
)) {
1241 InternalDeleteCookie(curit
, true, /*sync_to_store*/
1242 DELETE_COOKIE_EXPLICIT
);
1250 int CookieMonster::DeleteAllCreatedBetweenForHost(const Time delete_begin
,
1251 const Time delete_end
,
1253 base::AutoLock
autolock(lock_
);
1255 if (!HasCookieableScheme(url
))
1258 const std::string
host(url
.host());
1260 // We store host cookies in the store by their canonical host name;
1261 // domain cookies are stored with a leading ".". So this is a pretty
1262 // simple lookup and per-cookie delete.
1263 int num_deleted
= 0;
1264 for (CookieMapItPair its
= cookies_
.equal_range(GetKey(host
));
1265 its
.first
!= its
.second
;) {
1266 CookieMap::iterator curit
= its
.first
;
1269 const CanonicalCookie
* const cc
= curit
->second
;
1271 // Delete only on a match as a host cookie.
1272 if (cc
->IsHostCookie() && cc
->IsDomainMatch(host
) &&
1273 cc
->CreationDate() >= delete_begin
&&
1274 // The assumption that null |delete_end| is equivalent to
1275 // Time::Max() is confusing.
1276 (delete_end
.is_null() || cc
->CreationDate() < delete_end
)) {
1279 InternalDeleteCookie(curit
, true, DELETE_COOKIE_EXPLICIT
);
1285 int CookieMonster::DeleteAllForHost(const GURL
& url
) {
1286 return DeleteAllCreatedBetweenForHost(Time(), Time::Max(), url
);
1289 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie
& cookie
) {
1290 base::AutoLock
autolock(lock_
);
1292 for (CookieMapItPair its
= cookies_
.equal_range(GetKey(cookie
.Domain()));
1293 its
.first
!= its
.second
; ++its
.first
) {
1294 // The creation date acts as our unique index...
1295 if (its
.first
->second
->CreationDate() == cookie
.CreationDate()) {
1296 InternalDeleteCookie(its
.first
, true, DELETE_COOKIE_EXPLICIT
);
1303 void CookieMonster::SetCookieableSchemes(const char* const schemes
[],
1304 size_t num_schemes
) {
1305 base::AutoLock
autolock(lock_
);
1307 // Calls to this method will have no effect if made after a WebView or
1308 // CookieManager instance has been created.
1313 cookieable_schemes_
.clear();
1314 cookieable_schemes_
.insert(cookieable_schemes_
.end(), schemes
,
1315 schemes
+ num_schemes
);
1318 void CookieMonster::SetKeepExpiredCookies() {
1319 keep_expired_cookies_
= true;
1322 void CookieMonster::FlushStore(const base::Closure
& callback
) {
1323 base::AutoLock
autolock(lock_
);
1324 if (initialized_
&& store_
.get())
1325 store_
->Flush(callback
);
1326 else if (!callback
.is_null())
1327 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
, callback
);
1330 bool CookieMonster::SetCookieWithOptions(const GURL
& url
,
1331 const std::string
& cookie_line
,
1332 const CookieOptions
& options
) {
1333 base::AutoLock
autolock(lock_
);
1335 if (!HasCookieableScheme(url
)) {
1339 return SetCookieWithCreationTimeAndOptions(url
, cookie_line
, Time(), options
);
1342 std::string
CookieMonster::GetCookiesWithOptions(const GURL
& url
,
1343 const CookieOptions
& options
) {
1344 base::AutoLock
autolock(lock_
);
1346 if (!HasCookieableScheme(url
))
1347 return std::string();
1349 std::vector
<CanonicalCookie
*> cookies
;
1350 FindCookiesForHostAndDomain(url
, options
, true, &cookies
);
1351 std::sort(cookies
.begin(), cookies
.end(), CookieSorter
);
1353 std::string cookie_line
= BuildCookieLine(cookies
);
1355 VLOG(kVlogGetCookies
) << "GetCookies() result: " << cookie_line
;
1360 void CookieMonster::DeleteCookie(const GURL
& url
,
1361 const std::string
& cookie_name
) {
1362 base::AutoLock
autolock(lock_
);
1364 if (!HasCookieableScheme(url
))
1367 CookieOptions options
;
1368 options
.set_include_httponly();
1369 options
.set_include_first_party_only();
1370 // Get the cookies for this host and its domain(s).
1371 std::vector
<CanonicalCookie
*> cookies
;
1372 FindCookiesForHostAndDomain(url
, options
, true, &cookies
);
1373 std::set
<CanonicalCookie
*> matching_cookies
;
1375 for (std::vector
<CanonicalCookie
*>::const_iterator it
= cookies
.begin();
1376 it
!= cookies
.end(); ++it
) {
1377 if ((*it
)->Name() != cookie_name
)
1379 if (url
.path().find((*it
)->Path()))
1381 matching_cookies
.insert(*it
);
1384 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end();) {
1385 CookieMap::iterator curit
= it
;
1387 if (matching_cookies
.find(curit
->second
) != matching_cookies
.end()) {
1388 InternalDeleteCookie(curit
, true, DELETE_COOKIE_EXPLICIT
);
1393 int CookieMonster::DeleteSessionCookies() {
1394 base::AutoLock
autolock(lock_
);
1396 int num_deleted
= 0;
1397 for (CookieMap::iterator it
= cookies_
.begin(); it
!= cookies_
.end();) {
1398 CookieMap::iterator curit
= it
;
1399 CanonicalCookie
* cc
= curit
->second
;
1402 if (!cc
->IsPersistent()) {
1403 InternalDeleteCookie(curit
, true, /*sync_to_store*/
1404 DELETE_COOKIE_EXPIRED
);
1412 CookieMonster
* CookieMonster::GetCookieMonster() {
1416 // This function must be called before the CookieMonster is used.
1417 void CookieMonster::SetPersistSessionCookies(bool persist_session_cookies
) {
1418 DCHECK(!initialized_
);
1419 persist_session_cookies_
= persist_session_cookies
;
1422 void CookieMonster::SetForceKeepSessionState() {
1424 store_
->SetForceKeepSessionState();
1428 CookieMonster::~CookieMonster() {
1432 bool CookieMonster::SetCookieWithCreationTime(const GURL
& url
,
1433 const std::string
& cookie_line
,
1434 const base::Time
& creation_time
) {
1435 DCHECK(!store_
.get()) << "This method is only to be used by unit-tests.";
1436 base::AutoLock
autolock(lock_
);
1438 if (!HasCookieableScheme(url
)) {
1442 MarkCookieStoreAsInitialized();
1443 if (ShouldFetchAllCookiesWhenFetchingAnyCookie())
1444 FetchAllCookiesIfNecessary();
1446 return SetCookieWithCreationTimeAndOptions(url
, cookie_line
, creation_time
,
1450 void CookieMonster::MarkCookieStoreAsInitialized() {
1451 initialized_
= true;
1454 void CookieMonster::FetchAllCookiesIfNecessary() {
1455 if (store_
.get() && !started_fetching_all_cookies_
) {
1456 started_fetching_all_cookies_
= true;
1461 bool CookieMonster::ShouldFetchAllCookiesWhenFetchingAnyCookie() {
1462 if (fetch_strategy_
== kUnknownFetch
) {
1463 const std::string group_name
=
1464 base::FieldTrialList::FindFullName(kCookieMonsterFetchStrategyName
);
1465 if (group_name
== kFetchWhenNecessaryName
) {
1466 fetch_strategy_
= kFetchWhenNecessary
;
1467 } else if (group_name
== kAlwaysFetchName
) {
1468 fetch_strategy_
= kAlwaysFetch
;
1470 // The logic in the conditional is redundant, but it makes trials of
1471 // the Finch experiment more explicit.
1472 fetch_strategy_
= kAlwaysFetch
;
1476 return fetch_strategy_
== kAlwaysFetch
;
1479 void CookieMonster::FetchAllCookies() {
1480 DCHECK(store_
.get()) << "Store must exist to initialize";
1481 DCHECK(!finished_fetching_all_cookies_
)
1482 << "All cookies have already been fetched.";
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 // TODO(erikwright): Remove ScopedTracker below once crbug.com/457528 is
1537 tracked_objects::ScopedTracker
tracking_profile(
1538 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1539 "457528 CookieMonster::StoreLoadedCookies"));
1541 // Initialize the store and sync in any saved persistent cookies. We don't
1542 // care if it's expired, insert it so it can be garbage collected, removed,
1544 base::AutoLock
autolock(lock_
);
1546 CookieItVector cookies_with_control_chars
;
1548 for (std::vector
<CanonicalCookie
*>::const_iterator it
= cookies
.begin();
1549 it
!= cookies
.end(); ++it
) {
1550 int64 cookie_creation_time
= (*it
)->CreationDate().ToInternalValue();
1552 if (creation_times_
.insert(cookie_creation_time
).second
) {
1553 CookieMap::iterator inserted
=
1554 InternalInsertCookie(GetKey((*it
)->Domain()), *it
, false);
1555 const Time
cookie_access_time((*it
)->LastAccessDate());
1556 if (earliest_access_time_
.is_null() ||
1557 cookie_access_time
< earliest_access_time_
)
1558 earliest_access_time_
= cookie_access_time
;
1560 if (ContainsControlCharacter((*it
)->Name()) ||
1561 ContainsControlCharacter((*it
)->Value())) {
1562 cookies_with_control_chars
.push_back(inserted
);
1565 LOG(ERROR
) << base::StringPrintf(
1566 "Found cookies with duplicate creation "
1567 "times in backing store: "
1568 "{name='%s', domain='%s', path='%s'}",
1569 (*it
)->Name().c_str(), (*it
)->Domain().c_str(),
1570 (*it
)->Path().c_str());
1571 // We've been given ownership of the cookie and are throwing it
1572 // away; reclaim the space.
1577 // Any cookies that contain control characters that we have loaded from the
1578 // persistent store should be deleted. See http://crbug.com/238041.
1579 for (CookieItVector::iterator it
= cookies_with_control_chars
.begin();
1580 it
!= cookies_with_control_chars
.end();) {
1581 CookieItVector::iterator curit
= it
;
1584 InternalDeleteCookie(*curit
, true, DELETE_COOKIE_CONTROL_CHAR
);
1587 // After importing cookies from the PersistentCookieStore, verify that
1588 // none of our other constraints are violated.
1589 // In particular, the backing store might have given us duplicate cookies.
1591 // This method could be called multiple times due to priority loading, thus
1592 // cookies loaded in previous runs will be validated again, but this is OK
1593 // since they are expected to be much fewer than total DB.
1594 EnsureCookiesMapIsValid();
1597 void CookieMonster::InvokeQueue() {
1599 scoped_refptr
<CookieMonsterTask
> request_task
;
1601 base::AutoLock
autolock(lock_
);
1602 if (tasks_pending_
.empty()) {
1603 finished_fetching_all_cookies_
= true;
1604 creation_times_
.clear();
1605 keys_loaded_
.clear();
1608 request_task
= tasks_pending_
.front();
1609 tasks_pending_
.pop();
1611 request_task
->Run();
1615 void CookieMonster::EnsureCookiesMapIsValid() {
1616 lock_
.AssertAcquired();
1618 // Iterate through all the of the cookies, grouped by host.
1619 CookieMap::iterator prev_range_end
= cookies_
.begin();
1620 while (prev_range_end
!= cookies_
.end()) {
1621 CookieMap::iterator cur_range_begin
= prev_range_end
;
1622 const std::string key
= cur_range_begin
->first
; // Keep a copy.
1623 CookieMap::iterator cur_range_end
= cookies_
.upper_bound(key
);
1624 prev_range_end
= cur_range_end
;
1626 // Ensure no equivalent cookies for this host.
1627 TrimDuplicateCookiesForKey(key
, cur_range_begin
, cur_range_end
);
1631 void CookieMonster::TrimDuplicateCookiesForKey(const std::string
& key
,
1632 CookieMap::iterator begin
,
1633 CookieMap::iterator end
) {
1634 lock_
.AssertAcquired();
1636 // Set of cookies ordered by creation time.
1637 typedef std::set
<CookieMap::iterator
, OrderByCreationTimeDesc
> CookieSet
;
1639 // Helper map we populate to find the duplicates.
1640 typedef std::map
<CookieSignature
, CookieSet
> EquivalenceMap
;
1641 EquivalenceMap equivalent_cookies
;
1643 // The number of duplicate cookies that have been found.
1644 int num_duplicates
= 0;
1646 // Iterate through all of the cookies in our range, and insert them into
1647 // the equivalence map.
1648 for (CookieMap::iterator it
= begin
; it
!= end
; ++it
) {
1649 DCHECK_EQ(key
, it
->first
);
1650 CanonicalCookie
* cookie
= it
->second
;
1652 CookieSignature
signature(cookie
->Name(), cookie
->Domain(), cookie
->Path());
1653 CookieSet
& set
= equivalent_cookies
[signature
];
1655 // We found a duplicate!
1659 // We save the iterator into |cookies_| rather than the actual cookie
1660 // pointer, since we may need to delete it later.
1661 bool insert_success
= set
.insert(it
).second
;
1662 DCHECK(insert_success
)
1663 << "Duplicate creation times found in duplicate cookie name scan.";
1666 // If there were no duplicates, we are done!
1667 if (num_duplicates
== 0)
1670 // Make sure we find everything below that we did above.
1671 int num_duplicates_found
= 0;
1673 // Otherwise, delete all the duplicate cookies, both from our in-memory store
1674 // and from the backing store.
1675 for (EquivalenceMap::iterator it
= equivalent_cookies
.begin();
1676 it
!= equivalent_cookies
.end(); ++it
) {
1677 const CookieSignature
& signature
= it
->first
;
1678 CookieSet
& dupes
= it
->second
;
1680 if (dupes
.size() <= 1)
1681 continue; // This cookiename/path has no duplicates.
1682 num_duplicates_found
+= dupes
.size() - 1;
1684 // Since |dups| is sorted by creation time (descending), the first cookie
1685 // is the most recent one, so we will keep it. The rest are duplicates.
1686 dupes
.erase(dupes
.begin());
1688 LOG(ERROR
) << base::StringPrintf(
1689 "Found %d duplicate cookies for host='%s', "
1690 "with {name='%s', domain='%s', path='%s'}",
1691 static_cast<int>(dupes
.size()), key
.c_str(), signature
.name
.c_str(),
1692 signature
.domain
.c_str(), signature
.path
.c_str());
1694 // Remove all the cookies identified by |dupes|. It is valid to delete our
1695 // list of iterators one at a time, since |cookies_| is a multimap (they
1696 // don't invalidate existing iterators following deletion).
1697 for (CookieSet::iterator dupes_it
= dupes
.begin(); dupes_it
!= dupes
.end();
1699 InternalDeleteCookie(*dupes_it
, true,
1700 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE
);
1703 DCHECK_EQ(num_duplicates
, num_duplicates_found
);
1706 // Note: file must be the last scheme.
1707 const char* const CookieMonster::kDefaultCookieableSchemes
[] = {"http",
1712 const int CookieMonster::kDefaultCookieableSchemesCount
=
1713 arraysize(kDefaultCookieableSchemes
);
1715 void CookieMonster::SetDefaultCookieableSchemes() {
1716 // Always disable file scheme unless SetEnableFileScheme(true) is called.
1717 SetCookieableSchemes(kDefaultCookieableSchemes
,
1718 kDefaultCookieableSchemesCount
- 1);
1721 void CookieMonster::FindCookiesForHostAndDomain(
1723 const CookieOptions
& options
,
1724 bool update_access_time
,
1725 std::vector
<CanonicalCookie
*>* cookies
) {
1726 lock_
.AssertAcquired();
1728 const Time
current_time(CurrentTime());
1730 // Probe to save statistics relatively frequently. We do it here rather
1731 // than in the set path as many websites won't set cookies, and we
1732 // want to collect statistics whenever the browser's being used.
1733 RecordPeriodicStats(current_time
);
1735 // Can just dispatch to FindCookiesForKey
1736 const std::string
key(GetKey(url
.host()));
1737 FindCookiesForKey(key
, url
, options
, current_time
, update_access_time
,
1741 void CookieMonster::FindCookiesForKey(const std::string
& key
,
1743 const CookieOptions
& options
,
1744 const Time
& current
,
1745 bool update_access_time
,
1746 std::vector
<CanonicalCookie
*>* cookies
) {
1747 lock_
.AssertAcquired();
1749 for (CookieMapItPair its
= cookies_
.equal_range(key
);
1750 its
.first
!= its
.second
;) {
1751 CookieMap::iterator curit
= its
.first
;
1752 CanonicalCookie
* cc
= curit
->second
;
1755 // If the cookie is expired, delete it.
1756 if (cc
->IsExpired(current
) && !keep_expired_cookies_
) {
1757 InternalDeleteCookie(curit
, true, DELETE_COOKIE_EXPIRED
);
1761 // Filter out cookies that should not be included for a request to the
1762 // given |url|. HTTP only cookies are filtered depending on the passed
1763 // cookie |options|.
1764 if (!cc
->IncludeForRequestURL(url
, options
))
1767 // Add this cookie to the set of matching cookies. Update the access
1768 // time if we've been requested to do so.
1769 if (update_access_time
) {
1770 InternalUpdateCookieAccessTime(cc
, current
);
1772 cookies
->push_back(cc
);
1776 bool CookieMonster::DeleteAnyEquivalentCookie(const std::string
& key
,
1777 const CanonicalCookie
& ecc
,
1779 bool already_expired
) {
1780 lock_
.AssertAcquired();
1782 bool found_equivalent_cookie
= false;
1783 bool skipped_httponly
= false;
1784 for (CookieMapItPair its
= cookies_
.equal_range(key
);
1785 its
.first
!= its
.second
;) {
1786 CookieMap::iterator curit
= its
.first
;
1787 CanonicalCookie
* cc
= curit
->second
;
1790 if (ecc
.IsEquivalent(*cc
)) {
1791 // We should never have more than one equivalent cookie, since they should
1792 // overwrite each other.
1793 CHECK(!found_equivalent_cookie
)
1794 << "Duplicate equivalent cookies found, cookie store is corrupted.";
1795 if (skip_httponly
&& cc
->IsHttpOnly()) {
1796 skipped_httponly
= true;
1798 InternalDeleteCookie(curit
, true, already_expired
1799 ? DELETE_COOKIE_EXPIRED_OVERWRITE
1800 : DELETE_COOKIE_OVERWRITE
);
1802 found_equivalent_cookie
= true;
1805 return skipped_httponly
;
1808 CookieMonster::CookieMap::iterator
CookieMonster::InternalInsertCookie(
1809 const std::string
& key
,
1810 CanonicalCookie
* cc
,
1811 bool sync_to_store
) {
1812 // TODO(mkwst): Remove ScopedTracker below once crbug.com/456373 is fixed.
1813 tracked_objects::ScopedTracker
tracking_profile(
1814 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1815 "456373 CookieMonster::InternalInsertCookie"));
1816 lock_
.AssertAcquired();
1818 if ((cc
->IsPersistent() || persist_session_cookies_
) && store_
.get() &&
1820 store_
->AddCookie(*cc
);
1821 CookieMap::iterator inserted
=
1822 cookies_
.insert(CookieMap::value_type(key
, cc
));
1823 if (delegate_
.get()) {
1824 delegate_
->OnCookieChanged(*cc
, false,
1825 CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT
);
1828 // See InitializeHistograms() for details.
1829 int32_t cookie_type_sample
=
1830 cc
->IsFirstPartyOnly() ? 1 << COOKIE_TYPE_FIRSTPARTYONLY
: 0;
1831 cookie_type_sample
|= cc
->IsHttpOnly() ? 1 << COOKIE_TYPE_HTTPONLY
: 0;
1832 cookie_type_sample
|= cc
->IsSecure() ? 1 << COOKIE_TYPE_SECURE
: 0;
1833 histogram_cookie_type_
->Add(cookie_type_sample
);
1835 // Histogram the type of scheme used on URLs that set cookies. This
1836 // intentionally includes cookies that are set or overwritten by
1837 // http:// URLs, but not cookies that are cleared by http:// URLs, to
1838 // understand if the former behavior can be deprecated for Secure
1840 if (!cc
->Source().is_empty()) {
1841 CookieSource cookie_source_sample
;
1842 if (cc
->Source().SchemeIsCryptographic()) {
1843 cookie_source_sample
=
1844 cc
->IsSecure() ? COOKIE_SOURCE_SECURE_COOKIE_CRYPTOGRAPHIC_SCHEME
1845 : COOKIE_SOURCE_NONSECURE_COOKIE_CRYPTOGRAPHIC_SCHEME
;
1847 cookie_source_sample
=
1849 ? COOKIE_SOURCE_SECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME
1850 : COOKIE_SOURCE_NONSECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME
;
1852 histogram_cookie_source_scheme_
->Add(cookie_source_sample
);
1855 RunCallbacks(*cc
, false);
1860 bool CookieMonster::SetCookieWithCreationTimeAndOptions(
1862 const std::string
& cookie_line
,
1863 const Time
& creation_time_or_null
,
1864 const CookieOptions
& options
) {
1865 lock_
.AssertAcquired();
1867 VLOG(kVlogSetCookies
) << "SetCookie() line: " << cookie_line
;
1869 Time creation_time
= creation_time_or_null
;
1870 if (creation_time
.is_null()) {
1871 creation_time
= CurrentTime();
1872 last_time_seen_
= creation_time
;
1875 scoped_ptr
<CanonicalCookie
> cc(
1876 CanonicalCookie::Create(url
, cookie_line
, creation_time
, options
));
1879 VLOG(kVlogSetCookies
) << "WARNING: Failed to allocate CanonicalCookie";
1882 return SetCanonicalCookie(&cc
, creation_time
, options
);
1885 bool CookieMonster::SetCanonicalCookie(scoped_ptr
<CanonicalCookie
>* cc
,
1886 const Time
& creation_time
,
1887 const CookieOptions
& options
) {
1888 const std::string
key(GetKey((*cc
)->Domain()));
1889 bool already_expired
= (*cc
)->IsExpired(creation_time
);
1891 if (DeleteAnyEquivalentCookie(key
, **cc
, options
.exclude_httponly(),
1893 VLOG(kVlogSetCookies
) << "SetCookie() not clobbering httponly cookie";
1897 VLOG(kVlogSetCookies
) << "SetCookie() key: " << key
1898 << " cc: " << (*cc
)->DebugString();
1900 // Realize that we might be setting an expired cookie, and the only point
1901 // was to delete the cookie which we've already done.
1902 if (!already_expired
|| keep_expired_cookies_
) {
1903 // See InitializeHistograms() for details.
1904 if ((*cc
)->IsPersistent()) {
1905 histogram_expiration_duration_minutes_
->Add(
1906 ((*cc
)->ExpiryDate() - creation_time
).InMinutes());
1910 CanonicalCookie cookie
= *(cc
->get());
1911 InternalInsertCookie(key
, cc
->release(), true);
1914 VLOG(kVlogSetCookies
) << "SetCookie() not storing already expired cookie.";
1917 // We assume that hopefully setting a cookie will be less common than
1918 // querying a cookie. Since setting a cookie can put us over our limits,
1919 // make sure that we garbage collect... We can also make the assumption that
1920 // if a cookie was set, in the common case it will be used soon after,
1921 // and we will purge the expired cookies in GetCookies().
1922 GarbageCollect(creation_time
, key
);
1927 bool CookieMonster::SetCanonicalCookies(const CookieList
& list
) {
1928 base::AutoLock
autolock(lock_
);
1930 CookieOptions options
;
1931 options
.set_include_httponly();
1933 for (CookieList::const_iterator it
= list
.begin(); it
!= list
.end(); ++it
) {
1934 scoped_ptr
<CanonicalCookie
> canonical_cookie(new CanonicalCookie(*it
));
1935 if (!SetCanonicalCookie(&canonical_cookie
, it
->CreationDate(), options
))
1942 void CookieMonster::InternalUpdateCookieAccessTime(CanonicalCookie
* cc
,
1943 const Time
& current
) {
1944 lock_
.AssertAcquired();
1946 // Based off the Mozilla code. When a cookie has been accessed recently,
1947 // don't bother updating its access time again. This reduces the number of
1948 // updates we do during pageload, which in turn reduces the chance our storage
1949 // backend will hit its batch thresholds and be forced to update.
1950 if ((current
- cc
->LastAccessDate()) < last_access_threshold_
)
1953 cc
->SetLastAccessDate(current
);
1954 if ((cc
->IsPersistent() || persist_session_cookies_
) && store_
.get())
1955 store_
->UpdateCookieAccessTime(*cc
);
1958 // InternalDeleteCookies must not invalidate iterators other than the one being
1960 void CookieMonster::InternalDeleteCookie(CookieMap::iterator it
,
1962 DeletionCause deletion_cause
) {
1963 lock_
.AssertAcquired();
1965 // Ideally, this would be asserted up where we define ChangeCauseMapping,
1966 // but DeletionCause's visibility (or lack thereof) forces us to make
1968 static_assert(arraysize(ChangeCauseMapping
) == DELETE_COOKIE_LAST_ENTRY
+ 1,
1969 "ChangeCauseMapping size should match DeletionCause size");
1971 // See InitializeHistograms() for details.
1972 if (deletion_cause
!= DELETE_COOKIE_DONT_RECORD
)
1973 histogram_cookie_deletion_cause_
->Add(deletion_cause
);
1975 CanonicalCookie
* cc
= it
->second
;
1976 VLOG(kVlogSetCookies
) << "InternalDeleteCookie()"
1977 << ", cause:" << deletion_cause
1978 << ", cc: " << cc
->DebugString();
1980 if ((cc
->IsPersistent() || persist_session_cookies_
) && store_
.get() &&
1982 store_
->DeleteCookie(*cc
);
1983 if (delegate_
.get()) {
1984 ChangeCausePair mapping
= ChangeCauseMapping
[deletion_cause
];
1987 delegate_
->OnCookieChanged(*cc
, true, mapping
.cause
);
1989 RunCallbacks(*cc
, true);
1994 // Domain expiry behavior is unchanged by key/expiry scheme (the
1995 // meaning of the key is different, but that's not visible to this routine).
1996 int CookieMonster::GarbageCollect(const Time
& current
, const std::string
& key
) {
1997 lock_
.AssertAcquired();
1999 int num_deleted
= 0;
2000 Time
safe_date(Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays
));
2002 // Collect garbage for this key, minding cookie priorities.
2003 if (cookies_
.count(key
) > kDomainMaxCookies
) {
2004 VLOG(kVlogGarbageCollection
) << "GarbageCollect() key: " << key
;
2006 CookieItVector cookie_its
;
2008 GarbageCollectExpired(current
, cookies_
.equal_range(key
), &cookie_its
);
2009 if (cookie_its
.size() > kDomainMaxCookies
) {
2010 VLOG(kVlogGarbageCollection
) << "Deep Garbage Collect domain.";
2012 cookie_its
.size() - (kDomainMaxCookies
- kDomainPurgeCookies
);
2013 DCHECK(purge_goal
> kDomainPurgeCookies
);
2015 // Boundary iterators into |cookie_its| for different priorities.
2016 CookieItVector::iterator it_bdd
[4];
2017 // Intialize |it_bdd| while sorting |cookie_its| by priorities.
2018 // Schematic: [MLLHMHHLMM] => [LLL|MMMM|HHH], with 4 boundaries.
2019 it_bdd
[0] = cookie_its
.begin();
2020 it_bdd
[3] = cookie_its
.end();
2022 PartitionCookieByPriority(it_bdd
[0], it_bdd
[3], COOKIE_PRIORITY_LOW
);
2023 it_bdd
[2] = PartitionCookieByPriority(it_bdd
[1], it_bdd
[3],
2024 COOKIE_PRIORITY_MEDIUM
);
2025 size_t quota
[3] = {kDomainCookiesQuotaLow
,
2026 kDomainCookiesQuotaMedium
,
2027 kDomainCookiesQuotaHigh
};
2029 // Purge domain cookies in 3 rounds.
2030 // Round 1: consider low-priority cookies only: evict least-recently
2031 // accessed, while protecting quota[0] of these from deletion.
2032 // Round 2: consider {low, medium}-priority cookies, evict least-recently
2033 // accessed, while protecting quota[0] + quota[1].
2034 // Round 3: consider all cookies, evict least-recently accessed.
2035 size_t accumulated_quota
= 0;
2036 CookieItVector::iterator it_purge_begin
= it_bdd
[0];
2037 for (int i
= 0; i
< 3 && purge_goal
> 0; ++i
) {
2038 accumulated_quota
+= quota
[i
];
2040 size_t num_considered
= it_bdd
[i
+ 1] - it_purge_begin
;
2041 if (num_considered
<= accumulated_quota
)
2044 // Number of cookies that will be purged in this round.
2046 std::min(purge_goal
, num_considered
- accumulated_quota
);
2047 purge_goal
-= round_goal
;
2049 SortLeastRecentlyAccessed(it_purge_begin
, it_bdd
[i
+ 1], round_goal
);
2050 // Cookies accessed on or after |safe_date| would have been safe from
2051 // global purge, and we want to keep track of this.
2052 CookieItVector::iterator it_purge_end
= it_purge_begin
+ round_goal
;
2053 CookieItVector::iterator it_purge_middle
=
2054 LowerBoundAccessDate(it_purge_begin
, it_purge_end
, safe_date
);
2055 // Delete cookies accessed before |safe_date|.
2056 num_deleted
+= GarbageCollectDeleteRange(
2057 current
, DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
, it_purge_begin
,
2059 // Delete cookies accessed on or after |safe_date|.
2060 num_deleted
+= GarbageCollectDeleteRange(
2061 current
, DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
, it_purge_middle
,
2063 it_purge_begin
= it_purge_end
;
2065 DCHECK_EQ(0U, purge_goal
);
2069 // Collect garbage for everything. With firefox style we want to preserve
2070 // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict.
2071 if (cookies_
.size() > kMaxCookies
&& earliest_access_time_
< safe_date
) {
2072 VLOG(kVlogGarbageCollection
) << "GarbageCollect() everything";
2073 CookieItVector cookie_its
;
2074 num_deleted
+= GarbageCollectExpired(
2075 current
, CookieMapItPair(cookies_
.begin(), cookies_
.end()),
2077 if (cookie_its
.size() > kMaxCookies
) {
2078 VLOG(kVlogGarbageCollection
) << "Deep Garbage Collect everything.";
2079 size_t purge_goal
= cookie_its
.size() - (kMaxCookies
- kPurgeCookies
);
2080 DCHECK(purge_goal
> kPurgeCookies
);
2081 // Sorts up to *and including* |cookie_its[purge_goal]|, so
2082 // |earliest_access_time| will be properly assigned even if
2083 // |global_purge_it| == |cookie_its.begin() + purge_goal|.
2084 SortLeastRecentlyAccessed(cookie_its
.begin(), cookie_its
.end(),
2086 // Find boundary to cookies older than safe_date.
2087 CookieItVector::iterator global_purge_it
= LowerBoundAccessDate(
2088 cookie_its
.begin(), cookie_its
.begin() + purge_goal
, safe_date
);
2089 // Only delete the old cookies.
2091 GarbageCollectDeleteRange(current
, DELETE_COOKIE_EVICTED_GLOBAL
,
2092 cookie_its
.begin(), global_purge_it
);
2093 // Set access day to the oldest cookie that wasn't deleted.
2094 earliest_access_time_
= (*global_purge_it
)->second
->LastAccessDate();
2101 int CookieMonster::GarbageCollectExpired(const Time
& current
,
2102 const CookieMapItPair
& itpair
,
2103 CookieItVector
* cookie_its
) {
2104 if (keep_expired_cookies_
)
2107 lock_
.AssertAcquired();
2109 int num_deleted
= 0;
2110 for (CookieMap::iterator it
= itpair
.first
, end
= itpair
.second
; it
!= end
;) {
2111 CookieMap::iterator curit
= it
;
2114 if (curit
->second
->IsExpired(current
)) {
2115 InternalDeleteCookie(curit
, true, DELETE_COOKIE_EXPIRED
);
2117 } else if (cookie_its
) {
2118 cookie_its
->push_back(curit
);
2125 int CookieMonster::GarbageCollectDeleteRange(const Time
& current
,
2126 DeletionCause cause
,
2127 CookieItVector::iterator it_begin
,
2128 CookieItVector::iterator it_end
) {
2129 for (CookieItVector::iterator it
= it_begin
; it
!= it_end
; it
++) {
2130 histogram_evicted_last_access_minutes_
->Add(
2131 (current
- (*it
)->second
->LastAccessDate()).InMinutes());
2132 InternalDeleteCookie((*it
), true, cause
);
2134 return it_end
- it_begin
;
2137 // A wrapper around registry_controlled_domains::GetDomainAndRegistry
2138 // to make clear we're creating a key for our local map. Here and
2139 // in FindCookiesForHostAndDomain() are the only two places where
2140 // we need to conditionalize based on key type.
2142 // Note that this key algorithm explicitly ignores the scheme. This is
2143 // because when we're entering cookies into the map from the backing store,
2144 // we in general won't have the scheme at that point.
2145 // In practical terms, this means that file cookies will be stored
2146 // in the map either by an empty string or by UNC name (and will be
2147 // limited by kMaxCookiesPerHost), and extension cookies will be stored
2148 // based on the single extension id, as the extension id won't have the
2149 // form of a DNS host and hence GetKey() will return it unchanged.
2151 // Arguably the right thing to do here is to make the key
2152 // algorithm dependent on the scheme, and make sure that the scheme is
2153 // available everywhere the key must be obtained (specfically at backing
2154 // store load time). This would require either changing the backing store
2155 // database schema to include the scheme (far more trouble than it's worth), or
2156 // separating out file cookies into their own CookieMonster instance and
2157 // thus restricting each scheme to a single cookie monster (which might
2158 // be worth it, but is still too much trouble to solve what is currently a
2160 std::string
CookieMonster::GetKey(const std::string
& domain
) const {
2161 std::string
effective_domain(
2162 registry_controlled_domains::GetDomainAndRegistry(
2163 domain
, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
));
2164 if (effective_domain
.empty())
2165 effective_domain
= domain
;
2167 if (!effective_domain
.empty() && effective_domain
[0] == '.')
2168 return effective_domain
.substr(1);
2169 return effective_domain
;
2172 bool CookieMonster::IsCookieableScheme(const std::string
& scheme
) {
2173 base::AutoLock
autolock(lock_
);
2175 return std::find(cookieable_schemes_
.begin(), cookieable_schemes_
.end(),
2176 scheme
) != cookieable_schemes_
.end();
2179 bool CookieMonster::HasCookieableScheme(const GURL
& url
) {
2180 lock_
.AssertAcquired();
2182 // Make sure the request is on a cookie-able url scheme.
2183 for (size_t i
= 0; i
< cookieable_schemes_
.size(); ++i
) {
2184 // We matched a scheme.
2185 if (url
.SchemeIs(cookieable_schemes_
[i
].c_str())) {
2186 // We've matched a supported scheme.
2191 // The scheme didn't match any in our whitelist.
2192 VLOG(kVlogPerCookieMonster
)
2193 << "WARNING: Unsupported cookie scheme: " << url
.scheme();
2197 // Test to see if stats should be recorded, and record them if so.
2198 // The goal here is to get sampling for the average browser-hour of
2199 // activity. We won't take samples when the web isn't being surfed,
2200 // and when the web is being surfed, we'll take samples about every
2201 // kRecordStatisticsIntervalSeconds.
2202 // last_statistic_record_time_ is initialized to Now() rather than null
2203 // in the constructor so that we won't take statistics right after
2204 // startup, to avoid bias from browsers that are started but not used.
2205 void CookieMonster::RecordPeriodicStats(const base::Time
& current_time
) {
2206 const base::TimeDelta
kRecordStatisticsIntervalTime(
2207 base::TimeDelta::FromSeconds(kRecordStatisticsIntervalSeconds
));
2209 // If we've taken statistics recently, return.
2210 if (current_time
- last_statistic_record_time_
<=
2211 kRecordStatisticsIntervalTime
) {
2215 // See InitializeHistograms() for details.
2216 histogram_count_
->Add(cookies_
.size());
2218 // More detailed statistics on cookie counts at different granularities.
2219 last_statistic_record_time_
= current_time
;
2222 // Initialize all histogram counter variables used in this class.
2224 // Normal histogram usage involves using the macros defined in
2225 // histogram.h, which automatically takes care of declaring these
2226 // variables (as statics), initializing them, and accumulating into
2227 // them, all from a single entry point. Unfortunately, that solution
2228 // doesn't work for the CookieMonster, as it's vulnerable to races between
2229 // separate threads executing the same functions and hence initializing the
2230 // same static variables. There isn't a race danger in the histogram
2231 // accumulation calls; they are written to be resilient to simultaneous
2232 // calls from multiple threads.
2234 // The solution taken here is to have per-CookieMonster instance
2235 // variables that are constructed during CookieMonster construction.
2236 // Note that these variables refer to the same underlying histogram,
2237 // so we still race (but safely) with other CookieMonster instances
2238 // for accumulation.
2240 // To do this we've expanded out the individual histogram macros calls,
2241 // with declarations of the variables in the class decl, initialization here
2242 // (done from the class constructor) and direct calls to the accumulation
2243 // methods where needed. The specific histogram macro calls on which the
2244 // initialization is based are included in comments below.
2245 void CookieMonster::InitializeHistograms() {
2246 // From UMA_HISTOGRAM_CUSTOM_COUNTS
2247 histogram_expiration_duration_minutes_
= base::Histogram::FactoryGet(
2248 "Cookie.ExpirationDurationMinutes", 1, kMinutesInTenYears
, 50,
2249 base::Histogram::kUmaTargetedHistogramFlag
);
2250 histogram_evicted_last_access_minutes_
= base::Histogram::FactoryGet(
2251 "Cookie.EvictedLastAccessMinutes", 1, kMinutesInTenYears
, 50,
2252 base::Histogram::kUmaTargetedHistogramFlag
);
2253 histogram_count_
= base::Histogram::FactoryGet(
2254 "Cookie.Count", 1, 4000, 50, base::Histogram::kUmaTargetedHistogramFlag
);
2256 // From UMA_HISTOGRAM_ENUMERATION
2257 histogram_cookie_deletion_cause_
= base::LinearHistogram::FactoryGet(
2258 "Cookie.DeletionCause", 1, DELETE_COOKIE_LAST_ENTRY
- 1,
2259 DELETE_COOKIE_LAST_ENTRY
, base::Histogram::kUmaTargetedHistogramFlag
);
2260 histogram_cookie_type_
= base::LinearHistogram::FactoryGet(
2261 "Cookie.Type", 1, (1 << COOKIE_TYPE_LAST_ENTRY
) - 1,
2262 1 << COOKIE_TYPE_LAST_ENTRY
, base::Histogram::kUmaTargetedHistogramFlag
);
2263 histogram_cookie_source_scheme_
= base::LinearHistogram::FactoryGet(
2264 "Cookie.CookieSourceScheme", 1, COOKIE_SOURCE_LAST_ENTRY
- 1,
2265 COOKIE_SOURCE_LAST_ENTRY
, base::Histogram::kUmaTargetedHistogramFlag
);
2267 // From UMA_HISTOGRAM_{CUSTOM_,}TIMES
2268 histogram_time_blocked_on_load_
= base::Histogram::FactoryTimeGet(
2269 "Cookie.TimeBlockedOnLoad", base::TimeDelta::FromMilliseconds(1),
2270 base::TimeDelta::FromMinutes(1), 50,
2271 base::Histogram::kUmaTargetedHistogramFlag
);
2274 // The system resolution is not high enough, so we can have multiple
2275 // set cookies that result in the same system time. When this happens, we
2276 // increment by one Time unit. Let's hope computers don't get too fast.
2277 Time
CookieMonster::CurrentTime() {
2278 return std::max(Time::Now(), Time::FromInternalValue(
2279 last_time_seen_
.ToInternalValue() + 1));
2282 void CookieMonster::ComputeCookieDiff(CookieList
* old_cookies
,
2283 CookieList
* new_cookies
,
2284 CookieList
* cookies_to_add
,
2285 CookieList
* cookies_to_delete
) {
2286 DCHECK(old_cookies
);
2287 DCHECK(new_cookies
);
2288 DCHECK(cookies_to_add
);
2289 DCHECK(cookies_to_delete
);
2290 DCHECK(cookies_to_add
->empty());
2291 DCHECK(cookies_to_delete
->empty());
2294 // A set ordered by FullDiffCookieSorter is also ordered by
2295 // PartialDiffCookieSorter.
2296 std::sort(old_cookies
->begin(), old_cookies
->end(), FullDiffCookieSorter
);
2297 std::sort(new_cookies
->begin(), new_cookies
->end(), FullDiffCookieSorter
);
2299 // Select any old cookie for deletion if no new cookie has the same name,
2300 // domain, and path.
2301 std::set_difference(
2302 old_cookies
->begin(), old_cookies
->end(), new_cookies
->begin(),
2304 std::inserter(*cookies_to_delete
, cookies_to_delete
->begin()),
2305 PartialDiffCookieSorter
);
2307 // Select any new cookie for addition (or update) if no old cookie is exactly
2309 std::set_difference(new_cookies
->begin(), new_cookies
->end(),
2310 old_cookies
->begin(), old_cookies
->end(),
2311 std::inserter(*cookies_to_add
, cookies_to_add
->begin()),
2312 FullDiffCookieSorter
);
2315 scoped_ptr
<CookieStore::CookieChangedSubscription
>
2316 CookieMonster::AddCallbackForCookie(const GURL
& gurl
,
2317 const std::string
& name
,
2318 const CookieChangedCallback
& callback
) {
2319 base::AutoLock
autolock(lock_
);
2320 std::pair
<GURL
, std::string
> key(gurl
, name
);
2321 if (hook_map_
.count(key
) == 0)
2322 hook_map_
[key
] = make_linked_ptr(new CookieChangedCallbackList());
2323 return hook_map_
[key
]->Add(
2324 base::Bind(&RunAsync
, base::ThreadTaskRunnerHandle::Get(), callback
));
2327 #if defined(OS_ANDROID)
2328 void CookieMonster::SetEnableFileScheme(bool accept
) {
2329 // This assumes "file" is always at the end of the array. See the comment
2330 // above kDefaultCookieableSchemes.
2332 // TODO(mkwst): We're keeping this method around to support the
2333 // 'CookieManager::setAcceptFileSchemeCookies' method on Android's WebView;
2334 // if/when we can deprecate and remove that method, we can remove this one
2335 // as well. Until then, we'll just ensure that the method has no effect on
2336 // non-android systems.
2337 int num_schemes
= accept
? kDefaultCookieableSchemesCount
2338 : kDefaultCookieableSchemesCount
- 1;
2340 SetCookieableSchemes(kDefaultCookieableSchemes
, num_schemes
);
2344 void CookieMonster::RunCallbacks(const CanonicalCookie
& cookie
, bool removed
) {
2345 lock_
.AssertAcquired();
2347 opts
.set_include_httponly();
2348 opts
.set_include_first_party_only();
2349 // Note that the callbacks in hook_map_ are wrapped with MakeAsync(), so they
2350 // are guaranteed to not take long - they just post a RunAsync task back to
2351 // the appropriate thread's message loop and return. It is important that this
2352 // method not run user-supplied callbacks directly, since the CookieMonster
2353 // lock is held and it is easy to accidentally introduce deadlocks.
2354 for (CookieChangedHookMap::iterator it
= hook_map_
.begin();
2355 it
!= hook_map_
.end(); ++it
) {
2356 std::pair
<GURL
, std::string
> key
= it
->first
;
2357 if (cookie
.IncludeForRequestURL(key
.first
, opts
) &&
2358 cookie
.Name() == key
.second
) {
2359 it
->second
->Notify(cookie
, removed
);