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