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