[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> |
[email protected] | 0966648 | 2011-07-12 12:50:40 | [diff] [blame] | 49 | #include <set> |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 50 | |
| 51 | #include "base/basictypes.h" |
[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" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 54 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 55 | #include "base/memory/scoped_ptr.h" |
[email protected] | 5ee2098 | 2013-07-17 21:51:18 | [diff] [blame] | 56 | #include "base/message_loop/message_loop.h" |
[email protected] | 7ccb707 | 2013-06-10 20:56:28 | [diff] [blame] | 57 | #include "base/message_loop/message_loop_proxy.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 58 | #include "base/metrics/histogram.h" |
[email protected] | 4b35521 | 2013-06-11 10:35:19 | [diff] [blame] | 59 | #include "base/strings/string_util.h" |
| 60 | #include "base/strings/stringprintf.h" |
[email protected] | be28b5f4 | 2012-07-20 11:31:25 | [diff] [blame] | 61 | #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
[email protected] | 4b35521 | 2013-06-11 10:35:19 | [diff] [blame] | 62 | #include "net/cookies/canonical_cookie.h" |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 63 | #include "net/cookies/cookie_util.h" |
[email protected] | ebfe317 | 2012-07-12 12:21:41 | [diff] [blame] | 64 | #include "net/cookies/parsed_cookie.h" |
[email protected] | f89276a7 | 2013-07-12 06:41:54 | [diff] [blame] | 65 | #include "url/gurl.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 66 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 67 | using base::Time; |
| 68 | using base::TimeDelta; |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 69 | using base::TimeTicks; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 70 | |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 71 | // In steady state, most cookie requests can be satisfied by the in memory |
| 72 | // cookie monster store. However, if a request comes in during the initial |
| 73 | // cookie load, it must be delayed until that load completes. That is done by |
[email protected] | 0184df3 | 2013-05-14 00:53:55 | [diff] [blame] | 74 | // queueing it on CookieMonster::tasks_pending_ and running it when notification |
| 75 | // of cookie load completion is received via CookieMonster::OnLoaded. This |
| 76 | // callback is passed to the persistent store from CookieMonster::InitStore(), |
| 77 | // which is called on the first operation invoked on the CookieMonster. |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 78 | // |
| 79 | // On the browser critical paths (e.g. for loading initial web pages in a |
| 80 | // session restore) it may take too long to wait for the full load. If a cookie |
| 81 | // request is for a specific URL, DoCookieTaskForURL is called, which triggers a |
| 82 | // priority load if the key is not loaded yet by calling PersistentCookieStore |
[email protected] | 0184df3 | 2013-05-14 00:53:55 | [diff] [blame] | 83 | // :: LoadCookiesForKey. The request is queued in |
| 84 | // CookieMonster::tasks_pending_for_key_ and executed upon receiving |
| 85 | // notification of key load completion via CookieMonster::OnKeyLoaded(). If |
| 86 | // multiple requests for the same eTLD+1 are received before key load |
| 87 | // completion, only the first request calls |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 88 | // PersistentCookieStore::LoadCookiesForKey, all subsequent requests are queued |
[email protected] | 0184df3 | 2013-05-14 00:53:55 | [diff] [blame] | 89 | // in CookieMonster::tasks_pending_for_key_ and executed upon receiving |
| 90 | // notification of key load completion triggered by the first request for the |
| 91 | // same eTLD+1. |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 92 | |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 93 | static const int kMinutesInTenYears = 10 * 365 * 24 * 60; |
| 94 | |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 95 | namespace net { |
| 96 | |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 97 | // See comments at declaration of these variables in cookie_monster.h |
| 98 | // for details. |
[email protected] | 8807b32 | 2010-10-01 17:10:14 | [diff] [blame] | 99 | const size_t CookieMonster::kDomainMaxCookies = 180; |
| 100 | const size_t CookieMonster::kDomainPurgeCookies = 30; |
| 101 | const size_t CookieMonster::kMaxCookies = 3300; |
| 102 | const size_t CookieMonster::kPurgeCookies = 300; |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 103 | |
| 104 | const size_t CookieMonster::kDomainCookiesQuotaLow = 30; |
| 105 | const size_t CookieMonster::kDomainCookiesQuotaMedium = 50; |
| 106 | const size_t CookieMonster::kDomainCookiesQuotaHigh = |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 107 | kDomainMaxCookies - kDomainPurgeCookies |
| 108 | - kDomainCookiesQuotaLow - kDomainCookiesQuotaMedium; |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 109 | |
[email protected] | 8807b32 | 2010-10-01 17:10:14 | [diff] [blame] | 110 | const int CookieMonster::kSafeFromGlobalPurgeDays = 30; |
[email protected] | 297a4ed0 | 2010-02-12 08:12:52 | [diff] [blame] | 111 | |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 112 | namespace { |
[email protected] | e32306c5 | 2008-11-06 16:59:05 | [diff] [blame] | 113 | |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 114 | bool ContainsControlCharacter(const std::string& s) { |
| 115 | for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) { |
| 116 | if ((*i >= 0) && (*i <= 31)) |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | return false; |
| 121 | } |
| 122 | |
[email protected] | 5b9bc35 | 2012-07-18 13:13:34 | [diff] [blame] | 123 | typedef std::vector<CanonicalCookie*> CanonicalCookieVector; |
[email protected] | 34a160d | 2011-05-12 22:12:49 | [diff] [blame] | 124 | |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 125 | // Default minimum delay after updating a cookie's LastAccessDate before we |
| 126 | // will update it again. |
[email protected] | 297a4ed0 | 2010-02-12 08:12:52 | [diff] [blame] | 127 | const int kDefaultAccessUpdateThresholdSeconds = 60; |
| 128 | |
| 129 | // Comparator to sort cookies from highest creation date to lowest |
| 130 | // creation date. |
| 131 | struct OrderByCreationTimeDesc { |
| 132 | bool operator()(const CookieMonster::CookieMap::iterator& a, |
| 133 | const CookieMonster::CookieMap::iterator& b) const { |
| 134 | return a->second->CreationDate() > b->second->CreationDate(); |
| 135 | } |
| 136 | }; |
| 137 | |
[email protected] | 4d3ce78 | 2010-10-29 18:31:28 | [diff] [blame] | 138 | // Constants for use in VLOG |
| 139 | const int kVlogPerCookieMonster = 1; |
| 140 | const int kVlogPeriodic = 3; |
| 141 | const int kVlogGarbageCollection = 5; |
| 142 | const int kVlogSetCookies = 7; |
| 143 | const int kVlogGetCookies = 9; |
| 144 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 145 | // Mozilla sorts on the path length (longest first), and then it |
| 146 | // sorts by creation time (oldest first). |
| 147 | // The RFC says the sort order for the domain attribute is undefined. |
[email protected] | 5b9bc35 | 2012-07-18 13:13:34 | [diff] [blame] | 148 | bool CookieSorter(CanonicalCookie* cc1, CanonicalCookie* cc2) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 149 | if (cc1->Path().length() == cc2->Path().length()) |
| 150 | return cc1->CreationDate() < cc2->CreationDate(); |
| 151 | return cc1->Path().length() > cc2->Path().length(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 154 | bool LRACookieSorter(const CookieMonster::CookieMap::iterator& it1, |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 155 | const CookieMonster::CookieMap::iterator& it2) { |
| 156 | // Cookies accessed less recently should be deleted first. |
| 157 | if (it1->second->LastAccessDate() != it2->second->LastAccessDate()) |
| 158 | return it1->second->LastAccessDate() < it2->second->LastAccessDate(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 159 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 160 | // In rare cases we might have two cookies with identical last access times. |
| 161 | // To preserve the stability of the sort, in these cases prefer to delete |
| 162 | // older cookies over newer ones. CreationDate() is guaranteed to be unique. |
| 163 | return it1->second->CreationDate() < it2->second->CreationDate(); |
[email protected] | 297a4ed0 | 2010-02-12 08:12:52 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Our strategy to find duplicates is: |
| 167 | // (1) Build a map from (cookiename, cookiepath) to |
| 168 | // {list of cookies with this signature, sorted by creation time}. |
| 169 | // (2) For each list with more than 1 entry, keep the cookie having the |
| 170 | // most recent creation time, and delete the others. |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 171 | // |
[email protected] | 1655ba34 | 2010-07-14 18:17:42 | [diff] [blame] | 172 | // Two cookies are considered equivalent if they have the same domain, |
| 173 | // name, and path. |
| 174 | struct CookieSignature { |
| 175 | public: |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 176 | CookieSignature(const std::string& name, |
| 177 | const std::string& domain, |
[email protected] | 1655ba34 | 2010-07-14 18:17:42 | [diff] [blame] | 178 | const std::string& path) |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 179 | : name(name), domain(domain), path(path) { |
| 180 | } |
[email protected] | 1655ba34 | 2010-07-14 18:17:42 | [diff] [blame] | 181 | |
| 182 | // To be a key for a map this class needs to be assignable, copyable, |
| 183 | // and have an operator<. The default assignment operator |
| 184 | // and copy constructor are exactly what we want. |
| 185 | |
| 186 | bool operator<(const CookieSignature& cs) const { |
| 187 | // Name compare dominates, then domain, then path. |
| 188 | int diff = name.compare(cs.name); |
| 189 | if (diff != 0) |
| 190 | return diff < 0; |
| 191 | |
| 192 | diff = domain.compare(cs.domain); |
| 193 | if (diff != 0) |
| 194 | return diff < 0; |
| 195 | |
| 196 | return path.compare(cs.path) < 0; |
| 197 | } |
| 198 | |
| 199 | std::string name; |
| 200 | std::string domain; |
| 201 | std::string path; |
| 202 | }; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 203 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 204 | // For a CookieItVector iterator range [|it_begin|, |it_end|), |
| 205 | // sorts the first |num_sort| + 1 elements by LastAccessDate(). |
| 206 | // The + 1 element exists so for any interval of length <= |num_sort| starting |
| 207 | // from |cookies_its_begin|, a LastAccessDate() bound can be found. |
| 208 | void SortLeastRecentlyAccessed( |
| 209 | CookieMonster::CookieItVector::iterator it_begin, |
| 210 | CookieMonster::CookieItVector::iterator it_end, |
| 211 | size_t num_sort) { |
| 212 | DCHECK_LT(static_cast<int>(num_sort), it_end - it_begin); |
| 213 | std::partial_sort(it_begin, it_begin + num_sort + 1, it_end, LRACookieSorter); |
| 214 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 215 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 216 | // Predicate to support PartitionCookieByPriority(). |
| 217 | struct CookiePriorityEqualsTo |
| 218 | : std::unary_function<const CookieMonster::CookieMap::iterator, bool> { |
| 219 | CookiePriorityEqualsTo(CookiePriority priority) |
| 220 | : priority_(priority) {} |
| 221 | |
| 222 | bool operator()(const CookieMonster::CookieMap::iterator it) const { |
| 223 | return it->second->Priority() == priority_; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 224 | } |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 225 | |
| 226 | const CookiePriority priority_; |
| 227 | }; |
| 228 | |
| 229 | // For a CookieItVector iterator range [|it_begin|, |it_end|), |
| 230 | // moves all cookies with a given |priority| to the beginning of the list. |
| 231 | // Returns: An iterator in [it_begin, it_end) to the first element with |
| 232 | // priority != |priority|, or |it_end| if all have priority == |priority|. |
| 233 | CookieMonster::CookieItVector::iterator PartitionCookieByPriority( |
| 234 | CookieMonster::CookieItVector::iterator it_begin, |
| 235 | CookieMonster::CookieItVector::iterator it_end, |
| 236 | CookiePriority priority) { |
| 237 | return std::partition(it_begin, it_end, CookiePriorityEqualsTo(priority)); |
| 238 | } |
| 239 | |
| 240 | bool LowerBoundAccessDateComparator( |
| 241 | const CookieMonster::CookieMap::iterator it, const Time& access_date) { |
| 242 | return it->second->LastAccessDate() < access_date; |
| 243 | } |
| 244 | |
| 245 | // For a CookieItVector iterator range [|it_begin|, |it_end|) |
| 246 | // from a CookieItVector sorted by LastAccessDate(), returns the |
| 247 | // first iterator with access date >= |access_date|, or cookie_its_end if this |
| 248 | // holds for all. |
| 249 | CookieMonster::CookieItVector::iterator LowerBoundAccessDate( |
| 250 | const CookieMonster::CookieItVector::iterator its_begin, |
| 251 | const CookieMonster::CookieItVector::iterator its_end, |
| 252 | const Time& access_date) { |
| 253 | return std::lower_bound(its_begin, its_end, access_date, |
| 254 | LowerBoundAccessDateComparator); |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 255 | } |
| 256 | |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 257 | // Mapping between DeletionCause and Delegate::ChangeCause; the mapping also |
| 258 | // provides a boolean that specifies whether or not an OnCookieChanged |
| 259 | // notification ought to be generated. |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 260 | typedef struct ChangeCausePair_struct { |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 261 | CookieMonster::Delegate::ChangeCause cause; |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 262 | bool notify; |
| 263 | } ChangeCausePair; |
| 264 | ChangeCausePair ChangeCauseMapping[] = { |
| 265 | // DELETE_COOKIE_EXPLICIT |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 266 | { CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT, true }, |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 267 | // DELETE_COOKIE_OVERWRITE |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 268 | { CookieMonster::Delegate::CHANGE_COOKIE_OVERWRITE, true }, |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 269 | // DELETE_COOKIE_EXPIRED |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 270 | { CookieMonster::Delegate::CHANGE_COOKIE_EXPIRED, true }, |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 271 | // DELETE_COOKIE_EVICTED |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 272 | { CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true }, |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 273 | // DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 274 | { CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT, false }, |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 275 | // DELETE_COOKIE_DONT_RECORD |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 276 | { CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT, false }, |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 277 | // DELETE_COOKIE_EVICTED_DOMAIN |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 278 | { CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true }, |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 279 | // DELETE_COOKIE_EVICTED_GLOBAL |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 280 | { CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true }, |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 281 | // DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 282 | { CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true }, |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 283 | // DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 284 | { CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true }, |
[email protected] | e7c590e5 | 2011-03-30 08:33:55 | [diff] [blame] | 285 | // DELETE_COOKIE_EXPIRED_OVERWRITE |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 286 | { CookieMonster::Delegate::CHANGE_COOKIE_EXPIRED_OVERWRITE, true }, |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 287 | // DELETE_COOKIE_CONTROL_CHAR |
| 288 | { CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true}, |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 289 | // DELETE_COOKIE_LAST_ENTRY |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 290 | { CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT, false } |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 291 | }; |
| 292 | |
[email protected] | 34a160d | 2011-05-12 22:12:49 | [diff] [blame] | 293 | std::string BuildCookieLine(const CanonicalCookieVector& cookies) { |
| 294 | std::string cookie_line; |
| 295 | for (CanonicalCookieVector::const_iterator it = cookies.begin(); |
| 296 | it != cookies.end(); ++it) { |
| 297 | if (it != cookies.begin()) |
| 298 | cookie_line += "; "; |
| 299 | // In Mozilla if you set a cookie like AAAA, it will have an empty token |
| 300 | // and a value of AAAA. When it sends the cookie back, it will send AAAA, |
| 301 | // so we need to avoid sending =AAAA for a blank token value. |
| 302 | if (!(*it)->Name().empty()) |
| 303 | cookie_line += (*it)->Name() + "="; |
| 304 | cookie_line += (*it)->Value(); |
| 305 | } |
| 306 | return cookie_line; |
| 307 | } |
| 308 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 309 | } // namespace |
| 310 | |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 311 | // static |
| 312 | bool CookieMonster::default_enable_file_scheme_ = false; |
| 313 | |
| 314 | CookieMonster::CookieMonster(PersistentCookieStore* store, Delegate* delegate) |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 315 | : initialized_(false), |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 316 | loaded_(false), |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 317 | store_(store), |
| 318 | last_access_threshold_( |
| 319 | TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)), |
| 320 | delegate_(delegate), |
[email protected] | 8238866 | 2011-03-10 21:04:06 | [diff] [blame] | 321 | last_statistic_record_time_(Time::Now()), |
[email protected] | 93c53a3 | 2011-12-05 10:40:35 | [diff] [blame] | 322 | keep_expired_cookies_(false), |
[email protected] | 8976e29 | 2013-11-02 13:38:57 | [diff] [blame] | 323 | persist_session_cookies_(false) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 324 | InitializeHistograms(); |
| 325 | SetDefaultCookieableSchemes(); |
[email protected] | 2d0f89a | 2010-12-06 12:02:23 | [diff] [blame] | 326 | } |
| 327 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 328 | CookieMonster::CookieMonster(PersistentCookieStore* store, |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 329 | Delegate* delegate, |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 330 | int last_access_threshold_milliseconds) |
| 331 | : initialized_(false), |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 332 | loaded_(false), |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 333 | store_(store), |
| 334 | last_access_threshold_(base::TimeDelta::FromMilliseconds( |
| 335 | last_access_threshold_milliseconds)), |
| 336 | delegate_(delegate), |
[email protected] | 8238866 | 2011-03-10 21:04:06 | [diff] [blame] | 337 | last_statistic_record_time_(base::Time::Now()), |
[email protected] | 93c53a3 | 2011-12-05 10:40:35 | [diff] [blame] | 338 | keep_expired_cookies_(false), |
[email protected] | 8976e29 | 2013-11-02 13:38:57 | [diff] [blame] | 339 | persist_session_cookies_(false) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 340 | InitializeHistograms(); |
| 341 | SetDefaultCookieableSchemes(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 342 | } |
| 343 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 344 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 345 | // Task classes for queueing the coming request. |
| 346 | |
| 347 | class CookieMonster::CookieMonsterTask |
| 348 | : public base::RefCountedThreadSafe<CookieMonsterTask> { |
| 349 | public: |
| 350 | // Runs the task and invokes the client callback on the thread that |
| 351 | // originally constructed the task. |
| 352 | virtual void Run() = 0; |
| 353 | |
| 354 | protected: |
| 355 | explicit CookieMonsterTask(CookieMonster* cookie_monster); |
| 356 | virtual ~CookieMonsterTask(); |
| 357 | |
| 358 | // Invokes the callback immediately, if the current thread is the one |
| 359 | // that originated the task, or queues the callback for execution on the |
| 360 | // appropriate thread. Maintains a reference to this CookieMonsterTask |
| 361 | // instance until the callback completes. |
| 362 | void InvokeCallback(base::Closure callback); |
| 363 | |
| 364 | CookieMonster* cookie_monster() { |
| 365 | return cookie_monster_; |
| 366 | } |
| 367 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 368 | private: |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 369 | friend class base::RefCountedThreadSafe<CookieMonsterTask>; |
| 370 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 371 | CookieMonster* cookie_monster_; |
| 372 | scoped_refptr<base::MessageLoopProxy> thread_; |
| 373 | |
| 374 | DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask); |
| 375 | }; |
| 376 | |
| 377 | CookieMonster::CookieMonsterTask::CookieMonsterTask( |
| 378 | CookieMonster* cookie_monster) |
| 379 | : cookie_monster_(cookie_monster), |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 380 | thread_(base::MessageLoopProxy::current()) { |
| 381 | } |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 382 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 383 | CookieMonster::CookieMonsterTask::~CookieMonsterTask() {} |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 384 | |
| 385 | // Unfortunately, one cannot re-bind a Callback with parameters into a closure. |
| 386 | // Therefore, the closure passed to InvokeCallback is a clumsy binding of |
| 387 | // Callback::Run on a wrapped Callback instance. Since Callback is not |
| 388 | // reference counted, we bind to an instance that is a member of the |
| 389 | // CookieMonsterTask subclass. Then, we cannot simply post the callback to a |
| 390 | // message loop because the underlying instance may be destroyed (along with the |
| 391 | // CookieMonsterTask instance) in the interim. Therefore, we post a callback |
| 392 | // bound to the CookieMonsterTask, which *is* reference counted (thus preventing |
| 393 | // destruction of the original callback), and which invokes the closure (which |
| 394 | // invokes the original callback with the returned data). |
| 395 | void CookieMonster::CookieMonsterTask::InvokeCallback(base::Closure callback) { |
| 396 | if (thread_->BelongsToCurrentThread()) { |
| 397 | callback.Run(); |
| 398 | } else { |
| 399 | thread_->PostTask(FROM_HERE, base::Bind( |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 400 | &CookieMonsterTask::InvokeCallback, this, callback)); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | // Task class for SetCookieWithDetails call. |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 405 | class CookieMonster::SetCookieWithDetailsTask : public CookieMonsterTask { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 406 | public: |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 407 | SetCookieWithDetailsTask(CookieMonster* cookie_monster, |
| 408 | const GURL& url, |
| 409 | const std::string& name, |
| 410 | const std::string& value, |
| 411 | const std::string& domain, |
| 412 | const std::string& path, |
| 413 | const base::Time& expiration_time, |
| 414 | bool secure, |
| 415 | bool http_only, |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 416 | CookiePriority priority, |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 417 | const SetCookiesCallback& callback) |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 418 | : CookieMonsterTask(cookie_monster), |
| 419 | url_(url), |
| 420 | name_(name), |
| 421 | value_(value), |
| 422 | domain_(domain), |
| 423 | path_(path), |
| 424 | expiration_time_(expiration_time), |
| 425 | secure_(secure), |
| 426 | http_only_(http_only), |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 427 | priority_(priority), |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 428 | callback_(callback) { |
| 429 | } |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 430 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 431 | // CookieMonsterTask: |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 432 | virtual void Run() OVERRIDE; |
| 433 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 434 | protected: |
| 435 | virtual ~SetCookieWithDetailsTask() {} |
| 436 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 437 | private: |
| 438 | GURL url_; |
| 439 | std::string name_; |
| 440 | std::string value_; |
| 441 | std::string domain_; |
| 442 | std::string path_; |
| 443 | base::Time expiration_time_; |
| 444 | bool secure_; |
| 445 | bool http_only_; |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 446 | CookiePriority priority_; |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 447 | SetCookiesCallback callback_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 448 | |
| 449 | DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask); |
| 450 | }; |
| 451 | |
| 452 | void CookieMonster::SetCookieWithDetailsTask::Run() { |
| 453 | bool success = this->cookie_monster()-> |
| 454 | SetCookieWithDetails(url_, name_, value_, domain_, path_, |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 455 | expiration_time_, secure_, http_only_, priority_); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 456 | if (!callback_.is_null()) { |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 457 | this->InvokeCallback(base::Bind(&SetCookiesCallback::Run, |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 458 | base::Unretained(&callback_), success)); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | // Task class for GetAllCookies call. |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 463 | class CookieMonster::GetAllCookiesTask : public CookieMonsterTask { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 464 | public: |
| 465 | GetAllCookiesTask(CookieMonster* cookie_monster, |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 466 | const GetCookieListCallback& callback) |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 467 | : CookieMonsterTask(cookie_monster), |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 468 | callback_(callback) { |
| 469 | } |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 470 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 471 | // CookieMonsterTask |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 472 | virtual void Run() OVERRIDE; |
| 473 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 474 | protected: |
| 475 | virtual ~GetAllCookiesTask() {} |
| 476 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 477 | private: |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 478 | GetCookieListCallback callback_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 479 | |
| 480 | DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask); |
| 481 | }; |
| 482 | |
| 483 | void CookieMonster::GetAllCookiesTask::Run() { |
| 484 | if (!callback_.is_null()) { |
| 485 | CookieList cookies = this->cookie_monster()->GetAllCookies(); |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 486 | this->InvokeCallback(base::Bind(&GetCookieListCallback::Run, |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 487 | base::Unretained(&callback_), cookies)); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | // Task class for GetAllCookiesForURLWithOptions call. |
| 492 | class CookieMonster::GetAllCookiesForURLWithOptionsTask |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 493 | : public CookieMonsterTask { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 494 | public: |
| 495 | GetAllCookiesForURLWithOptionsTask( |
| 496 | CookieMonster* cookie_monster, |
| 497 | const GURL& url, |
| 498 | const CookieOptions& options, |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 499 | const GetCookieListCallback& callback) |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 500 | : CookieMonsterTask(cookie_monster), |
| 501 | url_(url), |
| 502 | options_(options), |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 503 | callback_(callback) { |
| 504 | } |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 505 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 506 | // CookieMonsterTask: |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 507 | virtual void Run() OVERRIDE; |
| 508 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 509 | protected: |
| 510 | virtual ~GetAllCookiesForURLWithOptionsTask() {} |
| 511 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 512 | private: |
| 513 | GURL url_; |
| 514 | CookieOptions options_; |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 515 | GetCookieListCallback callback_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 516 | |
| 517 | DISALLOW_COPY_AND_ASSIGN(GetAllCookiesForURLWithOptionsTask); |
| 518 | }; |
| 519 | |
| 520 | void CookieMonster::GetAllCookiesForURLWithOptionsTask::Run() { |
| 521 | if (!callback_.is_null()) { |
| 522 | CookieList cookies = this->cookie_monster()-> |
| 523 | GetAllCookiesForURLWithOptions(url_, options_); |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 524 | this->InvokeCallback(base::Bind(&GetCookieListCallback::Run, |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 525 | base::Unretained(&callback_), cookies)); |
| 526 | } |
| 527 | } |
| 528 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 529 | template <typename Result> struct CallbackType { |
| 530 | typedef base::Callback<void(Result)> Type; |
| 531 | }; |
| 532 | |
| 533 | template <> struct CallbackType<void> { |
| 534 | typedef base::Closure Type; |
| 535 | }; |
| 536 | |
| 537 | // Base task class for Delete*Task. |
| 538 | template <typename Result> |
| 539 | class CookieMonster::DeleteTask : public CookieMonsterTask { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 540 | public: |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 541 | DeleteTask(CookieMonster* cookie_monster, |
| 542 | const typename CallbackType<Result>::Type& callback) |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 543 | : CookieMonsterTask(cookie_monster), |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 544 | callback_(callback) { |
| 545 | } |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 546 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 547 | // CookieMonsterTask: |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 548 | virtual void Run() OVERRIDE; |
| 549 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 550 | private: |
| 551 | // Runs the delete task and returns a result. |
| 552 | virtual Result RunDeleteTask() = 0; |
| 553 | base::Closure RunDeleteTaskAndBindCallback(); |
| 554 | void FlushDone(const base::Closure& callback); |
| 555 | |
| 556 | typename CallbackType<Result>::Type callback_; |
| 557 | |
| 558 | DISALLOW_COPY_AND_ASSIGN(DeleteTask); |
| 559 | }; |
| 560 | |
| 561 | template <typename Result> |
| 562 | base::Closure CookieMonster::DeleteTask<Result>:: |
| 563 | RunDeleteTaskAndBindCallback() { |
| 564 | Result result = RunDeleteTask(); |
| 565 | if (callback_.is_null()) |
| 566 | return base::Closure(); |
| 567 | return base::Bind(callback_, result); |
| 568 | } |
| 569 | |
| 570 | template <> |
| 571 | base::Closure CookieMonster::DeleteTask<void>::RunDeleteTaskAndBindCallback() { |
| 572 | RunDeleteTask(); |
| 573 | return callback_; |
| 574 | } |
| 575 | |
| 576 | template <typename Result> |
| 577 | void CookieMonster::DeleteTask<Result>::Run() { |
| 578 | this->cookie_monster()->FlushStore( |
| 579 | base::Bind(&DeleteTask<Result>::FlushDone, this, |
| 580 | RunDeleteTaskAndBindCallback())); |
| 581 | } |
| 582 | |
| 583 | template <typename Result> |
| 584 | void CookieMonster::DeleteTask<Result>::FlushDone( |
| 585 | const base::Closure& callback) { |
| 586 | if (!callback.is_null()) { |
| 587 | this->InvokeCallback(callback); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | // Task class for DeleteAll call. |
| 592 | class CookieMonster::DeleteAllTask : public DeleteTask<int> { |
| 593 | public: |
| 594 | DeleteAllTask(CookieMonster* cookie_monster, |
| 595 | const DeleteCallback& callback) |
[email protected] | 151132f | 2013-11-18 21:37:00 | [diff] [blame^] | 596 | : DeleteTask<int>(cookie_monster, callback) { |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | // DeleteTask: |
| 600 | virtual int RunDeleteTask() OVERRIDE; |
| 601 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 602 | protected: |
| 603 | virtual ~DeleteAllTask() {} |
| 604 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 605 | private: |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 606 | DISALLOW_COPY_AND_ASSIGN(DeleteAllTask); |
| 607 | }; |
| 608 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 609 | int CookieMonster::DeleteAllTask::RunDeleteTask() { |
| 610 | return this->cookie_monster()->DeleteAll(true); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | // Task class for DeleteAllCreatedBetween call. |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 614 | class CookieMonster::DeleteAllCreatedBetweenTask : public DeleteTask<int> { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 615 | public: |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 616 | DeleteAllCreatedBetweenTask(CookieMonster* cookie_monster, |
| 617 | const Time& delete_begin, |
| 618 | const Time& delete_end, |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 619 | const DeleteCallback& callback) |
[email protected] | 151132f | 2013-11-18 21:37:00 | [diff] [blame^] | 620 | : DeleteTask<int>(cookie_monster, callback), |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 621 | delete_begin_(delete_begin), |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 622 | delete_end_(delete_end) { |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 623 | } |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 624 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 625 | // DeleteTask: |
| 626 | virtual int RunDeleteTask() OVERRIDE; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 627 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 628 | protected: |
| 629 | virtual ~DeleteAllCreatedBetweenTask() {} |
| 630 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 631 | private: |
| 632 | Time delete_begin_; |
| 633 | Time delete_end_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 634 | |
| 635 | DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask); |
| 636 | }; |
| 637 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 638 | int CookieMonster::DeleteAllCreatedBetweenTask::RunDeleteTask() { |
| 639 | return this->cookie_monster()-> |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 640 | DeleteAllCreatedBetween(delete_begin_, delete_end_); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | // Task class for DeleteAllForHost call. |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 644 | class CookieMonster::DeleteAllForHostTask : public DeleteTask<int> { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 645 | public: |
| 646 | DeleteAllForHostTask(CookieMonster* cookie_monster, |
| 647 | const GURL& url, |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 648 | const DeleteCallback& callback) |
[email protected] | 151132f | 2013-11-18 21:37:00 | [diff] [blame^] | 649 | : DeleteTask<int>(cookie_monster, callback), |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 650 | url_(url) { |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 651 | } |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 652 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 653 | // DeleteTask: |
| 654 | virtual int RunDeleteTask() OVERRIDE; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 655 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 656 | protected: |
| 657 | virtual ~DeleteAllForHostTask() {} |
| 658 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 659 | private: |
| 660 | GURL url_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 661 | |
| 662 | DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask); |
| 663 | }; |
| 664 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 665 | int CookieMonster::DeleteAllForHostTask::RunDeleteTask() { |
| 666 | return this->cookie_monster()->DeleteAllForHost(url_); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 667 | } |
| 668 | |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 669 | // Task class for DeleteAllCreatedBetweenForHost call. |
| 670 | class CookieMonster::DeleteAllCreatedBetweenForHostTask |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 671 | : public DeleteTask<int> { |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 672 | public: |
| 673 | DeleteAllCreatedBetweenForHostTask( |
| 674 | CookieMonster* cookie_monster, |
| 675 | Time delete_begin, |
| 676 | Time delete_end, |
| 677 | const GURL& url, |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 678 | const DeleteCallback& callback) |
[email protected] | 151132f | 2013-11-18 21:37:00 | [diff] [blame^] | 679 | : DeleteTask<int>(cookie_monster, callback), |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 680 | delete_begin_(delete_begin), |
| 681 | delete_end_(delete_end), |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 682 | url_(url) { |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 683 | } |
| 684 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 685 | // DeleteTask: |
| 686 | virtual int RunDeleteTask() OVERRIDE; |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 687 | |
| 688 | protected: |
| 689 | virtual ~DeleteAllCreatedBetweenForHostTask() {} |
| 690 | |
| 691 | private: |
| 692 | Time delete_begin_; |
| 693 | Time delete_end_; |
| 694 | GURL url_; |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 695 | |
| 696 | DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenForHostTask); |
| 697 | }; |
| 698 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 699 | int CookieMonster::DeleteAllCreatedBetweenForHostTask::RunDeleteTask() { |
| 700 | return this->cookie_monster()->DeleteAllCreatedBetweenForHost( |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 701 | delete_begin_, delete_end_, url_); |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 702 | } |
| 703 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 704 | // Task class for DeleteCanonicalCookie call. |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 705 | class CookieMonster::DeleteCanonicalCookieTask : public DeleteTask<bool> { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 706 | public: |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 707 | DeleteCanonicalCookieTask(CookieMonster* cookie_monster, |
| 708 | const CanonicalCookie& cookie, |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 709 | const DeleteCookieCallback& callback) |
[email protected] | 151132f | 2013-11-18 21:37:00 | [diff] [blame^] | 710 | : DeleteTask<bool>(cookie_monster, callback), |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 711 | cookie_(cookie) { |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 712 | } |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 713 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 714 | // DeleteTask: |
| 715 | virtual bool RunDeleteTask() OVERRIDE; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 716 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 717 | protected: |
| 718 | virtual ~DeleteCanonicalCookieTask() {} |
| 719 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 720 | private: |
[email protected] | 5b9bc35 | 2012-07-18 13:13:34 | [diff] [blame] | 721 | CanonicalCookie cookie_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 722 | |
| 723 | DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask); |
| 724 | }; |
| 725 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 726 | bool CookieMonster::DeleteCanonicalCookieTask::RunDeleteTask() { |
| 727 | return this->cookie_monster()->DeleteCanonicalCookie(cookie_); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | // Task class for SetCookieWithOptions call. |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 731 | class CookieMonster::SetCookieWithOptionsTask : public CookieMonsterTask { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 732 | public: |
| 733 | SetCookieWithOptionsTask(CookieMonster* cookie_monster, |
| 734 | const GURL& url, |
| 735 | const std::string& cookie_line, |
| 736 | const CookieOptions& options, |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 737 | const SetCookiesCallback& callback) |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 738 | : CookieMonsterTask(cookie_monster), |
| 739 | url_(url), |
| 740 | cookie_line_(cookie_line), |
| 741 | options_(options), |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 742 | callback_(callback) { |
| 743 | } |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 744 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 745 | // CookieMonsterTask: |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 746 | virtual void Run() OVERRIDE; |
| 747 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 748 | protected: |
| 749 | virtual ~SetCookieWithOptionsTask() {} |
| 750 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 751 | private: |
| 752 | GURL url_; |
| 753 | std::string cookie_line_; |
| 754 | CookieOptions options_; |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 755 | SetCookiesCallback callback_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 756 | |
| 757 | DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask); |
| 758 | }; |
| 759 | |
| 760 | void CookieMonster::SetCookieWithOptionsTask::Run() { |
| 761 | bool result = this->cookie_monster()-> |
| 762 | SetCookieWithOptions(url_, cookie_line_, options_); |
| 763 | if (!callback_.is_null()) { |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 764 | this->InvokeCallback(base::Bind(&SetCookiesCallback::Run, |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 765 | base::Unretained(&callback_), result)); |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | // Task class for GetCookiesWithOptions call. |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 770 | class CookieMonster::GetCookiesWithOptionsTask : public CookieMonsterTask { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 771 | public: |
| 772 | GetCookiesWithOptionsTask(CookieMonster* cookie_monster, |
[email protected] | 0298caf8 | 2011-12-20 23:15:46 | [diff] [blame] | 773 | const GURL& url, |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 774 | const CookieOptions& options, |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 775 | const GetCookiesCallback& callback) |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 776 | : CookieMonsterTask(cookie_monster), |
| 777 | url_(url), |
| 778 | options_(options), |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 779 | callback_(callback) { |
| 780 | } |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 781 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 782 | // CookieMonsterTask: |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 783 | virtual void Run() OVERRIDE; |
| 784 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 785 | protected: |
| 786 | virtual ~GetCookiesWithOptionsTask() {} |
| 787 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 788 | private: |
| 789 | GURL url_; |
| 790 | CookieOptions options_; |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 791 | GetCookiesCallback callback_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 792 | |
| 793 | DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask); |
| 794 | }; |
| 795 | |
| 796 | void CookieMonster::GetCookiesWithOptionsTask::Run() { |
| 797 | std::string cookie = this->cookie_monster()-> |
| 798 | GetCookiesWithOptions(url_, options_); |
| 799 | if (!callback_.is_null()) { |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 800 | this->InvokeCallback(base::Bind(&GetCookiesCallback::Run, |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 801 | base::Unretained(&callback_), cookie)); |
| 802 | } |
| 803 | } |
| 804 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 805 | // Task class for DeleteCookie call. |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 806 | class CookieMonster::DeleteCookieTask : public DeleteTask<void> { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 807 | public: |
| 808 | DeleteCookieTask(CookieMonster* cookie_monster, |
[email protected] | 0298caf8 | 2011-12-20 23:15:46 | [diff] [blame] | 809 | const GURL& url, |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 810 | const std::string& cookie_name, |
| 811 | const base::Closure& callback) |
[email protected] | 151132f | 2013-11-18 21:37:00 | [diff] [blame^] | 812 | : DeleteTask<void>(cookie_monster, callback), |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 813 | url_(url), |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 814 | cookie_name_(cookie_name) { |
| 815 | } |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 816 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 817 | // DeleteTask: |
| 818 | virtual void RunDeleteTask() OVERRIDE; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 819 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 820 | protected: |
| 821 | virtual ~DeleteCookieTask() {} |
| 822 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 823 | private: |
| 824 | GURL url_; |
| 825 | std::string cookie_name_; |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 826 | |
| 827 | DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask); |
| 828 | }; |
| 829 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 830 | void CookieMonster::DeleteCookieTask::RunDeleteTask() { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 831 | this->cookie_monster()->DeleteCookie(url_, cookie_name_); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 832 | } |
| 833 | |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 834 | // Task class for DeleteSessionCookies call. |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 835 | class CookieMonster::DeleteSessionCookiesTask : public DeleteTask<int> { |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 836 | public: |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 837 | DeleteSessionCookiesTask(CookieMonster* cookie_monster, |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 838 | const DeleteCallback& callback) |
[email protected] | 151132f | 2013-11-18 21:37:00 | [diff] [blame^] | 839 | : DeleteTask<int>(cookie_monster, callback) { |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 840 | } |
[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: |
| 843 | virtual 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: |
| 846 | virtual ~DeleteSessionCookiesTask() {} |
| 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 | |
| 850 | DISALLOW_COPY_AND_ASSIGN(DeleteSessionCookiesTask); |
| 851 | }; |
| 852 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 853 | int CookieMonster::DeleteSessionCookiesTask::RunDeleteTask() { |
| 854 | return this->cookie_monster()->DeleteSessionCookies(); |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 855 | } |
| 856 | |
[email protected] | ee20948 | 2013-04-19 19:50:04 | [diff] [blame] | 857 | // Task class for HasCookiesForETLDP1Task call. |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 858 | class CookieMonster::HasCookiesForETLDP1Task : public CookieMonsterTask { |
[email protected] | ee20948 | 2013-04-19 19:50:04 | [diff] [blame] | 859 | public: |
| 860 | HasCookiesForETLDP1Task( |
| 861 | CookieMonster* cookie_monster, |
| 862 | const std::string& etldp1, |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 863 | const HasCookiesForETLDP1Callback& callback) |
[email protected] | ee20948 | 2013-04-19 19:50:04 | [diff] [blame] | 864 | : CookieMonsterTask(cookie_monster), |
| 865 | etldp1_(etldp1), |
| 866 | callback_(callback) { |
| 867 | } |
| 868 | |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 869 | // CookieMonsterTask: |
[email protected] | ee20948 | 2013-04-19 19:50:04 | [diff] [blame] | 870 | virtual void Run() OVERRIDE; |
| 871 | |
| 872 | protected: |
| 873 | virtual ~HasCookiesForETLDP1Task() {} |
| 874 | |
| 875 | private: |
| 876 | std::string etldp1_; |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 877 | HasCookiesForETLDP1Callback callback_; |
[email protected] | ee20948 | 2013-04-19 19:50:04 | [diff] [blame] | 878 | |
| 879 | DISALLOW_COPY_AND_ASSIGN(HasCookiesForETLDP1Task); |
| 880 | }; |
| 881 | |
| 882 | void CookieMonster::HasCookiesForETLDP1Task::Run() { |
| 883 | bool result = this->cookie_monster()->HasCookiesForETLDP1(etldp1_); |
| 884 | if (!callback_.is_null()) { |
| 885 | this->InvokeCallback( |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 886 | base::Bind(&HasCookiesForETLDP1Callback::Run, |
[email protected] | ee20948 | 2013-04-19 19:50:04 | [diff] [blame] | 887 | base::Unretained(&callback_), result)); |
| 888 | } |
| 889 | } |
| 890 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 891 | // Asynchronous CookieMonster API |
| 892 | |
| 893 | void CookieMonster::SetCookieWithDetailsAsync( |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 894 | const GURL& url, |
| 895 | const std::string& name, |
| 896 | const std::string& value, |
| 897 | const std::string& domain, |
| 898 | const std::string& path, |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 899 | const Time& expiration_time, |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 900 | bool secure, |
| 901 | bool http_only, |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 902 | CookiePriority priority, |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 903 | const SetCookiesCallback& callback) { |
| 904 | scoped_refptr<SetCookieWithDetailsTask> task = |
| 905 | new SetCookieWithDetailsTask(this, url, name, value, domain, path, |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 906 | expiration_time, secure, http_only, priority, |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 907 | callback); |
| 908 | |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 909 | DoCookieTaskForURL(task, url); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) { |
| 913 | scoped_refptr<GetAllCookiesTask> task = |
| 914 | new GetAllCookiesTask(this, callback); |
| 915 | |
| 916 | DoCookieTask(task); |
| 917 | } |
| 918 | |
| 919 | |
| 920 | void CookieMonster::GetAllCookiesForURLWithOptionsAsync( |
| 921 | const GURL& url, |
| 922 | const CookieOptions& options, |
| 923 | const GetCookieListCallback& callback) { |
| 924 | scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = |
| 925 | new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); |
| 926 | |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 927 | DoCookieTaskForURL(task, url); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | void CookieMonster::GetAllCookiesForURLAsync( |
| 931 | const GURL& url, const GetCookieListCallback& callback) { |
| 932 | CookieOptions options; |
| 933 | options.set_include_httponly(); |
| 934 | scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = |
| 935 | new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); |
| 936 | |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 937 | DoCookieTaskForURL(task, url); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 938 | } |
| 939 | |
[email protected] | ee20948 | 2013-04-19 19:50:04 | [diff] [blame] | 940 | void CookieMonster::HasCookiesForETLDP1Async( |
| 941 | const std::string& etldp1, |
| 942 | const HasCookiesForETLDP1Callback& callback) { |
| 943 | scoped_refptr<HasCookiesForETLDP1Task> task = |
| 944 | new HasCookiesForETLDP1Task(this, etldp1, callback); |
| 945 | |
| 946 | DoCookieTaskForURL(task, GURL("http://" + etldp1)); |
| 947 | } |
| 948 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 949 | void CookieMonster::DeleteAllAsync(const DeleteCallback& callback) { |
| 950 | scoped_refptr<DeleteAllTask> task = |
| 951 | new DeleteAllTask(this, callback); |
| 952 | |
| 953 | DoCookieTask(task); |
| 954 | } |
| 955 | |
| 956 | void CookieMonster::DeleteAllCreatedBetweenAsync( |
| 957 | const Time& delete_begin, const Time& delete_end, |
| 958 | const DeleteCallback& callback) { |
| 959 | scoped_refptr<DeleteAllCreatedBetweenTask> task = |
| 960 | new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end, |
| 961 | callback); |
| 962 | |
| 963 | DoCookieTask(task); |
| 964 | } |
| 965 | |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 966 | void CookieMonster::DeleteAllCreatedBetweenForHostAsync( |
| 967 | const Time delete_begin, |
| 968 | const Time delete_end, |
| 969 | const GURL& url, |
| 970 | const DeleteCallback& callback) { |
| 971 | scoped_refptr<DeleteAllCreatedBetweenForHostTask> task = |
| 972 | new DeleteAllCreatedBetweenForHostTask( |
| 973 | this, delete_begin, delete_end, url, callback); |
| 974 | |
| 975 | DoCookieTaskForURL(task, url); |
| 976 | } |
| 977 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 978 | void CookieMonster::DeleteAllForHostAsync( |
| 979 | const GURL& url, const DeleteCallback& callback) { |
| 980 | scoped_refptr<DeleteAllForHostTask> task = |
| 981 | new DeleteAllForHostTask(this, url, callback); |
| 982 | |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 983 | DoCookieTaskForURL(task, url); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | void CookieMonster::DeleteCanonicalCookieAsync( |
| 987 | const CanonicalCookie& cookie, |
| 988 | const DeleteCookieCallback& callback) { |
| 989 | scoped_refptr<DeleteCanonicalCookieTask> task = |
| 990 | new DeleteCanonicalCookieTask(this, cookie, callback); |
| 991 | |
| 992 | DoCookieTask(task); |
| 993 | } |
| 994 | |
| 995 | void CookieMonster::SetCookieWithOptionsAsync( |
| 996 | const GURL& url, |
| 997 | const std::string& cookie_line, |
| 998 | const CookieOptions& options, |
| 999 | const SetCookiesCallback& callback) { |
| 1000 | scoped_refptr<SetCookieWithOptionsTask> task = |
| 1001 | new SetCookieWithOptionsTask(this, url, cookie_line, options, callback); |
| 1002 | |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1003 | DoCookieTaskForURL(task, url); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | void CookieMonster::GetCookiesWithOptionsAsync( |
| 1007 | const GURL& url, |
| 1008 | const CookieOptions& options, |
| 1009 | const GetCookiesCallback& callback) { |
| 1010 | scoped_refptr<GetCookiesWithOptionsTask> task = |
| 1011 | new GetCookiesWithOptionsTask(this, url, options, callback); |
| 1012 | |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1013 | DoCookieTaskForURL(task, url); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1014 | } |
| 1015 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1016 | void CookieMonster::DeleteCookieAsync(const GURL& url, |
| 1017 | const std::string& cookie_name, |
| 1018 | const base::Closure& callback) { |
| 1019 | scoped_refptr<DeleteCookieTask> task = |
| 1020 | new DeleteCookieTask(this, url, cookie_name, callback); |
| 1021 | |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1022 | DoCookieTaskForURL(task, url); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1023 | } |
| 1024 | |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 1025 | void CookieMonster::DeleteSessionCookiesAsync( |
| 1026 | const CookieStore::DeleteCallback& callback) { |
| 1027 | scoped_refptr<DeleteSessionCookiesTask> task = |
| 1028 | new DeleteSessionCookiesTask(this, callback); |
| 1029 | |
| 1030 | DoCookieTask(task); |
| 1031 | } |
| 1032 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1033 | void CookieMonster::DoCookieTask( |
| 1034 | const scoped_refptr<CookieMonsterTask>& task_item) { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1035 | { |
| 1036 | base::AutoLock autolock(lock_); |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1037 | InitIfNecessary(); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1038 | if (!loaded_) { |
[email protected] | 0184df3 | 2013-05-14 00:53:55 | [diff] [blame] | 1039 | tasks_pending_.push(task_item); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1040 | return; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | task_item->Run(); |
| 1045 | } |
| 1046 | |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1047 | void CookieMonster::DoCookieTaskForURL( |
| 1048 | const scoped_refptr<CookieMonsterTask>& task_item, |
| 1049 | const GURL& url) { |
| 1050 | { |
| 1051 | base::AutoLock autolock(lock_); |
| 1052 | InitIfNecessary(); |
| 1053 | // If cookies for the requested domain key (eTLD+1) have been loaded from DB |
| 1054 | // then run the task, otherwise load from DB. |
| 1055 | if (!loaded_) { |
| 1056 | // Checks if the domain key has been loaded. |
[email protected] | 2fb376a | 2011-11-17 09:22:03 | [diff] [blame] | 1057 | std::string key(cookie_util::GetEffectiveDomain(url.scheme(), |
| 1058 | url.host())); |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1059 | if (keys_loaded_.find(key) == keys_loaded_.end()) { |
| 1060 | std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > > |
[email protected] | 0184df3 | 2013-05-14 00:53:55 | [diff] [blame] | 1061 | ::iterator it = tasks_pending_for_key_.find(key); |
| 1062 | if (it == tasks_pending_for_key_.end()) { |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1063 | store_->LoadCookiesForKey(key, |
| 1064 | base::Bind(&CookieMonster::OnKeyLoaded, this, key)); |
[email protected] | 0184df3 | 2013-05-14 00:53:55 | [diff] [blame] | 1065 | it = tasks_pending_for_key_.insert(std::make_pair(key, |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1066 | std::deque<scoped_refptr<CookieMonsterTask> >())).first; |
| 1067 | } |
| 1068 | it->second.push_back(task_item); |
| 1069 | return; |
| 1070 | } |
| 1071 | } |
| 1072 | } |
| 1073 | task_item->Run(); |
| 1074 | } |
| 1075 | |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 1076 | bool CookieMonster::SetCookieWithDetails(const GURL& url, |
| 1077 | const std::string& name, |
| 1078 | const std::string& value, |
| 1079 | const std::string& domain, |
| 1080 | const std::string& path, |
| 1081 | const base::Time& expiration_time, |
| 1082 | bool secure, |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 1083 | bool http_only, |
| 1084 | CookiePriority priority) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 1085 | base::AutoLock autolock(lock_); |
[email protected] | 69bb587 | 2010-01-12 20:33:52 | [diff] [blame] | 1086 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1087 | if (!HasCookieableScheme(url)) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1088 | return false; |
| 1089 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1090 | Time creation_time = CurrentTime(); |
| 1091 | last_time_seen_ = creation_time; |
| 1092 | |
| 1093 | scoped_ptr<CanonicalCookie> cc; |
[email protected] | ab2d75c8 | 2013-04-19 18:39:04 | [diff] [blame] | 1094 | cc.reset(CanonicalCookie::Create(url, name, value, domain, path, |
| 1095 | creation_time, expiration_time, |
| 1096 | secure, http_only, priority)); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1097 | |
| 1098 | if (!cc.get()) |
| 1099 | return false; |
| 1100 | |
| 1101 | CookieOptions options; |
| 1102 | options.set_include_httponly(); |
| 1103 | return SetCanonicalCookie(&cc, creation_time, options); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1104 | } |
| 1105 | |
[email protected] | 30fa68c5 | 2011-08-12 20:15:36 | [diff] [blame] | 1106 | bool CookieMonster::InitializeFrom(const CookieList& list) { |
[email protected] | 93460df | 2011-07-20 00:58:21 | [diff] [blame] | 1107 | base::AutoLock autolock(lock_); |
| 1108 | InitIfNecessary(); |
| 1109 | for (net::CookieList::const_iterator iter = list.begin(); |
| 1110 | iter != list.end(); ++iter) { |
[email protected] | 5b9bc35 | 2012-07-18 13:13:34 | [diff] [blame] | 1111 | scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie(*iter)); |
[email protected] | 93460df | 2011-07-20 00:58:21 | [diff] [blame] | 1112 | net::CookieOptions options; |
| 1113 | options.set_include_httponly(); |
[email protected] | 5b9bc35 | 2012-07-18 13:13:34 | [diff] [blame] | 1114 | if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), options)) |
[email protected] | 93460df | 2011-07-20 00:58:21 | [diff] [blame] | 1115 | return false; |
[email protected] | 93460df | 2011-07-20 00:58:21 | [diff] [blame] | 1116 | } |
| 1117 | return true; |
| 1118 | } |
| 1119 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1120 | CookieList CookieMonster::GetAllCookies() { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 1121 | base::AutoLock autolock(lock_); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1122 | |
| 1123 | // This function is being called to scrape the cookie list for management UI |
| 1124 | // or similar. We shouldn't show expired cookies in this list since it will |
| 1125 | // just be confusing to users, and this function is called rarely enough (and |
| 1126 | // is already slow enough) that it's OK to take the time to garbage collect |
| 1127 | // the expired cookies now. |
| 1128 | // |
| 1129 | // Note that this does not prune cookies to be below our limits (if we've |
| 1130 | // exceeded them) the way that calling GarbageCollect() would. |
| 1131 | GarbageCollectExpired(Time::Now(), |
| 1132 | CookieMapItPair(cookies_.begin(), cookies_.end()), |
| 1133 | NULL); |
| 1134 | |
| 1135 | // Copy the CanonicalCookie pointers from the map so that we can use the same |
| 1136 | // sorter as elsewhere, then copy the result out. |
| 1137 | std::vector<CanonicalCookie*> cookie_ptrs; |
| 1138 | cookie_ptrs.reserve(cookies_.size()); |
| 1139 | for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end(); ++it) |
| 1140 | cookie_ptrs.push_back(it->second); |
| 1141 | std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); |
| 1142 | |
| 1143 | CookieList cookie_list; |
| 1144 | cookie_list.reserve(cookie_ptrs.size()); |
| 1145 | for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); |
| 1146 | it != cookie_ptrs.end(); ++it) |
| 1147 | cookie_list.push_back(**it); |
| 1148 | |
| 1149 | return cookie_list; |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame] | 1150 | } |
| 1151 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1152 | CookieList CookieMonster::GetAllCookiesForURLWithOptions( |
| 1153 | const GURL& url, |
| 1154 | const CookieOptions& options) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 1155 | base::AutoLock autolock(lock_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1156 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1157 | std::vector<CanonicalCookie*> cookie_ptrs; |
| 1158 | FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs); |
| 1159 | std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1160 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1161 | CookieList cookies; |
| 1162 | for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); |
| 1163 | it != cookie_ptrs.end(); it++) |
| 1164 | cookies.push_back(**it); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1165 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1166 | return cookies; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1167 | } |
| 1168 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1169 | CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) { |
| 1170 | CookieOptions options; |
| 1171 | options.set_include_httponly(); |
| 1172 | |
| 1173 | return GetAllCookiesForURLWithOptions(url, options); |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame] | 1174 | } |
| 1175 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1176 | int CookieMonster::DeleteAll(bool sync_to_store) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 1177 | base::AutoLock autolock(lock_); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1178 | |
| 1179 | int num_deleted = 0; |
| 1180 | for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 1181 | CookieMap::iterator curit = it; |
| 1182 | ++it; |
| 1183 | InternalDeleteCookie(curit, sync_to_store, |
| 1184 | sync_to_store ? DELETE_COOKIE_EXPLICIT : |
| 1185 | DELETE_COOKIE_DONT_RECORD /* Destruction. */); |
| 1186 | ++num_deleted; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1187 | } |
| 1188 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1189 | return num_deleted; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1190 | } |
| 1191 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1192 | int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin, |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1193 | const Time& delete_end) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 1194 | base::AutoLock autolock(lock_); |
[email protected] | d098033 | 2010-11-16 17:08:53 | [diff] [blame] | 1195 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1196 | int num_deleted = 0; |
| 1197 | for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 1198 | CookieMap::iterator curit = it; |
| 1199 | CanonicalCookie* cc = curit->second; |
| 1200 | ++it; |
[email protected] | d098033 | 2010-11-16 17:08:53 | [diff] [blame] | 1201 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1202 | if (cc->CreationDate() >= delete_begin && |
| 1203 | (delete_end.is_null() || cc->CreationDate() < delete_end)) { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1204 | InternalDeleteCookie(curit, |
| 1205 | true, /*sync_to_store*/ |
| 1206 | DELETE_COOKIE_EXPLICIT); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1207 | ++num_deleted; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1208 | } |
| 1209 | } |
| 1210 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1211 | return num_deleted; |
| 1212 | } |
| 1213 | |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 1214 | int CookieMonster::DeleteAllCreatedBetweenForHost(const Time delete_begin, |
| 1215 | const Time delete_end, |
| 1216 | const GURL& url) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 1217 | base::AutoLock autolock(lock_); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1218 | |
| 1219 | if (!HasCookieableScheme(url)) |
| 1220 | return 0; |
| 1221 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1222 | const std::string host(url.host()); |
| 1223 | |
| 1224 | // We store host cookies in the store by their canonical host name; |
| 1225 | // domain cookies are stored with a leading ".". So this is a pretty |
| 1226 | // simple lookup and per-cookie delete. |
| 1227 | int num_deleted = 0; |
| 1228 | for (CookieMapItPair its = cookies_.equal_range(GetKey(host)); |
| 1229 | its.first != its.second;) { |
| 1230 | CookieMap::iterator curit = its.first; |
| 1231 | ++its.first; |
| 1232 | |
| 1233 | const CanonicalCookie* const cc = curit->second; |
| 1234 | |
| 1235 | // Delete only on a match as a host cookie. |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 1236 | if (cc->IsHostCookie() && cc->IsDomainMatch(host) && |
| 1237 | cc->CreationDate() >= delete_begin && |
| 1238 | // The assumption that null |delete_end| is equivalent to |
| 1239 | // Time::Max() is confusing. |
| 1240 | (delete_end.is_null() || cc->CreationDate() < delete_end)) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1241 | num_deleted++; |
| 1242 | |
| 1243 | InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); |
| 1244 | } |
| 1245 | } |
| 1246 | return num_deleted; |
| 1247 | } |
| 1248 | |
[email protected] | d8428d5 | 2013-08-07 06:58:25 | [diff] [blame] | 1249 | int CookieMonster::DeleteAllForHost(const GURL& url) { |
| 1250 | return DeleteAllCreatedBetweenForHost(Time(), Time::Max(), url); |
| 1251 | } |
| 1252 | |
| 1253 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1254 | bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 1255 | base::AutoLock autolock(lock_); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1256 | |
| 1257 | for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); |
| 1258 | its.first != its.second; ++its.first) { |
| 1259 | // The creation date acts as our unique index... |
| 1260 | if (its.first->second->CreationDate() == cookie.CreationDate()) { |
| 1261 | InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT); |
| 1262 | return true; |
| 1263 | } |
| 1264 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1265 | return false; |
| 1266 | } |
| 1267 | |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 1268 | void CookieMonster::SetCookieableSchemes(const char* schemes[], |
| 1269 | size_t num_schemes) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 1270 | base::AutoLock autolock(lock_); |
[email protected] | bb890572 | 2010-05-21 17:29:04 | [diff] [blame] | 1271 | |
[email protected] | cf12bd1 | 2010-06-17 14:41:30 | [diff] [blame] | 1272 | // Cookieable Schemes must be set before first use of function. |
| 1273 | DCHECK(!initialized_); |
| 1274 | |
[email protected] | 47accfd6 | 2009-05-14 18:46:21 | [diff] [blame] | 1275 | cookieable_schemes_.clear(); |
| 1276 | cookieable_schemes_.insert(cookieable_schemes_.end(), |
| 1277 | schemes, schemes + num_schemes); |
| 1278 | } |
| 1279 | |
[email protected] | 97a3b6e | 2012-06-12 01:53:56 | [diff] [blame] | 1280 | void CookieMonster::SetEnableFileScheme(bool accept) { |
| 1281 | // This assumes "file" is always at the end of the array. See the comment |
| 1282 | // above kDefaultCookieableSchemes. |
| 1283 | int num_schemes = accept ? kDefaultCookieableSchemesCount : |
| 1284 | kDefaultCookieableSchemesCount - 1; |
| 1285 | SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes); |
| 1286 | } |
| 1287 | |
[email protected] | ba4ad0e | 2011-03-15 08:12:47 | [diff] [blame] | 1288 | void CookieMonster::SetKeepExpiredCookies() { |
| 1289 | keep_expired_cookies_ = true; |
| 1290 | } |
| 1291 | |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 1292 | // static |
| 1293 | void CookieMonster::EnableFileScheme() { |
| 1294 | default_enable_file_scheme_ = true; |
| 1295 | } |
| 1296 | |
[email protected] | e67f0f4 | 2011-12-20 02:29:21 | [diff] [blame] | 1297 | void CookieMonster::FlushStore(const base::Closure& callback) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 1298 | base::AutoLock autolock(lock_); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1299 | if (initialized_ && store_.get()) |
[email protected] | e67f0f4 | 2011-12-20 02:29:21 | [diff] [blame] | 1300 | store_->Flush(callback); |
| 1301 | else if (!callback.is_null()) |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1302 | base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | bool CookieMonster::SetCookieWithOptions(const GURL& url, |
| 1306 | const std::string& cookie_line, |
| 1307 | const CookieOptions& options) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 1308 | base::AutoLock autolock(lock_); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1309 | |
| 1310 | if (!HasCookieableScheme(url)) { |
| 1311 | return false; |
| 1312 | } |
| 1313 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1314 | return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options); |
| 1315 | } |
| 1316 | |
| 1317 | std::string CookieMonster::GetCookiesWithOptions(const GURL& url, |
| 1318 | const CookieOptions& options) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 1319 | base::AutoLock autolock(lock_); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1320 | |
[email protected] | 34a160d | 2011-05-12 22:12:49 | [diff] [blame] | 1321 | if (!HasCookieableScheme(url)) |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1322 | return std::string(); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1323 | |
| 1324 | TimeTicks start_time(TimeTicks::Now()); |
| 1325 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1326 | std::vector<CanonicalCookie*> cookies; |
| 1327 | FindCookiesForHostAndDomain(url, options, true, &cookies); |
| 1328 | std::sort(cookies.begin(), cookies.end(), CookieSorter); |
| 1329 | |
[email protected] | 34a160d | 2011-05-12 22:12:49 | [diff] [blame] | 1330 | std::string cookie_line = BuildCookieLine(cookies); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1331 | |
| 1332 | histogram_time_get_->AddTime(TimeTicks::Now() - start_time); |
| 1333 | |
| 1334 | VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line; |
| 1335 | |
| 1336 | return cookie_line; |
| 1337 | } |
| 1338 | |
| 1339 | void CookieMonster::DeleteCookie(const GURL& url, |
| 1340 | const std::string& cookie_name) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 1341 | base::AutoLock autolock(lock_); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1342 | |
| 1343 | if (!HasCookieableScheme(url)) |
| 1344 | return; |
| 1345 | |
| 1346 | CookieOptions options; |
| 1347 | options.set_include_httponly(); |
| 1348 | // Get the cookies for this host and its domain(s). |
| 1349 | std::vector<CanonicalCookie*> cookies; |
| 1350 | FindCookiesForHostAndDomain(url, options, true, &cookies); |
| 1351 | std::set<CanonicalCookie*> matching_cookies; |
| 1352 | |
| 1353 | for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); |
| 1354 | it != cookies.end(); ++it) { |
| 1355 | if ((*it)->Name() != cookie_name) |
| 1356 | continue; |
| 1357 | if (url.path().find((*it)->Path())) |
| 1358 | continue; |
| 1359 | matching_cookies.insert(*it); |
| 1360 | } |
| 1361 | |
| 1362 | for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 1363 | CookieMap::iterator curit = it; |
| 1364 | ++it; |
| 1365 | if (matching_cookies.find(curit->second) != matching_cookies.end()) { |
| 1366 | InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); |
| 1367 | } |
| 1368 | } |
| 1369 | } |
| 1370 | |
[email protected] | 264807b | 2012-04-25 14:49:37 | [diff] [blame] | 1371 | int CookieMonster::DeleteSessionCookies() { |
| 1372 | base::AutoLock autolock(lock_); |
| 1373 | |
| 1374 | int num_deleted = 0; |
| 1375 | for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 1376 | CookieMap::iterator curit = it; |
| 1377 | CanonicalCookie* cc = curit->second; |
| 1378 | ++it; |
| 1379 | |
| 1380 | if (!cc->IsPersistent()) { |
| 1381 | InternalDeleteCookie(curit, |
| 1382 | true, /*sync_to_store*/ |
| 1383 | DELETE_COOKIE_EXPIRED); |
| 1384 | ++num_deleted; |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | return num_deleted; |
| 1389 | } |
| 1390 | |
[email protected] | ee20948 | 2013-04-19 19:50:04 | [diff] [blame] | 1391 | bool CookieMonster::HasCookiesForETLDP1(const std::string& etldp1) { |
| 1392 | base::AutoLock autolock(lock_); |
| 1393 | |
| 1394 | const std::string key(GetKey(etldp1)); |
| 1395 | |
| 1396 | CookieMapItPair its = cookies_.equal_range(key); |
| 1397 | return its.first != its.second; |
| 1398 | } |
| 1399 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1400 | CookieMonster* CookieMonster::GetCookieMonster() { |
| 1401 | return this; |
| 1402 | } |
| 1403 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1404 | // This function must be called before the CookieMonster is used. |
[email protected] | 93c53a3 | 2011-12-05 10:40:35 | [diff] [blame] | 1405 | void CookieMonster::SetPersistSessionCookies(bool persist_session_cookies) { |
[email protected] | 93c53a3 | 2011-12-05 10:40:35 | [diff] [blame] | 1406 | DCHECK(!initialized_); |
| 1407 | persist_session_cookies_ = persist_session_cookies; |
| 1408 | } |
| 1409 | |
[email protected] | bf510ed | 2012-06-05 08:31:43 | [diff] [blame] | 1410 | void CookieMonster::SetForceKeepSessionState() { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1411 | if (store_.get()) { |
[email protected] | bf510ed | 2012-06-05 08:31:43 | [diff] [blame] | 1412 | store_->SetForceKeepSessionState(); |
[email protected] | 93c53a3 | 2011-12-05 10:40:35 | [diff] [blame] | 1413 | } |
| 1414 | } |
| 1415 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1416 | CookieMonster::~CookieMonster() { |
| 1417 | DeleteAll(false); |
| 1418 | } |
| 1419 | |
| 1420 | bool CookieMonster::SetCookieWithCreationTime(const GURL& url, |
| 1421 | const std::string& cookie_line, |
| 1422 | const base::Time& creation_time) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1423 | DCHECK(!store_.get()) << "This method is only to be used by unit-tests."; |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 1424 | base::AutoLock autolock(lock_); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1425 | |
| 1426 | if (!HasCookieableScheme(url)) { |
| 1427 | return false; |
| 1428 | } |
| 1429 | |
| 1430 | InitIfNecessary(); |
| 1431 | return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time, |
| 1432 | CookieOptions()); |
| 1433 | } |
| 1434 | |
| 1435 | void CookieMonster::InitStore() { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1436 | DCHECK(store_.get()) << "Store must exist to initialize"; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1437 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1438 | // We bind in the current time so that we can report the wall-clock time for |
| 1439 | // loading cookies. |
| 1440 | store_->Load(base::Bind(&CookieMonster::OnLoaded, this, TimeTicks::Now())); |
| 1441 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1442 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1443 | void CookieMonster::OnLoaded(TimeTicks beginning_time, |
| 1444 | const std::vector<CanonicalCookie*>& cookies) { |
| 1445 | StoreLoadedCookies(cookies); |
[email protected] | c7593fb2 | 2011-11-14 23:54:27 | [diff] [blame] | 1446 | histogram_time_blocked_on_load_->AddTime(TimeTicks::Now() - beginning_time); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1447 | |
| 1448 | // Invoke the task queue of cookie request. |
| 1449 | InvokeQueue(); |
| 1450 | } |
| 1451 | |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1452 | void CookieMonster::OnKeyLoaded(const std::string& key, |
| 1453 | const std::vector<CanonicalCookie*>& cookies) { |
| 1454 | // This function does its own separate locking. |
| 1455 | StoreLoadedCookies(cookies); |
| 1456 | |
[email protected] | 0184df3 | 2013-05-14 00:53:55 | [diff] [blame] | 1457 | std::deque<scoped_refptr<CookieMonsterTask> > tasks_pending_for_key; |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1458 | |
[email protected] | bab72ec | 2013-10-30 20:50:02 | [diff] [blame] | 1459 | // We need to do this repeatedly until no more tasks were added to the queue |
| 1460 | // during the period where we release the lock. |
| 1461 | while (true) { |
| 1462 | { |
| 1463 | base::AutoLock autolock(lock_); |
| 1464 | std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > > |
| 1465 | ::iterator it = tasks_pending_for_key_.find(key); |
| 1466 | if (it == tasks_pending_for_key_.end()) { |
| 1467 | keys_loaded_.insert(key); |
| 1468 | return; |
| 1469 | } |
| 1470 | if (it->second.empty()) { |
| 1471 | keys_loaded_.insert(key); |
| 1472 | tasks_pending_for_key_.erase(it); |
| 1473 | return; |
| 1474 | } |
| 1475 | it->second.swap(tasks_pending_for_key); |
| 1476 | } |
| 1477 | |
| 1478 | while (!tasks_pending_for_key.empty()) { |
| 1479 | scoped_refptr<CookieMonsterTask> task = tasks_pending_for_key.front(); |
| 1480 | task->Run(); |
| 1481 | tasks_pending_for_key.pop_front(); |
| 1482 | } |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1483 | } |
| 1484 | } |
| 1485 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1486 | void CookieMonster::StoreLoadedCookies( |
| 1487 | const std::vector<CanonicalCookie*>& cookies) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1488 | // Initialize the store and sync in any saved persistent cookies. We don't |
| 1489 | // care if it's expired, insert it so it can be garbage collected, removed, |
| 1490 | // and sync'd. |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1491 | base::AutoLock autolock(lock_); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1492 | |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1493 | CookieItVector cookies_with_control_chars; |
| 1494 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1495 | for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); |
| 1496 | it != cookies.end(); ++it) { |
| 1497 | int64 cookie_creation_time = (*it)->CreationDate().ToInternalValue(); |
| 1498 | |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1499 | if (creation_times_.insert(cookie_creation_time).second) { |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1500 | CookieMap::iterator inserted = |
| 1501 | InternalInsertCookie(GetKey((*it)->Domain()), *it, false); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1502 | const Time cookie_access_time((*it)->LastAccessDate()); |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1503 | if (earliest_access_time_.is_null() || |
| 1504 | cookie_access_time < earliest_access_time_) |
| 1505 | earliest_access_time_ = cookie_access_time; |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1506 | |
| 1507 | if (ContainsControlCharacter((*it)->Name()) || |
| 1508 | ContainsControlCharacter((*it)->Value())) { |
| 1509 | cookies_with_control_chars.push_back(inserted); |
| 1510 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1511 | } else { |
| 1512 | LOG(ERROR) << base::StringPrintf("Found cookies with duplicate creation " |
| 1513 | "times in backing store: " |
| 1514 | "{name='%s', domain='%s', path='%s'}", |
| 1515 | (*it)->Name().c_str(), |
| 1516 | (*it)->Domain().c_str(), |
| 1517 | (*it)->Path().c_str()); |
| 1518 | // We've been given ownership of the cookie and are throwing it |
| 1519 | // away; reclaim the space. |
| 1520 | delete (*it); |
| 1521 | } |
| 1522 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1523 | |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1524 | // Any cookies that contain control characters that we have loaded from the |
| 1525 | // persistent store should be deleted. See http://crbug.com/238041. |
| 1526 | for (CookieItVector::iterator it = cookies_with_control_chars.begin(); |
| 1527 | it != cookies_with_control_chars.end();) { |
| 1528 | CookieItVector::iterator curit = it; |
| 1529 | ++it; |
| 1530 | |
| 1531 | InternalDeleteCookie(*curit, true, DELETE_COOKIE_CONTROL_CHAR); |
| 1532 | } |
| 1533 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1534 | // After importing cookies from the PersistentCookieStore, verify that |
| 1535 | // none of our other constraints are violated. |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1536 | // In particular, the backing store might have given us duplicate cookies. |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1537 | |
| 1538 | // This method could be called multiple times due to priority loading, thus |
| 1539 | // cookies loaded in previous runs will be validated again, but this is OK |
| 1540 | // since they are expected to be much fewer than total DB. |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1541 | EnsureCookiesMapIsValid(); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1542 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1543 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1544 | void CookieMonster::InvokeQueue() { |
| 1545 | while (true) { |
| 1546 | scoped_refptr<CookieMonsterTask> request_task; |
| 1547 | { |
| 1548 | base::AutoLock autolock(lock_); |
[email protected] | 0184df3 | 2013-05-14 00:53:55 | [diff] [blame] | 1549 | if (tasks_pending_.empty()) { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1550 | loaded_ = true; |
[email protected] | 8562034 | 2011-10-17 17:35:04 | [diff] [blame] | 1551 | creation_times_.clear(); |
| 1552 | keys_loaded_.clear(); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1553 | break; |
| 1554 | } |
[email protected] | 0184df3 | 2013-05-14 00:53:55 | [diff] [blame] | 1555 | request_task = tasks_pending_.front(); |
| 1556 | tasks_pending_.pop(); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1557 | } |
| 1558 | request_task->Run(); |
| 1559 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | void CookieMonster::EnsureCookiesMapIsValid() { |
| 1563 | lock_.AssertAcquired(); |
| 1564 | |
| 1565 | int num_duplicates_trimmed = 0; |
| 1566 | |
| 1567 | // Iterate through all the of the cookies, grouped by host. |
| 1568 | CookieMap::iterator prev_range_end = cookies_.begin(); |
| 1569 | while (prev_range_end != cookies_.end()) { |
| 1570 | CookieMap::iterator cur_range_begin = prev_range_end; |
| 1571 | const std::string key = cur_range_begin->first; // Keep a copy. |
| 1572 | CookieMap::iterator cur_range_end = cookies_.upper_bound(key); |
| 1573 | prev_range_end = cur_range_end; |
| 1574 | |
| 1575 | // Ensure no equivalent cookies for this host. |
| 1576 | num_duplicates_trimmed += |
| 1577 | TrimDuplicateCookiesForKey(key, cur_range_begin, cur_range_end); |
| 1578 | } |
| 1579 | |
| 1580 | // Record how many duplicates were found in the database. |
| 1581 | // See InitializeHistograms() for details. |
| 1582 | histogram_cookie_deletion_cause_->Add(num_duplicates_trimmed); |
| 1583 | } |
| 1584 | |
| 1585 | int CookieMonster::TrimDuplicateCookiesForKey( |
| 1586 | const std::string& key, |
| 1587 | CookieMap::iterator begin, |
| 1588 | CookieMap::iterator end) { |
| 1589 | lock_.AssertAcquired(); |
| 1590 | |
| 1591 | // Set of cookies ordered by creation time. |
| 1592 | typedef std::set<CookieMap::iterator, OrderByCreationTimeDesc> CookieSet; |
| 1593 | |
| 1594 | // Helper map we populate to find the duplicates. |
| 1595 | typedef std::map<CookieSignature, CookieSet> EquivalenceMap; |
| 1596 | EquivalenceMap equivalent_cookies; |
| 1597 | |
| 1598 | // The number of duplicate cookies that have been found. |
| 1599 | int num_duplicates = 0; |
| 1600 | |
| 1601 | // Iterate through all of the cookies in our range, and insert them into |
| 1602 | // the equivalence map. |
| 1603 | for (CookieMap::iterator it = begin; it != end; ++it) { |
| 1604 | DCHECK_EQ(key, it->first); |
| 1605 | CanonicalCookie* cookie = it->second; |
| 1606 | |
| 1607 | CookieSignature signature(cookie->Name(), cookie->Domain(), |
| 1608 | cookie->Path()); |
| 1609 | CookieSet& set = equivalent_cookies[signature]; |
| 1610 | |
| 1611 | // We found a duplicate! |
| 1612 | if (!set.empty()) |
| 1613 | num_duplicates++; |
| 1614 | |
| 1615 | // We save the iterator into |cookies_| rather than the actual cookie |
| 1616 | // pointer, since we may need to delete it later. |
| 1617 | bool insert_success = set.insert(it).second; |
| 1618 | DCHECK(insert_success) << |
| 1619 | "Duplicate creation times found in duplicate cookie name scan."; |
| 1620 | } |
| 1621 | |
| 1622 | // If there were no duplicates, we are done! |
| 1623 | if (num_duplicates == 0) |
| 1624 | return 0; |
| 1625 | |
| 1626 | // Make sure we find everything below that we did above. |
| 1627 | int num_duplicates_found = 0; |
| 1628 | |
| 1629 | // Otherwise, delete all the duplicate cookies, both from our in-memory store |
| 1630 | // and from the backing store. |
| 1631 | for (EquivalenceMap::iterator it = equivalent_cookies.begin(); |
| 1632 | it != equivalent_cookies.end(); |
| 1633 | ++it) { |
| 1634 | const CookieSignature& signature = it->first; |
| 1635 | CookieSet& dupes = it->second; |
| 1636 | |
| 1637 | if (dupes.size() <= 1) |
| 1638 | continue; // This cookiename/path has no duplicates. |
| 1639 | num_duplicates_found += dupes.size() - 1; |
| 1640 | |
| 1641 | // Since |dups| is sorted by creation time (descending), the first cookie |
| 1642 | // is the most recent one, so we will keep it. The rest are duplicates. |
| 1643 | dupes.erase(dupes.begin()); |
| 1644 | |
| 1645 | LOG(ERROR) << base::StringPrintf( |
| 1646 | "Found %d duplicate cookies for host='%s', " |
| 1647 | "with {name='%s', domain='%s', path='%s'}", |
| 1648 | static_cast<int>(dupes.size()), |
| 1649 | key.c_str(), |
| 1650 | signature.name.c_str(), |
| 1651 | signature.domain.c_str(), |
| 1652 | signature.path.c_str()); |
| 1653 | |
| 1654 | // Remove all the cookies identified by |dupes|. It is valid to delete our |
| 1655 | // list of iterators one at a time, since |cookies_| is a multimap (they |
| 1656 | // don't invalidate existing iterators following deletion). |
| 1657 | for (CookieSet::iterator dupes_it = dupes.begin(); |
| 1658 | dupes_it != dupes.end(); |
| 1659 | ++dupes_it) { |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 1660 | InternalDeleteCookie(*dupes_it, true, |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1661 | DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE); |
| 1662 | } |
| 1663 | } |
| 1664 | DCHECK_EQ(num_duplicates, num_duplicates_found); |
| 1665 | |
| 1666 | return num_duplicates; |
| 1667 | } |
| 1668 | |
[email protected] | ba4ad0e | 2011-03-15 08:12:47 | [diff] [blame] | 1669 | // Note: file must be the last scheme. |
| 1670 | const char* CookieMonster::kDefaultCookieableSchemes[] = |
| 1671 | { "http", "https", "file" }; |
| 1672 | const int CookieMonster::kDefaultCookieableSchemesCount = |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 1673 | arraysize(kDefaultCookieableSchemes); |
[email protected] | ba4ad0e | 2011-03-15 08:12:47 | [diff] [blame] | 1674 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1675 | void CookieMonster::SetDefaultCookieableSchemes() { |
[email protected] | 33ad6ce9 | 2013-08-27 14:39:08 | [diff] [blame] | 1676 | int num_schemes = default_enable_file_scheme_ ? |
| 1677 | kDefaultCookieableSchemesCount : kDefaultCookieableSchemesCount - 1; |
| 1678 | SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1679 | } |
| 1680 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1681 | void CookieMonster::FindCookiesForHostAndDomain( |
| 1682 | const GURL& url, |
| 1683 | const CookieOptions& options, |
| 1684 | bool update_access_time, |
| 1685 | std::vector<CanonicalCookie*>* cookies) { |
| 1686 | lock_.AssertAcquired(); |
| 1687 | |
| 1688 | const Time current_time(CurrentTime()); |
| 1689 | |
| 1690 | // Probe to save statistics relatively frequently. We do it here rather |
| 1691 | // than in the set path as many websites won't set cookies, and we |
| 1692 | // want to collect statistics whenever the browser's being used. |
| 1693 | RecordPeriodicStats(current_time); |
| 1694 | |
[email protected] | 8e158367 | 2012-02-11 04:39:41 | [diff] [blame] | 1695 | // Can just dispatch to FindCookiesForKey |
| 1696 | const std::string key(GetKey(url.host())); |
| 1697 | FindCookiesForKey(key, url, options, current_time, |
| 1698 | update_access_time, cookies); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1699 | } |
| 1700 | |
[email protected] | dedec0b | 2013-02-28 04:50:10 | [diff] [blame] | 1701 | void CookieMonster::FindCookiesForKey(const std::string& key, |
| 1702 | const GURL& url, |
| 1703 | const CookieOptions& options, |
| 1704 | const Time& current, |
| 1705 | bool update_access_time, |
| 1706 | std::vector<CanonicalCookie*>* cookies) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1707 | lock_.AssertAcquired(); |
| 1708 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1709 | for (CookieMapItPair its = cookies_.equal_range(key); |
| 1710 | its.first != its.second; ) { |
| 1711 | CookieMap::iterator curit = its.first; |
| 1712 | CanonicalCookie* cc = curit->second; |
| 1713 | ++its.first; |
| 1714 | |
| 1715 | // If the cookie is expired, delete it. |
[email protected] | ba4ad0e | 2011-03-15 08:12:47 | [diff] [blame] | 1716 | if (cc->IsExpired(current) && !keep_expired_cookies_) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1717 | InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED); |
| 1718 | continue; |
| 1719 | } |
| 1720 | |
[email protected] | 65f4e7e | 2012-12-12 21:56:54 | [diff] [blame] | 1721 | // Filter out cookies that should not be included for a request to the |
| 1722 | // given |url|. HTTP only cookies are filtered depending on the passed |
| 1723 | // cookie |options|. |
| 1724 | if (!cc->IncludeForRequestURL(url, options)) |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1725 | continue; |
| 1726 | |
[email protected] | 65f4e7e | 2012-12-12 21:56:54 | [diff] [blame] | 1727 | // Add this cookie to the set of matching cookies. Update the access |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1728 | // time if we've been requested to do so. |
| 1729 | if (update_access_time) { |
| 1730 | InternalUpdateCookieAccessTime(cc, current); |
| 1731 | } |
| 1732 | cookies->push_back(cc); |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | bool CookieMonster::DeleteAnyEquivalentCookie(const std::string& key, |
| 1737 | const CanonicalCookie& ecc, |
[email protected] | e7c590e5 | 2011-03-30 08:33:55 | [diff] [blame] | 1738 | bool skip_httponly, |
| 1739 | bool already_expired) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1740 | lock_.AssertAcquired(); |
| 1741 | |
| 1742 | bool found_equivalent_cookie = false; |
| 1743 | bool skipped_httponly = false; |
| 1744 | for (CookieMapItPair its = cookies_.equal_range(key); |
| 1745 | its.first != its.second; ) { |
| 1746 | CookieMap::iterator curit = its.first; |
| 1747 | CanonicalCookie* cc = curit->second; |
| 1748 | ++its.first; |
| 1749 | |
| 1750 | if (ecc.IsEquivalent(*cc)) { |
| 1751 | // We should never have more than one equivalent cookie, since they should |
| 1752 | // overwrite each other. |
| 1753 | CHECK(!found_equivalent_cookie) << |
| 1754 | "Duplicate equivalent cookies found, cookie store is corrupted."; |
| 1755 | if (skip_httponly && cc->IsHttpOnly()) { |
| 1756 | skipped_httponly = true; |
| 1757 | } else { |
[email protected] | e7c590e5 | 2011-03-30 08:33:55 | [diff] [blame] | 1758 | InternalDeleteCookie(curit, true, already_expired ? |
| 1759 | DELETE_COOKIE_EXPIRED_OVERWRITE : DELETE_COOKIE_OVERWRITE); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1760 | } |
| 1761 | found_equivalent_cookie = true; |
| 1762 | } |
| 1763 | } |
| 1764 | return skipped_httponly; |
| 1765 | } |
| 1766 | |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1767 | CookieMonster::CookieMap::iterator CookieMonster::InternalInsertCookie( |
| 1768 | const std::string& key, |
| 1769 | CanonicalCookie* cc, |
| 1770 | bool sync_to_store) { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1771 | lock_.AssertAcquired(); |
| 1772 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1773 | if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() && |
| 1774 | sync_to_store) |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1775 | store_->AddCookie(*cc); |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1776 | CookieMap::iterator inserted = |
| 1777 | cookies_.insert(CookieMap::value_type(key, cc)); |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 1778 | if (delegate_.get()) { |
| 1779 | delegate_->OnCookieChanged( |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 1780 | *cc, false, Delegate::CHANGE_COOKIE_EXPLICIT); |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 1781 | } |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1782 | |
| 1783 | return inserted; |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 1784 | } |
| 1785 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1786 | bool CookieMonster::SetCookieWithCreationTimeAndOptions( |
| 1787 | const GURL& url, |
| 1788 | const std::string& cookie_line, |
| 1789 | const Time& creation_time_or_null, |
| 1790 | const CookieOptions& options) { |
[email protected] | b866a02d | 2010-07-28 16:41:04 | [diff] [blame] | 1791 | lock_.AssertAcquired(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1792 | |
[email protected] | 4d3ce78 | 2010-10-29 18:31:28 | [diff] [blame] | 1793 | VLOG(kVlogSetCookies) << "SetCookie() line: " << cookie_line; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1794 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 1795 | Time creation_time = creation_time_or_null; |
| 1796 | if (creation_time.is_null()) { |
| 1797 | creation_time = CurrentTime(); |
| 1798 | last_time_seen_ = creation_time; |
| 1799 | } |
| 1800 | |
[email protected] | abbd13b | 2012-11-15 17:54:20 | [diff] [blame] | 1801 | scoped_ptr<CanonicalCookie> cc( |
| 1802 | CanonicalCookie::Create(url, cookie_line, creation_time, options)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1803 | |
| 1804 | if (!cc.get()) { |
[email protected] | 4d3ce78 | 2010-10-29 18:31:28 | [diff] [blame] | 1805 | VLOG(kVlogSetCookies) << "WARNING: Failed to allocate CanonicalCookie"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1806 | return false; |
| 1807 | } |
[email protected] | fa77eb51 | 2010-07-22 16:12:51 | [diff] [blame] | 1808 | return SetCanonicalCookie(&cc, creation_time, options); |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame] | 1809 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1810 | |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame] | 1811 | bool CookieMonster::SetCanonicalCookie(scoped_ptr<CanonicalCookie>* cc, |
[email protected] | f325f1e1 | 2010-04-30 22:38:55 | [diff] [blame] | 1812 | const Time& creation_time, |
| 1813 | const CookieOptions& options) { |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1814 | const std::string key(GetKey((*cc)->Domain())); |
[email protected] | e7c590e5 | 2011-03-30 08:33:55 | [diff] [blame] | 1815 | bool already_expired = (*cc)->IsExpired(creation_time); |
| 1816 | if (DeleteAnyEquivalentCookie(key, **cc, options.exclude_httponly(), |
| 1817 | already_expired)) { |
[email protected] | 4d3ce78 | 2010-10-29 18:31:28 | [diff] [blame] | 1818 | VLOG(kVlogSetCookies) << "SetCookie() not clobbering httponly cookie"; |
[email protected] | 3a96c74 | 2008-11-19 19:46:27 | [diff] [blame] | 1819 | return false; |
| 1820 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1821 | |
[email protected] | 4d3ce78 | 2010-10-29 18:31:28 | [diff] [blame] | 1822 | VLOG(kVlogSetCookies) << "SetCookie() key: " << key << " cc: " |
| 1823 | << (*cc)->DebugString(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1824 | |
| 1825 | // Realize that we might be setting an expired cookie, and the only point |
| 1826 | // was to delete the cookie which we've already done. |
[email protected] | e7c590e5 | 2011-03-30 08:33:55 | [diff] [blame] | 1827 | if (!already_expired || keep_expired_cookies_) { |
[email protected] | 374f58b | 2010-07-20 15:29:26 | [diff] [blame] | 1828 | // See InitializeHistograms() for details. |
[email protected] | 10b691f | 2012-07-11 15:22:15 | [diff] [blame] | 1829 | if ((*cc)->IsPersistent()) { |
[email protected] | 8475bee | 2011-03-17 18:40:24 | [diff] [blame] | 1830 | histogram_expiration_duration_minutes_->Add( |
| 1831 | ((*cc)->ExpiryDate() - creation_time).InMinutes()); |
| 1832 | } |
| 1833 | |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1834 | InternalInsertCookie(key, cc->release(), true); |
[email protected] | 348dd66 | 2013-03-13 20:25:07 | [diff] [blame] | 1835 | } else { |
| 1836 | VLOG(kVlogSetCookies) << "SetCookie() not storing already expired cookie."; |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 1837 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1838 | |
| 1839 | // We assume that hopefully setting a cookie will be less common than |
| 1840 | // querying a cookie. Since setting a cookie can put us over our limits, |
| 1841 | // make sure that we garbage collect... We can also make the assumption that |
| 1842 | // if a cookie was set, in the common case it will be used soon after, |
| 1843 | // and we will purge the expired cookies in GetCookies(). |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1844 | GarbageCollect(creation_time, key); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1845 | |
| 1846 | return true; |
| 1847 | } |
| 1848 | |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1849 | void CookieMonster::InternalUpdateCookieAccessTime(CanonicalCookie* cc, |
| 1850 | const Time& current) { |
[email protected] | bb890572 | 2010-05-21 17:29:04 | [diff] [blame] | 1851 | lock_.AssertAcquired(); |
| 1852 | |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 1853 | // Based off the Mozilla code. When a cookie has been accessed recently, |
| 1854 | // don't bother updating its access time again. This reduces the number of |
| 1855 | // updates we do during pageload, which in turn reduces the chance our storage |
| 1856 | // backend will hit its batch thresholds and be forced to update. |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 1857 | if ((current - cc->LastAccessDate()) < last_access_threshold_) |
| 1858 | return; |
| 1859 | |
[email protected] | 374f58b | 2010-07-20 15:29:26 | [diff] [blame] | 1860 | // See InitializeHistograms() for details. |
| 1861 | histogram_between_access_interval_minutes_->Add( |
| 1862 | (current - cc->LastAccessDate()).InMinutes()); |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 1863 | |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 1864 | cc->SetLastAccessDate(current); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1865 | if ((cc->IsPersistent() || persist_session_cookies_) && store_.get()) |
[email protected] | 77e0a46 | 2008-11-01 00:43:35 | [diff] [blame] | 1866 | store_->UpdateCookieAccessTime(*cc); |
| 1867 | } |
| 1868 | |
[email protected] | 6210ce5 | 2013-09-20 03:33:14 | [diff] [blame] | 1869 | // InternalDeleteCookies must not invalidate iterators other than the one being |
| 1870 | // deleted. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1871 | void CookieMonster::InternalDeleteCookie(CookieMap::iterator it, |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 1872 | bool sync_to_store, |
| 1873 | DeletionCause deletion_cause) { |
[email protected] | bb890572 | 2010-05-21 17:29:04 | [diff] [blame] | 1874 | lock_.AssertAcquired(); |
| 1875 | |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 1876 | // Ideally, this would be asserted up where we define ChangeCauseMapping, |
| 1877 | // but DeletionCause's visibility (or lack thereof) forces us to make |
| 1878 | // this check here. |
| 1879 | COMPILE_ASSERT(arraysize(ChangeCauseMapping) == DELETE_COOKIE_LAST_ENTRY + 1, |
| 1880 | ChangeCauseMapping_size_not_eq_DeletionCause_enum_size); |
| 1881 | |
[email protected] | 374f58b | 2010-07-20 15:29:26 | [diff] [blame] | 1882 | // See InitializeHistograms() for details. |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1883 | if (deletion_cause != DELETE_COOKIE_DONT_RECORD) |
| 1884 | histogram_cookie_deletion_cause_->Add(deletion_cause); |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 1885 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1886 | CanonicalCookie* cc = it->second; |
[email protected] | 4d3ce78 | 2010-10-29 18:31:28 | [diff] [blame] | 1887 | VLOG(kVlogSetCookies) << "InternalDeleteCookie() cc: " << cc->DebugString(); |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1888 | |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1889 | if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() && |
| 1890 | sync_to_store) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1891 | store_->DeleteCookie(*cc); |
[email protected] | 8bb846f | 2011-03-23 12:08:18 | [diff] [blame] | 1892 | if (delegate_.get()) { |
| 1893 | ChangeCausePair mapping = ChangeCauseMapping[deletion_cause]; |
| 1894 | |
| 1895 | if (mapping.notify) |
| 1896 | delegate_->OnCookieChanged(*cc, true, mapping.cause); |
| 1897 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1898 | cookies_.erase(it); |
| 1899 | delete cc; |
| 1900 | } |
| 1901 | |
[email protected] | 8807b32 | 2010-10-01 17:10:14 | [diff] [blame] | 1902 | // Domain expiry behavior is unchanged by key/expiry scheme (the |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1903 | // meaning of the key is different, but that's not visible to this routine). |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1904 | int CookieMonster::GarbageCollect(const Time& current, |
| 1905 | const std::string& key) { |
[email protected] | bb890572 | 2010-05-21 17:29:04 | [diff] [blame] | 1906 | lock_.AssertAcquired(); |
| 1907 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1908 | int num_deleted = 0; |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1909 | Time safe_date( |
| 1910 | Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1911 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1912 | // Collect garbage for this key, minding cookie priorities. |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1913 | if (cookies_.count(key) > kDomainMaxCookies) { |
[email protected] | 4d3ce78 | 2010-10-29 18:31:28 | [diff] [blame] | 1914 | VLOG(kVlogGarbageCollection) << "GarbageCollect() key: " << key; |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1915 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1916 | CookieItVector cookie_its; |
[email protected] | 8807b32 | 2010-10-01 17:10:14 | [diff] [blame] | 1917 | num_deleted += GarbageCollectExpired( |
| 1918 | current, cookies_.equal_range(key), &cookie_its); |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1919 | if (cookie_its.size() > kDomainMaxCookies) { |
| 1920 | VLOG(kVlogGarbageCollection) << "Deep Garbage Collect domain."; |
| 1921 | size_t purge_goal = |
| 1922 | cookie_its.size() - (kDomainMaxCookies - kDomainPurgeCookies); |
| 1923 | DCHECK(purge_goal > kDomainPurgeCookies); |
| 1924 | |
| 1925 | // Boundary iterators into |cookie_its| for different priorities. |
| 1926 | CookieItVector::iterator it_bdd[4]; |
| 1927 | // Intialize |it_bdd| while sorting |cookie_its| by priorities. |
| 1928 | // Schematic: [MLLHMHHLMM] => [LLL|MMMM|HHH], with 4 boundaries. |
| 1929 | it_bdd[0] = cookie_its.begin(); |
| 1930 | it_bdd[3] = cookie_its.end(); |
| 1931 | it_bdd[1] = PartitionCookieByPriority(it_bdd[0], it_bdd[3], |
| 1932 | COOKIE_PRIORITY_LOW); |
| 1933 | it_bdd[2] = PartitionCookieByPriority(it_bdd[1], it_bdd[3], |
| 1934 | COOKIE_PRIORITY_MEDIUM); |
| 1935 | size_t quota[3] = { |
| 1936 | kDomainCookiesQuotaLow, |
| 1937 | kDomainCookiesQuotaMedium, |
| 1938 | kDomainCookiesQuotaHigh |
| 1939 | }; |
| 1940 | |
| 1941 | // Purge domain cookies in 3 rounds. |
| 1942 | // Round 1: consider low-priority cookies only: evict least-recently |
| 1943 | // accessed, while protecting quota[0] of these from deletion. |
| 1944 | // Round 2: consider {low, medium}-priority cookies, evict least-recently |
| 1945 | // accessed, while protecting quota[0] + quota[1]. |
| 1946 | // Round 3: consider all cookies, evict least-recently accessed. |
| 1947 | size_t accumulated_quota = 0; |
| 1948 | CookieItVector::iterator it_purge_begin = it_bdd[0]; |
| 1949 | for (int i = 0; i < 3 && purge_goal > 0; ++i) { |
| 1950 | accumulated_quota += quota[i]; |
| 1951 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1952 | size_t num_considered = it_bdd[i + 1] - it_purge_begin; |
| 1953 | if (num_considered <= accumulated_quota) |
| 1954 | continue; |
| 1955 | |
| 1956 | // Number of cookies that will be purged in this round. |
| 1957 | size_t round_goal = |
| 1958 | std::min(purge_goal, num_considered - accumulated_quota); |
| 1959 | purge_goal -= round_goal; |
| 1960 | |
| 1961 | SortLeastRecentlyAccessed(it_purge_begin, it_bdd[i + 1], round_goal); |
| 1962 | // Cookies accessed on or after |safe_date| would have been safe from |
| 1963 | // global purge, and we want to keep track of this. |
| 1964 | CookieItVector::iterator it_purge_end = it_purge_begin + round_goal; |
| 1965 | CookieItVector::iterator it_purge_middle = |
| 1966 | LowerBoundAccessDate(it_purge_begin, it_purge_end, safe_date); |
| 1967 | // Delete cookies accessed before |safe_date|. |
| 1968 | num_deleted += GarbageCollectDeleteRange( |
| 1969 | current, |
| 1970 | DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE, |
| 1971 | it_purge_begin, |
| 1972 | it_purge_middle); |
| 1973 | // Delete cookies accessed on or after |safe_date|. |
| 1974 | num_deleted += GarbageCollectDeleteRange( |
| 1975 | current, |
| 1976 | DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE, |
| 1977 | it_purge_middle, |
| 1978 | it_purge_end); |
| 1979 | it_purge_begin = it_purge_end; |
| 1980 | } |
| 1981 | DCHECK_EQ(0U, purge_goal); |
[email protected] | 8807b32 | 2010-10-01 17:10:14 | [diff] [blame] | 1982 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1983 | } |
| 1984 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1985 | // Collect garbage for everything. With firefox style we want to preserve |
| 1986 | // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict. |
[email protected] | 8807b32 | 2010-10-01 17:10:14 | [diff] [blame] | 1987 | if (cookies_.size() > kMaxCookies && |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1988 | earliest_access_time_ < safe_date) { |
[email protected] | 4d3ce78 | 2010-10-29 18:31:28 | [diff] [blame] | 1989 | VLOG(kVlogGarbageCollection) << "GarbageCollect() everything"; |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1990 | CookieItVector cookie_its; |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 1991 | num_deleted += GarbageCollectExpired( |
| 1992 | current, CookieMapItPair(cookies_.begin(), cookies_.end()), |
| 1993 | &cookie_its); |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 1994 | if (cookie_its.size() > kMaxCookies) { |
| 1995 | VLOG(kVlogGarbageCollection) << "Deep Garbage Collect everything."; |
| 1996 | size_t purge_goal = cookie_its.size() - (kMaxCookies - kPurgeCookies); |
| 1997 | DCHECK(purge_goal > kPurgeCookies); |
| 1998 | // Sorts up to *and including* |cookie_its[purge_goal]|, so |
| 1999 | // |earliest_access_time| will be properly assigned even if |
| 2000 | // |global_purge_it| == |cookie_its.begin() + purge_goal|. |
| 2001 | SortLeastRecentlyAccessed(cookie_its.begin(), cookie_its.end(), |
| 2002 | purge_goal); |
| 2003 | // Find boundary to cookies older than safe_date. |
| 2004 | CookieItVector::iterator global_purge_it = |
| 2005 | LowerBoundAccessDate(cookie_its.begin(), |
| 2006 | cookie_its.begin() + purge_goal, |
| 2007 | safe_date); |
| 2008 | // Only delete the old cookies. |
| 2009 | num_deleted += GarbageCollectDeleteRange( |
[email protected] | 8807b32 | 2010-10-01 17:10:14 | [diff] [blame] | 2010 | current, |
[email protected] | 8807b32 | 2010-10-01 17:10:14 | [diff] [blame] | 2011 | DELETE_COOKIE_EVICTED_GLOBAL, |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 2012 | cookie_its.begin(), |
| 2013 | global_purge_it); |
| 2014 | // Set access day to the oldest cookie that wasn't deleted. |
| 2015 | earliest_access_time_ = (*global_purge_it)->second->LastAccessDate(); |
[email protected] | 8807b32 | 2010-10-01 17:10:14 | [diff] [blame] | 2016 | } |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | return num_deleted; |
| 2020 | } |
| 2021 | |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 2022 | int CookieMonster::GarbageCollectExpired( |
| 2023 | const Time& current, |
| 2024 | const CookieMapItPair& itpair, |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 2025 | CookieItVector* cookie_its) { |
[email protected] | ba4ad0e | 2011-03-15 08:12:47 | [diff] [blame] | 2026 | if (keep_expired_cookies_) |
| 2027 | return 0; |
| 2028 | |
[email protected] | bb890572 | 2010-05-21 17:29:04 | [diff] [blame] | 2029 | lock_.AssertAcquired(); |
| 2030 | |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 2031 | int num_deleted = 0; |
| 2032 | for (CookieMap::iterator it = itpair.first, end = itpair.second; it != end;) { |
| 2033 | CookieMap::iterator curit = it; |
| 2034 | ++it; |
| 2035 | |
| 2036 | if (curit->second->IsExpired(current)) { |
[email protected] | 2f3f359 | 2010-07-07 20:11:51 | [diff] [blame] | 2037 | InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED); |
[email protected] | c890ed19 | 2008-10-30 23:45:53 | [diff] [blame] | 2038 | ++num_deleted; |
| 2039 | } else if (cookie_its) { |
| 2040 | cookie_its->push_back(curit); |
| 2041 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | return num_deleted; |
| 2045 | } |
| 2046 | |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 2047 | int CookieMonster::GarbageCollectDeleteRange( |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2048 | const Time& current, |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2049 | DeletionCause cause, |
[email protected] | 5fa4f9a | 2013-10-03 10:13:16 | [diff] [blame] | 2050 | CookieItVector::iterator it_begin, |
| 2051 | CookieItVector::iterator it_end) { |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 2052 | for (CookieItVector::iterator it = it_begin; it != it_end; it++) { |
| 2053 | histogram_evicted_last_access_minutes_->Add( |
| 2054 | (current - (*it)->second->LastAccessDate()).InMinutes()); |
| 2055 | InternalDeleteCookie((*it), true, cause); |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 2056 | } |
[email protected] | 8ad5d46 | 2013-05-02 08:45:26 | [diff] [blame] | 2057 | return it_end - it_begin; |
[email protected] | c10da4b0 | 2010-03-25 14:38:32 | [diff] [blame] | 2058 | } |
| 2059 | |
[email protected] | ed32c21 | 2013-05-14 20:49:29 | [diff] [blame] | 2060 | // A wrapper around registry_controlled_domains::GetDomainAndRegistry |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2061 | // to make clear we're creating a key for our local map. Here and |
| 2062 | // in FindCookiesForHostAndDomain() are the only two places where |
| 2063 | // we need to conditionalize based on key type. |
| 2064 | // |
| 2065 | // Note that this key algorithm explicitly ignores the scheme. This is |
| 2066 | // because when we're entering cookies into the map from the backing store, |
| 2067 | // we in general won't have the scheme at that point. |
| 2068 | // In practical terms, this means that file cookies will be stored |
| 2069 | // in the map either by an empty string or by UNC name (and will be |
| 2070 | // limited by kMaxCookiesPerHost), and extension cookies will be stored |
| 2071 | // based on the single extension id, as the extension id won't have the |
| 2072 | // form of a DNS host and hence GetKey() will return it unchanged. |
| 2073 | // |
| 2074 | // Arguably the right thing to do here is to make the key |
| 2075 | // algorithm dependent on the scheme, and make sure that the scheme is |
| 2076 | // available everywhere the key must be obtained (specfically at backing |
| 2077 | // store load time). This would require either changing the backing store |
| 2078 | // database schema to include the scheme (far more trouble than it's worth), or |
| 2079 | // separating out file cookies into their own CookieMonster instance and |
| 2080 | // thus restricting each scheme to a single cookie monster (which might |
| 2081 | // be worth it, but is still too much trouble to solve what is currently a |
| 2082 | // non-problem). |
| 2083 | std::string CookieMonster::GetKey(const std::string& domain) const { |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2084 | std::string effective_domain( |
[email protected] | ed32c21 | 2013-05-14 20:49:29 | [diff] [blame] | 2085 | registry_controlled_domains::GetDomainAndRegistry( |
| 2086 | domain, registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)); |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2087 | if (effective_domain.empty()) |
| 2088 | effective_domain = domain; |
| 2089 | |
| 2090 | if (!effective_domain.empty() && effective_domain[0] == '.') |
| 2091 | return effective_domain.substr(1); |
| 2092 | return effective_domain; |
| 2093 | } |
| 2094 | |
[email protected] | 97a3b6e | 2012-06-12 01:53:56 | [diff] [blame] | 2095 | bool CookieMonster::IsCookieableScheme(const std::string& scheme) { |
| 2096 | base::AutoLock autolock(lock_); |
| 2097 | |
| 2098 | return std::find(cookieable_schemes_.begin(), cookieable_schemes_.end(), |
| 2099 | scheme) != cookieable_schemes_.end(); |
| 2100 | } |
| 2101 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2102 | bool CookieMonster::HasCookieableScheme(const GURL& url) { |
| 2103 | lock_.AssertAcquired(); |
| 2104 | |
| 2105 | // Make sure the request is on a cookie-able url scheme. |
| 2106 | for (size_t i = 0; i < cookieable_schemes_.size(); ++i) { |
| 2107 | // We matched a scheme. |
| 2108 | if (url.SchemeIs(cookieable_schemes_[i].c_str())) { |
| 2109 | // We've matched a supported scheme. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2110 | return true; |
| 2111 | } |
| 2112 | } |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2113 | |
| 2114 | // The scheme didn't match any in our whitelist. |
| 2115 | VLOG(kVlogPerCookieMonster) << "WARNING: Unsupported cookie scheme: " |
| 2116 | << url.scheme(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2117 | return false; |
| 2118 | } |
| 2119 | |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 2120 | // Test to see if stats should be recorded, and record them if so. |
| 2121 | // The goal here is to get sampling for the average browser-hour of |
| 2122 | // activity. We won't take samples when the web isn't being surfed, |
| 2123 | // and when the web is being surfed, we'll take samples about every |
| 2124 | // kRecordStatisticsIntervalSeconds. |
| 2125 | // last_statistic_record_time_ is initialized to Now() rather than null |
| 2126 | // in the constructor so that we won't take statistics right after |
| 2127 | // startup, to avoid bias from browsers that are started but not used. |
| 2128 | void CookieMonster::RecordPeriodicStats(const base::Time& current_time) { |
| 2129 | const base::TimeDelta kRecordStatisticsIntervalTime( |
| 2130 | base::TimeDelta::FromSeconds(kRecordStatisticsIntervalSeconds)); |
| 2131 | |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 2132 | // If we've taken statistics recently, return. |
| 2133 | if (current_time - last_statistic_record_time_ <= |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 2134 | kRecordStatisticsIntervalTime) { |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 2135 | return; |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 2136 | } |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 2137 | |
| 2138 | // See InitializeHistograms() for details. |
| 2139 | histogram_count_->Add(cookies_.size()); |
| 2140 | |
| 2141 | // More detailed statistics on cookie counts at different granularities. |
| 2142 | TimeTicks beginning_of_time(TimeTicks::Now()); |
| 2143 | |
| 2144 | for (CookieMap::const_iterator it_key = cookies_.begin(); |
| 2145 | it_key != cookies_.end(); ) { |
| 2146 | const std::string& key(it_key->first); |
| 2147 | |
| 2148 | int key_count = 0; |
| 2149 | typedef std::map<std::string, unsigned int> DomainMap; |
| 2150 | DomainMap domain_map; |
| 2151 | CookieMapItPair its_cookies = cookies_.equal_range(key); |
| 2152 | while (its_cookies.first != its_cookies.second) { |
| 2153 | key_count++; |
| 2154 | const std::string& cookie_domain(its_cookies.first->second->Domain()); |
| 2155 | domain_map[cookie_domain]++; |
| 2156 | |
| 2157 | its_cookies.first++; |
| 2158 | } |
| 2159 | histogram_etldp1_count_->Add(key_count); |
| 2160 | histogram_domain_per_etldp1_count_->Add(domain_map.size()); |
| 2161 | for (DomainMap::const_iterator domain_map_it = domain_map.begin(); |
| 2162 | domain_map_it != domain_map.end(); domain_map_it++) |
| 2163 | histogram_domain_count_->Add(domain_map_it->second); |
| 2164 | |
| 2165 | it_key = its_cookies.second; |
| 2166 | } |
| 2167 | |
[email protected] | 4d3ce78 | 2010-10-29 18:31:28 | [diff] [blame] | 2168 | VLOG(kVlogPeriodic) |
| 2169 | << "Time for recording cookie stats (us): " |
| 2170 | << (TimeTicks::Now() - beginning_of_time).InMicroseconds(); |
[email protected] | 7a964a7 | 2010-09-07 19:33:26 | [diff] [blame] | 2171 | |
| 2172 | last_statistic_record_time_ = current_time; |
[email protected] | c4058fb | 2010-06-22 17:25:26 | [diff] [blame] | 2173 | } |
| 2174 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2175 | // Initialize all histogram counter variables used in this class. |
| 2176 | // |
| 2177 | // Normal histogram usage involves using the macros defined in |
| 2178 | // histogram.h, which automatically takes care of declaring these |
| 2179 | // variables (as statics), initializing them, and accumulating into |
| 2180 | // them, all from a single entry point. Unfortunately, that solution |
| 2181 | // doesn't work for the CookieMonster, as it's vulnerable to races between |
| 2182 | // separate threads executing the same functions and hence initializing the |
| 2183 | // same static variables. There isn't a race danger in the histogram |
| 2184 | // accumulation calls; they are written to be resilient to simultaneous |
| 2185 | // calls from multiple threads. |
| 2186 | // |
| 2187 | // The solution taken here is to have per-CookieMonster instance |
| 2188 | // variables that are constructed during CookieMonster construction. |
| 2189 | // Note that these variables refer to the same underlying histogram, |
| 2190 | // so we still race (but safely) with other CookieMonster instances |
| 2191 | // for accumulation. |
| 2192 | // |
| 2193 | // To do this we've expanded out the individual histogram macros calls, |
| 2194 | // with declarations of the variables in the class decl, initialization here |
| 2195 | // (done from the class constructor) and direct calls to the accumulation |
| 2196 | // methods where needed. The specific histogram macro calls on which the |
| 2197 | // initialization is based are included in comments below. |
| 2198 | void CookieMonster::InitializeHistograms() { |
| 2199 | // From UMA_HISTOGRAM_CUSTOM_COUNTS |
| 2200 | histogram_expiration_duration_minutes_ = base::Histogram::FactoryGet( |
| 2201 | "Cookie.ExpirationDurationMinutes", |
| 2202 | 1, kMinutesInTenYears, 50, |
| 2203 | base::Histogram::kUmaTargetedHistogramFlag); |
| 2204 | histogram_between_access_interval_minutes_ = base::Histogram::FactoryGet( |
| 2205 | "Cookie.BetweenAccessIntervalMinutes", |
| 2206 | 1, kMinutesInTenYears, 50, |
| 2207 | base::Histogram::kUmaTargetedHistogramFlag); |
| 2208 | histogram_evicted_last_access_minutes_ = base::Histogram::FactoryGet( |
| 2209 | "Cookie.EvictedLastAccessMinutes", |
| 2210 | 1, kMinutesInTenYears, 50, |
| 2211 | base::Histogram::kUmaTargetedHistogramFlag); |
| 2212 | histogram_count_ = base::Histogram::FactoryGet( |
| 2213 | "Cookie.Count", 1, 4000, 50, |
| 2214 | base::Histogram::kUmaTargetedHistogramFlag); |
| 2215 | histogram_domain_count_ = base::Histogram::FactoryGet( |
| 2216 | "Cookie.DomainCount", 1, 4000, 50, |
| 2217 | base::Histogram::kUmaTargetedHistogramFlag); |
| 2218 | histogram_etldp1_count_ = base::Histogram::FactoryGet( |
| 2219 | "Cookie.Etldp1Count", 1, 4000, 50, |
| 2220 | base::Histogram::kUmaTargetedHistogramFlag); |
| 2221 | histogram_domain_per_etldp1_count_ = base::Histogram::FactoryGet( |
| 2222 | "Cookie.DomainPerEtldp1Count", 1, 4000, 50, |
| 2223 | base::Histogram::kUmaTargetedHistogramFlag); |
| 2224 | |
| 2225 | // From UMA_HISTOGRAM_COUNTS_10000 & UMA_HISTOGRAM_CUSTOM_COUNTS |
| 2226 | histogram_number_duplicate_db_cookies_ = base::Histogram::FactoryGet( |
| 2227 | "Net.NumDuplicateCookiesInDb", 1, 10000, 50, |
| 2228 | base::Histogram::kUmaTargetedHistogramFlag); |
| 2229 | |
| 2230 | // From UMA_HISTOGRAM_ENUMERATION |
| 2231 | histogram_cookie_deletion_cause_ = base::LinearHistogram::FactoryGet( |
| 2232 | "Cookie.DeletionCause", 1, |
| 2233 | DELETE_COOKIE_LAST_ENTRY - 1, DELETE_COOKIE_LAST_ENTRY, |
| 2234 | base::Histogram::kUmaTargetedHistogramFlag); |
| 2235 | |
| 2236 | // From UMA_HISTOGRAM_{CUSTOM_,}TIMES |
| 2237 | histogram_time_get_ = base::Histogram::FactoryTimeGet("Cookie.TimeGet", |
| 2238 | base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), |
| 2239 | 50, base::Histogram::kUmaTargetedHistogramFlag); |
[email protected] | c7593fb2 | 2011-11-14 23:54:27 | [diff] [blame] | 2240 | histogram_time_blocked_on_load_ = base::Histogram::FactoryTimeGet( |
| 2241 | "Cookie.TimeBlockedOnLoad", |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 2242 | base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1), |
| 2243 | 50, base::Histogram::kUmaTargetedHistogramFlag); |
| 2244 | } |
| 2245 | |
| 2246 | |
| 2247 | // The system resolution is not high enough, so we can have multiple |
| 2248 | // set cookies that result in the same system time. When this happens, we |
| 2249 | // increment by one Time unit. Let's hope computers don't get too fast. |
| 2250 | Time CookieMonster::CurrentTime() { |
| 2251 | return std::max(Time::Now(), |
| 2252 | Time::FromInternalValue(last_time_seen_.ToInternalValue() + 1)); |
| 2253 | } |
| 2254 | |
[email protected] | 6372531 | 2012-07-19 08:24:16 | [diff] [blame] | 2255 | } // namespace net |