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