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