Misc. cleanup for HistoryURLProvider. No functional changes.
[chromium-blink-merge.git] / net / cookies / cookie_monster.cc
blob7aed8f8d0f7608ae3352aa304716120a72f66a60
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
18 * License.
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.
27 * Contributor(s):
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"
47 #include <algorithm>
48 #include <functional>
49 #include <set>
51 #include "base/basictypes.h"
52 #include "base/bind.h"
53 #include "base/callback.h"
54 #include "base/logging.h"
55 #include "base/memory/scoped_ptr.h"
56 #include "base/memory/scoped_vector.h"
57 #include "base/message_loop/message_loop.h"
58 #include "base/message_loop/message_loop_proxy.h"
59 #include "base/metrics/histogram.h"
60 #include "base/strings/string_util.h"
61 #include "base/strings/stringprintf.h"
62 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
63 #include "net/cookies/canonical_cookie.h"
64 #include "net/cookies/cookie_util.h"
65 #include "net/cookies/parsed_cookie.h"
66 #include "url/gurl.h"
68 using base::Time;
69 using base::TimeDelta;
70 using base::TimeTicks;
72 // In steady state, most cookie requests can be satisfied by the in memory
73 // cookie monster store. However, if a request comes in during the initial
74 // cookie load, it must be delayed until that load completes. That is done by
75 // queueing it on CookieMonster::tasks_pending_ and running it when notification
76 // of cookie load completion is received via CookieMonster::OnLoaded. This
77 // callback is passed to the persistent store from CookieMonster::InitStore(),
78 // which is called on the first operation invoked on the CookieMonster.
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
92 // same eTLD+1.
94 static const int kMinutesInTenYears = 10 * 365 * 24 * 60;
96 namespace net {
98 // See comments at declaration of these variables in cookie_monster.h
99 // for details.
100 const size_t CookieMonster::kDomainMaxCookies = 180;
101 const size_t CookieMonster::kDomainPurgeCookies = 30;
102 const size_t CookieMonster::kMaxCookies = 3300;
103 const size_t CookieMonster::kPurgeCookies = 300;
105 const size_t CookieMonster::kDomainCookiesQuotaLow = 30;
106 const size_t CookieMonster::kDomainCookiesQuotaMedium = 50;
107 const size_t CookieMonster::kDomainCookiesQuotaHigh =
108 kDomainMaxCookies - kDomainPurgeCookies
109 - kDomainCookiesQuotaLow - kDomainCookiesQuotaMedium;
111 const int CookieMonster::kSafeFromGlobalPurgeDays = 30;
113 namespace {
115 bool ContainsControlCharacter(const std::string& s) {
116 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {
117 if ((*i >= 0) && (*i <= 31))
118 return true;
121 return false;
124 typedef std::vector<CanonicalCookie*> CanonicalCookieVector;
126 // Default minimum delay after updating a cookie's LastAccessDate before we
127 // will update it again.
128 const int kDefaultAccessUpdateThresholdSeconds = 60;
130 // Comparator to sort cookies from highest creation date to lowest
131 // creation date.
132 struct OrderByCreationTimeDesc {
133 bool operator()(const CookieMonster::CookieMap::iterator& a,
134 const CookieMonster::CookieMap::iterator& b) const {
135 return a->second->CreationDate() > b->second->CreationDate();
139 // Constants for use in VLOG
140 const int kVlogPerCookieMonster = 1;
141 const int kVlogPeriodic = 3;
142 const int kVlogGarbageCollection = 5;
143 const int kVlogSetCookies = 7;
144 const int kVlogGetCookies = 9;
146 // Mozilla sorts on the path length (longest first), and then it
147 // sorts by creation time (oldest first).
148 // The RFC says the sort order for the domain attribute is undefined.
149 bool CookieSorter(CanonicalCookie* cc1, CanonicalCookie* cc2) {
150 if (cc1->Path().length() == cc2->Path().length())
151 return cc1->CreationDate() < cc2->CreationDate();
152 return cc1->Path().length() > cc2->Path().length();
155 bool LRACookieSorter(const CookieMonster::CookieMap::iterator& it1,
156 const CookieMonster::CookieMap::iterator& it2) {
157 // Cookies accessed less recently should be deleted first.
158 if (it1->second->LastAccessDate() != it2->second->LastAccessDate())
159 return it1->second->LastAccessDate() < it2->second->LastAccessDate();
161 // In rare cases we might have two cookies with identical last access times.
162 // To preserve the stability of the sort, in these cases prefer to delete
163 // older cookies over newer ones. CreationDate() is guaranteed to be unique.
164 return it1->second->CreationDate() < it2->second->CreationDate();
167 // Our strategy to find duplicates is:
168 // (1) Build a map from (cookiename, cookiepath) to
169 // {list of cookies with this signature, sorted by creation time}.
170 // (2) For each list with more than 1 entry, keep the cookie having the
171 // most recent creation time, and delete the others.
173 // Two cookies are considered equivalent if they have the same domain,
174 // name, and path.
175 struct CookieSignature {
176 public:
177 CookieSignature(const std::string& name,
178 const std::string& domain,
179 const std::string& path)
180 : name(name), domain(domain), path(path) {
183 // To be a key for a map this class needs to be assignable, copyable,
184 // and have an operator<. The default assignment operator
185 // and copy constructor are exactly what we want.
187 bool operator<(const CookieSignature& cs) const {
188 // Name compare dominates, then domain, then path.
189 int diff = name.compare(cs.name);
190 if (diff != 0)
191 return diff < 0;
193 diff = domain.compare(cs.domain);
194 if (diff != 0)
195 return diff < 0;
197 return path.compare(cs.path) < 0;
200 std::string name;
201 std::string domain;
202 std::string path;
205 // For a CookieItVector iterator range [|it_begin|, |it_end|),
206 // sorts the first |num_sort| + 1 elements by LastAccessDate().
207 // The + 1 element exists so for any interval of length <= |num_sort| starting
208 // from |cookies_its_begin|, a LastAccessDate() bound can be found.
209 void SortLeastRecentlyAccessed(
210 CookieMonster::CookieItVector::iterator it_begin,
211 CookieMonster::CookieItVector::iterator it_end,
212 size_t num_sort) {
213 DCHECK_LT(static_cast<int>(num_sort), it_end - it_begin);
214 std::partial_sort(it_begin, it_begin + num_sort + 1, it_end, LRACookieSorter);
217 // Predicate to support PartitionCookieByPriority().
218 struct CookiePriorityEqualsTo
219 : std::unary_function<const CookieMonster::CookieMap::iterator, bool> {
220 CookiePriorityEqualsTo(CookiePriority priority)
221 : priority_(priority) {}
223 bool operator()(const CookieMonster::CookieMap::iterator it) const {
224 return it->second->Priority() == priority_;
227 const CookiePriority priority_;
230 // For a CookieItVector iterator range [|it_begin|, |it_end|),
231 // moves all cookies with a given |priority| to the beginning of the list.
232 // Returns: An iterator in [it_begin, it_end) to the first element with
233 // priority != |priority|, or |it_end| if all have priority == |priority|.
234 CookieMonster::CookieItVector::iterator PartitionCookieByPriority(
235 CookieMonster::CookieItVector::iterator it_begin,
236 CookieMonster::CookieItVector::iterator it_end,
237 CookiePriority priority) {
238 return std::partition(it_begin, it_end, CookiePriorityEqualsTo(priority));
241 bool LowerBoundAccessDateComparator(
242 const CookieMonster::CookieMap::iterator it, const Time& access_date) {
243 return it->second->LastAccessDate() < access_date;
246 // For a CookieItVector iterator range [|it_begin|, |it_end|)
247 // from a CookieItVector sorted by LastAccessDate(), returns the
248 // first iterator with access date >= |access_date|, or cookie_its_end if this
249 // holds for all.
250 CookieMonster::CookieItVector::iterator LowerBoundAccessDate(
251 const CookieMonster::CookieItVector::iterator its_begin,
252 const CookieMonster::CookieItVector::iterator its_end,
253 const Time& access_date) {
254 return std::lower_bound(its_begin, its_end, access_date,
255 LowerBoundAccessDateComparator);
258 // Mapping between DeletionCause and CookieMonsterDelegate::ChangeCause; the
259 // mapping also provides a boolean that specifies whether or not an
260 // OnCookieChanged notification ought to be generated.
261 typedef struct ChangeCausePair_struct {
262 CookieMonsterDelegate::ChangeCause cause;
263 bool notify;
264 } ChangeCausePair;
265 ChangeCausePair ChangeCauseMapping[] = {
266 // DELETE_COOKIE_EXPLICIT
267 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, true },
268 // DELETE_COOKIE_OVERWRITE
269 { CookieMonsterDelegate::CHANGE_COOKIE_OVERWRITE, true },
270 // DELETE_COOKIE_EXPIRED
271 { CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED, true },
272 // DELETE_COOKIE_EVICTED
273 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
274 // DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE
275 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false },
276 // DELETE_COOKIE_DONT_RECORD
277 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false },
278 // DELETE_COOKIE_EVICTED_DOMAIN
279 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
280 // DELETE_COOKIE_EVICTED_GLOBAL
281 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
282 // DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
283 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
284 // DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
285 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
286 // DELETE_COOKIE_EXPIRED_OVERWRITE
287 { CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED_OVERWRITE, true },
288 // DELETE_COOKIE_CONTROL_CHAR
289 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true},
290 // DELETE_COOKIE_LAST_ENTRY
291 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false }
294 std::string BuildCookieLine(const CanonicalCookieVector& cookies) {
295 std::string cookie_line;
296 for (CanonicalCookieVector::const_iterator it = cookies.begin();
297 it != cookies.end(); ++it) {
298 if (it != cookies.begin())
299 cookie_line += "; ";
300 // In Mozilla if you set a cookie like AAAA, it will have an empty token
301 // and a value of AAAA. When it sends the cookie back, it will send AAAA,
302 // so we need to avoid sending =AAAA for a blank token value.
303 if (!(*it)->Name().empty())
304 cookie_line += (*it)->Name() + "=";
305 cookie_line += (*it)->Value();
307 return cookie_line;
310 } // namespace
312 CookieMonster::CookieMonster(PersistentCookieStore* store,
313 CookieMonsterDelegate* delegate)
314 : initialized_(false),
315 loaded_(store == NULL),
316 store_(store),
317 last_access_threshold_(
318 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)),
319 delegate_(delegate),
320 last_statistic_record_time_(Time::Now()),
321 keep_expired_cookies_(false),
322 persist_session_cookies_(false) {
323 InitializeHistograms();
324 SetDefaultCookieableSchemes();
327 CookieMonster::CookieMonster(PersistentCookieStore* store,
328 CookieMonsterDelegate* delegate,
329 int last_access_threshold_milliseconds)
330 : initialized_(false),
331 loaded_(store == NULL),
332 store_(store),
333 last_access_threshold_(base::TimeDelta::FromMilliseconds(
334 last_access_threshold_milliseconds)),
335 delegate_(delegate),
336 last_statistic_record_time_(base::Time::Now()),
337 keep_expired_cookies_(false),
338 persist_session_cookies_(false) {
339 InitializeHistograms();
340 SetDefaultCookieableSchemes();
344 // Task classes for queueing the coming request.
346 class CookieMonster::CookieMonsterTask
347 : public base::RefCountedThreadSafe<CookieMonsterTask> {
348 public:
349 // Runs the task and invokes the client callback on the thread that
350 // originally constructed the task.
351 virtual void Run() = 0;
353 protected:
354 explicit CookieMonsterTask(CookieMonster* cookie_monster);
355 virtual ~CookieMonsterTask();
357 // Invokes the callback immediately, if the current thread is the one
358 // that originated the task, or queues the callback for execution on the
359 // appropriate thread. Maintains a reference to this CookieMonsterTask
360 // instance until the callback completes.
361 void InvokeCallback(base::Closure callback);
363 CookieMonster* cookie_monster() {
364 return cookie_monster_;
367 private:
368 friend class base::RefCountedThreadSafe<CookieMonsterTask>;
370 CookieMonster* cookie_monster_;
371 scoped_refptr<base::MessageLoopProxy> thread_;
373 DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask);
376 CookieMonster::CookieMonsterTask::CookieMonsterTask(
377 CookieMonster* cookie_monster)
378 : cookie_monster_(cookie_monster),
379 thread_(base::MessageLoopProxy::current()) {
382 CookieMonster::CookieMonsterTask::~CookieMonsterTask() {}
384 // Unfortunately, one cannot re-bind a Callback with parameters into a closure.
385 // Therefore, the closure passed to InvokeCallback is a clumsy binding of
386 // Callback::Run on a wrapped Callback instance. Since Callback is not
387 // reference counted, we bind to an instance that is a member of the
388 // CookieMonsterTask subclass. Then, we cannot simply post the callback to a
389 // message loop because the underlying instance may be destroyed (along with the
390 // CookieMonsterTask instance) in the interim. Therefore, we post a callback
391 // bound to the CookieMonsterTask, which *is* reference counted (thus preventing
392 // destruction of the original callback), and which invokes the closure (which
393 // invokes the original callback with the returned data).
394 void CookieMonster::CookieMonsterTask::InvokeCallback(base::Closure callback) {
395 if (thread_->BelongsToCurrentThread()) {
396 callback.Run();
397 } else {
398 thread_->PostTask(FROM_HERE, base::Bind(
399 &CookieMonsterTask::InvokeCallback, this, callback));
403 // Task class for SetCookieWithDetails call.
404 class CookieMonster::SetCookieWithDetailsTask : public CookieMonsterTask {
405 public:
406 SetCookieWithDetailsTask(CookieMonster* cookie_monster,
407 const GURL& url,
408 const std::string& name,
409 const std::string& value,
410 const std::string& domain,
411 const std::string& path,
412 const base::Time& expiration_time,
413 bool secure,
414 bool http_only,
415 CookiePriority priority,
416 const SetCookiesCallback& callback)
417 : CookieMonsterTask(cookie_monster),
418 url_(url),
419 name_(name),
420 value_(value),
421 domain_(domain),
422 path_(path),
423 expiration_time_(expiration_time),
424 secure_(secure),
425 http_only_(http_only),
426 priority_(priority),
427 callback_(callback) {
430 // CookieMonsterTask:
431 virtual void Run() OVERRIDE;
433 protected:
434 virtual ~SetCookieWithDetailsTask() {}
436 private:
437 GURL url_;
438 std::string name_;
439 std::string value_;
440 std::string domain_;
441 std::string path_;
442 base::Time expiration_time_;
443 bool secure_;
444 bool http_only_;
445 CookiePriority priority_;
446 SetCookiesCallback callback_;
448 DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask);
451 void CookieMonster::SetCookieWithDetailsTask::Run() {
452 bool success = this->cookie_monster()->
453 SetCookieWithDetails(url_, name_, value_, domain_, path_,
454 expiration_time_, secure_, http_only_, priority_);
455 if (!callback_.is_null()) {
456 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run,
457 base::Unretained(&callback_), success));
461 // Task class for GetAllCookies call.
462 class CookieMonster::GetAllCookiesTask : public CookieMonsterTask {
463 public:
464 GetAllCookiesTask(CookieMonster* cookie_monster,
465 const GetCookieListCallback& callback)
466 : CookieMonsterTask(cookie_monster),
467 callback_(callback) {
470 // CookieMonsterTask
471 virtual void Run() OVERRIDE;
473 protected:
474 virtual ~GetAllCookiesTask() {}
476 private:
477 GetCookieListCallback callback_;
479 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask);
482 void CookieMonster::GetAllCookiesTask::Run() {
483 if (!callback_.is_null()) {
484 CookieList cookies = this->cookie_monster()->GetAllCookies();
485 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run,
486 base::Unretained(&callback_), cookies));
490 // Task class for GetAllCookiesForURLWithOptions call.
491 class CookieMonster::GetAllCookiesForURLWithOptionsTask
492 : public CookieMonsterTask {
493 public:
494 GetAllCookiesForURLWithOptionsTask(
495 CookieMonster* cookie_monster,
496 const GURL& url,
497 const CookieOptions& options,
498 const GetCookieListCallback& callback)
499 : CookieMonsterTask(cookie_monster),
500 url_(url),
501 options_(options),
502 callback_(callback) {
505 // CookieMonsterTask:
506 virtual void Run() OVERRIDE;
508 protected:
509 virtual ~GetAllCookiesForURLWithOptionsTask() {}
511 private:
512 GURL url_;
513 CookieOptions options_;
514 GetCookieListCallback callback_;
516 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesForURLWithOptionsTask);
519 void CookieMonster::GetAllCookiesForURLWithOptionsTask::Run() {
520 if (!callback_.is_null()) {
521 CookieList cookies = this->cookie_monster()->
522 GetAllCookiesForURLWithOptions(url_, options_);
523 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run,
524 base::Unretained(&callback_), cookies));
528 template <typename Result> struct CallbackType {
529 typedef base::Callback<void(Result)> Type;
532 template <> struct CallbackType<void> {
533 typedef base::Closure Type;
536 // Base task class for Delete*Task.
537 template <typename Result>
538 class CookieMonster::DeleteTask : public CookieMonsterTask {
539 public:
540 DeleteTask(CookieMonster* cookie_monster,
541 const typename CallbackType<Result>::Type& callback)
542 : CookieMonsterTask(cookie_monster),
543 callback_(callback) {
546 // CookieMonsterTask:
547 virtual void Run() OVERRIDE;
549 private:
550 // Runs the delete task and returns a result.
551 virtual Result RunDeleteTask() = 0;
552 base::Closure RunDeleteTaskAndBindCallback();
553 void FlushDone(const base::Closure& callback);
555 typename CallbackType<Result>::Type callback_;
557 DISALLOW_COPY_AND_ASSIGN(DeleteTask);
560 template <typename Result>
561 base::Closure CookieMonster::DeleteTask<Result>::
562 RunDeleteTaskAndBindCallback() {
563 Result result = RunDeleteTask();
564 if (callback_.is_null())
565 return base::Closure();
566 return base::Bind(callback_, result);
569 template <>
570 base::Closure CookieMonster::DeleteTask<void>::RunDeleteTaskAndBindCallback() {
571 RunDeleteTask();
572 return callback_;
575 template <typename Result>
576 void CookieMonster::DeleteTask<Result>::Run() {
577 this->cookie_monster()->FlushStore(
578 base::Bind(&DeleteTask<Result>::FlushDone, this,
579 RunDeleteTaskAndBindCallback()));
582 template <typename Result>
583 void CookieMonster::DeleteTask<Result>::FlushDone(
584 const base::Closure& callback) {
585 if (!callback.is_null()) {
586 this->InvokeCallback(callback);
590 // Task class for DeleteAll call.
591 class CookieMonster::DeleteAllTask : public DeleteTask<int> {
592 public:
593 DeleteAllTask(CookieMonster* cookie_monster,
594 const DeleteCallback& callback)
595 : DeleteTask<int>(cookie_monster, callback) {
598 // DeleteTask:
599 virtual int RunDeleteTask() OVERRIDE;
601 protected:
602 virtual ~DeleteAllTask() {}
604 private:
605 DISALLOW_COPY_AND_ASSIGN(DeleteAllTask);
608 int CookieMonster::DeleteAllTask::RunDeleteTask() {
609 return this->cookie_monster()->DeleteAll(true);
612 // Task class for DeleteAllCreatedBetween call.
613 class CookieMonster::DeleteAllCreatedBetweenTask : public DeleteTask<int> {
614 public:
615 DeleteAllCreatedBetweenTask(CookieMonster* cookie_monster,
616 const Time& delete_begin,
617 const Time& delete_end,
618 const DeleteCallback& callback)
619 : DeleteTask<int>(cookie_monster, callback),
620 delete_begin_(delete_begin),
621 delete_end_(delete_end) {
624 // DeleteTask:
625 virtual int RunDeleteTask() OVERRIDE;
627 protected:
628 virtual ~DeleteAllCreatedBetweenTask() {}
630 private:
631 Time delete_begin_;
632 Time delete_end_;
634 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask);
637 int CookieMonster::DeleteAllCreatedBetweenTask::RunDeleteTask() {
638 return this->cookie_monster()->
639 DeleteAllCreatedBetween(delete_begin_, delete_end_);
642 // Task class for DeleteAllForHost call.
643 class CookieMonster::DeleteAllForHostTask : public DeleteTask<int> {
644 public:
645 DeleteAllForHostTask(CookieMonster* cookie_monster,
646 const GURL& url,
647 const DeleteCallback& callback)
648 : DeleteTask<int>(cookie_monster, callback),
649 url_(url) {
652 // DeleteTask:
653 virtual int RunDeleteTask() OVERRIDE;
655 protected:
656 virtual ~DeleteAllForHostTask() {}
658 private:
659 GURL url_;
661 DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask);
664 int CookieMonster::DeleteAllForHostTask::RunDeleteTask() {
665 return this->cookie_monster()->DeleteAllForHost(url_);
668 // Task class for DeleteAllCreatedBetweenForHost call.
669 class CookieMonster::DeleteAllCreatedBetweenForHostTask
670 : public DeleteTask<int> {
671 public:
672 DeleteAllCreatedBetweenForHostTask(
673 CookieMonster* cookie_monster,
674 Time delete_begin,
675 Time delete_end,
676 const GURL& url,
677 const DeleteCallback& callback)
678 : DeleteTask<int>(cookie_monster, callback),
679 delete_begin_(delete_begin),
680 delete_end_(delete_end),
681 url_(url) {
684 // DeleteTask:
685 virtual int RunDeleteTask() OVERRIDE;
687 protected:
688 virtual ~DeleteAllCreatedBetweenForHostTask() {}
690 private:
691 Time delete_begin_;
692 Time delete_end_;
693 GURL url_;
695 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenForHostTask);
698 int CookieMonster::DeleteAllCreatedBetweenForHostTask::RunDeleteTask() {
699 return this->cookie_monster()->DeleteAllCreatedBetweenForHost(
700 delete_begin_, delete_end_, url_);
703 // Task class for DeleteCanonicalCookie call.
704 class CookieMonster::DeleteCanonicalCookieTask : public DeleteTask<bool> {
705 public:
706 DeleteCanonicalCookieTask(CookieMonster* cookie_monster,
707 const CanonicalCookie& cookie,
708 const DeleteCookieCallback& callback)
709 : DeleteTask<bool>(cookie_monster, callback),
710 cookie_(cookie) {
713 // DeleteTask:
714 virtual bool RunDeleteTask() OVERRIDE;
716 protected:
717 virtual ~DeleteCanonicalCookieTask() {}
719 private:
720 CanonicalCookie cookie_;
722 DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask);
725 bool CookieMonster::DeleteCanonicalCookieTask::RunDeleteTask() {
726 return this->cookie_monster()->DeleteCanonicalCookie(cookie_);
729 // Task class for SetCookieWithOptions call.
730 class CookieMonster::SetCookieWithOptionsTask : public CookieMonsterTask {
731 public:
732 SetCookieWithOptionsTask(CookieMonster* cookie_monster,
733 const GURL& url,
734 const std::string& cookie_line,
735 const CookieOptions& options,
736 const SetCookiesCallback& callback)
737 : CookieMonsterTask(cookie_monster),
738 url_(url),
739 cookie_line_(cookie_line),
740 options_(options),
741 callback_(callback) {
744 // CookieMonsterTask:
745 virtual void Run() OVERRIDE;
747 protected:
748 virtual ~SetCookieWithOptionsTask() {}
750 private:
751 GURL url_;
752 std::string cookie_line_;
753 CookieOptions options_;
754 SetCookiesCallback callback_;
756 DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask);
759 void CookieMonster::SetCookieWithOptionsTask::Run() {
760 bool result = this->cookie_monster()->
761 SetCookieWithOptions(url_, cookie_line_, options_);
762 if (!callback_.is_null()) {
763 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run,
764 base::Unretained(&callback_), result));
768 // Task class for GetCookiesWithOptions call.
769 class CookieMonster::GetCookiesWithOptionsTask : public CookieMonsterTask {
770 public:
771 GetCookiesWithOptionsTask(CookieMonster* cookie_monster,
772 const GURL& url,
773 const CookieOptions& options,
774 const GetCookiesCallback& callback)
775 : CookieMonsterTask(cookie_monster),
776 url_(url),
777 options_(options),
778 callback_(callback) {
781 // CookieMonsterTask:
782 virtual void Run() OVERRIDE;
784 protected:
785 virtual ~GetCookiesWithOptionsTask() {}
787 private:
788 GURL url_;
789 CookieOptions options_;
790 GetCookiesCallback callback_;
792 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask);
795 void CookieMonster::GetCookiesWithOptionsTask::Run() {
796 std::string cookie = this->cookie_monster()->
797 GetCookiesWithOptions(url_, options_);
798 if (!callback_.is_null()) {
799 this->InvokeCallback(base::Bind(&GetCookiesCallback::Run,
800 base::Unretained(&callback_), cookie));
804 // Task class for DeleteCookie call.
805 class CookieMonster::DeleteCookieTask : public DeleteTask<void> {
806 public:
807 DeleteCookieTask(CookieMonster* cookie_monster,
808 const GURL& url,
809 const std::string& cookie_name,
810 const base::Closure& callback)
811 : DeleteTask<void>(cookie_monster, callback),
812 url_(url),
813 cookie_name_(cookie_name) {
816 // DeleteTask:
817 virtual void RunDeleteTask() OVERRIDE;
819 protected:
820 virtual ~DeleteCookieTask() {}
822 private:
823 GURL url_;
824 std::string cookie_name_;
826 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
829 void CookieMonster::DeleteCookieTask::RunDeleteTask() {
830 this->cookie_monster()->DeleteCookie(url_, cookie_name_);
833 // Task class for DeleteSessionCookies call.
834 class CookieMonster::DeleteSessionCookiesTask : public DeleteTask<int> {
835 public:
836 DeleteSessionCookiesTask(CookieMonster* cookie_monster,
837 const DeleteCallback& callback)
838 : DeleteTask<int>(cookie_monster, callback) {
841 // DeleteTask:
842 virtual int RunDeleteTask() OVERRIDE;
844 protected:
845 virtual ~DeleteSessionCookiesTask() {}
847 private:
849 DISALLOW_COPY_AND_ASSIGN(DeleteSessionCookiesTask);
852 int CookieMonster::DeleteSessionCookiesTask::RunDeleteTask() {
853 return this->cookie_monster()->DeleteSessionCookies();
856 // Task class for HasCookiesForETLDP1Task call.
857 class CookieMonster::HasCookiesForETLDP1Task : public CookieMonsterTask {
858 public:
859 HasCookiesForETLDP1Task(
860 CookieMonster* cookie_monster,
861 const std::string& etldp1,
862 const HasCookiesForETLDP1Callback& callback)
863 : CookieMonsterTask(cookie_monster),
864 etldp1_(etldp1),
865 callback_(callback) {
868 // CookieMonsterTask:
869 virtual void Run() OVERRIDE;
871 protected:
872 virtual ~HasCookiesForETLDP1Task() {}
874 private:
875 std::string etldp1_;
876 HasCookiesForETLDP1Callback callback_;
878 DISALLOW_COPY_AND_ASSIGN(HasCookiesForETLDP1Task);
881 void CookieMonster::HasCookiesForETLDP1Task::Run() {
882 bool result = this->cookie_monster()->HasCookiesForETLDP1(etldp1_);
883 if (!callback_.is_null()) {
884 this->InvokeCallback(
885 base::Bind(&HasCookiesForETLDP1Callback::Run,
886 base::Unretained(&callback_), result));
890 // Asynchronous CookieMonster API
892 void CookieMonster::SetCookieWithDetailsAsync(
893 const GURL& url,
894 const std::string& name,
895 const std::string& value,
896 const std::string& domain,
897 const std::string& path,
898 const Time& expiration_time,
899 bool secure,
900 bool http_only,
901 CookiePriority priority,
902 const SetCookiesCallback& callback) {
903 scoped_refptr<SetCookieWithDetailsTask> task =
904 new SetCookieWithDetailsTask(this, url, name, value, domain, path,
905 expiration_time, secure, http_only, priority,
906 callback);
908 DoCookieTaskForURL(task, url);
911 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) {
912 scoped_refptr<GetAllCookiesTask> task =
913 new GetAllCookiesTask(this, callback);
915 DoCookieTask(task);
919 void CookieMonster::GetAllCookiesForURLWithOptionsAsync(
920 const GURL& url,
921 const CookieOptions& options,
922 const GetCookieListCallback& callback) {
923 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
924 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
926 DoCookieTaskForURL(task, url);
929 void CookieMonster::GetAllCookiesForURLAsync(
930 const GURL& url, const GetCookieListCallback& callback) {
931 CookieOptions options;
932 options.set_include_httponly();
933 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
934 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
936 DoCookieTaskForURL(task, url);
939 void CookieMonster::HasCookiesForETLDP1Async(
940 const std::string& etldp1,
941 const HasCookiesForETLDP1Callback& callback) {
942 scoped_refptr<HasCookiesForETLDP1Task> task =
943 new HasCookiesForETLDP1Task(this, etldp1, callback);
945 DoCookieTaskForURL(task, GURL("http://" + etldp1));
948 void CookieMonster::DeleteAllAsync(const DeleteCallback& callback) {
949 scoped_refptr<DeleteAllTask> task =
950 new DeleteAllTask(this, callback);
952 DoCookieTask(task);
955 void CookieMonster::DeleteAllCreatedBetweenAsync(
956 const Time& delete_begin, const Time& delete_end,
957 const DeleteCallback& callback) {
958 scoped_refptr<DeleteAllCreatedBetweenTask> task =
959 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end,
960 callback);
962 DoCookieTask(task);
965 void CookieMonster::DeleteAllCreatedBetweenForHostAsync(
966 const Time delete_begin,
967 const Time delete_end,
968 const GURL& url,
969 const DeleteCallback& callback) {
970 scoped_refptr<DeleteAllCreatedBetweenForHostTask> task =
971 new DeleteAllCreatedBetweenForHostTask(
972 this, delete_begin, delete_end, url, callback);
974 DoCookieTaskForURL(task, url);
977 void CookieMonster::DeleteAllForHostAsync(
978 const GURL& url, const DeleteCallback& callback) {
979 scoped_refptr<DeleteAllForHostTask> task =
980 new DeleteAllForHostTask(this, url, callback);
982 DoCookieTaskForURL(task, url);
985 void CookieMonster::DeleteCanonicalCookieAsync(
986 const CanonicalCookie& cookie,
987 const DeleteCookieCallback& callback) {
988 scoped_refptr<DeleteCanonicalCookieTask> task =
989 new DeleteCanonicalCookieTask(this, cookie, callback);
991 DoCookieTask(task);
994 void CookieMonster::SetCookieWithOptionsAsync(
995 const GURL& url,
996 const std::string& cookie_line,
997 const CookieOptions& options,
998 const SetCookiesCallback& callback) {
999 scoped_refptr<SetCookieWithOptionsTask> task =
1000 new SetCookieWithOptionsTask(this, url, cookie_line, options, callback);
1002 DoCookieTaskForURL(task, url);
1005 void CookieMonster::GetCookiesWithOptionsAsync(
1006 const GURL& url,
1007 const CookieOptions& options,
1008 const GetCookiesCallback& callback) {
1009 scoped_refptr<GetCookiesWithOptionsTask> task =
1010 new GetCookiesWithOptionsTask(this, url, options, callback);
1012 DoCookieTaskForURL(task, url);
1015 void CookieMonster::DeleteCookieAsync(const GURL& url,
1016 const std::string& cookie_name,
1017 const base::Closure& callback) {
1018 scoped_refptr<DeleteCookieTask> task =
1019 new DeleteCookieTask(this, url, cookie_name, callback);
1021 DoCookieTaskForURL(task, url);
1024 void CookieMonster::DeleteSessionCookiesAsync(
1025 const CookieStore::DeleteCallback& callback) {
1026 scoped_refptr<DeleteSessionCookiesTask> task =
1027 new DeleteSessionCookiesTask(this, callback);
1029 DoCookieTask(task);
1032 void CookieMonster::DoCookieTask(
1033 const scoped_refptr<CookieMonsterTask>& task_item) {
1035 base::AutoLock autolock(lock_);
1036 InitIfNecessary();
1037 if (!loaded_) {
1038 tasks_pending_.push(task_item);
1039 return;
1043 task_item->Run();
1046 void CookieMonster::DoCookieTaskForURL(
1047 const scoped_refptr<CookieMonsterTask>& task_item,
1048 const GURL& url) {
1050 base::AutoLock autolock(lock_);
1051 InitIfNecessary();
1052 // If cookies for the requested domain key (eTLD+1) have been loaded from DB
1053 // then run the task, otherwise load from DB.
1054 if (!loaded_) {
1055 // Checks if the domain key has been loaded.
1056 std::string key(cookie_util::GetEffectiveDomain(url.scheme(),
1057 url.host()));
1058 if (keys_loaded_.find(key) == keys_loaded_.end()) {
1059 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > >
1060 ::iterator it = tasks_pending_for_key_.find(key);
1061 if (it == tasks_pending_for_key_.end()) {
1062 store_->LoadCookiesForKey(key,
1063 base::Bind(&CookieMonster::OnKeyLoaded, this, key));
1064 it = tasks_pending_for_key_.insert(std::make_pair(key,
1065 std::deque<scoped_refptr<CookieMonsterTask> >())).first;
1067 it->second.push_back(task_item);
1068 return;
1072 task_item->Run();
1075 bool CookieMonster::SetCookieWithDetails(const GURL& url,
1076 const std::string& name,
1077 const std::string& value,
1078 const std::string& domain,
1079 const std::string& path,
1080 const base::Time& expiration_time,
1081 bool secure,
1082 bool http_only,
1083 CookiePriority priority) {
1084 base::AutoLock autolock(lock_);
1086 if (!HasCookieableScheme(url))
1087 return false;
1089 Time creation_time = CurrentTime();
1090 last_time_seen_ = creation_time;
1092 scoped_ptr<CanonicalCookie> cc;
1093 cc.reset(CanonicalCookie::Create(url, name, value, domain, path,
1094 creation_time, expiration_time,
1095 secure, http_only, priority));
1097 if (!cc.get())
1098 return false;
1100 CookieOptions options;
1101 options.set_include_httponly();
1102 return SetCanonicalCookie(&cc, creation_time, options);
1105 bool CookieMonster::InitializeFrom(const CookieList& list) {
1106 base::AutoLock autolock(lock_);
1107 InitIfNecessary();
1108 for (net::CookieList::const_iterator iter = list.begin();
1109 iter != list.end(); ++iter) {
1110 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie(*iter));
1111 net::CookieOptions options;
1112 options.set_include_httponly();
1113 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), options))
1114 return false;
1116 return true;
1119 CookieList CookieMonster::GetAllCookies() {
1120 base::AutoLock autolock(lock_);
1122 // This function is being called to scrape the cookie list for management UI
1123 // or similar. We shouldn't show expired cookies in this list since it will
1124 // just be confusing to users, and this function is called rarely enough (and
1125 // is already slow enough) that it's OK to take the time to garbage collect
1126 // the expired cookies now.
1128 // Note that this does not prune cookies to be below our limits (if we've
1129 // exceeded them) the way that calling GarbageCollect() would.
1130 GarbageCollectExpired(Time::Now(),
1131 CookieMapItPair(cookies_.begin(), cookies_.end()),
1132 NULL);
1134 // Copy the CanonicalCookie pointers from the map so that we can use the same
1135 // sorter as elsewhere, then copy the result out.
1136 std::vector<CanonicalCookie*> cookie_ptrs;
1137 cookie_ptrs.reserve(cookies_.size());
1138 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end(); ++it)
1139 cookie_ptrs.push_back(it->second);
1140 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
1142 CookieList cookie_list;
1143 cookie_list.reserve(cookie_ptrs.size());
1144 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
1145 it != cookie_ptrs.end(); ++it)
1146 cookie_list.push_back(**it);
1148 return cookie_list;
1151 CookieList CookieMonster::GetAllCookiesForURLWithOptions(
1152 const GURL& url,
1153 const CookieOptions& options) {
1154 base::AutoLock autolock(lock_);
1156 std::vector<CanonicalCookie*> cookie_ptrs;
1157 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs);
1158 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
1160 CookieList cookies;
1161 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
1162 it != cookie_ptrs.end(); it++)
1163 cookies.push_back(**it);
1165 return cookies;
1168 CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) {
1169 CookieOptions options;
1170 options.set_include_httponly();
1172 return GetAllCookiesForURLWithOptions(url, options);
1175 int CookieMonster::DeleteAll(bool sync_to_store) {
1176 base::AutoLock autolock(lock_);
1178 int num_deleted = 0;
1179 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1180 CookieMap::iterator curit = it;
1181 ++it;
1182 InternalDeleteCookie(curit, sync_to_store,
1183 sync_to_store ? DELETE_COOKIE_EXPLICIT :
1184 DELETE_COOKIE_DONT_RECORD /* Destruction. */);
1185 ++num_deleted;
1188 return num_deleted;
1191 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin,
1192 const Time& delete_end) {
1193 base::AutoLock autolock(lock_);
1195 int num_deleted = 0;
1196 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1197 CookieMap::iterator curit = it;
1198 CanonicalCookie* cc = curit->second;
1199 ++it;
1201 if (cc->CreationDate() >= delete_begin &&
1202 (delete_end.is_null() || cc->CreationDate() < delete_end)) {
1203 InternalDeleteCookie(curit,
1204 true, /*sync_to_store*/
1205 DELETE_COOKIE_EXPLICIT);
1206 ++num_deleted;
1210 return num_deleted;
1213 int CookieMonster::DeleteAllCreatedBetweenForHost(const Time delete_begin,
1214 const Time delete_end,
1215 const GURL& url) {
1216 base::AutoLock autolock(lock_);
1218 if (!HasCookieableScheme(url))
1219 return 0;
1221 const std::string host(url.host());
1223 // We store host cookies in the store by their canonical host name;
1224 // domain cookies are stored with a leading ".". So this is a pretty
1225 // simple lookup and per-cookie delete.
1226 int num_deleted = 0;
1227 for (CookieMapItPair its = cookies_.equal_range(GetKey(host));
1228 its.first != its.second;) {
1229 CookieMap::iterator curit = its.first;
1230 ++its.first;
1232 const CanonicalCookie* const cc = curit->second;
1234 // Delete only on a match as a host cookie.
1235 if (cc->IsHostCookie() && cc->IsDomainMatch(host) &&
1236 cc->CreationDate() >= delete_begin &&
1237 // The assumption that null |delete_end| is equivalent to
1238 // Time::Max() is confusing.
1239 (delete_end.is_null() || cc->CreationDate() < delete_end)) {
1240 num_deleted++;
1242 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT);
1245 return num_deleted;
1248 int CookieMonster::DeleteAllForHost(const GURL& url) {
1249 return DeleteAllCreatedBetweenForHost(Time(), Time::Max(), url);
1253 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) {
1254 base::AutoLock autolock(lock_);
1256 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain()));
1257 its.first != its.second; ++its.first) {
1258 // The creation date acts as our unique index...
1259 if (its.first->second->CreationDate() == cookie.CreationDate()) {
1260 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT);
1261 return true;
1264 return false;
1267 void CookieMonster::SetCookieableSchemes(const char* schemes[],
1268 size_t num_schemes) {
1269 base::AutoLock autolock(lock_);
1271 // Cookieable Schemes must be set before first use of function.
1272 DCHECK(!initialized_);
1274 cookieable_schemes_.clear();
1275 cookieable_schemes_.insert(cookieable_schemes_.end(),
1276 schemes, schemes + num_schemes);
1279 void CookieMonster::SetEnableFileScheme(bool accept) {
1280 // This assumes "file" is always at the end of the array. See the comment
1281 // above kDefaultCookieableSchemes.
1282 int num_schemes = accept ? kDefaultCookieableSchemesCount :
1283 kDefaultCookieableSchemesCount - 1;
1284 SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes);
1287 void CookieMonster::SetKeepExpiredCookies() {
1288 keep_expired_cookies_ = true;
1291 void CookieMonster::FlushStore(const base::Closure& callback) {
1292 base::AutoLock autolock(lock_);
1293 if (initialized_ && store_.get())
1294 store_->Flush(callback);
1295 else if (!callback.is_null())
1296 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
1299 bool CookieMonster::SetCookieWithOptions(const GURL& url,
1300 const std::string& cookie_line,
1301 const CookieOptions& options) {
1302 base::AutoLock autolock(lock_);
1304 if (!HasCookieableScheme(url)) {
1305 return false;
1308 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options);
1311 std::string CookieMonster::GetCookiesWithOptions(const GURL& url,
1312 const CookieOptions& options) {
1313 base::AutoLock autolock(lock_);
1315 if (!HasCookieableScheme(url))
1316 return std::string();
1318 TimeTicks start_time(TimeTicks::Now());
1320 std::vector<CanonicalCookie*> cookies;
1321 FindCookiesForHostAndDomain(url, options, true, &cookies);
1322 std::sort(cookies.begin(), cookies.end(), CookieSorter);
1324 std::string cookie_line = BuildCookieLine(cookies);
1326 histogram_time_get_->AddTime(TimeTicks::Now() - start_time);
1328 VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line;
1330 return cookie_line;
1333 void CookieMonster::DeleteCookie(const GURL& url,
1334 const std::string& cookie_name) {
1335 base::AutoLock autolock(lock_);
1337 if (!HasCookieableScheme(url))
1338 return;
1340 CookieOptions options;
1341 options.set_include_httponly();
1342 // Get the cookies for this host and its domain(s).
1343 std::vector<CanonicalCookie*> cookies;
1344 FindCookiesForHostAndDomain(url, options, true, &cookies);
1345 std::set<CanonicalCookie*> matching_cookies;
1347 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin();
1348 it != cookies.end(); ++it) {
1349 if ((*it)->Name() != cookie_name)
1350 continue;
1351 if (url.path().find((*it)->Path()))
1352 continue;
1353 matching_cookies.insert(*it);
1356 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1357 CookieMap::iterator curit = it;
1358 ++it;
1359 if (matching_cookies.find(curit->second) != matching_cookies.end()) {
1360 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT);
1365 int CookieMonster::DeleteSessionCookies() {
1366 base::AutoLock autolock(lock_);
1368 int num_deleted = 0;
1369 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1370 CookieMap::iterator curit = it;
1371 CanonicalCookie* cc = curit->second;
1372 ++it;
1374 if (!cc->IsPersistent()) {
1375 InternalDeleteCookie(curit,
1376 true, /*sync_to_store*/
1377 DELETE_COOKIE_EXPIRED);
1378 ++num_deleted;
1382 return num_deleted;
1385 bool CookieMonster::HasCookiesForETLDP1(const std::string& etldp1) {
1386 base::AutoLock autolock(lock_);
1388 const std::string key(GetKey(etldp1));
1390 CookieMapItPair its = cookies_.equal_range(key);
1391 return its.first != its.second;
1394 CookieMonster* CookieMonster::GetCookieMonster() {
1395 return this;
1398 // This function must be called before the CookieMonster is used.
1399 void CookieMonster::SetPersistSessionCookies(bool persist_session_cookies) {
1400 DCHECK(!initialized_);
1401 persist_session_cookies_ = persist_session_cookies;
1404 void CookieMonster::SetForceKeepSessionState() {
1405 if (store_.get()) {
1406 store_->SetForceKeepSessionState();
1410 CookieMonster::~CookieMonster() {
1411 DeleteAll(false);
1414 bool CookieMonster::SetCookieWithCreationTime(const GURL& url,
1415 const std::string& cookie_line,
1416 const base::Time& creation_time) {
1417 DCHECK(!store_.get()) << "This method is only to be used by unit-tests.";
1418 base::AutoLock autolock(lock_);
1420 if (!HasCookieableScheme(url)) {
1421 return false;
1424 InitIfNecessary();
1425 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time,
1426 CookieOptions());
1429 void CookieMonster::InitStore() {
1430 DCHECK(store_.get()) << "Store must exist to initialize";
1432 // We bind in the current time so that we can report the wall-clock time for
1433 // loading cookies.
1434 store_->Load(base::Bind(&CookieMonster::OnLoaded, this, TimeTicks::Now()));
1437 void CookieMonster::ReportLoaded() {
1438 if (delegate_.get())
1439 delegate_->OnLoaded();
1442 void CookieMonster::OnLoaded(TimeTicks beginning_time,
1443 const std::vector<CanonicalCookie*>& cookies) {
1444 StoreLoadedCookies(cookies);
1445 histogram_time_blocked_on_load_->AddTime(TimeTicks::Now() - beginning_time);
1447 // Invoke the task queue of cookie request.
1448 InvokeQueue();
1450 ReportLoaded();
1453 void CookieMonster::OnKeyLoaded(const std::string& key,
1454 const std::vector<CanonicalCookie*>& cookies) {
1455 // This function does its own separate locking.
1456 StoreLoadedCookies(cookies);
1458 std::deque<scoped_refptr<CookieMonsterTask> > tasks_pending_for_key;
1460 // We need to do this repeatedly until no more tasks were added to the queue
1461 // during the period where we release the lock.
1462 while (true) {
1464 base::AutoLock autolock(lock_);
1465 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > >
1466 ::iterator it = tasks_pending_for_key_.find(key);
1467 if (it == tasks_pending_for_key_.end()) {
1468 keys_loaded_.insert(key);
1469 return;
1471 if (it->second.empty()) {
1472 keys_loaded_.insert(key);
1473 tasks_pending_for_key_.erase(it);
1474 return;
1476 it->second.swap(tasks_pending_for_key);
1479 while (!tasks_pending_for_key.empty()) {
1480 scoped_refptr<CookieMonsterTask> task = tasks_pending_for_key.front();
1481 task->Run();
1482 tasks_pending_for_key.pop_front();
1487 void CookieMonster::StoreLoadedCookies(
1488 const std::vector<CanonicalCookie*>& cookies) {
1489 // Initialize the store and sync in any saved persistent cookies. We don't
1490 // care if it's expired, insert it so it can be garbage collected, removed,
1491 // and sync'd.
1492 base::AutoLock autolock(lock_);
1494 CookieItVector cookies_with_control_chars;
1496 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin();
1497 it != cookies.end(); ++it) {
1498 int64 cookie_creation_time = (*it)->CreationDate().ToInternalValue();
1500 if (creation_times_.insert(cookie_creation_time).second) {
1501 CookieMap::iterator inserted =
1502 InternalInsertCookie(GetKey((*it)->Domain()), *it, false);
1503 const Time cookie_access_time((*it)->LastAccessDate());
1504 if (earliest_access_time_.is_null() ||
1505 cookie_access_time < earliest_access_time_)
1506 earliest_access_time_ = cookie_access_time;
1508 if (ContainsControlCharacter((*it)->Name()) ||
1509 ContainsControlCharacter((*it)->Value())) {
1510 cookies_with_control_chars.push_back(inserted);
1512 } else {
1513 LOG(ERROR) << base::StringPrintf("Found cookies with duplicate creation "
1514 "times in backing store: "
1515 "{name='%s', domain='%s', path='%s'}",
1516 (*it)->Name().c_str(),
1517 (*it)->Domain().c_str(),
1518 (*it)->Path().c_str());
1519 // We've been given ownership of the cookie and are throwing it
1520 // away; reclaim the space.
1521 delete (*it);
1525 // Any cookies that contain control characters that we have loaded from the
1526 // persistent store should be deleted. See http://crbug.com/238041.
1527 for (CookieItVector::iterator it = cookies_with_control_chars.begin();
1528 it != cookies_with_control_chars.end();) {
1529 CookieItVector::iterator curit = it;
1530 ++it;
1532 InternalDeleteCookie(*curit, true, DELETE_COOKIE_CONTROL_CHAR);
1535 // After importing cookies from the PersistentCookieStore, verify that
1536 // none of our other constraints are violated.
1537 // In particular, the backing store might have given us duplicate cookies.
1539 // This method could be called multiple times due to priority loading, thus
1540 // cookies loaded in previous runs will be validated again, but this is OK
1541 // since they are expected to be much fewer than total DB.
1542 EnsureCookiesMapIsValid();
1545 void CookieMonster::InvokeQueue() {
1546 while (true) {
1547 scoped_refptr<CookieMonsterTask> request_task;
1549 base::AutoLock autolock(lock_);
1550 if (tasks_pending_.empty()) {
1551 loaded_ = true;
1552 creation_times_.clear();
1553 keys_loaded_.clear();
1554 break;
1556 request_task = tasks_pending_.front();
1557 tasks_pending_.pop();
1559 request_task->Run();
1563 void CookieMonster::EnsureCookiesMapIsValid() {
1564 lock_.AssertAcquired();
1566 int num_duplicates_trimmed = 0;
1568 // Iterate through all the of the cookies, grouped by host.
1569 CookieMap::iterator prev_range_end = cookies_.begin();
1570 while (prev_range_end != cookies_.end()) {
1571 CookieMap::iterator cur_range_begin = prev_range_end;
1572 const std::string key = cur_range_begin->first; // Keep a copy.
1573 CookieMap::iterator cur_range_end = cookies_.upper_bound(key);
1574 prev_range_end = cur_range_end;
1576 // Ensure no equivalent cookies for this host.
1577 num_duplicates_trimmed +=
1578 TrimDuplicateCookiesForKey(key, cur_range_begin, cur_range_end);
1581 // Record how many duplicates were found in the database.
1582 // See InitializeHistograms() for details.
1583 histogram_cookie_deletion_cause_->Add(num_duplicates_trimmed);
1586 int CookieMonster::TrimDuplicateCookiesForKey(
1587 const std::string& key,
1588 CookieMap::iterator begin,
1589 CookieMap::iterator end) {
1590 lock_.AssertAcquired();
1592 // Set of cookies ordered by creation time.
1593 typedef std::set<CookieMap::iterator, OrderByCreationTimeDesc> CookieSet;
1595 // Helper map we populate to find the duplicates.
1596 typedef std::map<CookieSignature, CookieSet> EquivalenceMap;
1597 EquivalenceMap equivalent_cookies;
1599 // The number of duplicate cookies that have been found.
1600 int num_duplicates = 0;
1602 // Iterate through all of the cookies in our range, and insert them into
1603 // the equivalence map.
1604 for (CookieMap::iterator it = begin; it != end; ++it) {
1605 DCHECK_EQ(key, it->first);
1606 CanonicalCookie* cookie = it->second;
1608 CookieSignature signature(cookie->Name(), cookie->Domain(),
1609 cookie->Path());
1610 CookieSet& set = equivalent_cookies[signature];
1612 // We found a duplicate!
1613 if (!set.empty())
1614 num_duplicates++;
1616 // We save the iterator into |cookies_| rather than the actual cookie
1617 // pointer, since we may need to delete it later.
1618 bool insert_success = set.insert(it).second;
1619 DCHECK(insert_success) <<
1620 "Duplicate creation times found in duplicate cookie name scan.";
1623 // If there were no duplicates, we are done!
1624 if (num_duplicates == 0)
1625 return 0;
1627 // Make sure we find everything below that we did above.
1628 int num_duplicates_found = 0;
1630 // Otherwise, delete all the duplicate cookies, both from our in-memory store
1631 // and from the backing store.
1632 for (EquivalenceMap::iterator it = equivalent_cookies.begin();
1633 it != equivalent_cookies.end();
1634 ++it) {
1635 const CookieSignature& signature = it->first;
1636 CookieSet& dupes = it->second;
1638 if (dupes.size() <= 1)
1639 continue; // This cookiename/path has no duplicates.
1640 num_duplicates_found += dupes.size() - 1;
1642 // Since |dups| is sorted by creation time (descending), the first cookie
1643 // is the most recent one, so we will keep it. The rest are duplicates.
1644 dupes.erase(dupes.begin());
1646 LOG(ERROR) << base::StringPrintf(
1647 "Found %d duplicate cookies for host='%s', "
1648 "with {name='%s', domain='%s', path='%s'}",
1649 static_cast<int>(dupes.size()),
1650 key.c_str(),
1651 signature.name.c_str(),
1652 signature.domain.c_str(),
1653 signature.path.c_str());
1655 // Remove all the cookies identified by |dupes|. It is valid to delete our
1656 // list of iterators one at a time, since |cookies_| is a multimap (they
1657 // don't invalidate existing iterators following deletion).
1658 for (CookieSet::iterator dupes_it = dupes.begin();
1659 dupes_it != dupes.end();
1660 ++dupes_it) {
1661 InternalDeleteCookie(*dupes_it, true,
1662 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE);
1665 DCHECK_EQ(num_duplicates, num_duplicates_found);
1667 return num_duplicates;
1670 // Note: file must be the last scheme.
1671 const char* CookieMonster::kDefaultCookieableSchemes[] =
1672 { "http", "https", "ws", "wss", "file" };
1673 const int CookieMonster::kDefaultCookieableSchemesCount =
1674 arraysize(kDefaultCookieableSchemes);
1676 void CookieMonster::SetDefaultCookieableSchemes() {
1677 // Always disable file scheme unless SetEnableFileScheme(true) is called.
1678 SetCookieableSchemes(kDefaultCookieableSchemes,
1679 kDefaultCookieableSchemesCount - 1);
1682 void CookieMonster::FindCookiesForHostAndDomain(
1683 const GURL& url,
1684 const CookieOptions& options,
1685 bool update_access_time,
1686 std::vector<CanonicalCookie*>* cookies) {
1687 lock_.AssertAcquired();
1689 const Time current_time(CurrentTime());
1691 // Probe to save statistics relatively frequently. We do it here rather
1692 // than in the set path as many websites won't set cookies, and we
1693 // want to collect statistics whenever the browser's being used.
1694 RecordPeriodicStats(current_time);
1696 // Can just dispatch to FindCookiesForKey
1697 const std::string key(GetKey(url.host()));
1698 FindCookiesForKey(key, url, options, current_time,
1699 update_access_time, cookies);
1702 void CookieMonster::FindCookiesForKey(const std::string& key,
1703 const GURL& url,
1704 const CookieOptions& options,
1705 const Time& current,
1706 bool update_access_time,
1707 std::vector<CanonicalCookie*>* cookies) {
1708 lock_.AssertAcquired();
1710 for (CookieMapItPair its = cookies_.equal_range(key);
1711 its.first != its.second; ) {
1712 CookieMap::iterator curit = its.first;
1713 CanonicalCookie* cc = curit->second;
1714 ++its.first;
1716 // If the cookie is expired, delete it.
1717 if (cc->IsExpired(current) && !keep_expired_cookies_) {
1718 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED);
1719 continue;
1722 // Filter out cookies that should not be included for a request to the
1723 // given |url|. HTTP only cookies are filtered depending on the passed
1724 // cookie |options|.
1725 if (!cc->IncludeForRequestURL(url, options))
1726 continue;
1728 // Add this cookie to the set of matching cookies. Update the access
1729 // time if we've been requested to do so.
1730 if (update_access_time) {
1731 InternalUpdateCookieAccessTime(cc, current);
1733 cookies->push_back(cc);
1737 bool CookieMonster::DeleteAnyEquivalentCookie(const std::string& key,
1738 const CanonicalCookie& ecc,
1739 bool skip_httponly,
1740 bool already_expired) {
1741 lock_.AssertAcquired();
1743 bool found_equivalent_cookie = false;
1744 bool skipped_httponly = false;
1745 for (CookieMapItPair its = cookies_.equal_range(key);
1746 its.first != its.second; ) {
1747 CookieMap::iterator curit = its.first;
1748 CanonicalCookie* cc = curit->second;
1749 ++its.first;
1751 if (ecc.IsEquivalent(*cc)) {
1752 // We should never have more than one equivalent cookie, since they should
1753 // overwrite each other.
1754 CHECK(!found_equivalent_cookie) <<
1755 "Duplicate equivalent cookies found, cookie store is corrupted.";
1756 if (skip_httponly && cc->IsHttpOnly()) {
1757 skipped_httponly = true;
1758 } else {
1759 InternalDeleteCookie(curit, true, already_expired ?
1760 DELETE_COOKIE_EXPIRED_OVERWRITE : DELETE_COOKIE_OVERWRITE);
1762 found_equivalent_cookie = true;
1765 return skipped_httponly;
1768 CookieMonster::CookieMap::iterator CookieMonster::InternalInsertCookie(
1769 const std::string& key,
1770 CanonicalCookie* cc,
1771 bool sync_to_store) {
1772 lock_.AssertAcquired();
1774 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() &&
1775 sync_to_store)
1776 store_->AddCookie(*cc);
1777 CookieMap::iterator inserted =
1778 cookies_.insert(CookieMap::value_type(key, cc));
1779 if (delegate_.get()) {
1780 delegate_->OnCookieChanged(
1781 *cc, false, CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT);
1784 return inserted;
1787 bool CookieMonster::SetCookieWithCreationTimeAndOptions(
1788 const GURL& url,
1789 const std::string& cookie_line,
1790 const Time& creation_time_or_null,
1791 const CookieOptions& options) {
1792 lock_.AssertAcquired();
1794 VLOG(kVlogSetCookies) << "SetCookie() line: " << cookie_line;
1796 Time creation_time = creation_time_or_null;
1797 if (creation_time.is_null()) {
1798 creation_time = CurrentTime();
1799 last_time_seen_ = creation_time;
1802 scoped_ptr<CanonicalCookie> cc(
1803 CanonicalCookie::Create(url, cookie_line, creation_time, options));
1805 if (!cc.get()) {
1806 VLOG(kVlogSetCookies) << "WARNING: Failed to allocate CanonicalCookie";
1807 return false;
1809 return SetCanonicalCookie(&cc, creation_time, options);
1812 bool CookieMonster::SetCanonicalCookie(scoped_ptr<CanonicalCookie>* cc,
1813 const Time& creation_time,
1814 const CookieOptions& options) {
1815 const std::string key(GetKey((*cc)->Domain()));
1816 bool already_expired = (*cc)->IsExpired(creation_time);
1817 if (DeleteAnyEquivalentCookie(key, **cc, options.exclude_httponly(),
1818 already_expired)) {
1819 VLOG(kVlogSetCookies) << "SetCookie() not clobbering httponly cookie";
1820 return false;
1823 VLOG(kVlogSetCookies) << "SetCookie() key: " << key << " cc: "
1824 << (*cc)->DebugString();
1826 // Realize that we might be setting an expired cookie, and the only point
1827 // was to delete the cookie which we've already done.
1828 if (!already_expired || keep_expired_cookies_) {
1829 // See InitializeHistograms() for details.
1830 if ((*cc)->IsPersistent()) {
1831 histogram_expiration_duration_minutes_->Add(
1832 ((*cc)->ExpiryDate() - creation_time).InMinutes());
1835 InternalInsertCookie(key, cc->release(), true);
1836 } else {
1837 VLOG(kVlogSetCookies) << "SetCookie() not storing already expired cookie.";
1840 // We assume that hopefully setting a cookie will be less common than
1841 // querying a cookie. Since setting a cookie can put us over our limits,
1842 // make sure that we garbage collect... We can also make the assumption that
1843 // if a cookie was set, in the common case it will be used soon after,
1844 // and we will purge the expired cookies in GetCookies().
1845 GarbageCollect(creation_time, key);
1847 return true;
1850 void CookieMonster::InternalUpdateCookieAccessTime(CanonicalCookie* cc,
1851 const Time& current) {
1852 lock_.AssertAcquired();
1854 // Based off the Mozilla code. When a cookie has been accessed recently,
1855 // don't bother updating its access time again. This reduces the number of
1856 // updates we do during pageload, which in turn reduces the chance our storage
1857 // backend will hit its batch thresholds and be forced to update.
1858 if ((current - cc->LastAccessDate()) < last_access_threshold_)
1859 return;
1861 // See InitializeHistograms() for details.
1862 histogram_between_access_interval_minutes_->Add(
1863 (current - cc->LastAccessDate()).InMinutes());
1865 cc->SetLastAccessDate(current);
1866 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get())
1867 store_->UpdateCookieAccessTime(*cc);
1870 // InternalDeleteCookies must not invalidate iterators other than the one being
1871 // deleted.
1872 void CookieMonster::InternalDeleteCookie(CookieMap::iterator it,
1873 bool sync_to_store,
1874 DeletionCause deletion_cause) {
1875 lock_.AssertAcquired();
1877 // Ideally, this would be asserted up where we define ChangeCauseMapping,
1878 // but DeletionCause's visibility (or lack thereof) forces us to make
1879 // this check here.
1880 COMPILE_ASSERT(arraysize(ChangeCauseMapping) == DELETE_COOKIE_LAST_ENTRY + 1,
1881 ChangeCauseMapping_size_not_eq_DeletionCause_enum_size);
1883 // See InitializeHistograms() for details.
1884 if (deletion_cause != DELETE_COOKIE_DONT_RECORD)
1885 histogram_cookie_deletion_cause_->Add(deletion_cause);
1887 CanonicalCookie* cc = it->second;
1888 VLOG(kVlogSetCookies) << "InternalDeleteCookie() cc: " << cc->DebugString();
1890 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() &&
1891 sync_to_store)
1892 store_->DeleteCookie(*cc);
1893 if (delegate_.get()) {
1894 ChangeCausePair mapping = ChangeCauseMapping[deletion_cause];
1896 if (mapping.notify)
1897 delegate_->OnCookieChanged(*cc, true, mapping.cause);
1899 cookies_.erase(it);
1900 delete cc;
1903 // Domain expiry behavior is unchanged by key/expiry scheme (the
1904 // meaning of the key is different, but that's not visible to this routine).
1905 int CookieMonster::GarbageCollect(const Time& current,
1906 const std::string& key) {
1907 lock_.AssertAcquired();
1909 int num_deleted = 0;
1910 Time safe_date(
1911 Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays));
1913 // Collect garbage for this key, minding cookie priorities.
1914 if (cookies_.count(key) > kDomainMaxCookies) {
1915 VLOG(kVlogGarbageCollection) << "GarbageCollect() key: " << key;
1917 CookieItVector cookie_its;
1918 num_deleted += GarbageCollectExpired(
1919 current, cookies_.equal_range(key), &cookie_its);
1920 if (cookie_its.size() > kDomainMaxCookies) {
1921 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect domain.";
1922 size_t purge_goal =
1923 cookie_its.size() - (kDomainMaxCookies - kDomainPurgeCookies);
1924 DCHECK(purge_goal > kDomainPurgeCookies);
1926 // Boundary iterators into |cookie_its| for different priorities.
1927 CookieItVector::iterator it_bdd[4];
1928 // Intialize |it_bdd| while sorting |cookie_its| by priorities.
1929 // Schematic: [MLLHMHHLMM] => [LLL|MMMM|HHH], with 4 boundaries.
1930 it_bdd[0] = cookie_its.begin();
1931 it_bdd[3] = cookie_its.end();
1932 it_bdd[1] = PartitionCookieByPriority(it_bdd[0], it_bdd[3],
1933 COOKIE_PRIORITY_LOW);
1934 it_bdd[2] = PartitionCookieByPriority(it_bdd[1], it_bdd[3],
1935 COOKIE_PRIORITY_MEDIUM);
1936 size_t quota[3] = {
1937 kDomainCookiesQuotaLow,
1938 kDomainCookiesQuotaMedium,
1939 kDomainCookiesQuotaHigh
1942 // Purge domain cookies in 3 rounds.
1943 // Round 1: consider low-priority cookies only: evict least-recently
1944 // accessed, while protecting quota[0] of these from deletion.
1945 // Round 2: consider {low, medium}-priority cookies, evict least-recently
1946 // accessed, while protecting quota[0] + quota[1].
1947 // Round 3: consider all cookies, evict least-recently accessed.
1948 size_t accumulated_quota = 0;
1949 CookieItVector::iterator it_purge_begin = it_bdd[0];
1950 for (int i = 0; i < 3 && purge_goal > 0; ++i) {
1951 accumulated_quota += quota[i];
1953 size_t num_considered = it_bdd[i + 1] - it_purge_begin;
1954 if (num_considered <= accumulated_quota)
1955 continue;
1957 // Number of cookies that will be purged in this round.
1958 size_t round_goal =
1959 std::min(purge_goal, num_considered - accumulated_quota);
1960 purge_goal -= round_goal;
1962 SortLeastRecentlyAccessed(it_purge_begin, it_bdd[i + 1], round_goal);
1963 // Cookies accessed on or after |safe_date| would have been safe from
1964 // global purge, and we want to keep track of this.
1965 CookieItVector::iterator it_purge_end = it_purge_begin + round_goal;
1966 CookieItVector::iterator it_purge_middle =
1967 LowerBoundAccessDate(it_purge_begin, it_purge_end, safe_date);
1968 // Delete cookies accessed before |safe_date|.
1969 num_deleted += GarbageCollectDeleteRange(
1970 current,
1971 DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE,
1972 it_purge_begin,
1973 it_purge_middle);
1974 // Delete cookies accessed on or after |safe_date|.
1975 num_deleted += GarbageCollectDeleteRange(
1976 current,
1977 DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE,
1978 it_purge_middle,
1979 it_purge_end);
1980 it_purge_begin = it_purge_end;
1982 DCHECK_EQ(0U, purge_goal);
1986 // Collect garbage for everything. With firefox style we want to preserve
1987 // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict.
1988 if (cookies_.size() > kMaxCookies &&
1989 earliest_access_time_ < safe_date) {
1990 VLOG(kVlogGarbageCollection) << "GarbageCollect() everything";
1991 CookieItVector cookie_its;
1992 num_deleted += GarbageCollectExpired(
1993 current, CookieMapItPair(cookies_.begin(), cookies_.end()),
1994 &cookie_its);
1995 if (cookie_its.size() > kMaxCookies) {
1996 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect everything.";
1997 size_t purge_goal = cookie_its.size() - (kMaxCookies - kPurgeCookies);
1998 DCHECK(purge_goal > kPurgeCookies);
1999 // Sorts up to *and including* |cookie_its[purge_goal]|, so
2000 // |earliest_access_time| will be properly assigned even if
2001 // |global_purge_it| == |cookie_its.begin() + purge_goal|.
2002 SortLeastRecentlyAccessed(cookie_its.begin(), cookie_its.end(),
2003 purge_goal);
2004 // Find boundary to cookies older than safe_date.
2005 CookieItVector::iterator global_purge_it =
2006 LowerBoundAccessDate(cookie_its.begin(),
2007 cookie_its.begin() + purge_goal,
2008 safe_date);
2009 // Only delete the old cookies.
2010 num_deleted += GarbageCollectDeleteRange(
2011 current,
2012 DELETE_COOKIE_EVICTED_GLOBAL,
2013 cookie_its.begin(),
2014 global_purge_it);
2015 // Set access day to the oldest cookie that wasn't deleted.
2016 earliest_access_time_ = (*global_purge_it)->second->LastAccessDate();
2020 return num_deleted;
2023 int CookieMonster::GarbageCollectExpired(
2024 const Time& current,
2025 const CookieMapItPair& itpair,
2026 CookieItVector* cookie_its) {
2027 if (keep_expired_cookies_)
2028 return 0;
2030 lock_.AssertAcquired();
2032 int num_deleted = 0;
2033 for (CookieMap::iterator it = itpair.first, end = itpair.second; it != end;) {
2034 CookieMap::iterator curit = it;
2035 ++it;
2037 if (curit->second->IsExpired(current)) {
2038 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED);
2039 ++num_deleted;
2040 } else if (cookie_its) {
2041 cookie_its->push_back(curit);
2045 return num_deleted;
2048 int CookieMonster::GarbageCollectDeleteRange(
2049 const Time& current,
2050 DeletionCause cause,
2051 CookieItVector::iterator it_begin,
2052 CookieItVector::iterator it_end) {
2053 for (CookieItVector::iterator it = it_begin; it != it_end; it++) {
2054 histogram_evicted_last_access_minutes_->Add(
2055 (current - (*it)->second->LastAccessDate()).InMinutes());
2056 InternalDeleteCookie((*it), true, cause);
2058 return it_end - it_begin;
2061 // A wrapper around registry_controlled_domains::GetDomainAndRegistry
2062 // to make clear we're creating a key for our local map. Here and
2063 // in FindCookiesForHostAndDomain() are the only two places where
2064 // we need to conditionalize based on key type.
2066 // Note that this key algorithm explicitly ignores the scheme. This is
2067 // because when we're entering cookies into the map from the backing store,
2068 // we in general won't have the scheme at that point.
2069 // In practical terms, this means that file cookies will be stored
2070 // in the map either by an empty string or by UNC name (and will be
2071 // limited by kMaxCookiesPerHost), and extension cookies will be stored
2072 // based on the single extension id, as the extension id won't have the
2073 // form of a DNS host and hence GetKey() will return it unchanged.
2075 // Arguably the right thing to do here is to make the key
2076 // algorithm dependent on the scheme, and make sure that the scheme is
2077 // available everywhere the key must be obtained (specfically at backing
2078 // store load time). This would require either changing the backing store
2079 // database schema to include the scheme (far more trouble than it's worth), or
2080 // separating out file cookies into their own CookieMonster instance and
2081 // thus restricting each scheme to a single cookie monster (which might
2082 // be worth it, but is still too much trouble to solve what is currently a
2083 // non-problem).
2084 std::string CookieMonster::GetKey(const std::string& domain) const {
2085 std::string effective_domain(
2086 registry_controlled_domains::GetDomainAndRegistry(
2087 domain, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
2088 if (effective_domain.empty())
2089 effective_domain = domain;
2091 if (!effective_domain.empty() && effective_domain[0] == '.')
2092 return effective_domain.substr(1);
2093 return effective_domain;
2096 bool CookieMonster::IsCookieableScheme(const std::string& scheme) {
2097 base::AutoLock autolock(lock_);
2099 return std::find(cookieable_schemes_.begin(), cookieable_schemes_.end(),
2100 scheme) != cookieable_schemes_.end();
2103 bool CookieMonster::HasCookieableScheme(const GURL& url) {
2104 lock_.AssertAcquired();
2106 // Make sure the request is on a cookie-able url scheme.
2107 for (size_t i = 0; i < cookieable_schemes_.size(); ++i) {
2108 // We matched a scheme.
2109 if (url.SchemeIs(cookieable_schemes_[i].c_str())) {
2110 // We've matched a supported scheme.
2111 return true;
2115 // The scheme didn't match any in our whitelist.
2116 VLOG(kVlogPerCookieMonster) << "WARNING: Unsupported cookie scheme: "
2117 << url.scheme();
2118 return false;
2121 // Test to see if stats should be recorded, and record them if so.
2122 // The goal here is to get sampling for the average browser-hour of
2123 // activity. We won't take samples when the web isn't being surfed,
2124 // and when the web is being surfed, we'll take samples about every
2125 // kRecordStatisticsIntervalSeconds.
2126 // last_statistic_record_time_ is initialized to Now() rather than null
2127 // in the constructor so that we won't take statistics right after
2128 // startup, to avoid bias from browsers that are started but not used.
2129 void CookieMonster::RecordPeriodicStats(const base::Time& current_time) {
2130 const base::TimeDelta kRecordStatisticsIntervalTime(
2131 base::TimeDelta::FromSeconds(kRecordStatisticsIntervalSeconds));
2133 // If we've taken statistics recently, return.
2134 if (current_time - last_statistic_record_time_ <=
2135 kRecordStatisticsIntervalTime) {
2136 return;
2139 // See InitializeHistograms() for details.
2140 histogram_count_->Add(cookies_.size());
2142 // More detailed statistics on cookie counts at different granularities.
2143 TimeTicks beginning_of_time(TimeTicks::Now());
2145 for (CookieMap::const_iterator it_key = cookies_.begin();
2146 it_key != cookies_.end(); ) {
2147 const std::string& key(it_key->first);
2149 int key_count = 0;
2150 typedef std::map<std::string, unsigned int> DomainMap;
2151 DomainMap domain_map;
2152 CookieMapItPair its_cookies = cookies_.equal_range(key);
2153 while (its_cookies.first != its_cookies.second) {
2154 key_count++;
2155 const std::string& cookie_domain(its_cookies.first->second->Domain());
2156 domain_map[cookie_domain]++;
2158 its_cookies.first++;
2160 histogram_etldp1_count_->Add(key_count);
2161 histogram_domain_per_etldp1_count_->Add(domain_map.size());
2162 for (DomainMap::const_iterator domain_map_it = domain_map.begin();
2163 domain_map_it != domain_map.end(); domain_map_it++)
2164 histogram_domain_count_->Add(domain_map_it->second);
2166 it_key = its_cookies.second;
2169 VLOG(kVlogPeriodic)
2170 << "Time for recording cookie stats (us): "
2171 << (TimeTicks::Now() - beginning_of_time).InMicroseconds();
2173 last_statistic_record_time_ = current_time;
2176 // Initialize all histogram counter variables used in this class.
2178 // Normal histogram usage involves using the macros defined in
2179 // histogram.h, which automatically takes care of declaring these
2180 // variables (as statics), initializing them, and accumulating into
2181 // them, all from a single entry point. Unfortunately, that solution
2182 // doesn't work for the CookieMonster, as it's vulnerable to races between
2183 // separate threads executing the same functions and hence initializing the
2184 // same static variables. There isn't a race danger in the histogram
2185 // accumulation calls; they are written to be resilient to simultaneous
2186 // calls from multiple threads.
2188 // The solution taken here is to have per-CookieMonster instance
2189 // variables that are constructed during CookieMonster construction.
2190 // Note that these variables refer to the same underlying histogram,
2191 // so we still race (but safely) with other CookieMonster instances
2192 // for accumulation.
2194 // To do this we've expanded out the individual histogram macros calls,
2195 // with declarations of the variables in the class decl, initialization here
2196 // (done from the class constructor) and direct calls to the accumulation
2197 // methods where needed. The specific histogram macro calls on which the
2198 // initialization is based are included in comments below.
2199 void CookieMonster::InitializeHistograms() {
2200 // From UMA_HISTOGRAM_CUSTOM_COUNTS
2201 histogram_expiration_duration_minutes_ = base::Histogram::FactoryGet(
2202 "Cookie.ExpirationDurationMinutes",
2203 1, kMinutesInTenYears, 50,
2204 base::Histogram::kUmaTargetedHistogramFlag);
2205 histogram_between_access_interval_minutes_ = base::Histogram::FactoryGet(
2206 "Cookie.BetweenAccessIntervalMinutes",
2207 1, kMinutesInTenYears, 50,
2208 base::Histogram::kUmaTargetedHistogramFlag);
2209 histogram_evicted_last_access_minutes_ = base::Histogram::FactoryGet(
2210 "Cookie.EvictedLastAccessMinutes",
2211 1, kMinutesInTenYears, 50,
2212 base::Histogram::kUmaTargetedHistogramFlag);
2213 histogram_count_ = base::Histogram::FactoryGet(
2214 "Cookie.Count", 1, 4000, 50,
2215 base::Histogram::kUmaTargetedHistogramFlag);
2216 histogram_domain_count_ = base::Histogram::FactoryGet(
2217 "Cookie.DomainCount", 1, 4000, 50,
2218 base::Histogram::kUmaTargetedHistogramFlag);
2219 histogram_etldp1_count_ = base::Histogram::FactoryGet(
2220 "Cookie.Etldp1Count", 1, 4000, 50,
2221 base::Histogram::kUmaTargetedHistogramFlag);
2222 histogram_domain_per_etldp1_count_ = base::Histogram::FactoryGet(
2223 "Cookie.DomainPerEtldp1Count", 1, 4000, 50,
2224 base::Histogram::kUmaTargetedHistogramFlag);
2226 // From UMA_HISTOGRAM_COUNTS_10000 & UMA_HISTOGRAM_CUSTOM_COUNTS
2227 histogram_number_duplicate_db_cookies_ = base::Histogram::FactoryGet(
2228 "Net.NumDuplicateCookiesInDb", 1, 10000, 50,
2229 base::Histogram::kUmaTargetedHistogramFlag);
2231 // From UMA_HISTOGRAM_ENUMERATION
2232 histogram_cookie_deletion_cause_ = base::LinearHistogram::FactoryGet(
2233 "Cookie.DeletionCause", 1,
2234 DELETE_COOKIE_LAST_ENTRY - 1, DELETE_COOKIE_LAST_ENTRY,
2235 base::Histogram::kUmaTargetedHistogramFlag);
2237 // From UMA_HISTOGRAM_{CUSTOM_,}TIMES
2238 histogram_time_get_ = base::Histogram::FactoryTimeGet("Cookie.TimeGet",
2239 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
2240 50, base::Histogram::kUmaTargetedHistogramFlag);
2241 histogram_time_blocked_on_load_ = base::Histogram::FactoryTimeGet(
2242 "Cookie.TimeBlockedOnLoad",
2243 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
2244 50, base::Histogram::kUmaTargetedHistogramFlag);
2248 // The system resolution is not high enough, so we can have multiple
2249 // set cookies that result in the same system time. When this happens, we
2250 // increment by one Time unit. Let's hope computers don't get too fast.
2251 Time CookieMonster::CurrentTime() {
2252 return std::max(Time::Now(),
2253 Time::FromInternalValue(last_time_seen_.ToInternalValue() + 1));
2256 bool CookieMonster::CopyCookiesForKeyToOtherCookieMonster(
2257 std::string key,
2258 CookieMonster* other) {
2259 ScopedVector<CanonicalCookie> duplicated_cookies;
2262 base::AutoLock autolock(lock_);
2263 DCHECK(other);
2264 if (!loaded_)
2265 return false;
2267 for (CookieMapItPair its = cookies_.equal_range(key);
2268 its.first != its.second;
2269 ++its.first) {
2270 CookieMap::iterator curit = its.first;
2271 CanonicalCookie* cc = curit->second;
2273 duplicated_cookies.push_back(cc->Duplicate());
2278 base::AutoLock autolock(other->lock_);
2279 if (!other->loaded_)
2280 return false;
2282 // There must not exist any entries for the key to be copied in |other|.
2283 CookieMapItPair its = other->cookies_.equal_range(key);
2284 if (its.first != its.second)
2285 return false;
2287 // Store the copied cookies in |other|.
2288 for (ScopedVector<CanonicalCookie>::const_iterator it =
2289 duplicated_cookies.begin();
2290 it != duplicated_cookies.end();
2291 ++it) {
2292 other->InternalInsertCookie(key, *it, true);
2295 // Since the cookies are owned by |other| now, weak clear must be used.
2296 duplicated_cookies.weak_clear();
2299 return true;
2302 bool CookieMonster::loaded() {
2303 base::AutoLock autolock(lock_);
2304 return loaded_;
2307 } // namespace net