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