Switch some page set from user_agent fields to coresponding shared_state classes
[chromium-blink-merge.git] / net / cookies / cookie_monster.cc
blobb47461636d4dffd0a912b367e99f8920dd3ae227
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/location.h"
55 #include "base/logging.h"
56 #include "base/memory/scoped_ptr.h"
57 #include "base/metrics/field_trial.h"
58 #include "base/metrics/histogram.h"
59 #include "base/profiler/scoped_tracker.h"
60 #include "base/single_thread_task_runner.h"
61 #include "base/strings/string_util.h"
62 #include "base/strings/stringprintf.h"
63 #include "base/thread_task_runner_handle.h"
64 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
65 #include "net/cookies/canonical_cookie.h"
66 #include "net/cookies/cookie_util.h"
67 #include "net/cookies/parsed_cookie.h"
69 using base::Time;
70 using base::TimeDelta;
71 using base::TimeTicks;
73 // In steady state, most cookie requests can be satisfied by the in memory
74 // cookie monster store. If the cookie request cannot be satisfied by the in
75 // memory store, the relevant cookies must be fetched from the persistent
76 // store. The task is queued in CookieMonster::tasks_pending_ if it requires
77 // all cookies to be loaded from the backend, or tasks_pending_for_key_ if it
78 // only requires all cookies associated with an eTLD+1.
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.
94 static const int kMinutesInTenYears = 10 * 365 * 24 * 60;
96 namespace {
98 const char kFetchWhenNecessaryName[] = "FetchWhenNecessary";
99 const char kAlwaysFetchName[] = "AlwaysFetch";
100 const char kCookieMonsterFetchStrategyName[] = "CookieMonsterFetchStrategy";
102 } // namespace
104 namespace net {
106 // See comments at declaration of these variables in cookie_monster.h
107 // for details.
108 const size_t CookieMonster::kDomainMaxCookies = 180;
109 const size_t CookieMonster::kDomainPurgeCookies = 30;
110 const size_t CookieMonster::kMaxCookies = 3300;
111 const size_t CookieMonster::kPurgeCookies = 300;
113 const size_t CookieMonster::kDomainCookiesQuotaLow = 30;
114 const size_t CookieMonster::kDomainCookiesQuotaMedium = 50;
115 const size_t CookieMonster::kDomainCookiesQuotaHigh =
116 kDomainMaxCookies - kDomainPurgeCookies - kDomainCookiesQuotaLow -
117 kDomainCookiesQuotaMedium;
119 const int CookieMonster::kSafeFromGlobalPurgeDays = 30;
121 namespace {
123 bool ContainsControlCharacter(const std::string& s) {
124 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {
125 if ((*i >= 0) && (*i <= 31))
126 return true;
129 return false;
132 typedef std::vector<CanonicalCookie*> CanonicalCookieVector;
134 // Default minimum delay after updating a cookie's LastAccessDate before we
135 // will update it again.
136 const int kDefaultAccessUpdateThresholdSeconds = 60;
138 // Comparator to sort cookies from highest creation date to lowest
139 // creation date.
140 struct OrderByCreationTimeDesc {
141 bool operator()(const CookieMonster::CookieMap::iterator& a,
142 const CookieMonster::CookieMap::iterator& b) const {
143 return a->second->CreationDate() > b->second->CreationDate();
147 // Constants for use in VLOG
148 const int kVlogPerCookieMonster = 1;
149 const int kVlogPeriodic = 3;
150 const int kVlogGarbageCollection = 5;
151 const int kVlogSetCookies = 7;
152 const int kVlogGetCookies = 9;
154 // Mozilla sorts on the path length (longest first), and then it
155 // sorts by creation time (oldest first).
156 // The RFC says the sort order for the domain attribute is undefined.
157 bool CookieSorter(CanonicalCookie* cc1, CanonicalCookie* cc2) {
158 if (cc1->Path().length() == cc2->Path().length())
159 return cc1->CreationDate() < cc2->CreationDate();
160 return cc1->Path().length() > cc2->Path().length();
163 bool LRACookieSorter(const CookieMonster::CookieMap::iterator& it1,
164 const CookieMonster::CookieMap::iterator& it2) {
165 // Cookies accessed less recently should be deleted first.
166 if (it1->second->LastAccessDate() != it2->second->LastAccessDate())
167 return it1->second->LastAccessDate() < it2->second->LastAccessDate();
169 // In rare cases we might have two cookies with identical last access times.
170 // To preserve the stability of the sort, in these cases prefer to delete
171 // older cookies over newer ones. CreationDate() is guaranteed to be unique.
172 return it1->second->CreationDate() < it2->second->CreationDate();
175 // Compare cookies using name, domain and path, so that "equivalent" cookies
176 // (per RFC 2965) are equal to each other.
177 bool PartialDiffCookieSorter(const CanonicalCookie& a,
178 const CanonicalCookie& b) {
179 return a.PartialCompare(b);
182 // This is a stricter ordering than PartialDiffCookieOrdering, where all fields
183 // are used.
184 bool FullDiffCookieSorter(const CanonicalCookie& a, const CanonicalCookie& b) {
185 return a.FullCompare(b);
188 // Our strategy to find duplicates is:
189 // (1) Build a map from (cookiename, cookiepath) to
190 // {list of cookies with this signature, sorted by creation time}.
191 // (2) For each list with more than 1 entry, keep the cookie having the
192 // most recent creation time, and delete the others.
194 // Two cookies are considered equivalent if they have the same domain,
195 // name, and path.
196 struct CookieSignature {
197 public:
198 CookieSignature(const std::string& name,
199 const std::string& domain,
200 const std::string& path)
201 : name(name), domain(domain), path(path) {}
203 // To be a key for a map this class needs to be assignable, copyable,
204 // and have an operator<. The default assignment operator
205 // and copy constructor are exactly what we want.
207 bool operator<(const CookieSignature& cs) const {
208 // Name compare dominates, then domain, then path.
209 int diff = name.compare(cs.name);
210 if (diff != 0)
211 return diff < 0;
213 diff = domain.compare(cs.domain);
214 if (diff != 0)
215 return diff < 0;
217 return path.compare(cs.path) < 0;
220 std::string name;
221 std::string domain;
222 std::string path;
225 // For a CookieItVector iterator range [|it_begin|, |it_end|),
226 // sorts the first |num_sort| + 1 elements by LastAccessDate().
227 // The + 1 element exists so for any interval of length <= |num_sort| starting
228 // from |cookies_its_begin|, a LastAccessDate() bound can be found.
229 void SortLeastRecentlyAccessed(CookieMonster::CookieItVector::iterator it_begin,
230 CookieMonster::CookieItVector::iterator it_end,
231 size_t num_sort) {
232 DCHECK_LT(static_cast<int>(num_sort), it_end - it_begin);
233 std::partial_sort(it_begin, it_begin + num_sort + 1, it_end, LRACookieSorter);
236 // Predicate to support PartitionCookieByPriority().
237 struct CookiePriorityEqualsTo
238 : std::unary_function<const CookieMonster::CookieMap::iterator, bool> {
239 explicit CookiePriorityEqualsTo(CookiePriority priority)
240 : priority_(priority) {}
242 bool operator()(const CookieMonster::CookieMap::iterator it) const {
243 return it->second->Priority() == priority_;
246 const CookiePriority priority_;
249 // For a CookieItVector iterator range [|it_begin|, |it_end|),
250 // moves all cookies with a given |priority| to the beginning of the list.
251 // Returns: An iterator in [it_begin, it_end) to the first element with
252 // priority != |priority|, or |it_end| if all have priority == |priority|.
253 CookieMonster::CookieItVector::iterator PartitionCookieByPriority(
254 CookieMonster::CookieItVector::iterator it_begin,
255 CookieMonster::CookieItVector::iterator it_end,
256 CookiePriority priority) {
257 return std::partition(it_begin, it_end, CookiePriorityEqualsTo(priority));
260 bool LowerBoundAccessDateComparator(const CookieMonster::CookieMap::iterator it,
261 const Time& access_date) {
262 return it->second->LastAccessDate() < access_date;
265 // For a CookieItVector iterator range [|it_begin|, |it_end|)
266 // from a CookieItVector sorted by LastAccessDate(), returns the
267 // first iterator with access date >= |access_date|, or cookie_its_end if this
268 // holds for all.
269 CookieMonster::CookieItVector::iterator LowerBoundAccessDate(
270 const CookieMonster::CookieItVector::iterator its_begin,
271 const CookieMonster::CookieItVector::iterator its_end,
272 const Time& access_date) {
273 return std::lower_bound(its_begin, its_end, access_date,
274 LowerBoundAccessDateComparator);
277 // Mapping between DeletionCause and CookieMonsterDelegate::ChangeCause; the
278 // mapping also provides a boolean that specifies whether or not an
279 // OnCookieChanged notification ought to be generated.
280 typedef struct ChangeCausePair_struct {
281 CookieMonsterDelegate::ChangeCause cause;
282 bool notify;
283 } ChangeCausePair;
284 ChangeCausePair ChangeCauseMapping[] = {
285 // DELETE_COOKIE_EXPLICIT
286 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, true},
287 // DELETE_COOKIE_OVERWRITE
288 {CookieMonsterDelegate::CHANGE_COOKIE_OVERWRITE, true},
289 // DELETE_COOKIE_EXPIRED
290 {CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED, true},
291 // DELETE_COOKIE_EVICTED
292 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true},
293 // DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE
294 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false},
295 // DELETE_COOKIE_DONT_RECORD
296 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false},
297 // DELETE_COOKIE_EVICTED_DOMAIN
298 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true},
299 // DELETE_COOKIE_EVICTED_GLOBAL
300 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true},
301 // DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
302 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true},
303 // DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
304 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true},
305 // DELETE_COOKIE_EXPIRED_OVERWRITE
306 {CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED_OVERWRITE, true},
307 // DELETE_COOKIE_CONTROL_CHAR
308 {CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true},
309 // DELETE_COOKIE_LAST_ENTRY
310 {CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false}};
312 std::string BuildCookieLine(const CanonicalCookieVector& cookies) {
313 std::string cookie_line;
314 for (CanonicalCookieVector::const_iterator it = cookies.begin();
315 it != cookies.end(); ++it) {
316 if (it != cookies.begin())
317 cookie_line += "; ";
318 // In Mozilla if you set a cookie like AAAA, it will have an empty token
319 // and a value of AAAA. When it sends the cookie back, it will send AAAA,
320 // so we need to avoid sending =AAAA for a blank token value.
321 if (!(*it)->Name().empty())
322 cookie_line += (*it)->Name() + "=";
323 cookie_line += (*it)->Value();
325 return cookie_line;
328 void RunAsync(scoped_refptr<base::TaskRunner> proxy,
329 const CookieStore::CookieChangedCallback& callback,
330 const CanonicalCookie& cookie,
331 bool removed) {
332 proxy->PostTask(FROM_HERE, base::Bind(callback, cookie, removed));
335 } // namespace
337 CookieMonster::CookieMonster(PersistentCookieStore* store,
338 CookieMonsterDelegate* delegate)
339 : initialized_(false),
340 started_fetching_all_cookies_(false),
341 finished_fetching_all_cookies_(false),
342 fetch_strategy_(kUnknownFetch),
343 store_(store),
344 last_access_threshold_(
345 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)),
346 delegate_(delegate),
347 last_statistic_record_time_(Time::Now()),
348 keep_expired_cookies_(false),
349 persist_session_cookies_(false) {
350 InitializeHistograms();
351 SetDefaultCookieableSchemes();
354 CookieMonster::CookieMonster(PersistentCookieStore* store,
355 CookieMonsterDelegate* delegate,
356 int last_access_threshold_milliseconds)
357 : initialized_(false),
358 started_fetching_all_cookies_(false),
359 finished_fetching_all_cookies_(false),
360 fetch_strategy_(kUnknownFetch),
361 store_(store),
362 last_access_threshold_(base::TimeDelta::FromMilliseconds(
363 last_access_threshold_milliseconds)),
364 delegate_(delegate),
365 last_statistic_record_time_(base::Time::Now()),
366 keep_expired_cookies_(false),
367 persist_session_cookies_(false) {
368 InitializeHistograms();
369 SetDefaultCookieableSchemes();
372 // Task classes for queueing the coming request.
374 class CookieMonster::CookieMonsterTask
375 : public base::RefCountedThreadSafe<CookieMonsterTask> {
376 public:
377 // Runs the task and invokes the client callback on the thread that
378 // originally constructed the task.
379 virtual void Run() = 0;
381 protected:
382 explicit CookieMonsterTask(CookieMonster* cookie_monster);
383 virtual ~CookieMonsterTask();
385 // Invokes the callback immediately, if the current thread is the one
386 // that originated the task, or queues the callback for execution on the
387 // appropriate thread. Maintains a reference to this CookieMonsterTask
388 // instance until the callback completes.
389 void InvokeCallback(base::Closure callback);
391 CookieMonster* cookie_monster() { return cookie_monster_; }
393 private:
394 friend class base::RefCountedThreadSafe<CookieMonsterTask>;
396 CookieMonster* cookie_monster_;
397 scoped_refptr<base::SingleThreadTaskRunner> thread_;
399 DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask);
402 CookieMonster::CookieMonsterTask::CookieMonsterTask(
403 CookieMonster* cookie_monster)
404 : cookie_monster_(cookie_monster),
405 thread_(base::ThreadTaskRunnerHandle::Get()) {
408 CookieMonster::CookieMonsterTask::~CookieMonsterTask() {
411 // Unfortunately, one cannot re-bind a Callback with parameters into a closure.
412 // Therefore, the closure passed to InvokeCallback is a clumsy binding of
413 // Callback::Run on a wrapped Callback instance. Since Callback is not
414 // reference counted, we bind to an instance that is a member of the
415 // CookieMonsterTask subclass. Then, we cannot simply post the callback to a
416 // message loop because the underlying instance may be destroyed (along with the
417 // CookieMonsterTask instance) in the interim. Therefore, we post a callback
418 // bound to the CookieMonsterTask, which *is* reference counted (thus preventing
419 // destruction of the original callback), and which invokes the closure (which
420 // invokes the original callback with the returned data).
421 void CookieMonster::CookieMonsterTask::InvokeCallback(base::Closure callback) {
422 if (thread_->BelongsToCurrentThread()) {
423 callback.Run();
424 } else {
425 thread_->PostTask(FROM_HERE, base::Bind(&CookieMonsterTask::InvokeCallback,
426 this, callback));
430 // Task class for SetCookieWithDetails call.
431 class CookieMonster::SetCookieWithDetailsTask : public CookieMonsterTask {
432 public:
433 SetCookieWithDetailsTask(CookieMonster* cookie_monster,
434 const GURL& url,
435 const std::string& name,
436 const std::string& value,
437 const std::string& domain,
438 const std::string& path,
439 const base::Time& expiration_time,
440 bool secure,
441 bool http_only,
442 bool first_party_only,
443 CookiePriority priority,
444 const SetCookiesCallback& callback)
445 : CookieMonsterTask(cookie_monster),
446 url_(url),
447 name_(name),
448 value_(value),
449 domain_(domain),
450 path_(path),
451 expiration_time_(expiration_time),
452 secure_(secure),
453 http_only_(http_only),
454 first_party_only_(first_party_only),
455 priority_(priority),
456 callback_(callback) {}
458 // CookieMonsterTask:
459 void Run() override;
461 protected:
462 ~SetCookieWithDetailsTask() override {}
464 private:
465 GURL url_;
466 std::string name_;
467 std::string value_;
468 std::string domain_;
469 std::string path_;
470 base::Time expiration_time_;
471 bool secure_;
472 bool http_only_;
473 bool first_party_only_;
474 CookiePriority priority_;
475 SetCookiesCallback callback_;
477 DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask);
480 void CookieMonster::SetCookieWithDetailsTask::Run() {
481 bool success = this->cookie_monster()->SetCookieWithDetails(
482 url_, name_, value_, domain_, path_, expiration_time_, secure_,
483 http_only_, first_party_only_, priority_);
484 if (!callback_.is_null()) {
485 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run,
486 base::Unretained(&callback_), success));
490 // Task class for GetAllCookies call.
491 class CookieMonster::GetAllCookiesTask : public CookieMonsterTask {
492 public:
493 GetAllCookiesTask(CookieMonster* cookie_monster,
494 const GetCookieListCallback& callback)
495 : CookieMonsterTask(cookie_monster), callback_(callback) {}
497 // CookieMonsterTask
498 void Run() override;
500 protected:
501 ~GetAllCookiesTask() override {}
503 private:
504 GetCookieListCallback callback_;
506 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask);
509 void CookieMonster::GetAllCookiesTask::Run() {
510 if (!callback_.is_null()) {
511 CookieList cookies = this->cookie_monster()->GetAllCookies();
512 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run,
513 base::Unretained(&callback_), cookies));
517 // Task class for GetAllCookiesForURLWithOptions call.
518 class CookieMonster::GetAllCookiesForURLWithOptionsTask
519 : public CookieMonsterTask {
520 public:
521 GetAllCookiesForURLWithOptionsTask(CookieMonster* cookie_monster,
522 const GURL& url,
523 const CookieOptions& options,
524 const GetCookieListCallback& callback)
525 : CookieMonsterTask(cookie_monster),
526 url_(url),
527 options_(options),
528 callback_(callback) {}
530 // CookieMonsterTask:
531 void Run() override;
533 protected:
534 ~GetAllCookiesForURLWithOptionsTask() override {}
536 private:
537 GURL url_;
538 CookieOptions options_;
539 GetCookieListCallback callback_;
541 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesForURLWithOptionsTask);
544 void CookieMonster::GetAllCookiesForURLWithOptionsTask::Run() {
545 if (!callback_.is_null()) {
546 CookieList cookies =
547 this->cookie_monster()->GetAllCookiesForURLWithOptions(url_, options_);
548 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run,
549 base::Unretained(&callback_), cookies));
553 template <typename Result>
554 struct CallbackType {
555 typedef base::Callback<void(Result)> Type;
558 template <>
559 struct CallbackType<void> {
560 typedef base::Closure Type;
563 // Base task class for Delete*Task.
564 template <typename Result>
565 class CookieMonster::DeleteTask : public CookieMonsterTask {
566 public:
567 DeleteTask(CookieMonster* cookie_monster,
568 const typename CallbackType<Result>::Type& callback)
569 : CookieMonsterTask(cookie_monster), callback_(callback) {}
571 // CookieMonsterTask:
572 void Run() override;
574 protected:
575 ~DeleteTask() override;
577 private:
578 // Runs the delete task and returns a result.
579 virtual Result RunDeleteTask() = 0;
580 base::Closure RunDeleteTaskAndBindCallback();
581 void FlushDone(const base::Closure& callback);
583 typename CallbackType<Result>::Type callback_;
585 DISALLOW_COPY_AND_ASSIGN(DeleteTask);
588 template <typename Result>
589 CookieMonster::DeleteTask<Result>::~DeleteTask() {
592 template <typename Result>
593 base::Closure
594 CookieMonster::DeleteTask<Result>::RunDeleteTaskAndBindCallback() {
595 Result result = RunDeleteTask();
596 if (callback_.is_null())
597 return base::Closure();
598 return base::Bind(callback_, result);
601 template <>
602 base::Closure CookieMonster::DeleteTask<void>::RunDeleteTaskAndBindCallback() {
603 RunDeleteTask();
604 return callback_;
607 template <typename Result>
608 void CookieMonster::DeleteTask<Result>::Run() {
609 this->cookie_monster()->FlushStore(base::Bind(
610 &DeleteTask<Result>::FlushDone, this, RunDeleteTaskAndBindCallback()));
613 template <typename Result>
614 void CookieMonster::DeleteTask<Result>::FlushDone(
615 const base::Closure& callback) {
616 if (!callback.is_null()) {
617 this->InvokeCallback(callback);
621 // Task class for DeleteAll call.
622 class CookieMonster::DeleteAllTask : public DeleteTask<int> {
623 public:
624 DeleteAllTask(CookieMonster* cookie_monster, const DeleteCallback& callback)
625 : DeleteTask<int>(cookie_monster, callback) {}
627 // DeleteTask:
628 int RunDeleteTask() override;
630 protected:
631 ~DeleteAllTask() override {}
633 private:
634 DISALLOW_COPY_AND_ASSIGN(DeleteAllTask);
637 int CookieMonster::DeleteAllTask::RunDeleteTask() {
638 return this->cookie_monster()->DeleteAll(true);
641 // Task class for DeleteAllCreatedBetween call.
642 class CookieMonster::DeleteAllCreatedBetweenTask : public DeleteTask<int> {
643 public:
644 DeleteAllCreatedBetweenTask(CookieMonster* cookie_monster,
645 const Time& delete_begin,
646 const Time& delete_end,
647 const DeleteCallback& callback)
648 : DeleteTask<int>(cookie_monster, callback),
649 delete_begin_(delete_begin),
650 delete_end_(delete_end) {}
652 // DeleteTask:
653 int RunDeleteTask() override;
655 protected:
656 ~DeleteAllCreatedBetweenTask() override {}
658 private:
659 Time delete_begin_;
660 Time delete_end_;
662 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask);
665 int CookieMonster::DeleteAllCreatedBetweenTask::RunDeleteTask() {
666 return this->cookie_monster()->DeleteAllCreatedBetween(delete_begin_,
667 delete_end_);
670 // Task class for DeleteAllForHost call.
671 class CookieMonster::DeleteAllForHostTask : public DeleteTask<int> {
672 public:
673 DeleteAllForHostTask(CookieMonster* cookie_monster,
674 const GURL& url,
675 const DeleteCallback& callback)
676 : DeleteTask<int>(cookie_monster, callback), url_(url) {}
678 // DeleteTask:
679 int RunDeleteTask() override;
681 protected:
682 ~DeleteAllForHostTask() override {}
684 private:
685 GURL url_;
687 DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask);
690 int CookieMonster::DeleteAllForHostTask::RunDeleteTask() {
691 return this->cookie_monster()->DeleteAllForHost(url_);
694 // Task class for DeleteAllCreatedBetweenForHost call.
695 class CookieMonster::DeleteAllCreatedBetweenForHostTask
696 : public DeleteTask<int> {
697 public:
698 DeleteAllCreatedBetweenForHostTask(CookieMonster* cookie_monster,
699 Time delete_begin,
700 Time delete_end,
701 const GURL& url,
702 const DeleteCallback& callback)
703 : DeleteTask<int>(cookie_monster, callback),
704 delete_begin_(delete_begin),
705 delete_end_(delete_end),
706 url_(url) {}
708 // DeleteTask:
709 int RunDeleteTask() override;
711 protected:
712 ~DeleteAllCreatedBetweenForHostTask() override {}
714 private:
715 Time delete_begin_;
716 Time delete_end_;
717 GURL url_;
719 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenForHostTask);
722 int CookieMonster::DeleteAllCreatedBetweenForHostTask::RunDeleteTask() {
723 return this->cookie_monster()->DeleteAllCreatedBetweenForHost(
724 delete_begin_, delete_end_, url_);
727 // Task class for DeleteCanonicalCookie call.
728 class CookieMonster::DeleteCanonicalCookieTask : public DeleteTask<bool> {
729 public:
730 DeleteCanonicalCookieTask(CookieMonster* cookie_monster,
731 const CanonicalCookie& cookie,
732 const DeleteCookieCallback& callback)
733 : DeleteTask<bool>(cookie_monster, callback), cookie_(cookie) {}
735 // DeleteTask:
736 bool RunDeleteTask() override;
738 protected:
739 ~DeleteCanonicalCookieTask() override {}
741 private:
742 CanonicalCookie cookie_;
744 DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask);
747 bool CookieMonster::DeleteCanonicalCookieTask::RunDeleteTask() {
748 return this->cookie_monster()->DeleteCanonicalCookie(cookie_);
751 // Task class for SetCookieWithOptions call.
752 class CookieMonster::SetCookieWithOptionsTask : public CookieMonsterTask {
753 public:
754 SetCookieWithOptionsTask(CookieMonster* cookie_monster,
755 const GURL& url,
756 const std::string& cookie_line,
757 const CookieOptions& options,
758 const SetCookiesCallback& callback)
759 : CookieMonsterTask(cookie_monster),
760 url_(url),
761 cookie_line_(cookie_line),
762 options_(options),
763 callback_(callback) {}
765 // CookieMonsterTask:
766 void Run() override;
768 protected:
769 ~SetCookieWithOptionsTask() override {}
771 private:
772 GURL url_;
773 std::string cookie_line_;
774 CookieOptions options_;
775 SetCookiesCallback callback_;
777 DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask);
780 void CookieMonster::SetCookieWithOptionsTask::Run() {
781 bool result = this->cookie_monster()->SetCookieWithOptions(url_, cookie_line_,
782 options_);
783 if (!callback_.is_null()) {
784 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run,
785 base::Unretained(&callback_), result));
789 // Task class for SetAllCookies call.
790 class CookieMonster::SetAllCookiesTask : public CookieMonsterTask {
791 public:
792 SetAllCookiesTask(CookieMonster* cookie_monster,
793 const CookieList& list,
794 const SetCookiesCallback& callback)
795 : CookieMonsterTask(cookie_monster), list_(list), callback_(callback) {}
797 // CookieMonsterTask:
798 void Run() override;
800 protected:
801 ~SetAllCookiesTask() override {}
803 private:
804 CookieList list_;
805 SetCookiesCallback callback_;
807 DISALLOW_COPY_AND_ASSIGN(SetAllCookiesTask);
810 void CookieMonster::SetAllCookiesTask::Run() {
811 CookieList positive_diff;
812 CookieList negative_diff;
813 CookieList old_cookies = this->cookie_monster()->GetAllCookies();
814 this->cookie_monster()->ComputeCookieDiff(&old_cookies, &list_,
815 &positive_diff, &negative_diff);
817 for (CookieList::const_iterator it = negative_diff.begin();
818 it != negative_diff.end(); ++it) {
819 this->cookie_monster()->DeleteCanonicalCookie(*it);
822 bool result = true;
823 if (positive_diff.size() > 0)
824 result = this->cookie_monster()->SetCanonicalCookies(list_);
826 if (!callback_.is_null()) {
827 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run,
828 base::Unretained(&callback_), result));
832 // Task class for GetCookiesWithOptions call.
833 class CookieMonster::GetCookiesWithOptionsTask : public CookieMonsterTask {
834 public:
835 GetCookiesWithOptionsTask(CookieMonster* cookie_monster,
836 const GURL& url,
837 const CookieOptions& options,
838 const GetCookiesCallback& callback)
839 : CookieMonsterTask(cookie_monster),
840 url_(url),
841 options_(options),
842 callback_(callback) {}
844 // CookieMonsterTask:
845 void Run() override;
847 protected:
848 ~GetCookiesWithOptionsTask() override {}
850 private:
851 GURL url_;
852 CookieOptions options_;
853 GetCookiesCallback callback_;
855 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask);
858 void CookieMonster::GetCookiesWithOptionsTask::Run() {
859 // TODO(mkwst): Remove ScopedTracker below once crbug.com/456373 is fixed.
860 tracked_objects::ScopedTracker tracking_profile(
861 FROM_HERE_WITH_EXPLICIT_FUNCTION(
862 "456373 CookieMonster::GetCookiesWithOptionsTask::Run"));
863 std::string cookie =
864 this->cookie_monster()->GetCookiesWithOptions(url_, options_);
865 if (!callback_.is_null()) {
866 this->InvokeCallback(base::Bind(&GetCookiesCallback::Run,
867 base::Unretained(&callback_), cookie));
871 // Task class for DeleteCookie call.
872 class CookieMonster::DeleteCookieTask : public DeleteTask<void> {
873 public:
874 DeleteCookieTask(CookieMonster* cookie_monster,
875 const GURL& url,
876 const std::string& cookie_name,
877 const base::Closure& callback)
878 : DeleteTask<void>(cookie_monster, callback),
879 url_(url),
880 cookie_name_(cookie_name) {}
882 // DeleteTask:
883 void RunDeleteTask() override;
885 protected:
886 ~DeleteCookieTask() override {}
888 private:
889 GURL url_;
890 std::string cookie_name_;
892 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
895 void CookieMonster::DeleteCookieTask::RunDeleteTask() {
896 this->cookie_monster()->DeleteCookie(url_, cookie_name_);
899 // Task class for DeleteSessionCookies call.
900 class CookieMonster::DeleteSessionCookiesTask : public DeleteTask<int> {
901 public:
902 DeleteSessionCookiesTask(CookieMonster* cookie_monster,
903 const DeleteCallback& callback)
904 : DeleteTask<int>(cookie_monster, callback) {}
906 // DeleteTask:
907 int RunDeleteTask() override;
909 protected:
910 ~DeleteSessionCookiesTask() override {}
912 private:
913 DISALLOW_COPY_AND_ASSIGN(DeleteSessionCookiesTask);
916 int CookieMonster::DeleteSessionCookiesTask::RunDeleteTask() {
917 return this->cookie_monster()->DeleteSessionCookies();
920 // Task class for HasCookiesForETLDP1Task call.
921 class CookieMonster::HasCookiesForETLDP1Task : public CookieMonsterTask {
922 public:
923 HasCookiesForETLDP1Task(CookieMonster* cookie_monster,
924 const std::string& etldp1,
925 const HasCookiesForETLDP1Callback& callback)
926 : CookieMonsterTask(cookie_monster),
927 etldp1_(etldp1),
928 callback_(callback) {}
930 // CookieMonsterTask:
931 void Run() override;
933 protected:
934 ~HasCookiesForETLDP1Task() override {}
936 private:
937 std::string etldp1_;
938 HasCookiesForETLDP1Callback callback_;
940 DISALLOW_COPY_AND_ASSIGN(HasCookiesForETLDP1Task);
943 void CookieMonster::HasCookiesForETLDP1Task::Run() {
944 bool result = this->cookie_monster()->HasCookiesForETLDP1(etldp1_);
945 if (!callback_.is_null()) {
946 this->InvokeCallback(base::Bind(&HasCookiesForETLDP1Callback::Run,
947 base::Unretained(&callback_), result));
951 // Asynchronous CookieMonster API
953 void CookieMonster::SetCookieWithDetailsAsync(
954 const GURL& url,
955 const std::string& name,
956 const std::string& value,
957 const std::string& domain,
958 const std::string& path,
959 const Time& expiration_time,
960 bool secure,
961 bool http_only,
962 bool first_party_only,
963 CookiePriority priority,
964 const SetCookiesCallback& callback) {
965 scoped_refptr<SetCookieWithDetailsTask> task = new SetCookieWithDetailsTask(
966 this, url, name, value, domain, path, expiration_time, secure, http_only,
967 first_party_only, priority, callback);
968 DoCookieTaskForURL(task, url);
971 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) {
972 scoped_refptr<GetAllCookiesTask> task = new GetAllCookiesTask(this, callback);
974 DoCookieTask(task);
977 void CookieMonster::GetAllCookiesForURLWithOptionsAsync(
978 const GURL& url,
979 const CookieOptions& options,
980 const GetCookieListCallback& callback) {
981 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
982 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
984 DoCookieTaskForURL(task, url);
987 void CookieMonster::GetAllCookiesForURLAsync(
988 const GURL& url,
989 const GetCookieListCallback& callback) {
990 CookieOptions options;
991 options.set_include_httponly();
992 options.set_include_first_party_only();
993 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
994 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
996 DoCookieTaskForURL(task, url);
999 void CookieMonster::HasCookiesForETLDP1Async(
1000 const std::string& etldp1,
1001 const HasCookiesForETLDP1Callback& callback) {
1002 scoped_refptr<HasCookiesForETLDP1Task> task =
1003 new HasCookiesForETLDP1Task(this, etldp1, callback);
1005 DoCookieTaskForURL(task, GURL("http://" + etldp1));
1008 void CookieMonster::DeleteAllAsync(const DeleteCallback& callback) {
1009 scoped_refptr<DeleteAllTask> task = new DeleteAllTask(this, callback);
1011 DoCookieTask(task);
1014 void CookieMonster::DeleteAllCreatedBetweenAsync(
1015 const Time& delete_begin,
1016 const Time& delete_end,
1017 const DeleteCallback& callback) {
1018 scoped_refptr<DeleteAllCreatedBetweenTask> task =
1019 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end, callback);
1021 DoCookieTask(task);
1024 void CookieMonster::DeleteAllCreatedBetweenForHostAsync(
1025 const Time delete_begin,
1026 const Time delete_end,
1027 const GURL& url,
1028 const DeleteCallback& callback) {
1029 scoped_refptr<DeleteAllCreatedBetweenForHostTask> task =
1030 new DeleteAllCreatedBetweenForHostTask(this, delete_begin, delete_end,
1031 url, callback);
1033 DoCookieTaskForURL(task, url);
1036 void CookieMonster::DeleteAllForHostAsync(const GURL& url,
1037 const DeleteCallback& callback) {
1038 scoped_refptr<DeleteAllForHostTask> task =
1039 new DeleteAllForHostTask(this, url, callback);
1041 DoCookieTaskForURL(task, url);
1044 void CookieMonster::DeleteCanonicalCookieAsync(
1045 const CanonicalCookie& cookie,
1046 const DeleteCookieCallback& callback) {
1047 scoped_refptr<DeleteCanonicalCookieTask> task =
1048 new DeleteCanonicalCookieTask(this, cookie, callback);
1050 DoCookieTask(task);
1053 void CookieMonster::SetAllCookiesAsync(const CookieList& list,
1054 const SetCookiesCallback& callback) {
1055 scoped_refptr<SetAllCookiesTask> task =
1056 new SetAllCookiesTask(this, list, callback);
1057 DoCookieTask(task);
1060 void CookieMonster::SetCookieWithOptionsAsync(
1061 const GURL& url,
1062 const std::string& cookie_line,
1063 const CookieOptions& options,
1064 const SetCookiesCallback& callback) {
1065 scoped_refptr<SetCookieWithOptionsTask> task =
1066 new SetCookieWithOptionsTask(this, url, cookie_line, options, callback);
1068 DoCookieTaskForURL(task, url);
1071 void CookieMonster::GetCookiesWithOptionsAsync(
1072 const GURL& url,
1073 const CookieOptions& options,
1074 const GetCookiesCallback& callback) {
1075 scoped_refptr<GetCookiesWithOptionsTask> task =
1076 new GetCookiesWithOptionsTask(this, url, options, callback);
1078 DoCookieTaskForURL(task, url);
1081 void CookieMonster::DeleteCookieAsync(const GURL& url,
1082 const std::string& cookie_name,
1083 const base::Closure& callback) {
1084 scoped_refptr<DeleteCookieTask> task =
1085 new DeleteCookieTask(this, url, cookie_name, callback);
1087 DoCookieTaskForURL(task, url);
1090 void CookieMonster::DeleteSessionCookiesAsync(
1091 const CookieStore::DeleteCallback& callback) {
1092 scoped_refptr<DeleteSessionCookiesTask> task =
1093 new DeleteSessionCookiesTask(this, callback);
1095 DoCookieTask(task);
1098 void CookieMonster::DoCookieTask(
1099 const scoped_refptr<CookieMonsterTask>& task_item) {
1101 base::AutoLock autolock(lock_);
1102 MarkCookieStoreAsInitialized();
1103 FetchAllCookiesIfNecessary();
1104 if (!finished_fetching_all_cookies_ && store_.get()) {
1105 tasks_pending_.push(task_item);
1106 return;
1110 task_item->Run();
1113 void CookieMonster::DoCookieTaskForURL(
1114 const scoped_refptr<CookieMonsterTask>& task_item,
1115 const GURL& url) {
1117 base::AutoLock autolock(lock_);
1118 MarkCookieStoreAsInitialized();
1119 if (ShouldFetchAllCookiesWhenFetchingAnyCookie())
1120 FetchAllCookiesIfNecessary();
1121 // If cookies for the requested domain key (eTLD+1) have been loaded from DB
1122 // then run the task, otherwise load from DB.
1123 if (!finished_fetching_all_cookies_ && store_.get()) {
1124 // Checks if the domain key has been loaded.
1125 std::string key(
1126 cookie_util::GetEffectiveDomain(url.scheme(), url.host()));
1127 if (keys_loaded_.find(key) == keys_loaded_.end()) {
1128 std::map<std::string,
1129 std::deque<scoped_refptr<CookieMonsterTask>>>::iterator it =
1130 tasks_pending_for_key_.find(key);
1131 if (it == tasks_pending_for_key_.end()) {
1132 store_->LoadCookiesForKey(
1133 key, base::Bind(&CookieMonster::OnKeyLoaded, this, key));
1134 it = tasks_pending_for_key_
1135 .insert(std::make_pair(
1136 key, std::deque<scoped_refptr<CookieMonsterTask>>()))
1137 .first;
1139 it->second.push_back(task_item);
1140 return;
1144 task_item->Run();
1147 bool CookieMonster::SetCookieWithDetails(const GURL& url,
1148 const std::string& name,
1149 const std::string& value,
1150 const std::string& domain,
1151 const std::string& path,
1152 const base::Time& expiration_time,
1153 bool secure,
1154 bool http_only,
1155 bool first_party_only,
1156 CookiePriority priority) {
1157 base::AutoLock autolock(lock_);
1159 if (!HasCookieableScheme(url))
1160 return false;
1162 Time creation_time = CurrentTime();
1163 last_time_seen_ = creation_time;
1165 scoped_ptr<CanonicalCookie> cc;
1166 cc.reset(CanonicalCookie::Create(url, name, value, domain, path,
1167 creation_time, expiration_time, secure,
1168 http_only, first_party_only, priority));
1170 if (!cc.get())
1171 return false;
1173 CookieOptions options;
1174 options.set_include_httponly();
1175 options.set_include_first_party_only();
1176 return SetCanonicalCookie(&cc, creation_time, options);
1179 bool CookieMonster::ImportCookies(const CookieList& list) {
1180 base::AutoLock autolock(lock_);
1181 MarkCookieStoreAsInitialized();
1182 if (ShouldFetchAllCookiesWhenFetchingAnyCookie())
1183 FetchAllCookiesIfNecessary();
1184 for (CookieList::const_iterator iter = list.begin(); iter != list.end();
1185 ++iter) {
1186 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie(*iter));
1187 CookieOptions options;
1188 options.set_include_httponly();
1189 options.set_include_first_party_only();
1190 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), options))
1191 return false;
1193 return true;
1196 CookieList CookieMonster::GetAllCookies() {
1197 base::AutoLock autolock(lock_);
1199 // This function is being called to scrape the cookie list for management UI
1200 // or similar. We shouldn't show expired cookies in this list since it will
1201 // just be confusing to users, and this function is called rarely enough (and
1202 // is already slow enough) that it's OK to take the time to garbage collect
1203 // the expired cookies now.
1205 // Note that this does not prune cookies to be below our limits (if we've
1206 // exceeded them) the way that calling GarbageCollect() would.
1207 GarbageCollectExpired(
1208 Time::Now(), CookieMapItPair(cookies_.begin(), cookies_.end()), NULL);
1210 // Copy the CanonicalCookie pointers from the map so that we can use the same
1211 // sorter as elsewhere, then copy the result out.
1212 std::vector<CanonicalCookie*> cookie_ptrs;
1213 cookie_ptrs.reserve(cookies_.size());
1214 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end(); ++it)
1215 cookie_ptrs.push_back(it->second);
1216 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
1218 CookieList cookie_list;
1219 cookie_list.reserve(cookie_ptrs.size());
1220 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
1221 it != cookie_ptrs.end(); ++it)
1222 cookie_list.push_back(**it);
1224 return cookie_list;
1227 CookieList CookieMonster::GetAllCookiesForURLWithOptions(
1228 const GURL& url,
1229 const CookieOptions& options) {
1230 base::AutoLock autolock(lock_);
1232 std::vector<CanonicalCookie*> cookie_ptrs;
1233 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs);
1234 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
1236 CookieList cookies;
1237 cookies.reserve(cookie_ptrs.size());
1238 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
1239 it != cookie_ptrs.end(); it++)
1240 cookies.push_back(**it);
1242 return cookies;
1245 CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) {
1246 CookieOptions options;
1247 options.set_include_httponly();
1248 options.set_first_party_url(url);
1250 return GetAllCookiesForURLWithOptions(url, options);
1253 int CookieMonster::DeleteAll(bool sync_to_store) {
1254 base::AutoLock autolock(lock_);
1256 int num_deleted = 0;
1257 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1258 CookieMap::iterator curit = it;
1259 ++it;
1260 InternalDeleteCookie(curit, sync_to_store,
1261 sync_to_store
1262 ? DELETE_COOKIE_EXPLICIT
1263 : DELETE_COOKIE_DONT_RECORD /* Destruction. */);
1264 ++num_deleted;
1267 return num_deleted;
1270 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin,
1271 const Time& delete_end) {
1272 base::AutoLock autolock(lock_);
1274 int num_deleted = 0;
1275 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1276 CookieMap::iterator curit = it;
1277 CanonicalCookie* cc = curit->second;
1278 ++it;
1280 if (cc->CreationDate() >= delete_begin &&
1281 (delete_end.is_null() || cc->CreationDate() < delete_end)) {
1282 InternalDeleteCookie(curit, true, /*sync_to_store*/
1283 DELETE_COOKIE_EXPLICIT);
1284 ++num_deleted;
1288 return num_deleted;
1291 int CookieMonster::DeleteAllCreatedBetweenForHost(const Time delete_begin,
1292 const Time delete_end,
1293 const GURL& url) {
1294 base::AutoLock autolock(lock_);
1296 if (!HasCookieableScheme(url))
1297 return 0;
1299 const std::string host(url.host());
1301 // We store host cookies in the store by their canonical host name;
1302 // domain cookies are stored with a leading ".". So this is a pretty
1303 // simple lookup and per-cookie delete.
1304 int num_deleted = 0;
1305 for (CookieMapItPair its = cookies_.equal_range(GetKey(host));
1306 its.first != its.second;) {
1307 CookieMap::iterator curit = its.first;
1308 ++its.first;
1310 const CanonicalCookie* const cc = curit->second;
1312 // Delete only on a match as a host cookie.
1313 if (cc->IsHostCookie() && cc->IsDomainMatch(host) &&
1314 cc->CreationDate() >= delete_begin &&
1315 // The assumption that null |delete_end| is equivalent to
1316 // Time::Max() is confusing.
1317 (delete_end.is_null() || cc->CreationDate() < delete_end)) {
1318 num_deleted++;
1320 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT);
1323 return num_deleted;
1326 int CookieMonster::DeleteAllForHost(const GURL& url) {
1327 return DeleteAllCreatedBetweenForHost(Time(), Time::Max(), url);
1330 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) {
1331 base::AutoLock autolock(lock_);
1333 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain()));
1334 its.first != its.second; ++its.first) {
1335 // The creation date acts as our unique index...
1336 if (its.first->second->CreationDate() == cookie.CreationDate()) {
1337 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT);
1338 return true;
1341 return false;
1344 void CookieMonster::SetCookieableSchemes(const char* const schemes[],
1345 size_t num_schemes) {
1346 base::AutoLock autolock(lock_);
1348 // Cookieable Schemes must be set before first use of function.
1349 DCHECK(!initialized_);
1351 cookieable_schemes_.clear();
1352 cookieable_schemes_.insert(cookieable_schemes_.end(), schemes,
1353 schemes + num_schemes);
1356 void CookieMonster::SetKeepExpiredCookies() {
1357 keep_expired_cookies_ = true;
1360 void CookieMonster::FlushStore(const base::Closure& callback) {
1361 base::AutoLock autolock(lock_);
1362 if (initialized_ && store_.get())
1363 store_->Flush(callback);
1364 else if (!callback.is_null())
1365 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback);
1368 bool CookieMonster::SetCookieWithOptions(const GURL& url,
1369 const std::string& cookie_line,
1370 const CookieOptions& options) {
1371 base::AutoLock autolock(lock_);
1373 if (!HasCookieableScheme(url)) {
1374 return false;
1377 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options);
1380 std::string CookieMonster::GetCookiesWithOptions(const GURL& url,
1381 const CookieOptions& options) {
1382 base::AutoLock autolock(lock_);
1384 if (!HasCookieableScheme(url))
1385 return std::string();
1387 std::vector<CanonicalCookie*> cookies;
1388 FindCookiesForHostAndDomain(url, options, true, &cookies);
1389 std::sort(cookies.begin(), cookies.end(), CookieSorter);
1391 std::string cookie_line = BuildCookieLine(cookies);
1393 VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line;
1395 return cookie_line;
1398 void CookieMonster::DeleteCookie(const GURL& url,
1399 const std::string& cookie_name) {
1400 base::AutoLock autolock(lock_);
1402 if (!HasCookieableScheme(url))
1403 return;
1405 CookieOptions options;
1406 options.set_include_httponly();
1407 options.set_include_first_party_only();
1408 // Get the cookies for this host and its domain(s).
1409 std::vector<CanonicalCookie*> cookies;
1410 FindCookiesForHostAndDomain(url, options, true, &cookies);
1411 std::set<CanonicalCookie*> matching_cookies;
1413 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin();
1414 it != cookies.end(); ++it) {
1415 if ((*it)->Name() != cookie_name)
1416 continue;
1417 if (url.path().find((*it)->Path()))
1418 continue;
1419 matching_cookies.insert(*it);
1422 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1423 CookieMap::iterator curit = it;
1424 ++it;
1425 if (matching_cookies.find(curit->second) != matching_cookies.end()) {
1426 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT);
1431 int CookieMonster::DeleteSessionCookies() {
1432 base::AutoLock autolock(lock_);
1434 int num_deleted = 0;
1435 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1436 CookieMap::iterator curit = it;
1437 CanonicalCookie* cc = curit->second;
1438 ++it;
1440 if (!cc->IsPersistent()) {
1441 InternalDeleteCookie(curit, true, /*sync_to_store*/
1442 DELETE_COOKIE_EXPIRED);
1443 ++num_deleted;
1447 return num_deleted;
1450 bool CookieMonster::HasCookiesForETLDP1(const std::string& etldp1) {
1451 base::AutoLock autolock(lock_);
1453 const std::string key(GetKey(etldp1));
1455 CookieMapItPair its = cookies_.equal_range(key);
1456 return its.first != its.second;
1459 CookieMonster* CookieMonster::GetCookieMonster() {
1460 return this;
1463 // This function must be called before the CookieMonster is used.
1464 void CookieMonster::SetPersistSessionCookies(bool persist_session_cookies) {
1465 DCHECK(!initialized_);
1466 persist_session_cookies_ = persist_session_cookies;
1469 void CookieMonster::SetForceKeepSessionState() {
1470 if (store_.get()) {
1471 store_->SetForceKeepSessionState();
1475 CookieMonster::~CookieMonster() {
1476 DeleteAll(false);
1479 bool CookieMonster::SetCookieWithCreationTime(const GURL& url,
1480 const std::string& cookie_line,
1481 const base::Time& creation_time) {
1482 DCHECK(!store_.get()) << "This method is only to be used by unit-tests.";
1483 base::AutoLock autolock(lock_);
1485 if (!HasCookieableScheme(url)) {
1486 return false;
1489 MarkCookieStoreAsInitialized();
1490 if (ShouldFetchAllCookiesWhenFetchingAnyCookie())
1491 FetchAllCookiesIfNecessary();
1493 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time,
1494 CookieOptions());
1497 void CookieMonster::MarkCookieStoreAsInitialized() {
1498 initialized_ = true;
1501 void CookieMonster::FetchAllCookiesIfNecessary() {
1502 if (store_.get() && !started_fetching_all_cookies_) {
1503 started_fetching_all_cookies_ = true;
1504 FetchAllCookies();
1508 bool CookieMonster::ShouldFetchAllCookiesWhenFetchingAnyCookie() {
1509 if (fetch_strategy_ == kUnknownFetch) {
1510 const std::string group_name =
1511 base::FieldTrialList::FindFullName(kCookieMonsterFetchStrategyName);
1512 if (group_name == kFetchWhenNecessaryName) {
1513 fetch_strategy_ = kFetchWhenNecessary;
1514 } else if (group_name == kAlwaysFetchName) {
1515 fetch_strategy_ = kAlwaysFetch;
1516 } else {
1517 // The logic in the conditional is redundant, but it makes trials of
1518 // the Finch experiment more explicit.
1519 fetch_strategy_ = kAlwaysFetch;
1523 return fetch_strategy_ == kAlwaysFetch;
1526 void CookieMonster::FetchAllCookies() {
1527 DCHECK(store_.get()) << "Store must exist to initialize";
1528 DCHECK(!finished_fetching_all_cookies_)
1529 << "All cookies have already been fetched.";
1531 // We bind in the current time so that we can report the wall-clock time for
1532 // loading cookies.
1533 store_->Load(base::Bind(&CookieMonster::OnLoaded, this, TimeTicks::Now()));
1536 void CookieMonster::OnLoaded(TimeTicks beginning_time,
1537 const std::vector<CanonicalCookie*>& cookies) {
1538 StoreLoadedCookies(cookies);
1539 histogram_time_blocked_on_load_->AddTime(TimeTicks::Now() - beginning_time);
1541 // Invoke the task queue of cookie request.
1542 InvokeQueue();
1545 void CookieMonster::OnKeyLoaded(const std::string& key,
1546 const std::vector<CanonicalCookie*>& cookies) {
1547 // This function does its own separate locking.
1548 StoreLoadedCookies(cookies);
1550 std::deque<scoped_refptr<CookieMonsterTask>> tasks_pending_for_key;
1552 // We need to do this repeatedly until no more tasks were added to the queue
1553 // during the period where we release the lock.
1554 while (true) {
1556 base::AutoLock autolock(lock_);
1557 std::map<std::string,
1558 std::deque<scoped_refptr<CookieMonsterTask>>>::iterator it =
1559 tasks_pending_for_key_.find(key);
1560 if (it == tasks_pending_for_key_.end()) {
1561 keys_loaded_.insert(key);
1562 return;
1564 if (it->second.empty()) {
1565 keys_loaded_.insert(key);
1566 tasks_pending_for_key_.erase(it);
1567 return;
1569 it->second.swap(tasks_pending_for_key);
1572 while (!tasks_pending_for_key.empty()) {
1573 scoped_refptr<CookieMonsterTask> task = tasks_pending_for_key.front();
1574 task->Run();
1575 tasks_pending_for_key.pop_front();
1580 void CookieMonster::StoreLoadedCookies(
1581 const std::vector<CanonicalCookie*>& cookies) {
1582 // TODO(erikwright): Remove ScopedTracker below once crbug.com/457528 is
1583 // fixed.
1584 tracked_objects::ScopedTracker tracking_profile(
1585 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1586 "457528 CookieMonster::StoreLoadedCookies"));
1588 // Initialize the store and sync in any saved persistent cookies. We don't
1589 // care if it's expired, insert it so it can be garbage collected, removed,
1590 // and sync'd.
1591 base::AutoLock autolock(lock_);
1593 CookieItVector cookies_with_control_chars;
1595 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin();
1596 it != cookies.end(); ++it) {
1597 int64 cookie_creation_time = (*it)->CreationDate().ToInternalValue();
1599 if (creation_times_.insert(cookie_creation_time).second) {
1600 CookieMap::iterator inserted =
1601 InternalInsertCookie(GetKey((*it)->Domain()), *it, false);
1602 const Time cookie_access_time((*it)->LastAccessDate());
1603 if (earliest_access_time_.is_null() ||
1604 cookie_access_time < earliest_access_time_)
1605 earliest_access_time_ = cookie_access_time;
1607 if (ContainsControlCharacter((*it)->Name()) ||
1608 ContainsControlCharacter((*it)->Value())) {
1609 cookies_with_control_chars.push_back(inserted);
1611 } else {
1612 LOG(ERROR) << base::StringPrintf(
1613 "Found cookies with duplicate creation "
1614 "times in backing store: "
1615 "{name='%s', domain='%s', path='%s'}",
1616 (*it)->Name().c_str(), (*it)->Domain().c_str(),
1617 (*it)->Path().c_str());
1618 // We've been given ownership of the cookie and are throwing it
1619 // away; reclaim the space.
1620 delete (*it);
1624 // Any cookies that contain control characters that we have loaded from the
1625 // persistent store should be deleted. See http://crbug.com/238041.
1626 for (CookieItVector::iterator it = cookies_with_control_chars.begin();
1627 it != cookies_with_control_chars.end();) {
1628 CookieItVector::iterator curit = it;
1629 ++it;
1631 InternalDeleteCookie(*curit, true, DELETE_COOKIE_CONTROL_CHAR);
1634 // After importing cookies from the PersistentCookieStore, verify that
1635 // none of our other constraints are violated.
1636 // In particular, the backing store might have given us duplicate cookies.
1638 // This method could be called multiple times due to priority loading, thus
1639 // cookies loaded in previous runs will be validated again, but this is OK
1640 // since they are expected to be much fewer than total DB.
1641 EnsureCookiesMapIsValid();
1644 void CookieMonster::InvokeQueue() {
1645 while (true) {
1646 scoped_refptr<CookieMonsterTask> request_task;
1648 base::AutoLock autolock(lock_);
1649 if (tasks_pending_.empty()) {
1650 finished_fetching_all_cookies_ = true;
1651 creation_times_.clear();
1652 keys_loaded_.clear();
1653 break;
1655 request_task = tasks_pending_.front();
1656 tasks_pending_.pop();
1658 request_task->Run();
1662 void CookieMonster::EnsureCookiesMapIsValid() {
1663 lock_.AssertAcquired();
1665 int num_duplicates_trimmed = 0;
1667 // Iterate through all the of the cookies, grouped by host.
1668 CookieMap::iterator prev_range_end = cookies_.begin();
1669 while (prev_range_end != cookies_.end()) {
1670 CookieMap::iterator cur_range_begin = prev_range_end;
1671 const std::string key = cur_range_begin->first; // Keep a copy.
1672 CookieMap::iterator cur_range_end = cookies_.upper_bound(key);
1673 prev_range_end = cur_range_end;
1675 // Ensure no equivalent cookies for this host.
1676 num_duplicates_trimmed +=
1677 TrimDuplicateCookiesForKey(key, cur_range_begin, cur_range_end);
1680 // Record how many duplicates were found in the database.
1681 // See InitializeHistograms() for details.
1682 histogram_number_duplicate_db_cookies_->Add(num_duplicates_trimmed);
1685 int CookieMonster::TrimDuplicateCookiesForKey(const std::string& key,
1686 CookieMap::iterator begin,
1687 CookieMap::iterator end) {
1688 lock_.AssertAcquired();
1690 // Set of cookies ordered by creation time.
1691 typedef std::set<CookieMap::iterator, OrderByCreationTimeDesc> CookieSet;
1693 // Helper map we populate to find the duplicates.
1694 typedef std::map<CookieSignature, CookieSet> EquivalenceMap;
1695 EquivalenceMap equivalent_cookies;
1697 // The number of duplicate cookies that have been found.
1698 int num_duplicates = 0;
1700 // Iterate through all of the cookies in our range, and insert them into
1701 // the equivalence map.
1702 for (CookieMap::iterator it = begin; it != end; ++it) {
1703 DCHECK_EQ(key, it->first);
1704 CanonicalCookie* cookie = it->second;
1706 CookieSignature signature(cookie->Name(), cookie->Domain(), cookie->Path());
1707 CookieSet& set = equivalent_cookies[signature];
1709 // We found a duplicate!
1710 if (!set.empty())
1711 num_duplicates++;
1713 // We save the iterator into |cookies_| rather than the actual cookie
1714 // pointer, since we may need to delete it later.
1715 bool insert_success = set.insert(it).second;
1716 DCHECK(insert_success)
1717 << "Duplicate creation times found in duplicate cookie name scan.";
1720 // If there were no duplicates, we are done!
1721 if (num_duplicates == 0)
1722 return 0;
1724 // Make sure we find everything below that we did above.
1725 int num_duplicates_found = 0;
1727 // Otherwise, delete all the duplicate cookies, both from our in-memory store
1728 // and from the backing store.
1729 for (EquivalenceMap::iterator it = equivalent_cookies.begin();
1730 it != equivalent_cookies.end(); ++it) {
1731 const CookieSignature& signature = it->first;
1732 CookieSet& dupes = it->second;
1734 if (dupes.size() <= 1)
1735 continue; // This cookiename/path has no duplicates.
1736 num_duplicates_found += dupes.size() - 1;
1738 // Since |dups| is sorted by creation time (descending), the first cookie
1739 // is the most recent one, so we will keep it. The rest are duplicates.
1740 dupes.erase(dupes.begin());
1742 LOG(ERROR) << base::StringPrintf(
1743 "Found %d duplicate cookies for host='%s', "
1744 "with {name='%s', domain='%s', path='%s'}",
1745 static_cast<int>(dupes.size()), key.c_str(), signature.name.c_str(),
1746 signature.domain.c_str(), signature.path.c_str());
1748 // Remove all the cookies identified by |dupes|. It is valid to delete our
1749 // list of iterators one at a time, since |cookies_| is a multimap (they
1750 // don't invalidate existing iterators following deletion).
1751 for (CookieSet::iterator dupes_it = dupes.begin(); dupes_it != dupes.end();
1752 ++dupes_it) {
1753 InternalDeleteCookie(*dupes_it, true,
1754 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE);
1757 DCHECK_EQ(num_duplicates, num_duplicates_found);
1759 return num_duplicates;
1762 // Note: file must be the last scheme.
1763 const char* const CookieMonster::kDefaultCookieableSchemes[] = {"http",
1764 "https",
1765 "ws",
1766 "wss",
1767 "file"};
1768 const int CookieMonster::kDefaultCookieableSchemesCount =
1769 arraysize(kDefaultCookieableSchemes);
1771 void CookieMonster::SetDefaultCookieableSchemes() {
1772 // Always disable file scheme unless SetEnableFileScheme(true) is called.
1773 SetCookieableSchemes(kDefaultCookieableSchemes,
1774 kDefaultCookieableSchemesCount - 1);
1777 void CookieMonster::FindCookiesForHostAndDomain(
1778 const GURL& url,
1779 const CookieOptions& options,
1780 bool update_access_time,
1781 std::vector<CanonicalCookie*>* cookies) {
1782 lock_.AssertAcquired();
1784 const Time current_time(CurrentTime());
1786 // Probe to save statistics relatively frequently. We do it here rather
1787 // than in the set path as many websites won't set cookies, and we
1788 // want to collect statistics whenever the browser's being used.
1789 RecordPeriodicStats(current_time);
1791 // Can just dispatch to FindCookiesForKey
1792 const std::string key(GetKey(url.host()));
1793 FindCookiesForKey(key, url, options, current_time, update_access_time,
1794 cookies);
1797 void CookieMonster::FindCookiesForKey(const std::string& key,
1798 const GURL& url,
1799 const CookieOptions& options,
1800 const Time& current,
1801 bool update_access_time,
1802 std::vector<CanonicalCookie*>* cookies) {
1803 lock_.AssertAcquired();
1805 for (CookieMapItPair its = cookies_.equal_range(key);
1806 its.first != its.second;) {
1807 CookieMap::iterator curit = its.first;
1808 CanonicalCookie* cc = curit->second;
1809 ++its.first;
1811 // If the cookie is expired, delete it.
1812 if (cc->IsExpired(current) && !keep_expired_cookies_) {
1813 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED);
1814 continue;
1817 // Filter out cookies that should not be included for a request to the
1818 // given |url|. HTTP only cookies are filtered depending on the passed
1819 // cookie |options|.
1820 if (!cc->IncludeForRequestURL(url, options))
1821 continue;
1823 // Add this cookie to the set of matching cookies. Update the access
1824 // time if we've been requested to do so.
1825 if (update_access_time) {
1826 InternalUpdateCookieAccessTime(cc, current);
1828 cookies->push_back(cc);
1832 bool CookieMonster::DeleteAnyEquivalentCookie(const std::string& key,
1833 const CanonicalCookie& ecc,
1834 bool skip_httponly,
1835 bool already_expired) {
1836 lock_.AssertAcquired();
1838 bool found_equivalent_cookie = false;
1839 bool skipped_httponly = false;
1840 for (CookieMapItPair its = cookies_.equal_range(key);
1841 its.first != its.second;) {
1842 CookieMap::iterator curit = its.first;
1843 CanonicalCookie* cc = curit->second;
1844 ++its.first;
1846 if (ecc.IsEquivalent(*cc)) {
1847 // We should never have more than one equivalent cookie, since they should
1848 // overwrite each other.
1849 CHECK(!found_equivalent_cookie)
1850 << "Duplicate equivalent cookies found, cookie store is corrupted.";
1851 if (skip_httponly && cc->IsHttpOnly()) {
1852 skipped_httponly = true;
1853 } else {
1854 InternalDeleteCookie(curit, true, already_expired
1855 ? DELETE_COOKIE_EXPIRED_OVERWRITE
1856 : DELETE_COOKIE_OVERWRITE);
1858 found_equivalent_cookie = true;
1861 return skipped_httponly;
1864 CookieMonster::CookieMap::iterator CookieMonster::InternalInsertCookie(
1865 const std::string& key,
1866 CanonicalCookie* cc,
1867 bool sync_to_store) {
1868 // TODO(mkwst): Remove ScopedTracker below once crbug.com/456373 is fixed.
1869 tracked_objects::ScopedTracker tracking_profile(
1870 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1871 "456373 CookieMonster::InternalInsertCookie"));
1872 lock_.AssertAcquired();
1874 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() &&
1875 sync_to_store)
1876 store_->AddCookie(*cc);
1877 CookieMap::iterator inserted =
1878 cookies_.insert(CookieMap::value_type(key, cc));
1879 if (delegate_.get()) {
1880 delegate_->OnCookieChanged(*cc, false,
1881 CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT);
1884 // See InitializeHistograms() for details.
1885 int32_t sample = cc->IsFirstPartyOnly() ? 1 << COOKIE_TYPE_FIRSTPARTYONLY : 0;
1886 sample |= cc->IsHttpOnly() ? 1 << COOKIE_TYPE_HTTPONLY : 0;
1887 sample |= cc->IsSecure() ? 1 << COOKIE_TYPE_SECURE : 0;
1888 histogram_cookie_type_->Add(sample);
1890 RunCallbacks(*cc, false);
1892 return inserted;
1895 bool CookieMonster::SetCookieWithCreationTimeAndOptions(
1896 const GURL& url,
1897 const std::string& cookie_line,
1898 const Time& creation_time_or_null,
1899 const CookieOptions& options) {
1900 lock_.AssertAcquired();
1902 VLOG(kVlogSetCookies) << "SetCookie() line: " << cookie_line;
1904 Time creation_time = creation_time_or_null;
1905 if (creation_time.is_null()) {
1906 creation_time = CurrentTime();
1907 last_time_seen_ = creation_time;
1910 scoped_ptr<CanonicalCookie> cc(
1911 CanonicalCookie::Create(url, cookie_line, creation_time, options));
1913 if (!cc.get()) {
1914 VLOG(kVlogSetCookies) << "WARNING: Failed to allocate CanonicalCookie";
1915 return false;
1917 return SetCanonicalCookie(&cc, creation_time, options);
1920 bool CookieMonster::SetCanonicalCookie(scoped_ptr<CanonicalCookie>* cc,
1921 const Time& creation_time,
1922 const CookieOptions& options) {
1923 const std::string key(GetKey((*cc)->Domain()));
1924 bool already_expired = (*cc)->IsExpired(creation_time);
1926 if (DeleteAnyEquivalentCookie(key, **cc, options.exclude_httponly(),
1927 already_expired)) {
1928 VLOG(kVlogSetCookies) << "SetCookie() not clobbering httponly cookie";
1929 return false;
1932 VLOG(kVlogSetCookies) << "SetCookie() key: " << key
1933 << " cc: " << (*cc)->DebugString();
1935 // Realize that we might be setting an expired cookie, and the only point
1936 // was to delete the cookie which we've already done.
1937 if (!already_expired || keep_expired_cookies_) {
1938 // See InitializeHistograms() for details.
1939 if ((*cc)->IsPersistent()) {
1940 histogram_expiration_duration_minutes_->Add(
1941 ((*cc)->ExpiryDate() - creation_time).InMinutes());
1945 CanonicalCookie cookie = *(cc->get());
1946 InternalInsertCookie(key, cc->release(), true);
1948 } else {
1949 VLOG(kVlogSetCookies) << "SetCookie() not storing already expired cookie.";
1952 // We assume that hopefully setting a cookie will be less common than
1953 // querying a cookie. Since setting a cookie can put us over our limits,
1954 // make sure that we garbage collect... We can also make the assumption that
1955 // if a cookie was set, in the common case it will be used soon after,
1956 // and we will purge the expired cookies in GetCookies().
1957 GarbageCollect(creation_time, key);
1959 return true;
1962 bool CookieMonster::SetCanonicalCookies(const CookieList& list) {
1963 base::AutoLock autolock(lock_);
1965 CookieOptions options;
1966 options.set_include_httponly();
1968 for (CookieList::const_iterator it = list.begin(); it != list.end(); ++it) {
1969 scoped_ptr<CanonicalCookie> canonical_cookie(new CanonicalCookie(*it));
1970 if (!SetCanonicalCookie(&canonical_cookie, it->CreationDate(), options))
1971 return false;
1974 return true;
1977 void CookieMonster::InternalUpdateCookieAccessTime(CanonicalCookie* cc,
1978 const Time& current) {
1979 lock_.AssertAcquired();
1981 // Based off the Mozilla code. When a cookie has been accessed recently,
1982 // don't bother updating its access time again. This reduces the number of
1983 // updates we do during pageload, which in turn reduces the chance our storage
1984 // backend will hit its batch thresholds and be forced to update.
1985 if ((current - cc->LastAccessDate()) < last_access_threshold_)
1986 return;
1988 // See InitializeHistograms() for details.
1989 histogram_between_access_interval_minutes_->Add(
1990 (current - cc->LastAccessDate()).InMinutes());
1992 cc->SetLastAccessDate(current);
1993 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get())
1994 store_->UpdateCookieAccessTime(*cc);
1997 // InternalDeleteCookies must not invalidate iterators other than the one being
1998 // deleted.
1999 void CookieMonster::InternalDeleteCookie(CookieMap::iterator it,
2000 bool sync_to_store,
2001 DeletionCause deletion_cause) {
2002 lock_.AssertAcquired();
2004 // Ideally, this would be asserted up where we define ChangeCauseMapping,
2005 // but DeletionCause's visibility (or lack thereof) forces us to make
2006 // this check here.
2007 static_assert(arraysize(ChangeCauseMapping) == DELETE_COOKIE_LAST_ENTRY + 1,
2008 "ChangeCauseMapping size should match DeletionCause size");
2010 // See InitializeHistograms() for details.
2011 if (deletion_cause != DELETE_COOKIE_DONT_RECORD)
2012 histogram_cookie_deletion_cause_->Add(deletion_cause);
2014 CanonicalCookie* cc = it->second;
2015 VLOG(kVlogSetCookies) << "InternalDeleteCookie()"
2016 << ", cause:" << deletion_cause
2017 << ", cc: " << cc->DebugString();
2019 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() &&
2020 sync_to_store)
2021 store_->DeleteCookie(*cc);
2022 if (delegate_.get()) {
2023 ChangeCausePair mapping = ChangeCauseMapping[deletion_cause];
2025 if (mapping.notify)
2026 delegate_->OnCookieChanged(*cc, true, mapping.cause);
2028 RunCallbacks(*cc, true);
2029 cookies_.erase(it);
2030 delete cc;
2033 // Domain expiry behavior is unchanged by key/expiry scheme (the
2034 // meaning of the key is different, but that's not visible to this routine).
2035 int CookieMonster::GarbageCollect(const Time& current, const std::string& key) {
2036 lock_.AssertAcquired();
2038 int num_deleted = 0;
2039 Time safe_date(Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays));
2041 // Collect garbage for this key, minding cookie priorities.
2042 if (cookies_.count(key) > kDomainMaxCookies) {
2043 VLOG(kVlogGarbageCollection) << "GarbageCollect() key: " << key;
2045 CookieItVector cookie_its;
2046 num_deleted +=
2047 GarbageCollectExpired(current, cookies_.equal_range(key), &cookie_its);
2048 if (cookie_its.size() > kDomainMaxCookies) {
2049 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect domain.";
2050 size_t purge_goal =
2051 cookie_its.size() - (kDomainMaxCookies - kDomainPurgeCookies);
2052 DCHECK(purge_goal > kDomainPurgeCookies);
2054 // Boundary iterators into |cookie_its| for different priorities.
2055 CookieItVector::iterator it_bdd[4];
2056 // Intialize |it_bdd| while sorting |cookie_its| by priorities.
2057 // Schematic: [MLLHMHHLMM] => [LLL|MMMM|HHH], with 4 boundaries.
2058 it_bdd[0] = cookie_its.begin();
2059 it_bdd[3] = cookie_its.end();
2060 it_bdd[1] =
2061 PartitionCookieByPriority(it_bdd[0], it_bdd[3], COOKIE_PRIORITY_LOW);
2062 it_bdd[2] = PartitionCookieByPriority(it_bdd[1], it_bdd[3],
2063 COOKIE_PRIORITY_MEDIUM);
2064 size_t quota[3] = {kDomainCookiesQuotaLow,
2065 kDomainCookiesQuotaMedium,
2066 kDomainCookiesQuotaHigh};
2068 // Purge domain cookies in 3 rounds.
2069 // Round 1: consider low-priority cookies only: evict least-recently
2070 // accessed, while protecting quota[0] of these from deletion.
2071 // Round 2: consider {low, medium}-priority cookies, evict least-recently
2072 // accessed, while protecting quota[0] + quota[1].
2073 // Round 3: consider all cookies, evict least-recently accessed.
2074 size_t accumulated_quota = 0;
2075 CookieItVector::iterator it_purge_begin = it_bdd[0];
2076 for (int i = 0; i < 3 && purge_goal > 0; ++i) {
2077 accumulated_quota += quota[i];
2079 size_t num_considered = it_bdd[i + 1] - it_purge_begin;
2080 if (num_considered <= accumulated_quota)
2081 continue;
2083 // Number of cookies that will be purged in this round.
2084 size_t round_goal =
2085 std::min(purge_goal, num_considered - accumulated_quota);
2086 purge_goal -= round_goal;
2088 SortLeastRecentlyAccessed(it_purge_begin, it_bdd[i + 1], round_goal);
2089 // Cookies accessed on or after |safe_date| would have been safe from
2090 // global purge, and we want to keep track of this.
2091 CookieItVector::iterator it_purge_end = it_purge_begin + round_goal;
2092 CookieItVector::iterator it_purge_middle =
2093 LowerBoundAccessDate(it_purge_begin, it_purge_end, safe_date);
2094 // Delete cookies accessed before |safe_date|.
2095 num_deleted += GarbageCollectDeleteRange(
2096 current, DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE, it_purge_begin,
2097 it_purge_middle);
2098 // Delete cookies accessed on or after |safe_date|.
2099 num_deleted += GarbageCollectDeleteRange(
2100 current, DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE, it_purge_middle,
2101 it_purge_end);
2102 it_purge_begin = it_purge_end;
2104 DCHECK_EQ(0U, purge_goal);
2108 // Collect garbage for everything. With firefox style we want to preserve
2109 // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict.
2110 if (cookies_.size() > kMaxCookies && earliest_access_time_ < safe_date) {
2111 VLOG(kVlogGarbageCollection) << "GarbageCollect() everything";
2112 CookieItVector cookie_its;
2113 num_deleted += GarbageCollectExpired(
2114 current, CookieMapItPair(cookies_.begin(), cookies_.end()),
2115 &cookie_its);
2116 if (cookie_its.size() > kMaxCookies) {
2117 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect everything.";
2118 size_t purge_goal = cookie_its.size() - (kMaxCookies - kPurgeCookies);
2119 DCHECK(purge_goal > kPurgeCookies);
2120 // Sorts up to *and including* |cookie_its[purge_goal]|, so
2121 // |earliest_access_time| will be properly assigned even if
2122 // |global_purge_it| == |cookie_its.begin() + purge_goal|.
2123 SortLeastRecentlyAccessed(cookie_its.begin(), cookie_its.end(),
2124 purge_goal);
2125 // Find boundary to cookies older than safe_date.
2126 CookieItVector::iterator global_purge_it = LowerBoundAccessDate(
2127 cookie_its.begin(), cookie_its.begin() + purge_goal, safe_date);
2128 // Only delete the old cookies.
2129 num_deleted +=
2130 GarbageCollectDeleteRange(current, DELETE_COOKIE_EVICTED_GLOBAL,
2131 cookie_its.begin(), global_purge_it);
2132 // Set access day to the oldest cookie that wasn't deleted.
2133 earliest_access_time_ = (*global_purge_it)->second->LastAccessDate();
2137 return num_deleted;
2140 int CookieMonster::GarbageCollectExpired(const Time& current,
2141 const CookieMapItPair& itpair,
2142 CookieItVector* cookie_its) {
2143 if (keep_expired_cookies_)
2144 return 0;
2146 lock_.AssertAcquired();
2148 int num_deleted = 0;
2149 for (CookieMap::iterator it = itpair.first, end = itpair.second; it != end;) {
2150 CookieMap::iterator curit = it;
2151 ++it;
2153 if (curit->second->IsExpired(current)) {
2154 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED);
2155 ++num_deleted;
2156 } else if (cookie_its) {
2157 cookie_its->push_back(curit);
2161 return num_deleted;
2164 int CookieMonster::GarbageCollectDeleteRange(const Time& current,
2165 DeletionCause cause,
2166 CookieItVector::iterator it_begin,
2167 CookieItVector::iterator it_end) {
2168 for (CookieItVector::iterator it = it_begin; it != it_end; it++) {
2169 histogram_evicted_last_access_minutes_->Add(
2170 (current - (*it)->second->LastAccessDate()).InMinutes());
2171 InternalDeleteCookie((*it), true, cause);
2173 return it_end - it_begin;
2176 // A wrapper around registry_controlled_domains::GetDomainAndRegistry
2177 // to make clear we're creating a key for our local map. Here and
2178 // in FindCookiesForHostAndDomain() are the only two places where
2179 // we need to conditionalize based on key type.
2181 // Note that this key algorithm explicitly ignores the scheme. This is
2182 // because when we're entering cookies into the map from the backing store,
2183 // we in general won't have the scheme at that point.
2184 // In practical terms, this means that file cookies will be stored
2185 // in the map either by an empty string or by UNC name (and will be
2186 // limited by kMaxCookiesPerHost), and extension cookies will be stored
2187 // based on the single extension id, as the extension id won't have the
2188 // form of a DNS host and hence GetKey() will return it unchanged.
2190 // Arguably the right thing to do here is to make the key
2191 // algorithm dependent on the scheme, and make sure that the scheme is
2192 // available everywhere the key must be obtained (specfically at backing
2193 // store load time). This would require either changing the backing store
2194 // database schema to include the scheme (far more trouble than it's worth), or
2195 // separating out file cookies into their own CookieMonster instance and
2196 // thus restricting each scheme to a single cookie monster (which might
2197 // be worth it, but is still too much trouble to solve what is currently a
2198 // non-problem).
2199 std::string CookieMonster::GetKey(const std::string& domain) const {
2200 std::string effective_domain(
2201 registry_controlled_domains::GetDomainAndRegistry(
2202 domain, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
2203 if (effective_domain.empty())
2204 effective_domain = domain;
2206 if (!effective_domain.empty() && effective_domain[0] == '.')
2207 return effective_domain.substr(1);
2208 return effective_domain;
2211 bool CookieMonster::IsCookieableScheme(const std::string& scheme) {
2212 base::AutoLock autolock(lock_);
2214 return std::find(cookieable_schemes_.begin(), cookieable_schemes_.end(),
2215 scheme) != cookieable_schemes_.end();
2218 bool CookieMonster::HasCookieableScheme(const GURL& url) {
2219 lock_.AssertAcquired();
2221 // Make sure the request is on a cookie-able url scheme.
2222 for (size_t i = 0; i < cookieable_schemes_.size(); ++i) {
2223 // We matched a scheme.
2224 if (url.SchemeIs(cookieable_schemes_[i].c_str())) {
2225 // We've matched a supported scheme.
2226 return true;
2230 // The scheme didn't match any in our whitelist.
2231 VLOG(kVlogPerCookieMonster)
2232 << "WARNING: Unsupported cookie scheme: " << url.scheme();
2233 return false;
2236 // Test to see if stats should be recorded, and record them if so.
2237 // The goal here is to get sampling for the average browser-hour of
2238 // activity. We won't take samples when the web isn't being surfed,
2239 // and when the web is being surfed, we'll take samples about every
2240 // kRecordStatisticsIntervalSeconds.
2241 // last_statistic_record_time_ is initialized to Now() rather than null
2242 // in the constructor so that we won't take statistics right after
2243 // startup, to avoid bias from browsers that are started but not used.
2244 void CookieMonster::RecordPeriodicStats(const base::Time& current_time) {
2245 const base::TimeDelta kRecordStatisticsIntervalTime(
2246 base::TimeDelta::FromSeconds(kRecordStatisticsIntervalSeconds));
2248 // If we've taken statistics recently, return.
2249 if (current_time - last_statistic_record_time_ <=
2250 kRecordStatisticsIntervalTime) {
2251 return;
2254 // See InitializeHistograms() for details.
2255 histogram_count_->Add(cookies_.size());
2257 // More detailed statistics on cookie counts at different granularities.
2258 TimeTicks beginning_of_time(TimeTicks::Now());
2260 for (CookieMap::const_iterator it_key = cookies_.begin();
2261 it_key != cookies_.end();) {
2262 const std::string& key(it_key->first);
2264 int key_count = 0;
2265 typedef std::map<std::string, unsigned int> DomainMap;
2266 DomainMap domain_map;
2267 CookieMapItPair its_cookies = cookies_.equal_range(key);
2268 while (its_cookies.first != its_cookies.second) {
2269 key_count++;
2270 const std::string& cookie_domain(its_cookies.first->second->Domain());
2271 domain_map[cookie_domain]++;
2273 its_cookies.first++;
2275 histogram_etldp1_count_->Add(key_count);
2276 histogram_domain_per_etldp1_count_->Add(domain_map.size());
2277 for (DomainMap::const_iterator domain_map_it = domain_map.begin();
2278 domain_map_it != domain_map.end(); domain_map_it++)
2279 histogram_domain_count_->Add(domain_map_it->second);
2281 it_key = its_cookies.second;
2284 VLOG(kVlogPeriodic) << "Time for recording cookie stats (us): "
2285 << (TimeTicks::Now() - beginning_of_time)
2286 .InMicroseconds();
2288 last_statistic_record_time_ = current_time;
2291 // Initialize all histogram counter variables used in this class.
2293 // Normal histogram usage involves using the macros defined in
2294 // histogram.h, which automatically takes care of declaring these
2295 // variables (as statics), initializing them, and accumulating into
2296 // them, all from a single entry point. Unfortunately, that solution
2297 // doesn't work for the CookieMonster, as it's vulnerable to races between
2298 // separate threads executing the same functions and hence initializing the
2299 // same static variables. There isn't a race danger in the histogram
2300 // accumulation calls; they are written to be resilient to simultaneous
2301 // calls from multiple threads.
2303 // The solution taken here is to have per-CookieMonster instance
2304 // variables that are constructed during CookieMonster construction.
2305 // Note that these variables refer to the same underlying histogram,
2306 // so we still race (but safely) with other CookieMonster instances
2307 // for accumulation.
2309 // To do this we've expanded out the individual histogram macros calls,
2310 // with declarations of the variables in the class decl, initialization here
2311 // (done from the class constructor) and direct calls to the accumulation
2312 // methods where needed. The specific histogram macro calls on which the
2313 // initialization is based are included in comments below.
2314 void CookieMonster::InitializeHistograms() {
2315 // From UMA_HISTOGRAM_CUSTOM_COUNTS
2316 histogram_expiration_duration_minutes_ = base::Histogram::FactoryGet(
2317 "Cookie.ExpirationDurationMinutes", 1, kMinutesInTenYears, 50,
2318 base::Histogram::kUmaTargetedHistogramFlag);
2319 histogram_between_access_interval_minutes_ = base::Histogram::FactoryGet(
2320 "Cookie.BetweenAccessIntervalMinutes", 1, kMinutesInTenYears, 50,
2321 base::Histogram::kUmaTargetedHistogramFlag);
2322 histogram_evicted_last_access_minutes_ = base::Histogram::FactoryGet(
2323 "Cookie.EvictedLastAccessMinutes", 1, kMinutesInTenYears, 50,
2324 base::Histogram::kUmaTargetedHistogramFlag);
2325 histogram_count_ = base::Histogram::FactoryGet(
2326 "Cookie.Count", 1, 4000, 50, base::Histogram::kUmaTargetedHistogramFlag);
2327 histogram_domain_count_ =
2328 base::Histogram::FactoryGet("Cookie.DomainCount", 1, 4000, 50,
2329 base::Histogram::kUmaTargetedHistogramFlag);
2330 histogram_etldp1_count_ =
2331 base::Histogram::FactoryGet("Cookie.Etldp1Count", 1, 4000, 50,
2332 base::Histogram::kUmaTargetedHistogramFlag);
2333 histogram_domain_per_etldp1_count_ =
2334 base::Histogram::FactoryGet("Cookie.DomainPerEtldp1Count", 1, 4000, 50,
2335 base::Histogram::kUmaTargetedHistogramFlag);
2337 // From UMA_HISTOGRAM_COUNTS_10000 & UMA_HISTOGRAM_CUSTOM_COUNTS
2338 histogram_number_duplicate_db_cookies_ =
2339 base::Histogram::FactoryGet("Net.NumDuplicateCookiesInDb", 1, 10000, 50,
2340 base::Histogram::kUmaTargetedHistogramFlag);
2342 // From UMA_HISTOGRAM_ENUMERATION
2343 histogram_cookie_deletion_cause_ = base::LinearHistogram::FactoryGet(
2344 "Cookie.DeletionCause", 1, DELETE_COOKIE_LAST_ENTRY - 1,
2345 DELETE_COOKIE_LAST_ENTRY, base::Histogram::kUmaTargetedHistogramFlag);
2346 histogram_cookie_type_ = base::LinearHistogram::FactoryGet(
2347 "Cookie.Type", 1, (1 << COOKIE_TYPE_LAST_ENTRY) - 1,
2348 1 << COOKIE_TYPE_LAST_ENTRY, base::Histogram::kUmaTargetedHistogramFlag);
2350 // From UMA_HISTOGRAM_{CUSTOM_,}TIMES
2351 histogram_time_blocked_on_load_ = base::Histogram::FactoryTimeGet(
2352 "Cookie.TimeBlockedOnLoad", base::TimeDelta::FromMilliseconds(1),
2353 base::TimeDelta::FromMinutes(1), 50,
2354 base::Histogram::kUmaTargetedHistogramFlag);
2357 // The system resolution is not high enough, so we can have multiple
2358 // set cookies that result in the same system time. When this happens, we
2359 // increment by one Time unit. Let's hope computers don't get too fast.
2360 Time CookieMonster::CurrentTime() {
2361 return std::max(Time::Now(), Time::FromInternalValue(
2362 last_time_seen_.ToInternalValue() + 1));
2365 void CookieMonster::ComputeCookieDiff(CookieList* old_cookies,
2366 CookieList* new_cookies,
2367 CookieList* cookies_to_add,
2368 CookieList* cookies_to_delete) {
2369 DCHECK(old_cookies);
2370 DCHECK(new_cookies);
2371 DCHECK(cookies_to_add);
2372 DCHECK(cookies_to_delete);
2373 DCHECK(cookies_to_add->empty());
2374 DCHECK(cookies_to_delete->empty());
2376 // Sort both lists.
2377 // A set ordered by FullDiffCookieSorter is also ordered by
2378 // PartialDiffCookieSorter.
2379 std::sort(old_cookies->begin(), old_cookies->end(), FullDiffCookieSorter);
2380 std::sort(new_cookies->begin(), new_cookies->end(), FullDiffCookieSorter);
2382 // Select any old cookie for deletion if no new cookie has the same name,
2383 // domain, and path.
2384 std::set_difference(
2385 old_cookies->begin(), old_cookies->end(), new_cookies->begin(),
2386 new_cookies->end(),
2387 std::inserter(*cookies_to_delete, cookies_to_delete->begin()),
2388 PartialDiffCookieSorter);
2390 // Select any new cookie for addition (or update) if no old cookie is exactly
2391 // equivalent.
2392 std::set_difference(new_cookies->begin(), new_cookies->end(),
2393 old_cookies->begin(), old_cookies->end(),
2394 std::inserter(*cookies_to_add, cookies_to_add->begin()),
2395 FullDiffCookieSorter);
2398 scoped_ptr<CookieStore::CookieChangedSubscription>
2399 CookieMonster::AddCallbackForCookie(const GURL& gurl,
2400 const std::string& name,
2401 const CookieChangedCallback& callback) {
2402 base::AutoLock autolock(lock_);
2403 std::pair<GURL, std::string> key(gurl, name);
2404 if (hook_map_.count(key) == 0)
2405 hook_map_[key] = make_linked_ptr(new CookieChangedCallbackList());
2406 return hook_map_[key]->Add(
2407 base::Bind(&RunAsync, base::ThreadTaskRunnerHandle::Get(), callback));
2410 #if defined(OS_ANDROID)
2411 void CookieMonster::SetEnableFileScheme(bool accept) {
2412 // This assumes "file" is always at the end of the array. See the comment
2413 // above kDefaultCookieableSchemes.
2415 // TODO(mkwst): We're keeping this method around to support the
2416 // 'CookieManager::setAcceptFileSchemeCookies' method on Android's WebView;
2417 // if/when we can deprecate and remove that method, we can remove this one
2418 // as well. Until then, we'll just ensure that the method has no effect on
2419 // non-android systems.
2420 int num_schemes = accept ? kDefaultCookieableSchemesCount
2421 : kDefaultCookieableSchemesCount - 1;
2423 SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes);
2425 #endif
2427 void CookieMonster::RunCallbacks(const CanonicalCookie& cookie, bool removed) {
2428 lock_.AssertAcquired();
2429 CookieOptions opts;
2430 opts.set_include_httponly();
2431 opts.set_include_first_party_only();
2432 // Note that the callbacks in hook_map_ are wrapped with MakeAsync(), so they
2433 // are guaranteed to not take long - they just post a RunAsync task back to
2434 // the appropriate thread's message loop and return. It is important that this
2435 // method not run user-supplied callbacks directly, since the CookieMonster
2436 // lock is held and it is easy to accidentally introduce deadlocks.
2437 for (CookieChangedHookMap::iterator it = hook_map_.begin();
2438 it != hook_map_.end(); ++it) {
2439 std::pair<GURL, std::string> key = it->first;
2440 if (cookie.IncludeForRequestURL(key.first, opts) &&
2441 cookie.Name() == key.second) {
2442 it->second->Notify(cookie, removed);
2447 } // namespace net