blob: 10a6f6b20ccc4d0df08a46636a6a5d85ac0baf56 [file] [log] [blame]
[email protected]8e1583672012-02-11 04:39:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5// Portions of this code based on Mozilla:
6// (netwerk/cookie/src/nsCookieService.cpp)
7/* ***** BEGIN LICENSE BLOCK *****
8 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
9 *
10 * The contents of this file are subject to the Mozilla Public License Version
11 * 1.1 (the "License"); you may not use this file except in compliance with
12 * the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
14 *
15 * Software distributed under the License is distributed on an "AS IS" basis,
16 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 * for the specific language governing rights and limitations under the
18 * License.
19 *
20 * The Original Code is mozilla.org code.
21 *
22 * The Initial Developer of the Original Code is
23 * Netscape Communications Corporation.
24 * Portions created by the Initial Developer are Copyright (C) 2003
25 * the Initial Developer. All Rights Reserved.
26 *
27 * Contributor(s):
28 * Daniel Witte ([email protected])
29 * Michiel van Leeuwen ([email protected])
30 *
31 * Alternatively, the contents of this file may be used under the terms of
32 * either the GNU General Public License Version 2 or later (the "GPL"), or
33 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
34 * in which case the provisions of the GPL or the LGPL are applicable instead
35 * of those above. If you wish to allow use of your version of this file only
36 * under the terms of either the GPL or the LGPL, and not to allow others to
37 * use your version of this file under the terms of the MPL, indicate your
38 * decision by deleting the provisions above and replace them with the notice
39 * and other provisions required by the GPL or the LGPL. If you do not delete
40 * the provisions above, a recipient may use your version of this file under
41 * the terms of any one of the MPL, the GPL or the LGPL.
42 *
43 * ***** END LICENSE BLOCK ***** */
44
[email protected]63ee33bd2012-03-15 09:29:5845#include "net/cookies/cookie_monster.h"
initial.commit586acc5fe2008-07-26 22:42:5246
47#include <algorithm>
[email protected]8ad5d462013-05-02 08:45:2648#include <functional>
[email protected]09666482011-07-12 12:50:4049#include <set>
initial.commit586acc5fe2008-07-26 22:42:5250
51#include "base/basictypes.h"
[email protected]218aa6a12011-09-13 17:38:3852#include "base/bind.h"
[email protected]8562034e2011-10-17 17:35:0453#include "base/callback.h"
initial.commit586acc5fe2008-07-26 22:42:5254#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1555#include "base/memory/scoped_ptr.h"
[email protected]28c5d0b72014-05-13 08:19:5956#include "base/memory/scoped_vector.h"
[email protected]5ee20982013-07-17 21:51:1857#include "base/message_loop/message_loop.h"
[email protected]7ccb7072013-06-10 20:56:2858#include "base/message_loop/message_loop_proxy.h"
[email protected]835d7c82010-10-14 04:38:3859#include "base/metrics/histogram.h"
[email protected]4b355212013-06-11 10:35:1960#include "base/strings/string_util.h"
61#include "base/strings/stringprintf.h"
[email protected]be28b5f42012-07-20 11:31:2562#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
[email protected]4b355212013-06-11 10:35:1963#include "net/cookies/canonical_cookie.h"
[email protected]63ee33bd2012-03-15 09:29:5864#include "net/cookies/cookie_util.h"
[email protected]ebfe3172012-07-12 12:21:4165#include "net/cookies/parsed_cookie.h"
[email protected]f89276a72013-07-12 06:41:5466#include "url/gurl.h"
initial.commit586acc5fe2008-07-26 22:42:5267
[email protected]e1acf6f2008-10-27 20:43:3368using base::Time;
69using base::TimeDelta;
[email protected]7a964a72010-09-07 19:33:2670using base::TimeTicks;
[email protected]e1acf6f2008-10-27 20:43:3371
[email protected]8562034e2011-10-17 17:35:0472// 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
[email protected]0184df32013-05-14 00:53:5575// 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.
[email protected]8562034e2011-10-17 17:35:0479//
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
[email protected]0184df32013-05-14 00:53:5584// :: 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
[email protected]8562034e2011-10-17 17:35:0489// PersistentCookieStore::LoadCookiesForKey, all subsequent requests are queued
[email protected]0184df32013-05-14 00:53:5590// 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.
[email protected]8562034e2011-10-17 17:35:0493
[email protected]c4058fb2010-06-22 17:25:2694static const int kMinutesInTenYears = 10 * 365 * 24 * 60;
95
[email protected]8ac1a752008-07-31 19:40:3796namespace net {
97
[email protected]7a964a72010-09-07 19:33:2698// See comments at declaration of these variables in cookie_monster.h
99// for details.
[email protected]8807b322010-10-01 17:10:14100const size_t CookieMonster::kDomainMaxCookies = 180;
101const size_t CookieMonster::kDomainPurgeCookies = 30;
102const size_t CookieMonster::kMaxCookies = 3300;
103const size_t CookieMonster::kPurgeCookies = 300;
[email protected]8ad5d462013-05-02 08:45:26104
105const size_t CookieMonster::kDomainCookiesQuotaLow = 30;
106const size_t CookieMonster::kDomainCookiesQuotaMedium = 50;
107const size_t CookieMonster::kDomainCookiesQuotaHigh =
[email protected]5fa4f9a2013-10-03 10:13:16108 kDomainMaxCookies - kDomainPurgeCookies
109 - kDomainCookiesQuotaLow - kDomainCookiesQuotaMedium;
[email protected]8ad5d462013-05-02 08:45:26110
[email protected]8807b322010-10-01 17:10:14111const int CookieMonster::kSafeFromGlobalPurgeDays = 30;
[email protected]297a4ed02010-02-12 08:12:52112
[email protected]7a964a72010-09-07 19:33:26113namespace {
[email protected]e32306c52008-11-06 16:59:05114
[email protected]6210ce52013-09-20 03:33:14115bool 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
[email protected]5b9bc352012-07-18 13:13:34124typedef std::vector<CanonicalCookie*> CanonicalCookieVector;
[email protected]34a160d2011-05-12 22:12:49125
[email protected]77e0a462008-11-01 00:43:35126// Default minimum delay after updating a cookie's LastAccessDate before we
127// will update it again.
[email protected]297a4ed02010-02-12 08:12:52128const int kDefaultAccessUpdateThresholdSeconds = 60;
129
130// Comparator to sort cookies from highest creation date to lowest
131// creation date.
132struct 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
[email protected]4d3ce782010-10-29 18:31:28139// Constants for use in VLOG
140const int kVlogPerCookieMonster = 1;
141const int kVlogPeriodic = 3;
142const int kVlogGarbageCollection = 5;
143const int kVlogSetCookies = 7;
144const int kVlogGetCookies = 9;
145
[email protected]f48b9432011-01-11 07:25:40146// 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.
[email protected]5b9bc352012-07-18 13:13:34149bool CookieSorter(CanonicalCookie* cc1, CanonicalCookie* cc2) {
[email protected]f48b9432011-01-11 07:25:40150 if (cc1->Path().length() == cc2->Path().length())
151 return cc1->CreationDate() < cc2->CreationDate();
152 return cc1->Path().length() > cc2->Path().length();
initial.commit586acc5fe2008-07-26 22:42:52153}
154
[email protected]8ad5d462013-05-02 08:45:26155bool LRACookieSorter(const CookieMonster::CookieMap::iterator& it1,
[email protected]f48b9432011-01-11 07:25:40156 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();
initial.commit586acc5fe2008-07-26 22:42:52160
[email protected]f48b9432011-01-11 07:25:40161 // 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();
[email protected]297a4ed02010-02-12 08:12:52165}
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.
[email protected]f48b9432011-01-11 07:25:40172//
[email protected]1655ba342010-07-14 18:17:42173// Two cookies are considered equivalent if they have the same domain,
174// name, and path.
175struct CookieSignature {
176 public:
[email protected]dedec0b2013-02-28 04:50:10177 CookieSignature(const std::string& name,
178 const std::string& domain,
[email protected]1655ba342010-07-14 18:17:42179 const std::string& path)
[email protected]dedec0b2013-02-28 04:50:10180 : name(name), domain(domain), path(path) {
181 }
[email protected]1655ba342010-07-14 18:17:42182
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};
[email protected]f48b9432011-01-11 07:25:40204
[email protected]8ad5d462013-05-02 08:45:26205// 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.
209void 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}
[email protected]f48b9432011-01-11 07:25:40216
[email protected]8ad5d462013-05-02 08:45:26217// Predicate to support PartitionCookieByPriority().
218struct CookiePriorityEqualsTo
219 : std::unary_function<const CookieMonster::CookieMap::iterator, bool> {
[email protected]5edff3c52014-06-23 20:27:48220 explicit CookiePriorityEqualsTo(CookiePriority priority)
[email protected]8ad5d462013-05-02 08:45:26221 : priority_(priority) {}
222
223 bool operator()(const CookieMonster::CookieMap::iterator it) const {
224 return it->second->Priority() == priority_;
[email protected]f48b9432011-01-11 07:25:40225 }
[email protected]8ad5d462013-05-02 08:45:26226
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|.
234CookieMonster::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
241bool 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.
250CookieMonster::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);
[email protected]7a964a72010-09-07 19:33:26256}
257
[email protected]7c4b66b2014-01-04 12:28:13258// 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.
[email protected]8bb846f2011-03-23 12:08:18261typedef struct ChangeCausePair_struct {
[email protected]7c4b66b2014-01-04 12:28:13262 CookieMonsterDelegate::ChangeCause cause;
[email protected]8bb846f2011-03-23 12:08:18263 bool notify;
264} ChangeCausePair;
265ChangeCausePair ChangeCauseMapping[] = {
266 // DELETE_COOKIE_EXPLICIT
[email protected]7c4b66b2014-01-04 12:28:13267 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, true },
[email protected]8bb846f2011-03-23 12:08:18268 // DELETE_COOKIE_OVERWRITE
[email protected]7c4b66b2014-01-04 12:28:13269 { CookieMonsterDelegate::CHANGE_COOKIE_OVERWRITE, true },
[email protected]8bb846f2011-03-23 12:08:18270 // DELETE_COOKIE_EXPIRED
[email protected]7c4b66b2014-01-04 12:28:13271 { CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED, true },
[email protected]8bb846f2011-03-23 12:08:18272 // DELETE_COOKIE_EVICTED
[email protected]7c4b66b2014-01-04 12:28:13273 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
[email protected]8bb846f2011-03-23 12:08:18274 // DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE
[email protected]7c4b66b2014-01-04 12:28:13275 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false },
[email protected]8bb846f2011-03-23 12:08:18276 // DELETE_COOKIE_DONT_RECORD
[email protected]7c4b66b2014-01-04 12:28:13277 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false },
[email protected]8bb846f2011-03-23 12:08:18278 // DELETE_COOKIE_EVICTED_DOMAIN
[email protected]7c4b66b2014-01-04 12:28:13279 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
[email protected]8bb846f2011-03-23 12:08:18280 // DELETE_COOKIE_EVICTED_GLOBAL
[email protected]7c4b66b2014-01-04 12:28:13281 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
[email protected]8bb846f2011-03-23 12:08:18282 // DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
[email protected]7c4b66b2014-01-04 12:28:13283 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
[email protected]8bb846f2011-03-23 12:08:18284 // DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
[email protected]7c4b66b2014-01-04 12:28:13285 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
[email protected]e7c590e52011-03-30 08:33:55286 // DELETE_COOKIE_EXPIRED_OVERWRITE
[email protected]7c4b66b2014-01-04 12:28:13287 { CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED_OVERWRITE, true },
[email protected]6210ce52013-09-20 03:33:14288 // DELETE_COOKIE_CONTROL_CHAR
[email protected]7c4b66b2014-01-04 12:28:13289 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true},
[email protected]8bb846f2011-03-23 12:08:18290 // DELETE_COOKIE_LAST_ENTRY
[email protected]7c4b66b2014-01-04 12:28:13291 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false }
[email protected]8bb846f2011-03-23 12:08:18292};
293
[email protected]34a160d2011-05-12 22:12:49294std::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
[email protected]f48b9432011-01-11 07:25:40310} // namespace
311
[email protected]7c4b66b2014-01-04 12:28:13312CookieMonster::CookieMonster(PersistentCookieStore* store,
313 CookieMonsterDelegate* delegate)
[email protected]f48b9432011-01-11 07:25:40314 : initialized_(false),
[email protected]28c5d0b72014-05-13 08:19:59315 loaded_(store == NULL),
[email protected]f48b9432011-01-11 07:25:40316 store_(store),
317 last_access_threshold_(
318 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)),
319 delegate_(delegate),
[email protected]82388662011-03-10 21:04:06320 last_statistic_record_time_(Time::Now()),
[email protected]93c53a32011-12-05 10:40:35321 keep_expired_cookies_(false),
[email protected]8976e292013-11-02 13:38:57322 persist_session_cookies_(false) {
[email protected]f48b9432011-01-11 07:25:40323 InitializeHistograms();
324 SetDefaultCookieableSchemes();
[email protected]2d0f89a2010-12-06 12:02:23325}
326
[email protected]f48b9432011-01-11 07:25:40327CookieMonster::CookieMonster(PersistentCookieStore* store,
[email protected]7c4b66b2014-01-04 12:28:13328 CookieMonsterDelegate* delegate,
[email protected]f48b9432011-01-11 07:25:40329 int last_access_threshold_milliseconds)
330 : initialized_(false),
[email protected]28c5d0b72014-05-13 08:19:59331 loaded_(store == NULL),
[email protected]f48b9432011-01-11 07:25:40332 store_(store),
333 last_access_threshold_(base::TimeDelta::FromMilliseconds(
334 last_access_threshold_milliseconds)),
335 delegate_(delegate),
[email protected]82388662011-03-10 21:04:06336 last_statistic_record_time_(base::Time::Now()),
[email protected]93c53a32011-12-05 10:40:35337 keep_expired_cookies_(false),
[email protected]8976e292013-11-02 13:38:57338 persist_session_cookies_(false) {
[email protected]f48b9432011-01-11 07:25:40339 InitializeHistograms();
340 SetDefaultCookieableSchemes();
initial.commit586acc5fe2008-07-26 22:42:52341}
342
initial.commit586acc5fe2008-07-26 22:42:52343
[email protected]218aa6a12011-09-13 17:38:38344// Task classes for queueing the coming request.
345
346class CookieMonster::CookieMonsterTask
347 : public base::RefCountedThreadSafe<CookieMonsterTask> {
348 public:
349 // Runs the task and invokes the client callback on the thread that
350 // originally constructed the task.
351 virtual void Run() = 0;
352
353 protected:
354 explicit CookieMonsterTask(CookieMonster* cookie_monster);
355 virtual ~CookieMonsterTask();
356
357 // Invokes the callback immediately, if the current thread is the one
358 // that originated the task, or queues the callback for execution on the
359 // appropriate thread. Maintains a reference to this CookieMonsterTask
360 // instance until the callback completes.
361 void InvokeCallback(base::Closure callback);
362
363 CookieMonster* cookie_monster() {
364 return cookie_monster_;
365 }
366
[email protected]a9813302012-04-28 09:29:28367 private:
[email protected]218aa6a12011-09-13 17:38:38368 friend class base::RefCountedThreadSafe<CookieMonsterTask>;
369
[email protected]218aa6a12011-09-13 17:38:38370 CookieMonster* cookie_monster_;
371 scoped_refptr<base::MessageLoopProxy> thread_;
372
373 DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask);
374};
375
376CookieMonster::CookieMonsterTask::CookieMonsterTask(
377 CookieMonster* cookie_monster)
378 : cookie_monster_(cookie_monster),
[email protected]a9813302012-04-28 09:29:28379 thread_(base::MessageLoopProxy::current()) {
380}
[email protected]218aa6a12011-09-13 17:38:38381
[email protected]a9813302012-04-28 09:29:28382CookieMonster::CookieMonsterTask::~CookieMonsterTask() {}
[email protected]218aa6a12011-09-13 17:38:38383
384// Unfortunately, one cannot re-bind a Callback with parameters into a closure.
385// Therefore, the closure passed to InvokeCallback is a clumsy binding of
386// Callback::Run on a wrapped Callback instance. Since Callback is not
387// reference counted, we bind to an instance that is a member of the
388// CookieMonsterTask subclass. Then, we cannot simply post the callback to a
389// message loop because the underlying instance may be destroyed (along with the
390// CookieMonsterTask instance) in the interim. Therefore, we post a callback
391// bound to the CookieMonsterTask, which *is* reference counted (thus preventing
392// destruction of the original callback), and which invokes the closure (which
393// invokes the original callback with the returned data).
394void CookieMonster::CookieMonsterTask::InvokeCallback(base::Closure callback) {
395 if (thread_->BelongsToCurrentThread()) {
396 callback.Run();
397 } else {
398 thread_->PostTask(FROM_HERE, base::Bind(
[email protected]5fa4f9a2013-10-03 10:13:16399 &CookieMonsterTask::InvokeCallback, this, callback));
[email protected]218aa6a12011-09-13 17:38:38400 }
401}
402
403// Task class for SetCookieWithDetails call.
[email protected]5fa4f9a2013-10-03 10:13:16404class CookieMonster::SetCookieWithDetailsTask : public CookieMonsterTask {
[email protected]218aa6a12011-09-13 17:38:38405 public:
[email protected]dedec0b2013-02-28 04:50:10406 SetCookieWithDetailsTask(CookieMonster* cookie_monster,
407 const GURL& url,
408 const std::string& name,
409 const std::string& value,
410 const std::string& domain,
411 const std::string& path,
412 const base::Time& expiration_time,
413 bool secure,
414 bool http_only,
[email protected]ab2d75c82013-04-19 18:39:04415 CookiePriority priority,
[email protected]5fa4f9a2013-10-03 10:13:16416 const SetCookiesCallback& callback)
[email protected]218aa6a12011-09-13 17:38:38417 : CookieMonsterTask(cookie_monster),
418 url_(url),
419 name_(name),
420 value_(value),
421 domain_(domain),
422 path_(path),
423 expiration_time_(expiration_time),
424 secure_(secure),
425 http_only_(http_only),
[email protected]ab2d75c82013-04-19 18:39:04426 priority_(priority),
[email protected]a9813302012-04-28 09:29:28427 callback_(callback) {
428 }
[email protected]218aa6a12011-09-13 17:38:38429
[email protected]5fa4f9a2013-10-03 10:13:16430 // CookieMonsterTask:
mostynbba063d6032014-10-09 11:01:13431 virtual void Run() override;
[email protected]218aa6a12011-09-13 17:38:38432
[email protected]a9813302012-04-28 09:29:28433 protected:
434 virtual ~SetCookieWithDetailsTask() {}
435
[email protected]218aa6a12011-09-13 17:38:38436 private:
437 GURL url_;
438 std::string name_;
439 std::string value_;
440 std::string domain_;
441 std::string path_;
442 base::Time expiration_time_;
443 bool secure_;
444 bool http_only_;
[email protected]ab2d75c82013-04-19 18:39:04445 CookiePriority priority_;
[email protected]5fa4f9a2013-10-03 10:13:16446 SetCookiesCallback callback_;
[email protected]218aa6a12011-09-13 17:38:38447
448 DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask);
449};
450
451void CookieMonster::SetCookieWithDetailsTask::Run() {
452 bool success = this->cookie_monster()->
453 SetCookieWithDetails(url_, name_, value_, domain_, path_,
[email protected]ab2d75c82013-04-19 18:39:04454 expiration_time_, secure_, http_only_, priority_);
[email protected]218aa6a12011-09-13 17:38:38455 if (!callback_.is_null()) {
[email protected]5fa4f9a2013-10-03 10:13:16456 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run,
[email protected]218aa6a12011-09-13 17:38:38457 base::Unretained(&callback_), success));
458 }
459}
460
461// Task class for GetAllCookies call.
[email protected]5fa4f9a2013-10-03 10:13:16462class CookieMonster::GetAllCookiesTask : public CookieMonsterTask {
[email protected]218aa6a12011-09-13 17:38:38463 public:
464 GetAllCookiesTask(CookieMonster* cookie_monster,
[email protected]5fa4f9a2013-10-03 10:13:16465 const GetCookieListCallback& callback)
[email protected]218aa6a12011-09-13 17:38:38466 : CookieMonsterTask(cookie_monster),
[email protected]a9813302012-04-28 09:29:28467 callback_(callback) {
468 }
[email protected]218aa6a12011-09-13 17:38:38469
[email protected]5fa4f9a2013-10-03 10:13:16470 // CookieMonsterTask
mostynbba063d6032014-10-09 11:01:13471 virtual void Run() override;
[email protected]218aa6a12011-09-13 17:38:38472
[email protected]a9813302012-04-28 09:29:28473 protected:
474 virtual ~GetAllCookiesTask() {}
475
[email protected]218aa6a12011-09-13 17:38:38476 private:
[email protected]5fa4f9a2013-10-03 10:13:16477 GetCookieListCallback callback_;
[email protected]218aa6a12011-09-13 17:38:38478
479 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask);
480};
481
482void CookieMonster::GetAllCookiesTask::Run() {
483 if (!callback_.is_null()) {
484 CookieList cookies = this->cookie_monster()->GetAllCookies();
[email protected]5fa4f9a2013-10-03 10:13:16485 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run,
[email protected]218aa6a12011-09-13 17:38:38486 base::Unretained(&callback_), cookies));
487 }
488}
489
490// Task class for GetAllCookiesForURLWithOptions call.
491class CookieMonster::GetAllCookiesForURLWithOptionsTask
[email protected]5fa4f9a2013-10-03 10:13:16492 : public CookieMonsterTask {
[email protected]218aa6a12011-09-13 17:38:38493 public:
494 GetAllCookiesForURLWithOptionsTask(
495 CookieMonster* cookie_monster,
496 const GURL& url,
497 const CookieOptions& options,
[email protected]5fa4f9a2013-10-03 10:13:16498 const GetCookieListCallback& callback)
[email protected]218aa6a12011-09-13 17:38:38499 : CookieMonsterTask(cookie_monster),
500 url_(url),
501 options_(options),
[email protected]a9813302012-04-28 09:29:28502 callback_(callback) {
503 }
[email protected]218aa6a12011-09-13 17:38:38504
[email protected]5fa4f9a2013-10-03 10:13:16505 // CookieMonsterTask:
mostynbba063d6032014-10-09 11:01:13506 virtual void Run() override;
[email protected]218aa6a12011-09-13 17:38:38507
[email protected]a9813302012-04-28 09:29:28508 protected:
509 virtual ~GetAllCookiesForURLWithOptionsTask() {}
510
[email protected]218aa6a12011-09-13 17:38:38511 private:
512 GURL url_;
513 CookieOptions options_;
[email protected]5fa4f9a2013-10-03 10:13:16514 GetCookieListCallback callback_;
[email protected]218aa6a12011-09-13 17:38:38515
516 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesForURLWithOptionsTask);
517};
518
519void CookieMonster::GetAllCookiesForURLWithOptionsTask::Run() {
520 if (!callback_.is_null()) {
521 CookieList cookies = this->cookie_monster()->
522 GetAllCookiesForURLWithOptions(url_, options_);
[email protected]5fa4f9a2013-10-03 10:13:16523 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run,
[email protected]218aa6a12011-09-13 17:38:38524 base::Unretained(&callback_), cookies));
525 }
526}
527
[email protected]5fa4f9a2013-10-03 10:13:16528template <typename Result> struct CallbackType {
529 typedef base::Callback<void(Result)> Type;
530};
531
532template <> struct CallbackType<void> {
533 typedef base::Closure Type;
534};
535
536// Base task class for Delete*Task.
537template <typename Result>
538class CookieMonster::DeleteTask : public CookieMonsterTask {
[email protected]218aa6a12011-09-13 17:38:38539 public:
[email protected]5fa4f9a2013-10-03 10:13:16540 DeleteTask(CookieMonster* cookie_monster,
541 const typename CallbackType<Result>::Type& callback)
[email protected]218aa6a12011-09-13 17:38:38542 : CookieMonsterTask(cookie_monster),
[email protected]a9813302012-04-28 09:29:28543 callback_(callback) {
544 }
[email protected]218aa6a12011-09-13 17:38:38545
[email protected]5fa4f9a2013-10-03 10:13:16546 // CookieMonsterTask:
mostynbba063d6032014-10-09 11:01:13547 virtual void Run() override;
[email protected]218aa6a12011-09-13 17:38:38548
[email protected]5fa4f9a2013-10-03 10:13:16549 private:
550 // Runs the delete task and returns a result.
551 virtual Result RunDeleteTask() = 0;
552 base::Closure RunDeleteTaskAndBindCallback();
553 void FlushDone(const base::Closure& callback);
554
555 typename CallbackType<Result>::Type callback_;
556
557 DISALLOW_COPY_AND_ASSIGN(DeleteTask);
558};
559
560template <typename Result>
561base::Closure CookieMonster::DeleteTask<Result>::
562RunDeleteTaskAndBindCallback() {
563 Result result = RunDeleteTask();
564 if (callback_.is_null())
565 return base::Closure();
566 return base::Bind(callback_, result);
567}
568
569template <>
570base::Closure CookieMonster::DeleteTask<void>::RunDeleteTaskAndBindCallback() {
571 RunDeleteTask();
572 return callback_;
573}
574
575template <typename Result>
576void CookieMonster::DeleteTask<Result>::Run() {
577 this->cookie_monster()->FlushStore(
578 base::Bind(&DeleteTask<Result>::FlushDone, this,
579 RunDeleteTaskAndBindCallback()));
580}
581
582template <typename Result>
583void CookieMonster::DeleteTask<Result>::FlushDone(
584 const base::Closure& callback) {
585 if (!callback.is_null()) {
586 this->InvokeCallback(callback);
587 }
588}
589
590// Task class for DeleteAll call.
591class CookieMonster::DeleteAllTask : public DeleteTask<int> {
592 public:
593 DeleteAllTask(CookieMonster* cookie_monster,
594 const DeleteCallback& callback)
[email protected]151132f2013-11-18 21:37:00595 : DeleteTask<int>(cookie_monster, callback) {
[email protected]5fa4f9a2013-10-03 10:13:16596 }
597
598 // DeleteTask:
mostynbba063d6032014-10-09 11:01:13599 virtual int RunDeleteTask() override;
[email protected]5fa4f9a2013-10-03 10:13:16600
[email protected]a9813302012-04-28 09:29:28601 protected:
602 virtual ~DeleteAllTask() {}
603
[email protected]218aa6a12011-09-13 17:38:38604 private:
[email protected]218aa6a12011-09-13 17:38:38605 DISALLOW_COPY_AND_ASSIGN(DeleteAllTask);
606};
607
[email protected]5fa4f9a2013-10-03 10:13:16608int CookieMonster::DeleteAllTask::RunDeleteTask() {
609 return this->cookie_monster()->DeleteAll(true);
[email protected]218aa6a12011-09-13 17:38:38610}
611
612// Task class for DeleteAllCreatedBetween call.
[email protected]5fa4f9a2013-10-03 10:13:16613class CookieMonster::DeleteAllCreatedBetweenTask : public DeleteTask<int> {
[email protected]218aa6a12011-09-13 17:38:38614 public:
[email protected]dedec0b2013-02-28 04:50:10615 DeleteAllCreatedBetweenTask(CookieMonster* cookie_monster,
616 const Time& delete_begin,
617 const Time& delete_end,
[email protected]5fa4f9a2013-10-03 10:13:16618 const DeleteCallback& callback)
[email protected]151132f2013-11-18 21:37:00619 : DeleteTask<int>(cookie_monster, callback),
[email protected]218aa6a12011-09-13 17:38:38620 delete_begin_(delete_begin),
[email protected]5fa4f9a2013-10-03 10:13:16621 delete_end_(delete_end) {
[email protected]a9813302012-04-28 09:29:28622 }
[email protected]218aa6a12011-09-13 17:38:38623
[email protected]5fa4f9a2013-10-03 10:13:16624 // DeleteTask:
mostynbba063d6032014-10-09 11:01:13625 virtual int RunDeleteTask() override;
[email protected]218aa6a12011-09-13 17:38:38626
[email protected]a9813302012-04-28 09:29:28627 protected:
628 virtual ~DeleteAllCreatedBetweenTask() {}
629
[email protected]218aa6a12011-09-13 17:38:38630 private:
631 Time delete_begin_;
632 Time delete_end_;
[email protected]218aa6a12011-09-13 17:38:38633
634 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask);
635};
636
[email protected]5fa4f9a2013-10-03 10:13:16637int CookieMonster::DeleteAllCreatedBetweenTask::RunDeleteTask() {
638 return this->cookie_monster()->
[email protected]218aa6a12011-09-13 17:38:38639 DeleteAllCreatedBetween(delete_begin_, delete_end_);
[email protected]218aa6a12011-09-13 17:38:38640}
641
642// Task class for DeleteAllForHost call.
[email protected]5fa4f9a2013-10-03 10:13:16643class CookieMonster::DeleteAllForHostTask : public DeleteTask<int> {
[email protected]218aa6a12011-09-13 17:38:38644 public:
645 DeleteAllForHostTask(CookieMonster* cookie_monster,
646 const GURL& url,
[email protected]5fa4f9a2013-10-03 10:13:16647 const DeleteCallback& callback)
[email protected]151132f2013-11-18 21:37:00648 : DeleteTask<int>(cookie_monster, callback),
[email protected]5fa4f9a2013-10-03 10:13:16649 url_(url) {
[email protected]a9813302012-04-28 09:29:28650 }
[email protected]218aa6a12011-09-13 17:38:38651
[email protected]5fa4f9a2013-10-03 10:13:16652 // DeleteTask:
mostynbba063d6032014-10-09 11:01:13653 virtual int RunDeleteTask() override;
[email protected]218aa6a12011-09-13 17:38:38654
[email protected]a9813302012-04-28 09:29:28655 protected:
656 virtual ~DeleteAllForHostTask() {}
657
[email protected]218aa6a12011-09-13 17:38:38658 private:
659 GURL url_;
[email protected]218aa6a12011-09-13 17:38:38660
661 DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask);
662};
663
[email protected]5fa4f9a2013-10-03 10:13:16664int CookieMonster::DeleteAllForHostTask::RunDeleteTask() {
665 return this->cookie_monster()->DeleteAllForHost(url_);
[email protected]218aa6a12011-09-13 17:38:38666}
667
[email protected]d8428d52013-08-07 06:58:25668// Task class for DeleteAllCreatedBetweenForHost call.
669class CookieMonster::DeleteAllCreatedBetweenForHostTask
[email protected]5fa4f9a2013-10-03 10:13:16670 : public DeleteTask<int> {
[email protected]d8428d52013-08-07 06:58:25671 public:
672 DeleteAllCreatedBetweenForHostTask(
673 CookieMonster* cookie_monster,
674 Time delete_begin,
675 Time delete_end,
676 const GURL& url,
[email protected]5fa4f9a2013-10-03 10:13:16677 const DeleteCallback& callback)
[email protected]151132f2013-11-18 21:37:00678 : DeleteTask<int>(cookie_monster, callback),
[email protected]d8428d52013-08-07 06:58:25679 delete_begin_(delete_begin),
680 delete_end_(delete_end),
[email protected]5fa4f9a2013-10-03 10:13:16681 url_(url) {
[email protected]d8428d52013-08-07 06:58:25682 }
683
[email protected]5fa4f9a2013-10-03 10:13:16684 // DeleteTask:
mostynbba063d6032014-10-09 11:01:13685 virtual int RunDeleteTask() override;
[email protected]d8428d52013-08-07 06:58:25686
687 protected:
688 virtual ~DeleteAllCreatedBetweenForHostTask() {}
689
690 private:
691 Time delete_begin_;
692 Time delete_end_;
693 GURL url_;
[email protected]d8428d52013-08-07 06:58:25694
695 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenForHostTask);
696};
697
[email protected]5fa4f9a2013-10-03 10:13:16698int CookieMonster::DeleteAllCreatedBetweenForHostTask::RunDeleteTask() {
699 return this->cookie_monster()->DeleteAllCreatedBetweenForHost(
[email protected]d8428d52013-08-07 06:58:25700 delete_begin_, delete_end_, url_);
[email protected]d8428d52013-08-07 06:58:25701}
702
[email protected]218aa6a12011-09-13 17:38:38703// Task class for DeleteCanonicalCookie call.
[email protected]5fa4f9a2013-10-03 10:13:16704class CookieMonster::DeleteCanonicalCookieTask : public DeleteTask<bool> {
[email protected]218aa6a12011-09-13 17:38:38705 public:
[email protected]dedec0b2013-02-28 04:50:10706 DeleteCanonicalCookieTask(CookieMonster* cookie_monster,
707 const CanonicalCookie& cookie,
[email protected]5fa4f9a2013-10-03 10:13:16708 const DeleteCookieCallback& callback)
[email protected]151132f2013-11-18 21:37:00709 : DeleteTask<bool>(cookie_monster, callback),
[email protected]5fa4f9a2013-10-03 10:13:16710 cookie_(cookie) {
[email protected]a9813302012-04-28 09:29:28711 }
[email protected]218aa6a12011-09-13 17:38:38712
[email protected]5fa4f9a2013-10-03 10:13:16713 // DeleteTask:
mostynbba063d6032014-10-09 11:01:13714 virtual bool RunDeleteTask() override;
[email protected]218aa6a12011-09-13 17:38:38715
[email protected]a9813302012-04-28 09:29:28716 protected:
717 virtual ~DeleteCanonicalCookieTask() {}
718
[email protected]218aa6a12011-09-13 17:38:38719 private:
[email protected]5b9bc352012-07-18 13:13:34720 CanonicalCookie cookie_;
[email protected]218aa6a12011-09-13 17:38:38721
722 DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask);
723};
724
[email protected]5fa4f9a2013-10-03 10:13:16725bool CookieMonster::DeleteCanonicalCookieTask::RunDeleteTask() {
726 return this->cookie_monster()->DeleteCanonicalCookie(cookie_);
[email protected]218aa6a12011-09-13 17:38:38727}
728
729// Task class for SetCookieWithOptions call.
[email protected]5fa4f9a2013-10-03 10:13:16730class CookieMonster::SetCookieWithOptionsTask : public CookieMonsterTask {
[email protected]218aa6a12011-09-13 17:38:38731 public:
732 SetCookieWithOptionsTask(CookieMonster* cookie_monster,
733 const GURL& url,
734 const std::string& cookie_line,
735 const CookieOptions& options,
[email protected]5fa4f9a2013-10-03 10:13:16736 const SetCookiesCallback& callback)
[email protected]218aa6a12011-09-13 17:38:38737 : CookieMonsterTask(cookie_monster),
738 url_(url),
739 cookie_line_(cookie_line),
740 options_(options),
[email protected]a9813302012-04-28 09:29:28741 callback_(callback) {
742 }
[email protected]218aa6a12011-09-13 17:38:38743
[email protected]5fa4f9a2013-10-03 10:13:16744 // CookieMonsterTask:
mostynbba063d6032014-10-09 11:01:13745 virtual void Run() override;
[email protected]218aa6a12011-09-13 17:38:38746
[email protected]a9813302012-04-28 09:29:28747 protected:
748 virtual ~SetCookieWithOptionsTask() {}
749
[email protected]218aa6a12011-09-13 17:38:38750 private:
751 GURL url_;
752 std::string cookie_line_;
753 CookieOptions options_;
[email protected]5fa4f9a2013-10-03 10:13:16754 SetCookiesCallback callback_;
[email protected]218aa6a12011-09-13 17:38:38755
756 DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask);
757};
758
759void CookieMonster::SetCookieWithOptionsTask::Run() {
760 bool result = this->cookie_monster()->
761 SetCookieWithOptions(url_, cookie_line_, options_);
762 if (!callback_.is_null()) {
[email protected]5fa4f9a2013-10-03 10:13:16763 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run,
[email protected]218aa6a12011-09-13 17:38:38764 base::Unretained(&callback_), result));
765 }
766}
767
768// Task class for GetCookiesWithOptions call.
[email protected]5fa4f9a2013-10-03 10:13:16769class CookieMonster::GetCookiesWithOptionsTask : public CookieMonsterTask {
[email protected]218aa6a12011-09-13 17:38:38770 public:
771 GetCookiesWithOptionsTask(CookieMonster* cookie_monster,
[email protected]0298caf82011-12-20 23:15:46772 const GURL& url,
[email protected]218aa6a12011-09-13 17:38:38773 const CookieOptions& options,
[email protected]5fa4f9a2013-10-03 10:13:16774 const GetCookiesCallback& callback)
[email protected]218aa6a12011-09-13 17:38:38775 : CookieMonsterTask(cookie_monster),
776 url_(url),
777 options_(options),
[email protected]a9813302012-04-28 09:29:28778 callback_(callback) {
779 }
[email protected]218aa6a12011-09-13 17:38:38780
[email protected]5fa4f9a2013-10-03 10:13:16781 // CookieMonsterTask:
mostynbba063d6032014-10-09 11:01:13782 virtual void Run() override;
[email protected]218aa6a12011-09-13 17:38:38783
[email protected]a9813302012-04-28 09:29:28784 protected:
785 virtual ~GetCookiesWithOptionsTask() {}
786
[email protected]218aa6a12011-09-13 17:38:38787 private:
788 GURL url_;
789 CookieOptions options_;
[email protected]5fa4f9a2013-10-03 10:13:16790 GetCookiesCallback callback_;
[email protected]218aa6a12011-09-13 17:38:38791
792 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask);
793};
794
795void CookieMonster::GetCookiesWithOptionsTask::Run() {
796 std::string cookie = this->cookie_monster()->
797 GetCookiesWithOptions(url_, options_);
798 if (!callback_.is_null()) {
[email protected]5fa4f9a2013-10-03 10:13:16799 this->InvokeCallback(base::Bind(&GetCookiesCallback::Run,
[email protected]218aa6a12011-09-13 17:38:38800 base::Unretained(&callback_), cookie));
801 }
802}
803
[email protected]218aa6a12011-09-13 17:38:38804// Task class for DeleteCookie call.
[email protected]5fa4f9a2013-10-03 10:13:16805class CookieMonster::DeleteCookieTask : public DeleteTask<void> {
[email protected]218aa6a12011-09-13 17:38:38806 public:
807 DeleteCookieTask(CookieMonster* cookie_monster,
[email protected]0298caf82011-12-20 23:15:46808 const GURL& url,
[email protected]218aa6a12011-09-13 17:38:38809 const std::string& cookie_name,
810 const base::Closure& callback)
[email protected]151132f2013-11-18 21:37:00811 : DeleteTask<void>(cookie_monster, callback),
[email protected]218aa6a12011-09-13 17:38:38812 url_(url),
[email protected]5fa4f9a2013-10-03 10:13:16813 cookie_name_(cookie_name) {
814 }
[email protected]218aa6a12011-09-13 17:38:38815
[email protected]5fa4f9a2013-10-03 10:13:16816 // DeleteTask:
mostynbba063d6032014-10-09 11:01:13817 virtual void RunDeleteTask() override;
[email protected]218aa6a12011-09-13 17:38:38818
[email protected]a9813302012-04-28 09:29:28819 protected:
820 virtual ~DeleteCookieTask() {}
821
[email protected]218aa6a12011-09-13 17:38:38822 private:
823 GURL url_;
824 std::string cookie_name_;
[email protected]218aa6a12011-09-13 17:38:38825
826 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
827};
828
[email protected]5fa4f9a2013-10-03 10:13:16829void CookieMonster::DeleteCookieTask::RunDeleteTask() {
[email protected]218aa6a12011-09-13 17:38:38830 this->cookie_monster()->DeleteCookie(url_, cookie_name_);
[email protected]218aa6a12011-09-13 17:38:38831}
832
[email protected]264807b2012-04-25 14:49:37833// Task class for DeleteSessionCookies call.
[email protected]5fa4f9a2013-10-03 10:13:16834class CookieMonster::DeleteSessionCookiesTask : public DeleteTask<int> {
[email protected]264807b2012-04-25 14:49:37835 public:
[email protected]dedec0b2013-02-28 04:50:10836 DeleteSessionCookiesTask(CookieMonster* cookie_monster,
[email protected]5fa4f9a2013-10-03 10:13:16837 const DeleteCallback& callback)
[email protected]151132f2013-11-18 21:37:00838 : DeleteTask<int>(cookie_monster, callback) {
[email protected]a9813302012-04-28 09:29:28839 }
[email protected]264807b2012-04-25 14:49:37840
[email protected]5fa4f9a2013-10-03 10:13:16841 // DeleteTask:
mostynbba063d6032014-10-09 11:01:13842 virtual int RunDeleteTask() override;
[email protected]264807b2012-04-25 14:49:37843
[email protected]a9813302012-04-28 09:29:28844 protected:
845 virtual ~DeleteSessionCookiesTask() {}
846
[email protected]264807b2012-04-25 14:49:37847 private:
[email protected]264807b2012-04-25 14:49:37848 DISALLOW_COPY_AND_ASSIGN(DeleteSessionCookiesTask);
849};
850
[email protected]5fa4f9a2013-10-03 10:13:16851int CookieMonster::DeleteSessionCookiesTask::RunDeleteTask() {
852 return this->cookie_monster()->DeleteSessionCookies();
[email protected]264807b2012-04-25 14:49:37853}
854
[email protected]ee209482013-04-19 19:50:04855// Task class for HasCookiesForETLDP1Task call.
[email protected]5fa4f9a2013-10-03 10:13:16856class CookieMonster::HasCookiesForETLDP1Task : public CookieMonsterTask {
[email protected]ee209482013-04-19 19:50:04857 public:
858 HasCookiesForETLDP1Task(
859 CookieMonster* cookie_monster,
860 const std::string& etldp1,
[email protected]5fa4f9a2013-10-03 10:13:16861 const HasCookiesForETLDP1Callback& callback)
[email protected]ee209482013-04-19 19:50:04862 : CookieMonsterTask(cookie_monster),
863 etldp1_(etldp1),
864 callback_(callback) {
865 }
866
[email protected]5fa4f9a2013-10-03 10:13:16867 // CookieMonsterTask:
mostynbba063d6032014-10-09 11:01:13868 virtual void Run() override;
[email protected]ee209482013-04-19 19:50:04869
870 protected:
871 virtual ~HasCookiesForETLDP1Task() {}
872
873 private:
874 std::string etldp1_;
[email protected]5fa4f9a2013-10-03 10:13:16875 HasCookiesForETLDP1Callback callback_;
[email protected]ee209482013-04-19 19:50:04876
877 DISALLOW_COPY_AND_ASSIGN(HasCookiesForETLDP1Task);
878};
879
880void CookieMonster::HasCookiesForETLDP1Task::Run() {
881 bool result = this->cookie_monster()->HasCookiesForETLDP1(etldp1_);
882 if (!callback_.is_null()) {
883 this->InvokeCallback(
[email protected]5fa4f9a2013-10-03 10:13:16884 base::Bind(&HasCookiesForETLDP1Callback::Run,
[email protected]ee209482013-04-19 19:50:04885 base::Unretained(&callback_), result));
886 }
887}
888
[email protected]218aa6a12011-09-13 17:38:38889// Asynchronous CookieMonster API
890
891void CookieMonster::SetCookieWithDetailsAsync(
[email protected]dedec0b2013-02-28 04:50:10892 const GURL& url,
893 const std::string& name,
894 const std::string& value,
895 const std::string& domain,
896 const std::string& path,
[email protected]d8428d52013-08-07 06:58:25897 const Time& expiration_time,
[email protected]dedec0b2013-02-28 04:50:10898 bool secure,
899 bool http_only,
[email protected]ab2d75c82013-04-19 18:39:04900 CookiePriority priority,
[email protected]218aa6a12011-09-13 17:38:38901 const SetCookiesCallback& callback) {
902 scoped_refptr<SetCookieWithDetailsTask> task =
903 new SetCookieWithDetailsTask(this, url, name, value, domain, path,
[email protected]ab2d75c82013-04-19 18:39:04904 expiration_time, secure, http_only, priority,
[email protected]218aa6a12011-09-13 17:38:38905 callback);
906
[email protected]8562034e2011-10-17 17:35:04907 DoCookieTaskForURL(task, url);
[email protected]218aa6a12011-09-13 17:38:38908}
909
910void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) {
911 scoped_refptr<GetAllCookiesTask> task =
912 new GetAllCookiesTask(this, callback);
913
914 DoCookieTask(task);
915}
916
917
918void CookieMonster::GetAllCookiesForURLWithOptionsAsync(
919 const GURL& url,
920 const CookieOptions& options,
921 const GetCookieListCallback& callback) {
922 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
923 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
924
[email protected]8562034e2011-10-17 17:35:04925 DoCookieTaskForURL(task, url);
[email protected]218aa6a12011-09-13 17:38:38926}
927
928void CookieMonster::GetAllCookiesForURLAsync(
929 const GURL& url, const GetCookieListCallback& callback) {
930 CookieOptions options;
931 options.set_include_httponly();
932 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
933 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
934
[email protected]8562034e2011-10-17 17:35:04935 DoCookieTaskForURL(task, url);
[email protected]218aa6a12011-09-13 17:38:38936}
937
[email protected]ee209482013-04-19 19:50:04938void CookieMonster::HasCookiesForETLDP1Async(
939 const std::string& etldp1,
940 const HasCookiesForETLDP1Callback& callback) {
941 scoped_refptr<HasCookiesForETLDP1Task> task =
942 new HasCookiesForETLDP1Task(this, etldp1, callback);
943
944 DoCookieTaskForURL(task, GURL("http://" + etldp1));
945}
946
[email protected]218aa6a12011-09-13 17:38:38947void CookieMonster::DeleteAllAsync(const DeleteCallback& callback) {
948 scoped_refptr<DeleteAllTask> task =
949 new DeleteAllTask(this, callback);
950
951 DoCookieTask(task);
952}
953
954void CookieMonster::DeleteAllCreatedBetweenAsync(
955 const Time& delete_begin, const Time& delete_end,
956 const DeleteCallback& callback) {
957 scoped_refptr<DeleteAllCreatedBetweenTask> task =
958 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end,
959 callback);
960
961 DoCookieTask(task);
962}
963
[email protected]d8428d52013-08-07 06:58:25964void CookieMonster::DeleteAllCreatedBetweenForHostAsync(
965 const Time delete_begin,
966 const Time delete_end,
967 const GURL& url,
968 const DeleteCallback& callback) {
969 scoped_refptr<DeleteAllCreatedBetweenForHostTask> task =
970 new DeleteAllCreatedBetweenForHostTask(
971 this, delete_begin, delete_end, url, callback);
972
973 DoCookieTaskForURL(task, url);
974}
975
[email protected]218aa6a12011-09-13 17:38:38976void CookieMonster::DeleteAllForHostAsync(
977 const GURL& url, const DeleteCallback& callback) {
978 scoped_refptr<DeleteAllForHostTask> task =
979 new DeleteAllForHostTask(this, url, callback);
980
[email protected]8562034e2011-10-17 17:35:04981 DoCookieTaskForURL(task, url);
[email protected]218aa6a12011-09-13 17:38:38982}
983
984void CookieMonster::DeleteCanonicalCookieAsync(
985 const CanonicalCookie& cookie,
986 const DeleteCookieCallback& callback) {
987 scoped_refptr<DeleteCanonicalCookieTask> task =
988 new DeleteCanonicalCookieTask(this, cookie, callback);
989
990 DoCookieTask(task);
991}
992
993void CookieMonster::SetCookieWithOptionsAsync(
994 const GURL& url,
995 const std::string& cookie_line,
996 const CookieOptions& options,
997 const SetCookiesCallback& callback) {
998 scoped_refptr<SetCookieWithOptionsTask> task =
999 new SetCookieWithOptionsTask(this, url, cookie_line, options, callback);
1000
[email protected]8562034e2011-10-17 17:35:041001 DoCookieTaskForURL(task, url);
[email protected]218aa6a12011-09-13 17:38:381002}
1003
1004void CookieMonster::GetCookiesWithOptionsAsync(
1005 const GURL& url,
1006 const CookieOptions& options,
1007 const GetCookiesCallback& callback) {
1008 scoped_refptr<GetCookiesWithOptionsTask> task =
1009 new GetCookiesWithOptionsTask(this, url, options, callback);
1010
[email protected]8562034e2011-10-17 17:35:041011 DoCookieTaskForURL(task, url);
[email protected]218aa6a12011-09-13 17:38:381012}
1013
[email protected]218aa6a12011-09-13 17:38:381014void CookieMonster::DeleteCookieAsync(const GURL& url,
1015 const std::string& cookie_name,
1016 const base::Closure& callback) {
1017 scoped_refptr<DeleteCookieTask> task =
1018 new DeleteCookieTask(this, url, cookie_name, callback);
1019
[email protected]8562034e2011-10-17 17:35:041020 DoCookieTaskForURL(task, url);
[email protected]218aa6a12011-09-13 17:38:381021}
1022
[email protected]264807b2012-04-25 14:49:371023void CookieMonster::DeleteSessionCookiesAsync(
1024 const CookieStore::DeleteCallback& callback) {
1025 scoped_refptr<DeleteSessionCookiesTask> task =
1026 new DeleteSessionCookiesTask(this, callback);
1027
1028 DoCookieTask(task);
1029}
1030
[email protected]218aa6a12011-09-13 17:38:381031void CookieMonster::DoCookieTask(
1032 const scoped_refptr<CookieMonsterTask>& task_item) {
[email protected]218aa6a12011-09-13 17:38:381033 {
1034 base::AutoLock autolock(lock_);
[email protected]8562034e2011-10-17 17:35:041035 InitIfNecessary();
[email protected]218aa6a12011-09-13 17:38:381036 if (!loaded_) {
[email protected]0184df32013-05-14 00:53:551037 tasks_pending_.push(task_item);
[email protected]218aa6a12011-09-13 17:38:381038 return;
1039 }
1040 }
1041
1042 task_item->Run();
1043}
1044
[email protected]8562034e2011-10-17 17:35:041045void CookieMonster::DoCookieTaskForURL(
1046 const scoped_refptr<CookieMonsterTask>& task_item,
1047 const GURL& url) {
1048 {
1049 base::AutoLock autolock(lock_);
1050 InitIfNecessary();
1051 // If cookies for the requested domain key (eTLD+1) have been loaded from DB
1052 // then run the task, otherwise load from DB.
1053 if (!loaded_) {
1054 // Checks if the domain key has been loaded.
[email protected]2fb376a2011-11-17 09:22:031055 std::string key(cookie_util::GetEffectiveDomain(url.scheme(),
1056 url.host()));
[email protected]8562034e2011-10-17 17:35:041057 if (keys_loaded_.find(key) == keys_loaded_.end()) {
1058 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > >
[email protected]0184df32013-05-14 00:53:551059 ::iterator it = tasks_pending_for_key_.find(key);
1060 if (it == tasks_pending_for_key_.end()) {
[email protected]8562034e2011-10-17 17:35:041061 store_->LoadCookiesForKey(key,
1062 base::Bind(&CookieMonster::OnKeyLoaded, this, key));
[email protected]0184df32013-05-14 00:53:551063 it = tasks_pending_for_key_.insert(std::make_pair(key,
[email protected]8562034e2011-10-17 17:35:041064 std::deque<scoped_refptr<CookieMonsterTask> >())).first;
1065 }
1066 it->second.push_back(task_item);
1067 return;
1068 }
1069 }
1070 }
1071 task_item->Run();
1072}
1073
[email protected]dedec0b2013-02-28 04:50:101074bool CookieMonster::SetCookieWithDetails(const GURL& url,
1075 const std::string& name,
1076 const std::string& value,
1077 const std::string& domain,
1078 const std::string& path,
1079 const base::Time& expiration_time,
1080 bool secure,
[email protected]ab2d75c82013-04-19 18:39:041081 bool http_only,
1082 CookiePriority priority) {
[email protected]20305ec2011-01-21 04:55:521083 base::AutoLock autolock(lock_);
[email protected]69bb5872010-01-12 20:33:521084
[email protected]f48b9432011-01-11 07:25:401085 if (!HasCookieableScheme(url))
initial.commit586acc5fe2008-07-26 22:42:521086 return false;
1087
[email protected]f48b9432011-01-11 07:25:401088 Time creation_time = CurrentTime();
1089 last_time_seen_ = creation_time;
1090
1091 scoped_ptr<CanonicalCookie> cc;
[email protected]ab2d75c82013-04-19 18:39:041092 cc.reset(CanonicalCookie::Create(url, name, value, domain, path,
1093 creation_time, expiration_time,
1094 secure, http_only, priority));
[email protected]f48b9432011-01-11 07:25:401095
1096 if (!cc.get())
1097 return false;
1098
1099 CookieOptions options;
1100 options.set_include_httponly();
1101 return SetCanonicalCookie(&cc, creation_time, options);
initial.commit586acc5fe2008-07-26 22:42:521102}
1103
bartfaba13acdf2014-09-05 15:07:281104bool CookieMonster::ImportCookies(const CookieList& list) {
[email protected]93460df2011-07-20 00:58:211105 base::AutoLock autolock(lock_);
1106 InitIfNecessary();
1107 for (net::CookieList::const_iterator iter = list.begin();
1108 iter != list.end(); ++iter) {
[email protected]5b9bc352012-07-18 13:13:341109 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie(*iter));
[email protected]93460df2011-07-20 00:58:211110 net::CookieOptions options;
1111 options.set_include_httponly();
[email protected]5b9bc352012-07-18 13:13:341112 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), options))
[email protected]93460df2011-07-20 00:58:211113 return false;
[email protected]93460df2011-07-20 00:58:211114 }
1115 return true;
1116}
1117
[email protected]f48b9432011-01-11 07:25:401118CookieList CookieMonster::GetAllCookies() {
[email protected]20305ec2011-01-21 04:55:521119 base::AutoLock autolock(lock_);
[email protected]f48b9432011-01-11 07:25:401120
1121 // This function is being called to scrape the cookie list for management UI
1122 // or similar. We shouldn't show expired cookies in this list since it will
1123 // just be confusing to users, and this function is called rarely enough (and
1124 // is already slow enough) that it's OK to take the time to garbage collect
1125 // the expired cookies now.
1126 //
1127 // Note that this does not prune cookies to be below our limits (if we've
1128 // exceeded them) the way that calling GarbageCollect() would.
1129 GarbageCollectExpired(Time::Now(),
1130 CookieMapItPair(cookies_.begin(), cookies_.end()),
1131 NULL);
1132
1133 // Copy the CanonicalCookie pointers from the map so that we can use the same
1134 // sorter as elsewhere, then copy the result out.
1135 std::vector<CanonicalCookie*> cookie_ptrs;
1136 cookie_ptrs.reserve(cookies_.size());
1137 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end(); ++it)
1138 cookie_ptrs.push_back(it->second);
1139 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
1140
1141 CookieList cookie_list;
1142 cookie_list.reserve(cookie_ptrs.size());
1143 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
1144 it != cookie_ptrs.end(); ++it)
1145 cookie_list.push_back(**it);
1146
1147 return cookie_list;
[email protected]f325f1e12010-04-30 22:38:551148}
1149
[email protected]f48b9432011-01-11 07:25:401150CookieList CookieMonster::GetAllCookiesForURLWithOptions(
1151 const GURL& url,
1152 const CookieOptions& options) {
[email protected]20305ec2011-01-21 04:55:521153 base::AutoLock autolock(lock_);
initial.commit586acc5fe2008-07-26 22:42:521154
[email protected]f48b9432011-01-11 07:25:401155 std::vector<CanonicalCookie*> cookie_ptrs;
1156 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs);
1157 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
initial.commit586acc5fe2008-07-26 22:42:521158
[email protected]f48b9432011-01-11 07:25:401159 CookieList cookies;
1160 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
1161 it != cookie_ptrs.end(); it++)
1162 cookies.push_back(**it);
initial.commit586acc5fe2008-07-26 22:42:521163
[email protected]f48b9432011-01-11 07:25:401164 return cookies;
initial.commit586acc5fe2008-07-26 22:42:521165}
1166
[email protected]f48b9432011-01-11 07:25:401167CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) {
1168 CookieOptions options;
1169 options.set_include_httponly();
1170
1171 return GetAllCookiesForURLWithOptions(url, options);
[email protected]f325f1e12010-04-30 22:38:551172}
1173
[email protected]f48b9432011-01-11 07:25:401174int CookieMonster::DeleteAll(bool sync_to_store) {
[email protected]20305ec2011-01-21 04:55:521175 base::AutoLock autolock(lock_);
[email protected]f48b9432011-01-11 07:25:401176
1177 int num_deleted = 0;
1178 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1179 CookieMap::iterator curit = it;
1180 ++it;
1181 InternalDeleteCookie(curit, sync_to_store,
1182 sync_to_store ? DELETE_COOKIE_EXPLICIT :
1183 DELETE_COOKIE_DONT_RECORD /* Destruction. */);
1184 ++num_deleted;
initial.commit586acc5fe2008-07-26 22:42:521185 }
1186
[email protected]f48b9432011-01-11 07:25:401187 return num_deleted;
initial.commit586acc5fe2008-07-26 22:42:521188}
1189
[email protected]f48b9432011-01-11 07:25:401190int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin,
[email protected]218aa6a12011-09-13 17:38:381191 const Time& delete_end) {
[email protected]20305ec2011-01-21 04:55:521192 base::AutoLock autolock(lock_);
[email protected]d0980332010-11-16 17:08:531193
[email protected]f48b9432011-01-11 07:25:401194 int num_deleted = 0;
1195 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1196 CookieMap::iterator curit = it;
1197 CanonicalCookie* cc = curit->second;
1198 ++it;
[email protected]d0980332010-11-16 17:08:531199
[email protected]f48b9432011-01-11 07:25:401200 if (cc->CreationDate() >= delete_begin &&
1201 (delete_end.is_null() || cc->CreationDate() < delete_end)) {
[email protected]218aa6a12011-09-13 17:38:381202 InternalDeleteCookie(curit,
1203 true, /*sync_to_store*/
1204 DELETE_COOKIE_EXPLICIT);
[email protected]f48b9432011-01-11 07:25:401205 ++num_deleted;
initial.commit586acc5fe2008-07-26 22:42:521206 }
1207 }
1208
[email protected]f48b9432011-01-11 07:25:401209 return num_deleted;
1210}
1211
[email protected]d8428d52013-08-07 06:58:251212int CookieMonster::DeleteAllCreatedBetweenForHost(const Time delete_begin,
1213 const Time delete_end,
1214 const GURL& url) {
[email protected]20305ec2011-01-21 04:55:521215 base::AutoLock autolock(lock_);
[email protected]f48b9432011-01-11 07:25:401216
1217 if (!HasCookieableScheme(url))
1218 return 0;
1219
[email protected]f48b9432011-01-11 07:25:401220 const std::string host(url.host());
1221
1222 // We store host cookies in the store by their canonical host name;
1223 // domain cookies are stored with a leading ".". So this is a pretty
1224 // simple lookup and per-cookie delete.
1225 int num_deleted = 0;
1226 for (CookieMapItPair its = cookies_.equal_range(GetKey(host));
1227 its.first != its.second;) {
1228 CookieMap::iterator curit = its.first;
1229 ++its.first;
1230
1231 const CanonicalCookie* const cc = curit->second;
1232
1233 // Delete only on a match as a host cookie.
[email protected]d8428d52013-08-07 06:58:251234 if (cc->IsHostCookie() && cc->IsDomainMatch(host) &&
1235 cc->CreationDate() >= delete_begin &&
1236 // The assumption that null |delete_end| is equivalent to
1237 // Time::Max() is confusing.
1238 (delete_end.is_null() || cc->CreationDate() < delete_end)) {
[email protected]f48b9432011-01-11 07:25:401239 num_deleted++;
1240
1241 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT);
1242 }
1243 }
1244 return num_deleted;
1245}
1246
[email protected]d8428d52013-08-07 06:58:251247int CookieMonster::DeleteAllForHost(const GURL& url) {
1248 return DeleteAllCreatedBetweenForHost(Time(), Time::Max(), url);
1249}
1250
1251
[email protected]f48b9432011-01-11 07:25:401252bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) {
[email protected]20305ec2011-01-21 04:55:521253 base::AutoLock autolock(lock_);
[email protected]f48b9432011-01-11 07:25:401254
1255 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain()));
1256 its.first != its.second; ++its.first) {
1257 // The creation date acts as our unique index...
1258 if (its.first->second->CreationDate() == cookie.CreationDate()) {
1259 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT);
1260 return true;
1261 }
1262 }
initial.commit586acc5fe2008-07-26 22:42:521263 return false;
1264}
1265
[email protected]5edff3c52014-06-23 20:27:481266void CookieMonster::SetCookieableSchemes(const char* const schemes[],
[email protected]dedec0b2013-02-28 04:50:101267 size_t num_schemes) {
[email protected]20305ec2011-01-21 04:55:521268 base::AutoLock autolock(lock_);
[email protected]bb8905722010-05-21 17:29:041269
[email protected]cf12bd12010-06-17 14:41:301270 // Cookieable Schemes must be set before first use of function.
1271 DCHECK(!initialized_);
1272
[email protected]47accfd62009-05-14 18:46:211273 cookieable_schemes_.clear();
1274 cookieable_schemes_.insert(cookieable_schemes_.end(),
1275 schemes, schemes + num_schemes);
1276}
1277
[email protected]97a3b6e2012-06-12 01:53:561278void CookieMonster::SetEnableFileScheme(bool accept) {
1279 // This assumes "file" is always at the end of the array. See the comment
1280 // above kDefaultCookieableSchemes.
1281 int num_schemes = accept ? kDefaultCookieableSchemesCount :
1282 kDefaultCookieableSchemesCount - 1;
1283 SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes);
1284}
1285
[email protected]ba4ad0e2011-03-15 08:12:471286void CookieMonster::SetKeepExpiredCookies() {
1287 keep_expired_cookies_ = true;
1288}
1289
[email protected]e67f0f42011-12-20 02:29:211290void CookieMonster::FlushStore(const base::Closure& callback) {
[email protected]20305ec2011-01-21 04:55:521291 base::AutoLock autolock(lock_);
[email protected]90499482013-06-01 00:39:501292 if (initialized_ && store_.get())
[email protected]e67f0f42011-12-20 02:29:211293 store_->Flush(callback);
1294 else if (!callback.is_null())
[email protected]2da659e2013-05-23 20:51:341295 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
[email protected]f48b9432011-01-11 07:25:401296}
1297
1298bool CookieMonster::SetCookieWithOptions(const GURL& url,
1299 const std::string& cookie_line,
1300 const CookieOptions& options) {
[email protected]20305ec2011-01-21 04:55:521301 base::AutoLock autolock(lock_);
[email protected]f48b9432011-01-11 07:25:401302
1303 if (!HasCookieableScheme(url)) {
1304 return false;
1305 }
1306
[email protected]f48b9432011-01-11 07:25:401307 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options);
1308}
1309
1310std::string CookieMonster::GetCookiesWithOptions(const GURL& url,
1311 const CookieOptions& options) {
[email protected]20305ec2011-01-21 04:55:521312 base::AutoLock autolock(lock_);
[email protected]f48b9432011-01-11 07:25:401313
[email protected]34a160d2011-05-12 22:12:491314 if (!HasCookieableScheme(url))
[email protected]f48b9432011-01-11 07:25:401315 return std::string();
[email protected]f48b9432011-01-11 07:25:401316
1317 TimeTicks start_time(TimeTicks::Now());
1318
[email protected]f48b9432011-01-11 07:25:401319 std::vector<CanonicalCookie*> cookies;
1320 FindCookiesForHostAndDomain(url, options, true, &cookies);
1321 std::sort(cookies.begin(), cookies.end(), CookieSorter);
1322
[email protected]34a160d2011-05-12 22:12:491323 std::string cookie_line = BuildCookieLine(cookies);
[email protected]f48b9432011-01-11 07:25:401324
1325 histogram_time_get_->AddTime(TimeTicks::Now() - start_time);
1326
1327 VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line;
1328
1329 return cookie_line;
1330}
1331
1332void CookieMonster::DeleteCookie(const GURL& url,
1333 const std::string& cookie_name) {
[email protected]20305ec2011-01-21 04:55:521334 base::AutoLock autolock(lock_);
[email protected]f48b9432011-01-11 07:25:401335
1336 if (!HasCookieableScheme(url))
1337 return;
1338
1339 CookieOptions options;
1340 options.set_include_httponly();
1341 // Get the cookies for this host and its domain(s).
1342 std::vector<CanonicalCookie*> cookies;
1343 FindCookiesForHostAndDomain(url, options, true, &cookies);
1344 std::set<CanonicalCookie*> matching_cookies;
1345
1346 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin();
1347 it != cookies.end(); ++it) {
1348 if ((*it)->Name() != cookie_name)
1349 continue;
1350 if (url.path().find((*it)->Path()))
1351 continue;
1352 matching_cookies.insert(*it);
1353 }
1354
1355 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1356 CookieMap::iterator curit = it;
1357 ++it;
1358 if (matching_cookies.find(curit->second) != matching_cookies.end()) {
1359 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT);
1360 }
1361 }
1362}
1363
[email protected]264807b2012-04-25 14:49:371364int CookieMonster::DeleteSessionCookies() {
1365 base::AutoLock autolock(lock_);
1366
1367 int num_deleted = 0;
1368 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1369 CookieMap::iterator curit = it;
1370 CanonicalCookie* cc = curit->second;
1371 ++it;
1372
1373 if (!cc->IsPersistent()) {
1374 InternalDeleteCookie(curit,
1375 true, /*sync_to_store*/
1376 DELETE_COOKIE_EXPIRED);
1377 ++num_deleted;
1378 }
1379 }
1380
1381 return num_deleted;
1382}
1383
[email protected]ee209482013-04-19 19:50:041384bool CookieMonster::HasCookiesForETLDP1(const std::string& etldp1) {
1385 base::AutoLock autolock(lock_);
1386
1387 const std::string key(GetKey(etldp1));
1388
1389 CookieMapItPair its = cookies_.equal_range(key);
1390 return its.first != its.second;
1391}
1392
[email protected]f48b9432011-01-11 07:25:401393CookieMonster* CookieMonster::GetCookieMonster() {
1394 return this;
1395}
1396
[email protected]8ad5d462013-05-02 08:45:261397// This function must be called before the CookieMonster is used.
[email protected]93c53a32011-12-05 10:40:351398void CookieMonster::SetPersistSessionCookies(bool persist_session_cookies) {
[email protected]93c53a32011-12-05 10:40:351399 DCHECK(!initialized_);
1400 persist_session_cookies_ = persist_session_cookies;
1401}
1402
[email protected]bf510ed2012-06-05 08:31:431403void CookieMonster::SetForceKeepSessionState() {
[email protected]90499482013-06-01 00:39:501404 if (store_.get()) {
[email protected]bf510ed2012-06-05 08:31:431405 store_->SetForceKeepSessionState();
[email protected]93c53a32011-12-05 10:40:351406 }
1407}
1408
[email protected]f48b9432011-01-11 07:25:401409CookieMonster::~CookieMonster() {
1410 DeleteAll(false);
1411}
1412
1413bool CookieMonster::SetCookieWithCreationTime(const GURL& url,
1414 const std::string& cookie_line,
1415 const base::Time& creation_time) {
[email protected]90499482013-06-01 00:39:501416 DCHECK(!store_.get()) << "This method is only to be used by unit-tests.";
[email protected]20305ec2011-01-21 04:55:521417 base::AutoLock autolock(lock_);
[email protected]f48b9432011-01-11 07:25:401418
1419 if (!HasCookieableScheme(url)) {
1420 return false;
1421 }
1422
1423 InitIfNecessary();
1424 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time,
1425 CookieOptions());
1426}
1427
1428void CookieMonster::InitStore() {
[email protected]90499482013-06-01 00:39:501429 DCHECK(store_.get()) << "Store must exist to initialize";
[email protected]f48b9432011-01-11 07:25:401430
[email protected]218aa6a12011-09-13 17:38:381431 // We bind in the current time so that we can report the wall-clock time for
1432 // loading cookies.
1433 store_->Load(base::Bind(&CookieMonster::OnLoaded, this, TimeTicks::Now()));
1434}
[email protected]f48b9432011-01-11 07:25:401435
[email protected]28c5d0b72014-05-13 08:19:591436void CookieMonster::ReportLoaded() {
1437 if (delegate_.get())
1438 delegate_->OnLoaded();
1439}
1440
[email protected]218aa6a12011-09-13 17:38:381441void CookieMonster::OnLoaded(TimeTicks beginning_time,
1442 const std::vector<CanonicalCookie*>& cookies) {
1443 StoreLoadedCookies(cookies);
[email protected]c7593fb22011-11-14 23:54:271444 histogram_time_blocked_on_load_->AddTime(TimeTicks::Now() - beginning_time);
[email protected]218aa6a12011-09-13 17:38:381445
1446 // Invoke the task queue of cookie request.
1447 InvokeQueue();
[email protected]28c5d0b72014-05-13 08:19:591448
1449 ReportLoaded();
[email protected]218aa6a12011-09-13 17:38:381450}
1451
[email protected]8562034e2011-10-17 17:35:041452void CookieMonster::OnKeyLoaded(const std::string& key,
1453 const std::vector<CanonicalCookie*>& cookies) {
1454 // This function does its own separate locking.
1455 StoreLoadedCookies(cookies);
1456
[email protected]0184df32013-05-14 00:53:551457 std::deque<scoped_refptr<CookieMonsterTask> > tasks_pending_for_key;
[email protected]8562034e2011-10-17 17:35:041458
[email protected]bab72ec2013-10-30 20:50:021459 // We need to do this repeatedly until no more tasks were added to the queue
1460 // during the period where we release the lock.
1461 while (true) {
1462 {
1463 base::AutoLock autolock(lock_);
1464 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > >
1465 ::iterator it = tasks_pending_for_key_.find(key);
1466 if (it == tasks_pending_for_key_.end()) {
1467 keys_loaded_.insert(key);
1468 return;
1469 }
1470 if (it->second.empty()) {
1471 keys_loaded_.insert(key);
1472 tasks_pending_for_key_.erase(it);
1473 return;
1474 }
1475 it->second.swap(tasks_pending_for_key);
1476 }
1477
1478 while (!tasks_pending_for_key.empty()) {
1479 scoped_refptr<CookieMonsterTask> task = tasks_pending_for_key.front();
1480 task->Run();
1481 tasks_pending_for_key.pop_front();
1482 }
[email protected]8562034e2011-10-17 17:35:041483 }
1484}
1485
[email protected]218aa6a12011-09-13 17:38:381486void CookieMonster::StoreLoadedCookies(
1487 const std::vector<CanonicalCookie*>& cookies) {
[email protected]f48b9432011-01-11 07:25:401488 // Initialize the store and sync in any saved persistent cookies. We don't
1489 // care if it's expired, insert it so it can be garbage collected, removed,
1490 // and sync'd.
[email protected]218aa6a12011-09-13 17:38:381491 base::AutoLock autolock(lock_);
[email protected]f48b9432011-01-11 07:25:401492
[email protected]6210ce52013-09-20 03:33:141493 CookieItVector cookies_with_control_chars;
1494
[email protected]f48b9432011-01-11 07:25:401495 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin();
1496 it != cookies.end(); ++it) {
1497 int64 cookie_creation_time = (*it)->CreationDate().ToInternalValue();
1498
[email protected]8562034e2011-10-17 17:35:041499 if (creation_times_.insert(cookie_creation_time).second) {
[email protected]6210ce52013-09-20 03:33:141500 CookieMap::iterator inserted =
1501 InternalInsertCookie(GetKey((*it)->Domain()), *it, false);
[email protected]f48b9432011-01-11 07:25:401502 const Time cookie_access_time((*it)->LastAccessDate());
[email protected]8562034e2011-10-17 17:35:041503 if (earliest_access_time_.is_null() ||
1504 cookie_access_time < earliest_access_time_)
1505 earliest_access_time_ = cookie_access_time;
[email protected]6210ce52013-09-20 03:33:141506
1507 if (ContainsControlCharacter((*it)->Name()) ||
1508 ContainsControlCharacter((*it)->Value())) {
1509 cookies_with_control_chars.push_back(inserted);
1510 }
[email protected]f48b9432011-01-11 07:25:401511 } else {
1512 LOG(ERROR) << base::StringPrintf("Found cookies with duplicate creation "
1513 "times in backing store: "
1514 "{name='%s', domain='%s', path='%s'}",
1515 (*it)->Name().c_str(),
1516 (*it)->Domain().c_str(),
1517 (*it)->Path().c_str());
1518 // We've been given ownership of the cookie and are throwing it
1519 // away; reclaim the space.
1520 delete (*it);
1521 }
1522 }
[email protected]f48b9432011-01-11 07:25:401523
[email protected]6210ce52013-09-20 03:33:141524 // Any cookies that contain control characters that we have loaded from the
1525 // persistent store should be deleted. See http://crbug.com/238041.
1526 for (CookieItVector::iterator it = cookies_with_control_chars.begin();
1527 it != cookies_with_control_chars.end();) {
1528 CookieItVector::iterator curit = it;
1529 ++it;
1530
1531 InternalDeleteCookie(*curit, true, DELETE_COOKIE_CONTROL_CHAR);
1532 }
1533
[email protected]f48b9432011-01-11 07:25:401534 // After importing cookies from the PersistentCookieStore, verify that
1535 // none of our other constraints are violated.
[email protected]f48b9432011-01-11 07:25:401536 // In particular, the backing store might have given us duplicate cookies.
[email protected]8562034e2011-10-17 17:35:041537
1538 // This method could be called multiple times due to priority loading, thus
1539 // cookies loaded in previous runs will be validated again, but this is OK
1540 // since they are expected to be much fewer than total DB.
[email protected]f48b9432011-01-11 07:25:401541 EnsureCookiesMapIsValid();
[email protected]218aa6a12011-09-13 17:38:381542}
[email protected]f48b9432011-01-11 07:25:401543
[email protected]218aa6a12011-09-13 17:38:381544void CookieMonster::InvokeQueue() {
1545 while (true) {
1546 scoped_refptr<CookieMonsterTask> request_task;
1547 {
1548 base::AutoLock autolock(lock_);
[email protected]0184df32013-05-14 00:53:551549 if (tasks_pending_.empty()) {
[email protected]218aa6a12011-09-13 17:38:381550 loaded_ = true;
[email protected]8562034e2011-10-17 17:35:041551 creation_times_.clear();
1552 keys_loaded_.clear();
[email protected]218aa6a12011-09-13 17:38:381553 break;
1554 }
[email protected]0184df32013-05-14 00:53:551555 request_task = tasks_pending_.front();
1556 tasks_pending_.pop();
[email protected]218aa6a12011-09-13 17:38:381557 }
1558 request_task->Run();
1559 }
[email protected]f48b9432011-01-11 07:25:401560}
1561
1562void CookieMonster::EnsureCookiesMapIsValid() {
1563 lock_.AssertAcquired();
1564
1565 int num_duplicates_trimmed = 0;
1566
1567 // Iterate through all the of the cookies, grouped by host.
1568 CookieMap::iterator prev_range_end = cookies_.begin();
1569 while (prev_range_end != cookies_.end()) {
1570 CookieMap::iterator cur_range_begin = prev_range_end;
1571 const std::string key = cur_range_begin->first; // Keep a copy.
1572 CookieMap::iterator cur_range_end = cookies_.upper_bound(key);
1573 prev_range_end = cur_range_end;
1574
1575 // Ensure no equivalent cookies for this host.
1576 num_duplicates_trimmed +=
1577 TrimDuplicateCookiesForKey(key, cur_range_begin, cur_range_end);
1578 }
1579
1580 // Record how many duplicates were found in the database.
1581 // See InitializeHistograms() for details.
1582 histogram_cookie_deletion_cause_->Add(num_duplicates_trimmed);
1583}
1584
1585int CookieMonster::TrimDuplicateCookiesForKey(
1586 const std::string& key,
1587 CookieMap::iterator begin,
1588 CookieMap::iterator end) {
1589 lock_.AssertAcquired();
1590
1591 // Set of cookies ordered by creation time.
1592 typedef std::set<CookieMap::iterator, OrderByCreationTimeDesc> CookieSet;
1593
1594 // Helper map we populate to find the duplicates.
1595 typedef std::map<CookieSignature, CookieSet> EquivalenceMap;
1596 EquivalenceMap equivalent_cookies;
1597
1598 // The number of duplicate cookies that have been found.
1599 int num_duplicates = 0;
1600
1601 // Iterate through all of the cookies in our range, and insert them into
1602 // the equivalence map.
1603 for (CookieMap::iterator it = begin; it != end; ++it) {
1604 DCHECK_EQ(key, it->first);
1605 CanonicalCookie* cookie = it->second;
1606
1607 CookieSignature signature(cookie->Name(), cookie->Domain(),
1608 cookie->Path());
1609 CookieSet& set = equivalent_cookies[signature];
1610
1611 // We found a duplicate!
1612 if (!set.empty())
1613 num_duplicates++;
1614
1615 // We save the iterator into |cookies_| rather than the actual cookie
1616 // pointer, since we may need to delete it later.
1617 bool insert_success = set.insert(it).second;
1618 DCHECK(insert_success) <<
1619 "Duplicate creation times found in duplicate cookie name scan.";
1620 }
1621
1622 // If there were no duplicates, we are done!
1623 if (num_duplicates == 0)
1624 return 0;
1625
1626 // Make sure we find everything below that we did above.
1627 int num_duplicates_found = 0;
1628
1629 // Otherwise, delete all the duplicate cookies, both from our in-memory store
1630 // and from the backing store.
1631 for (EquivalenceMap::iterator it = equivalent_cookies.begin();
1632 it != equivalent_cookies.end();
1633 ++it) {
1634 const CookieSignature& signature = it->first;
1635 CookieSet& dupes = it->second;
1636
1637 if (dupes.size() <= 1)
1638 continue; // This cookiename/path has no duplicates.
1639 num_duplicates_found += dupes.size() - 1;
1640
1641 // Since |dups| is sorted by creation time (descending), the first cookie
1642 // is the most recent one, so we will keep it. The rest are duplicates.
1643 dupes.erase(dupes.begin());
1644
1645 LOG(ERROR) << base::StringPrintf(
1646 "Found %d duplicate cookies for host='%s', "
1647 "with {name='%s', domain='%s', path='%s'}",
1648 static_cast<int>(dupes.size()),
1649 key.c_str(),
1650 signature.name.c_str(),
1651 signature.domain.c_str(),
1652 signature.path.c_str());
1653
1654 // Remove all the cookies identified by |dupes|. It is valid to delete our
1655 // list of iterators one at a time, since |cookies_| is a multimap (they
1656 // don't invalidate existing iterators following deletion).
1657 for (CookieSet::iterator dupes_it = dupes.begin();
1658 dupes_it != dupes.end();
1659 ++dupes_it) {
[email protected]218aa6a12011-09-13 17:38:381660 InternalDeleteCookie(*dupes_it, true,
[email protected]f48b9432011-01-11 07:25:401661 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE);
1662 }
1663 }
1664 DCHECK_EQ(num_duplicates, num_duplicates_found);
1665
1666 return num_duplicates;
1667}
1668
[email protected]ba4ad0e2011-03-15 08:12:471669// Note: file must be the last scheme.
[email protected]5edff3c52014-06-23 20:27:481670const char* const CookieMonster::kDefaultCookieableSchemes[] =
[email protected]6757a5b82013-12-09 02:18:571671 { "http", "https", "ws", "wss", "file" };
[email protected]ba4ad0e2011-03-15 08:12:471672const int CookieMonster::kDefaultCookieableSchemesCount =
[email protected]5fa4f9a2013-10-03 10:13:161673 arraysize(kDefaultCookieableSchemes);
[email protected]ba4ad0e2011-03-15 08:12:471674
[email protected]f48b9432011-01-11 07:25:401675void CookieMonster::SetDefaultCookieableSchemes() {
[email protected]7c4b66b2014-01-04 12:28:131676 // Always disable file scheme unless SetEnableFileScheme(true) is called.
1677 SetCookieableSchemes(kDefaultCookieableSchemes,
1678 kDefaultCookieableSchemesCount - 1);
[email protected]f48b9432011-01-11 07:25:401679}
1680
[email protected]f48b9432011-01-11 07:25:401681void CookieMonster::FindCookiesForHostAndDomain(
1682 const GURL& url,
1683 const CookieOptions& options,
1684 bool update_access_time,
1685 std::vector<CanonicalCookie*>* cookies) {
1686 lock_.AssertAcquired();
1687
1688 const Time current_time(CurrentTime());
1689
1690 // Probe to save statistics relatively frequently. We do it here rather
1691 // than in the set path as many websites won't set cookies, and we
1692 // want to collect statistics whenever the browser's being used.
1693 RecordPeriodicStats(current_time);
1694
[email protected]8e1583672012-02-11 04:39:411695 // Can just dispatch to FindCookiesForKey
1696 const std::string key(GetKey(url.host()));
1697 FindCookiesForKey(key, url, options, current_time,
1698 update_access_time, cookies);
[email protected]f48b9432011-01-11 07:25:401699}
1700
[email protected]dedec0b2013-02-28 04:50:101701void CookieMonster::FindCookiesForKey(const std::string& key,
1702 const GURL& url,
1703 const CookieOptions& options,
1704 const Time& current,
1705 bool update_access_time,
1706 std::vector<CanonicalCookie*>* cookies) {
[email protected]f48b9432011-01-11 07:25:401707 lock_.AssertAcquired();
1708
[email protected]f48b9432011-01-11 07:25:401709 for (CookieMapItPair its = cookies_.equal_range(key);
1710 its.first != its.second; ) {
1711 CookieMap::iterator curit = its.first;
1712 CanonicalCookie* cc = curit->second;
1713 ++its.first;
1714
1715 // If the cookie is expired, delete it.
[email protected]ba4ad0e2011-03-15 08:12:471716 if (cc->IsExpired(current) && !keep_expired_cookies_) {
[email protected]f48b9432011-01-11 07:25:401717 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED);
1718 continue;
1719 }
1720
[email protected]65f4e7e2012-12-12 21:56:541721 // Filter out cookies that should not be included for a request to the
1722 // given |url|. HTTP only cookies are filtered depending on the passed
1723 // cookie |options|.
1724 if (!cc->IncludeForRequestURL(url, options))
[email protected]f48b9432011-01-11 07:25:401725 continue;
1726
[email protected]65f4e7e2012-12-12 21:56:541727 // Add this cookie to the set of matching cookies. Update the access
[email protected]f48b9432011-01-11 07:25:401728 // time if we've been requested to do so.
1729 if (update_access_time) {
1730 InternalUpdateCookieAccessTime(cc, current);
1731 }
1732 cookies->push_back(cc);
1733 }
1734}
1735
1736bool CookieMonster::DeleteAnyEquivalentCookie(const std::string& key,
1737 const CanonicalCookie& ecc,
[email protected]e7c590e52011-03-30 08:33:551738 bool skip_httponly,
1739 bool already_expired) {
[email protected]f48b9432011-01-11 07:25:401740 lock_.AssertAcquired();
1741
1742 bool found_equivalent_cookie = false;
1743 bool skipped_httponly = false;
1744 for (CookieMapItPair its = cookies_.equal_range(key);
1745 its.first != its.second; ) {
1746 CookieMap::iterator curit = its.first;
1747 CanonicalCookie* cc = curit->second;
1748 ++its.first;
1749
1750 if (ecc.IsEquivalent(*cc)) {
1751 // We should never have more than one equivalent cookie, since they should
1752 // overwrite each other.
1753 CHECK(!found_equivalent_cookie) <<
1754 "Duplicate equivalent cookies found, cookie store is corrupted.";
1755 if (skip_httponly && cc->IsHttpOnly()) {
1756 skipped_httponly = true;
1757 } else {
[email protected]e7c590e52011-03-30 08:33:551758 InternalDeleteCookie(curit, true, already_expired ?
1759 DELETE_COOKIE_EXPIRED_OVERWRITE : DELETE_COOKIE_OVERWRITE);
[email protected]f48b9432011-01-11 07:25:401760 }
1761 found_equivalent_cookie = true;
1762 }
1763 }
1764 return skipped_httponly;
1765}
1766
[email protected]6210ce52013-09-20 03:33:141767CookieMonster::CookieMap::iterator CookieMonster::InternalInsertCookie(
1768 const std::string& key,
1769 CanonicalCookie* cc,
1770 bool sync_to_store) {
[email protected]f48b9432011-01-11 07:25:401771 lock_.AssertAcquired();
1772
[email protected]90499482013-06-01 00:39:501773 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() &&
1774 sync_to_store)
[email protected]f48b9432011-01-11 07:25:401775 store_->AddCookie(*cc);
[email protected]6210ce52013-09-20 03:33:141776 CookieMap::iterator inserted =
1777 cookies_.insert(CookieMap::value_type(key, cc));
[email protected]8bb846f2011-03-23 12:08:181778 if (delegate_.get()) {
1779 delegate_->OnCookieChanged(
[email protected]7c4b66b2014-01-04 12:28:131780 *cc, false, CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT);
[email protected]8bb846f2011-03-23 12:08:181781 }
[email protected]6210ce52013-09-20 03:33:141782
1783 return inserted;
[email protected]f48b9432011-01-11 07:25:401784}
1785
[email protected]34602282010-02-03 22:14:151786bool CookieMonster::SetCookieWithCreationTimeAndOptions(
1787 const GURL& url,
1788 const std::string& cookie_line,
1789 const Time& creation_time_or_null,
1790 const CookieOptions& options) {
[email protected]b866a02d2010-07-28 16:41:041791 lock_.AssertAcquired();
initial.commit586acc5fe2008-07-26 22:42:521792
[email protected]4d3ce782010-10-29 18:31:281793 VLOG(kVlogSetCookies) << "SetCookie() line: " << cookie_line;
initial.commit586acc5fe2008-07-26 22:42:521794
[email protected]34602282010-02-03 22:14:151795 Time creation_time = creation_time_or_null;
1796 if (creation_time.is_null()) {
1797 creation_time = CurrentTime();
1798 last_time_seen_ = creation_time;
1799 }
1800
[email protected]abbd13b2012-11-15 17:54:201801 scoped_ptr<CanonicalCookie> cc(
1802 CanonicalCookie::Create(url, cookie_line, creation_time, options));
initial.commit586acc5fe2008-07-26 22:42:521803
1804 if (!cc.get()) {
[email protected]4d3ce782010-10-29 18:31:281805 VLOG(kVlogSetCookies) << "WARNING: Failed to allocate CanonicalCookie";
initial.commit586acc5fe2008-07-26 22:42:521806 return false;
1807 }
[email protected]fa77eb512010-07-22 16:12:511808 return SetCanonicalCookie(&cc, creation_time, options);
[email protected]f325f1e12010-04-30 22:38:551809}
initial.commit586acc5fe2008-07-26 22:42:521810
[email protected]f325f1e12010-04-30 22:38:551811bool CookieMonster::SetCanonicalCookie(scoped_ptr<CanonicalCookie>* cc,
[email protected]f325f1e12010-04-30 22:38:551812 const Time& creation_time,
1813 const CookieOptions& options) {
[email protected]7a964a72010-09-07 19:33:261814 const std::string key(GetKey((*cc)->Domain()));
[email protected]e7c590e52011-03-30 08:33:551815 bool already_expired = (*cc)->IsExpired(creation_time);
1816 if (DeleteAnyEquivalentCookie(key, **cc, options.exclude_httponly(),
1817 already_expired)) {
[email protected]4d3ce782010-10-29 18:31:281818 VLOG(kVlogSetCookies) << "SetCookie() not clobbering httponly cookie";
[email protected]3a96c742008-11-19 19:46:271819 return false;
1820 }
initial.commit586acc5fe2008-07-26 22:42:521821
[email protected]4d3ce782010-10-29 18:31:281822 VLOG(kVlogSetCookies) << "SetCookie() key: " << key << " cc: "
1823 << (*cc)->DebugString();
initial.commit586acc5fe2008-07-26 22:42:521824
1825 // Realize that we might be setting an expired cookie, and the only point
1826 // was to delete the cookie which we've already done.
[email protected]e7c590e52011-03-30 08:33:551827 if (!already_expired || keep_expired_cookies_) {
[email protected]374f58b2010-07-20 15:29:261828 // See InitializeHistograms() for details.
[email protected]10b691f2012-07-11 15:22:151829 if ((*cc)->IsPersistent()) {
[email protected]8475bee2011-03-17 18:40:241830 histogram_expiration_duration_minutes_->Add(
1831 ((*cc)->ExpiryDate() - creation_time).InMinutes());
1832 }
1833
[email protected]7a964a72010-09-07 19:33:261834 InternalInsertCookie(key, cc->release(), true);
[email protected]348dd662013-03-13 20:25:071835 } else {
1836 VLOG(kVlogSetCookies) << "SetCookie() not storing already expired cookie.";
[email protected]c4058fb2010-06-22 17:25:261837 }
initial.commit586acc5fe2008-07-26 22:42:521838
1839 // We assume that hopefully setting a cookie will be less common than
1840 // querying a cookie. Since setting a cookie can put us over our limits,
1841 // make sure that we garbage collect... We can also make the assumption that
1842 // if a cookie was set, in the common case it will be used soon after,
1843 // and we will purge the expired cookies in GetCookies().
[email protected]7a964a72010-09-07 19:33:261844 GarbageCollect(creation_time, key);
initial.commit586acc5fe2008-07-26 22:42:521845
1846 return true;
1847}
1848
[email protected]7a964a72010-09-07 19:33:261849void CookieMonster::InternalUpdateCookieAccessTime(CanonicalCookie* cc,
1850 const Time& current) {
[email protected]bb8905722010-05-21 17:29:041851 lock_.AssertAcquired();
1852
[email protected]77e0a462008-11-01 00:43:351853 // Based off the Mozilla code. When a cookie has been accessed recently,
1854 // don't bother updating its access time again. This reduces the number of
1855 // updates we do during pageload, which in turn reduces the chance our storage
1856 // backend will hit its batch thresholds and be forced to update.
[email protected]77e0a462008-11-01 00:43:351857 if ((current - cc->LastAccessDate()) < last_access_threshold_)
1858 return;
1859
[email protected]374f58b2010-07-20 15:29:261860 // See InitializeHistograms() for details.
1861 histogram_between_access_interval_minutes_->Add(
1862 (current - cc->LastAccessDate()).InMinutes());
[email protected]c4058fb2010-06-22 17:25:261863
[email protected]77e0a462008-11-01 00:43:351864 cc->SetLastAccessDate(current);
[email protected]90499482013-06-01 00:39:501865 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get())
[email protected]77e0a462008-11-01 00:43:351866 store_->UpdateCookieAccessTime(*cc);
1867}
1868
[email protected]6210ce52013-09-20 03:33:141869// InternalDeleteCookies must not invalidate iterators other than the one being
1870// deleted.
initial.commit586acc5fe2008-07-26 22:42:521871void CookieMonster::InternalDeleteCookie(CookieMap::iterator it,
[email protected]c4058fb2010-06-22 17:25:261872 bool sync_to_store,
1873 DeletionCause deletion_cause) {
[email protected]bb8905722010-05-21 17:29:041874 lock_.AssertAcquired();
1875
[email protected]8bb846f2011-03-23 12:08:181876 // Ideally, this would be asserted up where we define ChangeCauseMapping,
1877 // but DeletionCause's visibility (or lack thereof) forces us to make
1878 // this check here.
1879 COMPILE_ASSERT(arraysize(ChangeCauseMapping) == DELETE_COOKIE_LAST_ENTRY + 1,
1880 ChangeCauseMapping_size_not_eq_DeletionCause_enum_size);
1881
[email protected]374f58b2010-07-20 15:29:261882 // See InitializeHistograms() for details.
[email protected]7a964a72010-09-07 19:33:261883 if (deletion_cause != DELETE_COOKIE_DONT_RECORD)
1884 histogram_cookie_deletion_cause_->Add(deletion_cause);
[email protected]c4058fb2010-06-22 17:25:261885
initial.commit586acc5fe2008-07-26 22:42:521886 CanonicalCookie* cc = it->second;
[email protected]4d3ce782010-10-29 18:31:281887 VLOG(kVlogSetCookies) << "InternalDeleteCookie() cc: " << cc->DebugString();
[email protected]7a964a72010-09-07 19:33:261888
[email protected]90499482013-06-01 00:39:501889 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() &&
1890 sync_to_store)
initial.commit586acc5fe2008-07-26 22:42:521891 store_->DeleteCookie(*cc);
[email protected]8bb846f2011-03-23 12:08:181892 if (delegate_.get()) {
1893 ChangeCausePair mapping = ChangeCauseMapping[deletion_cause];
1894
1895 if (mapping.notify)
1896 delegate_->OnCookieChanged(*cc, true, mapping.cause);
1897 }
initial.commit586acc5fe2008-07-26 22:42:521898 cookies_.erase(it);
1899 delete cc;
1900}
1901
[email protected]8807b322010-10-01 17:10:141902// Domain expiry behavior is unchanged by key/expiry scheme (the
[email protected]8ad5d462013-05-02 08:45:261903// meaning of the key is different, but that's not visible to this routine).
initial.commit586acc5fe2008-07-26 22:42:521904int CookieMonster::GarbageCollect(const Time& current,
1905 const std::string& key) {
[email protected]bb8905722010-05-21 17:29:041906 lock_.AssertAcquired();
1907
initial.commit586acc5fe2008-07-26 22:42:521908 int num_deleted = 0;
[email protected]8ad5d462013-05-02 08:45:261909 Time safe_date(
1910 Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays));
initial.commit586acc5fe2008-07-26 22:42:521911
[email protected]8ad5d462013-05-02 08:45:261912 // Collect garbage for this key, minding cookie priorities.
[email protected]7a964a72010-09-07 19:33:261913 if (cookies_.count(key) > kDomainMaxCookies) {
[email protected]4d3ce782010-10-29 18:31:281914 VLOG(kVlogGarbageCollection) << "GarbageCollect() key: " << key;
[email protected]7a964a72010-09-07 19:33:261915
[email protected]8ad5d462013-05-02 08:45:261916 CookieItVector cookie_its;
[email protected]8807b322010-10-01 17:10:141917 num_deleted += GarbageCollectExpired(
1918 current, cookies_.equal_range(key), &cookie_its);
[email protected]8ad5d462013-05-02 08:45:261919 if (cookie_its.size() > kDomainMaxCookies) {
1920 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect domain.";
1921 size_t purge_goal =
1922 cookie_its.size() - (kDomainMaxCookies - kDomainPurgeCookies);
1923 DCHECK(purge_goal > kDomainPurgeCookies);
1924
1925 // Boundary iterators into |cookie_its| for different priorities.
1926 CookieItVector::iterator it_bdd[4];
1927 // Intialize |it_bdd| while sorting |cookie_its| by priorities.
1928 // Schematic: [MLLHMHHLMM] => [LLL|MMMM|HHH], with 4 boundaries.
1929 it_bdd[0] = cookie_its.begin();
1930 it_bdd[3] = cookie_its.end();
1931 it_bdd[1] = PartitionCookieByPriority(it_bdd[0], it_bdd[3],
1932 COOKIE_PRIORITY_LOW);
1933 it_bdd[2] = PartitionCookieByPriority(it_bdd[1], it_bdd[3],
1934 COOKIE_PRIORITY_MEDIUM);
1935 size_t quota[3] = {
1936 kDomainCookiesQuotaLow,
1937 kDomainCookiesQuotaMedium,
1938 kDomainCookiesQuotaHigh
1939 };
1940
1941 // Purge domain cookies in 3 rounds.
1942 // Round 1: consider low-priority cookies only: evict least-recently
1943 // accessed, while protecting quota[0] of these from deletion.
1944 // Round 2: consider {low, medium}-priority cookies, evict least-recently
1945 // accessed, while protecting quota[0] + quota[1].
1946 // Round 3: consider all cookies, evict least-recently accessed.
1947 size_t accumulated_quota = 0;
1948 CookieItVector::iterator it_purge_begin = it_bdd[0];
1949 for (int i = 0; i < 3 && purge_goal > 0; ++i) {
1950 accumulated_quota += quota[i];
1951
[email protected]8ad5d462013-05-02 08:45:261952 size_t num_considered = it_bdd[i + 1] - it_purge_begin;
1953 if (num_considered <= accumulated_quota)
1954 continue;
1955
1956 // Number of cookies that will be purged in this round.
1957 size_t round_goal =
1958 std::min(purge_goal, num_considered - accumulated_quota);
1959 purge_goal -= round_goal;
1960
1961 SortLeastRecentlyAccessed(it_purge_begin, it_bdd[i + 1], round_goal);
1962 // Cookies accessed on or after |safe_date| would have been safe from
1963 // global purge, and we want to keep track of this.
1964 CookieItVector::iterator it_purge_end = it_purge_begin + round_goal;
1965 CookieItVector::iterator it_purge_middle =
1966 LowerBoundAccessDate(it_purge_begin, it_purge_end, safe_date);
1967 // Delete cookies accessed before |safe_date|.
1968 num_deleted += GarbageCollectDeleteRange(
1969 current,
1970 DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE,
1971 it_purge_begin,
1972 it_purge_middle);
1973 // Delete cookies accessed on or after |safe_date|.
1974 num_deleted += GarbageCollectDeleteRange(
1975 current,
1976 DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE,
1977 it_purge_middle,
1978 it_purge_end);
1979 it_purge_begin = it_purge_end;
1980 }
1981 DCHECK_EQ(0U, purge_goal);
[email protected]8807b322010-10-01 17:10:141982 }
initial.commit586acc5fe2008-07-26 22:42:521983 }
1984
[email protected]8ad5d462013-05-02 08:45:261985 // Collect garbage for everything. With firefox style we want to preserve
1986 // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict.
[email protected]8807b322010-10-01 17:10:141987 if (cookies_.size() > kMaxCookies &&
[email protected]8ad5d462013-05-02 08:45:261988 earliest_access_time_ < safe_date) {
[email protected]4d3ce782010-10-29 18:31:281989 VLOG(kVlogGarbageCollection) << "GarbageCollect() everything";
[email protected]8ad5d462013-05-02 08:45:261990 CookieItVector cookie_its;
[email protected]7a964a72010-09-07 19:33:261991 num_deleted += GarbageCollectExpired(
1992 current, CookieMapItPair(cookies_.begin(), cookies_.end()),
1993 &cookie_its);
[email protected]8ad5d462013-05-02 08:45:261994 if (cookie_its.size() > kMaxCookies) {
1995 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect everything.";
1996 size_t purge_goal = cookie_its.size() - (kMaxCookies - kPurgeCookies);
1997 DCHECK(purge_goal > kPurgeCookies);
1998 // Sorts up to *and including* |cookie_its[purge_goal]|, so
1999 // |earliest_access_time| will be properly assigned even if
2000 // |global_purge_it| == |cookie_its.begin() + purge_goal|.
2001 SortLeastRecentlyAccessed(cookie_its.begin(), cookie_its.end(),
2002 purge_goal);
2003 // Find boundary to cookies older than safe_date.
2004 CookieItVector::iterator global_purge_it =
2005 LowerBoundAccessDate(cookie_its.begin(),
2006 cookie_its.begin() + purge_goal,
2007 safe_date);
2008 // Only delete the old cookies.
2009 num_deleted += GarbageCollectDeleteRange(
[email protected]8807b322010-10-01 17:10:142010 current,
[email protected]8807b322010-10-01 17:10:142011 DELETE_COOKIE_EVICTED_GLOBAL,
[email protected]8ad5d462013-05-02 08:45:262012 cookie_its.begin(),
2013 global_purge_it);
2014 // Set access day to the oldest cookie that wasn't deleted.
2015 earliest_access_time_ = (*global_purge_it)->second->LastAccessDate();
[email protected]8807b322010-10-01 17:10:142016 }
[email protected]c890ed192008-10-30 23:45:532017 }
2018
2019 return num_deleted;
2020}
2021
[email protected]c890ed192008-10-30 23:45:532022int CookieMonster::GarbageCollectExpired(
2023 const Time& current,
2024 const CookieMapItPair& itpair,
[email protected]8ad5d462013-05-02 08:45:262025 CookieItVector* cookie_its) {
[email protected]ba4ad0e2011-03-15 08:12:472026 if (keep_expired_cookies_)
2027 return 0;
2028
[email protected]bb8905722010-05-21 17:29:042029 lock_.AssertAcquired();
2030
[email protected]c890ed192008-10-30 23:45:532031 int num_deleted = 0;
2032 for (CookieMap::iterator it = itpair.first, end = itpair.second; it != end;) {
2033 CookieMap::iterator curit = it;
2034 ++it;
2035
2036 if (curit->second->IsExpired(current)) {
[email protected]2f3f3592010-07-07 20:11:512037 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED);
[email protected]c890ed192008-10-30 23:45:532038 ++num_deleted;
2039 } else if (cookie_its) {
2040 cookie_its->push_back(curit);
2041 }
initial.commit586acc5fe2008-07-26 22:42:522042 }
2043
2044 return num_deleted;
2045}
2046
[email protected]8ad5d462013-05-02 08:45:262047int CookieMonster::GarbageCollectDeleteRange(
[email protected]f48b9432011-01-11 07:25:402048 const Time& current,
[email protected]f48b9432011-01-11 07:25:402049 DeletionCause cause,
[email protected]5fa4f9a2013-10-03 10:13:162050 CookieItVector::iterator it_begin,
2051 CookieItVector::iterator it_end) {
[email protected]8ad5d462013-05-02 08:45:262052 for (CookieItVector::iterator it = it_begin; it != it_end; it++) {
2053 histogram_evicted_last_access_minutes_->Add(
2054 (current - (*it)->second->LastAccessDate()).InMinutes());
2055 InternalDeleteCookie((*it), true, cause);
[email protected]c10da4b02010-03-25 14:38:322056 }
[email protected]8ad5d462013-05-02 08:45:262057 return it_end - it_begin;
[email protected]c10da4b02010-03-25 14:38:322058}
2059
[email protected]ed32c212013-05-14 20:49:292060// A wrapper around registry_controlled_domains::GetDomainAndRegistry
[email protected]f48b9432011-01-11 07:25:402061// to make clear we're creating a key for our local map. Here and
2062// in FindCookiesForHostAndDomain() are the only two places where
2063// we need to conditionalize based on key type.
2064//
2065// Note that this key algorithm explicitly ignores the scheme. This is
2066// because when we're entering cookies into the map from the backing store,
2067// we in general won't have the scheme at that point.
2068// In practical terms, this means that file cookies will be stored
2069// in the map either by an empty string or by UNC name (and will be
2070// limited by kMaxCookiesPerHost), and extension cookies will be stored
2071// based on the single extension id, as the extension id won't have the
2072// form of a DNS host and hence GetKey() will return it unchanged.
2073//
2074// Arguably the right thing to do here is to make the key
2075// algorithm dependent on the scheme, and make sure that the scheme is
2076// available everywhere the key must be obtained (specfically at backing
2077// store load time). This would require either changing the backing store
2078// database schema to include the scheme (far more trouble than it's worth), or
2079// separating out file cookies into their own CookieMonster instance and
2080// thus restricting each scheme to a single cookie monster (which might
2081// be worth it, but is still too much trouble to solve what is currently a
2082// non-problem).
2083std::string CookieMonster::GetKey(const std::string& domain) const {
[email protected]f48b9432011-01-11 07:25:402084 std::string effective_domain(
[email protected]ed32c212013-05-14 20:49:292085 registry_controlled_domains::GetDomainAndRegistry(
[email protected]aabe1792014-01-30 21:37:462086 domain, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
[email protected]f48b9432011-01-11 07:25:402087 if (effective_domain.empty())
2088 effective_domain = domain;
2089
2090 if (!effective_domain.empty() && effective_domain[0] == '.')
2091 return effective_domain.substr(1);
2092 return effective_domain;
2093}
2094
[email protected]97a3b6e2012-06-12 01:53:562095bool CookieMonster::IsCookieableScheme(const std::string& scheme) {
2096 base::AutoLock autolock(lock_);
2097
2098 return std::find(cookieable_schemes_.begin(), cookieable_schemes_.end(),
2099 scheme) != cookieable_schemes_.end();
2100}
2101
[email protected]f48b9432011-01-11 07:25:402102bool CookieMonster::HasCookieableScheme(const GURL& url) {
2103 lock_.AssertAcquired();
2104
2105 // Make sure the request is on a cookie-able url scheme.
2106 for (size_t i = 0; i < cookieable_schemes_.size(); ++i) {
2107 // We matched a scheme.
2108 if (url.SchemeIs(cookieable_schemes_[i].c_str())) {
2109 // We've matched a supported scheme.
initial.commit586acc5fe2008-07-26 22:42:522110 return true;
2111 }
2112 }
[email protected]f48b9432011-01-11 07:25:402113
2114 // The scheme didn't match any in our whitelist.
2115 VLOG(kVlogPerCookieMonster) << "WARNING: Unsupported cookie scheme: "
2116 << url.scheme();
initial.commit586acc5fe2008-07-26 22:42:522117 return false;
2118}
2119
[email protected]c4058fb2010-06-22 17:25:262120// Test to see if stats should be recorded, and record them if so.
2121// The goal here is to get sampling for the average browser-hour of
2122// activity. We won't take samples when the web isn't being surfed,
2123// and when the web is being surfed, we'll take samples about every
2124// kRecordStatisticsIntervalSeconds.
2125// last_statistic_record_time_ is initialized to Now() rather than null
2126// in the constructor so that we won't take statistics right after
2127// startup, to avoid bias from browsers that are started but not used.
2128void CookieMonster::RecordPeriodicStats(const base::Time& current_time) {
2129 const base::TimeDelta kRecordStatisticsIntervalTime(
2130 base::TimeDelta::FromSeconds(kRecordStatisticsIntervalSeconds));
2131
[email protected]7a964a72010-09-07 19:33:262132 // If we've taken statistics recently, return.
2133 if (current_time - last_statistic_record_time_ <=
[email protected]c4058fb2010-06-22 17:25:262134 kRecordStatisticsIntervalTime) {
[email protected]7a964a72010-09-07 19:33:262135 return;
[email protected]c4058fb2010-06-22 17:25:262136 }
[email protected]7a964a72010-09-07 19:33:262137
2138 // See InitializeHistograms() for details.
2139 histogram_count_->Add(cookies_.size());
2140
2141 // More detailed statistics on cookie counts at different granularities.
2142 TimeTicks beginning_of_time(TimeTicks::Now());
2143
2144 for (CookieMap::const_iterator it_key = cookies_.begin();
2145 it_key != cookies_.end(); ) {
2146 const std::string& key(it_key->first);
2147
2148 int key_count = 0;
2149 typedef std::map<std::string, unsigned int> DomainMap;
2150 DomainMap domain_map;
2151 CookieMapItPair its_cookies = cookies_.equal_range(key);
2152 while (its_cookies.first != its_cookies.second) {
2153 key_count++;
2154 const std::string& cookie_domain(its_cookies.first->second->Domain());
2155 domain_map[cookie_domain]++;
2156
2157 its_cookies.first++;
2158 }
2159 histogram_etldp1_count_->Add(key_count);
2160 histogram_domain_per_etldp1_count_->Add(domain_map.size());
2161 for (DomainMap::const_iterator domain_map_it = domain_map.begin();
2162 domain_map_it != domain_map.end(); domain_map_it++)
2163 histogram_domain_count_->Add(domain_map_it->second);
2164
2165 it_key = its_cookies.second;
2166 }
2167
[email protected]4d3ce782010-10-29 18:31:282168 VLOG(kVlogPeriodic)
2169 << "Time for recording cookie stats (us): "
2170 << (TimeTicks::Now() - beginning_of_time).InMicroseconds();
[email protected]7a964a72010-09-07 19:33:262171
2172 last_statistic_record_time_ = current_time;
[email protected]c4058fb2010-06-22 17:25:262173}
2174
[email protected]f48b9432011-01-11 07:25:402175// Initialize all histogram counter variables used in this class.
2176//
2177// Normal histogram usage involves using the macros defined in
2178// histogram.h, which automatically takes care of declaring these
2179// variables (as statics), initializing them, and accumulating into
2180// them, all from a single entry point. Unfortunately, that solution
2181// doesn't work for the CookieMonster, as it's vulnerable to races between
2182// separate threads executing the same functions and hence initializing the
2183// same static variables. There isn't a race danger in the histogram
2184// accumulation calls; they are written to be resilient to simultaneous
2185// calls from multiple threads.
2186//
2187// The solution taken here is to have per-CookieMonster instance
2188// variables that are constructed during CookieMonster construction.
2189// Note that these variables refer to the same underlying histogram,
2190// so we still race (but safely) with other CookieMonster instances
2191// for accumulation.
2192//
2193// To do this we've expanded out the individual histogram macros calls,
2194// with declarations of the variables in the class decl, initialization here
2195// (done from the class constructor) and direct calls to the accumulation
2196// methods where needed. The specific histogram macro calls on which the
2197// initialization is based are included in comments below.
2198void CookieMonster::InitializeHistograms() {
2199 // From UMA_HISTOGRAM_CUSTOM_COUNTS
2200 histogram_expiration_duration_minutes_ = base::Histogram::FactoryGet(
2201 "Cookie.ExpirationDurationMinutes",
2202 1, kMinutesInTenYears, 50,
2203 base::Histogram::kUmaTargetedHistogramFlag);
2204 histogram_between_access_interval_minutes_ = base::Histogram::FactoryGet(
2205 "Cookie.BetweenAccessIntervalMinutes",
2206 1, kMinutesInTenYears, 50,
2207 base::Histogram::kUmaTargetedHistogramFlag);
2208 histogram_evicted_last_access_minutes_ = base::Histogram::FactoryGet(
2209 "Cookie.EvictedLastAccessMinutes",
2210 1, kMinutesInTenYears, 50,
2211 base::Histogram::kUmaTargetedHistogramFlag);
2212 histogram_count_ = base::Histogram::FactoryGet(
2213 "Cookie.Count", 1, 4000, 50,
2214 base::Histogram::kUmaTargetedHistogramFlag);
2215 histogram_domain_count_ = base::Histogram::FactoryGet(
2216 "Cookie.DomainCount", 1, 4000, 50,
2217 base::Histogram::kUmaTargetedHistogramFlag);
2218 histogram_etldp1_count_ = base::Histogram::FactoryGet(
2219 "Cookie.Etldp1Count", 1, 4000, 50,
2220 base::Histogram::kUmaTargetedHistogramFlag);
2221 histogram_domain_per_etldp1_count_ = base::Histogram::FactoryGet(
2222 "Cookie.DomainPerEtldp1Count", 1, 4000, 50,
2223 base::Histogram::kUmaTargetedHistogramFlag);
2224
2225 // From UMA_HISTOGRAM_COUNTS_10000 & UMA_HISTOGRAM_CUSTOM_COUNTS
2226 histogram_number_duplicate_db_cookies_ = base::Histogram::FactoryGet(
2227 "Net.NumDuplicateCookiesInDb", 1, 10000, 50,
2228 base::Histogram::kUmaTargetedHistogramFlag);
2229
2230 // From UMA_HISTOGRAM_ENUMERATION
2231 histogram_cookie_deletion_cause_ = base::LinearHistogram::FactoryGet(
2232 "Cookie.DeletionCause", 1,
2233 DELETE_COOKIE_LAST_ENTRY - 1, DELETE_COOKIE_LAST_ENTRY,
2234 base::Histogram::kUmaTargetedHistogramFlag);
2235
2236 // From UMA_HISTOGRAM_{CUSTOM_,}TIMES
2237 histogram_time_get_ = base::Histogram::FactoryTimeGet("Cookie.TimeGet",
2238 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
2239 50, base::Histogram::kUmaTargetedHistogramFlag);
[email protected]c7593fb22011-11-14 23:54:272240 histogram_time_blocked_on_load_ = base::Histogram::FactoryTimeGet(
2241 "Cookie.TimeBlockedOnLoad",
[email protected]f48b9432011-01-11 07:25:402242 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
2243 50, base::Histogram::kUmaTargetedHistogramFlag);
2244}
2245
2246
2247// The system resolution is not high enough, so we can have multiple
2248// set cookies that result in the same system time. When this happens, we
2249// increment by one Time unit. Let's hope computers don't get too fast.
2250Time CookieMonster::CurrentTime() {
2251 return std::max(Time::Now(),
2252 Time::FromInternalValue(last_time_seen_.ToInternalValue() + 1));
2253}
2254
[email protected]28c5d0b72014-05-13 08:19:592255bool CookieMonster::CopyCookiesForKeyToOtherCookieMonster(
2256 std::string key,
2257 CookieMonster* other) {
2258 ScopedVector<CanonicalCookie> duplicated_cookies;
2259
2260 {
2261 base::AutoLock autolock(lock_);
2262 DCHECK(other);
2263 if (!loaded_)
2264 return false;
2265
2266 for (CookieMapItPair its = cookies_.equal_range(key);
2267 its.first != its.second;
2268 ++its.first) {
2269 CookieMap::iterator curit = its.first;
2270 CanonicalCookie* cc = curit->second;
2271
2272 duplicated_cookies.push_back(cc->Duplicate());
2273 }
2274 }
2275
2276 {
2277 base::AutoLock autolock(other->lock_);
2278 if (!other->loaded_)
2279 return false;
2280
2281 // There must not exist any entries for the key to be copied in |other|.
2282 CookieMapItPair its = other->cookies_.equal_range(key);
2283 if (its.first != its.second)
2284 return false;
2285
2286 // Store the copied cookies in |other|.
2287 for (ScopedVector<CanonicalCookie>::const_iterator it =
2288 duplicated_cookies.begin();
2289 it != duplicated_cookies.end();
2290 ++it) {
2291 other->InternalInsertCookie(key, *it, true);
2292 }
2293
2294 // Since the cookies are owned by |other| now, weak clear must be used.
2295 duplicated_cookies.weak_clear();
2296 }
2297
2298 return true;
2299}
2300
2301bool CookieMonster::loaded() {
2302 base::AutoLock autolock(lock_);
2303 return loaded_;
2304}
2305
[email protected]63725312012-07-19 08:24:162306} // namespace net