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