blob: e879a8faca7b700cc835deae40c13477892a4a02 [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"
initial.commit586acc5fe2008-07-26 22:42:5266
[email protected]e1acf6f2008-10-27 20:43:3367using base::Time;
68using base::TimeDelta;
[email protected]7a964a72010-09-07 19:33:2669using base::TimeTicks;
[email protected]e1acf6f2008-10-27 20:43:3370
[email protected]8562034e2011-10-17 17:35:0471// In steady state, most cookie requests can be satisfied by the in memory
72// cookie monster store. However, if a request comes in during the initial
73// cookie load, it must be delayed until that load completes. That is done by
[email protected]0184df32013-05-14 00:53:5574// queueing it on CookieMonster::tasks_pending_ and running it when notification
75// of cookie load completion is received via CookieMonster::OnLoaded. This
76// callback is passed to the persistent store from CookieMonster::InitStore(),
77// which is called on the first operation invoked on the CookieMonster.
[email protected]8562034e2011-10-17 17:35:0478//
79// On the browser critical paths (e.g. for loading initial web pages in a
80// session restore) it may take too long to wait for the full load. If a cookie
81// request is for a specific URL, DoCookieTaskForURL is called, which triggers a
82// priority load if the key is not loaded yet by calling PersistentCookieStore
[email protected]0184df32013-05-14 00:53:5583// :: LoadCookiesForKey. The request is queued in
84// CookieMonster::tasks_pending_for_key_ and executed upon receiving
85// notification of key load completion via CookieMonster::OnKeyLoaded(). If
86// multiple requests for the same eTLD+1 are received before key load
87// completion, only the first request calls
[email protected]8562034e2011-10-17 17:35:0488// PersistentCookieStore::LoadCookiesForKey, all subsequent requests are queued
[email protected]0184df32013-05-14 00:53:5589// in CookieMonster::tasks_pending_for_key_ and executed upon receiving
90// notification of key load completion triggered by the first request for the
91// same eTLD+1.
[email protected]8562034e2011-10-17 17:35:0492
[email protected]c4058fb2010-06-22 17:25:2693static const int kMinutesInTenYears = 10 * 365 * 24 * 60;
94
[email protected]8ac1a752008-07-31 19:40:3795namespace net {
96
[email protected]7a964a72010-09-07 19:33:2697// See comments at declaration of these variables in cookie_monster.h
98// for details.
[email protected]8807b322010-10-01 17:10:1499const size_t CookieMonster::kDomainMaxCookies = 180;
100const size_t CookieMonster::kDomainPurgeCookies = 30;
101const size_t CookieMonster::kMaxCookies = 3300;
102const size_t CookieMonster::kPurgeCookies = 300;
[email protected]8ad5d462013-05-02 08:45:26103
104const size_t CookieMonster::kDomainCookiesQuotaLow = 30;
105const size_t CookieMonster::kDomainCookiesQuotaMedium = 50;
106const size_t CookieMonster::kDomainCookiesQuotaHigh =
[email protected]5fa4f9a2013-10-03 10:13:16107 kDomainMaxCookies - kDomainPurgeCookies
108 - kDomainCookiesQuotaLow - kDomainCookiesQuotaMedium;
[email protected]8ad5d462013-05-02 08:45:26109
[email protected]8807b322010-10-01 17:10:14110const int CookieMonster::kSafeFromGlobalPurgeDays = 30;
[email protected]297a4ed02010-02-12 08:12:52111
[email protected]7a964a72010-09-07 19:33:26112namespace {
[email protected]e32306c52008-11-06 16:59:05113
[email protected]6210ce52013-09-20 03:33:14114bool ContainsControlCharacter(const std::string& s) {
115 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) {
116 if ((*i >= 0) && (*i <= 31))
117 return true;
118 }
119
120 return false;
121}
122
[email protected]5b9bc352012-07-18 13:13:34123typedef std::vector<CanonicalCookie*> CanonicalCookieVector;
[email protected]34a160d2011-05-12 22:12:49124
[email protected]77e0a462008-11-01 00:43:35125// Default minimum delay after updating a cookie's LastAccessDate before we
126// will update it again.
[email protected]297a4ed02010-02-12 08:12:52127const int kDefaultAccessUpdateThresholdSeconds = 60;
128
129// Comparator to sort cookies from highest creation date to lowest
130// creation date.
131struct OrderByCreationTimeDesc {
132 bool operator()(const CookieMonster::CookieMap::iterator& a,
133 const CookieMonster::CookieMap::iterator& b) const {
134 return a->second->CreationDate() > b->second->CreationDate();
135 }
136};
137
[email protected]4d3ce782010-10-29 18:31:28138// Constants for use in VLOG
139const int kVlogPerCookieMonster = 1;
140const int kVlogPeriodic = 3;
141const int kVlogGarbageCollection = 5;
142const int kVlogSetCookies = 7;
143const int kVlogGetCookies = 9;
144
[email protected]f48b9432011-01-11 07:25:40145// Mozilla sorts on the path length (longest first), and then it
146// sorts by creation time (oldest first).
147// The RFC says the sort order for the domain attribute is undefined.
[email protected]5b9bc352012-07-18 13:13:34148bool CookieSorter(CanonicalCookie* cc1, CanonicalCookie* cc2) {
[email protected]f48b9432011-01-11 07:25:40149 if (cc1->Path().length() == cc2->Path().length())
150 return cc1->CreationDate() < cc2->CreationDate();
151 return cc1->Path().length() > cc2->Path().length();
initial.commit586acc5fe2008-07-26 22:42:52152}
153
[email protected]8ad5d462013-05-02 08:45:26154bool LRACookieSorter(const CookieMonster::CookieMap::iterator& it1,
[email protected]f48b9432011-01-11 07:25:40155 const CookieMonster::CookieMap::iterator& it2) {
156 // Cookies accessed less recently should be deleted first.
157 if (it1->second->LastAccessDate() != it2->second->LastAccessDate())
158 return it1->second->LastAccessDate() < it2->second->LastAccessDate();
initial.commit586acc5fe2008-07-26 22:42:52159
[email protected]f48b9432011-01-11 07:25:40160 // In rare cases we might have two cookies with identical last access times.
161 // To preserve the stability of the sort, in these cases prefer to delete
162 // older cookies over newer ones. CreationDate() is guaranteed to be unique.
163 return it1->second->CreationDate() < it2->second->CreationDate();
[email protected]297a4ed02010-02-12 08:12:52164}
165
166// Our strategy to find duplicates is:
167// (1) Build a map from (cookiename, cookiepath) to
168// {list of cookies with this signature, sorted by creation time}.
169// (2) For each list with more than 1 entry, keep the cookie having the
170// most recent creation time, and delete the others.
[email protected]f48b9432011-01-11 07:25:40171//
[email protected]1655ba342010-07-14 18:17:42172// Two cookies are considered equivalent if they have the same domain,
173// name, and path.
174struct CookieSignature {
175 public:
[email protected]dedec0b2013-02-28 04:50:10176 CookieSignature(const std::string& name,
177 const std::string& domain,
[email protected]1655ba342010-07-14 18:17:42178 const std::string& path)
[email protected]dedec0b2013-02-28 04:50:10179 : name(name), domain(domain), path(path) {
180 }
[email protected]1655ba342010-07-14 18:17:42181
182 // To be a key for a map this class needs to be assignable, copyable,
183 // and have an operator<. The default assignment operator
184 // and copy constructor are exactly what we want.
185
186 bool operator<(const CookieSignature& cs) const {
187 // Name compare dominates, then domain, then path.
188 int diff = name.compare(cs.name);
189 if (diff != 0)
190 return diff < 0;
191
192 diff = domain.compare(cs.domain);
193 if (diff != 0)
194 return diff < 0;
195
196 return path.compare(cs.path) < 0;
197 }
198
199 std::string name;
200 std::string domain;
201 std::string path;
202};
[email protected]f48b9432011-01-11 07:25:40203
[email protected]8ad5d462013-05-02 08:45:26204// For a CookieItVector iterator range [|it_begin|, |it_end|),
205// sorts the first |num_sort| + 1 elements by LastAccessDate().
206// The + 1 element exists so for any interval of length <= |num_sort| starting
207// from |cookies_its_begin|, a LastAccessDate() bound can be found.
208void SortLeastRecentlyAccessed(
209 CookieMonster::CookieItVector::iterator it_begin,
210 CookieMonster::CookieItVector::iterator it_end,
211 size_t num_sort) {
212 DCHECK_LT(static_cast<int>(num_sort), it_end - it_begin);
213 std::partial_sort(it_begin, it_begin + num_sort + 1, it_end, LRACookieSorter);
214}
[email protected]f48b9432011-01-11 07:25:40215
[email protected]8ad5d462013-05-02 08:45:26216// Predicate to support PartitionCookieByPriority().
217struct CookiePriorityEqualsTo
218 : std::unary_function<const CookieMonster::CookieMap::iterator, bool> {
[email protected]5edff3c52014-06-23 20:27:48219 explicit CookiePriorityEqualsTo(CookiePriority priority)
[email protected]8ad5d462013-05-02 08:45:26220 : priority_(priority) {}
221
222 bool operator()(const CookieMonster::CookieMap::iterator it) const {
223 return it->second->Priority() == priority_;
[email protected]f48b9432011-01-11 07:25:40224 }
[email protected]8ad5d462013-05-02 08:45:26225
226 const CookiePriority priority_;
227};
228
229// For a CookieItVector iterator range [|it_begin|, |it_end|),
230// moves all cookies with a given |priority| to the beginning of the list.
231// Returns: An iterator in [it_begin, it_end) to the first element with
232// priority != |priority|, or |it_end| if all have priority == |priority|.
233CookieMonster::CookieItVector::iterator PartitionCookieByPriority(
234 CookieMonster::CookieItVector::iterator it_begin,
235 CookieMonster::CookieItVector::iterator it_end,
236 CookiePriority priority) {
237 return std::partition(it_begin, it_end, CookiePriorityEqualsTo(priority));
238}
239
240bool LowerBoundAccessDateComparator(
241 const CookieMonster::CookieMap::iterator it, const Time& access_date) {
242 return it->second->LastAccessDate() < access_date;
243}
244
245// For a CookieItVector iterator range [|it_begin|, |it_end|)
246// from a CookieItVector sorted by LastAccessDate(), returns the
247// first iterator with access date >= |access_date|, or cookie_its_end if this
248// holds for all.
249CookieMonster::CookieItVector::iterator LowerBoundAccessDate(
250 const CookieMonster::CookieItVector::iterator its_begin,
251 const CookieMonster::CookieItVector::iterator its_end,
252 const Time& access_date) {
253 return std::lower_bound(its_begin, its_end, access_date,
254 LowerBoundAccessDateComparator);
[email protected]7a964a72010-09-07 19:33:26255}
256
[email protected]7c4b66b2014-01-04 12:28:13257// Mapping between DeletionCause and CookieMonsterDelegate::ChangeCause; the
258// mapping also provides a boolean that specifies whether or not an
259// OnCookieChanged notification ought to be generated.
[email protected]8bb846f2011-03-23 12:08:18260typedef struct ChangeCausePair_struct {
[email protected]7c4b66b2014-01-04 12:28:13261 CookieMonsterDelegate::ChangeCause cause;
[email protected]8bb846f2011-03-23 12:08:18262 bool notify;
263} ChangeCausePair;
264ChangeCausePair ChangeCauseMapping[] = {
265 // DELETE_COOKIE_EXPLICIT
[email protected]7c4b66b2014-01-04 12:28:13266 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, true },
[email protected]8bb846f2011-03-23 12:08:18267 // DELETE_COOKIE_OVERWRITE
[email protected]7c4b66b2014-01-04 12:28:13268 { CookieMonsterDelegate::CHANGE_COOKIE_OVERWRITE, true },
[email protected]8bb846f2011-03-23 12:08:18269 // DELETE_COOKIE_EXPIRED
[email protected]7c4b66b2014-01-04 12:28:13270 { CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED, true },
[email protected]8bb846f2011-03-23 12:08:18271 // DELETE_COOKIE_EVICTED
[email protected]7c4b66b2014-01-04 12:28:13272 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
[email protected]8bb846f2011-03-23 12:08:18273 // DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE
[email protected]7c4b66b2014-01-04 12:28:13274 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false },
[email protected]8bb846f2011-03-23 12:08:18275 // DELETE_COOKIE_DONT_RECORD
[email protected]7c4b66b2014-01-04 12:28:13276 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false },
[email protected]8bb846f2011-03-23 12:08:18277 // DELETE_COOKIE_EVICTED_DOMAIN
[email protected]7c4b66b2014-01-04 12:28:13278 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
[email protected]8bb846f2011-03-23 12:08:18279 // DELETE_COOKIE_EVICTED_GLOBAL
[email protected]7c4b66b2014-01-04 12:28:13280 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
[email protected]8bb846f2011-03-23 12:08:18281 // DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
[email protected]7c4b66b2014-01-04 12:28:13282 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
[email protected]8bb846f2011-03-23 12:08:18283 // DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
[email protected]7c4b66b2014-01-04 12:28:13284 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true },
[email protected]e7c590e52011-03-30 08:33:55285 // DELETE_COOKIE_EXPIRED_OVERWRITE
[email protected]7c4b66b2014-01-04 12:28:13286 { CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED_OVERWRITE, true },
[email protected]6210ce52013-09-20 03:33:14287 // DELETE_COOKIE_CONTROL_CHAR
[email protected]7c4b66b2014-01-04 12:28:13288 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true},
[email protected]8bb846f2011-03-23 12:08:18289 // DELETE_COOKIE_LAST_ENTRY
[email protected]7c4b66b2014-01-04 12:28:13290 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false }
[email protected]8bb846f2011-03-23 12:08:18291};
292
[email protected]34a160d2011-05-12 22:12:49293std::string BuildCookieLine(const CanonicalCookieVector& cookies) {
294 std::string cookie_line;
295 for (CanonicalCookieVector::const_iterator it = cookies.begin();
296 it != cookies.end(); ++it) {
297 if (it != cookies.begin())
298 cookie_line += "; ";
299 // In Mozilla if you set a cookie like AAAA, it will have an empty token
300 // and a value of AAAA. When it sends the cookie back, it will send AAAA,
301 // so we need to avoid sending =AAAA for a blank token value.
302 if (!(*it)->Name().empty())
303 cookie_line += (*it)->Name() + "=";
304 cookie_line += (*it)->Value();
305 }
306 return cookie_line;
307}
308
ellyjones399e35a22014-10-27 11:09:56309void RunAsync(scoped_refptr<base::TaskRunner> proxy,
msarda0aad8f02014-10-30 09:22:39310 const CookieStore::CookieChangedCallback& callback,
311 const CanonicalCookie& cookie,
312 bool removed) {
313 proxy->PostTask(FROM_HERE, base::Bind(callback, cookie, removed));
ellyjones399e35a22014-10-27 11:09:56314}
315
[email protected]f48b9432011-01-11 07:25:40316} // namespace
317
[email protected]7c4b66b2014-01-04 12:28:13318CookieMonster::CookieMonster(PersistentCookieStore* store,
319 CookieMonsterDelegate* delegate)
[email protected]f48b9432011-01-11 07:25:40320 : initialized_(false),
[email protected]28c5d0b72014-05-13 08:19:59321 loaded_(store == NULL),
[email protected]f48b9432011-01-11 07:25:40322 store_(store),
323 last_access_threshold_(
324 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)),
325 delegate_(delegate),
[email protected]82388662011-03-10 21:04:06326 last_statistic_record_time_(Time::Now()),
[email protected]93c53a32011-12-05 10:40:35327 keep_expired_cookies_(false),
[email protected]8976e292013-11-02 13:38:57328 persist_session_cookies_(false) {
[email protected]f48b9432011-01-11 07:25:40329 InitializeHistograms();
330 SetDefaultCookieableSchemes();
[email protected]2d0f89a2010-12-06 12:02:23331}
332
[email protected]f48b9432011-01-11 07:25:40333CookieMonster::CookieMonster(PersistentCookieStore* store,
[email protected]7c4b66b2014-01-04 12:28:13334 CookieMonsterDelegate* delegate,
[email protected]f48b9432011-01-11 07:25:40335 int last_access_threshold_milliseconds)
336 : initialized_(false),
[email protected]28c5d0b72014-05-13 08:19:59337 loaded_(store == NULL),
[email protected]f48b9432011-01-11 07:25:40338 store_(store),
339 last_access_threshold_(base::TimeDelta::FromMilliseconds(
340 last_access_threshold_milliseconds)),
341 delegate_(delegate),
[email protected]82388662011-03-10 21:04:06342 last_statistic_record_time_(base::Time::Now()),
[email protected]93c53a32011-12-05 10:40:35343 keep_expired_cookies_(false),
[email protected]8976e292013-11-02 13:38:57344 persist_session_cookies_(false) {
[email protected]f48b9432011-01-11 07:25:40345 InitializeHistograms();
346 SetDefaultCookieableSchemes();
initial.commit586acc5fe2008-07-26 22:42:52347}
348
initial.commit586acc5fe2008-07-26 22:42:52349
[email protected]218aa6a12011-09-13 17:38:38350// Task classes for queueing the coming request.
351
352class CookieMonster::CookieMonsterTask
353 : public base::RefCountedThreadSafe<CookieMonsterTask> {
354 public:
355 // Runs the task and invokes the client callback on the thread that
356 // originally constructed the task.
357 virtual void Run() = 0;
358
359 protected:
360 explicit CookieMonsterTask(CookieMonster* cookie_monster);
361 virtual ~CookieMonsterTask();
362
363 // Invokes the callback immediately, if the current thread is the one
364 // that originated the task, or queues the callback for execution on the
365 // appropriate thread. Maintains a reference to this CookieMonsterTask
366 // instance until the callback completes.
367 void InvokeCallback(base::Closure callback);
368
369 CookieMonster* cookie_monster() {
370 return cookie_monster_;
371 }
372
[email protected]a9813302012-04-28 09:29:28373 private:
[email protected]218aa6a12011-09-13 17:38:38374 friend class base::RefCountedThreadSafe<CookieMonsterTask>;
375
[email protected]218aa6a12011-09-13 17:38:38376 CookieMonster* cookie_monster_;
377 scoped_refptr<base::MessageLoopProxy> thread_;
378
379 DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask);
380};
381
382CookieMonster::CookieMonsterTask::CookieMonsterTask(
383 CookieMonster* cookie_monster)
384 : cookie_monster_(cookie_monster),
[email protected]a9813302012-04-28 09:29:28385 thread_(base::MessageLoopProxy::current()) {
386}
[email protected]218aa6a12011-09-13 17:38:38387
[email protected]a9813302012-04-28 09:29:28388CookieMonster::CookieMonsterTask::~CookieMonsterTask() {}
[email protected]218aa6a12011-09-13 17:38:38389
390// Unfortunately, one cannot re-bind a Callback with parameters into a closure.
391// Therefore, the closure passed to InvokeCallback is a clumsy binding of
392// Callback::Run on a wrapped Callback instance. Since Callback is not
393// reference counted, we bind to an instance that is a member of the
394// CookieMonsterTask subclass. Then, we cannot simply post the callback to a
395// message loop because the underlying instance may be destroyed (along with the
396// CookieMonsterTask instance) in the interim. Therefore, we post a callback
397// bound to the CookieMonsterTask, which *is* reference counted (thus preventing
398// destruction of the original callback), and which invokes the closure (which
399// invokes the original callback with the returned data).
400void CookieMonster::CookieMonsterTask::InvokeCallback(base::Closure callback) {
401 if (thread_->BelongsToCurrentThread()) {
402 callback.Run();
403 } else {
404 thread_->PostTask(FROM_HERE, base::Bind(
[email protected]5fa4f9a2013-10-03 10:13:16405 &CookieMonsterTask::InvokeCallback, this, callback));
[email protected]218aa6a12011-09-13 17:38:38406 }
407}
408
409// Task class for SetCookieWithDetails call.
[email protected]5fa4f9a2013-10-03 10:13:16410class CookieMonster::SetCookieWithDetailsTask : public CookieMonsterTask {
[email protected]218aa6a12011-09-13 17:38:38411 public:
[email protected]dedec0b2013-02-28 04:50:10412 SetCookieWithDetailsTask(CookieMonster* cookie_monster,
413 const GURL& url,
414 const std::string& name,
415 const std::string& value,
416 const std::string& domain,
417 const std::string& path,
418 const base::Time& expiration_time,
419 bool secure,
420 bool http_only,
[email protected]ab2d75c82013-04-19 18:39:04421 CookiePriority priority,
[email protected]5fa4f9a2013-10-03 10:13:16422 const SetCookiesCallback& callback)
[email protected]218aa6a12011-09-13 17:38:38423 : CookieMonsterTask(cookie_monster),
424 url_(url),
425 name_(name),
426 value_(value),
427 domain_(domain),
428 path_(path),
429 expiration_time_(expiration_time),
430 secure_(secure),
431 http_only_(http_only),
[email protected]ab2d75c82013-04-19 18:39:04432 priority_(priority),
[email protected]a9813302012-04-28 09:29:28433 callback_(callback) {
434 }
[email protected]218aa6a12011-09-13 17:38:38435
[email protected]5fa4f9a2013-10-03 10:13:16436 // CookieMonsterTask:
dchengb03027d2014-10-21 12:00:20437 void Run() override;
[email protected]218aa6a12011-09-13 17:38:38438
[email protected]a9813302012-04-28 09:29:28439 protected:
dchengb03027d2014-10-21 12:00:20440 ~SetCookieWithDetailsTask() override {}
[email protected]a9813302012-04-28 09:29:28441
[email protected]218aa6a12011-09-13 17:38:38442 private:
443 GURL url_;
444 std::string name_;
445 std::string value_;
446 std::string domain_;
447 std::string path_;
448 base::Time expiration_time_;
449 bool secure_;
450 bool http_only_;
[email protected]ab2d75c82013-04-19 18:39:04451 CookiePriority priority_;
[email protected]5fa4f9a2013-10-03 10:13:16452 SetCookiesCallback callback_;
[email protected]218aa6a12011-09-13 17:38:38453
454 DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask);
455};
456
457void CookieMonster::SetCookieWithDetailsTask::Run() {
458 bool success = this->cookie_monster()->
459 SetCookieWithDetails(url_, name_, value_, domain_, path_,
[email protected]ab2d75c82013-04-19 18:39:04460 expiration_time_, secure_, http_only_, priority_);
[email protected]218aa6a12011-09-13 17:38:38461 if (!callback_.is_null()) {
[email protected]5fa4f9a2013-10-03 10:13:16462 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run,
[email protected]218aa6a12011-09-13 17:38:38463 base::Unretained(&callback_), success));
464 }
465}
466
467// Task class for GetAllCookies call.
[email protected]5fa4f9a2013-10-03 10:13:16468class CookieMonster::GetAllCookiesTask : public CookieMonsterTask {
[email protected]218aa6a12011-09-13 17:38:38469 public:
470 GetAllCookiesTask(CookieMonster* cookie_monster,
[email protected]5fa4f9a2013-10-03 10:13:16471 const GetCookieListCallback& callback)
[email protected]218aa6a12011-09-13 17:38:38472 : CookieMonsterTask(cookie_monster),
[email protected]a9813302012-04-28 09:29:28473 callback_(callback) {
474 }
[email protected]218aa6a12011-09-13 17:38:38475
[email protected]5fa4f9a2013-10-03 10:13:16476 // CookieMonsterTask
dchengb03027d2014-10-21 12:00:20477 void Run() override;
[email protected]218aa6a12011-09-13 17:38:38478
[email protected]a9813302012-04-28 09:29:28479 protected:
dchengb03027d2014-10-21 12:00:20480 ~GetAllCookiesTask() override {}
[email protected]a9813302012-04-28 09:29:28481
[email protected]218aa6a12011-09-13 17:38:38482 private:
[email protected]5fa4f9a2013-10-03 10:13:16483 GetCookieListCallback callback_;
[email protected]218aa6a12011-09-13 17:38:38484
485 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask);
486};
487
488void CookieMonster::GetAllCookiesTask::Run() {
489 if (!callback_.is_null()) {
490 CookieList cookies = this->cookie_monster()->GetAllCookies();
[email protected]5fa4f9a2013-10-03 10:13:16491 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run,
[email protected]218aa6a12011-09-13 17:38:38492 base::Unretained(&callback_), cookies));
493 }
494}
495
496// Task class for GetAllCookiesForURLWithOptions call.
497class CookieMonster::GetAllCookiesForURLWithOptionsTask
[email protected]5fa4f9a2013-10-03 10:13:16498 : public CookieMonsterTask {
[email protected]218aa6a12011-09-13 17:38:38499 public:
500 GetAllCookiesForURLWithOptionsTask(
501 CookieMonster* cookie_monster,
502 const GURL& url,
503 const CookieOptions& options,
[email protected]5fa4f9a2013-10-03 10:13:16504 const GetCookieListCallback& callback)
[email protected]218aa6a12011-09-13 17:38:38505 : CookieMonsterTask(cookie_monster),
506 url_(url),
507 options_(options),
[email protected]a9813302012-04-28 09:29:28508 callback_(callback) {
509 }
[email protected]218aa6a12011-09-13 17:38:38510
[email protected]5fa4f9a2013-10-03 10:13:16511 // CookieMonsterTask:
dchengb03027d2014-10-21 12:00:20512 void Run() override;
[email protected]218aa6a12011-09-13 17:38:38513
[email protected]a9813302012-04-28 09:29:28514 protected:
dchengb03027d2014-10-21 12:00:20515 ~GetAllCookiesForURLWithOptionsTask() override {}
[email protected]a9813302012-04-28 09:29:28516
[email protected]218aa6a12011-09-13 17:38:38517 private:
518 GURL url_;
519 CookieOptions options_;
[email protected]5fa4f9a2013-10-03 10:13:16520 GetCookieListCallback callback_;
[email protected]218aa6a12011-09-13 17:38:38521
522 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesForURLWithOptionsTask);
523};
524
525void CookieMonster::GetAllCookiesForURLWithOptionsTask::Run() {
526 if (!callback_.is_null()) {
527 CookieList cookies = this->cookie_monster()->
528 GetAllCookiesForURLWithOptions(url_, options_);
[email protected]5fa4f9a2013-10-03 10:13:16529 this->InvokeCallback(base::Bind(&GetCookieListCallback::Run,
[email protected]218aa6a12011-09-13 17:38:38530 base::Unretained(&callback_), cookies));
531 }
532}
533
[email protected]5fa4f9a2013-10-03 10:13:16534template <typename Result> struct CallbackType {
535 typedef base::Callback<void(Result)> Type;
536};
537
538template <> struct CallbackType<void> {
539 typedef base::Closure Type;
540};
541
542// Base task class for Delete*Task.
543template <typename Result>
544class CookieMonster::DeleteTask : public CookieMonsterTask {
[email protected]218aa6a12011-09-13 17:38:38545 public:
[email protected]5fa4f9a2013-10-03 10:13:16546 DeleteTask(CookieMonster* cookie_monster,
547 const typename CallbackType<Result>::Type& callback)
[email protected]218aa6a12011-09-13 17:38:38548 : CookieMonsterTask(cookie_monster),
[email protected]a9813302012-04-28 09:29:28549 callback_(callback) {
550 }
[email protected]218aa6a12011-09-13 17:38:38551
[email protected]5fa4f9a2013-10-03 10:13:16552 // CookieMonsterTask:
mostynbba063d6032014-10-09 11:01:13553 virtual void Run() override;
[email protected]218aa6a12011-09-13 17:38:38554
[email protected]5fa4f9a2013-10-03 10:13:16555 private:
556 // Runs the delete task and returns a result.
557 virtual Result RunDeleteTask() = 0;
558 base::Closure RunDeleteTaskAndBindCallback();
559 void FlushDone(const base::Closure& callback);
560
561 typename CallbackType<Result>::Type callback_;
562
563 DISALLOW_COPY_AND_ASSIGN(DeleteTask);
564};
565
566template <typename Result>
567base::Closure CookieMonster::DeleteTask<Result>::
568RunDeleteTaskAndBindCallback() {
569 Result result = RunDeleteTask();
570 if (callback_.is_null())
571 return base::Closure();
572 return base::Bind(callback_, result);
573}
574
575template <>
576base::Closure CookieMonster::DeleteTask<void>::RunDeleteTaskAndBindCallback() {
577 RunDeleteTask();
578 return callback_;
579}
580
581template <typename Result>
582void CookieMonster::DeleteTask<Result>::Run() {
583 this->cookie_monster()->FlushStore(
584 base::Bind(&DeleteTask<Result>::FlushDone, this,
585 RunDeleteTaskAndBindCallback()));
586}
587
588template <typename Result>
589void CookieMonster::DeleteTask<Result>::FlushDone(
590 const base::Closure& callback) {
591 if (!callback.is_null()) {
592 this->InvokeCallback(callback);
593 }
594}
595
596// Task class for DeleteAll call.
597class CookieMonster::DeleteAllTask : public DeleteTask<int> {
598 public:
599 DeleteAllTask(CookieMonster* cookie_monster,
600 const DeleteCallback& callback)
[email protected]151132f2013-11-18 21:37:00601 : DeleteTask<int>(cookie_monster, callback) {
[email protected]5fa4f9a2013-10-03 10:13:16602 }
603
604 // DeleteTask:
dchengb03027d2014-10-21 12:00:20605 int RunDeleteTask() override;
[email protected]5fa4f9a2013-10-03 10:13:16606
[email protected]a9813302012-04-28 09:29:28607 protected:
dchengb03027d2014-10-21 12:00:20608 ~DeleteAllTask() override {}
[email protected]a9813302012-04-28 09:29:28609
[email protected]218aa6a12011-09-13 17:38:38610 private:
[email protected]218aa6a12011-09-13 17:38:38611 DISALLOW_COPY_AND_ASSIGN(DeleteAllTask);
612};
613
[email protected]5fa4f9a2013-10-03 10:13:16614int CookieMonster::DeleteAllTask::RunDeleteTask() {
615 return this->cookie_monster()->DeleteAll(true);
[email protected]218aa6a12011-09-13 17:38:38616}
617
618// Task class for DeleteAllCreatedBetween call.
[email protected]5fa4f9a2013-10-03 10:13:16619class CookieMonster::DeleteAllCreatedBetweenTask : public DeleteTask<int> {
[email protected]218aa6a12011-09-13 17:38:38620 public:
[email protected]dedec0b2013-02-28 04:50:10621 DeleteAllCreatedBetweenTask(CookieMonster* cookie_monster,
622 const Time& delete_begin,
623 const Time& delete_end,
[email protected]5fa4f9a2013-10-03 10:13:16624 const DeleteCallback& callback)
[email protected]151132f2013-11-18 21:37:00625 : DeleteTask<int>(cookie_monster, callback),
[email protected]218aa6a12011-09-13 17:38:38626 delete_begin_(delete_begin),
[email protected]5fa4f9a2013-10-03 10:13:16627 delete_end_(delete_end) {
[email protected]a9813302012-04-28 09:29:28628 }
[email protected]218aa6a12011-09-13 17:38:38629
[email protected]5fa4f9a2013-10-03 10:13:16630 // DeleteTask:
dchengb03027d2014-10-21 12:00:20631 int RunDeleteTask() override;
[email protected]218aa6a12011-09-13 17:38:38632
[email protected]a9813302012-04-28 09:29:28633 protected:
dchengb03027d2014-10-21 12:00:20634 ~DeleteAllCreatedBetweenTask() override {}
[email protected]a9813302012-04-28 09:29:28635
[email protected]218aa6a12011-09-13 17:38:38636 private:
637 Time delete_begin_;
638 Time delete_end_;
[email protected]218aa6a12011-09-13 17:38:38639
640 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask);
641};
642
[email protected]5fa4f9a2013-10-03 10:13:16643int CookieMonster::DeleteAllCreatedBetweenTask::RunDeleteTask() {
644 return this->cookie_monster()->
[email protected]218aa6a12011-09-13 17:38:38645 DeleteAllCreatedBetween(delete_begin_, delete_end_);
[email protected]218aa6a12011-09-13 17:38:38646}
647
648// Task class for DeleteAllForHost call.
[email protected]5fa4f9a2013-10-03 10:13:16649class CookieMonster::DeleteAllForHostTask : public DeleteTask<int> {
[email protected]218aa6a12011-09-13 17:38:38650 public:
651 DeleteAllForHostTask(CookieMonster* cookie_monster,
652 const GURL& url,
[email protected]5fa4f9a2013-10-03 10:13:16653 const DeleteCallback& callback)
[email protected]151132f2013-11-18 21:37:00654 : DeleteTask<int>(cookie_monster, callback),
[email protected]5fa4f9a2013-10-03 10:13:16655 url_(url) {
[email protected]a9813302012-04-28 09:29:28656 }
[email protected]218aa6a12011-09-13 17:38:38657
[email protected]5fa4f9a2013-10-03 10:13:16658 // DeleteTask:
dchengb03027d2014-10-21 12:00:20659 int RunDeleteTask() override;
[email protected]218aa6a12011-09-13 17:38:38660
[email protected]a9813302012-04-28 09:29:28661 protected:
dchengb03027d2014-10-21 12:00:20662 ~DeleteAllForHostTask() override {}
[email protected]a9813302012-04-28 09:29:28663
[email protected]218aa6a12011-09-13 17:38:38664 private:
665 GURL url_;
[email protected]218aa6a12011-09-13 17:38:38666
667 DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask);
668};
669
[email protected]5fa4f9a2013-10-03 10:13:16670int CookieMonster::DeleteAllForHostTask::RunDeleteTask() {
671 return this->cookie_monster()->DeleteAllForHost(url_);
[email protected]218aa6a12011-09-13 17:38:38672}
673
[email protected]d8428d52013-08-07 06:58:25674// Task class for DeleteAllCreatedBetweenForHost call.
675class CookieMonster::DeleteAllCreatedBetweenForHostTask
[email protected]5fa4f9a2013-10-03 10:13:16676 : public DeleteTask<int> {
[email protected]d8428d52013-08-07 06:58:25677 public:
678 DeleteAllCreatedBetweenForHostTask(
679 CookieMonster* cookie_monster,
680 Time delete_begin,
681 Time delete_end,
682 const GURL& url,
[email protected]5fa4f9a2013-10-03 10:13:16683 const DeleteCallback& callback)
[email protected]151132f2013-11-18 21:37:00684 : DeleteTask<int>(cookie_monster, callback),
[email protected]d8428d52013-08-07 06:58:25685 delete_begin_(delete_begin),
686 delete_end_(delete_end),
[email protected]5fa4f9a2013-10-03 10:13:16687 url_(url) {
[email protected]d8428d52013-08-07 06:58:25688 }
689
[email protected]5fa4f9a2013-10-03 10:13:16690 // DeleteTask:
dchengb03027d2014-10-21 12:00:20691 int RunDeleteTask() override;
[email protected]d8428d52013-08-07 06:58:25692
693 protected:
dchengb03027d2014-10-21 12:00:20694 ~DeleteAllCreatedBetweenForHostTask() override {}
[email protected]d8428d52013-08-07 06:58:25695
696 private:
697 Time delete_begin_;
698 Time delete_end_;
699 GURL url_;
[email protected]d8428d52013-08-07 06:58:25700
701 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenForHostTask);
702};
703
[email protected]5fa4f9a2013-10-03 10:13:16704int CookieMonster::DeleteAllCreatedBetweenForHostTask::RunDeleteTask() {
705 return this->cookie_monster()->DeleteAllCreatedBetweenForHost(
[email protected]d8428d52013-08-07 06:58:25706 delete_begin_, delete_end_, url_);
[email protected]d8428d52013-08-07 06:58:25707}
708
[email protected]218aa6a12011-09-13 17:38:38709// Task class for DeleteCanonicalCookie call.
[email protected]5fa4f9a2013-10-03 10:13:16710class CookieMonster::DeleteCanonicalCookieTask : public DeleteTask<bool> {
[email protected]218aa6a12011-09-13 17:38:38711 public:
[email protected]dedec0b2013-02-28 04:50:10712 DeleteCanonicalCookieTask(CookieMonster* cookie_monster,
713 const CanonicalCookie& cookie,
[email protected]5fa4f9a2013-10-03 10:13:16714 const DeleteCookieCallback& callback)
[email protected]151132f2013-11-18 21:37:00715 : DeleteTask<bool>(cookie_monster, callback),
[email protected]5fa4f9a2013-10-03 10:13:16716 cookie_(cookie) {
[email protected]a9813302012-04-28 09:29:28717 }
[email protected]218aa6a12011-09-13 17:38:38718
[email protected]5fa4f9a2013-10-03 10:13:16719 // DeleteTask:
dchengb03027d2014-10-21 12:00:20720 bool RunDeleteTask() override;
[email protected]218aa6a12011-09-13 17:38:38721
[email protected]a9813302012-04-28 09:29:28722 protected:
dchengb03027d2014-10-21 12:00:20723 ~DeleteCanonicalCookieTask() override {}
[email protected]a9813302012-04-28 09:29:28724
[email protected]218aa6a12011-09-13 17:38:38725 private:
[email protected]5b9bc352012-07-18 13:13:34726 CanonicalCookie cookie_;
[email protected]218aa6a12011-09-13 17:38:38727
728 DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask);
729};
730
[email protected]5fa4f9a2013-10-03 10:13:16731bool CookieMonster::DeleteCanonicalCookieTask::RunDeleteTask() {
732 return this->cookie_monster()->DeleteCanonicalCookie(cookie_);
[email protected]218aa6a12011-09-13 17:38:38733}
734
735// Task class for SetCookieWithOptions call.
[email protected]5fa4f9a2013-10-03 10:13:16736class CookieMonster::SetCookieWithOptionsTask : public CookieMonsterTask {
[email protected]218aa6a12011-09-13 17:38:38737 public:
738 SetCookieWithOptionsTask(CookieMonster* cookie_monster,
739 const GURL& url,
740 const std::string& cookie_line,
741 const CookieOptions& options,
[email protected]5fa4f9a2013-10-03 10:13:16742 const SetCookiesCallback& callback)
[email protected]218aa6a12011-09-13 17:38:38743 : CookieMonsterTask(cookie_monster),
744 url_(url),
745 cookie_line_(cookie_line),
746 options_(options),
[email protected]a9813302012-04-28 09:29:28747 callback_(callback) {
748 }
[email protected]218aa6a12011-09-13 17:38:38749
[email protected]5fa4f9a2013-10-03 10:13:16750 // CookieMonsterTask:
dchengb03027d2014-10-21 12:00:20751 void Run() override;
[email protected]218aa6a12011-09-13 17:38:38752
[email protected]a9813302012-04-28 09:29:28753 protected:
dchengb03027d2014-10-21 12:00:20754 ~SetCookieWithOptionsTask() override {}
[email protected]a9813302012-04-28 09:29:28755
[email protected]218aa6a12011-09-13 17:38:38756 private:
757 GURL url_;
758 std::string cookie_line_;
759 CookieOptions options_;
[email protected]5fa4f9a2013-10-03 10:13:16760 SetCookiesCallback callback_;
[email protected]218aa6a12011-09-13 17:38:38761
762 DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask);
763};
764
765void CookieMonster::SetCookieWithOptionsTask::Run() {
766 bool result = this->cookie_monster()->
767 SetCookieWithOptions(url_, cookie_line_, options_);
768 if (!callback_.is_null()) {
[email protected]5fa4f9a2013-10-03 10:13:16769 this->InvokeCallback(base::Bind(&SetCookiesCallback::Run,
[email protected]218aa6a12011-09-13 17:38:38770 base::Unretained(&callback_), result));
771 }
772}
773
774// Task class for GetCookiesWithOptions call.
[email protected]5fa4f9a2013-10-03 10:13:16775class CookieMonster::GetCookiesWithOptionsTask : public CookieMonsterTask {
[email protected]218aa6a12011-09-13 17:38:38776 public:
777 GetCookiesWithOptionsTask(CookieMonster* cookie_monster,
[email protected]0298caf82011-12-20 23:15:46778 const GURL& url,
[email protected]218aa6a12011-09-13 17:38:38779 const CookieOptions& options,
[email protected]5fa4f9a2013-10-03 10:13:16780 const GetCookiesCallback& callback)
[email protected]218aa6a12011-09-13 17:38:38781 : CookieMonsterTask(cookie_monster),
782 url_(url),
783 options_(options),
[email protected]a9813302012-04-28 09:29:28784 callback_(callback) {
785 }
[email protected]218aa6a12011-09-13 17:38:38786
[email protected]5fa4f9a2013-10-03 10:13:16787 // CookieMonsterTask:
dchengb03027d2014-10-21 12:00:20788 void Run() override;
[email protected]218aa6a12011-09-13 17:38:38789
[email protected]a9813302012-04-28 09:29:28790 protected:
dchengb03027d2014-10-21 12:00:20791 ~GetCookiesWithOptionsTask() override {}
[email protected]a9813302012-04-28 09:29:28792
[email protected]218aa6a12011-09-13 17:38:38793 private:
794 GURL url_;
795 CookieOptions options_;
[email protected]5fa4f9a2013-10-03 10:13:16796 GetCookiesCallback callback_;
[email protected]218aa6a12011-09-13 17:38:38797
798 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask);
799};
800
801void CookieMonster::GetCookiesWithOptionsTask::Run() {
802 std::string cookie = this->cookie_monster()->
803 GetCookiesWithOptions(url_, options_);
804 if (!callback_.is_null()) {
[email protected]5fa4f9a2013-10-03 10:13:16805 this->InvokeCallback(base::Bind(&GetCookiesCallback::Run,
[email protected]218aa6a12011-09-13 17:38:38806 base::Unretained(&callback_), cookie));
807 }
808}
809
[email protected]218aa6a12011-09-13 17:38:38810// Task class for DeleteCookie call.
[email protected]5fa4f9a2013-10-03 10:13:16811class CookieMonster::DeleteCookieTask : public DeleteTask<void> {
[email protected]218aa6a12011-09-13 17:38:38812 public:
813 DeleteCookieTask(CookieMonster* cookie_monster,
[email protected]0298caf82011-12-20 23:15:46814 const GURL& url,
[email protected]218aa6a12011-09-13 17:38:38815 const std::string& cookie_name,
816 const base::Closure& callback)
[email protected]151132f2013-11-18 21:37:00817 : DeleteTask<void>(cookie_monster, callback),
[email protected]218aa6a12011-09-13 17:38:38818 url_(url),
[email protected]5fa4f9a2013-10-03 10:13:16819 cookie_name_(cookie_name) {
820 }
[email protected]218aa6a12011-09-13 17:38:38821
[email protected]5fa4f9a2013-10-03 10:13:16822 // DeleteTask:
dchengb03027d2014-10-21 12:00:20823 void RunDeleteTask() override;
[email protected]218aa6a12011-09-13 17:38:38824
[email protected]a9813302012-04-28 09:29:28825 protected:
dchengb03027d2014-10-21 12:00:20826 ~DeleteCookieTask() override {}
[email protected]a9813302012-04-28 09:29:28827
[email protected]218aa6a12011-09-13 17:38:38828 private:
829 GURL url_;
830 std::string cookie_name_;
[email protected]218aa6a12011-09-13 17:38:38831
832 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
833};
834
[email protected]5fa4f9a2013-10-03 10:13:16835void CookieMonster::DeleteCookieTask::RunDeleteTask() {
[email protected]218aa6a12011-09-13 17:38:38836 this->cookie_monster()->DeleteCookie(url_, cookie_name_);
[email protected]218aa6a12011-09-13 17:38:38837}
838
[email protected]264807b2012-04-25 14:49:37839// Task class for DeleteSessionCookies call.
[email protected]5fa4f9a2013-10-03 10:13:16840class CookieMonster::DeleteSessionCookiesTask : public DeleteTask<int> {
[email protected]264807b2012-04-25 14:49:37841 public:
[email protected]dedec0b2013-02-28 04:50:10842 DeleteSessionCookiesTask(CookieMonster* cookie_monster,
[email protected]5fa4f9a2013-10-03 10:13:16843 const DeleteCallback& callback)
[email protected]151132f2013-11-18 21:37:00844 : DeleteTask<int>(cookie_monster, callback) {
[email protected]a9813302012-04-28 09:29:28845 }
[email protected]264807b2012-04-25 14:49:37846
[email protected]5fa4f9a2013-10-03 10:13:16847 // DeleteTask:
dchengb03027d2014-10-21 12:00:20848 int RunDeleteTask() override;
[email protected]264807b2012-04-25 14:49:37849
[email protected]a9813302012-04-28 09:29:28850 protected:
dchengb03027d2014-10-21 12:00:20851 ~DeleteSessionCookiesTask() override {}
[email protected]a9813302012-04-28 09:29:28852
[email protected]264807b2012-04-25 14:49:37853 private:
[email protected]264807b2012-04-25 14:49:37854 DISALLOW_COPY_AND_ASSIGN(DeleteSessionCookiesTask);
855};
856
[email protected]5fa4f9a2013-10-03 10:13:16857int CookieMonster::DeleteSessionCookiesTask::RunDeleteTask() {
858 return this->cookie_monster()->DeleteSessionCookies();
[email protected]264807b2012-04-25 14:49:37859}
860
[email protected]ee209482013-04-19 19:50:04861// Task class for HasCookiesForETLDP1Task call.
[email protected]5fa4f9a2013-10-03 10:13:16862class CookieMonster::HasCookiesForETLDP1Task : public CookieMonsterTask {
[email protected]ee209482013-04-19 19:50:04863 public:
864 HasCookiesForETLDP1Task(
865 CookieMonster* cookie_monster,
866 const std::string& etldp1,
[email protected]5fa4f9a2013-10-03 10:13:16867 const HasCookiesForETLDP1Callback& callback)
[email protected]ee209482013-04-19 19:50:04868 : CookieMonsterTask(cookie_monster),
869 etldp1_(etldp1),
870 callback_(callback) {
871 }
872
[email protected]5fa4f9a2013-10-03 10:13:16873 // CookieMonsterTask:
dchengb03027d2014-10-21 12:00:20874 void Run() override;
[email protected]ee209482013-04-19 19:50:04875
876 protected:
dchengb03027d2014-10-21 12:00:20877 ~HasCookiesForETLDP1Task() override {}
[email protected]ee209482013-04-19 19:50:04878
879 private:
880 std::string etldp1_;
[email protected]5fa4f9a2013-10-03 10:13:16881 HasCookiesForETLDP1Callback callback_;
[email protected]ee209482013-04-19 19:50:04882
883 DISALLOW_COPY_AND_ASSIGN(HasCookiesForETLDP1Task);
884};
885
886void CookieMonster::HasCookiesForETLDP1Task::Run() {
887 bool result = this->cookie_monster()->HasCookiesForETLDP1(etldp1_);
888 if (!callback_.is_null()) {
889 this->InvokeCallback(
[email protected]5fa4f9a2013-10-03 10:13:16890 base::Bind(&HasCookiesForETLDP1Callback::Run,
[email protected]ee209482013-04-19 19:50:04891 base::Unretained(&callback_), result));
892 }
893}
894
[email protected]218aa6a12011-09-13 17:38:38895// Asynchronous CookieMonster API
896
897void CookieMonster::SetCookieWithDetailsAsync(
[email protected]dedec0b2013-02-28 04:50:10898 const GURL& url,
899 const std::string& name,
900 const std::string& value,
901 const std::string& domain,
902 const std::string& path,
[email protected]d8428d52013-08-07 06:58:25903 const Time& expiration_time,
[email protected]dedec0b2013-02-28 04:50:10904 bool secure,
905 bool http_only,
[email protected]ab2d75c82013-04-19 18:39:04906 CookiePriority priority,
[email protected]218aa6a12011-09-13 17:38:38907 const SetCookiesCallback& callback) {
908 scoped_refptr<SetCookieWithDetailsTask> task =
909 new SetCookieWithDetailsTask(this, url, name, value, domain, path,
[email protected]ab2d75c82013-04-19 18:39:04910 expiration_time, secure, http_only, priority,
[email protected]218aa6a12011-09-13 17:38:38911 callback);
[email protected]8562034e2011-10-17 17:35:04912 DoCookieTaskForURL(task, url);
[email protected]218aa6a12011-09-13 17:38:38913}
914
915void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) {
916 scoped_refptr<GetAllCookiesTask> task =
917 new GetAllCookiesTask(this, callback);
918
919 DoCookieTask(task);
920}
921
922
923void CookieMonster::GetAllCookiesForURLWithOptionsAsync(
924 const GURL& url,
925 const CookieOptions& options,
926 const GetCookieListCallback& callback) {
927 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
928 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
929
[email protected]8562034e2011-10-17 17:35:04930 DoCookieTaskForURL(task, url);
[email protected]218aa6a12011-09-13 17:38:38931}
932
933void CookieMonster::GetAllCookiesForURLAsync(
934 const GURL& url, const GetCookieListCallback& callback) {
935 CookieOptions options;
936 options.set_include_httponly();
937 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
938 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
939
[email protected]8562034e2011-10-17 17:35:04940 DoCookieTaskForURL(task, url);
[email protected]218aa6a12011-09-13 17:38:38941}
942
[email protected]ee209482013-04-19 19:50:04943void CookieMonster::HasCookiesForETLDP1Async(
944 const std::string& etldp1,
945 const HasCookiesForETLDP1Callback& callback) {
946 scoped_refptr<HasCookiesForETLDP1Task> task =
947 new HasCookiesForETLDP1Task(this, etldp1, callback);
948
949 DoCookieTaskForURL(task, GURL("http://" + etldp1));
950}
951
[email protected]218aa6a12011-09-13 17:38:38952void CookieMonster::DeleteAllAsync(const DeleteCallback& callback) {
953 scoped_refptr<DeleteAllTask> task =
954 new DeleteAllTask(this, callback);
955
956 DoCookieTask(task);
957}
958
959void CookieMonster::DeleteAllCreatedBetweenAsync(
960 const Time& delete_begin, const Time& delete_end,
961 const DeleteCallback& callback) {
962 scoped_refptr<DeleteAllCreatedBetweenTask> task =
963 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end,
964 callback);
965
966 DoCookieTask(task);
967}
968
[email protected]d8428d52013-08-07 06:58:25969void CookieMonster::DeleteAllCreatedBetweenForHostAsync(
970 const Time delete_begin,
971 const Time delete_end,
972 const GURL& url,
973 const DeleteCallback& callback) {
974 scoped_refptr<DeleteAllCreatedBetweenForHostTask> task =
975 new DeleteAllCreatedBetweenForHostTask(
976 this, delete_begin, delete_end, url, callback);
977
978 DoCookieTaskForURL(task, url);
979}
980
[email protected]218aa6a12011-09-13 17:38:38981void CookieMonster::DeleteAllForHostAsync(
982 const GURL& url, const DeleteCallback& callback) {
983 scoped_refptr<DeleteAllForHostTask> task =
984 new DeleteAllForHostTask(this, url, callback);
985
[email protected]8562034e2011-10-17 17:35:04986 DoCookieTaskForURL(task, url);
[email protected]218aa6a12011-09-13 17:38:38987}
988
989void CookieMonster::DeleteCanonicalCookieAsync(
990 const CanonicalCookie& cookie,
991 const DeleteCookieCallback& callback) {
992 scoped_refptr<DeleteCanonicalCookieTask> task =
993 new DeleteCanonicalCookieTask(this, cookie, callback);
994
995 DoCookieTask(task);
996}
997
998void CookieMonster::SetCookieWithOptionsAsync(
999 const GURL& url,
1000 const std::string& cookie_line,
1001 const CookieOptions& options,
1002 const SetCookiesCallback& callback) {
1003 scoped_refptr<SetCookieWithOptionsTask> task =
1004 new SetCookieWithOptionsTask(this, url, cookie_line, options, callback);
1005
[email protected]8562034e2011-10-17 17:35:041006 DoCookieTaskForURL(task, url);
[email protected]218aa6a12011-09-13 17:38:381007}
1008
1009void CookieMonster::GetCookiesWithOptionsAsync(
1010 const GURL& url,
1011 const CookieOptions& options,
1012 const GetCookiesCallback& callback) {
1013 scoped_refptr<GetCookiesWithOptionsTask> task =
1014 new GetCookiesWithOptionsTask(this, url, options, callback);
1015
[email protected]8562034e2011-10-17 17:35:041016 DoCookieTaskForURL(task, url);
[email protected]218aa6a12011-09-13 17:38:381017}
1018
[email protected]218aa6a12011-09-13 17:38:381019void CookieMonster::DeleteCookieAsync(const GURL& url,
1020 const std::string& cookie_name,
1021 const base::Closure& callback) {
1022 scoped_refptr<DeleteCookieTask> task =
1023 new DeleteCookieTask(this, url, cookie_name, callback);
1024
[email protected]8562034e2011-10-17 17:35:041025 DoCookieTaskForURL(task, url);
[email protected]218aa6a12011-09-13 17:38:381026}
1027
[email protected]264807b2012-04-25 14:49:371028void CookieMonster::DeleteSessionCookiesAsync(
1029 const CookieStore::DeleteCallback& callback) {
1030 scoped_refptr<DeleteSessionCookiesTask> task =
1031 new DeleteSessionCookiesTask(this, callback);
1032
1033 DoCookieTask(task);
1034}
1035
[email protected]218aa6a12011-09-13 17:38:381036void CookieMonster::DoCookieTask(
1037 const scoped_refptr<CookieMonsterTask>& task_item) {
[email protected]218aa6a12011-09-13 17:38:381038 {
1039 base::AutoLock autolock(lock_);
[email protected]8562034e2011-10-17 17:35:041040 InitIfNecessary();
[email protected]218aa6a12011-09-13 17:38:381041 if (!loaded_) {
[email protected]0184df32013-05-14 00:53:551042 tasks_pending_.push(task_item);
[email protected]218aa6a12011-09-13 17:38:381043 return;
1044 }
1045 }
1046
1047 task_item->Run();
1048}
1049
[email protected]8562034e2011-10-17 17:35:041050void CookieMonster::DoCookieTaskForURL(
1051 const scoped_refptr<CookieMonsterTask>& task_item,
1052 const GURL& url) {
1053 {
1054 base::AutoLock autolock(lock_);
1055 InitIfNecessary();
1056 // If cookies for the requested domain key (eTLD+1) have been loaded from DB
1057 // then run the task, otherwise load from DB.
1058 if (!loaded_) {
1059 // Checks if the domain key has been loaded.
[email protected]2fb376a2011-11-17 09:22:031060 std::string key(cookie_util::GetEffectiveDomain(url.scheme(),
1061 url.host()));
[email protected]8562034e2011-10-17 17:35:041062 if (keys_loaded_.find(key) == keys_loaded_.end()) {
1063 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > >
[email protected]0184df32013-05-14 00:53:551064 ::iterator it = tasks_pending_for_key_.find(key);
1065 if (it == tasks_pending_for_key_.end()) {
[email protected]8562034e2011-10-17 17:35:041066 store_->LoadCookiesForKey(key,
1067 base::Bind(&CookieMonster::OnKeyLoaded, this, key));
[email protected]0184df32013-05-14 00:53:551068 it = tasks_pending_for_key_.insert(std::make_pair(key,
[email protected]8562034e2011-10-17 17:35:041069 std::deque<scoped_refptr<CookieMonsterTask> >())).first;
1070 }
1071 it->second.push_back(task_item);
1072 return;
1073 }
1074 }
1075 }
1076 task_item->Run();
1077}
1078
[email protected]dedec0b2013-02-28 04:50:101079bool CookieMonster::SetCookieWithDetails(const GURL& url,
1080 const std::string& name,
1081 const std::string& value,
1082 const std::string& domain,
1083 const std::string& path,
1084 const base::Time& expiration_time,
1085 bool secure,
[email protected]ab2d75c82013-04-19 18:39:041086 bool http_only,
1087 CookiePriority priority) {
[email protected]20305ec2011-01-21 04:55:521088 base::AutoLock autolock(lock_);
[email protected]69bb5872010-01-12 20:33:521089
[email protected]f48b9432011-01-11 07:25:401090 if (!HasCookieableScheme(url))
initial.commit586acc5fe2008-07-26 22:42:521091 return false;
1092
[email protected]f48b9432011-01-11 07:25:401093 Time creation_time = CurrentTime();
1094 last_time_seen_ = creation_time;
1095
1096 scoped_ptr<CanonicalCookie> cc;
[email protected]ab2d75c82013-04-19 18:39:041097 cc.reset(CanonicalCookie::Create(url, name, value, domain, path,
1098 creation_time, expiration_time,
1099 secure, http_only, priority));
[email protected]f48b9432011-01-11 07:25:401100
1101 if (!cc.get())
1102 return false;
1103
1104 CookieOptions options;
1105 options.set_include_httponly();
1106 return SetCanonicalCookie(&cc, creation_time, options);
initial.commit586acc5fe2008-07-26 22:42:521107}
1108
bartfaba13acdf2014-09-05 15:07:281109bool CookieMonster::ImportCookies(const CookieList& list) {
[email protected]93460df2011-07-20 00:58:211110 base::AutoLock autolock(lock_);
1111 InitIfNecessary();
1112 for (net::CookieList::const_iterator iter = list.begin();
1113 iter != list.end(); ++iter) {
[email protected]5b9bc352012-07-18 13:13:341114 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie(*iter));
[email protected]93460df2011-07-20 00:58:211115 net::CookieOptions options;
1116 options.set_include_httponly();
[email protected]5b9bc352012-07-18 13:13:341117 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), options))
[email protected]93460df2011-07-20 00:58:211118 return false;
[email protected]93460df2011-07-20 00:58:211119 }
1120 return true;
1121}
1122
[email protected]f48b9432011-01-11 07:25:401123CookieList CookieMonster::GetAllCookies() {
[email protected]20305ec2011-01-21 04:55:521124 base::AutoLock autolock(lock_);
[email protected]f48b9432011-01-11 07:25:401125
1126 // This function is being called to scrape the cookie list for management UI
1127 // or similar. We shouldn't show expired cookies in this list since it will
1128 // just be confusing to users, and this function is called rarely enough (and
1129 // is already slow enough) that it's OK to take the time to garbage collect
1130 // the expired cookies now.
1131 //
1132 // Note that this does not prune cookies to be below our limits (if we've
1133 // exceeded them) the way that calling GarbageCollect() would.
1134 GarbageCollectExpired(Time::Now(),
1135 CookieMapItPair(cookies_.begin(), cookies_.end()),
1136 NULL);
1137
1138 // Copy the CanonicalCookie pointers from the map so that we can use the same
1139 // sorter as elsewhere, then copy the result out.
1140 std::vector<CanonicalCookie*> cookie_ptrs;
1141 cookie_ptrs.reserve(cookies_.size());
1142 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end(); ++it)
1143 cookie_ptrs.push_back(it->second);
1144 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
1145
1146 CookieList cookie_list;
1147 cookie_list.reserve(cookie_ptrs.size());
1148 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
1149 it != cookie_ptrs.end(); ++it)
1150 cookie_list.push_back(**it);
1151
1152 return cookie_list;
[email protected]f325f1e12010-04-30 22:38:551153}
1154
[email protected]f48b9432011-01-11 07:25:401155CookieList CookieMonster::GetAllCookiesForURLWithOptions(
1156 const GURL& url,
1157 const CookieOptions& options) {
[email protected]20305ec2011-01-21 04:55:521158 base::AutoLock autolock(lock_);
initial.commit586acc5fe2008-07-26 22:42:521159
[email protected]f48b9432011-01-11 07:25:401160 std::vector<CanonicalCookie*> cookie_ptrs;
1161 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs);
1162 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
initial.commit586acc5fe2008-07-26 22:42:521163
[email protected]f48b9432011-01-11 07:25:401164 CookieList cookies;
georgesakc15df6722014-12-02 23:52:121165 cookies.reserve(cookie_ptrs.size());
[email protected]f48b9432011-01-11 07:25:401166 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
1167 it != cookie_ptrs.end(); it++)
1168 cookies.push_back(**it);
initial.commit586acc5fe2008-07-26 22:42:521169
[email protected]f48b9432011-01-11 07:25:401170 return cookies;
initial.commit586acc5fe2008-07-26 22:42:521171}
1172
[email protected]f48b9432011-01-11 07:25:401173CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) {
1174 CookieOptions options;
1175 options.set_include_httponly();
1176
1177 return GetAllCookiesForURLWithOptions(url, options);
[email protected]f325f1e12010-04-30 22:38:551178}
1179
[email protected]f48b9432011-01-11 07:25:401180int CookieMonster::DeleteAll(bool sync_to_store) {
[email protected]20305ec2011-01-21 04:55:521181 base::AutoLock autolock(lock_);
[email protected]f48b9432011-01-11 07:25:401182
1183 int num_deleted = 0;
1184 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1185 CookieMap::iterator curit = it;
1186 ++it;
1187 InternalDeleteCookie(curit, sync_to_store,
1188 sync_to_store ? DELETE_COOKIE_EXPLICIT :
1189 DELETE_COOKIE_DONT_RECORD /* Destruction. */);
1190 ++num_deleted;
initial.commit586acc5fe2008-07-26 22:42:521191 }
1192
[email protected]f48b9432011-01-11 07:25:401193 return num_deleted;
initial.commit586acc5fe2008-07-26 22:42:521194}
1195
[email protected]f48b9432011-01-11 07:25:401196int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin,
[email protected]218aa6a12011-09-13 17:38:381197 const Time& delete_end) {
[email protected]20305ec2011-01-21 04:55:521198 base::AutoLock autolock(lock_);
[email protected]d0980332010-11-16 17:08:531199
[email protected]f48b9432011-01-11 07:25:401200 int num_deleted = 0;
1201 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1202 CookieMap::iterator curit = it;
1203 CanonicalCookie* cc = curit->second;
1204 ++it;
[email protected]d0980332010-11-16 17:08:531205
[email protected]f48b9432011-01-11 07:25:401206 if (cc->CreationDate() >= delete_begin &&
1207 (delete_end.is_null() || cc->CreationDate() < delete_end)) {
[email protected]218aa6a12011-09-13 17:38:381208 InternalDeleteCookie(curit,
1209 true, /*sync_to_store*/
1210 DELETE_COOKIE_EXPLICIT);
[email protected]f48b9432011-01-11 07:25:401211 ++num_deleted;
initial.commit586acc5fe2008-07-26 22:42:521212 }
1213 }
1214
[email protected]f48b9432011-01-11 07:25:401215 return num_deleted;
1216}
1217
[email protected]d8428d52013-08-07 06:58:251218int CookieMonster::DeleteAllCreatedBetweenForHost(const Time delete_begin,
1219 const Time delete_end,
1220 const GURL& url) {
[email protected]20305ec2011-01-21 04:55:521221 base::AutoLock autolock(lock_);
[email protected]f48b9432011-01-11 07:25:401222
1223 if (!HasCookieableScheme(url))
1224 return 0;
1225
[email protected]f48b9432011-01-11 07:25:401226 const std::string host(url.host());
1227
1228 // We store host cookies in the store by their canonical host name;
1229 // domain cookies are stored with a leading ".". So this is a pretty
1230 // simple lookup and per-cookie delete.
1231 int num_deleted = 0;
1232 for (CookieMapItPair its = cookies_.equal_range(GetKey(host));
1233 its.first != its.second;) {
1234 CookieMap::iterator curit = its.first;
1235 ++its.first;
1236
1237 const CanonicalCookie* const cc = curit->second;
1238
1239 // Delete only on a match as a host cookie.
[email protected]d8428d52013-08-07 06:58:251240 if (cc->IsHostCookie() && cc->IsDomainMatch(host) &&
1241 cc->CreationDate() >= delete_begin &&
1242 // The assumption that null |delete_end| is equivalent to
1243 // Time::Max() is confusing.
1244 (delete_end.is_null() || cc->CreationDate() < delete_end)) {
[email protected]f48b9432011-01-11 07:25:401245 num_deleted++;
1246
1247 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT);
1248 }
1249 }
1250 return num_deleted;
1251}
1252
[email protected]d8428d52013-08-07 06:58:251253int CookieMonster::DeleteAllForHost(const GURL& url) {
1254 return DeleteAllCreatedBetweenForHost(Time(), Time::Max(), url);
1255}
1256
1257
[email protected]f48b9432011-01-11 07:25:401258bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) {
[email protected]20305ec2011-01-21 04:55:521259 base::AutoLock autolock(lock_);
[email protected]f48b9432011-01-11 07:25:401260
1261 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain()));
1262 its.first != its.second; ++its.first) {
1263 // The creation date acts as our unique index...
1264 if (its.first->second->CreationDate() == cookie.CreationDate()) {
1265 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT);
1266 return true;
1267 }
1268 }
initial.commit586acc5fe2008-07-26 22:42:521269 return false;
1270}
1271
[email protected]5edff3c52014-06-23 20:27:481272void CookieMonster::SetCookieableSchemes(const char* const schemes[],
[email protected]dedec0b2013-02-28 04:50:101273 size_t num_schemes) {
[email protected]20305ec2011-01-21 04:55:521274 base::AutoLock autolock(lock_);
[email protected]bb8905722010-05-21 17:29:041275
[email protected]cf12bd12010-06-17 14:41:301276 // Cookieable Schemes must be set before first use of function.
1277 DCHECK(!initialized_);
1278
[email protected]47accfd62009-05-14 18:46:211279 cookieable_schemes_.clear();
1280 cookieable_schemes_.insert(cookieable_schemes_.end(),
1281 schemes, schemes + num_schemes);
1282}
1283
[email protected]97a3b6e2012-06-12 01:53:561284void CookieMonster::SetEnableFileScheme(bool accept) {
1285 // This assumes "file" is always at the end of the array. See the comment
1286 // above kDefaultCookieableSchemes.
1287 int num_schemes = accept ? kDefaultCookieableSchemesCount :
1288 kDefaultCookieableSchemesCount - 1;
1289 SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes);
1290}
1291
[email protected]ba4ad0e2011-03-15 08:12:471292void CookieMonster::SetKeepExpiredCookies() {
1293 keep_expired_cookies_ = true;
1294}
1295
[email protected]e67f0f42011-12-20 02:29:211296void CookieMonster::FlushStore(const base::Closure& callback) {
[email protected]20305ec2011-01-21 04:55:521297 base::AutoLock autolock(lock_);
[email protected]90499482013-06-01 00:39:501298 if (initialized_ && store_.get())
[email protected]e67f0f42011-12-20 02:29:211299 store_->Flush(callback);
1300 else if (!callback.is_null())
[email protected]2da659e2013-05-23 20:51:341301 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
[email protected]f48b9432011-01-11 07:25:401302}
1303
1304bool CookieMonster::SetCookieWithOptions(const GURL& url,
1305 const std::string& cookie_line,
1306 const CookieOptions& options) {
[email protected]20305ec2011-01-21 04:55:521307 base::AutoLock autolock(lock_);
[email protected]f48b9432011-01-11 07:25:401308
1309 if (!HasCookieableScheme(url)) {
1310 return false;
1311 }
1312
[email protected]f48b9432011-01-11 07:25:401313 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options);
1314}
1315
1316std::string CookieMonster::GetCookiesWithOptions(const GURL& url,
1317 const CookieOptions& options) {
[email protected]20305ec2011-01-21 04:55:521318 base::AutoLock autolock(lock_);
[email protected]f48b9432011-01-11 07:25:401319
[email protected]34a160d2011-05-12 22:12:491320 if (!HasCookieableScheme(url))
[email protected]f48b9432011-01-11 07:25:401321 return std::string();
[email protected]f48b9432011-01-11 07:25:401322
[email protected]f48b9432011-01-11 07:25:401323 std::vector<CanonicalCookie*> cookies;
1324 FindCookiesForHostAndDomain(url, options, true, &cookies);
1325 std::sort(cookies.begin(), cookies.end(), CookieSorter);
1326
[email protected]34a160d2011-05-12 22:12:491327 std::string cookie_line = BuildCookieLine(cookies);
[email protected]f48b9432011-01-11 07:25:401328
[email protected]f48b9432011-01-11 07:25:401329 VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line;
1330
1331 return cookie_line;
1332}
1333
1334void CookieMonster::DeleteCookie(const GURL& url,
1335 const std::string& cookie_name) {
[email protected]20305ec2011-01-21 04:55:521336 base::AutoLock autolock(lock_);
[email protected]f48b9432011-01-11 07:25:401337
1338 if (!HasCookieableScheme(url))
1339 return;
1340
1341 CookieOptions options;
1342 options.set_include_httponly();
1343 // Get the cookies for this host and its domain(s).
1344 std::vector<CanonicalCookie*> cookies;
1345 FindCookiesForHostAndDomain(url, options, true, &cookies);
1346 std::set<CanonicalCookie*> matching_cookies;
1347
1348 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin();
1349 it != cookies.end(); ++it) {
1350 if ((*it)->Name() != cookie_name)
1351 continue;
1352 if (url.path().find((*it)->Path()))
1353 continue;
1354 matching_cookies.insert(*it);
1355 }
1356
1357 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1358 CookieMap::iterator curit = it;
1359 ++it;
1360 if (matching_cookies.find(curit->second) != matching_cookies.end()) {
1361 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT);
1362 }
1363 }
1364}
1365
[email protected]264807b2012-04-25 14:49:371366int CookieMonster::DeleteSessionCookies() {
1367 base::AutoLock autolock(lock_);
1368
1369 int num_deleted = 0;
1370 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1371 CookieMap::iterator curit = it;
1372 CanonicalCookie* cc = curit->second;
1373 ++it;
1374
1375 if (!cc->IsPersistent()) {
1376 InternalDeleteCookie(curit,
1377 true, /*sync_to_store*/
1378 DELETE_COOKIE_EXPIRED);
1379 ++num_deleted;
1380 }
1381 }
1382
1383 return num_deleted;
1384}
1385
[email protected]ee209482013-04-19 19:50:041386bool CookieMonster::HasCookiesForETLDP1(const std::string& etldp1) {
1387 base::AutoLock autolock(lock_);
1388
1389 const std::string key(GetKey(etldp1));
1390
1391 CookieMapItPair its = cookies_.equal_range(key);
1392 return its.first != its.second;
1393}
1394
[email protected]f48b9432011-01-11 07:25:401395CookieMonster* CookieMonster::GetCookieMonster() {
1396 return this;
1397}
1398
[email protected]8ad5d462013-05-02 08:45:261399// This function must be called before the CookieMonster is used.
[email protected]93c53a32011-12-05 10:40:351400void CookieMonster::SetPersistSessionCookies(bool persist_session_cookies) {
[email protected]93c53a32011-12-05 10:40:351401 DCHECK(!initialized_);
1402 persist_session_cookies_ = persist_session_cookies;
1403}
1404
[email protected]bf510ed2012-06-05 08:31:431405void CookieMonster::SetForceKeepSessionState() {
[email protected]90499482013-06-01 00:39:501406 if (store_.get()) {
[email protected]bf510ed2012-06-05 08:31:431407 store_->SetForceKeepSessionState();
[email protected]93c53a32011-12-05 10:40:351408 }
1409}
1410
[email protected]f48b9432011-01-11 07:25:401411CookieMonster::~CookieMonster() {
1412 DeleteAll(false);
1413}
1414
1415bool CookieMonster::SetCookieWithCreationTime(const GURL& url,
1416 const std::string& cookie_line,
1417 const base::Time& creation_time) {
[email protected]90499482013-06-01 00:39:501418 DCHECK(!store_.get()) << "This method is only to be used by unit-tests.";
[email protected]20305ec2011-01-21 04:55:521419 base::AutoLock autolock(lock_);
[email protected]f48b9432011-01-11 07:25:401420
1421 if (!HasCookieableScheme(url)) {
1422 return false;
1423 }
1424
1425 InitIfNecessary();
1426 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time,
1427 CookieOptions());
1428}
1429
1430void CookieMonster::InitStore() {
[email protected]90499482013-06-01 00:39:501431 DCHECK(store_.get()) << "Store must exist to initialize";
[email protected]f48b9432011-01-11 07:25:401432
[email protected]218aa6a12011-09-13 17:38:381433 // We bind in the current time so that we can report the wall-clock time for
1434 // loading cookies.
1435 store_->Load(base::Bind(&CookieMonster::OnLoaded, this, TimeTicks::Now()));
1436}
[email protected]f48b9432011-01-11 07:25:401437
[email protected]28c5d0b72014-05-13 08:19:591438void CookieMonster::ReportLoaded() {
1439 if (delegate_.get())
1440 delegate_->OnLoaded();
1441}
1442
[email protected]218aa6a12011-09-13 17:38:381443void CookieMonster::OnLoaded(TimeTicks beginning_time,
1444 const std::vector<CanonicalCookie*>& cookies) {
1445 StoreLoadedCookies(cookies);
[email protected]c7593fb22011-11-14 23:54:271446 histogram_time_blocked_on_load_->AddTime(TimeTicks::Now() - beginning_time);
[email protected]218aa6a12011-09-13 17:38:381447
1448 // Invoke the task queue of cookie request.
1449 InvokeQueue();
[email protected]28c5d0b72014-05-13 08:19:591450
1451 ReportLoaded();
[email protected]218aa6a12011-09-13 17:38:381452}
1453
[email protected]8562034e2011-10-17 17:35:041454void CookieMonster::OnKeyLoaded(const std::string& key,
1455 const std::vector<CanonicalCookie*>& cookies) {
1456 // This function does its own separate locking.
1457 StoreLoadedCookies(cookies);
1458
[email protected]0184df32013-05-14 00:53:551459 std::deque<scoped_refptr<CookieMonsterTask> > tasks_pending_for_key;
[email protected]8562034e2011-10-17 17:35:041460
[email protected]bab72ec2013-10-30 20:50:021461 // We need to do this repeatedly until no more tasks were added to the queue
1462 // during the period where we release the lock.
1463 while (true) {
1464 {
1465 base::AutoLock autolock(lock_);
1466 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > >
1467 ::iterator it = tasks_pending_for_key_.find(key);
1468 if (it == tasks_pending_for_key_.end()) {
1469 keys_loaded_.insert(key);
1470 return;
1471 }
1472 if (it->second.empty()) {
1473 keys_loaded_.insert(key);
1474 tasks_pending_for_key_.erase(it);
1475 return;
1476 }
1477 it->second.swap(tasks_pending_for_key);
1478 }
1479
1480 while (!tasks_pending_for_key.empty()) {
1481 scoped_refptr<CookieMonsterTask> task = tasks_pending_for_key.front();
1482 task->Run();
1483 tasks_pending_for_key.pop_front();
1484 }
[email protected]8562034e2011-10-17 17:35:041485 }
1486}
1487
[email protected]218aa6a12011-09-13 17:38:381488void CookieMonster::StoreLoadedCookies(
1489 const std::vector<CanonicalCookie*>& cookies) {
[email protected]f48b9432011-01-11 07:25:401490 // Initialize the store and sync in any saved persistent cookies. We don't
1491 // care if it's expired, insert it so it can be garbage collected, removed,
1492 // and sync'd.
[email protected]218aa6a12011-09-13 17:38:381493 base::AutoLock autolock(lock_);
[email protected]f48b9432011-01-11 07:25:401494
[email protected]6210ce52013-09-20 03:33:141495 CookieItVector cookies_with_control_chars;
1496
[email protected]f48b9432011-01-11 07:25:401497 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin();
1498 it != cookies.end(); ++it) {
1499 int64 cookie_creation_time = (*it)->CreationDate().ToInternalValue();
1500
[email protected]8562034e2011-10-17 17:35:041501 if (creation_times_.insert(cookie_creation_time).second) {
[email protected]6210ce52013-09-20 03:33:141502 CookieMap::iterator inserted =
1503 InternalInsertCookie(GetKey((*it)->Domain()), *it, false);
[email protected]f48b9432011-01-11 07:25:401504 const Time cookie_access_time((*it)->LastAccessDate());
[email protected]8562034e2011-10-17 17:35:041505 if (earliest_access_time_.is_null() ||
1506 cookie_access_time < earliest_access_time_)
1507 earliest_access_time_ = cookie_access_time;
[email protected]6210ce52013-09-20 03:33:141508
1509 if (ContainsControlCharacter((*it)->Name()) ||
1510 ContainsControlCharacter((*it)->Value())) {
1511 cookies_with_control_chars.push_back(inserted);
1512 }
[email protected]f48b9432011-01-11 07:25:401513 } else {
1514 LOG(ERROR) << base::StringPrintf("Found cookies with duplicate creation "
1515 "times in backing store: "
1516 "{name='%s', domain='%s', path='%s'}",
1517 (*it)->Name().c_str(),
1518 (*it)->Domain().c_str(),
1519 (*it)->Path().c_str());
1520 // We've been given ownership of the cookie and are throwing it
1521 // away; reclaim the space.
1522 delete (*it);
1523 }
1524 }
[email protected]f48b9432011-01-11 07:25:401525
[email protected]6210ce52013-09-20 03:33:141526 // Any cookies that contain control characters that we have loaded from the
1527 // persistent store should be deleted. See http://crbug.com/238041.
1528 for (CookieItVector::iterator it = cookies_with_control_chars.begin();
1529 it != cookies_with_control_chars.end();) {
1530 CookieItVector::iterator curit = it;
1531 ++it;
1532
1533 InternalDeleteCookie(*curit, true, DELETE_COOKIE_CONTROL_CHAR);
1534 }
1535
[email protected]f48b9432011-01-11 07:25:401536 // After importing cookies from the PersistentCookieStore, verify that
1537 // none of our other constraints are violated.
[email protected]f48b9432011-01-11 07:25:401538 // In particular, the backing store might have given us duplicate cookies.
[email protected]8562034e2011-10-17 17:35:041539
1540 // This method could be called multiple times due to priority loading, thus
1541 // cookies loaded in previous runs will be validated again, but this is OK
1542 // since they are expected to be much fewer than total DB.
[email protected]f48b9432011-01-11 07:25:401543 EnsureCookiesMapIsValid();
[email protected]218aa6a12011-09-13 17:38:381544}
[email protected]f48b9432011-01-11 07:25:401545
[email protected]218aa6a12011-09-13 17:38:381546void CookieMonster::InvokeQueue() {
1547 while (true) {
1548 scoped_refptr<CookieMonsterTask> request_task;
1549 {
1550 base::AutoLock autolock(lock_);
[email protected]0184df32013-05-14 00:53:551551 if (tasks_pending_.empty()) {
[email protected]218aa6a12011-09-13 17:38:381552 loaded_ = true;
[email protected]8562034e2011-10-17 17:35:041553 creation_times_.clear();
1554 keys_loaded_.clear();
[email protected]218aa6a12011-09-13 17:38:381555 break;
1556 }
[email protected]0184df32013-05-14 00:53:551557 request_task = tasks_pending_.front();
1558 tasks_pending_.pop();
[email protected]218aa6a12011-09-13 17:38:381559 }
1560 request_task->Run();
1561 }
[email protected]f48b9432011-01-11 07:25:401562}
1563
1564void CookieMonster::EnsureCookiesMapIsValid() {
1565 lock_.AssertAcquired();
1566
1567 int num_duplicates_trimmed = 0;
1568
1569 // Iterate through all the of the cookies, grouped by host.
1570 CookieMap::iterator prev_range_end = cookies_.begin();
1571 while (prev_range_end != cookies_.end()) {
1572 CookieMap::iterator cur_range_begin = prev_range_end;
1573 const std::string key = cur_range_begin->first; // Keep a copy.
1574 CookieMap::iterator cur_range_end = cookies_.upper_bound(key);
1575 prev_range_end = cur_range_end;
1576
1577 // Ensure no equivalent cookies for this host.
1578 num_duplicates_trimmed +=
1579 TrimDuplicateCookiesForKey(key, cur_range_begin, cur_range_end);
1580 }
1581
1582 // Record how many duplicates were found in the database.
1583 // See InitializeHistograms() for details.
1584 histogram_cookie_deletion_cause_->Add(num_duplicates_trimmed);
1585}
1586
1587int CookieMonster::TrimDuplicateCookiesForKey(
1588 const std::string& key,
1589 CookieMap::iterator begin,
1590 CookieMap::iterator end) {
1591 lock_.AssertAcquired();
1592
1593 // Set of cookies ordered by creation time.
1594 typedef std::set<CookieMap::iterator, OrderByCreationTimeDesc> CookieSet;
1595
1596 // Helper map we populate to find the duplicates.
1597 typedef std::map<CookieSignature, CookieSet> EquivalenceMap;
1598 EquivalenceMap equivalent_cookies;
1599
1600 // The number of duplicate cookies that have been found.
1601 int num_duplicates = 0;
1602
1603 // Iterate through all of the cookies in our range, and insert them into
1604 // the equivalence map.
1605 for (CookieMap::iterator it = begin; it != end; ++it) {
1606 DCHECK_EQ(key, it->first);
1607 CanonicalCookie* cookie = it->second;
1608
1609 CookieSignature signature(cookie->Name(), cookie->Domain(),
1610 cookie->Path());
1611 CookieSet& set = equivalent_cookies[signature];
1612
1613 // We found a duplicate!
1614 if (!set.empty())
1615 num_duplicates++;
1616
1617 // We save the iterator into |cookies_| rather than the actual cookie
1618 // pointer, since we may need to delete it later.
1619 bool insert_success = set.insert(it).second;
1620 DCHECK(insert_success) <<
1621 "Duplicate creation times found in duplicate cookie name scan.";
1622 }
1623
1624 // If there were no duplicates, we are done!
1625 if (num_duplicates == 0)
1626 return 0;
1627
1628 // Make sure we find everything below that we did above.
1629 int num_duplicates_found = 0;
1630
1631 // Otherwise, delete all the duplicate cookies, both from our in-memory store
1632 // and from the backing store.
1633 for (EquivalenceMap::iterator it = equivalent_cookies.begin();
1634 it != equivalent_cookies.end();
1635 ++it) {
1636 const CookieSignature& signature = it->first;
1637 CookieSet& dupes = it->second;
1638
1639 if (dupes.size() <= 1)
1640 continue; // This cookiename/path has no duplicates.
1641 num_duplicates_found += dupes.size() - 1;
1642
1643 // Since |dups| is sorted by creation time (descending), the first cookie
1644 // is the most recent one, so we will keep it. The rest are duplicates.
1645 dupes.erase(dupes.begin());
1646
1647 LOG(ERROR) << base::StringPrintf(
1648 "Found %d duplicate cookies for host='%s', "
1649 "with {name='%s', domain='%s', path='%s'}",
1650 static_cast<int>(dupes.size()),
1651 key.c_str(),
1652 signature.name.c_str(),
1653 signature.domain.c_str(),
1654 signature.path.c_str());
1655
1656 // Remove all the cookies identified by |dupes|. It is valid to delete our
1657 // list of iterators one at a time, since |cookies_| is a multimap (they
1658 // don't invalidate existing iterators following deletion).
1659 for (CookieSet::iterator dupes_it = dupes.begin();
1660 dupes_it != dupes.end();
1661 ++dupes_it) {
[email protected]218aa6a12011-09-13 17:38:381662 InternalDeleteCookie(*dupes_it, true,
[email protected]f48b9432011-01-11 07:25:401663 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE);
1664 }
1665 }
1666 DCHECK_EQ(num_duplicates, num_duplicates_found);
1667
1668 return num_duplicates;
1669}
1670
[email protected]ba4ad0e2011-03-15 08:12:471671// Note: file must be the last scheme.
[email protected]5edff3c52014-06-23 20:27:481672const char* const CookieMonster::kDefaultCookieableSchemes[] =
[email protected]6757a5b82013-12-09 02:18:571673 { "http", "https", "ws", "wss", "file" };
[email protected]ba4ad0e2011-03-15 08:12:471674const int CookieMonster::kDefaultCookieableSchemesCount =
[email protected]5fa4f9a2013-10-03 10:13:161675 arraysize(kDefaultCookieableSchemes);
[email protected]ba4ad0e2011-03-15 08:12:471676
[email protected]f48b9432011-01-11 07:25:401677void CookieMonster::SetDefaultCookieableSchemes() {
[email protected]7c4b66b2014-01-04 12:28:131678 // Always disable file scheme unless SetEnableFileScheme(true) is called.
1679 SetCookieableSchemes(kDefaultCookieableSchemes,
1680 kDefaultCookieableSchemesCount - 1);
[email protected]f48b9432011-01-11 07:25:401681}
1682
[email protected]f48b9432011-01-11 07:25:401683void CookieMonster::FindCookiesForHostAndDomain(
1684 const GURL& url,
1685 const CookieOptions& options,
1686 bool update_access_time,
1687 std::vector<CanonicalCookie*>* cookies) {
1688 lock_.AssertAcquired();
1689
1690 const Time current_time(CurrentTime());
1691
1692 // Probe to save statistics relatively frequently. We do it here rather
1693 // than in the set path as many websites won't set cookies, and we
1694 // want to collect statistics whenever the browser's being used.
1695 RecordPeriodicStats(current_time);
1696
[email protected]8e1583672012-02-11 04:39:411697 // Can just dispatch to FindCookiesForKey
1698 const std::string key(GetKey(url.host()));
1699 FindCookiesForKey(key, url, options, current_time,
1700 update_access_time, cookies);
[email protected]f48b9432011-01-11 07:25:401701}
1702
[email protected]dedec0b2013-02-28 04:50:101703void CookieMonster::FindCookiesForKey(const std::string& key,
1704 const GURL& url,
1705 const CookieOptions& options,
1706 const Time& current,
1707 bool update_access_time,
1708 std::vector<CanonicalCookie*>* cookies) {
[email protected]f48b9432011-01-11 07:25:401709 lock_.AssertAcquired();
1710
[email protected]f48b9432011-01-11 07:25:401711 for (CookieMapItPair its = cookies_.equal_range(key);
1712 its.first != its.second; ) {
1713 CookieMap::iterator curit = its.first;
1714 CanonicalCookie* cc = curit->second;
1715 ++its.first;
1716
1717 // If the cookie is expired, delete it.
[email protected]ba4ad0e2011-03-15 08:12:471718 if (cc->IsExpired(current) && !keep_expired_cookies_) {
[email protected]f48b9432011-01-11 07:25:401719 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED);
1720 continue;
1721 }
1722
[email protected]65f4e7e2012-12-12 21:56:541723 // Filter out cookies that should not be included for a request to the
1724 // given |url|. HTTP only cookies are filtered depending on the passed
1725 // cookie |options|.
1726 if (!cc->IncludeForRequestURL(url, options))
[email protected]f48b9432011-01-11 07:25:401727 continue;
1728
[email protected]65f4e7e2012-12-12 21:56:541729 // Add this cookie to the set of matching cookies. Update the access
[email protected]f48b9432011-01-11 07:25:401730 // time if we've been requested to do so.
1731 if (update_access_time) {
1732 InternalUpdateCookieAccessTime(cc, current);
1733 }
1734 cookies->push_back(cc);
1735 }
1736}
1737
1738bool CookieMonster::DeleteAnyEquivalentCookie(const std::string& key,
1739 const CanonicalCookie& ecc,
[email protected]e7c590e52011-03-30 08:33:551740 bool skip_httponly,
1741 bool already_expired) {
[email protected]f48b9432011-01-11 07:25:401742 lock_.AssertAcquired();
1743
1744 bool found_equivalent_cookie = false;
1745 bool skipped_httponly = false;
1746 for (CookieMapItPair its = cookies_.equal_range(key);
1747 its.first != its.second; ) {
1748 CookieMap::iterator curit = its.first;
1749 CanonicalCookie* cc = curit->second;
1750 ++its.first;
1751
1752 if (ecc.IsEquivalent(*cc)) {
1753 // We should never have more than one equivalent cookie, since they should
1754 // overwrite each other.
1755 CHECK(!found_equivalent_cookie) <<
1756 "Duplicate equivalent cookies found, cookie store is corrupted.";
1757 if (skip_httponly && cc->IsHttpOnly()) {
1758 skipped_httponly = true;
1759 } else {
[email protected]e7c590e52011-03-30 08:33:551760 InternalDeleteCookie(curit, true, already_expired ?
1761 DELETE_COOKIE_EXPIRED_OVERWRITE : DELETE_COOKIE_OVERWRITE);
[email protected]f48b9432011-01-11 07:25:401762 }
1763 found_equivalent_cookie = true;
1764 }
1765 }
1766 return skipped_httponly;
1767}
1768
[email protected]6210ce52013-09-20 03:33:141769CookieMonster::CookieMap::iterator CookieMonster::InternalInsertCookie(
1770 const std::string& key,
1771 CanonicalCookie* cc,
1772 bool sync_to_store) {
[email protected]f48b9432011-01-11 07:25:401773 lock_.AssertAcquired();
1774
[email protected]90499482013-06-01 00:39:501775 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() &&
1776 sync_to_store)
[email protected]f48b9432011-01-11 07:25:401777 store_->AddCookie(*cc);
[email protected]6210ce52013-09-20 03:33:141778 CookieMap::iterator inserted =
1779 cookies_.insert(CookieMap::value_type(key, cc));
[email protected]8bb846f2011-03-23 12:08:181780 if (delegate_.get()) {
1781 delegate_->OnCookieChanged(
[email protected]7c4b66b2014-01-04 12:28:131782 *cc, false, CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT);
[email protected]8bb846f2011-03-23 12:08:181783 }
msarda0aad8f02014-10-30 09:22:391784 RunCallbacks(*cc, false);
[email protected]6210ce52013-09-20 03:33:141785
1786 return inserted;
[email protected]f48b9432011-01-11 07:25:401787}
1788
[email protected]34602282010-02-03 22:14:151789bool CookieMonster::SetCookieWithCreationTimeAndOptions(
1790 const GURL& url,
1791 const std::string& cookie_line,
1792 const Time& creation_time_or_null,
1793 const CookieOptions& options) {
[email protected]b866a02d2010-07-28 16:41:041794 lock_.AssertAcquired();
initial.commit586acc5fe2008-07-26 22:42:521795
[email protected]4d3ce782010-10-29 18:31:281796 VLOG(kVlogSetCookies) << "SetCookie() line: " << cookie_line;
initial.commit586acc5fe2008-07-26 22:42:521797
[email protected]34602282010-02-03 22:14:151798 Time creation_time = creation_time_or_null;
1799 if (creation_time.is_null()) {
1800 creation_time = CurrentTime();
1801 last_time_seen_ = creation_time;
1802 }
1803
[email protected]abbd13b2012-11-15 17:54:201804 scoped_ptr<CanonicalCookie> cc(
1805 CanonicalCookie::Create(url, cookie_line, creation_time, options));
initial.commit586acc5fe2008-07-26 22:42:521806
1807 if (!cc.get()) {
[email protected]4d3ce782010-10-29 18:31:281808 VLOG(kVlogSetCookies) << "WARNING: Failed to allocate CanonicalCookie";
initial.commit586acc5fe2008-07-26 22:42:521809 return false;
1810 }
[email protected]fa77eb512010-07-22 16:12:511811 return SetCanonicalCookie(&cc, creation_time, options);
[email protected]f325f1e12010-04-30 22:38:551812}
initial.commit586acc5fe2008-07-26 22:42:521813
[email protected]f325f1e12010-04-30 22:38:551814bool CookieMonster::SetCanonicalCookie(scoped_ptr<CanonicalCookie>* cc,
[email protected]f325f1e12010-04-30 22:38:551815 const Time& creation_time,
1816 const CookieOptions& options) {
[email protected]7a964a72010-09-07 19:33:261817 const std::string key(GetKey((*cc)->Domain()));
[email protected]e7c590e52011-03-30 08:33:551818 bool already_expired = (*cc)->IsExpired(creation_time);
ellyjones399e35a22014-10-27 11:09:561819
[email protected]e7c590e52011-03-30 08:33:551820 if (DeleteAnyEquivalentCookie(key, **cc, options.exclude_httponly(),
1821 already_expired)) {
[email protected]4d3ce782010-10-29 18:31:281822 VLOG(kVlogSetCookies) << "SetCookie() not clobbering httponly cookie";
[email protected]3a96c742008-11-19 19:46:271823 return false;
1824 }
initial.commit586acc5fe2008-07-26 22:42:521825
[email protected]4d3ce782010-10-29 18:31:281826 VLOG(kVlogSetCookies) << "SetCookie() key: " << key << " cc: "
1827 << (*cc)->DebugString();
initial.commit586acc5fe2008-07-26 22:42:521828
1829 // Realize that we might be setting an expired cookie, and the only point
1830 // was to delete the cookie which we've already done.
[email protected]e7c590e52011-03-30 08:33:551831 if (!already_expired || keep_expired_cookies_) {
[email protected]374f58b2010-07-20 15:29:261832 // See InitializeHistograms() for details.
[email protected]10b691f2012-07-11 15:22:151833 if ((*cc)->IsPersistent()) {
[email protected]8475bee2011-03-17 18:40:241834 histogram_expiration_duration_minutes_->Add(
1835 ((*cc)->ExpiryDate() - creation_time).InMinutes());
1836 }
1837
ellyjones399e35a22014-10-27 11:09:561838 {
1839 CanonicalCookie cookie = *(cc->get());
1840 InternalInsertCookie(key, cc->release(), true);
ellyjones399e35a22014-10-27 11:09:561841 }
[email protected]348dd662013-03-13 20:25:071842 } else {
1843 VLOG(kVlogSetCookies) << "SetCookie() not storing already expired cookie.";
[email protected]c4058fb2010-06-22 17:25:261844 }
initial.commit586acc5fe2008-07-26 22:42:521845
1846 // We assume that hopefully setting a cookie will be less common than
1847 // querying a cookie. Since setting a cookie can put us over our limits,
1848 // make sure that we garbage collect... We can also make the assumption that
1849 // if a cookie was set, in the common case it will be used soon after,
1850 // and we will purge the expired cookies in GetCookies().
[email protected]7a964a72010-09-07 19:33:261851 GarbageCollect(creation_time, key);
initial.commit586acc5fe2008-07-26 22:42:521852
1853 return true;
1854}
1855
[email protected]7a964a72010-09-07 19:33:261856void CookieMonster::InternalUpdateCookieAccessTime(CanonicalCookie* cc,
1857 const Time& current) {
[email protected]bb8905722010-05-21 17:29:041858 lock_.AssertAcquired();
1859
[email protected]77e0a462008-11-01 00:43:351860 // Based off the Mozilla code. When a cookie has been accessed recently,
1861 // don't bother updating its access time again. This reduces the number of
1862 // updates we do during pageload, which in turn reduces the chance our storage
1863 // backend will hit its batch thresholds and be forced to update.
[email protected]77e0a462008-11-01 00:43:351864 if ((current - cc->LastAccessDate()) < last_access_threshold_)
1865 return;
1866
[email protected]374f58b2010-07-20 15:29:261867 // See InitializeHistograms() for details.
1868 histogram_between_access_interval_minutes_->Add(
1869 (current - cc->LastAccessDate()).InMinutes());
[email protected]c4058fb2010-06-22 17:25:261870
[email protected]77e0a462008-11-01 00:43:351871 cc->SetLastAccessDate(current);
[email protected]90499482013-06-01 00:39:501872 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get())
[email protected]77e0a462008-11-01 00:43:351873 store_->UpdateCookieAccessTime(*cc);
1874}
1875
[email protected]6210ce52013-09-20 03:33:141876// InternalDeleteCookies must not invalidate iterators other than the one being
1877// deleted.
initial.commit586acc5fe2008-07-26 22:42:521878void CookieMonster::InternalDeleteCookie(CookieMap::iterator it,
[email protected]c4058fb2010-06-22 17:25:261879 bool sync_to_store,
1880 DeletionCause deletion_cause) {
[email protected]bb8905722010-05-21 17:29:041881 lock_.AssertAcquired();
1882
[email protected]8bb846f2011-03-23 12:08:181883 // Ideally, this would be asserted up where we define ChangeCauseMapping,
1884 // but DeletionCause's visibility (or lack thereof) forces us to make
1885 // this check here.
1886 COMPILE_ASSERT(arraysize(ChangeCauseMapping) == DELETE_COOKIE_LAST_ENTRY + 1,
1887 ChangeCauseMapping_size_not_eq_DeletionCause_enum_size);
1888
[email protected]374f58b2010-07-20 15:29:261889 // See InitializeHistograms() for details.
[email protected]7a964a72010-09-07 19:33:261890 if (deletion_cause != DELETE_COOKIE_DONT_RECORD)
1891 histogram_cookie_deletion_cause_->Add(deletion_cause);
[email protected]c4058fb2010-06-22 17:25:261892
initial.commit586acc5fe2008-07-26 22:42:521893 CanonicalCookie* cc = it->second;
[email protected]4d3ce782010-10-29 18:31:281894 VLOG(kVlogSetCookies) << "InternalDeleteCookie() cc: " << cc->DebugString();
[email protected]7a964a72010-09-07 19:33:261895
[email protected]90499482013-06-01 00:39:501896 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() &&
1897 sync_to_store)
initial.commit586acc5fe2008-07-26 22:42:521898 store_->DeleteCookie(*cc);
[email protected]8bb846f2011-03-23 12:08:181899 if (delegate_.get()) {
1900 ChangeCausePair mapping = ChangeCauseMapping[deletion_cause];
1901
1902 if (mapping.notify)
1903 delegate_->OnCookieChanged(*cc, true, mapping.cause);
1904 }
msarda0aad8f02014-10-30 09:22:391905 RunCallbacks(*cc, true);
initial.commit586acc5fe2008-07-26 22:42:521906 cookies_.erase(it);
1907 delete cc;
1908}
1909
[email protected]8807b322010-10-01 17:10:141910// Domain expiry behavior is unchanged by key/expiry scheme (the
[email protected]8ad5d462013-05-02 08:45:261911// meaning of the key is different, but that's not visible to this routine).
initial.commit586acc5fe2008-07-26 22:42:521912int CookieMonster::GarbageCollect(const Time& current,
1913 const std::string& key) {
[email protected]bb8905722010-05-21 17:29:041914 lock_.AssertAcquired();
1915
initial.commit586acc5fe2008-07-26 22:42:521916 int num_deleted = 0;
[email protected]8ad5d462013-05-02 08:45:261917 Time safe_date(
1918 Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays));
initial.commit586acc5fe2008-07-26 22:42:521919
[email protected]8ad5d462013-05-02 08:45:261920 // Collect garbage for this key, minding cookie priorities.
[email protected]7a964a72010-09-07 19:33:261921 if (cookies_.count(key) > kDomainMaxCookies) {
[email protected]4d3ce782010-10-29 18:31:281922 VLOG(kVlogGarbageCollection) << "GarbageCollect() key: " << key;
[email protected]7a964a72010-09-07 19:33:261923
[email protected]8ad5d462013-05-02 08:45:261924 CookieItVector cookie_its;
[email protected]8807b322010-10-01 17:10:141925 num_deleted += GarbageCollectExpired(
1926 current, cookies_.equal_range(key), &cookie_its);
[email protected]8ad5d462013-05-02 08:45:261927 if (cookie_its.size() > kDomainMaxCookies) {
1928 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect domain.";
1929 size_t purge_goal =
1930 cookie_its.size() - (kDomainMaxCookies - kDomainPurgeCookies);
1931 DCHECK(purge_goal > kDomainPurgeCookies);
1932
1933 // Boundary iterators into |cookie_its| for different priorities.
1934 CookieItVector::iterator it_bdd[4];
1935 // Intialize |it_bdd| while sorting |cookie_its| by priorities.
1936 // Schematic: [MLLHMHHLMM] => [LLL|MMMM|HHH], with 4 boundaries.
1937 it_bdd[0] = cookie_its.begin();
1938 it_bdd[3] = cookie_its.end();
1939 it_bdd[1] = PartitionCookieByPriority(it_bdd[0], it_bdd[3],
1940 COOKIE_PRIORITY_LOW);
1941 it_bdd[2] = PartitionCookieByPriority(it_bdd[1], it_bdd[3],
1942 COOKIE_PRIORITY_MEDIUM);
1943 size_t quota[3] = {
1944 kDomainCookiesQuotaLow,
1945 kDomainCookiesQuotaMedium,
1946 kDomainCookiesQuotaHigh
1947 };
1948
1949 // Purge domain cookies in 3 rounds.
1950 // Round 1: consider low-priority cookies only: evict least-recently
1951 // accessed, while protecting quota[0] of these from deletion.
1952 // Round 2: consider {low, medium}-priority cookies, evict least-recently
1953 // accessed, while protecting quota[0] + quota[1].
1954 // Round 3: consider all cookies, evict least-recently accessed.
1955 size_t accumulated_quota = 0;
1956 CookieItVector::iterator it_purge_begin = it_bdd[0];
1957 for (int i = 0; i < 3 && purge_goal > 0; ++i) {
1958 accumulated_quota += quota[i];
1959
[email protected]8ad5d462013-05-02 08:45:261960 size_t num_considered = it_bdd[i + 1] - it_purge_begin;
1961 if (num_considered <= accumulated_quota)
1962 continue;
1963
1964 // Number of cookies that will be purged in this round.
1965 size_t round_goal =
1966 std::min(purge_goal, num_considered - accumulated_quota);
1967 purge_goal -= round_goal;
1968
1969 SortLeastRecentlyAccessed(it_purge_begin, it_bdd[i + 1], round_goal);
1970 // Cookies accessed on or after |safe_date| would have been safe from
1971 // global purge, and we want to keep track of this.
1972 CookieItVector::iterator it_purge_end = it_purge_begin + round_goal;
1973 CookieItVector::iterator it_purge_middle =
1974 LowerBoundAccessDate(it_purge_begin, it_purge_end, safe_date);
1975 // Delete cookies accessed before |safe_date|.
1976 num_deleted += GarbageCollectDeleteRange(
1977 current,
1978 DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE,
1979 it_purge_begin,
1980 it_purge_middle);
1981 // Delete cookies accessed on or after |safe_date|.
1982 num_deleted += GarbageCollectDeleteRange(
1983 current,
1984 DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE,
1985 it_purge_middle,
1986 it_purge_end);
1987 it_purge_begin = it_purge_end;
1988 }
1989 DCHECK_EQ(0U, purge_goal);
[email protected]8807b322010-10-01 17:10:141990 }
initial.commit586acc5fe2008-07-26 22:42:521991 }
1992
[email protected]8ad5d462013-05-02 08:45:261993 // Collect garbage for everything. With firefox style we want to preserve
1994 // cookies accessed in kSafeFromGlobalPurgeDays, otherwise evict.
[email protected]8807b322010-10-01 17:10:141995 if (cookies_.size() > kMaxCookies &&
[email protected]8ad5d462013-05-02 08:45:261996 earliest_access_time_ < safe_date) {
[email protected]4d3ce782010-10-29 18:31:281997 VLOG(kVlogGarbageCollection) << "GarbageCollect() everything";
[email protected]8ad5d462013-05-02 08:45:261998 CookieItVector cookie_its;
[email protected]7a964a72010-09-07 19:33:261999 num_deleted += GarbageCollectExpired(
2000 current, CookieMapItPair(cookies_.begin(), cookies_.end()),
2001 &cookie_its);
[email protected]8ad5d462013-05-02 08:45:262002 if (cookie_its.size() > kMaxCookies) {
2003 VLOG(kVlogGarbageCollection) << "Deep Garbage Collect everything.";
2004 size_t purge_goal = cookie_its.size() - (kMaxCookies - kPurgeCookies);
2005 DCHECK(purge_goal > kPurgeCookies);
2006 // Sorts up to *and including* |cookie_its[purge_goal]|, so
2007 // |earliest_access_time| will be properly assigned even if
2008 // |global_purge_it| == |cookie_its.begin() + purge_goal|.
2009 SortLeastRecentlyAccessed(cookie_its.begin(), cookie_its.end(),
2010 purge_goal);
2011 // Find boundary to cookies older than safe_date.
2012 CookieItVector::iterator global_purge_it =
2013 LowerBoundAccessDate(cookie_its.begin(),
2014 cookie_its.begin() + purge_goal,
2015 safe_date);
2016 // Only delete the old cookies.
2017 num_deleted += GarbageCollectDeleteRange(
[email protected]8807b322010-10-01 17:10:142018 current,
[email protected]8807b322010-10-01 17:10:142019 DELETE_COOKIE_EVICTED_GLOBAL,
[email protected]8ad5d462013-05-02 08:45:262020 cookie_its.begin(),
2021 global_purge_it);
2022 // Set access day to the oldest cookie that wasn't deleted.
2023 earliest_access_time_ = (*global_purge_it)->second->LastAccessDate();
[email protected]8807b322010-10-01 17:10:142024 }
[email protected]c890ed192008-10-30 23:45:532025 }
2026
2027 return num_deleted;
2028}
2029
[email protected]c890ed192008-10-30 23:45:532030int CookieMonster::GarbageCollectExpired(
2031 const Time& current,
2032 const CookieMapItPair& itpair,
[email protected]8ad5d462013-05-02 08:45:262033 CookieItVector* cookie_its) {
[email protected]ba4ad0e2011-03-15 08:12:472034 if (keep_expired_cookies_)
2035 return 0;
2036
[email protected]bb8905722010-05-21 17:29:042037 lock_.AssertAcquired();
2038
[email protected]c890ed192008-10-30 23:45:532039 int num_deleted = 0;
2040 for (CookieMap::iterator it = itpair.first, end = itpair.second; it != end;) {
2041 CookieMap::iterator curit = it;
2042 ++it;
2043
2044 if (curit->second->IsExpired(current)) {
[email protected]2f3f3592010-07-07 20:11:512045 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED);
[email protected]c890ed192008-10-30 23:45:532046 ++num_deleted;
2047 } else if (cookie_its) {
2048 cookie_its->push_back(curit);
2049 }
initial.commit586acc5fe2008-07-26 22:42:522050 }
2051
2052 return num_deleted;
2053}
2054
[email protected]8ad5d462013-05-02 08:45:262055int CookieMonster::GarbageCollectDeleteRange(
[email protected]f48b9432011-01-11 07:25:402056 const Time& current,
[email protected]f48b9432011-01-11 07:25:402057 DeletionCause cause,
[email protected]5fa4f9a2013-10-03 10:13:162058 CookieItVector::iterator it_begin,
2059 CookieItVector::iterator it_end) {
[email protected]8ad5d462013-05-02 08:45:262060 for (CookieItVector::iterator it = it_begin; it != it_end; it++) {
2061 histogram_evicted_last_access_minutes_->Add(
2062 (current - (*it)->second->LastAccessDate()).InMinutes());
2063 InternalDeleteCookie((*it), true, cause);
[email protected]c10da4b02010-03-25 14:38:322064 }
[email protected]8ad5d462013-05-02 08:45:262065 return it_end - it_begin;
[email protected]c10da4b02010-03-25 14:38:322066}
2067
[email protected]ed32c212013-05-14 20:49:292068// A wrapper around registry_controlled_domains::GetDomainAndRegistry
[email protected]f48b9432011-01-11 07:25:402069// to make clear we're creating a key for our local map. Here and
2070// in FindCookiesForHostAndDomain() are the only two places where
2071// we need to conditionalize based on key type.
2072//
2073// Note that this key algorithm explicitly ignores the scheme. This is
2074// because when we're entering cookies into the map from the backing store,
2075// we in general won't have the scheme at that point.
2076// In practical terms, this means that file cookies will be stored
2077// in the map either by an empty string or by UNC name (and will be
2078// limited by kMaxCookiesPerHost), and extension cookies will be stored
2079// based on the single extension id, as the extension id won't have the
2080// form of a DNS host and hence GetKey() will return it unchanged.
2081//
2082// Arguably the right thing to do here is to make the key
2083// algorithm dependent on the scheme, and make sure that the scheme is
2084// available everywhere the key must be obtained (specfically at backing
2085// store load time). This would require either changing the backing store
2086// database schema to include the scheme (far more trouble than it's worth), or
2087// separating out file cookies into their own CookieMonster instance and
2088// thus restricting each scheme to a single cookie monster (which might
2089// be worth it, but is still too much trouble to solve what is currently a
2090// non-problem).
2091std::string CookieMonster::GetKey(const std::string& domain) const {
[email protected]f48b9432011-01-11 07:25:402092 std::string effective_domain(
[email protected]ed32c212013-05-14 20:49:292093 registry_controlled_domains::GetDomainAndRegistry(
[email protected]aabe1792014-01-30 21:37:462094 domain, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
[email protected]f48b9432011-01-11 07:25:402095 if (effective_domain.empty())
2096 effective_domain = domain;
2097
2098 if (!effective_domain.empty() && effective_domain[0] == '.')
2099 return effective_domain.substr(1);
2100 return effective_domain;
2101}
2102
[email protected]97a3b6e2012-06-12 01:53:562103bool CookieMonster::IsCookieableScheme(const std::string& scheme) {
2104 base::AutoLock autolock(lock_);
2105
2106 return std::find(cookieable_schemes_.begin(), cookieable_schemes_.end(),
2107 scheme) != cookieable_schemes_.end();
2108}
2109
[email protected]f48b9432011-01-11 07:25:402110bool CookieMonster::HasCookieableScheme(const GURL& url) {
2111 lock_.AssertAcquired();
2112
2113 // Make sure the request is on a cookie-able url scheme.
2114 for (size_t i = 0; i < cookieable_schemes_.size(); ++i) {
2115 // We matched a scheme.
2116 if (url.SchemeIs(cookieable_schemes_[i].c_str())) {
2117 // We've matched a supported scheme.
initial.commit586acc5fe2008-07-26 22:42:522118 return true;
2119 }
2120 }
[email protected]f48b9432011-01-11 07:25:402121
2122 // The scheme didn't match any in our whitelist.
2123 VLOG(kVlogPerCookieMonster) << "WARNING: Unsupported cookie scheme: "
2124 << url.scheme();
initial.commit586acc5fe2008-07-26 22:42:522125 return false;
2126}
2127
[email protected]c4058fb2010-06-22 17:25:262128// Test to see if stats should be recorded, and record them if so.
2129// The goal here is to get sampling for the average browser-hour of
2130// activity. We won't take samples when the web isn't being surfed,
2131// and when the web is being surfed, we'll take samples about every
2132// kRecordStatisticsIntervalSeconds.
2133// last_statistic_record_time_ is initialized to Now() rather than null
2134// in the constructor so that we won't take statistics right after
2135// startup, to avoid bias from browsers that are started but not used.
2136void CookieMonster::RecordPeriodicStats(const base::Time& current_time) {
2137 const base::TimeDelta kRecordStatisticsIntervalTime(
2138 base::TimeDelta::FromSeconds(kRecordStatisticsIntervalSeconds));
2139
[email protected]7a964a72010-09-07 19:33:262140 // If we've taken statistics recently, return.
2141 if (current_time - last_statistic_record_time_ <=
[email protected]c4058fb2010-06-22 17:25:262142 kRecordStatisticsIntervalTime) {
[email protected]7a964a72010-09-07 19:33:262143 return;
[email protected]c4058fb2010-06-22 17:25:262144 }
[email protected]7a964a72010-09-07 19:33:262145
2146 // See InitializeHistograms() for details.
2147 histogram_count_->Add(cookies_.size());
2148
2149 // More detailed statistics on cookie counts at different granularities.
2150 TimeTicks beginning_of_time(TimeTicks::Now());
2151
2152 for (CookieMap::const_iterator it_key = cookies_.begin();
2153 it_key != cookies_.end(); ) {
2154 const std::string& key(it_key->first);
2155
2156 int key_count = 0;
2157 typedef std::map<std::string, unsigned int> DomainMap;
2158 DomainMap domain_map;
2159 CookieMapItPair its_cookies = cookies_.equal_range(key);
2160 while (its_cookies.first != its_cookies.second) {
2161 key_count++;
2162 const std::string& cookie_domain(its_cookies.first->second->Domain());
2163 domain_map[cookie_domain]++;
2164
2165 its_cookies.first++;
2166 }
2167 histogram_etldp1_count_->Add(key_count);
2168 histogram_domain_per_etldp1_count_->Add(domain_map.size());
2169 for (DomainMap::const_iterator domain_map_it = domain_map.begin();
2170 domain_map_it != domain_map.end(); domain_map_it++)
2171 histogram_domain_count_->Add(domain_map_it->second);
2172
2173 it_key = its_cookies.second;
2174 }
2175
[email protected]4d3ce782010-10-29 18:31:282176 VLOG(kVlogPeriodic)
2177 << "Time for recording cookie stats (us): "
2178 << (TimeTicks::Now() - beginning_of_time).InMicroseconds();
[email protected]7a964a72010-09-07 19:33:262179
2180 last_statistic_record_time_ = current_time;
[email protected]c4058fb2010-06-22 17:25:262181}
2182
[email protected]f48b9432011-01-11 07:25:402183// Initialize all histogram counter variables used in this class.
2184//
2185// Normal histogram usage involves using the macros defined in
2186// histogram.h, which automatically takes care of declaring these
2187// variables (as statics), initializing them, and accumulating into
2188// them, all from a single entry point. Unfortunately, that solution
2189// doesn't work for the CookieMonster, as it's vulnerable to races between
2190// separate threads executing the same functions and hence initializing the
2191// same static variables. There isn't a race danger in the histogram
2192// accumulation calls; they are written to be resilient to simultaneous
2193// calls from multiple threads.
2194//
2195// The solution taken here is to have per-CookieMonster instance
2196// variables that are constructed during CookieMonster construction.
2197// Note that these variables refer to the same underlying histogram,
2198// so we still race (but safely) with other CookieMonster instances
2199// for accumulation.
2200//
2201// To do this we've expanded out the individual histogram macros calls,
2202// with declarations of the variables in the class decl, initialization here
2203// (done from the class constructor) and direct calls to the accumulation
2204// methods where needed. The specific histogram macro calls on which the
2205// initialization is based are included in comments below.
2206void CookieMonster::InitializeHistograms() {
2207 // From UMA_HISTOGRAM_CUSTOM_COUNTS
2208 histogram_expiration_duration_minutes_ = base::Histogram::FactoryGet(
2209 "Cookie.ExpirationDurationMinutes",
2210 1, kMinutesInTenYears, 50,
2211 base::Histogram::kUmaTargetedHistogramFlag);
2212 histogram_between_access_interval_minutes_ = base::Histogram::FactoryGet(
2213 "Cookie.BetweenAccessIntervalMinutes",
2214 1, kMinutesInTenYears, 50,
2215 base::Histogram::kUmaTargetedHistogramFlag);
2216 histogram_evicted_last_access_minutes_ = base::Histogram::FactoryGet(
2217 "Cookie.EvictedLastAccessMinutes",
2218 1, kMinutesInTenYears, 50,
2219 base::Histogram::kUmaTargetedHistogramFlag);
2220 histogram_count_ = base::Histogram::FactoryGet(
2221 "Cookie.Count", 1, 4000, 50,
2222 base::Histogram::kUmaTargetedHistogramFlag);
2223 histogram_domain_count_ = base::Histogram::FactoryGet(
2224 "Cookie.DomainCount", 1, 4000, 50,
2225 base::Histogram::kUmaTargetedHistogramFlag);
2226 histogram_etldp1_count_ = base::Histogram::FactoryGet(
2227 "Cookie.Etldp1Count", 1, 4000, 50,
2228 base::Histogram::kUmaTargetedHistogramFlag);
2229 histogram_domain_per_etldp1_count_ = base::Histogram::FactoryGet(
2230 "Cookie.DomainPerEtldp1Count", 1, 4000, 50,
2231 base::Histogram::kUmaTargetedHistogramFlag);
2232
2233 // From UMA_HISTOGRAM_COUNTS_10000 & UMA_HISTOGRAM_CUSTOM_COUNTS
2234 histogram_number_duplicate_db_cookies_ = base::Histogram::FactoryGet(
2235 "Net.NumDuplicateCookiesInDb", 1, 10000, 50,
2236 base::Histogram::kUmaTargetedHistogramFlag);
2237
2238 // From UMA_HISTOGRAM_ENUMERATION
2239 histogram_cookie_deletion_cause_ = base::LinearHistogram::FactoryGet(
2240 "Cookie.DeletionCause", 1,
2241 DELETE_COOKIE_LAST_ENTRY - 1, DELETE_COOKIE_LAST_ENTRY,
2242 base::Histogram::kUmaTargetedHistogramFlag);
2243
2244 // From UMA_HISTOGRAM_{CUSTOM_,}TIMES
[email protected]c7593fb22011-11-14 23:54:272245 histogram_time_blocked_on_load_ = base::Histogram::FactoryTimeGet(
2246 "Cookie.TimeBlockedOnLoad",
[email protected]f48b9432011-01-11 07:25:402247 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromMinutes(1),
2248 50, base::Histogram::kUmaTargetedHistogramFlag);
2249}
2250
2251
2252// The system resolution is not high enough, so we can have multiple
2253// set cookies that result in the same system time. When this happens, we
2254// increment by one Time unit. Let's hope computers don't get too fast.
2255Time CookieMonster::CurrentTime() {
2256 return std::max(Time::Now(),
2257 Time::FromInternalValue(last_time_seen_.ToInternalValue() + 1));
2258}
2259
[email protected]28c5d0b72014-05-13 08:19:592260bool CookieMonster::CopyCookiesForKeyToOtherCookieMonster(
2261 std::string key,
2262 CookieMonster* other) {
2263 ScopedVector<CanonicalCookie> duplicated_cookies;
2264
2265 {
2266 base::AutoLock autolock(lock_);
2267 DCHECK(other);
2268 if (!loaded_)
2269 return false;
2270
2271 for (CookieMapItPair its = cookies_.equal_range(key);
2272 its.first != its.second;
2273 ++its.first) {
2274 CookieMap::iterator curit = its.first;
2275 CanonicalCookie* cc = curit->second;
2276
2277 duplicated_cookies.push_back(cc->Duplicate());
2278 }
2279 }
2280
2281 {
2282 base::AutoLock autolock(other->lock_);
2283 if (!other->loaded_)
2284 return false;
2285
2286 // There must not exist any entries for the key to be copied in |other|.
2287 CookieMapItPair its = other->cookies_.equal_range(key);
2288 if (its.first != its.second)
2289 return false;
2290
2291 // Store the copied cookies in |other|.
2292 for (ScopedVector<CanonicalCookie>::const_iterator it =
2293 duplicated_cookies.begin();
2294 it != duplicated_cookies.end();
2295 ++it) {
2296 other->InternalInsertCookie(key, *it, true);
2297 }
2298
2299 // Since the cookies are owned by |other| now, weak clear must be used.
2300 duplicated_cookies.weak_clear();
2301 }
2302
2303 return true;
2304}
2305
2306bool CookieMonster::loaded() {
2307 base::AutoLock autolock(lock_);
2308 return loaded_;
2309}
2310
ellyjones399e35a22014-10-27 11:09:562311scoped_ptr<CookieStore::CookieChangedSubscription>
2312CookieMonster::AddCallbackForCookie(
2313 const GURL& gurl,
2314 const std::string& name,
2315 const CookieChangedCallback& callback) {
2316 base::AutoLock autolock(lock_);
2317 std::pair<GURL, std::string> key(gurl, name);
2318 if (hook_map_.count(key) == 0)
2319 hook_map_[key] = make_linked_ptr(new CookieChangedCallbackList());
msarda0aad8f02014-10-30 09:22:392320 return hook_map_[key]->Add(
2321 base::Bind(&RunAsync, base::MessageLoopProxy::current(), callback));
ellyjones399e35a22014-10-27 11:09:562322}
2323
msarda0aad8f02014-10-30 09:22:392324void CookieMonster::RunCallbacks(const CanonicalCookie& cookie, bool removed) {
ellyjones399e35a22014-10-27 11:09:562325 lock_.AssertAcquired();
2326 CookieOptions opts;
2327 opts.set_include_httponly();
2328 // Note that the callbacks in hook_map_ are wrapped with MakeAsync(), so they
2329 // are guaranteed to not take long - they just post a RunAsync task back to
2330 // the appropriate thread's message loop and return. It is important that this
2331 // method not run user-supplied callbacks directly, since the CookieMonster
2332 // lock is held and it is easy to accidentally introduce deadlocks.
2333 for (CookieChangedHookMap::iterator it = hook_map_.begin();
2334 it != hook_map_.end(); ++it) {
2335 std::pair<GURL, std::string> key = it->first;
2336 if (cookie.IncludeForRequestURL(key.first, opts) &&
2337 cookie.Name() == key.second) {
msarda0aad8f02014-10-30 09:22:392338 it->second->Notify(cookie, removed);
ellyjones399e35a22014-10-27 11:09:562339 }
2340 }
2341}
2342
[email protected]63725312012-07-19 08:24:162343} // namespace net