[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 | |
| 47 | #include <algorithm> |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 48 | #include <functional> |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 49 | #include <memory> |
[email protected] | 0966648 | 2011-07-12 12:50:40 | [diff] [blame] | 50 | #include <set> |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 51 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 52 | #include "base/bind.h" |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 53 | #include "base/callback.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 54 | #include "base/location.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 55 | #include "base/logging.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 56 | #include "base/macros.h" |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 57 | #include "base/memory/ptr_util.h" |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 58 | #include "base/metrics/field_trial.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 59 | #include "base/metrics/histogram.h" |
pkasting | b60049a | 2015-02-07 03:25:25 | [diff] [blame] | 60 | #include "base/profiler/scoped_tracker.h" |
anujk.sharma | afc4517 | 2015-05-15 00:50:34 | [diff] [blame] | 61 | #include "base/single_thread_task_runner.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" |
[email protected] | be28b5f4 | 2012-07-20 11:31:25 | [diff] [blame] | 65 | #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
[email protected] | 4b35521 | 2013-06-11 10:35:19 | [diff] [blame] | 66 | #include "net/cookies/canonical_cookie.h" |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 67 | #include "net/cookies/cookie_util.h" |
[email protected] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 68 | #include "net/cookies/parsed_cookie.h" |
nharper | 2b0ad9a | 2017-05-22 18:33:45 | [diff] [blame] | 69 | #include "net/ssl/channel_id_service.h" |
mkwst | 8241a12 | 2015-10-20 07:15:10 | [diff] [blame] | 70 | #include "url/origin.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 71 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 72 | using base::Time; |
| 73 | using base::TimeDelta; |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 74 | using base::TimeTicks; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 75 | |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 76 | // In steady state, most cookie requests can be satisfied by the in memory |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 77 | // cookie monster store. If the cookie request cannot be satisfied by the in |
| 78 | // memory store, the relevant cookies must be fetched from the persistent |
| 79 | // store. The task is queued in CookieMonster::tasks_pending_ if it requires |
| 80 | // all cookies to be loaded from the backend, or tasks_pending_for_key_ if it |
| 81 | // only requires all cookies associated with an eTLD+1. |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 82 | // |
| 83 | // On the browser critical paths (e.g. for loading initial web pages in a |
| 84 | // session restore) it may take too long to wait for the full load. If a cookie |
| 85 | // request is for a specific URL, DoCookieTaskForURL is called, which triggers a |
| 86 | // priority load if the key is not loaded yet by calling PersistentCookieStore |
[email protected] | 0184df3 | 2013-05-14 00:53:55 | [diff] [blame] | 87 | // :: LoadCookiesForKey. The request is queued in |
| 88 | // CookieMonster::tasks_pending_for_key_ and executed upon receiving |
| 89 | // notification of key load completion via CookieMonster::OnKeyLoaded(). If |
| 90 | // multiple requests for the same eTLD+1 are received before key load |
| 91 | // completion, only the first request calls |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 92 | // PersistentCookieStore::LoadCookiesForKey, all subsequent requests are queued |
[email protected] | 0184df3 | 2013-05-14 00:53:55 | [diff] [blame] | 93 | // in CookieMonster::tasks_pending_for_key_ and executed upon receiving |
| 94 | // notification of key load completion triggered by the first request for the |
| 95 | // same eTLD+1. |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 96 | |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 97 | static const int kMinutesInTenYears = 10 * 365 * 24 * 60; |
| 98 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 99 | namespace { |
| 100 | |
| 101 | const char kFetchWhenNecessaryName[] = "FetchWhenNecessary"; |
| 102 | const char kAlwaysFetchName[] = "AlwaysFetch"; |
| 103 | const char kCookieMonsterFetchStrategyName[] = "CookieMonsterFetchStrategy"; |
| 104 | |
| 105 | } // namespace |
| 106 | |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 107 | namespace net { |
| 108 | |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 109 | // See comments at declaration of these variables in cookie_monster.h |
| 110 | // for details. |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 111 | const size_t CookieMonster::kDomainMaxCookies = 180; |
| 112 | const size_t CookieMonster::kDomainPurgeCookies = 30; |
| 113 | const size_t CookieMonster::kMaxCookies = 3300; |
| 114 | const size_t CookieMonster::kPurgeCookies = 300; |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 115 | |
mkwst | 8773435 | 2016-03-03 17:36:23 | [diff] [blame] | 116 | const size_t CookieMonster::kDomainCookiesQuotaLow = 30; |
| 117 | const size_t CookieMonster::kDomainCookiesQuotaMedium = 50; |
| 118 | const size_t CookieMonster::kDomainCookiesQuotaHigh = |
| 119 | kDomainMaxCookies - kDomainPurgeCookies - kDomainCookiesQuotaLow - |
| 120 | kDomainCookiesQuotaMedium; |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 121 | |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 122 | const int CookieMonster::kSafeFromGlobalPurgeDays = 30; |
[email protected] | 297a4ed0 | 2010-02-12 08:12:52 | [diff] [blame] | 123 | |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 124 | namespace { |
[email protected] | e32306c5 | 2008-11-06 16:59:05 | [diff] [blame] | 125 | |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 126 | bool ContainsControlCharacter(const std::string& s) { |
| 127 | for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) { |
| 128 | if ((*i >= 0) && (*i <= 31)) |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | return false; |
| 133 | } |
| 134 | |
[email protected] | 5b9bc35 | 2012-07-18 13:13:34 | [diff] [blame] | 135 | typedef std::vector<CanonicalCookie*> CanonicalCookieVector; |
[email protected] | 34a160d | 2011-05-12 22:12:49 | [diff] [blame] | 136 | |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 137 | // Default minimum delay after updating a cookie's LastAccessDate before we |
| 138 | // will update it again. |
[email protected] | 297a4ed0 | 2010-02-12 08:12:52 | [diff] [blame] | 139 | const int kDefaultAccessUpdateThresholdSeconds = 60; |
| 140 | |
| 141 | // Comparator to sort cookies from highest creation date to lowest |
| 142 | // creation date. |
| 143 | struct OrderByCreationTimeDesc { |
| 144 | bool operator()(const CookieMonster::CookieMap::iterator& a, |
| 145 | const CookieMonster::CookieMap::iterator& b) const { |
| 146 | return a->second->CreationDate() > b->second->CreationDate(); |
| 147 | } |
| 148 | }; |
| 149 | |
[email protected] | 4d3ce78 | 2010-10-29 18:31:28 | [diff] [blame] | 150 | // Constants for use in VLOG |
| 151 | const int kVlogPerCookieMonster = 1; |
[email protected] | 4d3ce78 | 2010-10-29 18:31:28 | [diff] [blame] | 152 | const int kVlogGarbageCollection = 5; |
| 153 | const int kVlogSetCookies = 7; |
| 154 | const int kVlogGetCookies = 9; |
| 155 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 156 | // Mozilla sorts on the path length (longest first), and then it |
| 157 | // sorts by creation time (oldest first). |
| 158 | // The RFC says the sort order for the domain attribute is undefined. |
[email protected] | 5b9bc35 | 2012-07-18 13:13:34 | [diff] [blame] | 159 | bool CookieSorter(CanonicalCookie* cc1, CanonicalCookie* cc2) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 160 | if (cc1->Path().length() == cc2->Path().length()) |
| 161 | return cc1->CreationDate() < cc2->CreationDate(); |
| 162 | return cc1->Path().length() > cc2->Path().length(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 163 | } |
| 164 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 165 | bool LRACookieSorter(const CookieMonster::CookieMap::iterator& it1, |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 166 | const CookieMonster::CookieMap::iterator& it2) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 167 | if (it1->second->LastAccessDate() != it2->second->LastAccessDate()) |
| 168 | return it1->second->LastAccessDate() < it2->second->LastAccessDate(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 169 | |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 170 | // Ensure stability for == last access times by falling back to creation. |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 171 | return it1->second->CreationDate() < it2->second->CreationDate(); |
[email protected] | 297a4ed0 | 2010-02-12 08:12:52 | [diff] [blame] | 172 | } |
| 173 | |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 174 | // Compare cookies using name, domain and path, so that "equivalent" cookies |
| 175 | // (per RFC 2965) are equal to each other. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 176 | bool PartialDiffCookieSorter(const CanonicalCookie& a, |
| 177 | const CanonicalCookie& b) { |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 178 | return a.PartialCompare(b); |
| 179 | } |
| 180 | |
| 181 | // This is a stricter ordering than PartialDiffCookieOrdering, where all fields |
| 182 | // are used. |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 183 | bool FullDiffCookieSorter(const CanonicalCookie& a, const CanonicalCookie& b) { |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 184 | return a.FullCompare(b); |
| 185 | } |
| 186 | |
[email protected] | 297a4ed0 | 2010-02-12 08:12:52 | [diff] [blame] | 187 | // Our strategy to find duplicates is: |
| 188 | // (1) Build a map from (cookiename, cookiepath) to |
| 189 | // {list of cookies with this signature, sorted by creation time}. |
| 190 | // (2) For each list with more than 1 entry, keep the cookie having the |
| 191 | // most recent creation time, and delete the others. |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 192 | // |
[email protected] | 1655ba34 | 2010-07-14 18:17:42 | [diff] [blame] | 193 | // Two cookies are considered equivalent if they have the same domain, |
| 194 | // name, and path. |
| 195 | struct CookieSignature { |
| 196 | public: |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 197 | CookieSignature(const std::string& name, |
| 198 | const std::string& domain, |
[email protected] | 1655ba34 | 2010-07-14 18:17:42 | [diff] [blame] | 199 | const std::string& path) |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 200 | : name(name), domain(domain), path(path) {} |
[email protected] | 1655ba34 | 2010-07-14 18:17:42 | [diff] [blame] | 201 | |
| 202 | // To be a key for a map this class needs to be assignable, copyable, |
| 203 | // and have an operator<. The default assignment operator |
| 204 | // and copy constructor are exactly what we want. |
| 205 | |
| 206 | bool operator<(const CookieSignature& cs) const { |
| 207 | // Name compare dominates, then domain, then path. |
| 208 | int diff = name.compare(cs.name); |
| 209 | if (diff != 0) |
| 210 | return diff < 0; |
| 211 | |
| 212 | diff = domain.compare(cs.domain); |
| 213 | if (diff != 0) |
| 214 | return diff < 0; |
| 215 | |
| 216 | return path.compare(cs.path) < 0; |
| 217 | } |
| 218 | |
| 219 | std::string name; |
| 220 | std::string domain; |
| 221 | std::string path; |
| 222 | }; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 223 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 224 | // For a CookieItVector iterator range [|it_begin|, |it_end|), |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame^] | 225 | // sorts the first |num_sort| elements by LastAccessDate(). |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 226 | void SortLeastRecentlyAccessed(CookieMonster::CookieItVector::iterator it_begin, |
| 227 | CookieMonster::CookieItVector::iterator it_end, |
| 228 | size_t num_sort) { |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame^] | 229 | DCHECK_LE(static_cast<int>(num_sort), it_end - it_begin); |
| 230 | std::partial_sort(it_begin, it_begin + num_sort, it_end, LRACookieSorter); |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 231 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 232 | |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 233 | // Given a single cookie vector |cookie_its|, pushs all of the secure cookies in |
| 234 | // |cookie_its| into |secure_cookie_its| and all of the non-secure cookies into |
| 235 | // |non_secure_cookie_its|. Both |secure_cookie_its| and |non_secure_cookie_its| |
| 236 | // must be non-NULL. |
| 237 | void SplitCookieVectorIntoSecureAndNonSecure( |
| 238 | const CookieMonster::CookieItVector& cookie_its, |
| 239 | CookieMonster::CookieItVector* secure_cookie_its, |
| 240 | CookieMonster::CookieItVector* non_secure_cookie_its) { |
| 241 | DCHECK(secure_cookie_its && non_secure_cookie_its); |
| 242 | for (const auto& curit : cookie_its) { |
| 243 | if (curit->second->IsSecure()) |
| 244 | secure_cookie_its->push_back(curit); |
| 245 | else |
| 246 | non_secure_cookie_its->push_back(curit); |
| 247 | } |
| 248 | } |
| 249 | |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 250 | bool LowerBoundAccessDateComparator(const CookieMonster::CookieMap::iterator it, |
| 251 | const Time& access_date) { |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 252 | return it->second->LastAccessDate() < access_date; |
| 253 | } |
| 254 | |
| 255 | // For a CookieItVector iterator range [|it_begin|, |it_end|) |
| 256 | // from a CookieItVector sorted by LastAccessDate(), returns the |
| 257 | // first iterator with access date >= |access_date|, or cookie_its_end if this |
| 258 | // holds for all. |
| 259 | CookieMonster::CookieItVector::iterator LowerBoundAccessDate( |
| 260 | const CookieMonster::CookieItVector::iterator its_begin, |
| 261 | const CookieMonster::CookieItVector::iterator its_end, |
| 262 | const Time& access_date) { |
| 263 | return std::lower_bound(its_begin, its_end, access_date, |
| 264 | LowerBoundAccessDateComparator); |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 265 | } |
| 266 | |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 267 | // Mapping between DeletionCause and CookieStore::ChangeCause; the |
[email protected] | 7c4b66b | 2014-01-04 12:28:13 | [diff] [blame] | 268 | // mapping also provides a boolean that specifies whether or not an |
| 269 | // OnCookieChanged notification ought to be generated. |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 270 | typedef struct ChangeCausePair_struct { |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 271 | CookieStore::ChangeCause cause; |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 272 | bool notify; |
| 273 | } ChangeCausePair; |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 274 | const ChangeCausePair kChangeCauseMapping[] = { |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 275 | // DELETE_COOKIE_EXPLICIT |
nharper | 6890336 | 2017-01-20 04:07:14 | [diff] [blame] | 276 | {CookieStore::ChangeCause::EXPLICIT, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 277 | // DELETE_COOKIE_OVERWRITE |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 278 | {CookieStore::ChangeCause::OVERWRITE, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 279 | // DELETE_COOKIE_EXPIRED |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 280 | {CookieStore::ChangeCause::EXPIRED, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 281 | // DELETE_COOKIE_EVICTED |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 282 | {CookieStore::ChangeCause::EVICTED, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 283 | // DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE |
nharper | 6890336 | 2017-01-20 04:07:14 | [diff] [blame] | 284 | {CookieStore::ChangeCause::EXPLICIT, false}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 285 | // DELETE_COOKIE_DONT_RECORD |
nharper | 6890336 | 2017-01-20 04:07:14 | [diff] [blame] | 286 | {CookieStore::ChangeCause::EXPLICIT, false}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 287 | // DELETE_COOKIE_EVICTED_DOMAIN |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 288 | {CookieStore::ChangeCause::EVICTED, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 289 | // DELETE_COOKIE_EVICTED_GLOBAL |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 290 | {CookieStore::ChangeCause::EVICTED, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 291 | // DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 292 | {CookieStore::ChangeCause::EVICTED, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 293 | // DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 294 | {CookieStore::ChangeCause::EVICTED, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 295 | // DELETE_COOKIE_EXPIRED_OVERWRITE |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 296 | {CookieStore::ChangeCause::EXPIRED_OVERWRITE, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 297 | // DELETE_COOKIE_CONTROL_CHAR |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 298 | {CookieStore::ChangeCause::EVICTED, true}, |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 299 | // DELETE_COOKIE_NON_SECURE |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 300 | {CookieStore::ChangeCause::EVICTED, true}, |
nharper | 6890336 | 2017-01-20 04:07:14 | [diff] [blame] | 301 | // DELETE_COOKIE_CREATED_BETWEEN |
| 302 | {CookieStore::ChangeCause::EXPLICIT_DELETE_BETWEEN, true}, |
| 303 | // DELETE_COOKIE_CREATED_BETWEEN_WITH_PREDICATE |
| 304 | {CookieStore::ChangeCause::EXPLICIT_DELETE_PREDICATE, true}, |
| 305 | // DELETE_COOKIE_SINGLE |
| 306 | {CookieStore::ChangeCause::EXPLICIT_DELETE_SINGLE, true}, |
| 307 | // DELETE_COOKIE_CANONICAL |
| 308 | {CookieStore::ChangeCause::EXPLICIT_DELETE_CANONICAL, true}, |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 309 | // DELETE_COOKIE_LAST_ENTRY |
nharper | 6890336 | 2017-01-20 04:07:14 | [diff] [blame] | 310 | {CookieStore::ChangeCause::EXPLICIT, false}}; |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 311 | |
ellyjones | 399e35a2 | 2014-10-27 11:09:56 | [diff] [blame] | 312 | void RunAsync(scoped_refptr<base::TaskRunner> proxy, |
msarda | 0aad8f0 | 2014-10-30 09:22:39 | [diff] [blame] | 313 | const CookieStore::CookieChangedCallback& callback, |
| 314 | const CanonicalCookie& cookie, |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 315 | CookieStore::ChangeCause cause) { |
| 316 | proxy->PostTask(FROM_HERE, base::Bind(callback, cookie, cause)); |
ellyjones | 399e35a2 | 2014-10-27 11:09:56 | [diff] [blame] | 317 | } |
| 318 | |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 319 | bool IsCookieEligibleForEviction(CookiePriority current_priority_level, |
| 320 | bool protect_secure_cookies, |
| 321 | const CanonicalCookie* cookie) { |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 322 | if (cookie->Priority() == current_priority_level && protect_secure_cookies) |
| 323 | return !cookie->IsSecure(); |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 324 | |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 325 | return cookie->Priority() == current_priority_level; |
| 326 | } |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 327 | |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 328 | size_t CountCookiesForPossibleDeletion( |
| 329 | CookiePriority priority, |
| 330 | const CookieMonster::CookieItVector* cookies, |
| 331 | bool protect_secure_cookies) { |
| 332 | size_t cookies_count = 0U; |
| 333 | for (const auto& cookie : *cookies) { |
| 334 | if (cookie->second->Priority() == priority) { |
| 335 | if (!protect_secure_cookies || cookie->second->IsSecure()) |
| 336 | cookies_count++; |
| 337 | } |
| 338 | } |
| 339 | return cookies_count; |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 340 | } |
| 341 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 342 | } // namespace |
| 343 | |
[email protected] | 7c4b66b | 2014-01-04 12:28:13 | [diff] [blame] | 344 | CookieMonster::CookieMonster(PersistentCookieStore* store, |
| 345 | CookieMonsterDelegate* delegate) |
shess | f0bc118 | 2016-05-19 04:35:58 | [diff] [blame] | 346 | : CookieMonster( |
| 347 | store, |
| 348 | delegate, |
nharper | 2b0ad9a | 2017-05-22 18:33:45 | [diff] [blame] | 349 | nullptr, |
shess | f0bc118 | 2016-05-19 04:35:58 | [diff] [blame] | 350 | base::TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)) {} |
[email protected] | 2d0f89a | 2010-12-06 12:02:23 | [diff] [blame] | 351 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 352 | CookieMonster::CookieMonster(PersistentCookieStore* store, |
[email protected] | 7c4b66b | 2014-01-04 12:28:13 | [diff] [blame] | 353 | CookieMonsterDelegate* delegate, |
nharper | 2b0ad9a | 2017-05-22 18:33:45 | [diff] [blame] | 354 | ChannelIDService* channel_id_service) |
| 355 | : CookieMonster( |
| 356 | store, |
| 357 | delegate, |
| 358 | channel_id_service, |
| 359 | base::TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)) {} |
| 360 | |
| 361 | CookieMonster::CookieMonster(PersistentCookieStore* store, |
| 362 | CookieMonsterDelegate* delegate, |
| 363 | base::TimeDelta last_access_threshold) |
| 364 | : CookieMonster(store, delegate, nullptr, last_access_threshold) {} |
| 365 | |
| 366 | CookieMonster::CookieMonster(PersistentCookieStore* store, |
| 367 | CookieMonsterDelegate* delegate, |
| 368 | ChannelIDService* channel_id_service, |
shess | f0bc118 | 2016-05-19 04:35:58 | [diff] [blame] | 369 | base::TimeDelta last_access_threshold) |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 370 | : initialized_(false), |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 371 | started_fetching_all_cookies_(false), |
| 372 | finished_fetching_all_cookies_(false), |
| 373 | fetch_strategy_(kUnknownFetch), |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 374 | seen_global_task_(false), |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 375 | store_(store), |
shess | f0bc118 | 2016-05-19 04:35:58 | [diff] [blame] | 376 | last_access_threshold_(last_access_threshold), |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 377 | delegate_(delegate), |
nharper | 2b0ad9a | 2017-05-22 18:33:45 | [diff] [blame] | 378 | channel_id_service_(channel_id_service), |
[email protected] | 8238866 | 2011-03-10 21:04:06 | [diff] [blame] | 379 | last_statistic_record_time_(base::Time::Now()), |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 380 | persist_session_cookies_(false), |
| 381 | weak_ptr_factory_(this) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 382 | InitializeHistograms(); |
mmenke | 18dd8ba | 2016-02-01 18:42:10 | [diff] [blame] | 383 | cookieable_schemes_.insert( |
| 384 | cookieable_schemes_.begin(), kDefaultCookieableSchemes, |
| 385 | kDefaultCookieableSchemes + kDefaultCookieableSchemesCount); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 386 | } |
| 387 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 388 | // Task classes for queueing the coming request. |
| 389 | |
| 390 | class CookieMonster::CookieMonsterTask |
| 391 | : public base::RefCountedThreadSafe<CookieMonsterTask> { |
| 392 | public: |
| 393 | // Runs the task and invokes the client callback on the thread that |
| 394 | // originally constructed the task. |
| 395 | virtual void Run() = 0; |
| 396 | |
| 397 | protected: |
| 398 | explicit CookieMonsterTask(CookieMonster* cookie_monster); |
| 399 | virtual ~CookieMonsterTask(); |
| 400 | |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 401 | CookieMonster* cookie_monster() { return cookie_monster_; } |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 402 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 403 | private: |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 404 | friend class base::RefCountedThreadSafe<CookieMonsterTask>; |
| 405 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 406 | CookieMonster* cookie_monster_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 407 | |
| 408 | DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask); |
| 409 | }; |
| 410 | |
| 411 | CookieMonster::CookieMonsterTask::CookieMonsterTask( |
| 412 | CookieMonster* cookie_monster) |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 413 | : cookie_monster_(cookie_monster) {} |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 414 | |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 415 | CookieMonster::CookieMonsterTask::~CookieMonsterTask() { |
| 416 | } |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 417 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 418 | // Task class for SetCookieWithDetails call. |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 419 | class CookieMonster::SetCookieWithDetailsTask : public CookieMonsterTask { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 420 | public: |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 421 | SetCookieWithDetailsTask(CookieMonster* cookie_monster, |
| 422 | const GURL& url, |
| 423 | const std::string& name, |
| 424 | const std::string& value, |
| 425 | const std::string& domain, |
| 426 | const std::string& path, |
mmenke | fdd4fc7 | 2016-02-05 20:53:24 | [diff] [blame] | 427 | base::Time creation_time, |
| 428 | base::Time expiration_time, |
| 429 | base::Time last_access_time, |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 430 | bool secure, |
| 431 | bool http_only, |
mkwst | e1a2958 | 2016-03-15 10:07:52 | [diff] [blame] | 432 | CookieSameSite same_site, |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 433 | CookiePriority priority, |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 434 | const SetCookiesCallback& callback) |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 435 | : CookieMonsterTask(cookie_monster), |
| 436 | url_(url), |
| 437 | name_(name), |
| 438 | value_(value), |
| 439 | domain_(domain), |
| 440 | path_(path), |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 441 | creation_time_(creation_time), |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 442 | expiration_time_(expiration_time), |
mmenke | fdd4fc7 | 2016-02-05 20:53:24 | [diff] [blame] | 443 | last_access_time_(last_access_time), |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 444 | secure_(secure), |
| 445 | http_only_(http_only), |
mkwst | 4654941 | 2016-02-01 10:05:37 | [diff] [blame] | 446 | same_site_(same_site), |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 447 | priority_(priority), |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 448 | callback_(callback) {} |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 449 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 450 | // CookieMonsterTask: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 451 | void Run() override; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 452 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 453 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 454 | ~SetCookieWithDetailsTask() override {} |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 455 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 456 | private: |
| 457 | GURL url_; |
| 458 | std::string name_; |
| 459 | std::string value_; |
| 460 | std::string domain_; |
| 461 | std::string path_; |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 462 | base::Time creation_time_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 463 | base::Time expiration_time_; |
mmenke | fdd4fc7 | 2016-02-05 20:53:24 | [diff] [blame] | 464 | base::Time last_access_time_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 465 | bool secure_; |
| 466 | bool http_only_; |
mkwst | e1a2958 | 2016-03-15 10:07:52 | [diff] [blame] | 467 | CookieSameSite same_site_; |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 468 | CookiePriority priority_; |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 469 | SetCookiesCallback callback_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 470 | |
| 471 | DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask); |
| 472 | }; |
| 473 | |
| 474 | void CookieMonster::SetCookieWithDetailsTask::Run() { |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 475 | bool success = this->cookie_monster()->SetCookieWithDetails( |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 476 | url_, name_, value_, domain_, path_, creation_time_, expiration_time_, |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 477 | last_access_time_, secure_, http_only_, same_site_, priority_); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 478 | if (!callback_.is_null()) |
| 479 | callback_.Run(success); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | // Task class for GetAllCookies call. |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 483 | class CookieMonster::GetAllCookiesTask : public CookieMonsterTask { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 484 | public: |
| 485 | GetAllCookiesTask(CookieMonster* cookie_monster, |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 486 | const GetCookieListCallback& callback) |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 487 | : CookieMonsterTask(cookie_monster), callback_(callback) {} |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 488 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 489 | // CookieMonsterTask |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 490 | void Run() override; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 491 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 492 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 493 | ~GetAllCookiesTask() override {} |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 494 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 495 | private: |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 496 | GetCookieListCallback callback_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 497 | |
| 498 | DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask); |
| 499 | }; |
| 500 | |
| 501 | void CookieMonster::GetAllCookiesTask::Run() { |
| 502 | if (!callback_.is_null()) { |
| 503 | CookieList cookies = this->cookie_monster()->GetAllCookies(); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 504 | callback_.Run(cookies); |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 505 | } |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 506 | } |
| 507 | |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 508 | // Task class for GetCookieListWithOptionsAsync call. |
| 509 | class CookieMonster::GetCookieListWithOptionsTask : public CookieMonsterTask { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 510 | public: |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 511 | GetCookieListWithOptionsTask(CookieMonster* cookie_monster, |
| 512 | const GURL& url, |
| 513 | const CookieOptions& options, |
| 514 | const GetCookieListCallback& callback) |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 515 | : CookieMonsterTask(cookie_monster), |
| 516 | url_(url), |
| 517 | options_(options), |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 518 | callback_(callback) {} |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 519 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 520 | // CookieMonsterTask: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 521 | void Run() override; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 522 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 523 | protected: |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 524 | ~GetCookieListWithOptionsTask() override {} |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 525 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 526 | private: |
| 527 | GURL url_; |
| 528 | CookieOptions options_; |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 529 | GetCookieListCallback callback_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 530 | |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 531 | DISALLOW_COPY_AND_ASSIGN(GetCookieListWithOptionsTask); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 532 | }; |
| 533 | |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 534 | void CookieMonster::GetCookieListWithOptionsTask::Run() { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 535 | if (!callback_.is_null()) { |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 536 | CookieList cookies = |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 537 | this->cookie_monster()->GetCookieListWithOptions(url_, options_); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 538 | callback_.Run(cookies); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 542 | template <typename Result> |
| 543 | struct CallbackType { |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 544 | typedef base::Callback<void(Result)> Type; |
| 545 | }; |
| 546 | |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 547 | template <> |
| 548 | struct CallbackType<void> { |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 549 | typedef base::Closure Type; |
| 550 | }; |
| 551 | |
| 552 | // Base task class for Delete*Task. |
| 553 | template <typename Result> |
| 554 | class CookieMonster::DeleteTask : public CookieMonsterTask { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 555 | public: |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 556 | DeleteTask(CookieMonster* cookie_monster, |
| 557 | const typename CallbackType<Result>::Type& callback) |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 558 | : CookieMonsterTask(cookie_monster), callback_(callback) {} |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 559 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 560 | // CookieMonsterTask: |
nick | d3f30d02 | 2015-04-23 10:18:37 | [diff] [blame] | 561 | void Run() override; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 562 | |
dmichael | d6e570d | 2014-12-18 22:30:57 | [diff] [blame] | 563 | protected: |
| 564 | ~DeleteTask() override; |
| 565 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 566 | private: |
| 567 | // Runs the delete task and returns a result. |
| 568 | virtual Result RunDeleteTask() = 0; |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 569 | // Runs the delete task and then returns a callback to be called after |
| 570 | // flushing the persistent store. |
| 571 | // TODO(mmenke): This seems like a pretty ugly and needlessly confusing API. |
| 572 | // Simplify it? |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 573 | base::Closure RunDeleteTaskAndBindCallback(); |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 574 | |
| 575 | typename CallbackType<Result>::Type callback_; |
| 576 | |
| 577 | DISALLOW_COPY_AND_ASSIGN(DeleteTask); |
| 578 | }; |
| 579 | |
| 580 | template <typename Result> |
dmichael | d6e570d | 2014-12-18 22:30:57 | [diff] [blame] | 581 | CookieMonster::DeleteTask<Result>::~DeleteTask() { |
| 582 | } |
| 583 | |
| 584 | template <typename Result> |
| 585 | base::Closure |
| 586 | CookieMonster::DeleteTask<Result>::RunDeleteTaskAndBindCallback() { |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 587 | Result result = RunDeleteTask(); |
| 588 | if (callback_.is_null()) |
| 589 | return base::Closure(); |
| 590 | return base::Bind(callback_, result); |
| 591 | } |
| 592 | |
| 593 | template <> |
| 594 | base::Closure CookieMonster::DeleteTask<void>::RunDeleteTaskAndBindCallback() { |
| 595 | RunDeleteTask(); |
| 596 | return callback_; |
| 597 | } |
| 598 | |
| 599 | template <typename Result> |
| 600 | void CookieMonster::DeleteTask<Result>::Run() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 601 | base::Closure callback = RunDeleteTaskAndBindCallback(); |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 602 | if (!callback.is_null()) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 603 | callback = base::Bind( |
| 604 | &CookieMonster::RunCallback, |
| 605 | this->cookie_monster()->weak_ptr_factory_.GetWeakPtr(), callback); |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 606 | } |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 607 | this->cookie_monster()->FlushStore(callback); |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 608 | } |
| 609 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 610 | // Task class for DeleteAllCreatedBetween call. |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 611 | class CookieMonster::DeleteAllCreatedBetweenTask : public DeleteTask<int> { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 612 | public: |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 613 | DeleteAllCreatedBetweenTask(CookieMonster* cookie_monster, |
| 614 | const Time& delete_begin, |
| 615 | const Time& delete_end, |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 616 | const DeleteCallback& callback) |
[email protected] | 151132f | 2013-11-18 21:37:00 | [diff] [blame] | 617 | : DeleteTask<int>(cookie_monster, callback), |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 618 | delete_begin_(delete_begin), |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 619 | delete_end_(delete_end) {} |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 620 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 621 | // DeleteTask: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 622 | int RunDeleteTask() override; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 623 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 624 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 625 | ~DeleteAllCreatedBetweenTask() override {} |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 626 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 627 | private: |
| 628 | Time delete_begin_; |
| 629 | Time delete_end_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 630 | |
| 631 | DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask); |
| 632 | }; |
| 633 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 634 | int CookieMonster::DeleteAllCreatedBetweenTask::RunDeleteTask() { |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 635 | return this->cookie_monster()->DeleteAllCreatedBetween(delete_begin_, |
| 636 | delete_end_); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 637 | } |
| 638 | |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 639 | // Task class for DeleteAllCreatedBetweenWithPredicate call. |
| 640 | class CookieMonster::DeleteAllCreatedBetweenWithPredicateTask |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 641 | : public DeleteTask<int> { |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 642 | public: |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 643 | DeleteAllCreatedBetweenWithPredicateTask( |
| 644 | CookieMonster* cookie_monster, |
| 645 | Time delete_begin, |
| 646 | Time delete_end, |
| 647 | base::Callback<bool(const CanonicalCookie&)> predicate, |
| 648 | const DeleteCallback& callback) |
[email protected] | 151132f | 2013-11-18 21:37:00 | [diff] [blame] | 649 | : DeleteTask<int>(cookie_monster, callback), |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 650 | delete_begin_(delete_begin), |
| 651 | delete_end_(delete_end), |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 652 | predicate_(predicate) {} |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 653 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 654 | // DeleteTask: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 655 | int RunDeleteTask() override; |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 656 | |
| 657 | protected: |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 658 | ~DeleteAllCreatedBetweenWithPredicateTask() override {} |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 659 | |
| 660 | private: |
| 661 | Time delete_begin_; |
| 662 | Time delete_end_; |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 663 | base::Callback<bool(const CanonicalCookie&)> predicate_; |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 664 | |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 665 | DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenWithPredicateTask); |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 666 | }; |
| 667 | |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 668 | int CookieMonster::DeleteAllCreatedBetweenWithPredicateTask::RunDeleteTask() { |
| 669 | return this->cookie_monster()->DeleteAllCreatedBetweenWithPredicate( |
| 670 | delete_begin_, delete_end_, predicate_); |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 671 | } |
| 672 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 673 | // Task class for DeleteCanonicalCookie call. |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 674 | class CookieMonster::DeleteCanonicalCookieTask : public DeleteTask<int> { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 675 | public: |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 676 | DeleteCanonicalCookieTask(CookieMonster* cookie_monster, |
| 677 | const CanonicalCookie& cookie, |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 678 | const DeleteCallback& callback) |
| 679 | : DeleteTask<int>(cookie_monster, callback), cookie_(cookie) {} |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 680 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 681 | // DeleteTask: |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 682 | int RunDeleteTask() override; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 683 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 684 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 685 | ~DeleteCanonicalCookieTask() override {} |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 686 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 687 | private: |
[email protected] | 5b9bc35 | 2012-07-18 13:13:34 | [diff] [blame] | 688 | CanonicalCookie cookie_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 689 | |
| 690 | DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask); |
| 691 | }; |
| 692 | |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 693 | int CookieMonster::DeleteCanonicalCookieTask::RunDeleteTask() { |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 694 | return this->cookie_monster()->DeleteCanonicalCookie(cookie_); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | // Task class for SetCookieWithOptions call. |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 698 | class CookieMonster::SetCookieWithOptionsTask : public CookieMonsterTask { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 699 | public: |
| 700 | SetCookieWithOptionsTask(CookieMonster* cookie_monster, |
| 701 | const GURL& url, |
| 702 | const std::string& cookie_line, |
| 703 | const CookieOptions& options, |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 704 | const SetCookiesCallback& callback) |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 705 | : CookieMonsterTask(cookie_monster), |
| 706 | url_(url), |
| 707 | cookie_line_(cookie_line), |
| 708 | options_(options), |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 709 | callback_(callback) {} |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 710 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 711 | // CookieMonsterTask: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 712 | void Run() override; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 713 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 714 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 715 | ~SetCookieWithOptionsTask() override {} |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 716 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 717 | private: |
| 718 | GURL url_; |
| 719 | std::string cookie_line_; |
| 720 | CookieOptions options_; |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 721 | SetCookiesCallback callback_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 722 | |
| 723 | DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask); |
| 724 | }; |
| 725 | |
| 726 | void CookieMonster::SetCookieWithOptionsTask::Run() { |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 727 | bool result = this->cookie_monster()->SetCookieWithOptions(url_, cookie_line_, |
| 728 | options_); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 729 | if (!callback_.is_null()) |
| 730 | callback_.Run(result); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 731 | } |
| 732 | |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 733 | // Task class for SetAllCookies call. |
| 734 | class CookieMonster::SetAllCookiesTask : public CookieMonsterTask { |
| 735 | public: |
| 736 | SetAllCookiesTask(CookieMonster* cookie_monster, |
| 737 | const CookieList& list, |
| 738 | const SetCookiesCallback& callback) |
| 739 | : CookieMonsterTask(cookie_monster), list_(list), callback_(callback) {} |
| 740 | |
| 741 | // CookieMonsterTask: |
| 742 | void Run() override; |
| 743 | |
| 744 | protected: |
| 745 | ~SetAllCookiesTask() override {} |
| 746 | |
| 747 | private: |
| 748 | CookieList list_; |
| 749 | SetCookiesCallback callback_; |
| 750 | |
| 751 | DISALLOW_COPY_AND_ASSIGN(SetAllCookiesTask); |
| 752 | }; |
| 753 | |
| 754 | void CookieMonster::SetAllCookiesTask::Run() { |
| 755 | CookieList positive_diff; |
| 756 | CookieList negative_diff; |
| 757 | CookieList old_cookies = this->cookie_monster()->GetAllCookies(); |
| 758 | this->cookie_monster()->ComputeCookieDiff(&old_cookies, &list_, |
| 759 | &positive_diff, &negative_diff); |
| 760 | |
| 761 | for (CookieList::const_iterator it = negative_diff.begin(); |
| 762 | it != negative_diff.end(); ++it) { |
| 763 | this->cookie_monster()->DeleteCanonicalCookie(*it); |
| 764 | } |
| 765 | |
| 766 | bool result = true; |
| 767 | if (positive_diff.size() > 0) |
| 768 | result = this->cookie_monster()->SetCanonicalCookies(list_); |
| 769 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 770 | if (!callback_.is_null()) |
| 771 | callback_.Run(result); |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 772 | } |
| 773 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 774 | // Task class for GetCookiesWithOptions call. |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 775 | class CookieMonster::GetCookiesWithOptionsTask : public CookieMonsterTask { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 776 | public: |
| 777 | GetCookiesWithOptionsTask(CookieMonster* cookie_monster, |
[email protected] | 0298caf8 | 2011-12-20 23:15:46 | [diff] [blame] | 778 | const GURL& url, |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 779 | const CookieOptions& options, |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 780 | const GetCookiesCallback& callback) |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 781 | : CookieMonsterTask(cookie_monster), |
| 782 | url_(url), |
| 783 | options_(options), |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 784 | callback_(callback) {} |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 785 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 786 | // CookieMonsterTask: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 787 | void Run() override; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 788 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 789 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 790 | ~GetCookiesWithOptionsTask() override {} |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 791 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 792 | private: |
| 793 | GURL url_; |
| 794 | CookieOptions options_; |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 795 | GetCookiesCallback callback_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 796 | |
| 797 | DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask); |
| 798 | }; |
| 799 | |
| 800 | void CookieMonster::GetCookiesWithOptionsTask::Run() { |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 801 | std::string cookie = |
| 802 | this->cookie_monster()->GetCookiesWithOptions(url_, options_); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 803 | if (!callback_.is_null()) |
| 804 | callback_.Run(cookie); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 805 | } |
| 806 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 807 | // Task class for DeleteCookie call. |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 808 | class CookieMonster::DeleteCookieTask : public DeleteTask<void> { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 809 | public: |
| 810 | DeleteCookieTask(CookieMonster* cookie_monster, |
[email protected] | 0298caf8 | 2011-12-20 23:15:46 | [diff] [blame] | 811 | const GURL& url, |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 812 | const std::string& cookie_name, |
| 813 | const base::Closure& callback) |
[email protected] | 151132f | 2013-11-18 21:37:00 | [diff] [blame] | 814 | : DeleteTask<void>(cookie_monster, callback), |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 815 | url_(url), |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 816 | cookie_name_(cookie_name) {} |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 817 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 818 | // DeleteTask: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 819 | void RunDeleteTask() override; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 820 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 821 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 822 | ~DeleteCookieTask() override {} |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 823 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 824 | private: |
| 825 | GURL url_; |
| 826 | std::string cookie_name_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 827 | |
| 828 | DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask); |
| 829 | }; |
| 830 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 831 | void CookieMonster::DeleteCookieTask::RunDeleteTask() { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 832 | this->cookie_monster()->DeleteCookie(url_, cookie_name_); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 833 | } |
| 834 | |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 835 | // Task class for DeleteSessionCookies call. |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 836 | class CookieMonster::DeleteSessionCookiesTask : public DeleteTask<int> { |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 837 | public: |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 838 | DeleteSessionCookiesTask(CookieMonster* cookie_monster, |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 839 | const DeleteCallback& callback) |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 840 | : DeleteTask<int>(cookie_monster, callback) {} |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 841 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 842 | // DeleteTask: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 843 | int RunDeleteTask() override; |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 844 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 845 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 846 | ~DeleteSessionCookiesTask() override {} |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 847 | |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 848 | private: |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 849 | DISALLOW_COPY_AND_ASSIGN(DeleteSessionCookiesTask); |
| 850 | }; |
| 851 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 852 | int CookieMonster::DeleteSessionCookiesTask::RunDeleteTask() { |
| 853 | return this->cookie_monster()->DeleteSessionCookies(); |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 854 | } |
| 855 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 856 | // Asynchronous CookieMonster API |
| 857 | |
| 858 | void CookieMonster::SetCookieWithDetailsAsync( |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 859 | const GURL& url, |
| 860 | const std::string& name, |
| 861 | const std::string& value, |
| 862 | const std::string& domain, |
| 863 | const std::string& path, |
mmenke | fdd4fc7 | 2016-02-05 20:53:24 | [diff] [blame] | 864 | Time creation_time, |
| 865 | Time expiration_time, |
| 866 | Time last_access_time, |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 867 | bool secure, |
| 868 | bool http_only, |
mkwst | e1a2958 | 2016-03-15 10:07:52 | [diff] [blame] | 869 | CookieSameSite same_site, |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 870 | CookiePriority priority, |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 871 | const SetCookiesCallback& callback) { |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 872 | scoped_refptr<SetCookieWithDetailsTask> task = new SetCookieWithDetailsTask( |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 873 | this, url, name, value, domain, path, creation_time, expiration_time, |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 874 | last_access_time, secure, http_only, same_site, priority, callback); |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 875 | DoCookieTaskForURL(task, url); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 876 | } |
| 877 | |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 878 | void CookieMonster::FlushStore(const base::Closure& callback) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 879 | DCHECK(thread_checker_.CalledOnValidThread()); |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 880 | |
nharper | 2b0ad9a | 2017-05-22 18:33:45 | [diff] [blame] | 881 | if (initialized_ && store_.get()) { |
| 882 | if (channel_id_service_) { |
| 883 | channel_id_service_->GetChannelIDStore()->Flush(); |
| 884 | } |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 885 | store_->Flush(callback); |
nharper | 2b0ad9a | 2017-05-22 18:33:45 | [diff] [blame] | 886 | } else if (!callback.is_null()) { |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 887 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); |
nharper | 2b0ad9a | 2017-05-22 18:33:45 | [diff] [blame] | 888 | } |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 889 | } |
| 890 | |
mmenke | ded79da | 2016-02-06 08:28:51 | [diff] [blame] | 891 | void CookieMonster::SetForceKeepSessionState() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 892 | DCHECK(thread_checker_.CalledOnValidThread()); |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 893 | |
mmenke | ded79da | 2016-02-06 08:28:51 | [diff] [blame] | 894 | if (store_) |
| 895 | store_->SetForceKeepSessionState(); |
| 896 | } |
| 897 | |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 898 | void CookieMonster::SetAllCookiesAsync(const CookieList& list, |
| 899 | const SetCookiesCallback& callback) { |
| 900 | scoped_refptr<SetAllCookiesTask> task = |
| 901 | new SetAllCookiesTask(this, list, callback); |
| 902 | DoCookieTask(task); |
| 903 | } |
| 904 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 905 | void CookieMonster::SetCookieWithOptionsAsync( |
| 906 | const GURL& url, |
| 907 | const std::string& cookie_line, |
| 908 | const CookieOptions& options, |
| 909 | const SetCookiesCallback& callback) { |
| 910 | scoped_refptr<SetCookieWithOptionsTask> task = |
| 911 | new SetCookieWithOptionsTask(this, url, cookie_line, options, callback); |
| 912 | |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 913 | DoCookieTaskForURL(task, url); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | void CookieMonster::GetCookiesWithOptionsAsync( |
| 917 | const GURL& url, |
| 918 | const CookieOptions& options, |
| 919 | const GetCookiesCallback& callback) { |
| 920 | scoped_refptr<GetCookiesWithOptionsTask> task = |
| 921 | new GetCookiesWithOptionsTask(this, url, options, callback); |
| 922 | |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 923 | DoCookieTaskForURL(task, url); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 924 | } |
| 925 | |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 926 | void CookieMonster::GetCookieListWithOptionsAsync( |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 927 | const GURL& url, |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 928 | const CookieOptions& options, |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 929 | const GetCookieListCallback& callback) { |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 930 | scoped_refptr<GetCookieListWithOptionsTask> task = |
| 931 | new GetCookieListWithOptionsTask(this, url, options, callback); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 932 | |
| 933 | DoCookieTaskForURL(task, url); |
| 934 | } |
| 935 | |
mmenke | 9fa44f2d | 2016-01-22 23:36:39 | [diff] [blame] | 936 | void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) { |
| 937 | scoped_refptr<GetAllCookiesTask> task = new GetAllCookiesTask(this, callback); |
| 938 | |
| 939 | DoCookieTask(task); |
| 940 | } |
| 941 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 942 | void CookieMonster::DeleteCookieAsync(const GURL& url, |
| 943 | const std::string& cookie_name, |
| 944 | const base::Closure& callback) { |
| 945 | scoped_refptr<DeleteCookieTask> task = |
| 946 | new DeleteCookieTask(this, url, cookie_name, callback); |
| 947 | |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 948 | DoCookieTaskForURL(task, url); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 949 | } |
| 950 | |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 951 | void CookieMonster::DeleteCanonicalCookieAsync(const CanonicalCookie& cookie, |
| 952 | const DeleteCallback& callback) { |
| 953 | scoped_refptr<DeleteCanonicalCookieTask> task = |
| 954 | new DeleteCanonicalCookieTask(this, cookie, callback); |
| 955 | |
| 956 | DoCookieTask(task); |
| 957 | } |
| 958 | |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 959 | void CookieMonster::DeleteAllCreatedBetweenAsync( |
| 960 | const Time& delete_begin, |
| 961 | const Time& delete_end, |
| 962 | const DeleteCallback& callback) { |
| 963 | scoped_refptr<DeleteAllCreatedBetweenTask> task = |
| 964 | new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end, callback); |
| 965 | |
| 966 | DoCookieTask(task); |
| 967 | } |
| 968 | |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 969 | void CookieMonster::DeleteAllCreatedBetweenWithPredicateAsync( |
| 970 | const Time& delete_begin, |
| 971 | const Time& delete_end, |
| 972 | const base::Callback<bool(const CanonicalCookie&)>& predicate, |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 973 | const DeleteCallback& callback) { |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 974 | if (predicate.is_null()) { |
| 975 | callback.Run(0); |
| 976 | return; |
| 977 | } |
| 978 | scoped_refptr<DeleteAllCreatedBetweenWithPredicateTask> task = |
| 979 | new DeleteAllCreatedBetweenWithPredicateTask( |
| 980 | this, delete_begin, delete_end, predicate, callback); |
| 981 | DoCookieTask(task); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 982 | } |
| 983 | |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 984 | void CookieMonster::DeleteSessionCookiesAsync( |
| 985 | const CookieStore::DeleteCallback& callback) { |
| 986 | scoped_refptr<DeleteSessionCookiesTask> task = |
| 987 | new DeleteSessionCookiesTask(this, callback); |
| 988 | |
| 989 | DoCookieTask(task); |
| 990 | } |
| 991 | |
mmenke | 18dd8ba | 2016-02-01 18:42:10 | [diff] [blame] | 992 | void CookieMonster::SetCookieableSchemes( |
| 993 | const std::vector<std::string>& schemes) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 994 | DCHECK(thread_checker_.CalledOnValidThread()); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 995 | |
| 996 | // Calls to this method will have no effect if made after a WebView or |
| 997 | // CookieManager instance has been created. |
mmenke | 18dd8ba | 2016-02-01 18:42:10 | [diff] [blame] | 998 | if (initialized_) |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 999 | return; |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1000 | |
mmenke | 18dd8ba | 2016-02-01 18:42:10 | [diff] [blame] | 1001 | cookieable_schemes_ = schemes; |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1002 | } |
| 1003 | |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1004 | // This function must be called before the CookieMonster is used. |
| 1005 | void CookieMonster::SetPersistSessionCookies(bool persist_session_cookies) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1006 | DCHECK(thread_checker_.CalledOnValidThread()); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1007 | DCHECK(!initialized_); |
| 1008 | persist_session_cookies_ = persist_session_cookies; |
| 1009 | } |
| 1010 | |
| 1011 | bool CookieMonster::IsCookieableScheme(const std::string& scheme) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1012 | DCHECK(thread_checker_.CalledOnValidThread()); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1013 | |
| 1014 | return std::find(cookieable_schemes_.begin(), cookieable_schemes_.end(), |
| 1015 | scheme) != cookieable_schemes_.end(); |
| 1016 | } |
| 1017 | |
mmenke | 18dd8ba | 2016-02-01 18:42:10 | [diff] [blame] | 1018 | const char* const CookieMonster::kDefaultCookieableSchemes[] = {"http", "https", |
| 1019 | "ws", "wss"}; |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1020 | const int CookieMonster::kDefaultCookieableSchemesCount = |
| 1021 | arraysize(kDefaultCookieableSchemes); |
| 1022 | |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 1023 | std::unique_ptr<CookieStore::CookieChangedSubscription> |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1024 | CookieMonster::AddCallbackForCookie(const GURL& gurl, |
| 1025 | const std::string& name, |
| 1026 | const CookieChangedCallback& callback) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1027 | DCHECK(thread_checker_.CalledOnValidThread()); |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 1028 | |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1029 | std::pair<GURL, std::string> key(gurl, name); |
| 1030 | if (hook_map_.count(key) == 0) |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1031 | hook_map_[key] = base::MakeUnique<CookieChangedCallbackList>(); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1032 | return hook_map_[key]->Add( |
| 1033 | base::Bind(&RunAsync, base::ThreadTaskRunnerHandle::Get(), callback)); |
| 1034 | } |
| 1035 | |
nharper | 5babb5e6 | 2016-03-09 18:58:07 | [diff] [blame] | 1036 | bool CookieMonster::IsEphemeral() { |
| 1037 | return store_.get() == nullptr; |
| 1038 | } |
| 1039 | |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1040 | CookieMonster::~CookieMonster() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1041 | DCHECK(thread_checker_.CalledOnValidThread()); |
mmenke | 05255cf | 2016-02-03 15:49:31 | [diff] [blame] | 1042 | |
mmenke | 606c59c | 2016-03-07 18:20:55 | [diff] [blame] | 1043 | // TODO(mmenke): Does it really make sense to run |delegate_| and |
| 1044 | // CookieChanged callbacks when the CookieStore is destroyed? |
mmenke | 05255cf | 2016-02-03 15:49:31 | [diff] [blame] | 1045 | for (CookieMap::iterator cookie_it = cookies_.begin(); |
| 1046 | cookie_it != cookies_.end();) { |
| 1047 | CookieMap::iterator current_cookie_it = cookie_it; |
| 1048 | ++cookie_it; |
| 1049 | InternalDeleteCookie(current_cookie_it, false /* sync_to_store */, |
| 1050 | DELETE_COOKIE_DONT_RECORD); |
| 1051 | } |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1052 | } |
| 1053 | |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 1054 | bool CookieMonster::SetCookieWithDetails(const GURL& url, |
| 1055 | const std::string& name, |
| 1056 | const std::string& value, |
| 1057 | const std::string& domain, |
| 1058 | const std::string& path, |
mmenke | fdd4fc7 | 2016-02-05 20:53:24 | [diff] [blame] | 1059 | base::Time creation_time, |
| 1060 | base::Time expiration_time, |
| 1061 | base::Time last_access_time, |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 1062 | bool secure, |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 1063 | bool http_only, |
mkwst | e1a2958 | 2016-03-15 10:07:52 | [diff] [blame] | 1064 | CookieSameSite same_site, |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 1065 | CookiePriority priority) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1066 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 69bb587 | 2010-01-12 20:33:52 | [diff] [blame] | 1067 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1068 | if (!HasCookieableScheme(url)) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1069 | return false; |
| 1070 | |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 1071 | // TODO(mmenke): This class assumes each cookie to have a unique creation |
| 1072 | // time. Allowing the caller to set the creation time violates that |
| 1073 | // assumption. Worth fixing? Worth noting that time changes between browser |
| 1074 | // restarts can cause the same issue. |
| 1075 | base::Time actual_creation_time = creation_time; |
| 1076 | if (creation_time.is_null()) { |
| 1077 | actual_creation_time = CurrentTime(); |
| 1078 | last_time_seen_ = actual_creation_time; |
| 1079 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1080 | |
rdsmith | b4ecb4c | 2017-05-12 18:39:03 | [diff] [blame] | 1081 | // Validate consistency of passed arguments. |
| 1082 | if (ParsedCookie::ParseTokenString(name) != name || |
| 1083 | ParsedCookie::ParseValueString(value) != value || |
| 1084 | ParsedCookie::ParseValueString(domain) != domain || |
| 1085 | ParsedCookie::ParseValueString(path) != path) { |
| 1086 | return false; |
| 1087 | } |
| 1088 | |
| 1089 | // Validate passed arguments against URL. |
| 1090 | if (secure && !url.SchemeIsCryptographic()) |
| 1091 | return false; |
| 1092 | |
| 1093 | std::string cookie_domain; |
| 1094 | if (!cookie_util::GetCookieDomainWithString(url, domain, &cookie_domain)) |
| 1095 | return false; |
| 1096 | |
| 1097 | std::string cookie_path = CanonicalCookie::CanonPathWithString(url, path); |
| 1098 | if (!path.empty() && cookie_path != path) |
| 1099 | return false; |
| 1100 | |
| 1101 | // Canonicalize path again to make sure it escapes characters as needed. |
| 1102 | url::Component path_component(0, cookie_path.length()); |
| 1103 | url::RawCanonOutputT<char> canon_path; |
| 1104 | url::Component canon_path_component; |
| 1105 | url::CanonicalizePath(cookie_path.data(), path_component, &canon_path, |
| 1106 | &canon_path_component); |
| 1107 | cookie_path = std::string(canon_path.data() + canon_path_component.begin, |
| 1108 | canon_path_component.len); |
| 1109 | |
rdsmith | 51010b0 | 2017-05-16 16:23:19 | [diff] [blame] | 1110 | std::unique_ptr<CanonicalCookie> cc(base::MakeUnique<CanonicalCookie>( |
rdsmith | b4ecb4c | 2017-05-12 18:39:03 | [diff] [blame] | 1111 | name, value, cookie_domain, cookie_path, actual_creation_time, |
| 1112 | expiration_time, last_access_time, secure, http_only, same_site, |
| 1113 | priority)); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1114 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1115 | CookieOptions options; |
| 1116 | options.set_include_httponly(); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 1117 | options.set_same_site_cookie_mode( |
| 1118 | CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX); |
mmenke | 009cf62e | 2016-07-18 19:33:31 | [diff] [blame] | 1119 | return SetCanonicalCookie(std::move(cc), url, options); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1120 | } |
| 1121 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1122 | CookieList CookieMonster::GetAllCookies() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1123 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1124 | |
| 1125 | // This function is being called to scrape the cookie list for management UI |
| 1126 | // or similar. We shouldn't show expired cookies in this list since it will |
| 1127 | // just be confusing to users, and this function is called rarely enough (and |
| 1128 | // is already slow enough) that it's OK to take the time to garbage collect |
| 1129 | // the expired cookies now. |
| 1130 | // |
| 1131 | // Note that this does not prune cookies to be below our limits (if we've |
| 1132 | // exceeded them) the way that calling GarbageCollect() would. |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1133 | GarbageCollectExpired( |
| 1134 | Time::Now(), CookieMapItPair(cookies_.begin(), cookies_.end()), NULL); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1135 | |
| 1136 | // Copy the CanonicalCookie pointers from the map so that we can use the same |
| 1137 | // sorter as elsewhere, then copy the result out. |
| 1138 | std::vector<CanonicalCookie*> cookie_ptrs; |
| 1139 | cookie_ptrs.reserve(cookies_.size()); |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1140 | for (const auto& cookie : cookies_) |
| 1141 | cookie_ptrs.push_back(cookie.second.get()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1142 | std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); |
| 1143 | |
| 1144 | CookieList cookie_list; |
| 1145 | cookie_list.reserve(cookie_ptrs.size()); |
vmpstr | 6d9996c8 | 2017-02-23 00:43:25 | [diff] [blame] | 1146 | for (auto* cookie_ptr : cookie_ptrs) |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1147 | cookie_list.push_back(*cookie_ptr); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1148 | |
| 1149 | return cookie_list; |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame] | 1150 | } |
| 1151 | |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 1152 | CookieList CookieMonster::GetCookieListWithOptions( |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1153 | const GURL& url, |
| 1154 | const CookieOptions& options) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1155 | DCHECK(thread_checker_.CalledOnValidThread()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1156 | |
mkwst | c611e6d | 2016-02-23 15:45:55 | [diff] [blame] | 1157 | CookieList cookies; |
| 1158 | if (!HasCookieableScheme(url)) |
| 1159 | return cookies; |
| 1160 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1161 | std::vector<CanonicalCookie*> cookie_ptrs; |
mkwst | 72b6516 | 2016-02-22 19:58:54 | [diff] [blame] | 1162 | FindCookiesForHostAndDomain(url, options, &cookie_ptrs); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1163 | std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1164 | |
georgesak | c15df672 | 2014-12-02 23:52:12 | [diff] [blame] | 1165 | cookies.reserve(cookie_ptrs.size()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1166 | for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); |
| 1167 | it != cookie_ptrs.end(); it++) |
| 1168 | cookies.push_back(**it); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1169 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1170 | return cookies; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1171 | } |
| 1172 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1173 | int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin, |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1174 | const Time& delete_end) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1175 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | d098033 | 2010-11-16 17:08:53 | [diff] [blame] | 1176 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1177 | int num_deleted = 0; |
| 1178 | for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 1179 | CookieMap::iterator curit = it; |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1180 | CanonicalCookie* cc = curit->second.get(); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1181 | ++it; |
[email protected] | d098033 | 2010-11-16 17:08:53 | [diff] [blame] | 1182 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1183 | if (cc->CreationDate() >= delete_begin && |
| 1184 | (delete_end.is_null() || cc->CreationDate() < delete_end)) { |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1185 | InternalDeleteCookie(curit, true, /*sync_to_store*/ |
nharper | 6890336 | 2017-01-20 04:07:14 | [diff] [blame] | 1186 | DELETE_COOKIE_CREATED_BETWEEN); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1187 | ++num_deleted; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1191 | return num_deleted; |
| 1192 | } |
| 1193 | |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 1194 | int CookieMonster::DeleteAllCreatedBetweenWithPredicate( |
| 1195 | const base::Time& delete_begin, |
| 1196 | const base::Time& delete_end, |
| 1197 | const base::Callback<bool(const CanonicalCookie&)>& predicate) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1198 | int num_deleted = 0; |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 1199 | for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 1200 | CookieMap::iterator curit = it; |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1201 | CanonicalCookie* cc = curit->second.get(); |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 1202 | ++it; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1203 | |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 1204 | if (cc->CreationDate() >= delete_begin && |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 1205 | // The assumption that null |delete_end| is equivalent to |
| 1206 | // Time::Max() is confusing. |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 1207 | (delete_end.is_null() || cc->CreationDate() < delete_end) && |
| 1208 | predicate.Run(*cc)) { |
| 1209 | InternalDeleteCookie(curit, true, /*sync_to_store*/ |
nharper | 6890336 | 2017-01-20 04:07:14 | [diff] [blame] | 1210 | DELETE_COOKIE_CREATED_BETWEEN_WITH_PREDICATE); |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 1211 | ++num_deleted; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1212 | } |
| 1213 | } |
dmurph | faea244c | 2016-04-09 00:42:30 | [diff] [blame] | 1214 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1215 | return num_deleted; |
| 1216 | } |
| 1217 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1218 | bool CookieMonster::SetCookieWithOptions(const GURL& url, |
| 1219 | const std::string& cookie_line, |
| 1220 | const CookieOptions& options) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1221 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1222 | |
| 1223 | if (!HasCookieableScheme(url)) { |
| 1224 | return false; |
| 1225 | } |
| 1226 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1227 | return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options); |
| 1228 | } |
| 1229 | |
| 1230 | std::string CookieMonster::GetCookiesWithOptions(const GURL& url, |
| 1231 | const CookieOptions& options) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1232 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1233 | |
[email protected] | 34a160d | 2011-05-12 22:12:49 | [diff] [blame] | 1234 | if (!HasCookieableScheme(url)) |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1235 | return std::string(); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1236 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1237 | std::vector<CanonicalCookie*> cookies; |
mkwst | 72b6516 | 2016-02-22 19:58:54 | [diff] [blame] | 1238 | FindCookiesForHostAndDomain(url, options, &cookies); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1239 | std::sort(cookies.begin(), cookies.end(), CookieSorter); |
| 1240 | |
[email protected] | 34a160d | 2011-05-12 22:12:49 | [diff] [blame] | 1241 | std::string cookie_line = BuildCookieLine(cookies); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1242 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1243 | VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line; |
| 1244 | |
| 1245 | return cookie_line; |
| 1246 | } |
| 1247 | |
| 1248 | void CookieMonster::DeleteCookie(const GURL& url, |
| 1249 | const std::string& cookie_name) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1250 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1251 | |
| 1252 | if (!HasCookieableScheme(url)) |
| 1253 | return; |
| 1254 | |
| 1255 | CookieOptions options; |
| 1256 | options.set_include_httponly(); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 1257 | options.set_same_site_cookie_mode( |
| 1258 | CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1259 | // Get the cookies for this host and its domain(s). |
| 1260 | std::vector<CanonicalCookie*> cookies; |
mkwst | 72b6516 | 2016-02-22 19:58:54 | [diff] [blame] | 1261 | FindCookiesForHostAndDomain(url, options, &cookies); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1262 | std::set<CanonicalCookie*> matching_cookies; |
| 1263 | |
vmpstr | 6f21f24 | 2016-06-29 02:16:47 | [diff] [blame] | 1264 | for (auto* cookie : cookies) { |
mmenke | 4379aeb | 2016-03-05 12:22:07 | [diff] [blame] | 1265 | if (cookie->Name() != cookie_name) |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1266 | continue; |
mmenke | 4379aeb | 2016-03-05 12:22:07 | [diff] [blame] | 1267 | if (!cookie->IsOnPath(url.path())) |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1268 | continue; |
mmenke | 4379aeb | 2016-03-05 12:22:07 | [diff] [blame] | 1269 | matching_cookies.insert(cookie); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 1273 | CookieMap::iterator curit = it; |
| 1274 | ++it; |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1275 | if (matching_cookies.find(curit->second.get()) != matching_cookies.end()) { |
nharper | 6890336 | 2017-01-20 04:07:14 | [diff] [blame] | 1276 | InternalDeleteCookie(curit, true, DELETE_COOKIE_SINGLE); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 1281 | int CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1282 | DCHECK(thread_checker_.CalledOnValidThread()); |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 1283 | |
| 1284 | for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); |
| 1285 | its.first != its.second; ++its.first) { |
| 1286 | // The creation date acts as the unique index... |
| 1287 | if (its.first->second->CreationDate() == cookie.CreationDate()) { |
nharper | 6890336 | 2017-01-20 04:07:14 | [diff] [blame] | 1288 | InternalDeleteCookie(its.first, true, DELETE_COOKIE_CANONICAL); |
mmenke | 24379d5 | 2016-02-05 23:50:17 | [diff] [blame] | 1289 | return 1; |
| 1290 | } |
| 1291 | } |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1295 | bool CookieMonster::SetCookieWithCreationTime(const GURL& url, |
| 1296 | const std::string& cookie_line, |
| 1297 | const base::Time& creation_time) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1298 | DCHECK(thread_checker_.CalledOnValidThread()); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1299 | DCHECK(!store_.get()) << "This method is only to be used by unit-tests."; |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1300 | |
| 1301 | if (!HasCookieableScheme(url)) { |
| 1302 | return false; |
| 1303 | } |
| 1304 | |
| 1305 | MarkCookieStoreAsInitialized(); |
| 1306 | if (ShouldFetchAllCookiesWhenFetchingAnyCookie()) |
| 1307 | FetchAllCookiesIfNecessary(); |
| 1308 | |
| 1309 | return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time, |
| 1310 | CookieOptions()); |
| 1311 | } |
| 1312 | |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 1313 | int CookieMonster::DeleteSessionCookies() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1314 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 1315 | |
| 1316 | int num_deleted = 0; |
| 1317 | for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 1318 | CookieMap::iterator curit = it; |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1319 | CanonicalCookie* cc = curit->second.get(); |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 1320 | ++it; |
| 1321 | |
| 1322 | if (!cc->IsPersistent()) { |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1323 | InternalDeleteCookie(curit, true, /*sync_to_store*/ |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 1324 | DELETE_COOKIE_EXPIRED); |
| 1325 | ++num_deleted; |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | return num_deleted; |
| 1330 | } |
| 1331 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 1332 | void CookieMonster::MarkCookieStoreAsInitialized() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1333 | DCHECK(thread_checker_.CalledOnValidThread()); |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 1334 | initialized_ = true; |
| 1335 | } |
| 1336 | |
| 1337 | void CookieMonster::FetchAllCookiesIfNecessary() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1338 | DCHECK(thread_checker_.CalledOnValidThread()); |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 1339 | if (store_.get() && !started_fetching_all_cookies_) { |
| 1340 | started_fetching_all_cookies_ = true; |
| 1341 | FetchAllCookies(); |
| 1342 | } |
| 1343 | } |
| 1344 | |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1345 | void CookieMonster::FetchAllCookies() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1346 | DCHECK(thread_checker_.CalledOnValidThread()); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1347 | DCHECK(store_.get()) << "Store must exist to initialize"; |
| 1348 | DCHECK(!finished_fetching_all_cookies_) |
| 1349 | << "All cookies have already been fetched."; |
| 1350 | |
| 1351 | // We bind in the current time so that we can report the wall-clock time for |
| 1352 | // loading cookies. |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1353 | store_->Load(base::Bind(&CookieMonster::OnLoaded, |
| 1354 | weak_ptr_factory_.GetWeakPtr(), TimeTicks::Now())); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 1355 | } |
| 1356 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 1357 | bool CookieMonster::ShouldFetchAllCookiesWhenFetchingAnyCookie() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1358 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 1359 | |
erikchen | 1dd72a7 | 2015-05-06 20:45:05 | [diff] [blame] | 1360 | if (fetch_strategy_ == kUnknownFetch) { |
| 1361 | const std::string group_name = |
| 1362 | base::FieldTrialList::FindFullName(kCookieMonsterFetchStrategyName); |
| 1363 | if (group_name == kFetchWhenNecessaryName) { |
| 1364 | fetch_strategy_ = kFetchWhenNecessary; |
| 1365 | } else if (group_name == kAlwaysFetchName) { |
| 1366 | fetch_strategy_ = kAlwaysFetch; |
| 1367 | } else { |
| 1368 | // The logic in the conditional is redundant, but it makes trials of |
| 1369 | // the Finch experiment more explicit. |
| 1370 | fetch_strategy_ = kAlwaysFetch; |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | return fetch_strategy_ == kAlwaysFetch; |
| 1375 | } |
| 1376 | |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1377 | void CookieMonster::OnLoaded( |
| 1378 | TimeTicks beginning_time, |
| 1379 | std::vector<std::unique_ptr<CanonicalCookie>> cookies) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1380 | DCHECK(thread_checker_.CalledOnValidThread()); |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1381 | StoreLoadedCookies(std::move(cookies)); |
[email protected] | c7593fb2 | 2011-11-14 23:54:27 | [diff] [blame] | 1382 | histogram_time_blocked_on_load_->AddTime(TimeTicks::Now() - beginning_time); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1383 | |
| 1384 | // Invoke the task queue of cookie request. |
| 1385 | InvokeQueue(); |
| 1386 | } |
| 1387 | |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1388 | void CookieMonster::OnKeyLoaded( |
| 1389 | const std::string& key, |
| 1390 | std::vector<std::unique_ptr<CanonicalCookie>> cookies) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1391 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 1392 | |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1393 | StoreLoadedCookies(std::move(cookies)); |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1394 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1395 | auto tasks_pending_for_key = tasks_pending_for_key_.find(key); |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1396 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1397 | // TODO(mmenke): Can this be turned into a DCHECK? |
| 1398 | if (tasks_pending_for_key == tasks_pending_for_key_.end()) |
| 1399 | return; |
[email protected] | bab72ec | 2013-10-30 20:50:02 | [diff] [blame] | 1400 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1401 | // Run all tasks for the key. Note that running a task can result in multiple |
| 1402 | // tasks being added to the back of the deque. |
| 1403 | while (!tasks_pending_for_key->second.empty()) { |
| 1404 | scoped_refptr<CookieMonsterTask> task = |
| 1405 | tasks_pending_for_key->second.front(); |
| 1406 | tasks_pending_for_key->second.pop_front(); |
| 1407 | |
| 1408 | task->Run(); |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1409 | } |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1410 | |
| 1411 | tasks_pending_for_key_.erase(tasks_pending_for_key); |
| 1412 | |
| 1413 | // This has to be done last, in case running a task queues a new task for the |
| 1414 | // key, to ensure tasks are run in the correct order. |
| 1415 | keys_loaded_.insert(key); |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1416 | } |
| 1417 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1418 | void CookieMonster::StoreLoadedCookies( |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1419 | std::vector<std::unique_ptr<CanonicalCookie>> cookies) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1420 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 1421 | |
pkasting | ec2cdb5 | 2015-05-02 01:19:34 | [diff] [blame] | 1422 | // TODO(erikwright): Remove ScopedTracker below once crbug.com/457528 is |
| 1423 | // fixed. |
| 1424 | tracked_objects::ScopedTracker tracking_profile( |
| 1425 | FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 1426 | "457528 CookieMonster::StoreLoadedCookies")); |
| 1427 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1428 | // Even if a key is expired, insert it so it can be garbage collected, |
| 1429 | // removed, and sync'd. |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1430 | CookieItVector cookies_with_control_chars; |
| 1431 | |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1432 | for (auto& cookie : cookies) { |
| 1433 | int64_t cookie_creation_time = cookie->CreationDate().ToInternalValue(); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1434 | |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1435 | if (creation_times_.insert(cookie_creation_time).second) { |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1436 | CanonicalCookie* cookie_ptr = cookie.get(); |
| 1437 | CookieMap::iterator inserted = InternalInsertCookie( |
| 1438 | GetKey(cookie_ptr->Domain()), std::move(cookie), GURL(), false); |
| 1439 | const Time cookie_access_time(cookie_ptr->LastAccessDate()); |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1440 | if (earliest_access_time_.is_null() || |
| 1441 | cookie_access_time < earliest_access_time_) |
| 1442 | earliest_access_time_ = cookie_access_time; |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1443 | |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1444 | if (ContainsControlCharacter(cookie_ptr->Name()) || |
| 1445 | ContainsControlCharacter(cookie_ptr->Value())) { |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1446 | cookies_with_control_chars.push_back(inserted); |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1447 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1448 | } else { |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1449 | LOG(ERROR) << base::StringPrintf( |
| 1450 | "Found cookies with duplicate creation " |
| 1451 | "times in backing store: " |
| 1452 | "{name='%s', domain='%s', path='%s'}", |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1453 | cookie->Name().c_str(), cookie->Domain().c_str(), |
| 1454 | cookie->Path().c_str()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1455 | } |
| 1456 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1457 | |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1458 | // Any cookies that contain control characters that we have loaded from the |
| 1459 | // persistent store should be deleted. See http://crbug.com/238041. |
| 1460 | for (CookieItVector::iterator it = cookies_with_control_chars.begin(); |
| 1461 | it != cookies_with_control_chars.end();) { |
| 1462 | CookieItVector::iterator curit = it; |
| 1463 | ++it; |
| 1464 | |
| 1465 | InternalDeleteCookie(*curit, true, DELETE_COOKIE_CONTROL_CHAR); |
| 1466 | } |
| 1467 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1468 | // After importing cookies from the PersistentCookieStore, verify that |
| 1469 | // none of our other constraints are violated. |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1470 | // In particular, the backing store might have given us duplicate cookies. |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1471 | |
| 1472 | // This method could be called multiple times due to priority loading, thus |
| 1473 | // cookies loaded in previous runs will be validated again, but this is OK |
| 1474 | // since they are expected to be much fewer than total DB. |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1475 | EnsureCookiesMapIsValid(); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1476 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1477 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1478 | void CookieMonster::InvokeQueue() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1479 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 1480 | |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 1481 | // Move all per-key tasks into the global queue, if there are any. This is |
| 1482 | // protection about a race where the store learns about all cookies loading |
| 1483 | // before it learned about the cookies for a key loading. |
| 1484 | |
| 1485 | // Needed to prevent any recursively queued tasks from going back into the |
| 1486 | // per-key queues. |
| 1487 | seen_global_task_ = true; |
| 1488 | for (const auto& tasks_for_key : tasks_pending_for_key_) { |
| 1489 | tasks_pending_.insert(tasks_pending_.begin(), tasks_for_key.second.begin(), |
| 1490 | tasks_for_key.second.end()); |
| 1491 | } |
| 1492 | tasks_pending_for_key_.clear(); |
| 1493 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1494 | while (!tasks_pending_.empty()) { |
| 1495 | scoped_refptr<CookieMonsterTask> request_task = tasks_pending_.front(); |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 1496 | tasks_pending_.pop_front(); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1497 | request_task->Run(); |
| 1498 | } |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1499 | |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 1500 | DCHECK(tasks_pending_for_key_.empty()); |
| 1501 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1502 | finished_fetching_all_cookies_ = true; |
| 1503 | creation_times_.clear(); |
| 1504 | keys_loaded_.clear(); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | void CookieMonster::EnsureCookiesMapIsValid() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1508 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1509 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1510 | // Iterate through all the of the cookies, grouped by host. |
| 1511 | CookieMap::iterator prev_range_end = cookies_.begin(); |
| 1512 | while (prev_range_end != cookies_.end()) { |
| 1513 | CookieMap::iterator cur_range_begin = prev_range_end; |
| 1514 | const std::string key = cur_range_begin->first; // Keep a copy. |
| 1515 | CookieMap::iterator cur_range_end = cookies_.upper_bound(key); |
| 1516 | prev_range_end = cur_range_end; |
| 1517 | |
| 1518 | // Ensure no equivalent cookies for this host. |
ellyjones | cabf5742 | 2015-08-21 18:44:51 | [diff] [blame] | 1519 | TrimDuplicateCookiesForKey(key, cur_range_begin, cur_range_end); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1520 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1521 | } |
| 1522 | |
ellyjones | cabf5742 | 2015-08-21 18:44:51 | [diff] [blame] | 1523 | void CookieMonster::TrimDuplicateCookiesForKey(const std::string& key, |
| 1524 | CookieMap::iterator begin, |
| 1525 | CookieMap::iterator end) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1526 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1527 | |
| 1528 | // Set of cookies ordered by creation time. |
| 1529 | typedef std::set<CookieMap::iterator, OrderByCreationTimeDesc> CookieSet; |
| 1530 | |
| 1531 | // Helper map we populate to find the duplicates. |
| 1532 | typedef std::map<CookieSignature, CookieSet> EquivalenceMap; |
| 1533 | EquivalenceMap equivalent_cookies; |
| 1534 | |
| 1535 | // The number of duplicate cookies that have been found. |
| 1536 | int num_duplicates = 0; |
| 1537 | |
| 1538 | // Iterate through all of the cookies in our range, and insert them into |
| 1539 | // the equivalence map. |
| 1540 | for (CookieMap::iterator it = begin; it != end; ++it) { |
| 1541 | DCHECK_EQ(key, it->first); |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1542 | CanonicalCookie* cookie = it->second.get(); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1543 | |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1544 | CookieSignature signature(cookie->Name(), cookie->Domain(), cookie->Path()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1545 | CookieSet& set = equivalent_cookies[signature]; |
| 1546 | |
| 1547 | // We found a duplicate! |
| 1548 | if (!set.empty()) |
| 1549 | num_duplicates++; |
| 1550 | |
| 1551 | // We save the iterator into |cookies_| rather than the actual cookie |
| 1552 | // pointer, since we may need to delete it later. |
| 1553 | bool insert_success = set.insert(it).second; |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1554 | DCHECK(insert_success) |
| 1555 | << "Duplicate creation times found in duplicate cookie name scan."; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | // If there were no duplicates, we are done! |
| 1559 | if (num_duplicates == 0) |
ellyjones | cabf5742 | 2015-08-21 18:44:51 | [diff] [blame] | 1560 | return; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1561 | |
| 1562 | // Make sure we find everything below that we did above. |
| 1563 | int num_duplicates_found = 0; |
| 1564 | |
| 1565 | // Otherwise, delete all the duplicate cookies, both from our in-memory store |
| 1566 | // and from the backing store. |
| 1567 | for (EquivalenceMap::iterator it = equivalent_cookies.begin(); |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1568 | it != equivalent_cookies.end(); ++it) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1569 | const CookieSignature& signature = it->first; |
| 1570 | CookieSet& dupes = it->second; |
| 1571 | |
| 1572 | if (dupes.size() <= 1) |
| 1573 | continue; // This cookiename/path has no duplicates. |
| 1574 | num_duplicates_found += dupes.size() - 1; |
| 1575 | |
| 1576 | // Since |dups| is sorted by creation time (descending), the first cookie |
| 1577 | // is the most recent one, so we will keep it. The rest are duplicates. |
| 1578 | dupes.erase(dupes.begin()); |
| 1579 | |
| 1580 | LOG(ERROR) << base::StringPrintf( |
| 1581 | "Found %d duplicate cookies for host='%s', " |
| 1582 | "with {name='%s', domain='%s', path='%s'}", |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1583 | static_cast<int>(dupes.size()), key.c_str(), signature.name.c_str(), |
| 1584 | signature.domain.c_str(), signature.path.c_str()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1585 | |
| 1586 | // Remove all the cookies identified by |dupes|. It is valid to delete our |
| 1587 | // list of iterators one at a time, since |cookies_| is a multimap (they |
| 1588 | // don't invalidate existing iterators following deletion). |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1589 | for (CookieSet::iterator dupes_it = dupes.begin(); dupes_it != dupes.end(); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1590 | ++dupes_it) { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1591 | InternalDeleteCookie(*dupes_it, true, |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1592 | DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE); |
| 1593 | } |
| 1594 | } |
| 1595 | DCHECK_EQ(num_duplicates, num_duplicates_found); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1596 | } |
| 1597 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1598 | void CookieMonster::FindCookiesForHostAndDomain( |
| 1599 | const GURL& url, |
| 1600 | const CookieOptions& options, |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1601 | std::vector<CanonicalCookie*>* cookies) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1602 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1603 | |
| 1604 | const Time current_time(CurrentTime()); |
| 1605 | |
| 1606 | // Probe to save statistics relatively frequently. We do it here rather |
| 1607 | // than in the set path as many websites won't set cookies, and we |
| 1608 | // want to collect statistics whenever the browser's being used. |
| 1609 | RecordPeriodicStats(current_time); |
| 1610 | |
[email protected] | 8e158367 | 2012-02-11 04:39:41 | [diff] [blame] | 1611 | // Can just dispatch to FindCookiesForKey |
| 1612 | const std::string key(GetKey(url.host())); |
mkwst | 72b6516 | 2016-02-22 19:58:54 | [diff] [blame] | 1613 | FindCookiesForKey(key, url, options, current_time, cookies); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1614 | } |
| 1615 | |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 1616 | void CookieMonster::FindCookiesForKey(const std::string& key, |
| 1617 | const GURL& url, |
| 1618 | const CookieOptions& options, |
| 1619 | const Time& current, |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 1620 | std::vector<CanonicalCookie*>* cookies) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1621 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1622 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1623 | for (CookieMapItPair its = cookies_.equal_range(key); |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1624 | its.first != its.second;) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1625 | CookieMap::iterator curit = its.first; |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1626 | CanonicalCookie* cc = curit->second.get(); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1627 | ++its.first; |
| 1628 | |
| 1629 | // If the cookie is expired, delete it. |
mmenke | 3c79a65 | 2016-02-12 14:39:20 | [diff] [blame] | 1630 | if (cc->IsExpired(current)) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1631 | InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED); |
| 1632 | continue; |
| 1633 | } |
| 1634 | |
[email protected] | 65f4e7e | 2012-12-12 21:56:54 | [diff] [blame] | 1635 | // Filter out cookies that should not be included for a request to the |
| 1636 | // given |url|. HTTP only cookies are filtered depending on the passed |
| 1637 | // cookie |options|. |
| 1638 | if (!cc->IncludeForRequestURL(url, options)) |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1639 | continue; |
| 1640 | |
[email protected] | 65f4e7e | 2012-12-12 21:56:54 | [diff] [blame] | 1641 | // Add this cookie to the set of matching cookies. Update the access |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1642 | // time if we've been requested to do so. |
mkwst | 72b6516 | 2016-02-22 19:58:54 | [diff] [blame] | 1643 | if (options.update_access_time()) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1644 | InternalUpdateCookieAccessTime(cc, current); |
| 1645 | } |
| 1646 | cookies->push_back(cc); |
| 1647 | } |
| 1648 | } |
| 1649 | |
| 1650 | bool CookieMonster::DeleteAnyEquivalentCookie(const std::string& key, |
| 1651 | const CanonicalCookie& ecc, |
mmenke | 009cf62e | 2016-07-18 19:33:31 | [diff] [blame] | 1652 | const GURL& source_url, |
[email protected] | e7c590e5 | 2011-03-30 08:33:55 | [diff] [blame] | 1653 | bool skip_httponly, |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 1654 | bool already_expired) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1655 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1656 | |
| 1657 | bool found_equivalent_cookie = false; |
| 1658 | bool skipped_httponly = false; |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 1659 | bool skipped_secure_cookie = false; |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 1660 | |
| 1661 | histogram_cookie_delete_equivalent_->Add(COOKIE_DELETE_EQUIVALENT_ATTEMPT); |
| 1662 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1663 | for (CookieMapItPair its = cookies_.equal_range(key); |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1664 | its.first != its.second;) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1665 | CookieMap::iterator curit = its.first; |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1666 | CanonicalCookie* cc = curit->second.get(); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1667 | ++its.first; |
| 1668 | |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 1669 | // If the cookie is being set from an insecure scheme, then if a cookie |
| 1670 | // already exists with the same name and it is Secure, then the cookie |
| 1671 | // should *not* be updated if they domain-match and ignoring the path |
| 1672 | // attribute. |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 1673 | // |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 1674 | // See: https://tools.ietf.org/html/draft-ietf-httpbis-cookie-alone |
| 1675 | if (cc->IsSecure() && !source_url.SchemeIsCryptographic() && |
mmenke | 2830b072 | 2016-07-20 16:02:50 | [diff] [blame] | 1676 | ecc.IsEquivalentForSecureCookieMatching(*cc)) { |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 1677 | skipped_secure_cookie = true; |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 1678 | histogram_cookie_delete_equivalent_->Add( |
| 1679 | COOKIE_DELETE_EQUIVALENT_SKIPPING_SECURE); |
| 1680 | // If the cookie is equivalent to the new cookie and wouldn't have been |
| 1681 | // skipped for being HTTP-only, record that it is a skipped secure cookie |
| 1682 | // that would have been deleted otherwise. |
| 1683 | if (ecc.IsEquivalent(*cc)) { |
jww | a9a0d48 | 2015-12-16 18:19:41 | [diff] [blame] | 1684 | found_equivalent_cookie = true; |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 1685 | |
| 1686 | if (!skip_httponly || !cc->IsHttpOnly()) { |
| 1687 | histogram_cookie_delete_equivalent_->Add( |
| 1688 | COOKIE_DELETE_EQUIVALENT_WOULD_HAVE_DELETED); |
| 1689 | } |
| 1690 | } |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 1691 | } else if (ecc.IsEquivalent(*cc)) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1692 | // We should never have more than one equivalent cookie, since they should |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 1693 | // overwrite each other, unless secure cookies require secure scheme is |
| 1694 | // being enforced. In that case, cookies with different paths might exist |
| 1695 | // and be considered equivalent. |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1696 | CHECK(!found_equivalent_cookie) |
| 1697 | << "Duplicate equivalent cookies found, cookie store is corrupted."; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1698 | if (skip_httponly && cc->IsHttpOnly()) { |
| 1699 | skipped_httponly = true; |
| 1700 | } else { |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 1701 | histogram_cookie_delete_equivalent_->Add( |
| 1702 | COOKIE_DELETE_EQUIVALENT_FOUND); |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1703 | InternalDeleteCookie(curit, true, already_expired |
| 1704 | ? DELETE_COOKIE_EXPIRED_OVERWRITE |
| 1705 | : DELETE_COOKIE_OVERWRITE); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1706 | } |
| 1707 | found_equivalent_cookie = true; |
| 1708 | } |
| 1709 | } |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 1710 | return skipped_httponly || skipped_secure_cookie; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1711 | } |
| 1712 | |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1713 | CookieMonster::CookieMap::iterator CookieMonster::InternalInsertCookie( |
| 1714 | const std::string& key, |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1715 | std::unique_ptr<CanonicalCookie> cc, |
mmenke | 009cf62e | 2016-07-18 19:33:31 | [diff] [blame] | 1716 | const GURL& source_url, |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1717 | bool sync_to_store) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1718 | DCHECK(thread_checker_.CalledOnValidThread()); |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1719 | CanonicalCookie* cc_ptr = cc.get(); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1720 | |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1721 | if ((cc_ptr->IsPersistent() || persist_session_cookies_) && store_.get() && |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1722 | sync_to_store) |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1723 | store_->AddCookie(*cc_ptr); |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1724 | CookieMap::iterator inserted = |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1725 | cookies_.insert(CookieMap::value_type(key, std::move(cc))); |
| 1726 | if (delegate_.get()) { |
| 1727 | delegate_->OnCookieChanged(*cc_ptr, false, |
| 1728 | CookieStore::ChangeCause::INSERTED); |
| 1729 | } |
mkwst | c1aa4cc | 2015-04-03 19:57:45 | [diff] [blame] | 1730 | |
| 1731 | // See InitializeHistograms() for details. |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1732 | int32_t type_sample = cc_ptr->SameSite() != CookieSameSite::NO_RESTRICTION |
mkwst | e1a2958 | 2016-03-15 10:07:52 | [diff] [blame] | 1733 | ? 1 << COOKIE_TYPE_SAME_SITE |
| 1734 | : 0; |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1735 | type_sample |= cc_ptr->IsHttpOnly() ? 1 << COOKIE_TYPE_HTTPONLY : 0; |
| 1736 | type_sample |= cc_ptr->IsSecure() ? 1 << COOKIE_TYPE_SECURE : 0; |
mkwst | 4654941 | 2016-02-01 10:05:37 | [diff] [blame] | 1737 | histogram_cookie_type_->Add(type_sample); |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 1738 | |
| 1739 | // Histogram the type of scheme used on URLs that set cookies. This |
| 1740 | // intentionally includes cookies that are set or overwritten by |
| 1741 | // http:// URLs, but not cookies that are cleared by http:// URLs, to |
| 1742 | // understand if the former behavior can be deprecated for Secure |
| 1743 | // cookies. |
mmenke | 009cf62e | 2016-07-18 19:33:31 | [diff] [blame] | 1744 | if (!source_url.is_empty()) { |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 1745 | CookieSource cookie_source_sample; |
mmenke | 009cf62e | 2016-07-18 19:33:31 | [diff] [blame] | 1746 | if (source_url.SchemeIsCryptographic()) { |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 1747 | cookie_source_sample = |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1748 | cc_ptr->IsSecure() |
| 1749 | ? COOKIE_SOURCE_SECURE_COOKIE_CRYPTOGRAPHIC_SCHEME |
| 1750 | : COOKIE_SOURCE_NONSECURE_COOKIE_CRYPTOGRAPHIC_SCHEME; |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 1751 | } else { |
| 1752 | cookie_source_sample = |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1753 | cc_ptr->IsSecure() |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 1754 | ? COOKIE_SOURCE_SECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME |
| 1755 | : COOKIE_SOURCE_NONSECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME; |
| 1756 | } |
| 1757 | histogram_cookie_source_scheme_->Add(cookie_source_sample); |
| 1758 | } |
mkwst | c1aa4cc | 2015-04-03 19:57:45 | [diff] [blame] | 1759 | |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1760 | RunCookieChangedCallbacks(*cc_ptr, CookieStore::ChangeCause::INSERTED); |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1761 | |
| 1762 | return inserted; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1763 | } |
| 1764 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1765 | bool CookieMonster::SetCookieWithCreationTimeAndOptions( |
| 1766 | const GURL& url, |
| 1767 | const std::string& cookie_line, |
| 1768 | const Time& creation_time_or_null, |
| 1769 | const CookieOptions& options) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1770 | DCHECK(thread_checker_.CalledOnValidThread()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1771 | |
[email protected] | 4d3ce78 | 2010-10-29 18:31:28 | [diff] [blame] | 1772 | VLOG(kVlogSetCookies) << "SetCookie() line: " << cookie_line; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1773 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1774 | Time creation_time = creation_time_or_null; |
| 1775 | if (creation_time.is_null()) { |
| 1776 | creation_time = CurrentTime(); |
| 1777 | last_time_seen_ = creation_time; |
| 1778 | } |
| 1779 | |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 1780 | std::unique_ptr<CanonicalCookie> cc( |
[email protected] | abbd13b | 2012-11-15 17:54:20 | [diff] [blame] | 1781 | CanonicalCookie::Create(url, cookie_line, creation_time, options)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1782 | |
| 1783 | if (!cc.get()) { |
[email protected] | 4d3ce78 | 2010-10-29 18:31:28 | [diff] [blame] | 1784 | VLOG(kVlogSetCookies) << "WARNING: Failed to allocate CanonicalCookie"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1785 | return false; |
| 1786 | } |
mmenke | 009cf62e | 2016-07-18 19:33:31 | [diff] [blame] | 1787 | return SetCanonicalCookie(std::move(cc), url, options); |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame] | 1788 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1789 | |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 1790 | bool CookieMonster::SetCanonicalCookie(std::unique_ptr<CanonicalCookie> cc, |
mmenke | 009cf62e | 2016-07-18 19:33:31 | [diff] [blame] | 1791 | const GURL& source_url, |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame] | 1792 | const CookieOptions& options) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1793 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 1794 | |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 1795 | Time creation_time = cc->CreationDate(); |
| 1796 | const std::string key(GetKey(cc->Domain())); |
| 1797 | bool already_expired = cc->IsExpired(creation_time); |
ellyjones | 399e35a2 | 2014-10-27 11:09:56 | [diff] [blame] | 1798 | |
mmenke | 009cf62e | 2016-07-18 19:33:31 | [diff] [blame] | 1799 | if (DeleteAnyEquivalentCookie(key, *cc, source_url, |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 1800 | options.exclude_httponly(), already_expired)) { |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 1801 | std::string error; |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 1802 | error = |
| 1803 | "SetCookie() not clobbering httponly cookie or secure cookie for " |
| 1804 | "insecure scheme"; |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 1805 | |
| 1806 | VLOG(kVlogSetCookies) << error; |
[email protected] | 3a96c74 | 2008-11-19 19:46:27 | [diff] [blame] | 1807 | return false; |
| 1808 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1809 | |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1810 | VLOG(kVlogSetCookies) << "SetCookie() key: " << key |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 1811 | << " cc: " << cc->DebugString(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1812 | |
| 1813 | // Realize that we might be setting an expired cookie, and the only point |
| 1814 | // was to delete the cookie which we've already done. |
mmenke | 3c79a65 | 2016-02-12 14:39:20 | [diff] [blame] | 1815 | if (!already_expired) { |
[email protected] | 374f58b | 2010-07-20 15:29:26 | [diff] [blame] | 1816 | // See InitializeHistograms() for details. |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 1817 | if (cc->IsPersistent()) { |
[email protected] | 8475bee | 2011-03-17 18:40:24 | [diff] [blame] | 1818 | histogram_expiration_duration_minutes_->Add( |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 1819 | (cc->ExpiryDate() - creation_time).InMinutes()); |
[email protected] | 8475bee | 2011-03-17 18:40:24 | [diff] [blame] | 1820 | } |
| 1821 | |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1822 | InternalInsertCookie(key, std::move(cc), source_url, true); |
[email protected] | 348dd66 | 2013-03-13 20:25:07 | [diff] [blame] | 1823 | } else { |
| 1824 | VLOG(kVlogSetCookies) << "SetCookie() not storing already expired cookie."; |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 1825 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1826 | |
| 1827 | // We assume that hopefully setting a cookie will be less common than |
| 1828 | // querying a cookie. Since setting a cookie can put us over our limits, |
| 1829 | // make sure that we garbage collect... We can also make the assumption that |
| 1830 | // if a cookie was set, in the common case it will be used soon after, |
| 1831 | // and we will purge the expired cookies in GetCookies(). |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 1832 | GarbageCollect(creation_time, key); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1833 | |
| 1834 | return true; |
| 1835 | } |
| 1836 | |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 1837 | bool CookieMonster::SetCanonicalCookies(const CookieList& list) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1838 | DCHECK(thread_checker_.CalledOnValidThread()); |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 1839 | |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 1840 | CookieOptions options; |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 1841 | options.set_include_httponly(); |
| 1842 | |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 1843 | for (const auto& cookie : list) { |
mmenke | 009cf62e | 2016-07-18 19:33:31 | [diff] [blame] | 1844 | // Use an empty GURL. This method does not support setting secure cookies. |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 1845 | if (!SetCanonicalCookie(base::MakeUnique<CanonicalCookie>(cookie), GURL(), |
| 1846 | options)) { |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 1847 | return false; |
mmenke | ea4cd40 | 2016-02-02 04:03:10 | [diff] [blame] | 1848 | } |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | return true; |
| 1852 | } |
| 1853 | |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1854 | void CookieMonster::InternalUpdateCookieAccessTime(CanonicalCookie* cc, |
| 1855 | const Time& current) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1856 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | bb890572 | 2010-05-21 17:29:04 | [diff] [blame] | 1857 | |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 1858 | // Based off the Mozilla code. When a cookie has been accessed recently, |
| 1859 | // don't bother updating its access time again. This reduces the number of |
| 1860 | // updates we do during pageload, which in turn reduces the chance our storage |
| 1861 | // backend will hit its batch thresholds and be forced to update. |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 1862 | if ((current - cc->LastAccessDate()) < last_access_threshold_) |
| 1863 | return; |
| 1864 | |
| 1865 | cc->SetLastAccessDate(current); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1866 | if ((cc->IsPersistent() || persist_session_cookies_) && store_.get()) |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 1867 | store_->UpdateCookieAccessTime(*cc); |
| 1868 | } |
| 1869 | |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1870 | // InternalDeleteCookies must not invalidate iterators other than the one being |
| 1871 | // deleted. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1872 | void CookieMonster::InternalDeleteCookie(CookieMap::iterator it, |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 1873 | bool sync_to_store, |
| 1874 | DeletionCause deletion_cause) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1875 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | bb890572 | 2010-05-21 17:29:04 | [diff] [blame] | 1876 | |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 1877 | // Ideally, this would be asserted up where we define kChangeCauseMapping, |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 1878 | // but DeletionCause's visibility (or lack thereof) forces us to make |
| 1879 | // this check here. |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 1880 | static_assert(arraysize(kChangeCauseMapping) == DELETE_COOKIE_LAST_ENTRY + 1, |
| 1881 | "kChangeCauseMapping size should match DeletionCause size"); |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 1882 | |
[email protected] | 374f58b | 2010-07-20 15:29:26 | [diff] [blame] | 1883 | // See InitializeHistograms() for details. |
nharper | 6890336 | 2017-01-20 04:07:14 | [diff] [blame] | 1884 | DeletionCause deletion_cause_to_record = deletion_cause; |
| 1885 | if (deletion_cause >= DELETE_COOKIE_CREATED_BETWEEN && |
| 1886 | deletion_cause <= DELETE_COOKIE_CANONICAL) { |
| 1887 | deletion_cause_to_record = DELETE_COOKIE_EXPLICIT; |
| 1888 | } |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1889 | if (deletion_cause != DELETE_COOKIE_DONT_RECORD) |
nharper | 6890336 | 2017-01-20 04:07:14 | [diff] [blame] | 1890 | histogram_cookie_deletion_cause_->Add(deletion_cause_to_record); |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 1891 | |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 1892 | CanonicalCookie* cc = it->second.get(); |
xiyuan | 8dbb8989 | 2015-04-13 17:04:30 | [diff] [blame] | 1893 | VLOG(kVlogSetCookies) << "InternalDeleteCookie()" |
| 1894 | << ", cause:" << deletion_cause |
| 1895 | << ", cc: " << cc->DebugString(); |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1896 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1897 | if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() && |
| 1898 | sync_to_store) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1899 | store_->DeleteCookie(*cc); |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 1900 | ChangeCausePair mapping = kChangeCauseMapping[deletion_cause]; |
| 1901 | if (delegate_.get() && mapping.notify) |
| 1902 | delegate_->OnCookieChanged(*cc, true, mapping.cause); |
| 1903 | RunCookieChangedCallbacks(*cc, mapping.cause); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1904 | cookies_.erase(it); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1905 | } |
| 1906 | |
[email protected] | 8807b32 | 2010-10-01 17:10:14 | [diff] [blame] | 1907 | // Domain expiry behavior is unchanged by key/expiry scheme (the |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1908 | // meaning of the key is different, but that's not visible to this routine). |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 1909 | size_t CookieMonster::GarbageCollect(const Time& current, |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 1910 | const std::string& key) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 1911 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | bb890572 | 2010-05-21 17:29:04 | [diff] [blame] | 1912 | |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 1913 | size_t num_deleted = 0; |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 1914 | Time safe_date(Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1915 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1916 | // Collect garbage for this key, minding cookie priorities. |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1917 | if (cookies_.count(key) > kDomainMaxCookies) { |
[email protected] | 4d3ce78 | 2010-10-29 18:31:28 | [diff] [blame] | 1918 | VLOG(kVlogGarbageCollection) << "GarbageCollect() key: " << key; |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1919 | |
mkwst | 8773435 | 2016-03-03 17:36:23 | [diff] [blame] | 1920 | CookieItVector* cookie_its; |
jww | 601411a | 2015-11-20 19:46:57 | [diff] [blame] | 1921 | |
mkwst | 8773435 | 2016-03-03 17:36:23 | [diff] [blame] | 1922 | CookieItVector non_expired_cookie_its; |
| 1923 | cookie_its = &non_expired_cookie_its; |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 1924 | num_deleted += |
mkwst | 8773435 | 2016-03-03 17:36:23 | [diff] [blame] | 1925 | GarbageCollectExpired(current, cookies_.equal_range(key), cookie_its); |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 1926 | |
mkwst | 8773435 | 2016-03-03 17:36:23 | [diff] [blame] | 1927 | if (cookie_its->size() > kDomainMaxCookies) { |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1928 | VLOG(kVlogGarbageCollection) << "Deep Garbage Collect domain."; |
| 1929 | size_t purge_goal = |
mkwst | 8773435 | 2016-03-03 17:36:23 | [diff] [blame] | 1930 | cookie_its->size() - (kDomainMaxCookies - kDomainPurgeCookies); |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1931 | DCHECK(purge_goal > kDomainPurgeCookies); |
| 1932 | |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 1933 | // Sort the cookies by access date, from least-recent to most-recent. |
| 1934 | std::sort(cookie_its->begin(), cookie_its->end(), LRACookieSorter); |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1935 | |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 1936 | // Remove all but the kDomainCookiesQuotaLow most-recently accessed |
| 1937 | // cookies with low-priority. Then, if cookies still need to be removed, |
| 1938 | // bump the quota and remove low- and medium-priority. Then, if cookies |
| 1939 | // _still_ need to be removed, bump the quota and remove cookies with |
| 1940 | // any priority. |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1941 | // |
| 1942 | // 1. Low-priority non-secure cookies. |
| 1943 | // 2. Low-priority secure cookies. |
| 1944 | // 3. Medium-priority non-secure cookies. |
| 1945 | // 4. High-priority non-secure cookies. |
| 1946 | // 5. Medium-priority secure cookies. |
| 1947 | // 6. High-priority secure cookies. |
| 1948 | const static struct { |
| 1949 | CookiePriority priority; |
| 1950 | bool protect_secure_cookies; |
| 1951 | } purge_rounds[] = { |
| 1952 | // 1. Low-priority non-secure cookies. |
| 1953 | {COOKIE_PRIORITY_LOW, true}, |
| 1954 | // 2. Low-priority secure cookies. |
| 1955 | {COOKIE_PRIORITY_LOW, false}, |
| 1956 | // 3. Medium-priority non-secure cookies. |
| 1957 | {COOKIE_PRIORITY_MEDIUM, true}, |
| 1958 | // 4. High-priority non-secure cookies. |
| 1959 | {COOKIE_PRIORITY_HIGH, true}, |
| 1960 | // 5. Medium-priority secure cookies. |
| 1961 | {COOKIE_PRIORITY_MEDIUM, false}, |
| 1962 | // 6. High-priority secure cookies. |
| 1963 | {COOKIE_PRIORITY_HIGH, false}, |
| 1964 | }; |
| 1965 | |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 1966 | size_t quota = 0; |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1967 | for (const auto& purge_round : purge_rounds) { |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 1968 | // Adjust quota according to the priority of cookies. Each round should |
| 1969 | // protect certain number of cookies in order to avoid starvation. |
| 1970 | // For example, when each round starts to remove cookies, the number of |
| 1971 | // cookies of that priority are counted and a decision whether they |
| 1972 | // should be deleted or not is made. If yes, some number of cookies of |
| 1973 | // that priority are deleted considering the quota. |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1974 | switch (purge_round.priority) { |
| 1975 | case COOKIE_PRIORITY_LOW: |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 1976 | quota = kDomainCookiesQuotaLow; |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1977 | break; |
| 1978 | case COOKIE_PRIORITY_MEDIUM: |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 1979 | quota = kDomainCookiesQuotaMedium; |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1980 | break; |
| 1981 | case COOKIE_PRIORITY_HIGH: |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 1982 | quota = kDomainCookiesQuotaHigh; |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1983 | break; |
| 1984 | } |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1985 | size_t just_deleted = 0u; |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 1986 | // Purge up to |purge_goal| for all cookies at the given priority. This |
| 1987 | // path will be taken only if the initial non-secure purge did not evict |
| 1988 | // enough cookies. |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1989 | if (purge_goal > 0) { |
| 1990 | just_deleted = PurgeLeastRecentMatches( |
| 1991 | cookie_its, purge_round.priority, quota, purge_goal, |
| 1992 | purge_round.protect_secure_cookies); |
| 1993 | DCHECK_LE(just_deleted, purge_goal); |
| 1994 | purge_goal -= just_deleted; |
| 1995 | num_deleted += just_deleted; |
| 1996 | } |
mkwst | 162d271 | 2016-02-18 18:21:29 | [diff] [blame] | 1997 | } |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 1998 | |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 1999 | DCHECK_EQ(0u, purge_goal); |
[email protected] | 8807b32 | 2010-10-01 17:10:14 | [diff] [blame] | 2000 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2001 | } |
| 2002 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 2003 | // Collect garbage for everything. With firefox style we want to preserve |
| 2004 | // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict. |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 2005 | if (cookies_.size() > kMaxCookies && earliest_access_time_ < safe_date) { |
[email protected] | 4d3ce78 | 2010-10-29 18:31:28 | [diff] [blame] | 2006 | VLOG(kVlogGarbageCollection) << "GarbageCollect() everything"; |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 2007 | CookieItVector cookie_its; |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 2008 | |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 2009 | num_deleted += GarbageCollectExpired( |
| 2010 | current, CookieMapItPair(cookies_.begin(), cookies_.end()), |
| 2011 | &cookie_its); |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 2012 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 2013 | if (cookie_its.size() > kMaxCookies) { |
| 2014 | VLOG(kVlogGarbageCollection) << "Deep Garbage Collect everything."; |
| 2015 | size_t purge_goal = cookie_its.size() - (kMaxCookies - kPurgeCookies); |
| 2016 | DCHECK(purge_goal > kPurgeCookies); |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 2017 | |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 2018 | CookieItVector secure_cookie_its; |
| 2019 | CookieItVector non_secure_cookie_its; |
| 2020 | SplitCookieVectorIntoSecureAndNonSecure(cookie_its, &secure_cookie_its, |
| 2021 | &non_secure_cookie_its); |
| 2022 | size_t non_secure_purge_goal = |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame^] | 2023 | std::min<size_t>(purge_goal, non_secure_cookie_its.size()); |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 2024 | |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame^] | 2025 | base::Time earliest_non_secure_access_time; |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 2026 | size_t just_deleted = GarbageCollectLeastRecentlyAccessed( |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame^] | 2027 | current, safe_date, non_secure_purge_goal, non_secure_cookie_its, |
| 2028 | &earliest_non_secure_access_time); |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 2029 | num_deleted += just_deleted; |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 2030 | |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame^] | 2031 | if (secure_cookie_its.size() == 0) { |
| 2032 | // This case is unlikely, but should still update |
| 2033 | // |earliest_access_time_| if only have non-secure cookies. |
| 2034 | earliest_access_time_ = earliest_non_secure_access_time; |
| 2035 | // Garbage collection can't delete all cookies. |
| 2036 | DCHECK(!earliest_access_time_.is_null()); |
| 2037 | } else if (just_deleted < purge_goal) { |
| 2038 | size_t secure_purge_goal = std::min<size_t>(purge_goal - just_deleted, |
| 2039 | secure_cookie_its.size()); |
| 2040 | base::Time earliest_secure_access_time; |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 2041 | num_deleted += GarbageCollectLeastRecentlyAccessed( |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame^] | 2042 | current, safe_date, secure_purge_goal, secure_cookie_its, |
| 2043 | &earliest_secure_access_time); |
| 2044 | |
| 2045 | if (!earliest_non_secure_access_time.is_null() && |
| 2046 | earliest_non_secure_access_time < earliest_secure_access_time) { |
| 2047 | earliest_access_time_ = earliest_non_secure_access_time; |
| 2048 | } else { |
| 2049 | earliest_access_time_ = earliest_secure_access_time; |
| 2050 | } |
| 2051 | |
| 2052 | // Garbage collection can't delete all cookies. |
| 2053 | DCHECK(!earliest_access_time_.is_null()); |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 2054 | } |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame^] | 2055 | |
| 2056 | // If there are secure cookies, but deleting non-secure cookies was enough |
| 2057 | // to meet the purge goal, secure cookies are never examined, so |
| 2058 | // |earliest_access_time_| can't be determined. Leaving it alone will mean |
| 2059 | // it's no later than the real earliest last access time, so this won't |
| 2060 | // lead to any problems. |
[email protected] | 8807b32 | 2010-10-01 17:10:14 | [diff] [blame] | 2061 | } |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | return num_deleted; |
| 2065 | } |
| 2066 | |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 2067 | size_t CookieMonster::PurgeLeastRecentMatches(CookieItVector* cookies, |
| 2068 | CookiePriority priority, |
| 2069 | size_t to_protect, |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 2070 | size_t purge_goal, |
| 2071 | bool protect_secure_cookies) { |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 2072 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 2073 | |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 2074 | // 1. Count number of the cookies at |priority| |
| 2075 | size_t cookies_count_possibly_to_be_deleted = CountCookiesForPossibleDeletion( |
| 2076 | priority, cookies, false /* count all cookies */); |
| 2077 | |
| 2078 | // 2. If |cookies_count_possibly_to_be_deleted| at |priority| is less than or |
| 2079 | // equal |to_protect|, skip round in order to preserve the quota. This |
| 2080 | // involves secure and non-secure cookies at |priority|. |
| 2081 | if (cookies_count_possibly_to_be_deleted <= to_protect) |
| 2082 | return 0u; |
| 2083 | |
| 2084 | // 3. Calculate number of secure cookies at |priority| |
| 2085 | // and number of cookies at |priority| that can possibly be deleted. |
| 2086 | // It is guaranteed we do not delete more than |purge_goal| even if |
| 2087 | // |cookies_count_possibly_to_be_deleted| is higher. |
| 2088 | size_t secure_cookies = 0u; |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 2089 | if (protect_secure_cookies) { |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 2090 | secure_cookies = CountCookiesForPossibleDeletion( |
| 2091 | priority, cookies, protect_secure_cookies /* count secure cookies */); |
| 2092 | cookies_count_possibly_to_be_deleted -= |
| 2093 | std::max(secure_cookies, to_protect - secure_cookies); |
| 2094 | } else { |
| 2095 | cookies_count_possibly_to_be_deleted -= to_protect; |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 2096 | } |
| 2097 | |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 2098 | size_t removed = 0u; |
| 2099 | size_t current = 0u; |
| 2100 | while ((removed < purge_goal && current < cookies->size()) && |
| 2101 | cookies_count_possibly_to_be_deleted > 0) { |
avi | e7cd11a | 2016-10-11 02:00:35 | [diff] [blame] | 2102 | const CanonicalCookie* current_cookie = cookies->at(current)->second.get(); |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 2103 | // Only delete the current cookie if the priority is equal to |
| 2104 | // the current level. |
jww | c00ac71 | 2016-05-05 22:21:44 | [diff] [blame] | 2105 | if (IsCookieEligibleForEviction(priority, protect_secure_cookies, |
| 2106 | current_cookie)) { |
mkwst | aa07ee8 | 2016-03-11 15:32:14 | [diff] [blame] | 2107 | InternalDeleteCookie(cookies->at(current), true, |
| 2108 | DELETE_COOKIE_EVICTED_DOMAIN); |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 2109 | cookies->erase(cookies->begin() + current); |
| 2110 | removed++; |
mmenke | 645ca677 | 2016-06-17 18:46:43 | [diff] [blame] | 2111 | cookies_count_possibly_to_be_deleted--; |
mkwst | e079ac41 | 2016-03-11 09:04:06 | [diff] [blame] | 2112 | } else { |
| 2113 | current++; |
| 2114 | } |
| 2115 | } |
| 2116 | return removed; |
| 2117 | } |
| 2118 | |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 2119 | size_t CookieMonster::GarbageCollectExpired(const Time& current, |
| 2120 | const CookieMapItPair& itpair, |
| 2121 | CookieItVector* cookie_its) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2122 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | bb890572 | 2010-05-21 17:29:04 | [diff] [blame] | 2123 | |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 2124 | int num_deleted = 0; |
| 2125 | for (CookieMap::iterator it = itpair.first, end = itpair.second; it != end;) { |
| 2126 | CookieMap::iterator curit = it; |
| 2127 | ++it; |
| 2128 | |
| 2129 | if (curit->second->IsExpired(current)) { |
[email protected] | 2f3f359 | 2010-07-07 20:11:51 | [diff] [blame] | 2130 | InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED); |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 2131 | ++num_deleted; |
| 2132 | } else if (cookie_its) { |
| 2133 | cookie_its->push_back(curit); |
| 2134 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | return num_deleted; |
| 2138 | } |
| 2139 | |
jww | 82d99c1 | 2015-11-25 18:39:53 | [diff] [blame] | 2140 | size_t CookieMonster::GarbageCollectDeleteRange( |
| 2141 | const Time& current, |
| 2142 | DeletionCause cause, |
| 2143 | CookieItVector::iterator it_begin, |
| 2144 | CookieItVector::iterator it_end) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2145 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 2146 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 2147 | for (CookieItVector::iterator it = it_begin; it != it_end; it++) { |
| 2148 | histogram_evicted_last_access_minutes_->Add( |
| 2149 | (current - (*it)->second->LastAccessDate()).InMinutes()); |
| 2150 | InternalDeleteCookie((*it), true, cause); |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 2151 | } |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 2152 | return it_end - it_begin; |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 2153 | } |
| 2154 | |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 2155 | size_t CookieMonster::GarbageCollectLeastRecentlyAccessed( |
| 2156 | const base::Time& current, |
| 2157 | const base::Time& safe_date, |
| 2158 | size_t purge_goal, |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame^] | 2159 | CookieItVector cookie_its, |
| 2160 | base::Time* earliest_time) { |
| 2161 | DCHECK_LE(purge_goal, cookie_its.size()); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2162 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 2163 | |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame^] | 2164 | // Sorts up to *and including* |cookie_its[purge_goal]| (if it exists), so |
| 2165 | // |earliest_time| will be properly assigned even if |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 2166 | // |global_purge_it| == |cookie_its.begin() + purge_goal|. |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame^] | 2167 | SortLeastRecentlyAccessed( |
| 2168 | cookie_its.begin(), cookie_its.end(), |
| 2169 | cookie_its.size() < purge_goal ? purge_goal + 1 : purge_goal); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 2170 | // Find boundary to cookies older than safe_date. |
| 2171 | CookieItVector::iterator global_purge_it = LowerBoundAccessDate( |
| 2172 | cookie_its.begin(), cookie_its.begin() + purge_goal, safe_date); |
jww | a26e439d | 2017-01-27 18:17:27 | [diff] [blame] | 2173 | // Only delete the old cookies and delete non-secure ones first. |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 2174 | size_t num_deleted = |
| 2175 | GarbageCollectDeleteRange(current, DELETE_COOKIE_EVICTED_GLOBAL, |
| 2176 | cookie_its.begin(), global_purge_it); |
mmenke | f4721d99 | 2017-06-07 17:13:59 | [diff] [blame^] | 2177 | if (global_purge_it != cookie_its.end()) |
| 2178 | *earliest_time = (*global_purge_it)->second->LastAccessDate(); |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 2179 | return num_deleted; |
| 2180 | } |
| 2181 | |
[email protected] | ed32c21 | 2013-05-14 20:49:29 | [diff] [blame] | 2182 | // A wrapper around registry_controlled_domains::GetDomainAndRegistry |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2183 | // to make clear we're creating a key for our local map. Here and |
| 2184 | // in FindCookiesForHostAndDomain() are the only two places where |
| 2185 | // we need to conditionalize based on key type. |
| 2186 | // |
| 2187 | // Note that this key algorithm explicitly ignores the scheme. This is |
| 2188 | // because when we're entering cookies into the map from the backing store, |
| 2189 | // we in general won't have the scheme at that point. |
| 2190 | // In practical terms, this means that file cookies will be stored |
| 2191 | // in the map either by an empty string or by UNC name (and will be |
| 2192 | // limited by kMaxCookiesPerHost), and extension cookies will be stored |
| 2193 | // based on the single extension id, as the extension id won't have the |
| 2194 | // form of a DNS host and hence GetKey() will return it unchanged. |
| 2195 | // |
| 2196 | // Arguably the right thing to do here is to make the key |
| 2197 | // algorithm dependent on the scheme, and make sure that the scheme is |
| 2198 | // available everywhere the key must be obtained (specfically at backing |
| 2199 | // store load time). This would require either changing the backing store |
| 2200 | // database schema to include the scheme (far more trouble than it's worth), or |
| 2201 | // separating out file cookies into their own CookieMonster instance and |
| 2202 | // thus restricting each scheme to a single cookie monster (which might |
| 2203 | // be worth it, but is still too much trouble to solve what is currently a |
| 2204 | // non-problem). |
| 2205 | std::string CookieMonster::GetKey(const std::string& domain) const { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2206 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 2207 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2208 | std::string effective_domain( |
[email protected] | ed32c21 | 2013-05-14 20:49:29 | [diff] [blame] | 2209 | registry_controlled_domains::GetDomainAndRegistry( |
[email protected] | aabe179 | 2014-01-30 21:37:46 | [diff] [blame] | 2210 | domain, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2211 | if (effective_domain.empty()) |
| 2212 | effective_domain = domain; |
| 2213 | |
| 2214 | if (!effective_domain.empty() && effective_domain[0] == '.') |
| 2215 | return effective_domain.substr(1); |
| 2216 | return effective_domain; |
| 2217 | } |
| 2218 | |
| 2219 | bool CookieMonster::HasCookieableScheme(const GURL& url) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2220 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2221 | |
| 2222 | // Make sure the request is on a cookie-able url scheme. |
| 2223 | for (size_t i = 0; i < cookieable_schemes_.size(); ++i) { |
| 2224 | // We matched a scheme. |
| 2225 | if (url.SchemeIs(cookieable_schemes_[i].c_str())) { |
| 2226 | // We've matched a supported scheme. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2227 | return true; |
| 2228 | } |
| 2229 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2230 | |
| 2231 | // The scheme didn't match any in our whitelist. |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 2232 | VLOG(kVlogPerCookieMonster) |
| 2233 | << "WARNING: Unsupported cookie scheme: " << url.scheme(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2234 | return false; |
| 2235 | } |
| 2236 | |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 2237 | // Test to see if stats should be recorded, and record them if so. |
| 2238 | // The goal here is to get sampling for the average browser-hour of |
| 2239 | // activity. We won't take samples when the web isn't being surfed, |
| 2240 | // and when the web is being surfed, we'll take samples about every |
| 2241 | // kRecordStatisticsIntervalSeconds. |
| 2242 | // last_statistic_record_time_ is initialized to Now() rather than null |
| 2243 | // in the constructor so that we won't take statistics right after |
| 2244 | // startup, to avoid bias from browsers that are started but not used. |
| 2245 | void CookieMonster::RecordPeriodicStats(const base::Time& current_time) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2246 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 2247 | |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 2248 | const base::TimeDelta kRecordStatisticsIntervalTime( |
| 2249 | base::TimeDelta::FromSeconds(kRecordStatisticsIntervalSeconds)); |
| 2250 | |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 2251 | // If we've taken statistics recently, return. |
| 2252 | if (current_time - last_statistic_record_time_ <= |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 2253 | kRecordStatisticsIntervalTime) { |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 2254 | return; |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 2255 | } |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 2256 | |
| 2257 | // See InitializeHistograms() for details. |
| 2258 | histogram_count_->Add(cookies_.size()); |
| 2259 | |
| 2260 | // More detailed statistics on cookie counts at different granularities. |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 2261 | last_statistic_record_time_ = current_time; |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 2262 | } |
| 2263 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2264 | // Initialize all histogram counter variables used in this class. |
| 2265 | // |
| 2266 | // Normal histogram usage involves using the macros defined in |
| 2267 | // histogram.h, which automatically takes care of declaring these |
| 2268 | // variables (as statics), initializing them, and accumulating into |
| 2269 | // them, all from a single entry point. Unfortunately, that solution |
| 2270 | // doesn't work for the CookieMonster, as it's vulnerable to races between |
| 2271 | // separate threads executing the same functions and hence initializing the |
| 2272 | // same static variables. There isn't a race danger in the histogram |
| 2273 | // accumulation calls; they are written to be resilient to simultaneous |
| 2274 | // calls from multiple threads. |
| 2275 | // |
| 2276 | // The solution taken here is to have per-CookieMonster instance |
| 2277 | // variables that are constructed during CookieMonster construction. |
| 2278 | // Note that these variables refer to the same underlying histogram, |
| 2279 | // so we still race (but safely) with other CookieMonster instances |
| 2280 | // for accumulation. |
| 2281 | // |
| 2282 | // To do this we've expanded out the individual histogram macros calls, |
| 2283 | // with declarations of the variables in the class decl, initialization here |
| 2284 | // (done from the class constructor) and direct calls to the accumulation |
| 2285 | // methods where needed. The specific histogram macro calls on which the |
| 2286 | // initialization is based are included in comments below. |
| 2287 | void CookieMonster::InitializeHistograms() { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2288 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 2289 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2290 | // From UMA_HISTOGRAM_CUSTOM_COUNTS |
| 2291 | histogram_expiration_duration_minutes_ = base::Histogram::FactoryGet( |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 2292 | "Cookie.ExpirationDurationMinutes", 1, kMinutesInTenYears, 50, |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2293 | base::Histogram::kUmaTargetedHistogramFlag); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2294 | histogram_evicted_last_access_minutes_ = base::Histogram::FactoryGet( |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 2295 | "Cookie.EvictedLastAccessMinutes", 1, kMinutesInTenYears, 50, |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2296 | base::Histogram::kUmaTargetedHistogramFlag); |
| 2297 | histogram_count_ = base::Histogram::FactoryGet( |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 2298 | "Cookie.Count", 1, 4000, 50, base::Histogram::kUmaTargetedHistogramFlag); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2299 | |
| 2300 | // From UMA_HISTOGRAM_ENUMERATION |
| 2301 | histogram_cookie_deletion_cause_ = base::LinearHistogram::FactoryGet( |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 2302 | "Cookie.DeletionCause", 1, DELETE_COOKIE_LAST_ENTRY - 1, |
| 2303 | DELETE_COOKIE_LAST_ENTRY, base::Histogram::kUmaTargetedHistogramFlag); |
mkwst | c1aa4cc | 2015-04-03 19:57:45 | [diff] [blame] | 2304 | histogram_cookie_type_ = base::LinearHistogram::FactoryGet( |
mkwst | 87378d9 | 2015-04-10 21:22:11 | [diff] [blame] | 2305 | "Cookie.Type", 1, (1 << COOKIE_TYPE_LAST_ENTRY) - 1, |
| 2306 | 1 << COOKIE_TYPE_LAST_ENTRY, base::Histogram::kUmaTargetedHistogramFlag); |
estark | 7feb65c2b | 2015-08-21 23:38:20 | [diff] [blame] | 2307 | histogram_cookie_source_scheme_ = base::LinearHistogram::FactoryGet( |
| 2308 | "Cookie.CookieSourceScheme", 1, COOKIE_SOURCE_LAST_ENTRY - 1, |
| 2309 | COOKIE_SOURCE_LAST_ENTRY, base::Histogram::kUmaTargetedHistogramFlag); |
jww | 31e3263 | 2015-12-16 23:38:34 | [diff] [blame] | 2310 | histogram_cookie_delete_equivalent_ = base::LinearHistogram::FactoryGet( |
| 2311 | "Cookie.CookieDeleteEquivalent", 1, |
| 2312 | COOKIE_DELETE_EQUIVALENT_LAST_ENTRY - 1, |
| 2313 | COOKIE_DELETE_EQUIVALENT_LAST_ENTRY, |
| 2314 | base::Histogram::kUmaTargetedHistogramFlag); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2315 | |
| 2316 | // From UMA_HISTOGRAM_{CUSTOM_,}TIMES |
[email protected] | c7593fb2 | 2011-11-14 23:54:27 | [diff] [blame] | 2317 | histogram_time_blocked_on_load_ = base::Histogram::FactoryTimeGet( |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 2318 | "Cookie.TimeBlockedOnLoad", base::TimeDelta::FromMilliseconds(1), |
| 2319 | base::TimeDelta::FromMinutes(1), 50, |
| 2320 | base::Histogram::kUmaTargetedHistogramFlag); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2321 | } |
| 2322 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2323 | // The system resolution is not high enough, so we can have multiple |
| 2324 | // set cookies that result in the same system time. When this happens, we |
| 2325 | // increment by one Time unit. Let's hope computers don't get too fast. |
| 2326 | Time CookieMonster::CurrentTime() { |
mkwst | be84af31 | 2015-02-20 08:52:45 | [diff] [blame] | 2327 | return std::max(Time::Now(), Time::FromInternalValue( |
| 2328 | last_time_seen_.ToInternalValue() + 1)); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2329 | } |
| 2330 | |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 2331 | void CookieMonster::DoCookieTask( |
| 2332 | const scoped_refptr<CookieMonsterTask>& task_item) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2333 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 2334 | |
| 2335 | MarkCookieStoreAsInitialized(); |
| 2336 | FetchAllCookiesIfNecessary(); |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 2337 | seen_global_task_ = true; |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2338 | |
| 2339 | if (!finished_fetching_all_cookies_ && store_.get()) { |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 2340 | tasks_pending_.push_back(task_item); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2341 | return; |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | task_item->Run(); |
| 2345 | } |
| 2346 | |
| 2347 | void CookieMonster::DoCookieTaskForURL( |
| 2348 | const scoped_refptr<CookieMonsterTask>& task_item, |
| 2349 | const GURL& url) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2350 | MarkCookieStoreAsInitialized(); |
| 2351 | if (ShouldFetchAllCookiesWhenFetchingAnyCookie()) |
| 2352 | FetchAllCookiesIfNecessary(); |
| 2353 | |
| 2354 | // If cookies for the requested domain key (eTLD+1) have been loaded from DB |
| 2355 | // then run the task, otherwise load from DB. |
| 2356 | if (!finished_fetching_all_cookies_ && store_.get()) { |
mmenke | f49fca0e | 2016-03-08 12:46:24 | [diff] [blame] | 2357 | // If a global task has been previously seen, queue the task as a global |
| 2358 | // task. Note that the CookieMonster may be in the middle of executing |
| 2359 | // the global queue, |tasks_pending_| may be empty, which is why another |
| 2360 | // bool is needed. |
| 2361 | if (seen_global_task_) { |
| 2362 | tasks_pending_.push_back(task_item); |
| 2363 | return; |
| 2364 | } |
| 2365 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2366 | // Checks if the domain key has been loaded. |
| 2367 | std::string key(cookie_util::GetEffectiveDomain(url.scheme(), url.host())); |
| 2368 | if (keys_loaded_.find(key) == keys_loaded_.end()) { |
| 2369 | std::map<std::string, |
| 2370 | std::deque<scoped_refptr<CookieMonsterTask>>>::iterator it = |
| 2371 | tasks_pending_for_key_.find(key); |
| 2372 | if (it == tasks_pending_for_key_.end()) { |
| 2373 | store_->LoadCookiesForKey( |
| 2374 | key, base::Bind(&CookieMonster::OnKeyLoaded, |
| 2375 | weak_ptr_factory_.GetWeakPtr(), key)); |
| 2376 | it = tasks_pending_for_key_ |
| 2377 | .insert(std::make_pair( |
| 2378 | key, std::deque<scoped_refptr<CookieMonsterTask>>())) |
| 2379 | .first; |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 2380 | } |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2381 | it->second.push_back(task_item); |
| 2382 | return; |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 2383 | } |
| 2384 | } |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2385 | |
mmenke | 74bcbd5 | 2016-01-21 17:17:56 | [diff] [blame] | 2386 | task_item->Run(); |
| 2387 | } |
| 2388 | |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 2389 | void CookieMonster::ComputeCookieDiff(CookieList* old_cookies, |
| 2390 | CookieList* new_cookies, |
| 2391 | CookieList* cookies_to_add, |
| 2392 | CookieList* cookies_to_delete) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2393 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 2394 | |
droger | d5d1278c | 2015-03-17 19:21:51 | [diff] [blame] | 2395 | DCHECK(old_cookies); |
| 2396 | DCHECK(new_cookies); |
| 2397 | DCHECK(cookies_to_add); |
| 2398 | DCHECK(cookies_to_delete); |
| 2399 | DCHECK(cookies_to_add->empty()); |
| 2400 | DCHECK(cookies_to_delete->empty()); |
| 2401 | |
| 2402 | // Sort both lists. |
| 2403 | // A set ordered by FullDiffCookieSorter is also ordered by |
| 2404 | // PartialDiffCookieSorter. |
| 2405 | std::sort(old_cookies->begin(), old_cookies->end(), FullDiffCookieSorter); |
| 2406 | std::sort(new_cookies->begin(), new_cookies->end(), FullDiffCookieSorter); |
| 2407 | |
| 2408 | // Select any old cookie for deletion if no new cookie has the same name, |
| 2409 | // domain, and path. |
| 2410 | std::set_difference( |
| 2411 | old_cookies->begin(), old_cookies->end(), new_cookies->begin(), |
| 2412 | new_cookies->end(), |
| 2413 | std::inserter(*cookies_to_delete, cookies_to_delete->begin()), |
| 2414 | PartialDiffCookieSorter); |
| 2415 | |
| 2416 | // Select any new cookie for addition (or update) if no old cookie is exactly |
| 2417 | // equivalent. |
| 2418 | std::set_difference(new_cookies->begin(), new_cookies->end(), |
| 2419 | old_cookies->begin(), old_cookies->end(), |
| 2420 | std::inserter(*cookies_to_add, cookies_to_add->begin()), |
| 2421 | FullDiffCookieSorter); |
| 2422 | } |
| 2423 | |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2424 | void CookieMonster::RunCallback(const base::Closure& callback) { |
| 2425 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 2426 | callback.Run(); |
| 2427 | } |
| 2428 | |
| 2429 | void CookieMonster::RunCookieChangedCallbacks(const CanonicalCookie& cookie, |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 2430 | ChangeCause cause) { |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2431 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 2432 | |
ellyjones | 399e35a2 | 2014-10-27 11:09:56 | [diff] [blame] | 2433 | CookieOptions opts; |
| 2434 | opts.set_include_httponly(); |
mkwst | f71d0bd | 2016-03-21 14:15:24 | [diff] [blame] | 2435 | opts.set_same_site_cookie_mode( |
| 2436 | CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX); |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2437 | // Note that the callbacks in hook_map_ are wrapped with RunAsync(), so they |
ellyjones | 399e35a2 | 2014-10-27 11:09:56 | [diff] [blame] | 2438 | // are guaranteed to not take long - they just post a RunAsync task back to |
mmenke | be0910d | 2016-03-01 19:09:09 | [diff] [blame] | 2439 | // the appropriate thread's message loop and return. |
| 2440 | // TODO(mmenke): Consider running these synchronously? |
ellyjones | 399e35a2 | 2014-10-27 11:09:56 | [diff] [blame] | 2441 | for (CookieChangedHookMap::iterator it = hook_map_.begin(); |
| 2442 | it != hook_map_.end(); ++it) { |
| 2443 | std::pair<GURL, std::string> key = it->first; |
| 2444 | if (cookie.IncludeForRequestURL(key.first, opts) && |
| 2445 | cookie.Name() == key.second) { |
nharper | 352933e | 2016-09-30 18:24:57 | [diff] [blame] | 2446 | it->second->Notify(cookie, cause); |
ellyjones | 399e35a2 | 2014-10-27 11:09:56 | [diff] [blame] | 2447 | } |
| 2448 | } |
| 2449 | } |
| 2450 | |
[email protected] | 6372531 | 2012-07-19 08:24:16 | [diff] [blame] | 2451 | } // namespace net |