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