[email protected] | 8e158367 | 2012-02-11 04:39:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | // Portions of this code based on Mozilla: |
| 6 | // (netwerk/cookie/src/nsCookieService.cpp) |
| 7 | /* ***** BEGIN LICENSE BLOCK ***** |
| 8 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 9 | * |
| 10 | * The contents of this file are subject to the Mozilla Public License Version |
| 11 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 12 | * the License. You may obtain a copy of the License at |
| 13 | * http://www.mozilla.org/MPL/ |
| 14 | * |
| 15 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 16 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 17 | * for the specific language governing rights and limitations under the |
| 18 | * License. |
| 19 | * |
| 20 | * The Original Code is mozilla.org code. |
| 21 | * |
| 22 | * The Initial Developer of the Original Code is |
| 23 | * Netscape Communications Corporation. |
| 24 | * Portions created by the Initial Developer are Copyright (C) 2003 |
| 25 | * the Initial Developer. All Rights Reserved. |
| 26 | * |
| 27 | * Contributor(s): |
| 28 | * Daniel Witte ([email protected]) |
| 29 | * Michiel van Leeuwen ([email protected]) |
| 30 | * |
| 31 | * Alternatively, the contents of this file may be used under the terms of |
| 32 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 33 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 34 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 35 | * of those above. If you wish to allow use of your version of this file only |
| 36 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 37 | * use your version of this file under the terms of the MPL, indicate your |
| 38 | * decision by deleting the provisions above and replace them with the notice |
| 39 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 40 | * the provisions above, a recipient may use your version of this file under |
| 41 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 42 | * |
| 43 | * ***** END LICENSE BLOCK ***** */ |
| 44 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 45 | #include "net/cookies/cookie_monster.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 46 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 47 | #include <functional> |
[email protected] | 0966648 | 2011-07-12 12:50:40 | [diff] [blame] | 48 | #include <set> |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 49 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 50 | #include "base/bind.h" |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 51 | #include "base/callback.h" |
Lily Chen | 8a648cb7 | 2019-04-25 20:29:15 | [diff] [blame] | 52 | #include "base/feature_list.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 53 | #include "base/location.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 54 | #include "base/logging.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 55 | #include "base/macros.h" |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 56 | #include "base/metrics/field_trial.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 57 | #include "base/metrics/histogram.h" |
Ken Rockot | c204861 | 2018-03-29 01:43:32 | [diff] [blame] | 58 | #include "base/metrics/histogram_macros.h" |
anujk.sharma | afc4517 | 2015-05-15 00:50:34 | [diff] [blame] | 59 | #include "base/single_thread_task_runner.h" |
tripta.g | dda7202 | 2017-06-19 05:16:23 | [diff] [blame] | 60 | #include "base/stl_util.h" |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 61 | #include "base/strings/string_piece.h" |
[email protected] | 4b35521 | 2013-06-11 10:35:19 | [diff] [blame] | 62 | #include "base/strings/string_util.h" |
| 63 | #include "base/strings/stringprintf.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 64 | #include "base/threading/thread_task_runner_handle.h" |
Maks Orlovich | 5cf437b0 | 2018-03-27 04:40:42 | [diff] [blame] | 65 | #include "base/trace_event/process_memory_dump.h" |
Lily Chen | 8a648cb7 | 2019-04-25 20:29:15 | [diff] [blame] | 66 | #include "net/base/features.h" |
[email protected] | be28b5f4 | 2012-07-20 11:31:25 | [diff] [blame] | 67 | #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
[email protected] | 4b35521 | 2013-06-11 10:35:19 | [diff] [blame] | 68 | #include "net/cookies/canonical_cookie.h" |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 69 | #include "net/cookies/cookie_monster_change_dispatcher.h" |
Helen Li | cd0fab86 | 2018-08-13 16:07:53 | [diff] [blame] | 70 | #include "net/cookies/cookie_monster_netlog_params.h" |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 71 | #include "net/cookies/cookie_util.h" |
[email protected] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 72 | #include "net/cookies/parsed_cookie.h" |
Helen Li | cd0fab86 | 2018-08-13 16:07:53 | [diff] [blame] | 73 | #include "net/log/net_log.h" |
mkwst | 8241a12 | 2015-10-20 07:15:10 | [diff] [blame] | 74 | #include "url/origin.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 75 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 76 | using base::Time; |
| 77 | using base::TimeDelta; |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 78 | using base::TimeTicks; |
Chris Mumford | d8ed9f8 | 2018-05-01 15:43:13 | [diff] [blame] | 79 | using TimeRange = net::CookieDeletionInfo::TimeRange; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 80 | |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 81 | // In steady state, most cookie requests can be satisfied by the in memory |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 82 | // cookie monster store. If the cookie request cannot be satisfied by the in |
| 83 | // memory store, the relevant cookies must be fetched from the persistent |
| 84 | // store. The task is queued in CookieMonster::tasks_pending_ if it requires |
| 85 | // all cookies to be loaded from the backend, or tasks_pending_for_key_ if it |
| 86 | // only requires all cookies associated with an eTLD+1. |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 87 | // |
| 88 | // On the browser critical paths (e.g. for loading initial web pages in a |
| 89 | // session restore) it may take too long to wait for the full load. If a cookie |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 90 | // request is for a specific URL, DoCookieCallbackForURL is called, which |
| 91 | // triggers a priority load if the key is not loaded yet by calling |
| 92 | // PersistentCookieStore::LoadCookiesForKey. The request is queued in |
[email protected] | 0184df3 | 2013-05-14 00:53:55 | [diff] [blame] | 93 | // CookieMonster::tasks_pending_for_key_ and executed upon receiving |
| 94 | // notification of key load completion via CookieMonster::OnKeyLoaded(). If |
| 95 | // multiple requests for the same eTLD+1 are received before key load |
| 96 | // completion, only the first request calls |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 97 | // PersistentCookieStore::LoadCookiesForKey, all subsequent requests are queued |
[email protected] | 0184df3 | 2013-05-14 00:53:55 | [diff] [blame] | 98 | // in CookieMonster::tasks_pending_for_key_ and executed upon receiving |
| 99 | // notification of key load completion triggered by the first request for the |
| 100 | // same eTLD+1. |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 101 | |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 102 | static const int kMinutesInTenYears = 10 * 365 * 24 * 60; |
| 103 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 104 | namespace { |
| 105 | |
Maks Orlovich | f97d1b9 | 2018-03-06 17:25:39 | [diff] [blame] | 106 | void MaybeRunDeleteCallback(base::WeakPtr<net::CookieMonster> cookie_monster, |
| 107 | base::OnceClosure callback) { |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 108 | if (cookie_monster && callback) |
| 109 | std::move(callback).Run(); |
| 110 | } |
| 111 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 112 | template <typename T> |
| 113 | void MaybeRunCookieCallback(base::OnceCallback<void(const T&)> callback, |
| 114 | const T& result) { |
| 115 | if (callback) |
| 116 | std::move(callback).Run(result); |
| 117 | } |
| 118 | |
Aaron Tagliaboschi | a4c64b5 | 2019-01-25 03:28:49 | [diff] [blame] | 119 | template <typename T, typename U> |
| 120 | void MaybeRunCookieCallback( |
| 121 | base::OnceCallback<void(const T&, const U&)> callback, |
| 122 | const T& first, |
| 123 | const U& second) { |
| 124 | if (callback) |
| 125 | std::move(callback).Run(first, second); |
| 126 | } |
| 127 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 128 | template <typename T> |
| 129 | void MaybeRunCookieCallback(base::OnceCallback<void(T)> callback, |
| 130 | const T& result) { |
| 131 | if (callback) |
| 132 | std::move(callback).Run(result); |
| 133 | } |
| 134 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 135 | } // namespace |
| 136 | |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 137 | namespace net { |
| 138 | |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 139 | // See comments at declaration of these variables in cookie_monster.h |
| 140 | // for details. |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 141 | const size_t CookieMonster::kDomainMaxCookies = 180; |
| 142 | const size_t CookieMonster::kDomainPurgeCookies = 30; |
| 143 | const size_t CookieMonster::kMaxCookies = 3300; |
| 144 | const size_t CookieMonster::kPurgeCookies = 300; |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 145 | |
mkwst | 8773435 | 2016-03-03 17:36:23 | [diff] [blame] | 146 | const size_t CookieMonster::kDomainCookiesQuotaLow = 30; |
| 147 | const size_t CookieMonster::kDomainCookiesQuotaMedium = 50; |
| 148 | const size_t CookieMonster::kDomainCookiesQuotaHigh = |
| 149 | kDomainMaxCookies - kDomainPurgeCookies - kDomainCookiesQuotaLow - |
| 150 | kDomainCookiesQuotaMedium; |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 151 | |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 152 | const int CookieMonster::kSafeFromGlobalPurgeDays = 30; |
[email protected] | 297a4ed0 | 2010-02-12 08:12:52 | [diff] [blame] | 153 | |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 154 | namespace { |
[email protected] | e32306c5 | 2008-11-06 16:59:05 | [diff] [blame] | 155 | |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 156 | bool ContainsControlCharacter(const std::string& s) { |
| 157 | for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) { |
| 158 | if ((*i >= 0) && (*i <= 31)) |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | return false; |
| 163 | } |
| 164 | |
[email protected] | 5b9bc35 | 2012-07-18 13:13:34 | [diff] [blame] | 165 | typedef std::vector<CanonicalCookie*> CanonicalCookieVector; |
[email protected] | 34a160d | 2011-05-12 22:12:49 | [diff] [blame] | 166 | |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 167 | // Default minimum delay after updating a cookie's LastAccessDate before we |
| 168 | // will update it again. |
[email protected] | 297a4ed0 | 2010-02-12 08:12:52 | [diff] [blame] | 169 | const int kDefaultAccessUpdateThresholdSeconds = 60; |
| 170 | |
| 171 | // Comparator to sort cookies from highest creation date to lowest |
| 172 | // creation date. |
| 173 | struct OrderByCreationTimeDesc { |
| 174 | bool operator()(const CookieMonster::CookieMap::iterator& a, |
| 175 | const CookieMonster::CookieMap::iterator& b) const { |
| 176 | return a->second->CreationDate() > b->second->CreationDate(); |
| 177 | } |
| 178 | }; |
| 179 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 180 | // Mozilla sorts on the path length (longest first), and then it |
| 181 | // sorts by creation time (oldest first). |
| 182 | // The RFC says the sort order for the domain attribute is undefined. |
[email protected] | 5b9bc35 | 2012-07-18 13:13:34 | [diff] [blame] | 183 | bool CookieSorter(CanonicalCookie* cc1, CanonicalCookie* cc2) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 184 | if (cc1->Path().length() == cc2->Path().length()) |
| 185 | return cc1->CreationDate() < cc2->CreationDate(); |
| 186 | return cc1->Path().length() > cc2->Path().length(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 187 | } |
| 188 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 189 | bool LRACookieSorter(const CookieMonster::CookieMap::iterator& it1, |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 190 | const CookieMonster::CookieMap::iterator& it2) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 191 | if (it1->second->LastAccessDate() != it2->second->LastAccessDate()) |
| 192 | return it1->second->LastAccessDate() < it2->second->LastAccessDate(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 193 | |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 194 | // Ensure stability for == last access times by falling back to creation. |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 195 | return it1->second->CreationDate() < it2->second->CreationDate(); |
[email protected] | 297a4ed0 | 2010-02-12 08:12:52 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // Our strategy to find duplicates is: |
| 199 | // (1) Build a map from (cookiename, cookiepath) to |
| 200 | // {list of cookies with this signature, sorted by creation time}. |
| 201 | // (2) For each list with more than 1 entry, keep the cookie having the |
| 202 | // most recent creation time, and delete the others. |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 203 | // |
[email protected] | 1655ba34 | 2010-07-14 18:17:42 | [diff] [blame] | 204 | // Two cookies are considered equivalent if they have the same domain, |
| 205 | // name, and path. |
| 206 | struct CookieSignature { |
| 207 | public: |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 208 | CookieSignature(const std::string& name, |
| 209 | const std::string& domain, |
[email protected] | 1655ba34 | 2010-07-14 18:17:42 | [diff] [blame] | 210 | const std::string& path) |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 211 | : name(name), domain(domain), path(path) {} |
[email protected] | 1655ba34 | 2010-07-14 18:17:42 | [diff] [blame] | 212 | |
| 213 | // To be a key for a map this class needs to be assignable, copyable, |
| 214 | // and have an operator<. The default assignment operator |
| 215 | // and copy constructor are exactly what we want. |
| 216 | |
| 217 | bool operator<(const CookieSignature& cs) const { |
| 218 | // Name compare dominates, then domain, then path. |
| 219 | int diff = name.compare(cs.name); |
| 220 | if (diff != 0) |
| 221 | return diff < 0; |
| 222 | |
| 223 | diff = domain.compare(cs.domain); |
| 224 | if (diff != 0) |
| 225 | return diff < 0; |
| 226 | |
| 227 | return path.compare(cs.path) < 0; |
| 228 | } |
| 229 | |
| 230 | std::string name; |
| 231 | std::string domain; |
| 232 | std::string path; |
| 233 | }; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 234 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 235 | // For a CookieItVector iterator range [|it_begin|, |it_end|), |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 236 | // sorts the first |num_sort| elements by LastAccessDate(). |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 237 | void SortLeastRecentlyAccessed(CookieMonster::CookieItVector::iterator it_begin, |
| 238 | CookieMonster::CookieItVector::iterator it_end, |
| 239 | size_t num_sort) { |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 240 | DCHECK_LE(static_cast<int>(num_sort), it_end - it_begin); |
| 241 | std::partial_sort(it_begin, it_begin + num_sort, it_end, LRACookieSorter); |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 242 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 243 | |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 244 | // Given a single cookie vector |cookie_its|, pushs all of the secure cookies in |
| 245 | // |cookie_its| into |secure_cookie_its| and all of the non-secure cookies into |
| 246 | // |non_secure_cookie_its|. Both |secure_cookie_its| and |non_secure_cookie_its| |
| 247 | // must be non-NULL. |
| 248 | void SplitCookieVectorIntoSecureAndNonSecure( |
| 249 | const CookieMonster::CookieItVector& cookie_its, |
| 250 | CookieMonster::CookieItVector* secure_cookie_its, |
| 251 | CookieMonster::CookieItVector* non_secure_cookie_its) { |
| 252 | DCHECK(secure_cookie_its && non_secure_cookie_its); |
| 253 | for (const auto& curit : cookie_its) { |
| 254 | if (curit->second->IsSecure()) |
| 255 | secure_cookie_its->push_back(curit); |
| 256 | else |
| 257 | non_secure_cookie_its->push_back(curit); |
| 258 | } |
| 259 | } |
| 260 | |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 261 | bool LowerBoundAccessDateComparator(const CookieMonster::CookieMap::iterator it, |
| 262 | const Time& access_date) { |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 263 | return it->second->LastAccessDate() < access_date; |
| 264 | } |
| 265 | |
| 266 | // For a CookieItVector iterator range [|it_begin|, |it_end|) |
| 267 | // from a CookieItVector sorted by LastAccessDate(), returns the |
| 268 | // first iterator with access date >= |access_date|, or cookie_its_end if this |
| 269 | // holds for all. |
| 270 | CookieMonster::CookieItVector::iterator LowerBoundAccessDate( |
| 271 | const CookieMonster::CookieItVector::iterator its_begin, |
| 272 | const CookieMonster::CookieItVector::iterator its_end, |
| 273 | const Time& access_date) { |
| 274 | return std::lower_bound(its_begin, its_end, access_date, |
| 275 | LowerBoundAccessDateComparator); |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 276 | } |
| 277 | |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 278 | // Mapping between DeletionCause and CookieChangeCause; the |
[email protected] | 7c4b66b | 2014-01-04 12:28:13 | [diff] [blame] | 279 | // mapping also provides a boolean that specifies whether or not an |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 280 | // OnCookieChange notification ought to be generated. |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 281 | typedef struct ChangeCausePair_struct { |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 282 | CookieChangeCause cause; |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 283 | bool notify; |
| 284 | } ChangeCausePair; |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 285 | const ChangeCausePair kChangeCauseMapping[] = { |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 286 | // DELETE_COOKIE_EXPLICIT |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 287 | {CookieChangeCause::EXPLICIT, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 288 | // DELETE_COOKIE_OVERWRITE |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 289 | {CookieChangeCause::OVERWRITE, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 290 | // DELETE_COOKIE_EXPIRED |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 291 | {CookieChangeCause::EXPIRED, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 292 | // DELETE_COOKIE_EVICTED |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 293 | {CookieChangeCause::EVICTED, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 294 | // DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 295 | {CookieChangeCause::EXPLICIT, false}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 296 | // DELETE_COOKIE_DONT_RECORD |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 297 | {CookieChangeCause::EXPLICIT, false}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 298 | // DELETE_COOKIE_EVICTED_DOMAIN |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 299 | {CookieChangeCause::EVICTED, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 300 | // DELETE_COOKIE_EVICTED_GLOBAL |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 301 | {CookieChangeCause::EVICTED, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 302 | // DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 303 | {CookieChangeCause::EVICTED, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 304 | // DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 305 | {CookieChangeCause::EVICTED, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 306 | // DELETE_COOKIE_EXPIRED_OVERWRITE |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 307 | {CookieChangeCause::EXPIRED_OVERWRITE, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 308 | // DELETE_COOKIE_CONTROL_CHAR |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 309 | {CookieChangeCause::EVICTED, true}, |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 310 | // DELETE_COOKIE_NON_SECURE |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 311 | {CookieChangeCause::EVICTED, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 312 | // DELETE_COOKIE_LAST_ENTRY |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 313 | {CookieChangeCause::EXPLICIT, false}}; |
ellyjones | 399e35a2 | 2014-10-27 11:09:56 | [diff] [blame] | 314 | |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 315 | bool IsCookieEligibleForEviction(CookiePriority current_priority_level, |
| 316 | bool protect_secure_cookies, |
| 317 | const CanonicalCookie* cookie) { |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 318 | if (cookie->Priority() == current_priority_level && protect_secure_cookies) |
| 319 | return !cookie->IsSecure(); |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 320 | |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 321 | return cookie->Priority() == current_priority_level; |
| 322 | } |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 323 | |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 324 | size_t CountCookiesForPossibleDeletion( |
| 325 | CookiePriority priority, |
| 326 | const CookieMonster::CookieItVector* cookies, |
| 327 | bool protect_secure_cookies) { |
| 328 | size_t cookies_count = 0U; |
| 329 | for (const auto& cookie : *cookies) { |
| 330 | if (cookie->second->Priority() == priority) { |
| 331 | if (!protect_secure_cookies || cookie->second->IsSecure()) |
| 332 | cookies_count++; |
| 333 | } |
| 334 | } |
| 335 | return cookies_count; |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 336 | } |
| 337 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 338 | } // namespace |
| 339 | |
Pritam | 8354cf70 | 2018-03-10 08:55:41 | [diff] [blame] | 340 | CookieMonster::CookieMonster(scoped_refptr<PersistentCookieStore> store, |
Helen Li | fb313a9 | 2018-08-14 15:46:44 | [diff] [blame] | 341 | NetLog* net_log) |
Randy Smith | b78e092 | 2018-03-01 21:19:54 | [diff] [blame] | 342 | : CookieMonster( |
Pritam | 8354cf70 | 2018-03-10 08:55:41 | [diff] [blame] | 343 | std::move(store), |
Helen Li | fb313a9 | 2018-08-14 15:46:44 | [diff] [blame] | 344 | base::TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds), |
| 345 | net_log) {} |
Randy Smith | b78e092 | 2018-03-01 21:19:54 | [diff] [blame] | 346 | |
Pritam | 8354cf70 | 2018-03-10 08:55:41 | [diff] [blame] | 347 | CookieMonster::CookieMonster(scoped_refptr<PersistentCookieStore> store, |
Helen Li | fb313a9 | 2018-08-14 15:46:44 | [diff] [blame] | 348 | base::TimeDelta last_access_threshold, |
| 349 | NetLog* net_log) |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 350 | : initialized_(false), |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 351 | started_fetching_all_cookies_(false), |
| 352 | finished_fetching_all_cookies_(false), |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 353 | seen_global_task_(false), |
Helen Li | fb313a9 | 2018-08-14 15:46:44 | [diff] [blame] | 354 | net_log_(NetLogWithSource::Make(net_log, NetLogSourceType::COOKIE_STORE)), |
Pritam | 8354cf70 | 2018-03-10 08:55:41 | [diff] [blame] | 355 | store_(std::move(store)), |
shess | f0bc118 | 2016-05-19 04:35:58 | [diff] [blame] | 356 | last_access_threshold_(last_access_threshold), |
[email protected] | 8238866 | 2011-03-10 21:04:06 | [diff] [blame] | 357 | last_statistic_record_time_(base::Time::Now()), |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 358 | persist_session_cookies_(false), |
| 359 | weak_ptr_factory_(this) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 360 | InitializeHistograms(); |
mmenke | 18dd8ba | 2016-02-01 18:42:10 | [diff] [blame] | 361 | cookieable_schemes_.insert( |
| 362 | cookieable_schemes_.begin(), kDefaultCookieableSchemes, |
| 363 | kDefaultCookieableSchemes + kDefaultCookieableSchemesCount); |
Helen Li | cd0fab86 | 2018-08-13 16:07:53 | [diff] [blame] | 364 | net_log_.BeginEvent( |
| 365 | NetLogEventType::COOKIE_STORE_ALIVE, |
| 366 | base::BindRepeating(&NetLogCookieMonsterConstructorCallback, |
Nick Harper | 57142b1c | 2019-03-14 21:03:59 | [diff] [blame] | 367 | store != nullptr)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 368 | } |
| 369 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 370 | // Asynchronous CookieMonster API |
| 371 | |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 372 | void CookieMonster::FlushStore(base::OnceClosure callback) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 373 | DCHECK(thread_checker_.CalledOnValidThread()); |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 374 | |
nharper | 2b0ad9a | 2017-05-22 18:33:45 | [diff] [blame] | 375 | if (initialized_ && store_.get()) { |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 376 | store_->Flush(std::move(callback)); |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 377 | } else if (callback) { |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 378 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 379 | std::move(callback)); |
nharper | 2b0ad9a | 2017-05-22 18:33:45 | [diff] [blame] | 380 | } |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 381 | } |
| 382 | |
mmenke | ded79da | 2016-02-06 08:28:51 | [diff] [blame] | 383 | void CookieMonster::SetForceKeepSessionState() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 384 | DCHECK(thread_checker_.CalledOnValidThread()); |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 385 | |
mmenke | ded79da | 2016-02-06 08:28:51 | [diff] [blame] | 386 | if (store_) |
| 387 | store_->SetForceKeepSessionState(); |
| 388 | } |
| 389 | |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 390 | void CookieMonster::SetAllCookiesAsync(const CookieList& list, |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 391 | SetCookiesCallback callback) { |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 392 | DoCookieCallback(base::BindOnce( |
| 393 | // base::Unretained is safe as DoCookieCallbackForURL stores |
| 394 | // the callback on |*this|, so the callback will not outlive |
| 395 | // the object. |
| 396 | &CookieMonster::SetAllCookies, base::Unretained(this), list, |
| 397 | std::move(callback))); |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 398 | } |
| 399 | |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 400 | void CookieMonster::SetCanonicalCookieAsync( |
| 401 | std::unique_ptr<CanonicalCookie> cookie, |
Maks Orlovich | 44525ce | 2019-02-25 14:17:58 | [diff] [blame] | 402 | std::string source_scheme, |
Maks Orlovich | fdbc8be | 2019-03-18 18:34:52 | [diff] [blame] | 403 | const CookieOptions& options, |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 404 | SetCookiesCallback callback) { |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 405 | DCHECK(cookie->IsCanonical()); |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 406 | |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 407 | std::string domain = cookie->Domain(); |
| 408 | DoCookieCallbackForHostOrDomain( |
| 409 | base::BindOnce( |
| 410 | // base::Unretained is safe as DoCookieCallbackForURL stores |
| 411 | // the callback on |*this|, so the callback will not outlive |
| 412 | // the object. |
| 413 | &CookieMonster::SetCanonicalCookie, base::Unretained(this), |
Maks Orlovich | fdbc8be | 2019-03-18 18:34:52 | [diff] [blame] | 414 | std::move(cookie), std::move(source_scheme), options, |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 415 | std::move(callback)), |
| 416 | domain); |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 417 | } |
| 418 | |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 419 | void CookieMonster::SetCookieWithOptionsAsync(const GURL& url, |
| 420 | const std::string& cookie_line, |
| 421 | const CookieOptions& options, |
| 422 | SetCookiesCallback callback) { |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 423 | DoCookieCallbackForURL( |
| 424 | base::BindOnce( |
| 425 | // base::Unretained is safe as DoCookieCallbackForURL stores |
| 426 | // the callback on |*this|, so the callback will not outlive |
| 427 | // the object. |
| 428 | &CookieMonster::SetCookieWithOptions, base::Unretained(this), url, |
| 429 | cookie_line, options, std::move(callback)), |
| 430 | url); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 431 | } |
| 432 | |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 433 | void CookieMonster::GetCookieListWithOptionsAsync( |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 434 | const GURL& url, |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 435 | const CookieOptions& options, |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 436 | GetCookieListCallback callback) { |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 437 | DoCookieCallbackForURL( |
Ken Rockot | ec216e3dd | 2019-02-20 00:43:59 | [diff] [blame] | 438 | base::BindOnce( |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 439 | // base::Unretained is safe as DoCookieCallbackForURL stores |
| 440 | // the callback on |*this|, so the callback will not outlive |
| 441 | // the object. |
| 442 | &CookieMonster::GetCookieListWithOptions, base::Unretained(this), url, |
Ken Rockot | ec216e3dd | 2019-02-20 00:43:59 | [diff] [blame] | 443 | options, std::move(callback)), |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 444 | url); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 445 | } |
| 446 | |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 447 | void CookieMonster::GetAllCookiesAsync(GetCookieListCallback callback) { |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 448 | DoCookieCallback(base::BindOnce( |
| 449 | // base::Unretained is safe as DoCookieCallbackForURL stores |
| 450 | // the callback on |*this|, so the callback will not outlive |
| 451 | // the object. |
| 452 | &CookieMonster::GetAllCookies, base::Unretained(this), |
| 453 | std::move(callback))); |
mmenke | 9fa44f2d | 2016-01-22 23:36:39 | [diff] [blame] | 454 | } |
| 455 | |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 456 | void CookieMonster::DeleteCanonicalCookieAsync(const CanonicalCookie& cookie, |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 457 | DeleteCallback callback) { |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 458 | DoCookieCallback(base::BindOnce( |
| 459 | // base::Unretained is safe as DoCookieCallbackForURL stores |
| 460 | // the callback on |*this|, so the callback will not outlive |
| 461 | // the object. |
| 462 | &CookieMonster::DeleteCanonicalCookie, base::Unretained(this), cookie, |
| 463 | std::move(callback))); |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 464 | } |
| 465 | |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 466 | void CookieMonster::DeleteAllCreatedInTimeRangeAsync( |
| 467 | const TimeRange& creation_range, |
| 468 | DeleteCallback callback) { |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 469 | DoCookieCallback(base::BindOnce( |
| 470 | // base::Unretained is safe as DoCookieCallbackForURL stores |
| 471 | // the callback on |*this|, so the callback will not outlive |
| 472 | // the object. |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 473 | &CookieMonster::DeleteAllCreatedInTimeRange, base::Unretained(this), |
| 474 | creation_range, std::move(callback))); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 475 | } |
| 476 | |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 477 | void CookieMonster::DeleteAllMatchingInfoAsync(CookieDeletionInfo delete_info, |
| 478 | DeleteCallback callback) { |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 479 | DoCookieCallback(base::BindOnce( |
| 480 | // base::Unretained is safe as DoCookieCallbackForURL stores |
| 481 | // the callback on |*this|, so the callback will not outlive |
| 482 | // the object. |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 483 | &CookieMonster::DeleteAllMatchingInfo, base::Unretained(this), |
| 484 | std::move(delete_info), std::move(callback))); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 485 | } |
| 486 | |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 487 | void CookieMonster::DeleteSessionCookiesAsync( |
rdsmith | 7ac8171 | 2017-06-22 17:09:54 | [diff] [blame] | 488 | CookieStore::DeleteCallback callback) { |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 489 | DoCookieCallback(base::BindOnce( |
| 490 | // base::Unretained is safe as DoCookieCallbackForURL stores |
| 491 | // the callback on |*this|, so the callback will not outlive |
| 492 | // the object. |
| 493 | &CookieMonster::DeleteSessionCookies, base::Unretained(this), |
| 494 | std::move(callback))); |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 495 | } |
| 496 | |
mmenke | 18dd8ba | 2016-02-01 18:42:10 | [diff] [blame] | 497 | void CookieMonster::SetCookieableSchemes( |
Nate Fischer | c6fb6cf | 2019-03-27 00:39:49 | [diff] [blame] | 498 | const std::vector<std::string>& schemes, |
| 499 | SetCookieableSchemesCallback callback) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 500 | DCHECK(thread_checker_.CalledOnValidThread()); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 501 | |
| 502 | // Calls to this method will have no effect if made after a WebView or |
| 503 | // CookieManager instance has been created. |
Nate Fischer | c6fb6cf | 2019-03-27 00:39:49 | [diff] [blame] | 504 | if (initialized_) { |
| 505 | MaybeRunCookieCallback(std::move(callback), false); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 506 | return; |
Nate Fischer | c6fb6cf | 2019-03-27 00:39:49 | [diff] [blame] | 507 | } |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 508 | |
mmenke | 18dd8ba | 2016-02-01 18:42:10 | [diff] [blame] | 509 | cookieable_schemes_ = schemes; |
Nate Fischer | c6fb6cf | 2019-03-27 00:39:49 | [diff] [blame] | 510 | MaybeRunCookieCallback(std::move(callback), true); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 511 | } |
| 512 | |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 513 | // This function must be called before the CookieMonster is used. |
| 514 | void CookieMonster::SetPersistSessionCookies(bool persist_session_cookies) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 515 | DCHECK(thread_checker_.CalledOnValidThread()); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 516 | DCHECK(!initialized_); |
Helen Li | cd0fab86 | 2018-08-13 16:07:53 | [diff] [blame] | 517 | net_log_.AddEvent( |
| 518 | NetLogEventType::COOKIE_STORE_SESSION_PERSISTENCE, |
| 519 | NetLog::BoolCallback("persistence", persist_session_cookies)); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 520 | persist_session_cookies_ = persist_session_cookies; |
| 521 | } |
| 522 | |
| 523 | bool CookieMonster::IsCookieableScheme(const std::string& scheme) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 524 | DCHECK(thread_checker_.CalledOnValidThread()); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 525 | |
tripta.g | dda7202 | 2017-06-19 05:16:23 | [diff] [blame] | 526 | return base::ContainsValue(cookieable_schemes_, scheme); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 527 | } |
| 528 | |
mmenke | 18dd8ba | 2016-02-01 18:42:10 | [diff] [blame] | 529 | const char* const CookieMonster::kDefaultCookieableSchemes[] = {"http", "https", |
| 530 | "ws", "wss"}; |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 531 | const int CookieMonster::kDefaultCookieableSchemesCount = |
Ryan Sleevi | 435a3a2 | 2018-05-15 02:16:07 | [diff] [blame] | 532 | base::size(kDefaultCookieableSchemes); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 533 | |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 534 | CookieChangeDispatcher& CookieMonster::GetChangeDispatcher() { |
| 535 | return change_dispatcher_; |
Randy Smith | d32dc8c | 2017-08-30 18:03:40 | [diff] [blame] | 536 | } |
| 537 | |
Maks Orlovich | 5cf437b0 | 2018-03-27 04:40:42 | [diff] [blame] | 538 | void CookieMonster::DumpMemoryStats( |
| 539 | base::trace_event::ProcessMemoryDump* pmd, |
| 540 | const std::string& parent_absolute_name) const { |
| 541 | const char kRelPath[] = "/cookie_monster"; |
| 542 | |
| 543 | pmd->CreateAllocatorDump(parent_absolute_name + kRelPath + "/cookies") |
| 544 | ->AddScalar(base::trace_event::MemoryAllocatorDump::kNameObjectCount, |
| 545 | base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 546 | cookies_.size()); |
| 547 | |
| 548 | pmd->CreateAllocatorDump(parent_absolute_name + kRelPath + |
| 549 | "/tasks_pending_global") |
| 550 | ->AddScalar(base::trace_event::MemoryAllocatorDump::kNameObjectCount, |
| 551 | base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 552 | tasks_pending_.size()); |
| 553 | |
| 554 | size_t total_pending_for_key = 0; |
| 555 | for (const auto& kv : tasks_pending_for_key_) |
| 556 | total_pending_for_key += kv.second.size(); |
| 557 | pmd->CreateAllocatorDump(parent_absolute_name + kRelPath + |
| 558 | "/tasks_pending_for_key") |
| 559 | ->AddScalar(base::trace_event::MemoryAllocatorDump::kNameObjectCount, |
| 560 | base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 561 | total_pending_for_key); |
| 562 | } |
| 563 | |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 564 | CookieMonster::~CookieMonster() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 565 | DCHECK(thread_checker_.CalledOnValidThread()); |
mmenke | 05255cf | 2016-02-03 15:49:31 | [diff] [blame] | 566 | |
Randy Smith | 0a52266 | 2017-08-30 19:35:34 | [diff] [blame] | 567 | // TODO(mmenke): Does it really make sense to run |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 568 | // CookieChanged callbacks when the CookieStore is destroyed? |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 569 | for (auto cookie_it = cookies_.begin(); cookie_it != cookies_.end();) { |
| 570 | auto current_cookie_it = cookie_it; |
mmenke | 05255cf | 2016-02-03 15:49:31 | [diff] [blame] | 571 | ++cookie_it; |
| 572 | InternalDeleteCookie(current_cookie_it, false /* sync_to_store */, |
| 573 | DELETE_COOKIE_DONT_RECORD); |
| 574 | } |
Helen Li | cd0fab86 | 2018-08-13 16:07:53 | [diff] [blame] | 575 | net_log_.EndEvent(NetLogEventType::COOKIE_STORE_ALIVE); |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 576 | } |
| 577 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 578 | void CookieMonster::GetAllCookies(GetCookieListCallback callback) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 579 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 580 | |
| 581 | // This function is being called to scrape the cookie list for management UI |
| 582 | // or similar. We shouldn't show expired cookies in this list since it will |
| 583 | // just be confusing to users, and this function is called rarely enough (and |
| 584 | // is already slow enough) that it's OK to take the time to garbage collect |
| 585 | // the expired cookies now. |
| 586 | // |
| 587 | // Note that this does not prune cookies to be below our limits (if we've |
| 588 | // exceeded them) the way that calling GarbageCollect() would. |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 589 | GarbageCollectExpired( |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 590 | Time::Now(), CookieMapItPair(cookies_.begin(), cookies_.end()), nullptr); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 591 | |
| 592 | // Copy the CanonicalCookie pointers from the map so that we can use the same |
| 593 | // sorter as elsewhere, then copy the result out. |
| 594 | std::vector<CanonicalCookie*> cookie_ptrs; |
| 595 | cookie_ptrs.reserve(cookies_.size()); |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 596 | for (const auto& cookie : cookies_) |
| 597 | cookie_ptrs.push_back(cookie.second.get()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 598 | std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); |
| 599 | |
| 600 | CookieList cookie_list; |
| 601 | cookie_list.reserve(cookie_ptrs.size()); |
vmpstr | 6d9996c8 | 2017-02-23 00:43:25 | [diff] [blame] | 602 | for (auto* cookie_ptr : cookie_ptrs) |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 603 | cookie_list.push_back(*cookie_ptr); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 604 | |
Aaron Tagliaboschi | a4c64b5 | 2019-01-25 03:28:49 | [diff] [blame] | 605 | MaybeRunCookieCallback(std::move(callback), cookie_list, CookieStatusList()); |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame] | 606 | } |
| 607 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 608 | void CookieMonster::GetCookieListWithOptions(const GURL& url, |
| 609 | const CookieOptions& options, |
| 610 | GetCookieListCallback callback) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 611 | DCHECK(thread_checker_.CalledOnValidThread()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 612 | |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 613 | CookieList cookies; |
Aaron Tagliaboschi | a4c64b5 | 2019-01-25 03:28:49 | [diff] [blame] | 614 | CookieStatusList excluded_cookies; |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 615 | if (HasCookieableScheme(url)) { |
| 616 | std::vector<CanonicalCookie*> cookie_ptrs; |
Aaron Tagliaboschi | bd34cc0c | 2019-01-31 01:19:52 | [diff] [blame] | 617 | FindCookiesForRegistryControlledHost(url, &cookie_ptrs); |
Aaron Tagliaboschi | 29033b7f | 2019-01-31 19:58:20 | [diff] [blame] | 618 | std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 619 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 620 | cookies.reserve(cookie_ptrs.size()); |
Aaron Tagliaboschi | 29033b7f | 2019-01-31 19:58:20 | [diff] [blame] | 621 | std::vector<CanonicalCookie*> included_cookie_ptrs; |
| 622 | FilterCookiesWithOptions(url, options, &cookie_ptrs, &included_cookie_ptrs, |
| 623 | &excluded_cookies); |
Aaron Tagliaboschi | bd34cc0c | 2019-01-31 01:19:52 | [diff] [blame] | 624 | |
Aaron Tagliaboschi | 29033b7f | 2019-01-31 19:58:20 | [diff] [blame] | 625 | for (auto* cookie : included_cookie_ptrs) { |
Aaron Tagliaboschi | bd34cc0c | 2019-01-31 01:19:52 | [diff] [blame] | 626 | cookies.push_back(*cookie); |
| 627 | } |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 628 | } |
Aaron Tagliaboschi | 29033b7f | 2019-01-31 19:58:20 | [diff] [blame] | 629 | |
Aaron Tagliaboschi | a4c64b5 | 2019-01-25 03:28:49 | [diff] [blame] | 630 | MaybeRunCookieCallback(std::move(callback), cookies, excluded_cookies); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 631 | } |
| 632 | |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 633 | void CookieMonster::DeleteAllCreatedInTimeRange(const TimeRange& creation_range, |
| 634 | DeleteCallback callback) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 635 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | d098033 | 2010-11-16 17:08:53 | [diff] [blame] | 636 | |
rdsmith | a5beda16 | 2017-07-08 13:55:42 | [diff] [blame] | 637 | uint32_t num_deleted = 0; |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 638 | for (auto it = cookies_.begin(); it != cookies_.end();) { |
| 639 | auto curit = it; |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 640 | CanonicalCookie* cc = curit->second.get(); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 641 | ++it; |
[email protected] | d098033 | 2010-11-16 17:08:53 | [diff] [blame] | 642 | |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 643 | if (creation_range.Contains(cc->CreationDate())) { |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 644 | InternalDeleteCookie(curit, true, /*sync_to_store*/ |
Nick Harper | 7a6683a | 2018-01-30 20:42:52 | [diff] [blame] | 645 | DELETE_COOKIE_EXPLICIT); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 646 | ++num_deleted; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 650 | FlushStore( |
Maks Orlovich | f97d1b9 | 2018-03-06 17:25:39 | [diff] [blame] | 651 | base::BindOnce(&MaybeRunDeleteCallback, weak_ptr_factory_.GetWeakPtr(), |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 652 | callback ? base::BindOnce(std::move(callback), num_deleted) |
| 653 | : base::OnceClosure())); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 654 | } |
| 655 | |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 656 | void CookieMonster::DeleteAllMatchingInfo(CookieDeletionInfo delete_info, |
| 657 | DeleteCallback callback) { |
rdsmith | a5beda16 | 2017-07-08 13:55:42 | [diff] [blame] | 658 | uint32_t num_deleted = 0; |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 659 | for (auto it = cookies_.begin(); it != cookies_.end();) { |
| 660 | auto curit = it; |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 661 | CanonicalCookie* cc = curit->second.get(); |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 662 | ++it; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 663 | |
Chris Mumford | 800caa6 | 2018-04-20 19:34:44 | [diff] [blame] | 664 | if (delete_info.Matches(*cc)) { |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 665 | InternalDeleteCookie(curit, true, /*sync_to_store*/ |
Nick Harper | 7a6683a | 2018-01-30 20:42:52 | [diff] [blame] | 666 | DELETE_COOKIE_EXPLICIT); |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 667 | ++num_deleted; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 668 | } |
| 669 | } |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 670 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 671 | FlushStore( |
Maks Orlovich | f97d1b9 | 2018-03-06 17:25:39 | [diff] [blame] | 672 | base::BindOnce(&MaybeRunDeleteCallback, weak_ptr_factory_.GetWeakPtr(), |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 673 | callback ? base::BindOnce(std::move(callback), num_deleted) |
| 674 | : base::OnceClosure())); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 675 | } |
| 676 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 677 | void CookieMonster::SetCookieWithOptions(const GURL& url, |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 678 | const std::string& cookie_line, |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 679 | const CookieOptions& options, |
| 680 | SetCookiesCallback callback) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 681 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 682 | |
| 683 | if (!HasCookieableScheme(url)) { |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 684 | MaybeRunCookieCallback( |
| 685 | std::move(callback), |
| 686 | CanonicalCookie::CookieInclusionStatus::EXCLUDE_NONCOOKIEABLE_SCHEME); |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 687 | return; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 688 | } |
| 689 | |
Maks Orlovich | 35d74c04 | 2019-04-18 16:38:01 | [diff] [blame] | 690 | DVLOG(net::cookie_util::kVlogSetCookies) |
Oscar Johansson | 63e83cf | 2018-07-02 08:47:26 | [diff] [blame] | 691 | << "SetCookie() line: " << cookie_line; |
Maks Orlovich | a61ed02 | 2018-03-01 18:24:24 | [diff] [blame] | 692 | |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 693 | CanonicalCookie::CookieInclusionStatus status; |
Maks Orlovich | a61ed02 | 2018-03-01 18:24:24 | [diff] [blame] | 694 | |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 695 | std::unique_ptr<CanonicalCookie> cc( |
| 696 | CanonicalCookie::Create(url, cookie_line, Time::Now(), options, &status)); |
| 697 | |
| 698 | if (status != CanonicalCookie::CookieInclusionStatus::INCLUDE) { |
| 699 | DCHECK(!cc); |
Maks Orlovich | 35d74c04 | 2019-04-18 16:38:01 | [diff] [blame] | 700 | DVLOG(net::cookie_util::kVlogSetCookies) |
Oscar Johansson | 63e83cf | 2018-07-02 08:47:26 | [diff] [blame] | 701 | << "WARNING: Failed to allocate CanonicalCookie"; |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 702 | MaybeRunCookieCallback(std::move(callback), status); |
Maks Orlovich | a61ed02 | 2018-03-01 18:24:24 | [diff] [blame] | 703 | return; |
| 704 | } |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 705 | |
| 706 | DCHECK(cc); |
Maks Orlovich | fdbc8be | 2019-03-18 18:34:52 | [diff] [blame] | 707 | SetCanonicalCookie(std::move(cc), url.scheme(), options, std::move(callback)); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 708 | } |
| 709 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 710 | void CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie, |
| 711 | DeleteCallback callback) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 712 | DCHECK(thread_checker_.CalledOnValidThread()); |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 713 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 714 | uint32_t result = 0u; |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 715 | for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); |
| 716 | its.first != its.second; ++its.first) { |
Maks Orlovich | 2896ec3f | 2018-04-06 13:34:27 | [diff] [blame] | 717 | const std::unique_ptr<CanonicalCookie>& candidate = its.first->second; |
| 718 | // Historically, this has refused modification if the cookie has changed |
| 719 | // value in between the CanonicalCookie object was returned by a getter |
| 720 | // and when this ran. The later parts of the conditional (everything but |
| 721 | // the equivalence check) attempt to preserve this behavior. |
| 722 | if (candidate->IsEquivalent(cookie) && |
Maks Orlovich | 2896ec3f | 2018-04-06 13:34:27 | [diff] [blame] | 723 | candidate->Value() == cookie.Value()) { |
Nick Harper | 7a6683a | 2018-01-30 20:42:52 | [diff] [blame] | 724 | InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT); |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 725 | result = 1u; |
| 726 | break; |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 727 | } |
| 728 | } |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 729 | FlushStore( |
Maks Orlovich | f97d1b9 | 2018-03-06 17:25:39 | [diff] [blame] | 730 | base::BindOnce(&MaybeRunDeleteCallback, weak_ptr_factory_.GetWeakPtr(), |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 731 | callback ? base::BindOnce(std::move(callback), result) |
| 732 | : base::OnceClosure())); |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 733 | } |
| 734 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 735 | void CookieMonster::DeleteSessionCookies(DeleteCallback callback) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 736 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 737 | |
rdsmith | a5beda16 | 2017-07-08 13:55:42 | [diff] [blame] | 738 | uint32_t num_deleted = 0; |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 739 | for (auto it = cookies_.begin(); it != cookies_.end();) { |
| 740 | auto curit = it; |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 741 | CanonicalCookie* cc = curit->second.get(); |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 742 | ++it; |
| 743 | |
| 744 | if (!cc->IsPersistent()) { |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 745 | InternalDeleteCookie(curit, true, /*sync_to_store*/ |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 746 | DELETE_COOKIE_EXPIRED); |
| 747 | ++num_deleted; |
| 748 | } |
| 749 | } |
| 750 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 751 | FlushStore( |
Maks Orlovich | f97d1b9 | 2018-03-06 17:25:39 | [diff] [blame] | 752 | base::BindOnce(&MaybeRunDeleteCallback, weak_ptr_factory_.GetWeakPtr(), |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 753 | callback ? base::BindOnce(std::move(callback), num_deleted) |
| 754 | : base::OnceClosure())); |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 755 | } |
| 756 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 757 | void CookieMonster::MarkCookieStoreAsInitialized() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 758 | DCHECK(thread_checker_.CalledOnValidThread()); |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 759 | initialized_ = true; |
| 760 | } |
| 761 | |
| 762 | void CookieMonster::FetchAllCookiesIfNecessary() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 763 | DCHECK(thread_checker_.CalledOnValidThread()); |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 764 | if (store_.get() && !started_fetching_all_cookies_) { |
| 765 | started_fetching_all_cookies_ = true; |
| 766 | FetchAllCookies(); |
| 767 | } |
| 768 | } |
| 769 | |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 770 | void CookieMonster::FetchAllCookies() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 771 | DCHECK(thread_checker_.CalledOnValidThread()); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 772 | DCHECK(store_.get()) << "Store must exist to initialize"; |
| 773 | DCHECK(!finished_fetching_all_cookies_) |
| 774 | << "All cookies have already been fetched."; |
| 775 | |
| 776 | // We bind in the current time so that we can report the wall-clock time for |
| 777 | // loading cookies. |
Maks Orlovich | 108cb4c | 2019-03-26 20:24:57 | [diff] [blame] | 778 | store_->Load(base::BindOnce(&CookieMonster::OnLoaded, |
| 779 | weak_ptr_factory_.GetWeakPtr(), TimeTicks::Now()), |
Helen Li | 92a29f10 | 2018-08-15 23:02:26 | [diff] [blame] | 780 | net_log_); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 781 | } |
| 782 | |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 783 | void CookieMonster::OnLoaded( |
| 784 | TimeTicks beginning_time, |
| 785 | std::vector<std::unique_ptr<CanonicalCookie>> cookies) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 786 | DCHECK(thread_checker_.CalledOnValidThread()); |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 787 | StoreLoadedCookies(std::move(cookies)); |
[email protected] | c7593fb2 | 2011-11-14 23:54:27 | [diff] [blame] | 788 | histogram_time_blocked_on_load_->AddTime(TimeTicks::Now() - beginning_time); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 789 | |
| 790 | // Invoke the task queue of cookie request. |
| 791 | InvokeQueue(); |
| 792 | } |
| 793 | |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 794 | void CookieMonster::OnKeyLoaded( |
| 795 | const std::string& key, |
| 796 | std::vector<std::unique_ptr<CanonicalCookie>> cookies) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 797 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 798 | |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 799 | StoreLoadedCookies(std::move(cookies)); |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 800 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 801 | auto tasks_pending_for_key = tasks_pending_for_key_.find(key); |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 802 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 803 | // TODO(mmenke): Can this be turned into a DCHECK? |
| 804 | if (tasks_pending_for_key == tasks_pending_for_key_.end()) |
| 805 | return; |
[email protected] | bab72ec | 2013-10-30 20:50:02 | [diff] [blame] | 806 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 807 | // Run all tasks for the key. Note that running a task can result in multiple |
| 808 | // tasks being added to the back of the deque. |
| 809 | while (!tasks_pending_for_key->second.empty()) { |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 810 | base::OnceClosure task = std::move(tasks_pending_for_key->second.front()); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 811 | tasks_pending_for_key->second.pop_front(); |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 812 | std::move(task).Run(); |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 813 | } |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 814 | |
| 815 | tasks_pending_for_key_.erase(tasks_pending_for_key); |
| 816 | |
| 817 | // This has to be done last, in case running a task queues a new task for the |
| 818 | // key, to ensure tasks are run in the correct order. |
| 819 | keys_loaded_.insert(key); |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 820 | } |
| 821 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 822 | void CookieMonster::StoreLoadedCookies( |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 823 | std::vector<std::unique_ptr<CanonicalCookie>> cookies) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 824 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 825 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 826 | // Even if a key is expired, insert it so it can be garbage collected, |
| 827 | // removed, and sync'd. |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 828 | CookieItVector cookies_with_control_chars; |
| 829 | |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 830 | for (auto& cookie : cookies) { |
Maks Orlovich | 2b0d5b1 | 2018-04-10 19:33:47 | [diff] [blame] | 831 | CanonicalCookie* cookie_ptr = cookie.get(); |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 832 | auto inserted = InternalInsertCookie(GetKey(cookie_ptr->Domain()), |
| 833 | std::move(cookie), false); |
Maks Orlovich | 2b0d5b1 | 2018-04-10 19:33:47 | [diff] [blame] | 834 | const Time cookie_access_time(cookie_ptr->LastAccessDate()); |
| 835 | if (earliest_access_time_.is_null() || |
Helen Li | d84010b | 2018-08-22 16:27:44 | [diff] [blame] | 836 | cookie_access_time < earliest_access_time_) { |
Maks Orlovich | 2b0d5b1 | 2018-04-10 19:33:47 | [diff] [blame] | 837 | earliest_access_time_ = cookie_access_time; |
Helen Li | d84010b | 2018-08-22 16:27:44 | [diff] [blame] | 838 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 839 | |
Maks Orlovich | 2b0d5b1 | 2018-04-10 19:33:47 | [diff] [blame] | 840 | if (ContainsControlCharacter(cookie_ptr->Name()) || |
| 841 | ContainsControlCharacter(cookie_ptr->Value())) { |
| 842 | cookies_with_control_chars.push_back(inserted); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 843 | } |
| 844 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 845 | |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 846 | // Any cookies that contain control characters that we have loaded from the |
| 847 | // persistent store should be deleted. See http://crbug.com/238041. |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 848 | for (auto it = cookies_with_control_chars.begin(); |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 849 | it != cookies_with_control_chars.end();) { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 850 | auto curit = it; |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 851 | ++it; |
| 852 | |
| 853 | InternalDeleteCookie(*curit, true, DELETE_COOKIE_CONTROL_CHAR); |
| 854 | } |
| 855 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 856 | // After importing cookies from the PersistentCookieStore, verify that |
| 857 | // none of our other constraints are violated. |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 858 | // In particular, the backing store might have given us duplicate cookies. |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 859 | |
| 860 | // This method could be called multiple times due to priority loading, thus |
| 861 | // cookies loaded in previous runs will be validated again, but this is OK |
| 862 | // since they are expected to be much fewer than total DB. |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 863 | EnsureCookiesMapIsValid(); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 864 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 865 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 866 | void CookieMonster::InvokeQueue() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 867 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 868 | |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 869 | // Move all per-key tasks into the global queue, if there are any. This is |
| 870 | // protection about a race where the store learns about all cookies loading |
| 871 | // before it learned about the cookies for a key loading. |
| 872 | |
| 873 | // Needed to prevent any recursively queued tasks from going back into the |
| 874 | // per-key queues. |
| 875 | seen_global_task_ = true; |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 876 | for (auto& tasks_for_key : tasks_pending_for_key_) { |
| 877 | tasks_pending_.insert(tasks_pending_.begin(), |
| 878 | std::make_move_iterator(tasks_for_key.second.begin()), |
| 879 | std::make_move_iterator(tasks_for_key.second.end())); |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 880 | } |
| 881 | tasks_pending_for_key_.clear(); |
| 882 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 883 | while (!tasks_pending_.empty()) { |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 884 | base::OnceClosure request_task = std::move(tasks_pending_.front()); |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 885 | tasks_pending_.pop_front(); |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 886 | std::move(request_task).Run(); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 887 | } |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 888 | |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 889 | DCHECK(tasks_pending_for_key_.empty()); |
| 890 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 891 | finished_fetching_all_cookies_ = true; |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 892 | keys_loaded_.clear(); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | void CookieMonster::EnsureCookiesMapIsValid() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 896 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 897 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 898 | // Iterate through all the of the cookies, grouped by host. |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 899 | auto prev_range_end = cookies_.begin(); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 900 | while (prev_range_end != cookies_.end()) { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 901 | auto cur_range_begin = prev_range_end; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 902 | const std::string key = cur_range_begin->first; // Keep a copy. |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 903 | auto cur_range_end = cookies_.upper_bound(key); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 904 | prev_range_end = cur_range_end; |
| 905 | |
| 906 | // Ensure no equivalent cookies for this host. |
ellyjones | cabf5742 | 2015-08-21 18:44:51 | [diff] [blame] | 907 | TrimDuplicateCookiesForKey(key, cur_range_begin, cur_range_end); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 908 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 909 | } |
| 910 | |
ellyjones | cabf5742 | 2015-08-21 18:44:51 | [diff] [blame] | 911 | void CookieMonster::TrimDuplicateCookiesForKey(const std::string& key, |
| 912 | CookieMap::iterator begin, |
| 913 | CookieMap::iterator end) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 914 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 915 | |
| 916 | // Set of cookies ordered by creation time. |
Maks Orlovich | 2b0d5b1 | 2018-04-10 19:33:47 | [diff] [blame] | 917 | typedef std::multiset<CookieMap::iterator, OrderByCreationTimeDesc> CookieSet; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 918 | |
| 919 | // Helper map we populate to find the duplicates. |
| 920 | typedef std::map<CookieSignature, CookieSet> EquivalenceMap; |
| 921 | EquivalenceMap equivalent_cookies; |
| 922 | |
| 923 | // The number of duplicate cookies that have been found. |
| 924 | int num_duplicates = 0; |
| 925 | |
| 926 | // Iterate through all of the cookies in our range, and insert them into |
| 927 | // the equivalence map. |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 928 | for (auto it = begin; it != end; ++it) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 929 | DCHECK_EQ(key, it->first); |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 930 | CanonicalCookie* cookie = it->second.get(); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 931 | |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 932 | CookieSignature signature(cookie->Name(), cookie->Domain(), cookie->Path()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 933 | CookieSet& set = equivalent_cookies[signature]; |
| 934 | |
| 935 | // We found a duplicate! |
| 936 | if (!set.empty()) |
| 937 | num_duplicates++; |
| 938 | |
| 939 | // We save the iterator into |cookies_| rather than the actual cookie |
| 940 | // pointer, since we may need to delete it later. |
Maks Orlovich | 2b0d5b1 | 2018-04-10 19:33:47 | [diff] [blame] | 941 | set.insert(it); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | // If there were no duplicates, we are done! |
| 945 | if (num_duplicates == 0) |
ellyjones | cabf5742 | 2015-08-21 18:44:51 | [diff] [blame] | 946 | return; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 947 | |
| 948 | // Make sure we find everything below that we did above. |
| 949 | int num_duplicates_found = 0; |
| 950 | |
| 951 | // Otherwise, delete all the duplicate cookies, both from our in-memory store |
| 952 | // and from the backing store. |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 953 | for (auto it = equivalent_cookies.begin(); it != equivalent_cookies.end(); |
| 954 | ++it) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 955 | const CookieSignature& signature = it->first; |
| 956 | CookieSet& dupes = it->second; |
| 957 | |
| 958 | if (dupes.size() <= 1) |
| 959 | continue; // This cookiename/path has no duplicates. |
| 960 | num_duplicates_found += dupes.size() - 1; |
| 961 | |
Maks Orlovich | 2b0d5b1 | 2018-04-10 19:33:47 | [diff] [blame] | 962 | // Since |dupes| is sorted by creation time (descending), the first cookie |
| 963 | // is the most recent one (or tied for it), so we will keep it. The rest are |
| 964 | // duplicates. |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 965 | dupes.erase(dupes.begin()); |
| 966 | |
| 967 | LOG(ERROR) << base::StringPrintf( |
| 968 | "Found %d duplicate cookies for host='%s', " |
| 969 | "with {name='%s', domain='%s', path='%s'}", |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 970 | static_cast<int>(dupes.size()), key.c_str(), signature.name.c_str(), |
| 971 | signature.domain.c_str(), signature.path.c_str()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 972 | |
| 973 | // Remove all the cookies identified by |dupes|. It is valid to delete our |
| 974 | // list of iterators one at a time, since |cookies_| is a multimap (they |
| 975 | // don't invalidate existing iterators following deletion). |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 976 | for (auto dupes_it = dupes.begin(); dupes_it != dupes.end(); ++dupes_it) { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 977 | InternalDeleteCookie(*dupes_it, true, |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 978 | DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE); |
| 979 | } |
| 980 | } |
| 981 | DCHECK_EQ(num_duplicates, num_duplicates_found); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 982 | } |
| 983 | |
Aaron Tagliaboschi | bd34cc0c | 2019-01-31 01:19:52 | [diff] [blame] | 984 | void CookieMonster::FindCookiesForRegistryControlledHost( |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 985 | const GURL& url, |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 986 | std::vector<CanonicalCookie*>* cookies) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 987 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 988 | |
Maks Orlovich | c86cf29 | 2019-02-11 19:25:17 | [diff] [blame] | 989 | Time current_time = Time::Now(); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 990 | |
Aaron Tagliaboschi | bd34cc0c | 2019-01-31 01:19:52 | [diff] [blame] | 991 | // Retrieve all cookies for a given key |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 992 | const std::string key(GetKey(url.host_piece())); |
Aaron Tagliaboschi | 9e1de1b | 2019-01-15 14:40:06 | [diff] [blame] | 993 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 994 | for (CookieMapItPair its = cookies_.equal_range(key); |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 995 | its.first != its.second;) { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 996 | auto curit = its.first; |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 997 | CanonicalCookie* cc = curit->second.get(); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 998 | ++its.first; |
| 999 | |
| 1000 | // If the cookie is expired, delete it. |
Aaron Tagliaboschi | bd34cc0c | 2019-01-31 01:19:52 | [diff] [blame] | 1001 | if (cc->IsExpired(current_time)) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1002 | InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED); |
| 1003 | continue; |
| 1004 | } |
Aaron Tagliaboschi | bd34cc0c | 2019-01-31 01:19:52 | [diff] [blame] | 1005 | cookies->push_back(cc); |
Aaron Tagliaboschi | 9e1de1b | 2019-01-15 14:40:06 | [diff] [blame] | 1006 | } |
Aaron Tagliaboschi | bd34cc0c | 2019-01-31 01:19:52 | [diff] [blame] | 1007 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1008 | |
Aaron Tagliaboschi | bd34cc0c | 2019-01-31 01:19:52 | [diff] [blame] | 1009 | void CookieMonster::FilterCookiesWithOptions( |
| 1010 | const GURL url, |
| 1011 | const CookieOptions options, |
| 1012 | std::vector<CanonicalCookie*>* cookie_ptrs, |
Aaron Tagliaboschi | 29033b7f | 2019-01-31 19:58:20 | [diff] [blame] | 1013 | std::vector<CanonicalCookie*>* included_cookie_ptrs, |
| 1014 | CookieStatusList* excluded_cookies) { |
Aaron Tagliaboschi | bd34cc0c | 2019-01-31 01:19:52 | [diff] [blame] | 1015 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 1016 | |
| 1017 | // Probe to save statistics relatively frequently. We do it here rather |
| 1018 | // than in the set path as many websites won't set cookies, and we |
| 1019 | // want to collect statistics whenever the browser's being used. |
Maks Orlovich | c86cf29 | 2019-02-11 19:25:17 | [diff] [blame] | 1020 | Time current_time = Time::Now(); |
Aaron Tagliaboschi | bd34cc0c | 2019-01-31 01:19:52 | [diff] [blame] | 1021 | RecordPeriodicStats(current_time); |
| 1022 | |
| 1023 | for (std::vector<CanonicalCookie*>::iterator it = cookie_ptrs->begin(); |
| 1024 | it != cookie_ptrs->end(); it++) { |
[email protected] | 65f4e7e | 2012-12-12 21:56:54 | [diff] [blame] | 1025 | // Filter out cookies that should not be included for a request to the |
| 1026 | // given |url|. HTTP only cookies are filtered depending on the passed |
| 1027 | // cookie |options|. |
Aaron Tagliaboschi | 29033b7f | 2019-01-31 19:58:20 | [diff] [blame] | 1028 | CanonicalCookie::CookieInclusionStatus status = |
| 1029 | (*it)->IncludeForRequestURL(url, options); |
| 1030 | |
| 1031 | if (status != CanonicalCookie::CookieInclusionStatus::INCLUDE) { |
| 1032 | if (options.return_excluded_cookies()) |
| 1033 | excluded_cookies->push_back({**it, status}); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1034 | continue; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1035 | } |
Aaron Tagliaboschi | bd34cc0c | 2019-01-31 01:19:52 | [diff] [blame] | 1036 | |
| 1037 | if (options.update_access_time()) |
| 1038 | InternalUpdateCookieAccessTime(*it, current_time); |
| 1039 | |
Aaron Tagliaboschi | 29033b7f | 2019-01-31 19:58:20 | [diff] [blame] | 1040 | included_cookie_ptrs->push_back(*it); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 1044 | CanonicalCookie::CookieInclusionStatus CookieMonster::DeleteAnyEquivalentCookie( |
Mike West | c4a777b | 2017-10-06 14:04:20 | [diff] [blame] | 1045 | const std::string& key, |
| 1046 | const CanonicalCookie& ecc, |
| 1047 | bool source_secure, |
| 1048 | bool skip_httponly, |
| 1049 | bool already_expired, |
| 1050 | base::Time* creation_date_to_inherit) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1051 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1052 | |
| 1053 | bool found_equivalent_cookie = false; |
| 1054 | bool skipped_httponly = false; |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 1055 | bool skipped_secure_cookie = false; |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 1056 | |
| 1057 | histogram_cookie_delete_equivalent_->Add(COOKIE_DELETE_EQUIVALENT_ATTEMPT); |
| 1058 | |
Lily Chen | 22707642b | 2018-10-02 17:27:21 | [diff] [blame] | 1059 | CookieMap::iterator cookie_it_to_possibly_delete = cookies_.end(); |
| 1060 | CanonicalCookie* cc_skipped_secure = nullptr; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1061 | for (CookieMapItPair its = cookies_.equal_range(key); |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1062 | its.first != its.second;) { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 1063 | auto curit = its.first; |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1064 | CanonicalCookie* cc = curit->second.get(); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1065 | ++its.first; |
| 1066 | |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 1067 | // If the cookie is being set from an insecure scheme, then if a cookie |
| 1068 | // already exists with the same name and it is Secure, then the cookie |
| 1069 | // should *not* be updated if they domain-match and ignoring the path |
| 1070 | // attribute. |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 1071 | // |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 1072 | // See: https://tools.ietf.org/html/draft-ietf-httpbis-cookie-alone |
rdsmith | 2709eee | 2017-06-20 22:43:27 | [diff] [blame] | 1073 | if (cc->IsSecure() && !source_secure && |
mmenke | 2830b072 | 2016-07-20 16:02:50 | [diff] [blame] | 1074 | ecc.IsEquivalentForSecureCookieMatching(*cc)) { |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 1075 | skipped_secure_cookie = true; |
Lily Chen | 22707642b | 2018-10-02 17:27:21 | [diff] [blame] | 1076 | cc_skipped_secure = cc; |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 1077 | histogram_cookie_delete_equivalent_->Add( |
| 1078 | COOKIE_DELETE_EQUIVALENT_SKIPPING_SECURE); |
Helen Li | cd0fab86 | 2018-08-13 16:07:53 | [diff] [blame] | 1079 | net_log_.AddEvent( |
| 1080 | NetLogEventType::COOKIE_STORE_COOKIE_REJECTED_SECURE, |
| 1081 | base::BindRepeating(&NetLogCookieMonsterCookieRejectedSecure, cc, |
| 1082 | &ecc)); |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 1083 | // If the cookie is equivalent to the new cookie and wouldn't have been |
| 1084 | // skipped for being HTTP-only, record that it is a skipped secure cookie |
| 1085 | // that would have been deleted otherwise. |
| 1086 | if (ecc.IsEquivalent(*cc)) { |
jww | a9a0d48 | 2015-12-16 18:19:41 | [diff] [blame] | 1087 | found_equivalent_cookie = true; |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 1088 | |
| 1089 | if (!skip_httponly || !cc->IsHttpOnly()) { |
| 1090 | histogram_cookie_delete_equivalent_->Add( |
| 1091 | COOKIE_DELETE_EQUIVALENT_WOULD_HAVE_DELETED); |
| 1092 | } |
| 1093 | } |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 1094 | } else if (ecc.IsEquivalent(*cc)) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1095 | // We should never have more than one equivalent cookie, since they should |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 1096 | // overwrite each other, unless secure cookies require secure scheme is |
| 1097 | // being enforced. In that case, cookies with different paths might exist |
| 1098 | // and be considered equivalent. |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1099 | CHECK(!found_equivalent_cookie) |
| 1100 | << "Duplicate equivalent cookies found, cookie store is corrupted."; |
Lily Chen | 22707642b | 2018-10-02 17:27:21 | [diff] [blame] | 1101 | DCHECK(cookie_it_to_possibly_delete == cookies_.end()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1102 | if (skip_httponly && cc->IsHttpOnly()) { |
| 1103 | skipped_httponly = true; |
Helen Li | cd0fab86 | 2018-08-13 16:07:53 | [diff] [blame] | 1104 | net_log_.AddEvent( |
| 1105 | NetLogEventType::COOKIE_STORE_COOKIE_REJECTED_HTTPONLY, |
| 1106 | base::BindRepeating(&NetLogCookieMonsterCookieRejectedHttponly, cc, |
| 1107 | &ecc)); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1108 | } else { |
Lily Chen | 22707642b | 2018-10-02 17:27:21 | [diff] [blame] | 1109 | cookie_it_to_possibly_delete = curit; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1110 | } |
| 1111 | found_equivalent_cookie = true; |
| 1112 | } |
| 1113 | } |
Lily Chen | 22707642b | 2018-10-02 17:27:21 | [diff] [blame] | 1114 | |
| 1115 | if (cookie_it_to_possibly_delete != cookies_.end()) { |
| 1116 | CanonicalCookie* cc_to_possibly_delete = |
| 1117 | cookie_it_to_possibly_delete->second.get(); |
| 1118 | // If a secure cookie was encountered (and left alone), don't actually |
| 1119 | // modify any of the pre-existing cookies. Only delete if no secure cookies |
| 1120 | // were skipped. |
| 1121 | if (!skipped_secure_cookie) { |
| 1122 | histogram_cookie_delete_equivalent_->Add(COOKIE_DELETE_EQUIVALENT_FOUND); |
| 1123 | if (cc_to_possibly_delete->Value() == ecc.Value()) { |
| 1124 | *creation_date_to_inherit = cc_to_possibly_delete->CreationDate(); |
| 1125 | histogram_cookie_delete_equivalent_->Add( |
| 1126 | COOKIE_DELETE_EQUIVALENT_FOUND_WITH_SAME_VALUE); |
| 1127 | } |
| 1128 | InternalDeleteCookie(cookie_it_to_possibly_delete, true, |
| 1129 | already_expired ? DELETE_COOKIE_EXPIRED_OVERWRITE |
| 1130 | : DELETE_COOKIE_OVERWRITE); |
| 1131 | } else { |
| 1132 | // If any secure cookie was skipped, preserve the pre-existing cookie. |
| 1133 | DCHECK(cc_skipped_secure); |
| 1134 | net_log_.AddEvent( |
| 1135 | NetLogEventType::COOKIE_STORE_COOKIE_PRESERVED_SKIPPED_SECURE, |
| 1136 | base::BindRepeating(&NetLogCookieMonsterCookiePreservedSkippedSecure, |
| 1137 | cc_skipped_secure, cc_to_possibly_delete, &ecc)); |
| 1138 | } |
| 1139 | } |
| 1140 | |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 1141 | if (skipped_httponly) |
| 1142 | return CanonicalCookie::CookieInclusionStatus::EXCLUDE_OVERWRITE_HTTP_ONLY; |
| 1143 | |
| 1144 | if (skipped_secure_cookie) |
| 1145 | return CanonicalCookie::CookieInclusionStatus::EXCLUDE_OVERWRITE_SECURE; |
| 1146 | |
| 1147 | return CanonicalCookie::CookieInclusionStatus::INCLUDE; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1148 | } |
| 1149 | |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1150 | CookieMonster::CookieMap::iterator CookieMonster::InternalInsertCookie( |
| 1151 | const std::string& key, |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1152 | std::unique_ptr<CanonicalCookie> cc, |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1153 | bool sync_to_store) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1154 | DCHECK(thread_checker_.CalledOnValidThread()); |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1155 | CanonicalCookie* cc_ptr = cc.get(); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1156 | |
Helen Li | cd0fab86 | 2018-08-13 16:07:53 | [diff] [blame] | 1157 | net_log_.AddEvent(NetLogEventType::COOKIE_STORE_COOKIE_ADDED, |
| 1158 | base::BindRepeating(&NetLogCookieMonsterCookieAdded, |
| 1159 | cc.get(), sync_to_store)); |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1160 | if ((cc_ptr->IsPersistent() || persist_session_cookies_) && store_.get() && |
Helen Li | d84010b | 2018-08-22 16:27:44 | [diff] [blame] | 1161 | sync_to_store) { |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1162 | store_->AddCookie(*cc_ptr); |
Helen Li | d84010b | 2018-08-22 16:27:44 | [diff] [blame] | 1163 | } |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 1164 | auto inserted = cookies_.insert(CookieMap::value_type(key, std::move(cc))); |
mkwst | c1aa4cc | 2015-04-03 19:57:45 | [diff] [blame] | 1165 | |
| 1166 | // See InitializeHistograms() for details. |
Lily Chen | f93d43e | 2019-04-18 20:22:02 | [diff] [blame] | 1167 | int32_t type_sample = |
| 1168 | cc_ptr->GetEffectiveSameSite() != CookieSameSite::NO_RESTRICTION |
| 1169 | ? 1 << COOKIE_TYPE_SAME_SITE |
| 1170 | : 0; |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1171 | type_sample |= cc_ptr->IsHttpOnly() ? 1 << COOKIE_TYPE_HTTPONLY : 0; |
| 1172 | type_sample |= cc_ptr->IsSecure() ? 1 << COOKIE_TYPE_SECURE : 0; |
mkwst | 4654941 | 2016-02-01 10:05:37 | [diff] [blame] | 1173 | histogram_cookie_type_->Add(type_sample); |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 1174 | |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 1175 | change_dispatcher_.DispatchChange(*cc_ptr, CookieChangeCause::INSERTED, true); |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1176 | |
| 1177 | return inserted; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1178 | } |
| 1179 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 1180 | void CookieMonster::SetCanonicalCookie(std::unique_ptr<CanonicalCookie> cc, |
Maks Orlovich | 44525ce | 2019-02-25 14:17:58 | [diff] [blame] | 1181 | std::string source_scheme, |
Maks Orlovich | fdbc8be | 2019-03-18 18:34:52 | [diff] [blame] | 1182 | const CookieOptions& options, |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 1183 | SetCookiesCallback callback) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1184 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 1185 | |
Maks Orlovich | 44525ce | 2019-02-25 14:17:58 | [diff] [blame] | 1186 | std::string scheme_lower = base::ToLowerASCII(source_scheme); |
| 1187 | bool secure_source = GURL::SchemeIsCryptographic(scheme_lower); |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 1188 | if ((cc->IsSecure() && !secure_source)) { |
| 1189 | MaybeRunCookieCallback( |
| 1190 | std::move(callback), |
| 1191 | CanonicalCookie::CookieInclusionStatus::EXCLUDE_SECURE_ONLY); |
| 1192 | return; |
| 1193 | } |
| 1194 | |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 1195 | CanonicalCookie::CookieInclusionStatus status = |
| 1196 | cc->IsSetPermittedInContext(options); |
| 1197 | if (status != CanonicalCookie::CookieInclusionStatus::INCLUDE) { |
| 1198 | // IsSetPermittedInContext already logs if it rejects a cookie, so |
| 1199 | // CookieMonster doesn't need to. |
| 1200 | MaybeRunCookieCallback(std::move(callback), status); |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 1201 | return; |
| 1202 | } |
rdsmith | 2709eee | 2017-06-20 22:43:27 | [diff] [blame] | 1203 | |
Maks Orlovich | 44525ce | 2019-02-25 14:17:58 | [diff] [blame] | 1204 | if (!IsCookieableScheme(scheme_lower)) { |
| 1205 | MaybeRunCookieCallback( |
| 1206 | std::move(callback), |
| 1207 | CanonicalCookie::CookieInclusionStatus::EXCLUDE_NONCOOKIEABLE_SCHEME); |
| 1208 | return; |
| 1209 | } |
| 1210 | |
Lily Chen | 8a648cb7 | 2019-04-25 20:29:15 | [diff] [blame] | 1211 | // If both SameSiteByDefaultCookies and CookiesWithoutSameSiteMustBeSecure |
| 1212 | // are enabled, non-SameSite cookies without the Secure attribute will be |
Lily Chen | d3bf638d | 2019-05-03 16:54:25 | [diff] [blame] | 1213 | // rejected. |
Lily Chen | 8a648cb7 | 2019-04-25 20:29:15 | [diff] [blame] | 1214 | if (base::FeatureList::IsEnabled(features::kSameSiteByDefaultCookies) && |
| 1215 | base::FeatureList::IsEnabled( |
| 1216 | features::kCookiesWithoutSameSiteMustBeSecure) && |
| 1217 | cc->GetEffectiveSameSite() == CookieSameSite::NO_RESTRICTION && |
| 1218 | !cc->IsSecure()) { |
Lily Chen | 8a648cb7 | 2019-04-25 20:29:15 | [diff] [blame] | 1219 | DVLOG(net::cookie_util::kVlogSetCookies) |
Lily Chen | d3bf638d | 2019-05-03 16:54:25 | [diff] [blame] | 1220 | << "SetCookie() rejecting insecure cookie with SameSite=None."; |
| 1221 | status = |
| 1222 | CanonicalCookie::CookieInclusionStatus::EXCLUDE_SAMESITE_NONE_INSECURE; |
| 1223 | MaybeRunCookieCallback(std::move(callback), status); |
| 1224 | return; |
Lily Chen | 8a648cb7 | 2019-04-25 20:29:15 | [diff] [blame] | 1225 | } |
| 1226 | |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 1227 | const std::string key(GetKey(cc->Domain())); |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 1228 | |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 1229 | base::Time creation_date = cc->CreationDate(); |
| 1230 | if (creation_date.is_null()) { |
Maks Orlovich | c86cf29 | 2019-02-11 19:25:17 | [diff] [blame] | 1231 | creation_date = Time::Now(); |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 1232 | cc->SetCreationDate(creation_date); |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 1233 | } |
| 1234 | bool already_expired = cc->IsExpired(creation_date); |
ellyjones | 399e35a2 | 2014-10-27 11:09:56 | [diff] [blame] | 1235 | |
Mike West | c4a777b | 2017-10-06 14:04:20 | [diff] [blame] | 1236 | base::Time creation_date_to_inherit; |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 1237 | |
Maks Orlovich | 8a3e413 | 2019-04-23 16:59:24 | [diff] [blame] | 1238 | status = DeleteAnyEquivalentCookie( |
Maks Orlovich | fdbc8be | 2019-03-18 18:34:52 | [diff] [blame] | 1239 | key, *cc, secure_source, options.exclude_httponly(), already_expired, |
| 1240 | &creation_date_to_inherit); |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 1241 | |
| 1242 | if (status != CanonicalCookie::CookieInclusionStatus::INCLUDE) { |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 1243 | std::string error; |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 1244 | error = |
| 1245 | "SetCookie() not clobbering httponly cookie or secure cookie for " |
| 1246 | "insecure scheme"; |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 1247 | |
Maks Orlovich | 35d74c04 | 2019-04-18 16:38:01 | [diff] [blame] | 1248 | DVLOG(net::cookie_util::kVlogSetCookies) << error; |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 1249 | MaybeRunCookieCallback(std::move(callback), status); |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 1250 | return; |
[email protected] | 3a96c74 | 2008-11-19 19:46:27 | [diff] [blame] | 1251 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1252 | |
Maks Orlovich | 35d74c04 | 2019-04-18 16:38:01 | [diff] [blame] | 1253 | DVLOG(net::cookie_util::kVlogSetCookies) |
Oscar Johansson | 63e83cf | 2018-07-02 08:47:26 | [diff] [blame] | 1254 | << "SetCookie() key: " << key << " cc: " << cc->DebugString(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1255 | |
| 1256 | // Realize that we might be setting an expired cookie, and the only point |
| 1257 | // was to delete the cookie which we've already done. |
mmenke | 3c79a65 | 2016-02-12 14:39:20 | [diff] [blame] | 1258 | if (!already_expired) { |
[email protected] | 374f58b | 2010-07-20 15:29:26 | [diff] [blame] | 1259 | // See InitializeHistograms() for details. |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 1260 | if (cc->IsPersistent()) { |
[email protected] | 8475bee | 2011-03-17 18:40:24 | [diff] [blame] | 1261 | histogram_expiration_duration_minutes_->Add( |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 1262 | (cc->ExpiryDate() - creation_date).InMinutes()); |
[email protected] | 8475bee | 2011-03-17 18:40:24 | [diff] [blame] | 1263 | } |
| 1264 | |
rdsmith | 2709eee | 2017-06-20 22:43:27 | [diff] [blame] | 1265 | // Histogram the type of scheme used on URLs that set cookies. This |
| 1266 | // intentionally includes cookies that are set or overwritten by |
| 1267 | // http:// URLs, but not cookies that are cleared by http:// URLs, to |
| 1268 | // understand if the former behavior can be deprecated for Secure |
| 1269 | // cookies. |
| 1270 | CookieSource cookie_source_sample = |
| 1271 | (secure_source |
| 1272 | ? (cc->IsSecure() |
| 1273 | ? COOKIE_SOURCE_SECURE_COOKIE_CRYPTOGRAPHIC_SCHEME |
| 1274 | : COOKIE_SOURCE_NONSECURE_COOKIE_CRYPTOGRAPHIC_SCHEME) |
| 1275 | : (cc->IsSecure() |
| 1276 | ? COOKIE_SOURCE_SECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME |
| 1277 | : COOKIE_SOURCE_NONSECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME)); |
| 1278 | histogram_cookie_source_scheme_->Add(cookie_source_sample); |
| 1279 | |
Mike West | c4a777b | 2017-10-06 14:04:20 | [diff] [blame] | 1280 | if (!creation_date_to_inherit.is_null()) { |
| 1281 | cc->SetCreationDate(creation_date_to_inherit); |
Mike West | c4a777b | 2017-10-06 14:04:20 | [diff] [blame] | 1282 | } |
| 1283 | |
rdsmith | 2709eee | 2017-06-20 22:43:27 | [diff] [blame] | 1284 | InternalInsertCookie(key, std::move(cc), true); |
[email protected] | 348dd66 | 2013-03-13 20:25:07 | [diff] [blame] | 1285 | } else { |
Maks Orlovich | 35d74c04 | 2019-04-18 16:38:01 | [diff] [blame] | 1286 | DVLOG(net::cookie_util::kVlogSetCookies) |
Oscar Johansson | 63e83cf | 2018-07-02 08:47:26 | [diff] [blame] | 1287 | << "SetCookie() not storing already expired cookie."; |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 1288 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1289 | |
| 1290 | // We assume that hopefully setting a cookie will be less common than |
| 1291 | // querying a cookie. Since setting a cookie can put us over our limits, |
| 1292 | // make sure that we garbage collect... We can also make the assumption that |
| 1293 | // if a cookie was set, in the common case it will be used soon after, |
| 1294 | // and we will purge the expired cookies in GetCookies(). |
rdsmith | a6ce444 | 2017-06-21 17:11:05 | [diff] [blame] | 1295 | GarbageCollect(creation_date, key); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1296 | |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 1297 | MaybeRunCookieCallback(std::move(callback), |
| 1298 | CanonicalCookie::CookieInclusionStatus::INCLUDE); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1299 | } |
| 1300 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 1301 | void CookieMonster::SetAllCookies(CookieList list, |
| 1302 | SetCookiesCallback callback) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1303 | DCHECK(thread_checker_.CalledOnValidThread()); |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 1304 | |
rdsmith | 0e84cea | 2017-07-13 03:09:53 | [diff] [blame] | 1305 | // Nuke the existing store. |
| 1306 | while (!cookies_.empty()) { |
| 1307 | // TODO(rdsmith): The CANONICAL is a lie. |
Nick Harper | 7a6683a | 2018-01-30 20:42:52 | [diff] [blame] | 1308 | InternalDeleteCookie(cookies_.begin(), true, DELETE_COOKIE_EXPLICIT); |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 1309 | } |
| 1310 | |
rdsmith | 0e84cea | 2017-07-13 03:09:53 | [diff] [blame] | 1311 | // Set all passed in cookies. |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 1312 | for (const auto& cookie : list) { |
rdsmith | 2709eee | 2017-06-20 22:43:27 | [diff] [blame] | 1313 | const std::string key(GetKey(cookie.Domain())); |
| 1314 | Time creation_time = cookie.CreationDate(); |
rdsmith | 0e84cea | 2017-07-13 03:09:53 | [diff] [blame] | 1315 | if (cookie.IsExpired(creation_time)) |
rdsmith | 2709eee | 2017-06-20 22:43:27 | [diff] [blame] | 1316 | continue; |
| 1317 | |
| 1318 | if (cookie.IsPersistent()) { |
| 1319 | histogram_expiration_duration_minutes_->Add( |
| 1320 | (cookie.ExpiryDate() - creation_time).InMinutes()); |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 1321 | } |
rdsmith | 2709eee | 2017-06-20 22:43:27 | [diff] [blame] | 1322 | |
Jeremy Roman | 0579ed6 | 2017-08-29 15:56:19 | [diff] [blame] | 1323 | InternalInsertCookie(key, std::make_unique<CanonicalCookie>(cookie), true); |
rdsmith | 2709eee | 2017-06-20 22:43:27 | [diff] [blame] | 1324 | GarbageCollect(creation_time, key); |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 1325 | } |
| 1326 | |
rdsmith | 2709eee | 2017-06-20 22:43:27 | [diff] [blame] | 1327 | // TODO(rdsmith): If this function always returns the same value, it |
| 1328 | // shouldn't have a return value. But it should also be deleted (see |
| 1329 | // https://codereview.chromium.org/2882063002/#msg64), which would |
| 1330 | // solve the return value problem. |
Aaron Tagliaboschi | 29764f5 | 2019-02-21 17:19:59 | [diff] [blame] | 1331 | MaybeRunCookieCallback(std::move(callback), |
| 1332 | CanonicalCookie::CookieInclusionStatus::INCLUDE); |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 1333 | } |
| 1334 | |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1335 | void CookieMonster::InternalUpdateCookieAccessTime(CanonicalCookie* cc, |
| 1336 | const Time& current) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1337 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | bb890572 | 2010-05-21 17:29:04 | [diff] [blame] | 1338 | |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 1339 | // Based off the Mozilla code. When a cookie has been accessed recently, |
| 1340 | // don't bother updating its access time again. This reduces the number of |
| 1341 | // updates we do during pageload, which in turn reduces the chance our storage |
| 1342 | // backend will hit its batch thresholds and be forced to update. |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 1343 | if ((current - cc->LastAccessDate()) < last_access_threshold_) |
| 1344 | return; |
| 1345 | |
| 1346 | cc->SetLastAccessDate(current); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1347 | if ((cc->IsPersistent() || persist_session_cookies_) && store_.get()) |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 1348 | store_->UpdateCookieAccessTime(*cc); |
| 1349 | } |
| 1350 | |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1351 | // InternalDeleteCookies must not invalidate iterators other than the one being |
| 1352 | // deleted. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1353 | void CookieMonster::InternalDeleteCookie(CookieMap::iterator it, |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 1354 | bool sync_to_store, |
| 1355 | DeletionCause deletion_cause) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1356 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | bb890572 | 2010-05-21 17:29:04 | [diff] [blame] | 1357 | |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 1358 | // Ideally, this would be asserted up where we define kChangeCauseMapping, |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 1359 | // but DeletionCause's visibility (or lack thereof) forces us to make |
| 1360 | // this check here. |
Ryan Sleevi | 435a3a2 | 2018-05-15 02:16:07 | [diff] [blame] | 1361 | static_assert(base::size(kChangeCauseMapping) == DELETE_COOKIE_LAST_ENTRY + 1, |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 1362 | "kChangeCauseMapping size should match DeletionCause size"); |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 1363 | |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1364 | CanonicalCookie* cc = it->second.get(); |
Maks Orlovich | 35d74c04 | 2019-04-18 16:38:01 | [diff] [blame] | 1365 | DVLOG(net::cookie_util::kVlogSetCookies) |
Oscar Johansson | 63e83cf | 2018-07-02 08:47:26 | [diff] [blame] | 1366 | << "InternalDeleteCookie()" |
| 1367 | << ", cause:" << deletion_cause << ", cc: " << cc->DebugString(); |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1368 | |
Helen Li | cd0fab86 | 2018-08-13 16:07:53 | [diff] [blame] | 1369 | ChangeCausePair mapping = kChangeCauseMapping[deletion_cause]; |
| 1370 | if (deletion_cause != DELETE_COOKIE_DONT_RECORD) { |
| 1371 | net_log_.AddEvent(NetLogEventType::COOKIE_STORE_COOKIE_DELETED, |
| 1372 | base::BindRepeating(&NetLogCookieMonsterCookieDeleted, cc, |
| 1373 | mapping.cause, sync_to_store)); |
| 1374 | } |
| 1375 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1376 | if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() && |
Helen Li | d84010b | 2018-08-22 16:27:44 | [diff] [blame] | 1377 | sync_to_store) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1378 | store_->DeleteCookie(*cc); |
Helen Li | d84010b | 2018-08-22 16:27:44 | [diff] [blame] | 1379 | } |
Victor Costan | 14f47c1 | 2018-03-01 08:02:24 | [diff] [blame] | 1380 | change_dispatcher_.DispatchChange(*cc, mapping.cause, mapping.notify); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1381 | cookies_.erase(it); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1382 | } |
| 1383 | |
[email protected] | 8807b32 | 2010-10-01 17:10:14 | [diff] [blame] | 1384 | // Domain expiry behavior is unchanged by key/expiry scheme (the |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1385 | // meaning of the key is different, but that's not visible to this routine). |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 1386 | size_t CookieMonster::GarbageCollect(const Time& current, |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 1387 | const std::string& key) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1388 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | bb890572 | 2010-05-21 17:29:04 | [diff] [blame] | 1389 | |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 1390 | size_t num_deleted = 0; |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1391 | Time safe_date(Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1392 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1393 | // Collect garbage for this key, minding cookie priorities. |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1394 | if (cookies_.count(key) > kDomainMaxCookies) { |
Maks Orlovich | 35d74c04 | 2019-04-18 16:38:01 | [diff] [blame] | 1395 | DVLOG(net::cookie_util::kVlogGarbageCollection) |
Oscar Johansson | 63e83cf | 2018-07-02 08:47:26 | [diff] [blame] | 1396 | << "GarbageCollect() key: " << key; |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1397 | |
mkwst | 8773435 | 2016-03-03 17:36:23 | [diff] [blame] | 1398 | CookieItVector* cookie_its; |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 1399 | |
mkwst | 8773435 | 2016-03-03 17:36:23 | [diff] [blame] | 1400 | CookieItVector non_expired_cookie_its; |
| 1401 | cookie_its = &non_expired_cookie_its; |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 1402 | num_deleted += |
mkwst | 8773435 | 2016-03-03 17:36:23 | [diff] [blame] | 1403 | GarbageCollectExpired(current, cookies_.equal_range(key), cookie_its); |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 1404 | |
mkwst | 8773435 | 2016-03-03 17:36:23 | [diff] [blame] | 1405 | if (cookie_its->size() > kDomainMaxCookies) { |
Maks Orlovich | 35d74c04 | 2019-04-18 16:38:01 | [diff] [blame] | 1406 | DVLOG(net::cookie_util::kVlogGarbageCollection) |
Oscar Johansson | 63e83cf | 2018-07-02 08:47:26 | [diff] [blame] | 1407 | << "Deep Garbage Collect domain."; |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1408 | size_t purge_goal = |
mkwst | 8773435 | 2016-03-03 17:36:23 | [diff] [blame] | 1409 | cookie_its->size() - (kDomainMaxCookies - kDomainPurgeCookies); |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1410 | DCHECK(purge_goal > kDomainPurgeCookies); |
| 1411 | |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 1412 | // Sort the cookies by access date, from least-recent to most-recent. |
| 1413 | std::sort(cookie_its->begin(), cookie_its->end(), LRACookieSorter); |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1414 | |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 1415 | // Remove all but the kDomainCookiesQuotaLow most-recently accessed |
| 1416 | // cookies with low-priority. Then, if cookies still need to be removed, |
| 1417 | // bump the quota and remove low- and medium-priority. Then, if cookies |
| 1418 | // _still_ need to be removed, bump the quota and remove cookies with |
| 1419 | // any priority. |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1420 | // |
| 1421 | // 1. Low-priority non-secure cookies. |
| 1422 | // 2. Low-priority secure cookies. |
| 1423 | // 3. Medium-priority non-secure cookies. |
| 1424 | // 4. High-priority non-secure cookies. |
| 1425 | // 5. Medium-priority secure cookies. |
| 1426 | // 6. High-priority secure cookies. |
| 1427 | const static struct { |
| 1428 | CookiePriority priority; |
| 1429 | bool protect_secure_cookies; |
| 1430 | } purge_rounds[] = { |
| 1431 | // 1. Low-priority non-secure cookies. |
| 1432 | {COOKIE_PRIORITY_LOW, true}, |
| 1433 | // 2. Low-priority secure cookies. |
| 1434 | {COOKIE_PRIORITY_LOW, false}, |
| 1435 | // 3. Medium-priority non-secure cookies. |
| 1436 | {COOKIE_PRIORITY_MEDIUM, true}, |
| 1437 | // 4. High-priority non-secure cookies. |
| 1438 | {COOKIE_PRIORITY_HIGH, true}, |
| 1439 | // 5. Medium-priority secure cookies. |
| 1440 | {COOKIE_PRIORITY_MEDIUM, false}, |
| 1441 | // 6. High-priority secure cookies. |
| 1442 | {COOKIE_PRIORITY_HIGH, false}, |
| 1443 | }; |
| 1444 | |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 1445 | size_t quota = 0; |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1446 | for (const auto& purge_round : purge_rounds) { |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 1447 | // Adjust quota according to the priority of cookies. Each round should |
| 1448 | // protect certain number of cookies in order to avoid starvation. |
| 1449 | // For example, when each round starts to remove cookies, the number of |
| 1450 | // cookies of that priority are counted and a decision whether they |
| 1451 | // should be deleted or not is made. If yes, some number of cookies of |
| 1452 | // that priority are deleted considering the quota. |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1453 | switch (purge_round.priority) { |
| 1454 | case COOKIE_PRIORITY_LOW: |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 1455 | quota = kDomainCookiesQuotaLow; |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1456 | break; |
| 1457 | case COOKIE_PRIORITY_MEDIUM: |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 1458 | quota = kDomainCookiesQuotaMedium; |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1459 | break; |
| 1460 | case COOKIE_PRIORITY_HIGH: |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 1461 | quota = kDomainCookiesQuotaHigh; |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1462 | break; |
| 1463 | } |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1464 | size_t just_deleted = 0u; |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 1465 | // Purge up to |purge_goal| for all cookies at the given priority. This |
| 1466 | // path will be taken only if the initial non-secure purge did not evict |
| 1467 | // enough cookies. |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1468 | if (purge_goal > 0) { |
| 1469 | just_deleted = PurgeLeastRecentMatches( |
| 1470 | cookie_its, purge_round.priority, quota, purge_goal, |
| 1471 | purge_round.protect_secure_cookies); |
| 1472 | DCHECK_LE(just_deleted, purge_goal); |
| 1473 | purge_goal -= just_deleted; |
| 1474 | num_deleted += just_deleted; |
| 1475 | } |
mkwst | 162d271 | 2016-02-18 18:21:29 | [diff] [blame] | 1476 | } |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 1477 | |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1478 | DCHECK_EQ(0u, purge_goal); |
[email protected] | 8807b32 | 2010-10-01 17:10:14 | [diff] [blame] | 1479 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1480 | } |
| 1481 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1482 | // Collect garbage for everything. With firefox style we want to preserve |
| 1483 | // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict. |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1484 | if (cookies_.size() > kMaxCookies && earliest_access_time_ < safe_date) { |
Maks Orlovich | 35d74c04 | 2019-04-18 16:38:01 | [diff] [blame] | 1485 | DVLOG(net::cookie_util::kVlogGarbageCollection) |
Oscar Johansson | 63e83cf | 2018-07-02 08:47:26 | [diff] [blame] | 1486 | << "GarbageCollect() everything"; |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1487 | CookieItVector cookie_its; |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 1488 | |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1489 | num_deleted += GarbageCollectExpired( |
| 1490 | current, CookieMapItPair(cookies_.begin(), cookies_.end()), |
| 1491 | &cookie_its); |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 1492 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1493 | if (cookie_its.size() > kMaxCookies) { |
Maks Orlovich | 35d74c04 | 2019-04-18 16:38:01 | [diff] [blame] | 1494 | DVLOG(net::cookie_util::kVlogGarbageCollection) |
Oscar Johansson | 63e83cf | 2018-07-02 08:47:26 | [diff] [blame] | 1495 | << "Deep Garbage Collect everything."; |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1496 | size_t purge_goal = cookie_its.size() - (kMaxCookies - kPurgeCookies); |
| 1497 | DCHECK(purge_goal > kPurgeCookies); |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 1498 | |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 1499 | CookieItVector secure_cookie_its; |
| 1500 | CookieItVector non_secure_cookie_its; |
| 1501 | SplitCookieVectorIntoSecureAndNonSecure(cookie_its, &secure_cookie_its, |
| 1502 | &non_secure_cookie_its); |
| 1503 | size_t non_secure_purge_goal = |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 1504 | std::min<size_t>(purge_goal, non_secure_cookie_its.size()); |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 1505 | |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 1506 | base::Time earliest_non_secure_access_time; |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 1507 | size_t just_deleted = GarbageCollectLeastRecentlyAccessed( |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 1508 | current, safe_date, non_secure_purge_goal, non_secure_cookie_its, |
| 1509 | &earliest_non_secure_access_time); |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 1510 | num_deleted += just_deleted; |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 1511 | |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 1512 | if (secure_cookie_its.size() == 0) { |
| 1513 | // This case is unlikely, but should still update |
| 1514 | // |earliest_access_time_| if only have non-secure cookies. |
| 1515 | earliest_access_time_ = earliest_non_secure_access_time; |
| 1516 | // Garbage collection can't delete all cookies. |
| 1517 | DCHECK(!earliest_access_time_.is_null()); |
| 1518 | } else if (just_deleted < purge_goal) { |
| 1519 | size_t secure_purge_goal = std::min<size_t>(purge_goal - just_deleted, |
| 1520 | secure_cookie_its.size()); |
| 1521 | base::Time earliest_secure_access_time; |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 1522 | num_deleted += GarbageCollectLeastRecentlyAccessed( |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 1523 | current, safe_date, secure_purge_goal, secure_cookie_its, |
| 1524 | &earliest_secure_access_time); |
| 1525 | |
| 1526 | if (!earliest_non_secure_access_time.is_null() && |
| 1527 | earliest_non_secure_access_time < earliest_secure_access_time) { |
| 1528 | earliest_access_time_ = earliest_non_secure_access_time; |
| 1529 | } else { |
| 1530 | earliest_access_time_ = earliest_secure_access_time; |
| 1531 | } |
| 1532 | |
| 1533 | // Garbage collection can't delete all cookies. |
| 1534 | DCHECK(!earliest_access_time_.is_null()); |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 1535 | } |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 1536 | |
| 1537 | // If there are secure cookies, but deleting non-secure cookies was enough |
| 1538 | // to meet the purge goal, secure cookies are never examined, so |
| 1539 | // |earliest_access_time_| can't be determined. Leaving it alone will mean |
| 1540 | // it's no later than the real earliest last access time, so this won't |
| 1541 | // lead to any problems. |
[email protected] | 8807b32 | 2010-10-01 17:10:14 | [diff] [blame] | 1542 | } |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | return num_deleted; |
| 1546 | } |
| 1547 | |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 1548 | size_t CookieMonster::PurgeLeastRecentMatches(CookieItVector* cookies, |
| 1549 | CookiePriority priority, |
| 1550 | size_t to_protect, |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1551 | size_t purge_goal, |
| 1552 | bool protect_secure_cookies) { |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 1553 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 1554 | |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 1555 | // 1. Count number of the cookies at |priority| |
| 1556 | size_t cookies_count_possibly_to_be_deleted = CountCookiesForPossibleDeletion( |
| 1557 | priority, cookies, false /* count all cookies */); |
| 1558 | |
| 1559 | // 2. If |cookies_count_possibly_to_be_deleted| at |priority| is less than or |
| 1560 | // equal |to_protect|, skip round in order to preserve the quota. This |
| 1561 | // involves secure and non-secure cookies at |priority|. |
| 1562 | if (cookies_count_possibly_to_be_deleted <= to_protect) |
| 1563 | return 0u; |
| 1564 | |
| 1565 | // 3. Calculate number of secure cookies at |priority| |
| 1566 | // and number of cookies at |priority| that can possibly be deleted. |
| 1567 | // It is guaranteed we do not delete more than |purge_goal| even if |
| 1568 | // |cookies_count_possibly_to_be_deleted| is higher. |
| 1569 | size_t secure_cookies = 0u; |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1570 | if (protect_secure_cookies) { |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 1571 | secure_cookies = CountCookiesForPossibleDeletion( |
| 1572 | priority, cookies, protect_secure_cookies /* count secure cookies */); |
| 1573 | cookies_count_possibly_to_be_deleted -= |
| 1574 | std::max(secure_cookies, to_protect - secure_cookies); |
| 1575 | } else { |
| 1576 | cookies_count_possibly_to_be_deleted -= to_protect; |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1577 | } |
| 1578 | |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 1579 | size_t removed = 0u; |
| 1580 | size_t current = 0u; |
| 1581 | while ((removed < purge_goal && current < cookies->size()) && |
| 1582 | cookies_count_possibly_to_be_deleted > 0) { |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1583 | const CanonicalCookie* current_cookie = cookies->at(current)->second.get(); |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 1584 | // Only delete the current cookie if the priority is equal to |
| 1585 | // the current level. |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1586 | if (IsCookieEligibleForEviction(priority, protect_secure_cookies, |
| 1587 | current_cookie)) { |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 1588 | InternalDeleteCookie(cookies->at(current), true, |
| 1589 | DELETE_COOKIE_EVICTED_DOMAIN); |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 1590 | cookies->erase(cookies->begin() + current); |
| 1591 | removed++; |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 1592 | cookies_count_possibly_to_be_deleted--; |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 1593 | } else { |
| 1594 | current++; |
| 1595 | } |
| 1596 | } |
| 1597 | return removed; |
| 1598 | } |
| 1599 | |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 1600 | size_t CookieMonster::GarbageCollectExpired(const Time& current, |
| 1601 | const CookieMapItPair& itpair, |
| 1602 | CookieItVector* cookie_its) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1603 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | bb890572 | 2010-05-21 17:29:04 | [diff] [blame] | 1604 | |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 1605 | int num_deleted = 0; |
| 1606 | for (CookieMap::iterator it = itpair.first, end = itpair.second; it != end;) { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 1607 | auto curit = it; |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 1608 | ++it; |
| 1609 | |
| 1610 | if (curit->second->IsExpired(current)) { |
[email protected] | 2f3f359 | 2010-07-07 20:11:51 | [diff] [blame] | 1611 | InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED); |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 1612 | ++num_deleted; |
| 1613 | } else if (cookie_its) { |
| 1614 | cookie_its->push_back(curit); |
| 1615 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | return num_deleted; |
| 1619 | } |
| 1620 | |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 1621 | size_t CookieMonster::GarbageCollectDeleteRange( |
| 1622 | const Time& current, |
| 1623 | DeletionCause cause, |
| 1624 | CookieItVector::iterator it_begin, |
| 1625 | CookieItVector::iterator it_end) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1626 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 1627 | |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 1628 | for (auto it = it_begin; it != it_end; it++) { |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1629 | InternalDeleteCookie((*it), true, cause); |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 1630 | } |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1631 | return it_end - it_begin; |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 1632 | } |
| 1633 | |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1634 | size_t CookieMonster::GarbageCollectLeastRecentlyAccessed( |
| 1635 | const base::Time& current, |
| 1636 | const base::Time& safe_date, |
| 1637 | size_t purge_goal, |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 1638 | CookieItVector cookie_its, |
| 1639 | base::Time* earliest_time) { |
| 1640 | DCHECK_LE(purge_goal, cookie_its.size()); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1641 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 1642 | |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 1643 | // Sorts up to *and including* |cookie_its[purge_goal]| (if it exists), so |
| 1644 | // |earliest_time| will be properly assigned even if |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1645 | // |global_purge_it| == |cookie_its.begin() + purge_goal|. |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 1646 | SortLeastRecentlyAccessed( |
| 1647 | cookie_its.begin(), cookie_its.end(), |
| 1648 | cookie_its.size() < purge_goal ? purge_goal + 1 : purge_goal); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1649 | // Find boundary to cookies older than safe_date. |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 1650 | auto global_purge_it = LowerBoundAccessDate( |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1651 | cookie_its.begin(), cookie_its.begin() + purge_goal, safe_date); |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 1652 | // Only delete the old cookies and delete non-secure ones first. |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1653 | size_t num_deleted = |
| 1654 | GarbageCollectDeleteRange(current, DELETE_COOKIE_EVICTED_GLOBAL, |
| 1655 | cookie_its.begin(), global_purge_it); |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame] | 1656 | if (global_purge_it != cookie_its.end()) |
| 1657 | *earliest_time = (*global_purge_it)->second->LastAccessDate(); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1658 | return num_deleted; |
| 1659 | } |
| 1660 | |
[email protected] | ed32c21 | 2013-05-14 20:49:29 | [diff] [blame] | 1661 | // A wrapper around registry_controlled_domains::GetDomainAndRegistry |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 1662 | // to make clear we're creating a key for our local map or for the persistent |
Aaron Tagliaboschi | bd34cc0c | 2019-01-31 01:19:52 | [diff] [blame] | 1663 | // store's use. Here and in FindCookiesForRegistryControlledHost() are the only |
| 1664 | // two places where we need to conditionalize based on key type. |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1665 | // |
| 1666 | // Note that this key algorithm explicitly ignores the scheme. This is |
| 1667 | // because when we're entering cookies into the map from the backing store, |
| 1668 | // we in general won't have the scheme at that point. |
| 1669 | // In practical terms, this means that file cookies will be stored |
| 1670 | // in the map either by an empty string or by UNC name (and will be |
| 1671 | // limited by kMaxCookiesPerHost), and extension cookies will be stored |
| 1672 | // based on the single extension id, as the extension id won't have the |
| 1673 | // form of a DNS host and hence GetKey() will return it unchanged. |
| 1674 | // |
| 1675 | // Arguably the right thing to do here is to make the key |
| 1676 | // algorithm dependent on the scheme, and make sure that the scheme is |
| 1677 | // available everywhere the key must be obtained (specfically at backing |
| 1678 | // store load time). This would require either changing the backing store |
| 1679 | // database schema to include the scheme (far more trouble than it's worth), or |
| 1680 | // separating out file cookies into their own CookieMonster instance and |
| 1681 | // thus restricting each scheme to a single cookie monster (which might |
| 1682 | // be worth it, but is still too much trouble to solve what is currently a |
| 1683 | // non-problem). |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 1684 | // |
| 1685 | // static |
| 1686 | std::string CookieMonster::GetKey(base::StringPiece domain) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1687 | std::string effective_domain( |
[email protected] | ed32c21 | 2013-05-14 20:49:29 | [diff] [blame] | 1688 | registry_controlled_domains::GetDomainAndRegistry( |
[email protected] | aabe179 | 2014-01-30 21:37:46 | [diff] [blame] | 1689 | domain, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1690 | if (effective_domain.empty()) |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 1691 | domain.CopyToString(&effective_domain); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1692 | |
| 1693 | if (!effective_domain.empty() && effective_domain[0] == '.') |
| 1694 | return effective_domain.substr(1); |
| 1695 | return effective_domain; |
| 1696 | } |
| 1697 | |
| 1698 | bool CookieMonster::HasCookieableScheme(const GURL& url) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1699 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1700 | |
| 1701 | // Make sure the request is on a cookie-able url scheme. |
| 1702 | for (size_t i = 0; i < cookieable_schemes_.size(); ++i) { |
| 1703 | // We matched a scheme. |
| 1704 | if (url.SchemeIs(cookieable_schemes_[i].c_str())) { |
| 1705 | // We've matched a supported scheme. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1706 | return true; |
| 1707 | } |
| 1708 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1709 | |
| 1710 | // The scheme didn't match any in our whitelist. |
Maks Orlovich | 35d74c04 | 2019-04-18 16:38:01 | [diff] [blame] | 1711 | DVLOG(net::cookie_util::kVlogPerCookieMonster) |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1712 | << "WARNING: Unsupported cookie scheme: " << url.scheme(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1713 | return false; |
| 1714 | } |
| 1715 | |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 1716 | // Test to see if stats should be recorded, and record them if so. |
| 1717 | // The goal here is to get sampling for the average browser-hour of |
| 1718 | // activity. We won't take samples when the web isn't being surfed, |
| 1719 | // and when the web is being surfed, we'll take samples about every |
| 1720 | // kRecordStatisticsIntervalSeconds. |
| 1721 | // last_statistic_record_time_ is initialized to Now() rather than null |
| 1722 | // in the constructor so that we won't take statistics right after |
| 1723 | // startup, to avoid bias from browsers that are started but not used. |
| 1724 | void CookieMonster::RecordPeriodicStats(const base::Time& current_time) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1725 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 1726 | |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 1727 | const base::TimeDelta kRecordStatisticsIntervalTime( |
| 1728 | base::TimeDelta::FromSeconds(kRecordStatisticsIntervalSeconds)); |
| 1729 | |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1730 | // If we've taken statistics recently, return. |
| 1731 | if (current_time - last_statistic_record_time_ <= |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 1732 | kRecordStatisticsIntervalTime) { |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1733 | return; |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 1734 | } |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1735 | |
| 1736 | // See InitializeHistograms() for details. |
| 1737 | histogram_count_->Add(cookies_.size()); |
| 1738 | |
| 1739 | // More detailed statistics on cookie counts at different granularities. |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1740 | last_statistic_record_time_ = current_time; |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 1741 | } |
| 1742 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1743 | // Initialize all histogram counter variables used in this class. |
| 1744 | // |
| 1745 | // Normal histogram usage involves using the macros defined in |
| 1746 | // histogram.h, which automatically takes care of declaring these |
| 1747 | // variables (as statics), initializing them, and accumulating into |
| 1748 | // them, all from a single entry point. Unfortunately, that solution |
| 1749 | // doesn't work for the CookieMonster, as it's vulnerable to races between |
| 1750 | // separate threads executing the same functions and hence initializing the |
| 1751 | // same static variables. There isn't a race danger in the histogram |
| 1752 | // accumulation calls; they are written to be resilient to simultaneous |
| 1753 | // calls from multiple threads. |
| 1754 | // |
| 1755 | // The solution taken here is to have per-CookieMonster instance |
| 1756 | // variables that are constructed during CookieMonster construction. |
| 1757 | // Note that these variables refer to the same underlying histogram, |
| 1758 | // so we still race (but safely) with other CookieMonster instances |
| 1759 | // for accumulation. |
| 1760 | // |
| 1761 | // To do this we've expanded out the individual histogram macros calls, |
| 1762 | // with declarations of the variables in the class decl, initialization here |
| 1763 | // (done from the class constructor) and direct calls to the accumulation |
| 1764 | // methods where needed. The specific histogram macro calls on which the |
| 1765 | // initialization is based are included in comments below. |
| 1766 | void CookieMonster::InitializeHistograms() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1767 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 1768 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1769 | // From UMA_HISTOGRAM_CUSTOM_COUNTS |
| 1770 | histogram_expiration_duration_minutes_ = base::Histogram::FactoryGet( |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1771 | "Cookie.ExpirationDurationMinutes", 1, kMinutesInTenYears, 50, |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1772 | base::Histogram::kUmaTargetedHistogramFlag); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1773 | histogram_count_ = base::Histogram::FactoryGet( |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1774 | "Cookie.Count", 1, 4000, 50, base::Histogram::kUmaTargetedHistogramFlag); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1775 | |
| 1776 | // From UMA_HISTOGRAM_ENUMERATION |
mkwst | c1aa4cc | 2015-04-03 19:57:45 | [diff] [blame] | 1777 | histogram_cookie_type_ = base::LinearHistogram::FactoryGet( |
mkwst | 87378d9 | 2015-04-10 21:22:11 | [diff] [blame] | 1778 | "Cookie.Type", 1, (1 << COOKIE_TYPE_LAST_ENTRY) - 1, |
| 1779 | 1 << COOKIE_TYPE_LAST_ENTRY, base::Histogram::kUmaTargetedHistogramFlag); |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 1780 | histogram_cookie_source_scheme_ = base::LinearHistogram::FactoryGet( |
| 1781 | "Cookie.CookieSourceScheme", 1, COOKIE_SOURCE_LAST_ENTRY - 1, |
| 1782 | COOKIE_SOURCE_LAST_ENTRY, base::Histogram::kUmaTargetedHistogramFlag); |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 1783 | histogram_cookie_delete_equivalent_ = base::LinearHistogram::FactoryGet( |
| 1784 | "Cookie.CookieDeleteEquivalent", 1, |
| 1785 | COOKIE_DELETE_EQUIVALENT_LAST_ENTRY - 1, |
| 1786 | COOKIE_DELETE_EQUIVALENT_LAST_ENTRY, |
| 1787 | base::Histogram::kUmaTargetedHistogramFlag); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1788 | |
| 1789 | // From UMA_HISTOGRAM_{CUSTOM_,}TIMES |
[email protected] | c7593fb2 | 2011-11-14 23:54:27 | [diff] [blame] | 1790 | histogram_time_blocked_on_load_ = base::Histogram::FactoryTimeGet( |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1791 | "Cookie.TimeBlockedOnLoad", base::TimeDelta::FromMilliseconds(1), |
| 1792 | base::TimeDelta::FromMinutes(1), 50, |
| 1793 | base::Histogram::kUmaTargetedHistogramFlag); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1794 | } |
| 1795 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 1796 | void CookieMonster::DoCookieCallback(base::OnceClosure callback) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1797 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 1798 | |
| 1799 | MarkCookieStoreAsInitialized(); |
| 1800 | FetchAllCookiesIfNecessary(); |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 1801 | seen_global_task_ = true; |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1802 | |
| 1803 | if (!finished_fetching_all_cookies_ && store_.get()) { |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 1804 | tasks_pending_.push_back(std::move(callback)); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1805 | return; |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1806 | } |
| 1807 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 1808 | std::move(callback).Run(); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1809 | } |
| 1810 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 1811 | void CookieMonster::DoCookieCallbackForURL(base::OnceClosure callback, |
| 1812 | const GURL& url) { |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 1813 | DoCookieCallbackForHostOrDomain(std::move(callback), url.host_piece()); |
| 1814 | } |
| 1815 | |
| 1816 | void CookieMonster::DoCookieCallbackForHostOrDomain( |
| 1817 | base::OnceClosure callback, |
| 1818 | base::StringPiece host_or_domain) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1819 | MarkCookieStoreAsInitialized(); |
erikchen | de4c39e | 2018-01-29 21:33:36 | [diff] [blame] | 1820 | FetchAllCookiesIfNecessary(); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1821 | |
| 1822 | // If cookies for the requested domain key (eTLD+1) have been loaded from DB |
| 1823 | // then run the task, otherwise load from DB. |
| 1824 | if (!finished_fetching_all_cookies_ && store_.get()) { |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 1825 | // If a global task has been previously seen, queue the task as a global |
| 1826 | // task. Note that the CookieMonster may be in the middle of executing |
| 1827 | // the global queue, |tasks_pending_| may be empty, which is why another |
| 1828 | // bool is needed. |
| 1829 | if (seen_global_task_) { |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 1830 | tasks_pending_.push_back(std::move(callback)); |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 1831 | return; |
| 1832 | } |
| 1833 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1834 | // Checks if the domain key has been loaded. |
Maks Orlovich | 323efaf | 2018-03-06 02:56:39 | [diff] [blame] | 1835 | std::string key = GetKey(host_or_domain); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1836 | if (keys_loaded_.find(key) == keys_loaded_.end()) { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 1837 | auto it = tasks_pending_for_key_.find(key); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1838 | if (it == tasks_pending_for_key_.end()) { |
| 1839 | store_->LoadCookiesForKey( |
Maks Orlovich | 108cb4c | 2019-03-26 20:24:57 | [diff] [blame] | 1840 | key, base::BindOnce(&CookieMonster::OnKeyLoaded, |
| 1841 | weak_ptr_factory_.GetWeakPtr(), key)); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1842 | it = tasks_pending_for_key_ |
Brett Wilson | c6a0c82 | 2017-09-12 00:04:29 | [diff] [blame] | 1843 | .insert(std::make_pair( |
| 1844 | key, base::circular_deque<base::OnceClosure>())) |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1845 | .first; |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1846 | } |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 1847 | it->second.push_back(std::move(callback)); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1848 | return; |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1849 | } |
| 1850 | } |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1851 | |
rdsmith | e5c701d | 2017-07-12 21:50:00 | [diff] [blame] | 1852 | std::move(callback).Run(); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1853 | } |
| 1854 | |
[email protected] | 6372531 | 2012-07-19 08:24:16 | [diff] [blame] | 1855 | } // namespace net |