Roll src/third_party/skia e1a828c:3e3b58d
[chromium-blink-merge.git] / net / cookies / cookie_monster.cc
blobf6895537b8c689d7d2b6f939d70268b89e37d2a2
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"
67 using base::Time;
68 using base::TimeDelta;
69 using base::TimeTicks;
71 // In steady state, most cookie requests can be satisfied by the in memory
72 // cookie monster store. However, if a request comes in during the initial
73 // cookie load, it must be delayed until that load completes. That is done by
74 // queueing it on CookieMonster::tasks_pending_ and running it when notification
75 // of cookie load completion is received via CookieMonster::OnLoaded. This
76 // callback is passed to the persistent store from CookieMonster::InitStore(),
77 // which is called on the first operation invoked on the CookieMonster.
79 // On the browser critical paths (e.g. for loading initial web pages in a
80 // session restore) it may take too long to wait for the full load. If a cookie
81 // request is for a specific URL, DoCookieTaskForURL is called, which triggers a
82 // priority load if the key is not loaded yet by calling PersistentCookieStore
83 // :: LoadCookiesForKey. The request is queued in
84 // CookieMonster::tasks_pending_for_key_ and executed upon receiving
85 // notification of key load completion via CookieMonster::OnKeyLoaded(). If
86 // multiple requests for the same eTLD+1 are received before key load
87 // completion, only the first request calls
88 // PersistentCookieStore::LoadCookiesForKey, all subsequent requests are queued
89 // in CookieMonster::tasks_pending_for_key_ and executed upon receiving
90 // notification of key load completion triggered by the first request for the
91 // same eTLD+1.
93 static const int kMinutesInTenYears = 10 * 365 * 24 * 60;
95 namespace net {
97 // See comments at declaration of these variables in cookie_monster.h
98 // for details.
99 const size_t CookieMonster::kDomainMaxCookies = 180;
100 const size_t CookieMonster::kDomainPurgeCookies = 30;
101 const size_t CookieMonster::kMaxCookies = 3300;
102 const size_t CookieMonster::kPurgeCookies = 300;
104 const size_t CookieMonster::kDomainCookiesQuotaLow = 30;
105 const size_t CookieMonster::kDomainCookiesQuotaMedium = 50;
106 const size_t CookieMonster::kDomainCookiesQuotaHigh =
107 kDomainMaxCookies - kDomainPurgeCookies
108 - kDomainCookiesQuotaLow - kDomainCookiesQuotaMedium;
110 const int CookieMonster::kSafeFromGlobalPurgeDays = 30;
112 namespace {
114 bool ContainsControlCharacter(const std::string& s) {
115 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {
116 if ((*i >= 0) && (*i <= 31))
117 return true;
120 return false;
123 typedef std::vector<CanonicalCookie*> CanonicalCookieVector;
125 // Default minimum delay after updating a cookie's LastAccessDate before we
126 // will update it again.
127 const int kDefaultAccessUpdateThresholdSeconds = 60;
129 // Comparator to sort cookies from highest creation date to lowest
130 // creation date.
131 struct OrderByCreationTimeDesc {
132 bool operator()(const CookieMonster::CookieMap::iterator& a,
133 const CookieMonster::CookieMap::iterator& b) const {
134 return a->second->CreationDate() > b->second->CreationDate();
138 // Constants for use in VLOG
139 const int kVlogPerCookieMonster = 1;
140 const int kVlogPeriodic = 3;
141 const int kVlogGarbageCollection = 5;
142 const int kVlogSetCookies = 7;
143 const int kVlogGetCookies = 9;
145 // Mozilla sorts on the path length (longest first), and then it
146 // sorts by creation time (oldest first).
147 // The RFC says the sort order for the domain attribute is undefined.
148 bool CookieSorter(CanonicalCookie* cc1, CanonicalCookie* cc2) {
149 if (cc1->Path().length() == cc2->Path().length())
150 return cc1->CreationDate() < cc2->CreationDate();
151 return cc1->Path().length() > cc2->Path().length();
154 bool LRACookieSorter(const CookieMonster::CookieMap::iterator& it1,
155 const CookieMonster::CookieMap::iterator& it2) {
156 // Cookies accessed less recently should be deleted first.
157 if (it1->second->LastAccessDate() != it2->second->LastAccessDate())
158 return it1->second->LastAccessDate() < it2->second->LastAccessDate();
160 // In rare cases we might have two cookies with identical last access times.
161 // To preserve the stability of the sort, in these cases prefer to delete
162 // older cookies over newer ones. CreationDate() is guaranteed to be unique.
163 return it1->second->CreationDate() < it2->second->CreationDate();
166 // Our strategy to find duplicates is:
167 // (1) Build a map from (cookiename, cookiepath) to
168 // {list of cookies with this signature, sorted by creation time}.
169 // (2) For each list with more than 1 entry, keep the cookie having the
170 // most recent creation time, and delete the others.
172 // Two cookies are considered equivalent if they have the same domain,
173 // name, and path.
174 struct CookieSignature {
175 public:
176 CookieSignature(const std::string& name,
177 const std::string& domain,
178 const std::string& path)
179 : name(name), domain(domain), path(path) {
182 // To be a key for a map this class needs to be assignable, copyable,
183 // and have an operator<. The default assignment operator
184 // and copy constructor are exactly what we want.
186 bool operator<(const CookieSignature& cs) const {
187 // Name compare dominates, then domain, then path.
188 int diff = name.compare(cs.name);
189 if (diff != 0)
190 return diff < 0;
192 diff = domain.compare(cs.domain);
193 if (diff != 0)
194 return diff < 0;
196 return path.compare(cs.path) < 0;
199 std::string name;
200 std::string domain;
201 std::string path;
204 // For a CookieItVector iterator range [|it_begin|, |it_end|),
205 // sorts the first |num_sort| + 1 elements by LastAccessDate().
206 // The + 1 element exists so for any interval of length <= |num_sort| starting
207 // from |cookies_its_begin|, a LastAccessDate() bound can be found.
208 void SortLeastRecentlyAccessed(
209 CookieMonster::CookieItVector::iterator it_begin,
210 CookieMonster::CookieItVector::iterator it_end,
211 size_t num_sort) {
212 DCHECK_LT(static_cast<int>(num_sort), it_end - it_begin);
213 std::partial_sort(it_begin, it_begin + num_sort + 1, it_end, LRACookieSorter);
216 // Predicate to support PartitionCookieByPriority().
217 struct CookiePriorityEqualsTo
218 : std::unary_function<const CookieMonster::CookieMap::iterator, bool> {
219 explicit CookiePriorityEqualsTo(CookiePriority priority)
220 : priority_(priority) {}
222 bool operator()(const CookieMonster::CookieMap::iterator it) const {
223 return it->second->Priority() == priority_;
226 const CookiePriority priority_;
229 // For a CookieItVector iterator range [|it_begin|, |it_end|),
230 // moves all cookies with a given |priority| to the beginning of the list.
231 // Returns: An iterator in [it_begin, it_end) to the first element with
232 // priority != |priority|, or |it_end| if all have priority == |priority|.
233 CookieMonster::CookieItVector::iterator PartitionCookieByPriority(
234 CookieMonster::CookieItVector::iterator it_begin,
235 CookieMonster::CookieItVector::iterator it_end,
236 CookiePriority priority) {
237 return std::partition(it_begin, it_end, CookiePriorityEqualsTo(priority));
240 bool LowerBoundAccessDateComparator(
241 const CookieMonster::CookieMap::iterator it, const Time& access_date) {
242 return it->second->LastAccessDate() < access_date;
245 // For a CookieItVector iterator range [|it_begin|, |it_end|)
246 // from a CookieItVector sorted by LastAccessDate(), returns the
247 // first iterator with access date >= |access_date|, or cookie_its_end if this
248 // holds for all.
249 CookieMonster::CookieItVector::iterator LowerBoundAccessDate(
250 const CookieMonster::CookieItVector::iterator its_begin,
251 const CookieMonster::CookieItVector::iterator its_end,
252 const Time& access_date) {
253 return std::lower_bound(its_begin, its_end, access_date,
254 LowerBoundAccessDateComparator);
257 // Mapping between DeletionCause and CookieMonsterDelegate::ChangeCause; the
258 // mapping also provides a boolean that specifies whether or not an
259 // OnCookieChanged notification ought to be generated.
260 typedef struct ChangeCausePair_struct {
261 CookieMonsterDelegate::ChangeCause cause;
262 bool notify;
263 } ChangeCausePair;
264 ChangeCausePair ChangeCauseMapping[] = {
265 // DELETE_COOKIE_EXPLICIT
266 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, true },
267 // DELETE_COOKIE_OVERWRITE
268 { CookieMonsterDelegate::CHANGE_COOKIE_OVERWRITE, true },
269 // DELETE_COOKIE_EXPIRED
270 { CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED, true },
271 // DELETE_COOKIE_EVICTED
272 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
273 // DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE
274 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false },
275 // DELETE_COOKIE_DONT_RECORD
276 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false },
277 // DELETE_COOKIE_EVICTED_DOMAIN
278 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
279 // DELETE_COOKIE_EVICTED_GLOBAL
280 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
281 // DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
282 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
283 // DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
284 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
285 // DELETE_COOKIE_EXPIRED_OVERWRITE
286 { CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED_OVERWRITE, true },
287 // DELETE_COOKIE_CONTROL_CHAR
288 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true},
289 // DELETE_COOKIE_LAST_ENTRY
290 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false }
293 std::string BuildCookieLine(const CanonicalCookieVector& cookies) {
294 std::string cookie_line;
295 for (CanonicalCookieVector::const_iterator it = cookies.begin();
296 it != cookies.end(); ++it) {
297 if (it != cookies.begin())
298 cookie_line += "; ";
299 // In Mozilla if you set a cookie like AAAA, it will have an empty token
300 // and a value of AAAA. When it sends the cookie back, it will send AAAA,
301 // so we need to avoid sending =AAAA for a blank token value.
302 if (!(*it)->Name().empty())
303 cookie_line += (*it)->Name() + "=";
304 cookie_line += (*it)->Value();
306 return cookie_line;
309 void RunAsync(scoped_refptr<base::TaskRunner> proxy,
310 const CookieStore::CookieChangedCallback& callback,
311 const CanonicalCookie& cookie,
312 bool removed) {
313 proxy->PostTask(FROM_HERE, base::Bind(callback, cookie, removed));
316 } // namespace
318 CookieMonster::CookieMonster(PersistentCookieStore* store,
319 CookieMonsterDelegate* delegate)
320 : initialized_(false),
321 loaded_(store == NULL),
322 store_(store),
323 last_access_threshold_(
324 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)),
325 delegate_(delegate),
326 last_statistic_record_time_(Time::Now()),
327 keep_expired_cookies_(false),
328 persist_session_cookies_(false) {
329 InitializeHistograms();
330 SetDefaultCookieableSchemes();
333 CookieMonster::CookieMonster(PersistentCookieStore* store,
334 CookieMonsterDelegate* delegate,
335 int last_access_threshold_milliseconds)
336 : initialized_(false),
337 loaded_(store == NULL),
338 store_(store),
339 last_access_threshold_(base::TimeDelta::FromMilliseconds(
340 last_access_threshold_milliseconds)),
341 delegate_(delegate),
342 last_statistic_record_time_(base::Time::Now()),
343 keep_expired_cookies_(false),
344 persist_session_cookies_(false) {
345 InitializeHistograms();
346 SetDefaultCookieableSchemes();
350 // Task classes for queueing the coming request.
352 class CookieMonster::CookieMonsterTask
353 : public base::RefCountedThreadSafe<CookieMonsterTask> {
354 public:
355 // Runs the task and invokes the client callback on the thread that
356 // originally constructed the task.
357 virtual void Run() = 0;
359 protected:
360 explicit CookieMonsterTask(CookieMonster* cookie_monster);
361 virtual ~CookieMonsterTask();
363 // Invokes the callback immediately, if the current thread is the one
364 // that originated the task, or queues the callback for execution on the
365 // appropriate thread. Maintains a reference to this CookieMonsterTask
366 // instance until the callback completes.
367 void InvokeCallback(base::Closure callback);
369 CookieMonster* cookie_monster() {
370 return cookie_monster_;
373 private:
374 friend class base::RefCountedThreadSafe<CookieMonsterTask>;
376 CookieMonster* cookie_monster_;
377 scoped_refptr<base::MessageLoopProxy> thread_;
379 DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask);
382 CookieMonster::CookieMonsterTask::CookieMonsterTask(
383 CookieMonster* cookie_monster)
384 : cookie_monster_(cookie_monster),
385 thread_(base::MessageLoopProxy::current()) {
388 CookieMonster::CookieMonsterTask::~CookieMonsterTask() {}
390 // Unfortunately, one cannot re-bind a Callback with parameters into a closure.
391 // Therefore, the closure passed to InvokeCallback is a clumsy binding of
392 // Callback::Run on a wrapped Callback instance. Since Callback is not
393 // reference counted, we bind to an instance that is a member of the
394 // CookieMonsterTask subclass. Then, we cannot simply post the callback to a
395 // message loop because the underlying instance may be destroyed (along with the
396 // CookieMonsterTask instance) in the interim. Therefore, we post a callback
397 // bound to the CookieMonsterTask, which *is* reference counted (thus preventing
398 // destruction of the original callback), and which invokes the closure (which
399 // invokes the original callback with the returned data).
400 void CookieMonster::CookieMonsterTask::InvokeCallback(base::Closure callback) {
401 if (thread_->BelongsToCurrentThread()) {
402 callback.Run();
403 } else {
404 thread_->PostTask(FROM_HERE, base::Bind(
405 &CookieMonsterTask::InvokeCallback, this, callback));
409 // Task class for SetCookieWithDetails call.
410 class CookieMonster::SetCookieWithDetailsTask : public CookieMonsterTask {
411 public:
412 SetCookieWithDetailsTask(CookieMonster* cookie_monster,
413 const GURL& url,
414 const std::string& name,
415 const std::string& value,
416 const std::string& domain,
417 const std::string& path,
418 const base::Time& expiration_time,
419 bool secure,
420 bool http_only,
421 CookiePriority priority,
422 const SetCookiesCallback& callback)
423 : CookieMonsterTask(cookie_monster),
424 url_(url),
425 name_(name),
426 value_(value),
427 domain_(domain),
428 path_(path),
429 expiration_time_(expiration_time),
430 secure_(secure),
431 http_only_(http_only),
432 priority_(priority),
433 callback_(callback) {
436 // CookieMonsterTask:
437 void Run() override;
439 protected:
440 ~SetCookieWithDetailsTask() override {}
442 private:
443 GURL url_;
444 std::string name_;
445 std::string value_;
446 std::string domain_;
447 std::string path_;
448 base::Time expiration_time_;
449 bool secure_;
450 bool http_only_;
451 CookiePriority priority_;
452 SetCookiesCallback callback_;
454 DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask);
457 void CookieMonster::SetCookieWithDetailsTask::Run() {
458 bool success = this->cookie_monster()->
459 SetCookieWithDetails(url_, name_, value_, domain_, path_,
460 expiration_time_, secure_, http_only_, priority_);
461 if (!callback_.is_null()) {
462 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run,
463 base::Unretained(&callback_), success));
467 // Task class for GetAllCookies call.
468 class CookieMonster::GetAllCookiesTask : public CookieMonsterTask {
469 public:
470 GetAllCookiesTask(CookieMonster* cookie_monster,
471 const GetCookieListCallback& callback)
472 : CookieMonsterTask(cookie_monster),
473 callback_(callback) {
476 // CookieMonsterTask
477 void Run() override;
479 protected:
480 ~GetAllCookiesTask() override {}
482 private:
483 GetCookieListCallback callback_;
485 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask);
488 void CookieMonster::GetAllCookiesTask::Run() {
489 if (!callback_.is_null()) {
490 CookieList cookies = this->cookie_monster()->GetAllCookies();
491 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run,
492 base::Unretained(&callback_), cookies));
496 // Task class for GetAllCookiesForURLWithOptions call.
497 class CookieMonster::GetAllCookiesForURLWithOptionsTask
498 : public CookieMonsterTask {
499 public:
500 GetAllCookiesForURLWithOptionsTask(
501 CookieMonster* cookie_monster,
502 const GURL& url,
503 const CookieOptions& options,
504 const GetCookieListCallback& callback)
505 : CookieMonsterTask(cookie_monster),
506 url_(url),
507 options_(options),
508 callback_(callback) {
511 // CookieMonsterTask:
512 void Run() override;
514 protected:
515 ~GetAllCookiesForURLWithOptionsTask() override {}
517 private:
518 GURL url_;
519 CookieOptions options_;
520 GetCookieListCallback callback_;
522 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesForURLWithOptionsTask);
525 void CookieMonster::GetAllCookiesForURLWithOptionsTask::Run() {
526 if (!callback_.is_null()) {
527 CookieList cookies = this->cookie_monster()->
528 GetAllCookiesForURLWithOptions(url_, options_);
529 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run,
530 base::Unretained(&callback_), cookies));
534 template <typename Result> struct CallbackType {
535 typedef base::Callback<void(Result)> Type;
538 template <> struct CallbackType<void> {
539 typedef base::Closure Type;
542 // Base task class for Delete*Task.
543 template <typename Result>
544 class CookieMonster::DeleteTask : public CookieMonsterTask {
545 public:
546 DeleteTask(CookieMonster* cookie_monster,
547 const typename CallbackType<Result>::Type& callback)
548 : CookieMonsterTask(cookie_monster),
549 callback_(callback) {
552 // CookieMonsterTask:
553 virtual void Run() override;
555 protected:
556 ~DeleteTask() override;
558 private:
559 // Runs the delete task and returns a result.
560 virtual Result RunDeleteTask() = 0;
561 base::Closure RunDeleteTaskAndBindCallback();
562 void FlushDone(const base::Closure& callback);
564 typename CallbackType<Result>::Type callback_;
566 DISALLOW_COPY_AND_ASSIGN(DeleteTask);
569 template <typename Result>
570 CookieMonster::DeleteTask<Result>::~DeleteTask() {
573 template <typename Result>
574 base::Closure
575 CookieMonster::DeleteTask<Result>::RunDeleteTaskAndBindCallback() {
576 Result result = RunDeleteTask();
577 if (callback_.is_null())
578 return base::Closure();
579 return base::Bind(callback_, result);
582 template <>
583 base::Closure CookieMonster::DeleteTask<void>::RunDeleteTaskAndBindCallback() {
584 RunDeleteTask();
585 return callback_;
588 template <typename Result>
589 void CookieMonster::DeleteTask<Result>::Run() {
590 this->cookie_monster()->FlushStore(
591 base::Bind(&DeleteTask<Result>::FlushDone, this,
592 RunDeleteTaskAndBindCallback()));
595 template <typename Result>
596 void CookieMonster::DeleteTask<Result>::FlushDone(
597 const base::Closure& callback) {
598 if (!callback.is_null()) {
599 this->InvokeCallback(callback);
603 // Task class for DeleteAll call.
604 class CookieMonster::DeleteAllTask : public DeleteTask<int> {
605 public:
606 DeleteAllTask(CookieMonster* cookie_monster,
607 const DeleteCallback& callback)
608 : DeleteTask<int>(cookie_monster, callback) {
611 // DeleteTask:
612 int RunDeleteTask() override;
614 protected:
615 ~DeleteAllTask() override {}
617 private:
618 DISALLOW_COPY_AND_ASSIGN(DeleteAllTask);
621 int CookieMonster::DeleteAllTask::RunDeleteTask() {
622 return this->cookie_monster()->DeleteAll(true);
625 // Task class for DeleteAllCreatedBetween call.
626 class CookieMonster::DeleteAllCreatedBetweenTask : public DeleteTask<int> {
627 public:
628 DeleteAllCreatedBetweenTask(CookieMonster* cookie_monster,
629 const Time& delete_begin,
630 const Time& delete_end,
631 const DeleteCallback& callback)
632 : DeleteTask<int>(cookie_monster, callback),
633 delete_begin_(delete_begin),
634 delete_end_(delete_end) {
637 // DeleteTask:
638 int RunDeleteTask() override;
640 protected:
641 ~DeleteAllCreatedBetweenTask() override {}
643 private:
644 Time delete_begin_;
645 Time delete_end_;
647 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask);
650 int CookieMonster::DeleteAllCreatedBetweenTask::RunDeleteTask() {
651 return this->cookie_monster()->
652 DeleteAllCreatedBetween(delete_begin_, delete_end_);
655 // Task class for DeleteAllForHost call.
656 class CookieMonster::DeleteAllForHostTask : public DeleteTask<int> {
657 public:
658 DeleteAllForHostTask(CookieMonster* cookie_monster,
659 const GURL& url,
660 const DeleteCallback& callback)
661 : DeleteTask<int>(cookie_monster, callback),
662 url_(url) {
665 // DeleteTask:
666 int RunDeleteTask() override;
668 protected:
669 ~DeleteAllForHostTask() override {}
671 private:
672 GURL url_;
674 DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask);
677 int CookieMonster::DeleteAllForHostTask::RunDeleteTask() {
678 return this->cookie_monster()->DeleteAllForHost(url_);
681 // Task class for DeleteAllCreatedBetweenForHost call.
682 class CookieMonster::DeleteAllCreatedBetweenForHostTask
683 : public DeleteTask<int> {
684 public:
685 DeleteAllCreatedBetweenForHostTask(
686 CookieMonster* cookie_monster,
687 Time delete_begin,
688 Time delete_end,
689 const GURL& url,
690 const DeleteCallback& callback)
691 : DeleteTask<int>(cookie_monster, callback),
692 delete_begin_(delete_begin),
693 delete_end_(delete_end),
694 url_(url) {
697 // DeleteTask:
698 int RunDeleteTask() override;
700 protected:
701 ~DeleteAllCreatedBetweenForHostTask() override {}
703 private:
704 Time delete_begin_;
705 Time delete_end_;
706 GURL url_;
708 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenForHostTask);
711 int CookieMonster::DeleteAllCreatedBetweenForHostTask::RunDeleteTask() {
712 return this->cookie_monster()->DeleteAllCreatedBetweenForHost(
713 delete_begin_, delete_end_, url_);
716 // Task class for DeleteCanonicalCookie call.
717 class CookieMonster::DeleteCanonicalCookieTask : public DeleteTask<bool> {
718 public:
719 DeleteCanonicalCookieTask(CookieMonster* cookie_monster,
720 const CanonicalCookie& cookie,
721 const DeleteCookieCallback& callback)
722 : DeleteTask<bool>(cookie_monster, callback),
723 cookie_(cookie) {
726 // DeleteTask:
727 bool RunDeleteTask() override;
729 protected:
730 ~DeleteCanonicalCookieTask() override {}
732 private:
733 CanonicalCookie cookie_;
735 DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask);
738 bool CookieMonster::DeleteCanonicalCookieTask::RunDeleteTask() {
739 return this->cookie_monster()->DeleteCanonicalCookie(cookie_);
742 // Task class for SetCookieWithOptions call.
743 class CookieMonster::SetCookieWithOptionsTask : public CookieMonsterTask {
744 public:
745 SetCookieWithOptionsTask(CookieMonster* cookie_monster,
746 const GURL& url,
747 const std::string& cookie_line,
748 const CookieOptions& options,
749 const SetCookiesCallback& callback)
750 : CookieMonsterTask(cookie_monster),
751 url_(url),
752 cookie_line_(cookie_line),
753 options_(options),
754 callback_(callback) {
757 // CookieMonsterTask:
758 void Run() override;
760 protected:
761 ~SetCookieWithOptionsTask() override {}
763 private:
764 GURL url_;
765 std::string cookie_line_;
766 CookieOptions options_;
767 SetCookiesCallback callback_;
769 DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask);
772 void CookieMonster::SetCookieWithOptionsTask::Run() {
773 bool result = this->cookie_monster()->
774 SetCookieWithOptions(url_, cookie_line_, options_);
775 if (!callback_.is_null()) {
776 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run,
777 base::Unretained(&callback_), result));
781 // Task class for GetCookiesWithOptions call.
782 class CookieMonster::GetCookiesWithOptionsTask : public CookieMonsterTask {
783 public:
784 GetCookiesWithOptionsTask(CookieMonster* cookie_monster,
785 const GURL& url,
786 const CookieOptions& options,
787 const GetCookiesCallback& callback)
788 : CookieMonsterTask(cookie_monster),
789 url_(url),
790 options_(options),
791 callback_(callback) {
794 // CookieMonsterTask:
795 void Run() override;
797 protected:
798 ~GetCookiesWithOptionsTask() override {}
800 private:
801 GURL url_;
802 CookieOptions options_;
803 GetCookiesCallback callback_;
805 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask);
808 void CookieMonster::GetCookiesWithOptionsTask::Run() {
809 std::string cookie = this->cookie_monster()->
810 GetCookiesWithOptions(url_, options_);
811 if (!callback_.is_null()) {
812 this->InvokeCallback(base::Bind(&GetCookiesCallback::Run,
813 base::Unretained(&callback_), cookie));
817 // Task class for DeleteCookie call.
818 class CookieMonster::DeleteCookieTask : public DeleteTask<void> {
819 public:
820 DeleteCookieTask(CookieMonster* cookie_monster,
821 const GURL& url,
822 const std::string& cookie_name,
823 const base::Closure& callback)
824 : DeleteTask<void>(cookie_monster, callback),
825 url_(url),
826 cookie_name_(cookie_name) {
829 // DeleteTask:
830 void RunDeleteTask() override;
832 protected:
833 ~DeleteCookieTask() override {}
835 private:
836 GURL url_;
837 std::string cookie_name_;
839 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
842 void CookieMonster::DeleteCookieTask::RunDeleteTask() {
843 this->cookie_monster()->DeleteCookie(url_, cookie_name_);
846 // Task class for DeleteSessionCookies call.
847 class CookieMonster::DeleteSessionCookiesTask : public DeleteTask<int> {
848 public:
849 DeleteSessionCookiesTask(CookieMonster* cookie_monster,
850 const DeleteCallback& callback)
851 : DeleteTask<int>(cookie_monster, callback) {
854 // DeleteTask:
855 int RunDeleteTask() override;
857 protected:
858 ~DeleteSessionCookiesTask() override {}
860 private:
861 DISALLOW_COPY_AND_ASSIGN(DeleteSessionCookiesTask);
864 int CookieMonster::DeleteSessionCookiesTask::RunDeleteTask() {
865 return this->cookie_monster()->DeleteSessionCookies();
868 // Task class for HasCookiesForETLDP1Task call.
869 class CookieMonster::HasCookiesForETLDP1Task : public CookieMonsterTask {
870 public:
871 HasCookiesForETLDP1Task(
872 CookieMonster* cookie_monster,
873 const std::string& etldp1,
874 const HasCookiesForETLDP1Callback& callback)
875 : CookieMonsterTask(cookie_monster),
876 etldp1_(etldp1),
877 callback_(callback) {
880 // CookieMonsterTask:
881 void Run() override;
883 protected:
884 ~HasCookiesForETLDP1Task() override {}
886 private:
887 std::string etldp1_;
888 HasCookiesForETLDP1Callback callback_;
890 DISALLOW_COPY_AND_ASSIGN(HasCookiesForETLDP1Task);
893 void CookieMonster::HasCookiesForETLDP1Task::Run() {
894 bool result = this->cookie_monster()->HasCookiesForETLDP1(etldp1_);
895 if (!callback_.is_null()) {
896 this->InvokeCallback(
897 base::Bind(&HasCookiesForETLDP1Callback::Run,
898 base::Unretained(&callback_), result));
902 // Asynchronous CookieMonster API
904 void CookieMonster::SetCookieWithDetailsAsync(
905 const GURL& url,
906 const std::string& name,
907 const std::string& value,
908 const std::string& domain,
909 const std::string& path,
910 const Time& expiration_time,
911 bool secure,
912 bool http_only,
913 CookiePriority priority,
914 const SetCookiesCallback& callback) {
915 scoped_refptr<SetCookieWithDetailsTask> task =
916 new SetCookieWithDetailsTask(this, url, name, value, domain, path,
917 expiration_time, secure, http_only, priority,
918 callback);
919 DoCookieTaskForURL(task, url);
922 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) {
923 scoped_refptr<GetAllCookiesTask> task =
924 new GetAllCookiesTask(this, callback);
926 DoCookieTask(task);
930 void CookieMonster::GetAllCookiesForURLWithOptionsAsync(
931 const GURL& url,
932 const CookieOptions& options,
933 const GetCookieListCallback& callback) {
934 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
935 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
937 DoCookieTaskForURL(task, url);
940 void CookieMonster::GetAllCookiesForURLAsync(
941 const GURL& url, const GetCookieListCallback& callback) {
942 CookieOptions options;
943 options.set_include_httponly();
944 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
945 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
947 DoCookieTaskForURL(task, url);
950 void CookieMonster::HasCookiesForETLDP1Async(
951 const std::string& etldp1,
952 const HasCookiesForETLDP1Callback& callback) {
953 scoped_refptr<HasCookiesForETLDP1Task> task =
954 new HasCookiesForETLDP1Task(this, etldp1, callback);
956 DoCookieTaskForURL(task, GURL("http://" + etldp1));
959 void CookieMonster::DeleteAllAsync(const DeleteCallback& callback) {
960 scoped_refptr<DeleteAllTask> task =
961 new DeleteAllTask(this, callback);
963 DoCookieTask(task);
966 void CookieMonster::DeleteAllCreatedBetweenAsync(
967 const Time& delete_begin, const Time& delete_end,
968 const DeleteCallback& callback) {
969 scoped_refptr<DeleteAllCreatedBetweenTask> task =
970 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end,
971 callback);
973 DoCookieTask(task);
976 void CookieMonster::DeleteAllCreatedBetweenForHostAsync(
977 const Time delete_begin,
978 const Time delete_end,
979 const GURL& url,
980 const DeleteCallback& callback) {
981 scoped_refptr<DeleteAllCreatedBetweenForHostTask> task =
982 new DeleteAllCreatedBetweenForHostTask(
983 this, delete_begin, delete_end, url, callback);
985 DoCookieTaskForURL(task, url);
988 void CookieMonster::DeleteAllForHostAsync(
989 const GURL& url, const DeleteCallback& callback) {
990 scoped_refptr<DeleteAllForHostTask> task =
991 new DeleteAllForHostTask(this, url, callback);
993 DoCookieTaskForURL(task, url);
996 void CookieMonster::DeleteCanonicalCookieAsync(
997 const CanonicalCookie& cookie,
998 const DeleteCookieCallback& callback) {
999 scoped_refptr<DeleteCanonicalCookieTask> task =
1000 new DeleteCanonicalCookieTask(this, cookie, callback);
1002 DoCookieTask(task);
1005 void CookieMonster::SetCookieWithOptionsAsync(
1006 const GURL& url,
1007 const std::string& cookie_line,
1008 const CookieOptions& options,
1009 const SetCookiesCallback& callback) {
1010 scoped_refptr<SetCookieWithOptionsTask> task =
1011 new SetCookieWithOptionsTask(this, url, cookie_line, options, callback);
1013 DoCookieTaskForURL(task, url);
1016 void CookieMonster::GetCookiesWithOptionsAsync(
1017 const GURL& url,
1018 const CookieOptions& options,
1019 const GetCookiesCallback& callback) {
1020 scoped_refptr<GetCookiesWithOptionsTask> task =
1021 new GetCookiesWithOptionsTask(this, url, options, callback);
1023 DoCookieTaskForURL(task, url);
1026 void CookieMonster::DeleteCookieAsync(const GURL& url,
1027 const std::string& cookie_name,
1028 const base::Closure& callback) {
1029 scoped_refptr<DeleteCookieTask> task =
1030 new DeleteCookieTask(this, url, cookie_name, callback);
1032 DoCookieTaskForURL(task, url);
1035 void CookieMonster::DeleteSessionCookiesAsync(
1036 const CookieStore::DeleteCallback& callback) {
1037 scoped_refptr<DeleteSessionCookiesTask> task =
1038 new DeleteSessionCookiesTask(this, callback);
1040 DoCookieTask(task);
1043 void CookieMonster::DoCookieTask(
1044 const scoped_refptr<CookieMonsterTask>& task_item) {
1046 base::AutoLock autolock(lock_);
1047 InitIfNecessary();
1048 if (!loaded_) {
1049 tasks_pending_.push(task_item);
1050 return;
1054 task_item->Run();
1057 void CookieMonster::DoCookieTaskForURL(
1058 const scoped_refptr<CookieMonsterTask>& task_item,
1059 const GURL& url) {
1061 base::AutoLock autolock(lock_);
1062 InitIfNecessary();
1063 // If cookies for the requested domain key (eTLD+1) have been loaded from DB
1064 // then run the task, otherwise load from DB.
1065 if (!loaded_) {
1066 // Checks if the domain key has been loaded.
1067 std::string key(cookie_util::GetEffectiveDomain(url.scheme(),
1068 url.host()));
1069 if (keys_loaded_.find(key) == keys_loaded_.end()) {
1070 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > >
1071 ::iterator it = tasks_pending_for_key_.find(key);
1072 if (it == tasks_pending_for_key_.end()) {
1073 store_->LoadCookiesForKey(key,
1074 base::Bind(&CookieMonster::OnKeyLoaded, this, key));
1075 it = tasks_pending_for_key_.insert(std::make_pair(key,
1076 std::deque<scoped_refptr<CookieMonsterTask> >())).first;
1078 it->second.push_back(task_item);
1079 return;
1083 task_item->Run();
1086 bool CookieMonster::SetCookieWithDetails(const GURL& url,
1087 const std::string& name,
1088 const std::string& value,
1089 const std::string& domain,
1090 const std::string& path,
1091 const base::Time& expiration_time,
1092 bool secure,
1093 bool http_only,
1094 CookiePriority priority) {
1095 base::AutoLock autolock(lock_);
1097 if (!HasCookieableScheme(url))
1098 return false;
1100 Time creation_time = CurrentTime();
1101 last_time_seen_ = creation_time;
1103 scoped_ptr<CanonicalCookie> cc;
1104 cc.reset(CanonicalCookie::Create(url, name, value, domain, path,
1105 creation_time, expiration_time,
1106 secure, http_only, priority));
1108 if (!cc.get())
1109 return false;
1111 CookieOptions options;
1112 options.set_include_httponly();
1113 return SetCanonicalCookie(&cc, creation_time, options);
1116 bool CookieMonster::ImportCookies(const CookieList& list) {
1117 base::AutoLock autolock(lock_);
1118 InitIfNecessary();
1119 for (net::CookieList::const_iterator iter = list.begin();
1120 iter != list.end(); ++iter) {
1121 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie(*iter));
1122 net::CookieOptions options;
1123 options.set_include_httponly();
1124 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), options))
1125 return false;
1127 return true;
1130 CookieList CookieMonster::GetAllCookies() {
1131 base::AutoLock autolock(lock_);
1133 // This function is being called to scrape the cookie list for management UI
1134 // or similar. We shouldn't show expired cookies in this list since it will
1135 // just be confusing to users, and this function is called rarely enough (and
1136 // is already slow enough) that it's OK to take the time to garbage collect
1137 // the expired cookies now.
1139 // Note that this does not prune cookies to be below our limits (if we've
1140 // exceeded them) the way that calling GarbageCollect() would.
1141 GarbageCollectExpired(Time::Now(),
1142 CookieMapItPair(cookies_.begin(), cookies_.end()),
1143 NULL);
1145 // Copy the CanonicalCookie pointers from the map so that we can use the same
1146 // sorter as elsewhere, then copy the result out.
1147 std::vector<CanonicalCookie*> cookie_ptrs;
1148 cookie_ptrs.reserve(cookies_.size());
1149 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end(); ++it)
1150 cookie_ptrs.push_back(it->second);
1151 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
1153 CookieList cookie_list;
1154 cookie_list.reserve(cookie_ptrs.size());
1155 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
1156 it != cookie_ptrs.end(); ++it)
1157 cookie_list.push_back(**it);
1159 return cookie_list;
1162 CookieList CookieMonster::GetAllCookiesForURLWithOptions(
1163 const GURL& url,
1164 const CookieOptions& options) {
1165 base::AutoLock autolock(lock_);
1167 std::vector<CanonicalCookie*> cookie_ptrs;
1168 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs);
1169 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
1171 CookieList cookies;
1172 cookies.reserve(cookie_ptrs.size());
1173 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
1174 it != cookie_ptrs.end(); it++)
1175 cookies.push_back(**it);
1177 return cookies;
1180 CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) {
1181 CookieOptions options;
1182 options.set_include_httponly();
1184 return GetAllCookiesForURLWithOptions(url, options);
1187 int CookieMonster::DeleteAll(bool sync_to_store) {
1188 base::AutoLock autolock(lock_);
1190 int num_deleted = 0;
1191 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1192 CookieMap::iterator curit = it;
1193 ++it;
1194 InternalDeleteCookie(curit, sync_to_store,
1195 sync_to_store ? DELETE_COOKIE_EXPLICIT :
1196 DELETE_COOKIE_DONT_RECORD /* Destruction. */);
1197 ++num_deleted;
1200 return num_deleted;
1203 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin,
1204 const Time& delete_end) {
1205 base::AutoLock autolock(lock_);
1207 int num_deleted = 0;
1208 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1209 CookieMap::iterator curit = it;
1210 CanonicalCookie* cc = curit->second;
1211 ++it;
1213 if (cc->CreationDate() >= delete_begin &&
1214 (delete_end.is_null() || cc->CreationDate() < delete_end)) {
1215 InternalDeleteCookie(curit,
1216 true, /*sync_to_store*/
1217 DELETE_COOKIE_EXPLICIT);
1218 ++num_deleted;
1222 return num_deleted;
1225 int CookieMonster::DeleteAllCreatedBetweenForHost(const Time delete_begin,
1226 const Time delete_end,
1227 const GURL& url) {
1228 base::AutoLock autolock(lock_);
1230 if (!HasCookieableScheme(url))
1231 return 0;
1233 const std::string host(url.host());
1235 // We store host cookies in the store by their canonical host name;
1236 // domain cookies are stored with a leading ".". So this is a pretty
1237 // simple lookup and per-cookie delete.
1238 int num_deleted = 0;
1239 for (CookieMapItPair its = cookies_.equal_range(GetKey(host));
1240 its.first != its.second;) {
1241 CookieMap::iterator curit = its.first;
1242 ++its.first;
1244 const CanonicalCookie* const cc = curit->second;
1246 // Delete only on a match as a host cookie.
1247 if (cc->IsHostCookie() && cc->IsDomainMatch(host) &&
1248 cc->CreationDate() >= delete_begin &&
1249 // The assumption that null |delete_end| is equivalent to
1250 // Time::Max() is confusing.
1251 (delete_end.is_null() || cc->CreationDate() < delete_end)) {
1252 num_deleted++;
1254 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT);
1257 return num_deleted;
1260 int CookieMonster::DeleteAllForHost(const GURL& url) {
1261 return DeleteAllCreatedBetweenForHost(Time(), Time::Max(), url);
1265 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) {
1266 base::AutoLock autolock(lock_);
1268 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain()));
1269 its.first != its.second; ++its.first) {
1270 // The creation date acts as our unique index...
1271 if (its.first->second->CreationDate() == cookie.CreationDate()) {
1272 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT);
1273 return true;
1276 return false;
1279 void CookieMonster::SetCookieableSchemes(const char* const schemes[],
1280 size_t num_schemes) {
1281 base::AutoLock autolock(lock_);
1283 // Cookieable Schemes must be set before first use of function.
1284 DCHECK(!initialized_);
1286 cookieable_schemes_.clear();
1287 cookieable_schemes_.insert(cookieable_schemes_.end(),
1288 schemes, schemes + num_schemes);
1291 void CookieMonster::SetEnableFileScheme(bool accept) {
1292 // This assumes "file" is always at the end of the array. See the comment
1293 // above kDefaultCookieableSchemes.
1294 int num_schemes = accept ? kDefaultCookieableSchemesCount :
1295 kDefaultCookieableSchemesCount - 1;
1296 SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes);
1299 void CookieMonster::SetKeepExpiredCookies() {
1300 keep_expired_cookies_ = true;
1303 void CookieMonster::FlushStore(const base::Closure& callback) {
1304 base::AutoLock autolock(lock_);
1305 if (initialized_ && store_.get())
1306 store_->Flush(callback);
1307 else if (!callback.is_null())
1308 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
1311 bool CookieMonster::SetCookieWithOptions(const GURL& url,
1312 const std::string& cookie_line,
1313 const CookieOptions& options) {
1314 base::AutoLock autolock(lock_);
1316 if (!HasCookieableScheme(url)) {
1317 return false;
1320 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options);
1323 std::string CookieMonster::GetCookiesWithOptions(const GURL& url,
1324 const CookieOptions& options) {
1325 base::AutoLock autolock(lock_);
1327 if (!HasCookieableScheme(url))
1328 return std::string();
1330 std::vector<CanonicalCookie*> cookies;
1331 FindCookiesForHostAndDomain(url, options, true, &cookies);
1332 std::sort(cookies.begin(), cookies.end(), CookieSorter);
1334 std::string cookie_line = BuildCookieLine(cookies);
1336 VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line;
1338 return cookie_line;
1341 void CookieMonster::DeleteCookie(const GURL& url,
1342 const std::string& cookie_name) {
1343 base::AutoLock autolock(lock_);
1345 if (!HasCookieableScheme(url))
1346 return;
1348 CookieOptions options;
1349 options.set_include_httponly();
1350 // Get the cookies for this host and its domain(s).
1351 std::vector<CanonicalCookie*> cookies;
1352 FindCookiesForHostAndDomain(url, options, true, &cookies);
1353 std::set<CanonicalCookie*> matching_cookies;
1355 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin();
1356 it != cookies.end(); ++it) {
1357 if ((*it)->Name() != cookie_name)
1358 continue;
1359 if (url.path().find((*it)->Path()))
1360 continue;
1361 matching_cookies.insert(*it);
1364 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1365 CookieMap::iterator curit = it;
1366 ++it;
1367 if (matching_cookies.find(curit->second) != matching_cookies.end()) {
1368 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT);
1373 int CookieMonster::DeleteSessionCookies() {
1374 base::AutoLock autolock(lock_);
1376 int num_deleted = 0;
1377 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1378 CookieMap::iterator curit = it;
1379 CanonicalCookie* cc = curit->second;
1380 ++it;
1382 if (!cc->IsPersistent()) {
1383 InternalDeleteCookie(curit,
1384 true, /*sync_to_store*/
1385 DELETE_COOKIE_EXPIRED);
1386 ++num_deleted;
1390 return num_deleted;
1393 bool CookieMonster::HasCookiesForETLDP1(const std::string& etldp1) {
1394 base::AutoLock autolock(lock_);
1396 const std::string key(GetKey(etldp1));
1398 CookieMapItPair its = cookies_.equal_range(key);
1399 return its.first != its.second;
1402 CookieMonster* CookieMonster::GetCookieMonster() {
1403 return this;
1406 // This function must be called before the CookieMonster is used.
1407 void CookieMonster::SetPersistSessionCookies(bool persist_session_cookies) {
1408 DCHECK(!initialized_);
1409 persist_session_cookies_ = persist_session_cookies;
1412 void CookieMonster::SetForceKeepSessionState() {
1413 if (store_.get()) {
1414 store_->SetForceKeepSessionState();
1418 CookieMonster::~CookieMonster() {
1419 DeleteAll(false);
1422 bool CookieMonster::SetCookieWithCreationTime(const GURL& url,
1423 const std::string& cookie_line,
1424 const base::Time& creation_time) {
1425 DCHECK(!store_.get()) << "This method is only to be used by unit-tests.";
1426 base::AutoLock autolock(lock_);
1428 if (!HasCookieableScheme(url)) {
1429 return false;
1432 InitIfNecessary();
1433 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time,
1434 CookieOptions());
1437 void CookieMonster::InitStore() {
1438 DCHECK(store_.get()) << "Store must exist to initialize";
1440 // We bind in the current time so that we can report the wall-clock time for
1441 // loading cookies.
1442 store_->Load(base::Bind(&CookieMonster::OnLoaded, this, TimeTicks::Now()));
1445 void CookieMonster::ReportLoaded() {
1446 if (delegate_.get())
1447 delegate_->OnLoaded();
1450 void CookieMonster::OnLoaded(TimeTicks beginning_time,
1451 const std::vector<CanonicalCookie*>& cookies) {
1452 StoreLoadedCookies(cookies);
1453 histogram_time_blocked_on_load_->AddTime(TimeTicks::Now() - beginning_time);
1455 // Invoke the task queue of cookie request.
1456 InvokeQueue();
1458 ReportLoaded();
1461 void CookieMonster::OnKeyLoaded(const std::string& key,
1462 const std::vector<CanonicalCookie*>& cookies) {
1463 // This function does its own separate locking.
1464 StoreLoadedCookies(cookies);
1466 std::deque<scoped_refptr<CookieMonsterTask> > tasks_pending_for_key;
1468 // We need to do this repeatedly until no more tasks were added to the queue
1469 // during the period where we release the lock.
1470 while (true) {
1472 base::AutoLock autolock(lock_);
1473 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > >
1474 ::iterator it = tasks_pending_for_key_.find(key);
1475 if (it == tasks_pending_for_key_.end()) {
1476 keys_loaded_.insert(key);
1477 return;
1479 if (it->second.empty()) {
1480 keys_loaded_.insert(key);
1481 tasks_pending_for_key_.erase(it);
1482 return;
1484 it->second.swap(tasks_pending_for_key);
1487 while (!tasks_pending_for_key.empty()) {
1488 scoped_refptr<CookieMonsterTask> task = tasks_pending_for_key.front();
1489 task->Run();
1490 tasks_pending_for_key.pop_front();
1495 void CookieMonster::StoreLoadedCookies(
1496 const std::vector<CanonicalCookie*>& cookies) {
1497 // Initialize the store and sync in any saved persistent cookies. We don't
1498 // care if it's expired, insert it so it can be garbage collected, removed,
1499 // and sync'd.
1500 base::AutoLock autolock(lock_);
1502 CookieItVector cookies_with_control_chars;
1504 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin();
1505 it != cookies.end(); ++it) {
1506 int64 cookie_creation_time = (*it)->CreationDate().ToInternalValue();
1508 if (creation_times_.insert(cookie_creation_time).second) {
1509 CookieMap::iterator inserted =
1510 InternalInsertCookie(GetKey((*it)->Domain()), *it, false);
1511 const Time cookie_access_time((*it)->LastAccessDate());
1512 if (earliest_access_time_.is_null() ||
1513 cookie_access_time < earliest_access_time_)
1514 earliest_access_time_ = cookie_access_time;
1516 if (ContainsControlCharacter((*it)->Name()) ||
1517 ContainsControlCharacter((*it)->Value())) {
1518 cookies_with_control_chars.push_back(inserted);
1520 } else {
1521 LOG(ERROR) << base::StringPrintf("Found cookies with duplicate creation "
1522 "times in backing store: "
1523 "{name='%s', domain='%s', path='%s'}",
1524 (*it)->Name().c_str(),
1525 (*it)->Domain().c_str(),
1526 (*it)->Path().c_str());
1527 // We've been given ownership of the cookie and are throwing it
1528 // away; reclaim the space.
1529 delete (*it);
1533 // Any cookies that contain control characters that we have loaded from the
1534 // persistent store should be deleted. See http://crbug.com/238041.
1535 for (CookieItVector::iterator it = cookies_with_control_chars.begin();
1536 it != cookies_with_control_chars.end();) {
1537 CookieItVector::iterator curit = it;
1538 ++it;
1540 InternalDeleteCookie(*curit, true, DELETE_COOKIE_CONTROL_CHAR);
1543 // After importing cookies from the PersistentCookieStore, verify that
1544 // none of our other constraints are violated.
1545 // In particular, the backing store might have given us duplicate cookies.
1547 // This method could be called multiple times due to priority loading, thus
1548 // cookies loaded in previous runs will be validated again, but this is OK
1549 // since they are expected to be much fewer than total DB.
1550 EnsureCookiesMapIsValid();
1553 void CookieMonster::InvokeQueue() {
1554 while (true) {
1555 scoped_refptr<CookieMonsterTask> request_task;
1557 base::AutoLock autolock(lock_);
1558 if (tasks_pending_.empty()) {
1559 loaded_ = true;
1560 creation_times_.clear();
1561 keys_loaded_.clear();
1562 break;
1564 request_task = tasks_pending_.front();
1565 tasks_pending_.pop();
1567 request_task->Run();
1571 void CookieMonster::EnsureCookiesMapIsValid() {
1572 lock_.AssertAcquired();
1574 int num_duplicates_trimmed = 0;
1576 // Iterate through all the of the cookies, grouped by host.
1577 CookieMap::iterator prev_range_end = cookies_.begin();
1578 while (prev_range_end != cookies_.end()) {
1579 CookieMap::iterator cur_range_begin = prev_range_end;
1580 const std::string key = cur_range_begin->first; // Keep a copy.
1581 CookieMap::iterator cur_range_end = cookies_.upper_bound(key);
1582 prev_range_end = cur_range_end;
1584 // Ensure no equivalent cookies for this host.
1585 num_duplicates_trimmed +=
1586 TrimDuplicateCookiesForKey(key, cur_range_begin, cur_range_end);
1589 // Record how many duplicates were found in the database.
1590 // See InitializeHistograms() for details.
1591 histogram_cookie_deletion_cause_->Add(num_duplicates_trimmed);
1594 int CookieMonster::TrimDuplicateCookiesForKey(
1595 const std::string& key,
1596 CookieMap::iterator begin,
1597 CookieMap::iterator end) {
1598 lock_.AssertAcquired();
1600 // Set of cookies ordered by creation time.
1601 typedef std::set<CookieMap::iterator, OrderByCreationTimeDesc> CookieSet;
1603 // Helper map we populate to find the duplicates.
1604 typedef std::map<CookieSignature, CookieSet> EquivalenceMap;
1605 EquivalenceMap equivalent_cookies;
1607 // The number of duplicate cookies that have been found.
1608 int num_duplicates = 0;
1610 // Iterate through all of the cookies in our range, and insert them into
1611 // the equivalence map.
1612 for (CookieMap::iterator it = begin; it != end; ++it) {
1613 DCHECK_EQ(key, it->first);
1614 CanonicalCookie* cookie = it->second;
1616 CookieSignature signature(cookie->Name(), cookie->Domain(),
1617 cookie->Path());
1618 CookieSet& set = equivalent_cookies[signature];
1620 // We found a duplicate!
1621 if (!set.empty())
1622 num_duplicates++;
1624 // We save the iterator into |cookies_| rather than the actual cookie
1625 // pointer, since we may need to delete it later.
1626 bool insert_success = set.insert(it).second;
1627 DCHECK(insert_success) <<
1628 "Duplicate creation times found in duplicate cookie name scan.";
1631 // If there were no duplicates, we are done!
1632 if (num_duplicates == 0)
1633 return 0;
1635 // Make sure we find everything below that we did above.
1636 int num_duplicates_found = 0;
1638 // Otherwise, delete all the duplicate cookies, both from our in-memory store
1639 // and from the backing store.
1640 for (EquivalenceMap::iterator it = equivalent_cookies.begin();
1641 it != equivalent_cookies.end();
1642 ++it) {
1643 const CookieSignature& signature = it->first;
1644 CookieSet& dupes = it->second;
1646 if (dupes.size() <= 1)
1647 continue; // This cookiename/path has no duplicates.
1648 num_duplicates_found += dupes.size() - 1;
1650 // Since |dups| is sorted by creation time (descending), the first cookie
1651 // is the most recent one, so we will keep it. The rest are duplicates.
1652 dupes.erase(dupes.begin());
1654 LOG(ERROR) << base::StringPrintf(
1655 "Found %d duplicate cookies for host='%s', "
1656 "with {name='%s', domain='%s', path='%s'}",
1657 static_cast<int>(dupes.size()),
1658 key.c_str(),
1659 signature.name.c_str(),
1660 signature.domain.c_str(),
1661 signature.path.c_str());
1663 // Remove all the cookies identified by |dupes|. It is valid to delete our
1664 // list of iterators one at a time, since |cookies_| is a multimap (they
1665 // don't invalidate existing iterators following deletion).
1666 for (CookieSet::iterator dupes_it = dupes.begin();
1667 dupes_it != dupes.end();
1668 ++dupes_it) {
1669 InternalDeleteCookie(*dupes_it, true,
1670 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE);
1673 DCHECK_EQ(num_duplicates, num_duplicates_found);
1675 return num_duplicates;
1678 // Note: file must be the last scheme.
1679 const char* const CookieMonster::kDefaultCookieableSchemes[] =
1680 { "http", "https", "ws", "wss", "file" };
1681 const int CookieMonster::kDefaultCookieableSchemesCount =
1682 arraysize(kDefaultCookieableSchemes);
1684 void CookieMonster::SetDefaultCookieableSchemes() {
1685 // Always disable file scheme unless SetEnableFileScheme(true) is called.
1686 SetCookieableSchemes(kDefaultCookieableSchemes,
1687 kDefaultCookieableSchemesCount - 1);
1690 void CookieMonster::FindCookiesForHostAndDomain(
1691 const GURL& url,
1692 const CookieOptions& options,
1693 bool update_access_time,
1694 std::vector<CanonicalCookie*>* cookies) {
1695 lock_.AssertAcquired();
1697 const Time current_time(CurrentTime());
1699 // Probe to save statistics relatively frequently. We do it here rather
1700 // than in the set path as many websites won't set cookies, and we
1701 // want to collect statistics whenever the browser's being used.
1702 RecordPeriodicStats(current_time);
1704 // Can just dispatch to FindCookiesForKey
1705 const std::string key(GetKey(url.host()));
1706 FindCookiesForKey(key, url, options, current_time,
1707 update_access_time, cookies);
1710 void CookieMonster::FindCookiesForKey(const std::string& key,
1711 const GURL& url,
1712 const CookieOptions& options,
1713 const Time& current,
1714 bool update_access_time,
1715 std::vector<CanonicalCookie*>* cookies) {
1716 lock_.AssertAcquired();
1718 for (CookieMapItPair its = cookies_.equal_range(key);
1719 its.first != its.second; ) {
1720 CookieMap::iterator curit = its.first;
1721 CanonicalCookie* cc = curit->second;
1722 ++its.first;
1724 // If the cookie is expired, delete it.
1725 if (cc->IsExpired(current) && !keep_expired_cookies_) {
1726 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED);
1727 continue;
1730 // Filter out cookies that should not be included for a request to the
1731 // given |url|. HTTP only cookies are filtered depending on the passed
1732 // cookie |options|.
1733 if (!cc->IncludeForRequestURL(url, options))
1734 continue;
1736 // Add this cookie to the set of matching cookies. Update the access
1737 // time if we've been requested to do so.
1738 if (update_access_time) {
1739 InternalUpdateCookieAccessTime(cc, current);
1741 cookies->push_back(cc);
1745 bool CookieMonster::DeleteAnyEquivalentCookie(const std::string& key,
1746 const CanonicalCookie& ecc,
1747 bool skip_httponly,
1748 bool already_expired) {
1749 lock_.AssertAcquired();
1751 bool found_equivalent_cookie = false;
1752 bool skipped_httponly = false;
1753 for (CookieMapItPair its = cookies_.equal_range(key);
1754 its.first != its.second; ) {
1755 CookieMap::iterator curit = its.first;
1756 CanonicalCookie* cc = curit->second;
1757 ++its.first;
1759 if (ecc.IsEquivalent(*cc)) {
1760 // We should never have more than one equivalent cookie, since they should
1761 // overwrite each other.
1762 CHECK(!found_equivalent_cookie) <<
1763 "Duplicate equivalent cookies found, cookie store is corrupted.";
1764 if (skip_httponly && cc->IsHttpOnly()) {
1765 skipped_httponly = true;
1766 } else {
1767 InternalDeleteCookie(curit, true, already_expired ?
1768 DELETE_COOKIE_EXPIRED_OVERWRITE : DELETE_COOKIE_OVERWRITE);
1770 found_equivalent_cookie = true;
1773 return skipped_httponly;
1776 CookieMonster::CookieMap::iterator CookieMonster::InternalInsertCookie(
1777 const std::string& key,
1778 CanonicalCookie* cc,
1779 bool sync_to_store) {
1780 lock_.AssertAcquired();
1782 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() &&
1783 sync_to_store)
1784 store_->AddCookie(*cc);
1785 CookieMap::iterator inserted =
1786 cookies_.insert(CookieMap::value_type(key, cc));
1787 if (delegate_.get()) {
1788 delegate_->OnCookieChanged(
1789 *cc, false, CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT);
1791 RunCallbacks(*cc, false);
1793 return inserted;
1796 bool CookieMonster::SetCookieWithCreationTimeAndOptions(
1797 const GURL& url,
1798 const std::string& cookie_line,
1799 const Time& creation_time_or_null,
1800 const CookieOptions& options) {
1801 lock_.AssertAcquired();
1803 VLOG(kVlogSetCookies) << "SetCookie() line: " << cookie_line;
1805 Time creation_time = creation_time_or_null;
1806 if (creation_time.is_null()) {
1807 creation_time = CurrentTime();
1808 last_time_seen_ = creation_time;
1811 scoped_ptr<CanonicalCookie> cc(
1812 CanonicalCookie::Create(url, cookie_line, creation_time, options));
1814 if (!cc.get()) {
1815 VLOG(kVlogSetCookies) << "WARNING: Failed to allocate CanonicalCookie";
1816 return false;
1818 return SetCanonicalCookie(&cc, creation_time, options);
1821 bool CookieMonster::SetCanonicalCookie(scoped_ptr<CanonicalCookie>* cc,
1822 const Time& creation_time,
1823 const CookieOptions& options) {
1824 const std::string key(GetKey((*cc)->Domain()));
1825 bool already_expired = (*cc)->IsExpired(creation_time);
1827 if (DeleteAnyEquivalentCookie(key, **cc, options.exclude_httponly(),
1828 already_expired)) {
1829 VLOG(kVlogSetCookies) << "SetCookie() not clobbering httponly cookie";
1830 return false;
1833 VLOG(kVlogSetCookies) << "SetCookie() key: " << key << " cc: "
1834 << (*cc)->DebugString();
1836 // Realize that we might be setting an expired cookie, and the only point
1837 // was to delete the cookie which we've already done.
1838 if (!already_expired || keep_expired_cookies_) {
1839 // See InitializeHistograms() for details.
1840 if ((*cc)->IsPersistent()) {
1841 histogram_expiration_duration_minutes_->Add(
1842 ((*cc)->ExpiryDate() - creation_time).InMinutes());
1846 CanonicalCookie cookie = *(cc->get());
1847 InternalInsertCookie(key, cc->release(), true);
1849 } else {
1850 VLOG(kVlogSetCookies) << "SetCookie() not storing already expired cookie.";
1853 // We assume that hopefully setting a cookie will be less common than
1854 // querying a cookie. Since setting a cookie can put us over our limits,
1855 // make sure that we garbage collect... We can also make the assumption that
1856 // if a cookie was set, in the common case it will be used soon after,
1857 // and we will purge the expired cookies in GetCookies().
1858 GarbageCollect(creation_time, key);
1860 return true;
1863 void CookieMonster::InternalUpdateCookieAccessTime(CanonicalCookie* cc,
1864 const Time& current) {
1865 lock_.AssertAcquired();
1867 // Based off the Mozilla code. When a cookie has been accessed recently,
1868 // don't bother updating its access time again. This reduces the number of
1869 // updates we do during pageload, which in turn reduces the chance our storage
1870 // backend will hit its batch thresholds and be forced to update.
1871 if ((current - cc->LastAccessDate()) < last_access_threshold_)
1872 return;
1874 // See InitializeHistograms() for details.
1875 histogram_between_access_interval_minutes_->Add(
1876 (current - cc->LastAccessDate()).InMinutes());
1878 cc->SetLastAccessDate(current);
1879 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get())
1880 store_->UpdateCookieAccessTime(*cc);
1883 // InternalDeleteCookies must not invalidate iterators other than the one being
1884 // deleted.
1885 void CookieMonster::InternalDeleteCookie(CookieMap::iterator it,
1886 bool sync_to_store,
1887 DeletionCause deletion_cause) {
1888 lock_.AssertAcquired();
1890 // Ideally, this would be asserted up where we define ChangeCauseMapping,
1891 // but DeletionCause's visibility (or lack thereof) forces us to make
1892 // this check here.
1893 static_assert(arraysize(ChangeCauseMapping) == DELETE_COOKIE_LAST_ENTRY + 1,
1894 "ChangeCauseMapping size should match DeletionCause size");
1896 // See InitializeHistograms() for details.
1897 if (deletion_cause != DELETE_COOKIE_DONT_RECORD)
1898 histogram_cookie_deletion_cause_->Add(deletion_cause);
1900 CanonicalCookie* cc = it->second;
1901 VLOG(kVlogSetCookies) << "InternalDeleteCookie() cc: " << cc->DebugString();
1903 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() &&
1904 sync_to_store)
1905 store_->DeleteCookie(*cc);
1906 if (delegate_.get()) {
1907 ChangeCausePair mapping = ChangeCauseMapping[deletion_cause];
1909 if (mapping.notify)
1910 delegate_->OnCookieChanged(*cc, true, mapping.cause);
1912 RunCallbacks(*cc, true);
1913 cookies_.erase(it);
1914 delete cc;
1917 // Domain expiry behavior is unchanged by key/expiry scheme (the
1918 // meaning of the key is different, but that's not visible to this routine).
1919 int CookieMonster::GarbageCollect(const Time& current,
1920 const std::string& key) {
1921 lock_.AssertAcquired();
1923 int num_deleted = 0;
1924 Time safe_date(
1925 Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays));
1927 // Collect garbage for this key, minding cookie priorities.
1928 if (cookies_.count(key) > kDomainMaxCookies) {
1929 VLOG(kVlogGarbageCollection) << "GarbageCollect() key: " << key;
1931 CookieItVector cookie_its;
1932 num_deleted += GarbageCollectExpired(
1933 current, cookies_.equal_range(key), &cookie_its);
1934 if (cookie_its.size() > kDomainMaxCookies) {
1935 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect domain.";
1936 size_t purge_goal =
1937 cookie_its.size() - (kDomainMaxCookies - kDomainPurgeCookies);
1938 DCHECK(purge_goal > kDomainPurgeCookies);
1940 // Boundary iterators into |cookie_its| for different priorities.
1941 CookieItVector::iterator it_bdd[4];
1942 // Intialize |it_bdd| while sorting |cookie_its| by priorities.
1943 // Schematic: [MLLHMHHLMM] => [LLL|MMMM|HHH], with 4 boundaries.
1944 it_bdd[0] = cookie_its.begin();
1945 it_bdd[3] = cookie_its.end();
1946 it_bdd[1] = PartitionCookieByPriority(it_bdd[0], it_bdd[3],
1947 COOKIE_PRIORITY_LOW);
1948 it_bdd[2] = PartitionCookieByPriority(it_bdd[1], it_bdd[3],
1949 COOKIE_PRIORITY_MEDIUM);
1950 size_t quota[3] = {
1951 kDomainCookiesQuotaLow,
1952 kDomainCookiesQuotaMedium,
1953 kDomainCookiesQuotaHigh
1956 // Purge domain cookies in 3 rounds.
1957 // Round 1: consider low-priority cookies only: evict least-recently
1958 // accessed, while protecting quota[0] of these from deletion.
1959 // Round 2: consider {low, medium}-priority cookies, evict least-recently
1960 // accessed, while protecting quota[0] + quota[1].
1961 // Round 3: consider all cookies, evict least-recently accessed.
1962 size_t accumulated_quota = 0;
1963 CookieItVector::iterator it_purge_begin = it_bdd[0];
1964 for (int i = 0; i < 3 && purge_goal > 0; ++i) {
1965 accumulated_quota += quota[i];
1967 size_t num_considered = it_bdd[i + 1] - it_purge_begin;
1968 if (num_considered <= accumulated_quota)
1969 continue;
1971 // Number of cookies that will be purged in this round.
1972 size_t round_goal =
1973 std::min(purge_goal, num_considered - accumulated_quota);
1974 purge_goal -= round_goal;
1976 SortLeastRecentlyAccessed(it_purge_begin, it_bdd[i + 1], round_goal);
1977 // Cookies accessed on or after |safe_date| would have been safe from
1978 // global purge, and we want to keep track of this.
1979 CookieItVector::iterator it_purge_end = it_purge_begin + round_goal;
1980 CookieItVector::iterator it_purge_middle =
1981 LowerBoundAccessDate(it_purge_begin, it_purge_end, safe_date);
1982 // Delete cookies accessed before |safe_date|.
1983 num_deleted += GarbageCollectDeleteRange(
1984 current,
1985 DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE,
1986 it_purge_begin,
1987 it_purge_middle);
1988 // Delete cookies accessed on or after |safe_date|.
1989 num_deleted += GarbageCollectDeleteRange(
1990 current,
1991 DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE,
1992 it_purge_middle,
1993 it_purge_end);
1994 it_purge_begin = it_purge_end;
1996 DCHECK_EQ(0U, purge_goal);
2000 // Collect garbage for everything. With firefox style we want to preserve
2001 // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict.
2002 if (cookies_.size() > kMaxCookies &&
2003 earliest_access_time_ < safe_date) {
2004 VLOG(kVlogGarbageCollection) << "GarbageCollect() everything";
2005 CookieItVector cookie_its;
2006 num_deleted += GarbageCollectExpired(
2007 current, CookieMapItPair(cookies_.begin(), cookies_.end()),
2008 &cookie_its);
2009 if (cookie_its.size() > kMaxCookies) {
2010 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect everything.";
2011 size_t purge_goal = cookie_its.size() - (kMaxCookies - kPurgeCookies);
2012 DCHECK(purge_goal > kPurgeCookies);
2013 // Sorts up to *and including* |cookie_its[purge_goal]|, so
2014 // |earliest_access_time| will be properly assigned even if
2015 // |global_purge_it| == |cookie_its.begin() + purge_goal|.
2016 SortLeastRecentlyAccessed(cookie_its.begin(), cookie_its.end(),
2017 purge_goal);
2018 // Find boundary to cookies older than safe_date.
2019 CookieItVector::iterator global_purge_it =
2020 LowerBoundAccessDate(cookie_its.begin(),
2021 cookie_its.begin() + purge_goal,
2022 safe_date);
2023 // Only delete the old cookies.
2024 num_deleted += GarbageCollectDeleteRange(
2025 current,
2026 DELETE_COOKIE_EVICTED_GLOBAL,
2027 cookie_its.begin(),
2028 global_purge_it);
2029 // Set access day to the oldest cookie that wasn't deleted.
2030 earliest_access_time_ = (*global_purge_it)->second->LastAccessDate();
2034 return num_deleted;
2037 int CookieMonster::GarbageCollectExpired(
2038 const Time& current,
2039 const CookieMapItPair& itpair,
2040 CookieItVector* cookie_its) {
2041 if (keep_expired_cookies_)
2042 return 0;
2044 lock_.AssertAcquired();
2046 int num_deleted = 0;
2047 for (CookieMap::iterator it = itpair.first, end = itpair.second; it != end;) {
2048 CookieMap::iterator curit = it;
2049 ++it;
2051 if (curit->second->IsExpired(current)) {
2052 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED);
2053 ++num_deleted;
2054 } else if (cookie_its) {
2055 cookie_its->push_back(curit);
2059 return num_deleted;
2062 int CookieMonster::GarbageCollectDeleteRange(
2063 const Time& current,
2064 DeletionCause cause,
2065 CookieItVector::iterator it_begin,
2066 CookieItVector::iterator it_end) {
2067 for (CookieItVector::iterator it = it_begin; it != it_end; it++) {
2068 histogram_evicted_last_access_minutes_->Add(
2069 (current - (*it)->second->LastAccessDate()).InMinutes());
2070 InternalDeleteCookie((*it), true, cause);
2072 return it_end - it_begin;
2075 // A wrapper around registry_controlled_domains::GetDomainAndRegistry
2076 // to make clear we're creating a key for our local map. Here and
2077 // in FindCookiesForHostAndDomain() are the only two places where
2078 // we need to conditionalize based on key type.
2080 // Note that this key algorithm explicitly ignores the scheme. This is
2081 // because when we're entering cookies into the map from the backing store,
2082 // we in general won't have the scheme at that point.
2083 // In practical terms, this means that file cookies will be stored
2084 // in the map either by an empty string or by UNC name (and will be
2085 // limited by kMaxCookiesPerHost), and extension cookies will be stored
2086 // based on the single extension id, as the extension id won't have the
2087 // form of a DNS host and hence GetKey() will return it unchanged.
2089 // Arguably the right thing to do here is to make the key
2090 // algorithm dependent on the scheme, and make sure that the scheme is
2091 // available everywhere the key must be obtained (specfically at backing
2092 // store load time). This would require either changing the backing store
2093 // database schema to include the scheme (far more trouble than it's worth), or
2094 // separating out file cookies into their own CookieMonster instance and
2095 // thus restricting each scheme to a single cookie monster (which might
2096 // be worth it, but is still too much trouble to solve what is currently a
2097 // non-problem).
2098 std::string CookieMonster::GetKey(const std::string& domain) const {
2099 std::string effective_domain(
2100 registry_controlled_domains::GetDomainAndRegistry(
2101 domain, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
2102 if (effective_domain.empty())
2103 effective_domain = domain;
2105 if (!effective_domain.empty() && effective_domain[0] == '.')
2106 return effective_domain.substr(1);
2107 return effective_domain;
2110 bool CookieMonster::IsCookieableScheme(const std::string& scheme) {
2111 base::AutoLock autolock(lock_);
2113 return std::find(cookieable_schemes_.begin(), cookieable_schemes_.end(),
2114 scheme) != cookieable_schemes_.end();
2117 bool CookieMonster::HasCookieableScheme(const GURL& url) {
2118 lock_.AssertAcquired();
2120 // Make sure the request is on a cookie-able url scheme.
2121 for (size_t i = 0; i < cookieable_schemes_.size(); ++i) {
2122 // We matched a scheme.
2123 if (url.SchemeIs(cookieable_schemes_[i].c_str())) {
2124 // We've matched a supported scheme.
2125 return true;
2129 // The scheme didn't match any in our whitelist.
2130 VLOG(kVlogPerCookieMonster) << "WARNING: Unsupported cookie scheme: "
2131 << url.scheme();
2132 return false;
2135 // Test to see if stats should be recorded, and record them if so.
2136 // The goal here is to get sampling for the average browser-hour of
2137 // activity. We won't take samples when the web isn't being surfed,
2138 // and when the web is being surfed, we'll take samples about every
2139 // kRecordStatisticsIntervalSeconds.
2140 // last_statistic_record_time_ is initialized to Now() rather than null
2141 // in the constructor so that we won't take statistics right after
2142 // startup, to avoid bias from browsers that are started but not used.
2143 void CookieMonster::RecordPeriodicStats(const base::Time& current_time) {
2144 const base::TimeDelta kRecordStatisticsIntervalTime(
2145 base::TimeDelta::FromSeconds(kRecordStatisticsIntervalSeconds));
2147 // If we've taken statistics recently, return.
2148 if (current_time - last_statistic_record_time_ <=
2149 kRecordStatisticsIntervalTime) {
2150 return;
2153 // See InitializeHistograms() for details.
2154 histogram_count_->Add(cookies_.size());
2156 // More detailed statistics on cookie counts at different granularities.
2157 TimeTicks beginning_of_time(TimeTicks::Now());
2159 for (CookieMap::const_iterator it_key = cookies_.begin();
2160 it_key != cookies_.end(); ) {
2161 const std::string& key(it_key->first);
2163 int key_count = 0;
2164 typedef std::map<std::string, unsigned int> DomainMap;
2165 DomainMap domain_map;
2166 CookieMapItPair its_cookies = cookies_.equal_range(key);
2167 while (its_cookies.first != its_cookies.second) {
2168 key_count++;
2169 const std::string& cookie_domain(its_cookies.first->second->Domain());
2170 domain_map[cookie_domain]++;
2172 its_cookies.first++;
2174 histogram_etldp1_count_->Add(key_count);
2175 histogram_domain_per_etldp1_count_->Add(domain_map.size());
2176 for (DomainMap::const_iterator domain_map_it = domain_map.begin();
2177 domain_map_it != domain_map.end(); domain_map_it++)
2178 histogram_domain_count_->Add(domain_map_it->second);
2180 it_key = its_cookies.second;
2183 VLOG(kVlogPeriodic)
2184 << "Time for recording cookie stats (us): "
2185 << (TimeTicks::Now() - beginning_of_time).InMicroseconds();
2187 last_statistic_record_time_ = current_time;
2190 // Initialize all histogram counter variables used in this class.
2192 // Normal histogram usage involves using the macros defined in
2193 // histogram.h, which automatically takes care of declaring these
2194 // variables (as statics), initializing them, and accumulating into
2195 // them, all from a single entry point. Unfortunately, that solution
2196 // doesn't work for the CookieMonster, as it's vulnerable to races between
2197 // separate threads executing the same functions and hence initializing the
2198 // same static variables. There isn't a race danger in the histogram
2199 // accumulation calls; they are written to be resilient to simultaneous
2200 // calls from multiple threads.
2202 // The solution taken here is to have per-CookieMonster instance
2203 // variables that are constructed during CookieMonster construction.
2204 // Note that these variables refer to the same underlying histogram,
2205 // so we still race (but safely) with other CookieMonster instances
2206 // for accumulation.
2208 // To do this we've expanded out the individual histogram macros calls,
2209 // with declarations of the variables in the class decl, initialization here
2210 // (done from the class constructor) and direct calls to the accumulation
2211 // methods where needed. The specific histogram macro calls on which the
2212 // initialization is based are included in comments below.
2213 void CookieMonster::InitializeHistograms() {
2214 // From UMA_HISTOGRAM_CUSTOM_COUNTS
2215 histogram_expiration_duration_minutes_ = base::Histogram::FactoryGet(
2216 "Cookie.ExpirationDurationMinutes",
2217 1, kMinutesInTenYears, 50,
2218 base::Histogram::kUmaTargetedHistogramFlag);
2219 histogram_between_access_interval_minutes_ = base::Histogram::FactoryGet(
2220 "Cookie.BetweenAccessIntervalMinutes",
2221 1, kMinutesInTenYears, 50,
2222 base::Histogram::kUmaTargetedHistogramFlag);
2223 histogram_evicted_last_access_minutes_ = base::Histogram::FactoryGet(
2224 "Cookie.EvictedLastAccessMinutes",
2225 1, kMinutesInTenYears, 50,
2226 base::Histogram::kUmaTargetedHistogramFlag);
2227 histogram_count_ = base::Histogram::FactoryGet(
2228 "Cookie.Count", 1, 4000, 50,
2229 base::Histogram::kUmaTargetedHistogramFlag);
2230 histogram_domain_count_ = base::Histogram::FactoryGet(
2231 "Cookie.DomainCount", 1, 4000, 50,
2232 base::Histogram::kUmaTargetedHistogramFlag);
2233 histogram_etldp1_count_ = base::Histogram::FactoryGet(
2234 "Cookie.Etldp1Count", 1, 4000, 50,
2235 base::Histogram::kUmaTargetedHistogramFlag);
2236 histogram_domain_per_etldp1_count_ = base::Histogram::FactoryGet(
2237 "Cookie.DomainPerEtldp1Count", 1, 4000, 50,
2238 base::Histogram::kUmaTargetedHistogramFlag);
2240 // From UMA_HISTOGRAM_COUNTS_10000 & UMA_HISTOGRAM_CUSTOM_COUNTS
2241 histogram_number_duplicate_db_cookies_ = base::Histogram::FactoryGet(
2242 "Net.NumDuplicateCookiesInDb", 1, 10000, 50,
2243 base::Histogram::kUmaTargetedHistogramFlag);
2245 // From UMA_HISTOGRAM_ENUMERATION
2246 histogram_cookie_deletion_cause_ = base::LinearHistogram::FactoryGet(
2247 "Cookie.DeletionCause", 1,
2248 DELETE_COOKIE_LAST_ENTRY - 1, DELETE_COOKIE_LAST_ENTRY,
2249 base::Histogram::kUmaTargetedHistogramFlag);
2251 // From UMA_HISTOGRAM_{CUSTOM_,}TIMES
2252 histogram_time_blocked_on_load_ = base::Histogram::FactoryTimeGet(
2253 "Cookie.TimeBlockedOnLoad",
2254 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
2255 50, base::Histogram::kUmaTargetedHistogramFlag);
2259 // The system resolution is not high enough, so we can have multiple
2260 // set cookies that result in the same system time. When this happens, we
2261 // increment by one Time unit. Let's hope computers don't get too fast.
2262 Time CookieMonster::CurrentTime() {
2263 return std::max(Time::Now(),
2264 Time::FromInternalValue(last_time_seen_.ToInternalValue() + 1));
2267 bool CookieMonster::CopyCookiesForKeyToOtherCookieMonster(
2268 std::string key,
2269 CookieMonster* other) {
2270 ScopedVector<CanonicalCookie> duplicated_cookies;
2273 base::AutoLock autolock(lock_);
2274 DCHECK(other);
2275 if (!loaded_)
2276 return false;
2278 for (CookieMapItPair its = cookies_.equal_range(key);
2279 its.first != its.second;
2280 ++its.first) {
2281 CookieMap::iterator curit = its.first;
2282 CanonicalCookie* cc = curit->second;
2284 duplicated_cookies.push_back(cc->Duplicate());
2289 base::AutoLock autolock(other->lock_);
2290 if (!other->loaded_)
2291 return false;
2293 // There must not exist any entries for the key to be copied in |other|.
2294 CookieMapItPair its = other->cookies_.equal_range(key);
2295 if (its.first != its.second)
2296 return false;
2298 // Store the copied cookies in |other|.
2299 for (ScopedVector<CanonicalCookie>::const_iterator it =
2300 duplicated_cookies.begin();
2301 it != duplicated_cookies.end();
2302 ++it) {
2303 other->InternalInsertCookie(key, *it, true);
2306 // Since the cookies are owned by |other| now, weak clear must be used.
2307 duplicated_cookies.weak_clear();
2310 return true;
2313 bool CookieMonster::loaded() {
2314 base::AutoLock autolock(lock_);
2315 return loaded_;
2318 scoped_ptr<CookieStore::CookieChangedSubscription>
2319 CookieMonster::AddCallbackForCookie(
2320 const GURL& gurl,
2321 const std::string& name,
2322 const CookieChangedCallback& callback) {
2323 base::AutoLock autolock(lock_);
2324 std::pair<GURL, std::string> key(gurl, name);
2325 if (hook_map_.count(key) == 0)
2326 hook_map_[key] = make_linked_ptr(new CookieChangedCallbackList());
2327 return hook_map_[key]->Add(
2328 base::Bind(&RunAsync, base::MessageLoopProxy::current(), callback));
2331 void CookieMonster::RunCallbacks(const CanonicalCookie& cookie, bool removed) {
2332 lock_.AssertAcquired();
2333 CookieOptions opts;
2334 opts.set_include_httponly();
2335 // Note that the callbacks in hook_map_ are wrapped with MakeAsync(), so they
2336 // are guaranteed to not take long - they just post a RunAsync task back to
2337 // the appropriate thread's message loop and return. It is important that this
2338 // method not run user-supplied callbacks directly, since the CookieMonster
2339 // lock is held and it is easy to accidentally introduce deadlocks.
2340 for (CookieChangedHookMap::iterator it = hook_map_.begin();
2341 it != hook_map_.end(); ++it) {
2342 std::pair<GURL, std::string> key = it->first;
2343 if (cookie.IncludeForRequestURL(key.first, opts) &&
2344 cookie.Name() == key.second) {
2345 it->second->Notify(cookie, removed);
2350 } // namespace net