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