Use PlaybackToMemory for BitmapRasterWorkerPool playback
[chromium-blink-merge.git] / net / cookies / cookie_monster.cc
blobcd9c9b8f34dafb148c79fc3fd58ac30ebfe95476
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 private:
556 // Runs the delete task and returns a result.
557 virtual Result RunDeleteTask() = 0;
558 base::Closure RunDeleteTaskAndBindCallback();
559 void FlushDone(const base::Closure& callback);
561 typename CallbackType<Result>::Type callback_;
563 DISALLOW_COPY_AND_ASSIGN(DeleteTask);
566 template <typename Result>
567 base::Closure CookieMonster::DeleteTask<Result>::
568 RunDeleteTaskAndBindCallback() {
569 Result result = RunDeleteTask();
570 if (callback_.is_null())
571 return base::Closure();
572 return base::Bind(callback_, result);
575 template <>
576 base::Closure CookieMonster::DeleteTask<void>::RunDeleteTaskAndBindCallback() {
577 RunDeleteTask();
578 return callback_;
581 template <typename Result>
582 void CookieMonster::DeleteTask<Result>::Run() {
583 this->cookie_monster()->FlushStore(
584 base::Bind(&DeleteTask<Result>::FlushDone, this,
585 RunDeleteTaskAndBindCallback()));
588 template <typename Result>
589 void CookieMonster::DeleteTask<Result>::FlushDone(
590 const base::Closure& callback) {
591 if (!callback.is_null()) {
592 this->InvokeCallback(callback);
596 // Task class for DeleteAll call.
597 class CookieMonster::DeleteAllTask : public DeleteTask<int> {
598 public:
599 DeleteAllTask(CookieMonster* cookie_monster,
600 const DeleteCallback& callback)
601 : DeleteTask<int>(cookie_monster, callback) {
604 // DeleteTask:
605 int RunDeleteTask() override;
607 protected:
608 ~DeleteAllTask() override {}
610 private:
611 DISALLOW_COPY_AND_ASSIGN(DeleteAllTask);
614 int CookieMonster::DeleteAllTask::RunDeleteTask() {
615 return this->cookie_monster()->DeleteAll(true);
618 // Task class for DeleteAllCreatedBetween call.
619 class CookieMonster::DeleteAllCreatedBetweenTask : public DeleteTask<int> {
620 public:
621 DeleteAllCreatedBetweenTask(CookieMonster* cookie_monster,
622 const Time& delete_begin,
623 const Time& delete_end,
624 const DeleteCallback& callback)
625 : DeleteTask<int>(cookie_monster, callback),
626 delete_begin_(delete_begin),
627 delete_end_(delete_end) {
630 // DeleteTask:
631 int RunDeleteTask() override;
633 protected:
634 ~DeleteAllCreatedBetweenTask() override {}
636 private:
637 Time delete_begin_;
638 Time delete_end_;
640 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask);
643 int CookieMonster::DeleteAllCreatedBetweenTask::RunDeleteTask() {
644 return this->cookie_monster()->
645 DeleteAllCreatedBetween(delete_begin_, delete_end_);
648 // Task class for DeleteAllForHost call.
649 class CookieMonster::DeleteAllForHostTask : public DeleteTask<int> {
650 public:
651 DeleteAllForHostTask(CookieMonster* cookie_monster,
652 const GURL& url,
653 const DeleteCallback& callback)
654 : DeleteTask<int>(cookie_monster, callback),
655 url_(url) {
658 // DeleteTask:
659 int RunDeleteTask() override;
661 protected:
662 ~DeleteAllForHostTask() override {}
664 private:
665 GURL url_;
667 DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask);
670 int CookieMonster::DeleteAllForHostTask::RunDeleteTask() {
671 return this->cookie_monster()->DeleteAllForHost(url_);
674 // Task class for DeleteAllCreatedBetweenForHost call.
675 class CookieMonster::DeleteAllCreatedBetweenForHostTask
676 : public DeleteTask<int> {
677 public:
678 DeleteAllCreatedBetweenForHostTask(
679 CookieMonster* cookie_monster,
680 Time delete_begin,
681 Time delete_end,
682 const GURL& url,
683 const DeleteCallback& callback)
684 : DeleteTask<int>(cookie_monster, callback),
685 delete_begin_(delete_begin),
686 delete_end_(delete_end),
687 url_(url) {
690 // DeleteTask:
691 int RunDeleteTask() override;
693 protected:
694 ~DeleteAllCreatedBetweenForHostTask() override {}
696 private:
697 Time delete_begin_;
698 Time delete_end_;
699 GURL url_;
701 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenForHostTask);
704 int CookieMonster::DeleteAllCreatedBetweenForHostTask::RunDeleteTask() {
705 return this->cookie_monster()->DeleteAllCreatedBetweenForHost(
706 delete_begin_, delete_end_, url_);
709 // Task class for DeleteCanonicalCookie call.
710 class CookieMonster::DeleteCanonicalCookieTask : public DeleteTask<bool> {
711 public:
712 DeleteCanonicalCookieTask(CookieMonster* cookie_monster,
713 const CanonicalCookie& cookie,
714 const DeleteCookieCallback& callback)
715 : DeleteTask<bool>(cookie_monster, callback),
716 cookie_(cookie) {
719 // DeleteTask:
720 bool RunDeleteTask() override;
722 protected:
723 ~DeleteCanonicalCookieTask() override {}
725 private:
726 CanonicalCookie cookie_;
728 DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask);
731 bool CookieMonster::DeleteCanonicalCookieTask::RunDeleteTask() {
732 return this->cookie_monster()->DeleteCanonicalCookie(cookie_);
735 // Task class for SetCookieWithOptions call.
736 class CookieMonster::SetCookieWithOptionsTask : public CookieMonsterTask {
737 public:
738 SetCookieWithOptionsTask(CookieMonster* cookie_monster,
739 const GURL& url,
740 const std::string& cookie_line,
741 const CookieOptions& options,
742 const SetCookiesCallback& callback)
743 : CookieMonsterTask(cookie_monster),
744 url_(url),
745 cookie_line_(cookie_line),
746 options_(options),
747 callback_(callback) {
750 // CookieMonsterTask:
751 void Run() override;
753 protected:
754 ~SetCookieWithOptionsTask() override {}
756 private:
757 GURL url_;
758 std::string cookie_line_;
759 CookieOptions options_;
760 SetCookiesCallback callback_;
762 DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask);
765 void CookieMonster::SetCookieWithOptionsTask::Run() {
766 bool result = this->cookie_monster()->
767 SetCookieWithOptions(url_, cookie_line_, options_);
768 if (!callback_.is_null()) {
769 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run,
770 base::Unretained(&callback_), result));
774 // Task class for GetCookiesWithOptions call.
775 class CookieMonster::GetCookiesWithOptionsTask : public CookieMonsterTask {
776 public:
777 GetCookiesWithOptionsTask(CookieMonster* cookie_monster,
778 const GURL& url,
779 const CookieOptions& options,
780 const GetCookiesCallback& callback)
781 : CookieMonsterTask(cookie_monster),
782 url_(url),
783 options_(options),
784 callback_(callback) {
787 // CookieMonsterTask:
788 void Run() override;
790 protected:
791 ~GetCookiesWithOptionsTask() override {}
793 private:
794 GURL url_;
795 CookieOptions options_;
796 GetCookiesCallback callback_;
798 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask);
801 void CookieMonster::GetCookiesWithOptionsTask::Run() {
802 std::string cookie = this->cookie_monster()->
803 GetCookiesWithOptions(url_, options_);
804 if (!callback_.is_null()) {
805 this->InvokeCallback(base::Bind(&GetCookiesCallback::Run,
806 base::Unretained(&callback_), cookie));
810 // Task class for DeleteCookie call.
811 class CookieMonster::DeleteCookieTask : public DeleteTask<void> {
812 public:
813 DeleteCookieTask(CookieMonster* cookie_monster,
814 const GURL& url,
815 const std::string& cookie_name,
816 const base::Closure& callback)
817 : DeleteTask<void>(cookie_monster, callback),
818 url_(url),
819 cookie_name_(cookie_name) {
822 // DeleteTask:
823 void RunDeleteTask() override;
825 protected:
826 ~DeleteCookieTask() override {}
828 private:
829 GURL url_;
830 std::string cookie_name_;
832 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
835 void CookieMonster::DeleteCookieTask::RunDeleteTask() {
836 this->cookie_monster()->DeleteCookie(url_, cookie_name_);
839 // Task class for DeleteSessionCookies call.
840 class CookieMonster::DeleteSessionCookiesTask : public DeleteTask<int> {
841 public:
842 DeleteSessionCookiesTask(CookieMonster* cookie_monster,
843 const DeleteCallback& callback)
844 : DeleteTask<int>(cookie_monster, callback) {
847 // DeleteTask:
848 int RunDeleteTask() override;
850 protected:
851 ~DeleteSessionCookiesTask() override {}
853 private:
854 DISALLOW_COPY_AND_ASSIGN(DeleteSessionCookiesTask);
857 int CookieMonster::DeleteSessionCookiesTask::RunDeleteTask() {
858 return this->cookie_monster()->DeleteSessionCookies();
861 // Task class for HasCookiesForETLDP1Task call.
862 class CookieMonster::HasCookiesForETLDP1Task : public CookieMonsterTask {
863 public:
864 HasCookiesForETLDP1Task(
865 CookieMonster* cookie_monster,
866 const std::string& etldp1,
867 const HasCookiesForETLDP1Callback& callback)
868 : CookieMonsterTask(cookie_monster),
869 etldp1_(etldp1),
870 callback_(callback) {
873 // CookieMonsterTask:
874 void Run() override;
876 protected:
877 ~HasCookiesForETLDP1Task() override {}
879 private:
880 std::string etldp1_;
881 HasCookiesForETLDP1Callback callback_;
883 DISALLOW_COPY_AND_ASSIGN(HasCookiesForETLDP1Task);
886 void CookieMonster::HasCookiesForETLDP1Task::Run() {
887 bool result = this->cookie_monster()->HasCookiesForETLDP1(etldp1_);
888 if (!callback_.is_null()) {
889 this->InvokeCallback(
890 base::Bind(&HasCookiesForETLDP1Callback::Run,
891 base::Unretained(&callback_), result));
895 // Asynchronous CookieMonster API
897 void CookieMonster::SetCookieWithDetailsAsync(
898 const GURL& url,
899 const std::string& name,
900 const std::string& value,
901 const std::string& domain,
902 const std::string& path,
903 const Time& expiration_time,
904 bool secure,
905 bool http_only,
906 CookiePriority priority,
907 const SetCookiesCallback& callback) {
908 scoped_refptr<SetCookieWithDetailsTask> task =
909 new SetCookieWithDetailsTask(this, url, name, value, domain, path,
910 expiration_time, secure, http_only, priority,
911 callback);
912 DoCookieTaskForURL(task, url);
915 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) {
916 scoped_refptr<GetAllCookiesTask> task =
917 new GetAllCookiesTask(this, callback);
919 DoCookieTask(task);
923 void CookieMonster::GetAllCookiesForURLWithOptionsAsync(
924 const GURL& url,
925 const CookieOptions& options,
926 const GetCookieListCallback& callback) {
927 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
928 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
930 DoCookieTaskForURL(task, url);
933 void CookieMonster::GetAllCookiesForURLAsync(
934 const GURL& url, const GetCookieListCallback& callback) {
935 CookieOptions options;
936 options.set_include_httponly();
937 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
938 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
940 DoCookieTaskForURL(task, url);
943 void CookieMonster::HasCookiesForETLDP1Async(
944 const std::string& etldp1,
945 const HasCookiesForETLDP1Callback& callback) {
946 scoped_refptr<HasCookiesForETLDP1Task> task =
947 new HasCookiesForETLDP1Task(this, etldp1, callback);
949 DoCookieTaskForURL(task, GURL("http://" + etldp1));
952 void CookieMonster::DeleteAllAsync(const DeleteCallback& callback) {
953 scoped_refptr<DeleteAllTask> task =
954 new DeleteAllTask(this, callback);
956 DoCookieTask(task);
959 void CookieMonster::DeleteAllCreatedBetweenAsync(
960 const Time& delete_begin, const Time& delete_end,
961 const DeleteCallback& callback) {
962 scoped_refptr<DeleteAllCreatedBetweenTask> task =
963 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end,
964 callback);
966 DoCookieTask(task);
969 void CookieMonster::DeleteAllCreatedBetweenForHostAsync(
970 const Time delete_begin,
971 const Time delete_end,
972 const GURL& url,
973 const DeleteCallback& callback) {
974 scoped_refptr<DeleteAllCreatedBetweenForHostTask> task =
975 new DeleteAllCreatedBetweenForHostTask(
976 this, delete_begin, delete_end, url, callback);
978 DoCookieTaskForURL(task, url);
981 void CookieMonster::DeleteAllForHostAsync(
982 const GURL& url, const DeleteCallback& callback) {
983 scoped_refptr<DeleteAllForHostTask> task =
984 new DeleteAllForHostTask(this, url, callback);
986 DoCookieTaskForURL(task, url);
989 void CookieMonster::DeleteCanonicalCookieAsync(
990 const CanonicalCookie& cookie,
991 const DeleteCookieCallback& callback) {
992 scoped_refptr<DeleteCanonicalCookieTask> task =
993 new DeleteCanonicalCookieTask(this, cookie, callback);
995 DoCookieTask(task);
998 void CookieMonster::SetCookieWithOptionsAsync(
999 const GURL& url,
1000 const std::string& cookie_line,
1001 const CookieOptions& options,
1002 const SetCookiesCallback& callback) {
1003 scoped_refptr<SetCookieWithOptionsTask> task =
1004 new SetCookieWithOptionsTask(this, url, cookie_line, options, callback);
1006 DoCookieTaskForURL(task, url);
1009 void CookieMonster::GetCookiesWithOptionsAsync(
1010 const GURL& url,
1011 const CookieOptions& options,
1012 const GetCookiesCallback& callback) {
1013 scoped_refptr<GetCookiesWithOptionsTask> task =
1014 new GetCookiesWithOptionsTask(this, url, options, callback);
1016 DoCookieTaskForURL(task, url);
1019 void CookieMonster::DeleteCookieAsync(const GURL& url,
1020 const std::string& cookie_name,
1021 const base::Closure& callback) {
1022 scoped_refptr<DeleteCookieTask> task =
1023 new DeleteCookieTask(this, url, cookie_name, callback);
1025 DoCookieTaskForURL(task, url);
1028 void CookieMonster::DeleteSessionCookiesAsync(
1029 const CookieStore::DeleteCallback& callback) {
1030 scoped_refptr<DeleteSessionCookiesTask> task =
1031 new DeleteSessionCookiesTask(this, callback);
1033 DoCookieTask(task);
1036 void CookieMonster::DoCookieTask(
1037 const scoped_refptr<CookieMonsterTask>& task_item) {
1039 base::AutoLock autolock(lock_);
1040 InitIfNecessary();
1041 if (!loaded_) {
1042 tasks_pending_.push(task_item);
1043 return;
1047 task_item->Run();
1050 void CookieMonster::DoCookieTaskForURL(
1051 const scoped_refptr<CookieMonsterTask>& task_item,
1052 const GURL& url) {
1054 base::AutoLock autolock(lock_);
1055 InitIfNecessary();
1056 // If cookies for the requested domain key (eTLD+1) have been loaded from DB
1057 // then run the task, otherwise load from DB.
1058 if (!loaded_) {
1059 // Checks if the domain key has been loaded.
1060 std::string key(cookie_util::GetEffectiveDomain(url.scheme(),
1061 url.host()));
1062 if (keys_loaded_.find(key) == keys_loaded_.end()) {
1063 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > >
1064 ::iterator it = tasks_pending_for_key_.find(key);
1065 if (it == tasks_pending_for_key_.end()) {
1066 store_->LoadCookiesForKey(key,
1067 base::Bind(&CookieMonster::OnKeyLoaded, this, key));
1068 it = tasks_pending_for_key_.insert(std::make_pair(key,
1069 std::deque<scoped_refptr<CookieMonsterTask> >())).first;
1071 it->second.push_back(task_item);
1072 return;
1076 task_item->Run();
1079 bool CookieMonster::SetCookieWithDetails(const GURL& url,
1080 const std::string& name,
1081 const std::string& value,
1082 const std::string& domain,
1083 const std::string& path,
1084 const base::Time& expiration_time,
1085 bool secure,
1086 bool http_only,
1087 CookiePriority priority) {
1088 base::AutoLock autolock(lock_);
1090 if (!HasCookieableScheme(url))
1091 return false;
1093 Time creation_time = CurrentTime();
1094 last_time_seen_ = creation_time;
1096 scoped_ptr<CanonicalCookie> cc;
1097 cc.reset(CanonicalCookie::Create(url, name, value, domain, path,
1098 creation_time, expiration_time,
1099 secure, http_only, priority));
1101 if (!cc.get())
1102 return false;
1104 CookieOptions options;
1105 options.set_include_httponly();
1106 return SetCanonicalCookie(&cc, creation_time, options);
1109 bool CookieMonster::ImportCookies(const CookieList& list) {
1110 base::AutoLock autolock(lock_);
1111 InitIfNecessary();
1112 for (net::CookieList::const_iterator iter = list.begin();
1113 iter != list.end(); ++iter) {
1114 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie(*iter));
1115 net::CookieOptions options;
1116 options.set_include_httponly();
1117 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), options))
1118 return false;
1120 return true;
1123 CookieList CookieMonster::GetAllCookies() {
1124 base::AutoLock autolock(lock_);
1126 // This function is being called to scrape the cookie list for management UI
1127 // or similar. We shouldn't show expired cookies in this list since it will
1128 // just be confusing to users, and this function is called rarely enough (and
1129 // is already slow enough) that it's OK to take the time to garbage collect
1130 // the expired cookies now.
1132 // Note that this does not prune cookies to be below our limits (if we've
1133 // exceeded them) the way that calling GarbageCollect() would.
1134 GarbageCollectExpired(Time::Now(),
1135 CookieMapItPair(cookies_.begin(), cookies_.end()),
1136 NULL);
1138 // Copy the CanonicalCookie pointers from the map so that we can use the same
1139 // sorter as elsewhere, then copy the result out.
1140 std::vector<CanonicalCookie*> cookie_ptrs;
1141 cookie_ptrs.reserve(cookies_.size());
1142 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end(); ++it)
1143 cookie_ptrs.push_back(it->second);
1144 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
1146 CookieList cookie_list;
1147 cookie_list.reserve(cookie_ptrs.size());
1148 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
1149 it != cookie_ptrs.end(); ++it)
1150 cookie_list.push_back(**it);
1152 return cookie_list;
1155 CookieList CookieMonster::GetAllCookiesForURLWithOptions(
1156 const GURL& url,
1157 const CookieOptions& options) {
1158 base::AutoLock autolock(lock_);
1160 std::vector<CanonicalCookie*> cookie_ptrs;
1161 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs);
1162 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
1164 CookieList cookies;
1165 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
1166 it != cookie_ptrs.end(); it++)
1167 cookies.push_back(**it);
1169 return cookies;
1172 CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) {
1173 CookieOptions options;
1174 options.set_include_httponly();
1176 return GetAllCookiesForURLWithOptions(url, options);
1179 int CookieMonster::DeleteAll(bool sync_to_store) {
1180 base::AutoLock autolock(lock_);
1182 int num_deleted = 0;
1183 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1184 CookieMap::iterator curit = it;
1185 ++it;
1186 InternalDeleteCookie(curit, sync_to_store,
1187 sync_to_store ? DELETE_COOKIE_EXPLICIT :
1188 DELETE_COOKIE_DONT_RECORD /* Destruction. */);
1189 ++num_deleted;
1192 return num_deleted;
1195 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin,
1196 const Time& delete_end) {
1197 base::AutoLock autolock(lock_);
1199 int num_deleted = 0;
1200 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1201 CookieMap::iterator curit = it;
1202 CanonicalCookie* cc = curit->second;
1203 ++it;
1205 if (cc->CreationDate() >= delete_begin &&
1206 (delete_end.is_null() || cc->CreationDate() < delete_end)) {
1207 InternalDeleteCookie(curit,
1208 true, /*sync_to_store*/
1209 DELETE_COOKIE_EXPLICIT);
1210 ++num_deleted;
1214 return num_deleted;
1217 int CookieMonster::DeleteAllCreatedBetweenForHost(const Time delete_begin,
1218 const Time delete_end,
1219 const GURL& url) {
1220 base::AutoLock autolock(lock_);
1222 if (!HasCookieableScheme(url))
1223 return 0;
1225 const std::string host(url.host());
1227 // We store host cookies in the store by their canonical host name;
1228 // domain cookies are stored with a leading ".". So this is a pretty
1229 // simple lookup and per-cookie delete.
1230 int num_deleted = 0;
1231 for (CookieMapItPair its = cookies_.equal_range(GetKey(host));
1232 its.first != its.second;) {
1233 CookieMap::iterator curit = its.first;
1234 ++its.first;
1236 const CanonicalCookie* const cc = curit->second;
1238 // Delete only on a match as a host cookie.
1239 if (cc->IsHostCookie() && cc->IsDomainMatch(host) &&
1240 cc->CreationDate() >= delete_begin &&
1241 // The assumption that null |delete_end| is equivalent to
1242 // Time::Max() is confusing.
1243 (delete_end.is_null() || cc->CreationDate() < delete_end)) {
1244 num_deleted++;
1246 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT);
1249 return num_deleted;
1252 int CookieMonster::DeleteAllForHost(const GURL& url) {
1253 return DeleteAllCreatedBetweenForHost(Time(), Time::Max(), url);
1257 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) {
1258 base::AutoLock autolock(lock_);
1260 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain()));
1261 its.first != its.second; ++its.first) {
1262 // The creation date acts as our unique index...
1263 if (its.first->second->CreationDate() == cookie.CreationDate()) {
1264 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT);
1265 return true;
1268 return false;
1271 void CookieMonster::SetCookieableSchemes(const char* const schemes[],
1272 size_t num_schemes) {
1273 base::AutoLock autolock(lock_);
1275 // Cookieable Schemes must be set before first use of function.
1276 DCHECK(!initialized_);
1278 cookieable_schemes_.clear();
1279 cookieable_schemes_.insert(cookieable_schemes_.end(),
1280 schemes, schemes + num_schemes);
1283 void CookieMonster::SetEnableFileScheme(bool accept) {
1284 // This assumes "file" is always at the end of the array. See the comment
1285 // above kDefaultCookieableSchemes.
1286 int num_schemes = accept ? kDefaultCookieableSchemesCount :
1287 kDefaultCookieableSchemesCount - 1;
1288 SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes);
1291 void CookieMonster::SetKeepExpiredCookies() {
1292 keep_expired_cookies_ = true;
1295 void CookieMonster::FlushStore(const base::Closure& callback) {
1296 base::AutoLock autolock(lock_);
1297 if (initialized_ && store_.get())
1298 store_->Flush(callback);
1299 else if (!callback.is_null())
1300 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
1303 bool CookieMonster::SetCookieWithOptions(const GURL& url,
1304 const std::string& cookie_line,
1305 const CookieOptions& options) {
1306 base::AutoLock autolock(lock_);
1308 if (!HasCookieableScheme(url)) {
1309 return false;
1312 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options);
1315 std::string CookieMonster::GetCookiesWithOptions(const GURL& url,
1316 const CookieOptions& options) {
1317 base::AutoLock autolock(lock_);
1319 if (!HasCookieableScheme(url))
1320 return std::string();
1322 std::vector<CanonicalCookie*> cookies;
1323 FindCookiesForHostAndDomain(url, options, true, &cookies);
1324 std::sort(cookies.begin(), cookies.end(), CookieSorter);
1326 std::string cookie_line = BuildCookieLine(cookies);
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* const 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);
1783 RunCallbacks(*cc, false);
1785 return inserted;
1788 bool CookieMonster::SetCookieWithCreationTimeAndOptions(
1789 const GURL& url,
1790 const std::string& cookie_line,
1791 const Time& creation_time_or_null,
1792 const CookieOptions& options) {
1793 lock_.AssertAcquired();
1795 VLOG(kVlogSetCookies) << "SetCookie() line: " << cookie_line;
1797 Time creation_time = creation_time_or_null;
1798 if (creation_time.is_null()) {
1799 creation_time = CurrentTime();
1800 last_time_seen_ = creation_time;
1803 scoped_ptr<CanonicalCookie> cc(
1804 CanonicalCookie::Create(url, cookie_line, creation_time, options));
1806 if (!cc.get()) {
1807 VLOG(kVlogSetCookies) << "WARNING: Failed to allocate CanonicalCookie";
1808 return false;
1810 return SetCanonicalCookie(&cc, creation_time, options);
1813 bool CookieMonster::SetCanonicalCookie(scoped_ptr<CanonicalCookie>* cc,
1814 const Time& creation_time,
1815 const CookieOptions& options) {
1816 const std::string key(GetKey((*cc)->Domain()));
1817 bool already_expired = (*cc)->IsExpired(creation_time);
1819 if (DeleteAnyEquivalentCookie(key, **cc, options.exclude_httponly(),
1820 already_expired)) {
1821 VLOG(kVlogSetCookies) << "SetCookie() not clobbering httponly cookie";
1822 return false;
1825 VLOG(kVlogSetCookies) << "SetCookie() key: " << key << " cc: "
1826 << (*cc)->DebugString();
1828 // Realize that we might be setting an expired cookie, and the only point
1829 // was to delete the cookie which we've already done.
1830 if (!already_expired || keep_expired_cookies_) {
1831 // See InitializeHistograms() for details.
1832 if ((*cc)->IsPersistent()) {
1833 histogram_expiration_duration_minutes_->Add(
1834 ((*cc)->ExpiryDate() - creation_time).InMinutes());
1838 CanonicalCookie cookie = *(cc->get());
1839 InternalInsertCookie(key, cc->release(), true);
1841 } else {
1842 VLOG(kVlogSetCookies) << "SetCookie() not storing already expired cookie.";
1845 // We assume that hopefully setting a cookie will be less common than
1846 // querying a cookie. Since setting a cookie can put us over our limits,
1847 // make sure that we garbage collect... We can also make the assumption that
1848 // if a cookie was set, in the common case it will be used soon after,
1849 // and we will purge the expired cookies in GetCookies().
1850 GarbageCollect(creation_time, key);
1852 return true;
1855 void CookieMonster::InternalUpdateCookieAccessTime(CanonicalCookie* cc,
1856 const Time& current) {
1857 lock_.AssertAcquired();
1859 // Based off the Mozilla code. When a cookie has been accessed recently,
1860 // don't bother updating its access time again. This reduces the number of
1861 // updates we do during pageload, which in turn reduces the chance our storage
1862 // backend will hit its batch thresholds and be forced to update.
1863 if ((current - cc->LastAccessDate()) < last_access_threshold_)
1864 return;
1866 // See InitializeHistograms() for details.
1867 histogram_between_access_interval_minutes_->Add(
1868 (current - cc->LastAccessDate()).InMinutes());
1870 cc->SetLastAccessDate(current);
1871 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get())
1872 store_->UpdateCookieAccessTime(*cc);
1875 // InternalDeleteCookies must not invalidate iterators other than the one being
1876 // deleted.
1877 void CookieMonster::InternalDeleteCookie(CookieMap::iterator it,
1878 bool sync_to_store,
1879 DeletionCause deletion_cause) {
1880 lock_.AssertAcquired();
1882 // Ideally, this would be asserted up where we define ChangeCauseMapping,
1883 // but DeletionCause's visibility (or lack thereof) forces us to make
1884 // this check here.
1885 COMPILE_ASSERT(arraysize(ChangeCauseMapping) == DELETE_COOKIE_LAST_ENTRY + 1,
1886 ChangeCauseMapping_size_not_eq_DeletionCause_enum_size);
1888 // See InitializeHistograms() for details.
1889 if (deletion_cause != DELETE_COOKIE_DONT_RECORD)
1890 histogram_cookie_deletion_cause_->Add(deletion_cause);
1892 CanonicalCookie* cc = it->second;
1893 VLOG(kVlogSetCookies) << "InternalDeleteCookie() cc: " << cc->DebugString();
1895 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() &&
1896 sync_to_store)
1897 store_->DeleteCookie(*cc);
1898 if (delegate_.get()) {
1899 ChangeCausePair mapping = ChangeCauseMapping[deletion_cause];
1901 if (mapping.notify)
1902 delegate_->OnCookieChanged(*cc, true, mapping.cause);
1904 RunCallbacks(*cc, true);
1905 cookies_.erase(it);
1906 delete cc;
1909 // Domain expiry behavior is unchanged by key/expiry scheme (the
1910 // meaning of the key is different, but that's not visible to this routine).
1911 int CookieMonster::GarbageCollect(const Time& current,
1912 const std::string& key) {
1913 lock_.AssertAcquired();
1915 int num_deleted = 0;
1916 Time safe_date(
1917 Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays));
1919 // Collect garbage for this key, minding cookie priorities.
1920 if (cookies_.count(key) > kDomainMaxCookies) {
1921 VLOG(kVlogGarbageCollection) << "GarbageCollect() key: " << key;
1923 CookieItVector cookie_its;
1924 num_deleted += GarbageCollectExpired(
1925 current, cookies_.equal_range(key), &cookie_its);
1926 if (cookie_its.size() > kDomainMaxCookies) {
1927 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect domain.";
1928 size_t purge_goal =
1929 cookie_its.size() - (kDomainMaxCookies - kDomainPurgeCookies);
1930 DCHECK(purge_goal > kDomainPurgeCookies);
1932 // Boundary iterators into |cookie_its| for different priorities.
1933 CookieItVector::iterator it_bdd[4];
1934 // Intialize |it_bdd| while sorting |cookie_its| by priorities.
1935 // Schematic: [MLLHMHHLMM] => [LLL|MMMM|HHH], with 4 boundaries.
1936 it_bdd[0] = cookie_its.begin();
1937 it_bdd[3] = cookie_its.end();
1938 it_bdd[1] = PartitionCookieByPriority(it_bdd[0], it_bdd[3],
1939 COOKIE_PRIORITY_LOW);
1940 it_bdd[2] = PartitionCookieByPriority(it_bdd[1], it_bdd[3],
1941 COOKIE_PRIORITY_MEDIUM);
1942 size_t quota[3] = {
1943 kDomainCookiesQuotaLow,
1944 kDomainCookiesQuotaMedium,
1945 kDomainCookiesQuotaHigh
1948 // Purge domain cookies in 3 rounds.
1949 // Round 1: consider low-priority cookies only: evict least-recently
1950 // accessed, while protecting quota[0] of these from deletion.
1951 // Round 2: consider {low, medium}-priority cookies, evict least-recently
1952 // accessed, while protecting quota[0] + quota[1].
1953 // Round 3: consider all cookies, evict least-recently accessed.
1954 size_t accumulated_quota = 0;
1955 CookieItVector::iterator it_purge_begin = it_bdd[0];
1956 for (int i = 0; i < 3 && purge_goal > 0; ++i) {
1957 accumulated_quota += quota[i];
1959 size_t num_considered = it_bdd[i + 1] - it_purge_begin;
1960 if (num_considered <= accumulated_quota)
1961 continue;
1963 // Number of cookies that will be purged in this round.
1964 size_t round_goal =
1965 std::min(purge_goal, num_considered - accumulated_quota);
1966 purge_goal -= round_goal;
1968 SortLeastRecentlyAccessed(it_purge_begin, it_bdd[i + 1], round_goal);
1969 // Cookies accessed on or after |safe_date| would have been safe from
1970 // global purge, and we want to keep track of this.
1971 CookieItVector::iterator it_purge_end = it_purge_begin + round_goal;
1972 CookieItVector::iterator it_purge_middle =
1973 LowerBoundAccessDate(it_purge_begin, it_purge_end, safe_date);
1974 // Delete cookies accessed before |safe_date|.
1975 num_deleted += GarbageCollectDeleteRange(
1976 current,
1977 DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE,
1978 it_purge_begin,
1979 it_purge_middle);
1980 // Delete cookies accessed on or after |safe_date|.
1981 num_deleted += GarbageCollectDeleteRange(
1982 current,
1983 DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE,
1984 it_purge_middle,
1985 it_purge_end);
1986 it_purge_begin = it_purge_end;
1988 DCHECK_EQ(0U, purge_goal);
1992 // Collect garbage for everything. With firefox style we want to preserve
1993 // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict.
1994 if (cookies_.size() > kMaxCookies &&
1995 earliest_access_time_ < safe_date) {
1996 VLOG(kVlogGarbageCollection) << "GarbageCollect() everything";
1997 CookieItVector cookie_its;
1998 num_deleted += GarbageCollectExpired(
1999 current, CookieMapItPair(cookies_.begin(), cookies_.end()),
2000 &cookie_its);
2001 if (cookie_its.size() > kMaxCookies) {
2002 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect everything.";
2003 size_t purge_goal = cookie_its.size() - (kMaxCookies - kPurgeCookies);
2004 DCHECK(purge_goal > kPurgeCookies);
2005 // Sorts up to *and including* |cookie_its[purge_goal]|, so
2006 // |earliest_access_time| will be properly assigned even if
2007 // |global_purge_it| == |cookie_its.begin() + purge_goal|.
2008 SortLeastRecentlyAccessed(cookie_its.begin(), cookie_its.end(),
2009 purge_goal);
2010 // Find boundary to cookies older than safe_date.
2011 CookieItVector::iterator global_purge_it =
2012 LowerBoundAccessDate(cookie_its.begin(),
2013 cookie_its.begin() + purge_goal,
2014 safe_date);
2015 // Only delete the old cookies.
2016 num_deleted += GarbageCollectDeleteRange(
2017 current,
2018 DELETE_COOKIE_EVICTED_GLOBAL,
2019 cookie_its.begin(),
2020 global_purge_it);
2021 // Set access day to the oldest cookie that wasn't deleted.
2022 earliest_access_time_ = (*global_purge_it)->second->LastAccessDate();
2026 return num_deleted;
2029 int CookieMonster::GarbageCollectExpired(
2030 const Time& current,
2031 const CookieMapItPair& itpair,
2032 CookieItVector* cookie_its) {
2033 if (keep_expired_cookies_)
2034 return 0;
2036 lock_.AssertAcquired();
2038 int num_deleted = 0;
2039 for (CookieMap::iterator it = itpair.first, end = itpair.second; it != end;) {
2040 CookieMap::iterator curit = it;
2041 ++it;
2043 if (curit->second->IsExpired(current)) {
2044 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED);
2045 ++num_deleted;
2046 } else if (cookie_its) {
2047 cookie_its->push_back(curit);
2051 return num_deleted;
2054 int CookieMonster::GarbageCollectDeleteRange(
2055 const Time& current,
2056 DeletionCause cause,
2057 CookieItVector::iterator it_begin,
2058 CookieItVector::iterator it_end) {
2059 for (CookieItVector::iterator it = it_begin; it != it_end; it++) {
2060 histogram_evicted_last_access_minutes_->Add(
2061 (current - (*it)->second->LastAccessDate()).InMinutes());
2062 InternalDeleteCookie((*it), true, cause);
2064 return it_end - it_begin;
2067 // A wrapper around registry_controlled_domains::GetDomainAndRegistry
2068 // to make clear we're creating a key for our local map. Here and
2069 // in FindCookiesForHostAndDomain() are the only two places where
2070 // we need to conditionalize based on key type.
2072 // Note that this key algorithm explicitly ignores the scheme. This is
2073 // because when we're entering cookies into the map from the backing store,
2074 // we in general won't have the scheme at that point.
2075 // In practical terms, this means that file cookies will be stored
2076 // in the map either by an empty string or by UNC name (and will be
2077 // limited by kMaxCookiesPerHost), and extension cookies will be stored
2078 // based on the single extension id, as the extension id won't have the
2079 // form of a DNS host and hence GetKey() will return it unchanged.
2081 // Arguably the right thing to do here is to make the key
2082 // algorithm dependent on the scheme, and make sure that the scheme is
2083 // available everywhere the key must be obtained (specfically at backing
2084 // store load time). This would require either changing the backing store
2085 // database schema to include the scheme (far more trouble than it's worth), or
2086 // separating out file cookies into their own CookieMonster instance and
2087 // thus restricting each scheme to a single cookie monster (which might
2088 // be worth it, but is still too much trouble to solve what is currently a
2089 // non-problem).
2090 std::string CookieMonster::GetKey(const std::string& domain) const {
2091 std::string effective_domain(
2092 registry_controlled_domains::GetDomainAndRegistry(
2093 domain, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
2094 if (effective_domain.empty())
2095 effective_domain = domain;
2097 if (!effective_domain.empty() && effective_domain[0] == '.')
2098 return effective_domain.substr(1);
2099 return effective_domain;
2102 bool CookieMonster::IsCookieableScheme(const std::string& scheme) {
2103 base::AutoLock autolock(lock_);
2105 return std::find(cookieable_schemes_.begin(), cookieable_schemes_.end(),
2106 scheme) != cookieable_schemes_.end();
2109 bool CookieMonster::HasCookieableScheme(const GURL& url) {
2110 lock_.AssertAcquired();
2112 // Make sure the request is on a cookie-able url scheme.
2113 for (size_t i = 0; i < cookieable_schemes_.size(); ++i) {
2114 // We matched a scheme.
2115 if (url.SchemeIs(cookieable_schemes_[i].c_str())) {
2116 // We've matched a supported scheme.
2117 return true;
2121 // The scheme didn't match any in our whitelist.
2122 VLOG(kVlogPerCookieMonster) << "WARNING: Unsupported cookie scheme: "
2123 << url.scheme();
2124 return false;
2127 // Test to see if stats should be recorded, and record them if so.
2128 // The goal here is to get sampling for the average browser-hour of
2129 // activity. We won't take samples when the web isn't being surfed,
2130 // and when the web is being surfed, we'll take samples about every
2131 // kRecordStatisticsIntervalSeconds.
2132 // last_statistic_record_time_ is initialized to Now() rather than null
2133 // in the constructor so that we won't take statistics right after
2134 // startup, to avoid bias from browsers that are started but not used.
2135 void CookieMonster::RecordPeriodicStats(const base::Time& current_time) {
2136 const base::TimeDelta kRecordStatisticsIntervalTime(
2137 base::TimeDelta::FromSeconds(kRecordStatisticsIntervalSeconds));
2139 // If we've taken statistics recently, return.
2140 if (current_time - last_statistic_record_time_ <=
2141 kRecordStatisticsIntervalTime) {
2142 return;
2145 // See InitializeHistograms() for details.
2146 histogram_count_->Add(cookies_.size());
2148 // More detailed statistics on cookie counts at different granularities.
2149 TimeTicks beginning_of_time(TimeTicks::Now());
2151 for (CookieMap::const_iterator it_key = cookies_.begin();
2152 it_key != cookies_.end(); ) {
2153 const std::string& key(it_key->first);
2155 int key_count = 0;
2156 typedef std::map<std::string, unsigned int> DomainMap;
2157 DomainMap domain_map;
2158 CookieMapItPair its_cookies = cookies_.equal_range(key);
2159 while (its_cookies.first != its_cookies.second) {
2160 key_count++;
2161 const std::string& cookie_domain(its_cookies.first->second->Domain());
2162 domain_map[cookie_domain]++;
2164 its_cookies.first++;
2166 histogram_etldp1_count_->Add(key_count);
2167 histogram_domain_per_etldp1_count_->Add(domain_map.size());
2168 for (DomainMap::const_iterator domain_map_it = domain_map.begin();
2169 domain_map_it != domain_map.end(); domain_map_it++)
2170 histogram_domain_count_->Add(domain_map_it->second);
2172 it_key = its_cookies.second;
2175 VLOG(kVlogPeriodic)
2176 << "Time for recording cookie stats (us): "
2177 << (TimeTicks::Now() - beginning_of_time).InMicroseconds();
2179 last_statistic_record_time_ = current_time;
2182 // Initialize all histogram counter variables used in this class.
2184 // Normal histogram usage involves using the macros defined in
2185 // histogram.h, which automatically takes care of declaring these
2186 // variables (as statics), initializing them, and accumulating into
2187 // them, all from a single entry point. Unfortunately, that solution
2188 // doesn't work for the CookieMonster, as it's vulnerable to races between
2189 // separate threads executing the same functions and hence initializing the
2190 // same static variables. There isn't a race danger in the histogram
2191 // accumulation calls; they are written to be resilient to simultaneous
2192 // calls from multiple threads.
2194 // The solution taken here is to have per-CookieMonster instance
2195 // variables that are constructed during CookieMonster construction.
2196 // Note that these variables refer to the same underlying histogram,
2197 // so we still race (but safely) with other CookieMonster instances
2198 // for accumulation.
2200 // To do this we've expanded out the individual histogram macros calls,
2201 // with declarations of the variables in the class decl, initialization here
2202 // (done from the class constructor) and direct calls to the accumulation
2203 // methods where needed. The specific histogram macro calls on which the
2204 // initialization is based are included in comments below.
2205 void CookieMonster::InitializeHistograms() {
2206 // From UMA_HISTOGRAM_CUSTOM_COUNTS
2207 histogram_expiration_duration_minutes_ = base::Histogram::FactoryGet(
2208 "Cookie.ExpirationDurationMinutes",
2209 1, kMinutesInTenYears, 50,
2210 base::Histogram::kUmaTargetedHistogramFlag);
2211 histogram_between_access_interval_minutes_ = base::Histogram::FactoryGet(
2212 "Cookie.BetweenAccessIntervalMinutes",
2213 1, kMinutesInTenYears, 50,
2214 base::Histogram::kUmaTargetedHistogramFlag);
2215 histogram_evicted_last_access_minutes_ = base::Histogram::FactoryGet(
2216 "Cookie.EvictedLastAccessMinutes",
2217 1, kMinutesInTenYears, 50,
2218 base::Histogram::kUmaTargetedHistogramFlag);
2219 histogram_count_ = base::Histogram::FactoryGet(
2220 "Cookie.Count", 1, 4000, 50,
2221 base::Histogram::kUmaTargetedHistogramFlag);
2222 histogram_domain_count_ = base::Histogram::FactoryGet(
2223 "Cookie.DomainCount", 1, 4000, 50,
2224 base::Histogram::kUmaTargetedHistogramFlag);
2225 histogram_etldp1_count_ = base::Histogram::FactoryGet(
2226 "Cookie.Etldp1Count", 1, 4000, 50,
2227 base::Histogram::kUmaTargetedHistogramFlag);
2228 histogram_domain_per_etldp1_count_ = base::Histogram::FactoryGet(
2229 "Cookie.DomainPerEtldp1Count", 1, 4000, 50,
2230 base::Histogram::kUmaTargetedHistogramFlag);
2232 // From UMA_HISTOGRAM_COUNTS_10000 & UMA_HISTOGRAM_CUSTOM_COUNTS
2233 histogram_number_duplicate_db_cookies_ = base::Histogram::FactoryGet(
2234 "Net.NumDuplicateCookiesInDb", 1, 10000, 50,
2235 base::Histogram::kUmaTargetedHistogramFlag);
2237 // From UMA_HISTOGRAM_ENUMERATION
2238 histogram_cookie_deletion_cause_ = base::LinearHistogram::FactoryGet(
2239 "Cookie.DeletionCause", 1,
2240 DELETE_COOKIE_LAST_ENTRY - 1, DELETE_COOKIE_LAST_ENTRY,
2241 base::Histogram::kUmaTargetedHistogramFlag);
2243 // From UMA_HISTOGRAM_{CUSTOM_,}TIMES
2244 histogram_time_blocked_on_load_ = base::Histogram::FactoryTimeGet(
2245 "Cookie.TimeBlockedOnLoad",
2246 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
2247 50, base::Histogram::kUmaTargetedHistogramFlag);
2251 // The system resolution is not high enough, so we can have multiple
2252 // set cookies that result in the same system time. When this happens, we
2253 // increment by one Time unit. Let's hope computers don't get too fast.
2254 Time CookieMonster::CurrentTime() {
2255 return std::max(Time::Now(),
2256 Time::FromInternalValue(last_time_seen_.ToInternalValue() + 1));
2259 bool CookieMonster::CopyCookiesForKeyToOtherCookieMonster(
2260 std::string key,
2261 CookieMonster* other) {
2262 ScopedVector<CanonicalCookie> duplicated_cookies;
2265 base::AutoLock autolock(lock_);
2266 DCHECK(other);
2267 if (!loaded_)
2268 return false;
2270 for (CookieMapItPair its = cookies_.equal_range(key);
2271 its.first != its.second;
2272 ++its.first) {
2273 CookieMap::iterator curit = its.first;
2274 CanonicalCookie* cc = curit->second;
2276 duplicated_cookies.push_back(cc->Duplicate());
2281 base::AutoLock autolock(other->lock_);
2282 if (!other->loaded_)
2283 return false;
2285 // There must not exist any entries for the key to be copied in |other|.
2286 CookieMapItPair its = other->cookies_.equal_range(key);
2287 if (its.first != its.second)
2288 return false;
2290 // Store the copied cookies in |other|.
2291 for (ScopedVector<CanonicalCookie>::const_iterator it =
2292 duplicated_cookies.begin();
2293 it != duplicated_cookies.end();
2294 ++it) {
2295 other->InternalInsertCookie(key, *it, true);
2298 // Since the cookies are owned by |other| now, weak clear must be used.
2299 duplicated_cookies.weak_clear();
2302 return true;
2305 bool CookieMonster::loaded() {
2306 base::AutoLock autolock(lock_);
2307 return loaded_;
2310 scoped_ptr<CookieStore::CookieChangedSubscription>
2311 CookieMonster::AddCallbackForCookie(
2312 const GURL& gurl,
2313 const std::string& name,
2314 const CookieChangedCallback& callback) {
2315 base::AutoLock autolock(lock_);
2316 std::pair<GURL, std::string> key(gurl, name);
2317 if (hook_map_.count(key) == 0)
2318 hook_map_[key] = make_linked_ptr(new CookieChangedCallbackList());
2319 return hook_map_[key]->Add(
2320 base::Bind(&RunAsync, base::MessageLoopProxy::current(), callback));
2323 void CookieMonster::RunCallbacks(const CanonicalCookie& cookie, bool removed) {
2324 lock_.AssertAcquired();
2325 CookieOptions opts;
2326 opts.set_include_httponly();
2327 // Note that the callbacks in hook_map_ are wrapped with MakeAsync(), so they
2328 // are guaranteed to not take long - they just post a RunAsync task back to
2329 // the appropriate thread's message loop and return. It is important that this
2330 // method not run user-supplied callbacks directly, since the CookieMonster
2331 // lock is held and it is easy to accidentally introduce deadlocks.
2332 for (CookieChangedHookMap::iterator it = hook_map_.begin();
2333 it != hook_map_.end(); ++it) {
2334 std::pair<GURL, std::string> key = it->first;
2335 if (cookie.IncludeForRequestURL(key.first, opts) &&
2336 cookie.Name() == key.second) {
2337 it->second->Notify(cookie, removed);
2342 } // namespace net