1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Brought to you by the letter D and the number 2.
7 #ifndef NET_COOKIES_COOKIE_MONSTER_H_
8 #define NET_COOKIES_COOKIE_MONSTER_H_
19 #include "base/basictypes.h"
20 #include "base/callback_forward.h"
21 #include "base/gtest_prod_util.h"
22 #include "base/memory/ref_counted.h"
23 #include "base/memory/scoped_ptr.h"
24 #include "base/synchronization/lock.h"
25 #include "base/time.h"
26 #include "net/cookies/cookie_store.h"
27 #include "net/base/net_export.h"
40 // The cookie monster is the system for storing and retrieving cookies. It has
41 // an in-memory list of all cookies, and synchronizes non-session cookies to an
42 // optional permanent storage that implements the PersistentCookieStore
45 // This class IS thread-safe. Normally, it is only used on the I/O thread, but
46 // is also accessed directly through Automation for UI testing.
48 // All cookie tasks are handled asynchronously. Tasks may be deferred if
49 // all affected cookies are not yet loaded from the backing store. Otherwise,
50 // the callback may be invoked immediately (prior to return of the asynchronous
53 // A cookie task is either pending loading of the entire cookie store, or
54 // loading of cookies for a specfic domain key(eTLD+1). In the former case, the
55 // cookie task will be queued in queue_ while PersistentCookieStore chain loads
56 // the cookie store on DB thread. In the latter case, the cookie task will be
57 // queued in tasks_queued_ while PermanentCookieStore loads cookies for the
58 // specified domain key(eTLD+1) on DB thread.
60 // Callbacks are guaranteed to be invoked on the calling thread.
62 // TODO(deanm) Implement CookieMonster, the cookie database.
63 // - Verify that our domain enforcement and non-dotted handling is correct
64 class NET_EXPORT CookieMonster
: public CookieStore
{
66 class CanonicalCookie
;
69 class PersistentCookieStore
;
72 // * The 'top level domain' (TLD) of an internet domain name is
73 // the terminal "." free substring (e.g. "com" for google.com
75 // * The 'effective top level domain' (eTLD) is the longest
76 // "." initiated terminal substring of an internet domain name
77 // that is controlled by a general domain registrar.
78 // (e.g. "co.uk" for news.bbc.co.uk).
79 // * The 'effective top level domain plus one' (eTLD+1) is the
80 // shortest "." delimited terminal substring of an internet
81 // domain name that is not controlled by a general domain
82 // registrar (e.g. "bbc.co.uk" for news.bbc.co.uk, or
83 // "google.com" for news.google.com). The general assumption
84 // is that all hosts and domains under an eTLD+1 share some
85 // administrative control.
87 // CookieMap is the central data structure of the CookieMonster. It
88 // is a map whose values are pointers to CanonicalCookie data
89 // structures (the data structures are owned by the CookieMonster
90 // and must be destroyed when removed from the map). The key is based on the
91 // effective domain of the cookies. If the domain of the cookie has an
92 // eTLD+1, that is the key for the map. If the domain of the cookie does not
93 // have an eTLD+1, the key of the map is the host the cookie applies to (it is
94 // not legal to have domain cookies without an eTLD+1). This rule
95 // excludes cookies for, e.g, ".com", ".co.uk", or ".internalnetwork".
96 // This behavior is the same as the behavior in Firefox v 3.6.10.
99 // I benchmarked hash_multimap vs multimap. We're going to be query-heavy
100 // so it would seem like hashing would help. However they were very
101 // close, with multimap being a tiny bit faster. I think this is because
102 // our map is at max around 1000 entries, and the additional complexity
103 // for the hashing might not overcome the O(log(1000)) for querying
104 // a multimap. Also, multimap is standard, another reason to use it.
105 // TODO(rdsmith): This benchmark should be re-done now that we're allowing
106 // subtantially more entries in the map.
107 typedef std::multimap
<std::string
, CanonicalCookie
*> CookieMap
;
108 typedef std::pair
<CookieMap::iterator
, CookieMap::iterator
> CookieMapItPair
;
110 // The store passed in should not have had Init() called on it yet. This
111 // class will take care of initializing it. The backing store is NOT owned by
112 // this class, but it must remain valid for the duration of the cookie
113 // monster's existence. If |store| is NULL, then no backing store will be
114 // updated. If |delegate| is non-NULL, it will be notified on
115 // creation/deletion of cookies.
116 CookieMonster(PersistentCookieStore
* store
, Delegate
* delegate
);
118 // Only used during unit testing.
119 CookieMonster(PersistentCookieStore
* store
,
121 int last_access_threshold_milliseconds
);
123 // Parses the string with the cookie time (very forgivingly).
124 static base::Time
ParseCookieTime(const std::string
& time_string
);
126 // Helper function that adds all cookies from |list| into this instance.
127 bool InitializeFrom(const CookieList
& list
);
129 typedef base::Callback
<void(const CookieList
& cookies
)> GetCookieListCallback
;
130 typedef base::Callback
<void(bool success
)> DeleteCookieCallback
;
132 // Sets a cookie given explicit user-provided cookie attributes. The cookie
133 // name, value, domain, etc. are each provided as separate strings. This
134 // function expects each attribute to be well-formed. It will check for
135 // disallowed characters (e.g. the ';' character is disallowed within the
136 // cookie value attribute) and will return false without setting the cookie
137 // if such characters are found.
138 void SetCookieWithDetailsAsync(const GURL
& url
,
139 const std::string
& name
,
140 const std::string
& value
,
141 const std::string
& domain
,
142 const std::string
& path
,
143 const base::Time
& expiration_time
,
144 bool secure
, bool http_only
,
145 const SetCookiesCallback
& callback
);
148 // Returns all the cookies, for use in management UI, etc. This does not mark
149 // the cookies as having been accessed.
150 // The returned cookies are ordered by longest path, then by earliest
152 void GetAllCookiesAsync(const GetCookieListCallback
& callback
);
154 // Returns all the cookies, for use in management UI, etc. Filters results
155 // using given url scheme, host / domain and path and options. This does not
156 // mark the cookies as having been accessed.
157 // The returned cookies are ordered by longest path, then earliest
159 void GetAllCookiesForURLWithOptionsAsync(
161 const CookieOptions
& options
,
162 const GetCookieListCallback
& callback
);
164 // Invokes GetAllCookiesForURLWithOptions with options set to include HTTP
166 void GetAllCookiesForURLAsync(const GURL
& url
,
167 const GetCookieListCallback
& callback
);
169 // Deletes all of the cookies.
170 void DeleteAllAsync(const DeleteCallback
& callback
);
172 // Deletes all cookies that match the host of the given URL
173 // regardless of path. This includes all http_only and secure cookies,
174 // but does not include any domain cookies that may apply to this host.
175 // Returns the number of cookies deleted.
176 void DeleteAllForHostAsync(const GURL
& url
,
177 const DeleteCallback
& callback
);
179 // Deletes one specific cookie.
180 void DeleteCanonicalCookieAsync(const CanonicalCookie
& cookie
,
181 const DeleteCookieCallback
& callback
);
183 // Override the default list of schemes that are allowed to be set in
184 // this cookie store. Calling his overrides the value of
185 // "enable_file_scheme_".
186 // If this this method is called, it must be called before first use of
187 // the instance (i.e. as part of the instance initialization process).
188 void SetCookieableSchemes(const char* schemes
[], size_t num_schemes
);
190 // Instructs the cookie monster to not delete expired cookies. This is used
191 // in cases where the cookie monster is used as a data structure to keep
192 // arbitrary cookies.
193 void SetKeepExpiredCookies();
195 // Delegates the call to set the |clear_local_store_on_exit_| flag of the
196 // PersistentStore if it exists.
197 void SetClearPersistentStoreOnExit(bool clear_local_store
);
199 // There are some unknowns about how to correctly handle file:// cookies,
200 // and our implementation for this is not robust enough. This allows you
201 // to enable support, but it should only be used for testing. Bug 1157243.
202 // Must be called before creating a CookieMonster instance.
203 static void EnableFileScheme();
205 // Flush the backing store (if any) to disk and post the given callback when
207 // WARNING: THE CALLBACK WILL RUN ON A RANDOM THREAD. IT MUST BE THREAD SAFE.
208 // It may be posted to the current thread, or it may run on the thread that
209 // actually does the flushing. Your Task should generally post a notification
210 // to the thread you actually want to be notified on.
211 void FlushStore(const base::Closure
& callback
);
213 // CookieStore implementation.
215 // Sets the cookies specified by |cookie_list| returned from |url|
216 // with options |options| in effect.
217 virtual void SetCookieWithOptionsAsync(
219 const std::string
& cookie_line
,
220 const CookieOptions
& options
,
221 const SetCookiesCallback
& callback
) OVERRIDE
;
223 // Gets all cookies that apply to |url| given |options|.
224 // The returned cookies are ordered by longest path, then earliest
226 virtual void GetCookiesWithOptionsAsync(
228 const CookieOptions
& options
,
229 const GetCookiesCallback
& callback
) OVERRIDE
;
231 virtual void GetCookiesWithInfoAsync(
233 const CookieOptions
& options
,
234 const GetCookieInfoCallback
& callback
) OVERRIDE
;
236 // Deletes all cookies with that might apply to |url| that has |cookie_name|.
237 virtual void DeleteCookieAsync(
238 const GURL
& url
, const std::string
& cookie_name
,
239 const base::Closure
& callback
) OVERRIDE
;
241 // Deletes all of the cookies that have a creation_date greater than or equal
242 // to |delete_begin| and less than |delete_end|
243 // Returns the number of cookies that have been deleted.
244 virtual void DeleteAllCreatedBetweenAsync(
245 const base::Time
& delete_begin
,
246 const base::Time
& delete_end
,
247 const DeleteCallback
& callback
) OVERRIDE
;
249 virtual void DeleteSessionCookiesAsync(const DeleteCallback
&) OVERRIDE
;
251 virtual CookieMonster
* GetCookieMonster() OVERRIDE
;
253 // Enables writing session cookies into the cookie database. If this this
254 // method is called, it must be called before first use of the instance
255 // (i.e. as part of the instance initialization process).
256 void SetPersistSessionCookies(bool persist_session_cookies
);
258 // Protects session cookies from deletion on shutdown.
259 void SaveSessionCookies();
261 // Debugging method to perform various validation checks on the map.
262 // Currently just checking that there are no null CanonicalCookie pointers
264 // Argument |arg| is to allow retaining of arbitrary data if the CHECKs
265 // in the function trip. TODO(rdsmith):Remove hack.
266 void ValidateMap(int arg
);
268 // The default list of schemes the cookie monster can handle.
269 static const char* kDefaultCookieableSchemes
[];
270 static const int kDefaultCookieableSchemesCount
;
273 // For queueing the cookie monster calls.
274 class CookieMonsterTask
;
275 class DeleteAllCreatedBetweenTask
;
276 class DeleteAllForHostTask
;
278 class DeleteCookieTask
;
279 class DeleteCanonicalCookieTask
;
280 class GetAllCookiesForURLWithOptionsTask
;
281 class GetAllCookiesTask
;
282 class GetCookiesWithOptionsTask
;
283 class GetCookiesWithInfoTask
;
284 class SetCookieWithDetailsTask
;
285 class SetCookieWithOptionsTask
;
286 class DeleteSessionCookiesTask
;
289 // For SetCookieWithCreationTime.
290 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest
,
291 TestCookieDeleteAllCreatedBetweenTimestamps
);
293 // For gargage collection constants.
294 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest
, TestHostGarbageCollection
);
295 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest
, TestTotalGarbageCollection
);
296 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest
, GarbageCollectionTriggers
);
297 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest
, TestGCTimes
);
299 // For validation of key values.
300 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest
, TestDomainTree
);
301 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest
, TestImport
);
302 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest
, GetKey
);
303 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest
, TestGetKey
);
305 // For FindCookiesForKey.
306 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest
, ShortLivedSessionCookies
);
308 // Internal reasons for deletion, used to populate informative histograms
309 // and to provide a public cause for onCookieChange notifications.
311 // If you add or remove causes from this list, please be sure to also update
312 // the Delegate::ChangeCause mapping inside ChangeCauseMapping. Moreover,
313 // these are used as array indexes, so avoid reordering to keep the
314 // histogram buckets consistent. New items (if necessary) should be added
315 // at the end of the list, just before DELETE_COOKIE_LAST_ENTRY.
317 DELETE_COOKIE_EXPLICIT
= 0,
318 DELETE_COOKIE_OVERWRITE
,
319 DELETE_COOKIE_EXPIRED
,
320 DELETE_COOKIE_EVICTED
,
321 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE
,
322 DELETE_COOKIE_DONT_RECORD
, // e.g. For final cleanup after flush to store.
323 DELETE_COOKIE_EVICTED_DOMAIN
,
324 DELETE_COOKIE_EVICTED_GLOBAL
,
326 // Cookies evicted during domain level garbage collection that
327 // were accessed longer ago than kSafeFromGlobalPurgeDays
328 DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE
,
330 // Cookies evicted during domain level garbage collection that
331 // were accessed more recently than kSafeFromGlobalPurgeDays
332 // (and thus would have been preserved by global garbage collection).
333 DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE
,
335 // A common idiom is to remove a cookie by overwriting it with an
336 // already-expired expiration date. This captures that case.
337 DELETE_COOKIE_EXPIRED_OVERWRITE
,
339 DELETE_COOKIE_LAST_ENTRY
342 // Cookie garbage collection thresholds. Based off of the Mozilla defaults.
343 // When the number of cookies gets to k{Domain,}MaxCookies
344 // purge down to k{Domain,}MaxCookies - k{Domain,}PurgeCookies.
345 // It might seem scary to have a high purge value, but really it's not.
346 // You just make sure that you increase the max to cover the increase
347 // in purge, and we would have been purging the same amount of cookies.
348 // We're just going through the garbage collection process less often.
349 // Note that the DOMAIN values are per eTLD+1; see comment for the
350 // CookieMap typedef. So, e.g., the maximum number of cookies allowed for
351 // google.com and all of its subdomains will be 150-180.
353 // Any cookies accessed more recently than kSafeFromGlobalPurgeDays will not
354 // be evicted by global garbage collection, even if we have more than
355 // kMaxCookies. This does not affect domain garbage collection.
357 // Present in .h file to make accessible to tests through FRIEND_TEST.
358 // Actual definitions are in cookie_monster.cc.
359 static const size_t kDomainMaxCookies
;
360 static const size_t kDomainPurgeCookies
;
361 static const size_t kMaxCookies
;
362 static const size_t kPurgeCookies
;
364 // The number of days since last access that cookies will not be subject
365 // to global garbage collection.
366 static const int kSafeFromGlobalPurgeDays
;
368 // Record statistics every kRecordStatisticsIntervalSeconds of uptime.
369 static const int kRecordStatisticsIntervalSeconds
= 10 * 60;
371 virtual ~CookieMonster();
373 // The following are synchronous calls to which the asynchronous methods
374 // delegate either immediately (if the store is loaded) or through a deferred
375 // task (if the store is not yet loaded).
376 bool SetCookieWithDetails(const GURL
& url
,
377 const std::string
& name
,
378 const std::string
& value
,
379 const std::string
& domain
,
380 const std::string
& path
,
381 const base::Time
& expiration_time
,
382 bool secure
, bool http_only
);
384 CookieList
GetAllCookies();
386 CookieList
GetAllCookiesForURLWithOptions(const GURL
& url
,
387 const CookieOptions
& options
);
389 CookieList
GetAllCookiesForURL(const GURL
& url
);
391 int DeleteAll(bool sync_to_store
);
393 int DeleteAllCreatedBetween(const base::Time
& delete_begin
,
394 const base::Time
& delete_end
);
396 int DeleteAllForHost(const GURL
& url
);
398 bool DeleteCanonicalCookie(const CanonicalCookie
& cookie
);
400 bool SetCookieWithOptions(const GURL
& url
,
401 const std::string
& cookie_line
,
402 const CookieOptions
& options
);
404 std::string
GetCookiesWithOptions(const GURL
& url
,
405 const CookieOptions
& options
);
407 void GetCookiesWithInfo(const GURL
& url
,
408 const CookieOptions
& options
,
409 std::string
* cookie_line
,
410 std::vector
<CookieInfo
>* cookie_infos
);
412 void DeleteCookie(const GURL
& url
, const std::string
& cookie_name
);
414 bool SetCookieWithCreationTime(const GURL
& url
,
415 const std::string
& cookie_line
,
416 const base::Time
& creation_time
);
418 int DeleteSessionCookies();
420 // Called by all non-static functions to ensure that the cookies store has
421 // been initialized. This is not done during creating so it doesn't block
422 // the window showing.
423 // Note: this method should always be called with lock_ held.
424 void InitIfNecessary() {
435 // Initializes the backing store and reads existing cookies from it.
436 // Should only be called by InitIfNecessary().
439 // Stores cookies loaded from the backing store and invokes any deferred
440 // calls. |beginning_time| should be the moment PersistentCookieStore::Load
441 // was invoked and is used for reporting histogram_time_blocked_on_load_.
442 // See PersistentCookieStore::Load for details on the contents of cookies.
443 void OnLoaded(base::TimeTicks beginning_time
,
444 const std::vector
<CanonicalCookie
*>& cookies
);
446 // Stores cookies loaded from the backing store and invokes the deferred
447 // task(s) pending loading of cookies associated with the domain key
448 // (eTLD+1). Called when all cookies for the domain key(eTLD+1) have been
449 // loaded from DB. See PersistentCookieStore::Load for details on the contents
452 const std::string
& key
,
453 const std::vector
<CanonicalCookie
*>& cookies
);
455 // Stores the loaded cookies.
456 void StoreLoadedCookies(const std::vector
<CanonicalCookie
*>& cookies
);
458 // Invokes deferred calls.
461 // Checks that |cookies_| matches our invariants, and tries to repair any
462 // inconsistencies. (In other words, it does not have duplicate cookies).
463 void EnsureCookiesMapIsValid();
465 // Checks for any duplicate cookies for CookieMap key |key| which lie between
466 // |begin| and |end|. If any are found, all but the most recent are deleted.
467 // Returns the number of duplicate cookies that were deleted.
468 int TrimDuplicateCookiesForKey(const std::string
& key
,
469 CookieMap::iterator begin
,
470 CookieMap::iterator end
);
472 void SetDefaultCookieableSchemes();
474 void FindCookiesForHostAndDomain(const GURL
& url
,
475 const CookieOptions
& options
,
476 bool update_access_time
,
477 std::vector
<CanonicalCookie
*>* cookies
);
479 void FindCookiesForKey(const std::string
& key
,
481 const CookieOptions
& options
,
482 const base::Time
& current
,
483 bool update_access_time
,
484 std::vector
<CanonicalCookie
*>* cookies
);
486 // Delete any cookies that are equivalent to |ecc| (same path, domain, etc).
487 // If |skip_httponly| is true, httponly cookies will not be deleted. The
488 // return value with be true if |skip_httponly| skipped an httponly cookie.
489 // |key| is the key to find the cookie in cookies_; see the comment before
490 // the CookieMap typedef for details.
491 // NOTE: There should never be more than a single matching equivalent cookie.
492 bool DeleteAnyEquivalentCookie(const std::string
& key
,
493 const CanonicalCookie
& ecc
,
495 bool already_expired
);
497 // Takes ownership of *cc.
498 void InternalInsertCookie(const std::string
& key
,
502 // Helper function that sets cookies with more control.
503 // Not exposed as we don't want callers to have the ability
504 // to specify (potentially duplicate) creation times.
505 bool SetCookieWithCreationTimeAndOptions(const GURL
& url
,
506 const std::string
& cookie_line
,
507 const base::Time
& creation_time
,
508 const CookieOptions
& options
);
510 // Helper function that sets a canonical cookie, deleting equivalents and
511 // performing garbage collection.
512 bool SetCanonicalCookie(scoped_ptr
<CanonicalCookie
>* cc
,
513 const base::Time
& creation_time
,
514 const CookieOptions
& options
);
516 void InternalUpdateCookieAccessTime(CanonicalCookie
* cc
,
517 const base::Time
& current_time
);
519 // |deletion_cause| argument is used for collecting statistics and choosing
520 // the correct Delegate::ChangeCause for OnCookieChanged notifications.
521 void InternalDeleteCookie(CookieMap::iterator it
, bool sync_to_store
,
522 DeletionCause deletion_cause
);
524 // If the number of cookies for CookieMap key |key|, or globally, are
525 // over the preset maximums above, garbage collect, first for the host and
526 // then globally. See comments above garbage collection threshold
527 // constants for details.
529 // Returns the number of cookies deleted (useful for debugging).
530 int GarbageCollect(const base::Time
& current
, const std::string
& key
);
532 // Helper for GarbageCollect(); can be called directly as well. Deletes
533 // all expired cookies in |itpair|. If |cookie_its| is non-NULL, it is
534 // populated with all the non-expired cookies from |itpair|.
536 // Returns the number of cookies deleted.
537 int GarbageCollectExpired(const base::Time
& current
,
538 const CookieMapItPair
& itpair
,
539 std::vector
<CookieMap::iterator
>* cookie_its
);
541 // Helper for GarbageCollect(). Deletes all cookies in the list
542 // that were accessed before |keep_accessed_after|, using DeletionCause
543 // |cause|. If |keep_accessed_after| is null, deletes all cookies in the
544 // list. Returns the number of cookies deleted.
545 int GarbageCollectDeleteList(const base::Time
& current
,
546 const base::Time
& keep_accessed_after
,
548 std::vector
<CookieMap::iterator
>& cookie_its
);
550 // Find the key (for lookup in cookies_) based on the given domain.
551 // See comment on keys before the CookieMap typedef.
552 std::string
GetKey(const std::string
& domain
) const;
554 bool HasCookieableScheme(const GURL
& url
);
556 // Statistics support
558 // This function should be called repeatedly, and will record
559 // statistics if a sufficient time period has passed.
560 void RecordPeriodicStats(const base::Time
& current_time
);
562 // Initialize the above variables; should only be called from
564 void InitializeHistograms();
566 // The resolution of our time isn't enough, so we do something
567 // ugly and increment when we've seen the same time twice.
568 base::Time
CurrentTime();
570 // Runs the task if, or defers the task until, the full cookie database is
572 void DoCookieTask(const scoped_refptr
<CookieMonsterTask
>& task_item
);
574 // Runs the task if, or defers the task until, the cookies for the given URL
576 void DoCookieTaskForURL(const scoped_refptr
<CookieMonsterTask
>& task_item
,
579 // Histogram variables; see CookieMonster::InitializeHistograms() in
580 // cookie_monster.cc for details.
581 base::Histogram
* histogram_expiration_duration_minutes_
;
582 base::Histogram
* histogram_between_access_interval_minutes_
;
583 base::Histogram
* histogram_evicted_last_access_minutes_
;
584 base::Histogram
* histogram_count_
;
585 base::Histogram
* histogram_domain_count_
;
586 base::Histogram
* histogram_etldp1_count_
;
587 base::Histogram
* histogram_domain_per_etldp1_count_
;
588 base::Histogram
* histogram_number_duplicate_db_cookies_
;
589 base::Histogram
* histogram_cookie_deletion_cause_
;
590 base::Histogram
* histogram_time_get_
;
591 base::Histogram
* histogram_time_mac_
;
592 base::Histogram
* histogram_time_blocked_on_load_
;
596 // Indicates whether the cookie store has been initialized. This happens
597 // lazily in InitStoreIfNecessary().
600 // Indicates whether loading from the backend store is completed and
601 // calls may be immediately processed.
604 // List of domain keys that have been loaded from the DB.
605 std::set
<std::string
> keys_loaded_
;
607 // Map of domain keys to their associated task queues. These tasks are blocked
608 // until all cookies for the associated domain key eTLD+1 are loaded from the
610 std::map
<std::string
, std::deque
<scoped_refptr
<CookieMonsterTask
> > >
613 // Queues tasks that are blocked until all cookies are loaded from the backend
615 std::queue
<scoped_refptr
<CookieMonsterTask
> > queue_
;
617 scoped_refptr
<PersistentCookieStore
> store_
;
619 base::Time last_time_seen_
;
621 // Minimum delay after updating a cookie's LastAccessDate before we will
623 const base::TimeDelta last_access_threshold_
;
625 // Approximate date of access time of least recently accessed cookie
626 // in |cookies_|. Note that this is not guaranteed to be accurate, only a)
627 // to be before or equal to the actual time, and b) to be accurate
628 // immediately after a garbage collection that scans through all the cookies.
629 // This value is used to determine whether global garbage collection might
630 // find cookies to purge.
631 // Note: The default Time() constructor will create a value that compares
632 // earlier than any other time value, which is wanted. Thus this
633 // value is not initialized.
634 base::Time earliest_access_time_
;
636 // During loading, holds the set of all loaded cookie creation times. Used to
637 // avoid ever letting cookies with duplicate creation times into the store;
638 // that way we don't have to worry about what sections of code are safe
639 // to call while it's in that state.
640 std::set
<int64
> creation_times_
;
642 std::vector
<std::string
> cookieable_schemes_
;
644 scoped_refptr
<Delegate
> delegate_
;
646 // Lock for thread-safety
649 base::Time last_statistic_record_time_
;
651 bool keep_expired_cookies_
;
652 bool persist_session_cookies_
;
654 static bool enable_file_scheme_
;
656 DISALLOW_COPY_AND_ASSIGN(CookieMonster
);
659 class NET_EXPORT
CookieMonster::CanonicalCookie
{
662 // These constructors do no validation or canonicalization of their inputs;
663 // the resulting CanonicalCookies should not be relied on to be canonical
664 // unless the caller has done appropriate validation and canonicalization
667 CanonicalCookie(const GURL
& url
,
668 const std::string
& name
,
669 const std::string
& value
,
670 const std::string
& domain
,
671 const std::string
& path
,
672 const std::string
& mac_key
,
673 const std::string
& mac_algorithm
,
674 const base::Time
& creation
,
675 const base::Time
& expiration
,
676 const base::Time
& last_access
,
682 // This constructor does canonicalization but not validation.
683 // The result of this constructor should not be relied on in contexts
684 // in which pre-validation of the ParsedCookie has not been done.
685 CanonicalCookie(const GURL
& url
, const ParsedCookie
& pc
);
689 // Supports the default copy constructor.
691 // Creates a canonical cookie from parsed cookie.
692 // Canonicalizes and validates inputs. May return NULL if an attribute
694 static CanonicalCookie
* Create(const GURL
& url
,
695 const ParsedCookie
& pc
);
697 // Creates a canonical cookie from unparsed attribute values.
698 // Canonicalizes and validates inputs. May return NULL if an attribute
700 static CanonicalCookie
* Create(const GURL
& url
,
701 const std::string
& name
,
702 const std::string
& value
,
703 const std::string
& domain
,
704 const std::string
& path
,
705 const std::string
& mac_key
,
706 const std::string
& mac_algorithm
,
707 const base::Time
& creation
,
708 const base::Time
& expiration
,
713 const std::string
& Source() const { return source_
; }
714 const std::string
& Name() const { return name_
; }
715 const std::string
& Value() const { return value_
; }
716 const std::string
& Domain() const { return domain_
; }
717 const std::string
& Path() const { return path_
; }
718 const std::string
& MACKey() const { return mac_key_
; }
719 const std::string
& MACAlgorithm() const { return mac_algorithm_
; }
720 const base::Time
& CreationDate() const { return creation_date_
; }
721 const base::Time
& LastAccessDate() const { return last_access_date_
; }
722 bool DoesExpire() const { return has_expires_
; }
723 bool IsPersistent() const { return is_persistent_
; }
724 const base::Time
& ExpiryDate() const { return expiry_date_
; }
725 bool IsSecure() const { return secure_
; }
726 bool IsHttpOnly() const { return httponly_
; }
727 bool IsDomainCookie() const {
728 return !domain_
.empty() && domain_
[0] == '.'; }
729 bool IsHostCookie() const { return !IsDomainCookie(); }
731 bool IsExpired(const base::Time
& current
) {
732 return has_expires_
&& current
>= expiry_date_
;
735 // Are the cookies considered equivalent in the eyes of RFC 2965.
736 // The RFC says that name must match (case-sensitive), domain must
737 // match (case insensitive), and path must match (case sensitive).
738 // For the case insensitive domain compare, we rely on the domain
739 // having been canonicalized (in
740 // GetCookieDomainWithString->CanonicalizeHost).
741 bool IsEquivalent(const CanonicalCookie
& ecc
) const {
742 // It seems like it would make sense to take secure and httponly into
743 // account, but the RFC doesn't specify this.
744 // NOTE: Keep this logic in-sync with TrimDuplicateCookiesForHost().
745 return (name_
== ecc
.Name() && domain_
== ecc
.Domain()
746 && path_
== ecc
.Path());
749 void SetLastAccessDate(const base::Time
& date
) {
750 last_access_date_
= date
;
753 bool IsOnPath(const std::string
& url_path
) const;
754 bool IsDomainMatch(const std::string
& scheme
, const std::string
& host
) const;
756 std::string
DebugString() const;
758 // Returns the cookie source when cookies are set for |url|. This function
759 // is public for unit test purposes only.
760 static std::string
GetCookieSourceFromURL(const GURL
& url
);
763 // Gives the session cookie an expiration time if needed
764 void SetSessionCookieExpiryTime();
766 // The source member of a canonical cookie is the origin of the URL that tried
767 // to set this cookie, minus the port number if any. This field is not
768 // persistent though; its only used in the in-tab cookies dialog to show the
769 // user the source URL. This is used for both allowed and blocked cookies.
770 // When a CanonicalCookie is constructed from the backing store (common case)
771 // this field will be null. CanonicalCookie consumers should not rely on
772 // this field unless they guarantee that the creator of those
773 // CanonicalCookies properly initialized the field.
774 // TODO(abarth): We might need to make this field persistent for MAC cookies.
780 std::string mac_key_
; // TODO(abarth): Persist to disk.
781 std::string mac_algorithm_
; // TODO(abarth): Persist to disk.
782 base::Time creation_date_
;
783 base::Time expiry_date_
;
784 base::Time last_access_date_
;
791 class CookieMonster::Delegate
792 : public base::RefCountedThreadSafe
<CookieMonster::Delegate
> {
794 // The publicly relevant reasons a cookie might be changed.
796 // The cookie was changed directly by a consumer's action.
797 CHANGE_COOKIE_EXPLICIT
,
798 // The cookie was automatically removed due to an insert operation that
800 CHANGE_COOKIE_OVERWRITE
,
801 // The cookie was automatically removed as it expired.
802 CHANGE_COOKIE_EXPIRED
,
803 // The cookie was automatically evicted during garbage collection.
804 CHANGE_COOKIE_EVICTED
,
805 // The cookie was overwritten with an already-expired expiration date.
806 CHANGE_COOKIE_EXPIRED_OVERWRITE
809 // Will be called when a cookie is added or removed. The function is passed
810 // the respective |cookie| which was added to or removed from the cookies.
811 // If |removed| is true, the cookie was deleted, and |cause| will be set
812 // to the reason for its removal. If |removed| is false, the cookie was
813 // added, and |cause| will be set to CHANGE_COOKIE_EXPLICIT.
815 // As a special case, note that updating a cookie's properties is implemented
816 // as a two step process: the cookie to be updated is first removed entirely,
817 // generating a notification with cause CHANGE_COOKIE_OVERWRITE. Afterwards,
818 // a new cookie is written with the updated values, generating a notification
819 // with cause CHANGE_COOKIE_EXPLICIT.
820 virtual void OnCookieChanged(const CookieMonster::CanonicalCookie
& cookie
,
822 ChangeCause cause
) = 0;
824 friend class base::RefCountedThreadSafe
<CookieMonster::Delegate
>;
825 virtual ~Delegate() {}
828 class NET_EXPORT
CookieMonster::ParsedCookie
{
830 typedef std::pair
<std::string
, std::string
> TokenValuePair
;
831 typedef std::vector
<TokenValuePair
> PairList
;
833 // The maximum length of a cookie string we will try to parse
834 static const size_t kMaxCookieSize
= 4096;
835 // The maximum number of Token/Value pairs. Shouldn't have more than 8.
836 static const int kMaxPairs
= 16;
838 // Construct from a cookie string like "BLAH=1; path=/; domain=.google.com"
839 ParsedCookie(const std::string
& cookie_line
);
842 // You should not call any other methods on the class if !IsValid
843 bool IsValid() const { return is_valid_
; }
845 const std::string
& Name() const { return pairs_
[0].first
; }
846 const std::string
& Token() const { return Name(); }
847 const std::string
& Value() const { return pairs_
[0].second
; }
849 bool HasPath() const { return path_index_
!= 0; }
850 const std::string
& Path() const { return pairs_
[path_index_
].second
; }
851 bool HasDomain() const { return domain_index_
!= 0; }
852 const std::string
& Domain() const { return pairs_
[domain_index_
].second
; }
853 bool HasMACKey() const { return mac_key_index_
!= 0; }
854 const std::string
& MACKey() const { return pairs_
[mac_key_index_
].second
; }
855 bool HasMACAlgorithm() const { return mac_algorithm_index_
!= 0; }
856 const std::string
& MACAlgorithm() const {
857 return pairs_
[mac_algorithm_index_
].second
;
859 bool HasExpires() const { return expires_index_
!= 0; }
860 const std::string
& Expires() const { return pairs_
[expires_index_
].second
; }
861 bool HasMaxAge() const { return maxage_index_
!= 0; }
862 const std::string
& MaxAge() const { return pairs_
[maxage_index_
].second
; }
863 bool IsSecure() const { return secure_index_
!= 0; }
864 bool IsHttpOnly() const { return httponly_index_
!= 0; }
866 // Returns the number of attributes, for example, returning 2 for:
867 // "BLAH=hah; path=/; domain=.google.com"
868 size_t NumberOfAttributes() const { return pairs_
.size() - 1; }
870 // For debugging only!
871 std::string
DebugString() const;
873 // Returns an iterator pointing to the first terminator character found in
875 static std::string::const_iterator
FindFirstTerminator(const std::string
& s
);
877 // Given iterators pointing to the beginning and end of a string segment,
878 // returns as output arguments token_start and token_end to the start and end
879 // positions of a cookie attribute token name parsed from the segment, and
880 // updates the segment iterator to point to the next segment to be parsed.
881 // If no token is found, the function returns false.
882 static bool ParseToken(std::string::const_iterator
* it
,
883 const std::string::const_iterator
& end
,
884 std::string::const_iterator
* token_start
,
885 std::string::const_iterator
* token_end
);
887 // Given iterators pointing to the beginning and end of a string segment,
888 // returns as output arguments value_start and value_end to the start and end
889 // positions of a cookie attribute value parsed from the segment, and updates
890 // the segment iterator to point to the next segment to be parsed.
891 static void ParseValue(std::string::const_iterator
* it
,
892 const std::string::const_iterator
& end
,
893 std::string::const_iterator
* value_start
,
894 std::string::const_iterator
* value_end
);
896 // Same as the above functions, except the input is assumed to contain the
897 // desired token/value and nothing else.
898 static std::string
ParseTokenString(const std::string
& token
);
899 static std::string
ParseValueString(const std::string
& value
);
902 static const char kTerminator
[];
903 static const int kTerminatorLen
;
904 static const char kWhitespace
[];
905 static const char kValueSeparator
[];
906 static const char kTokenSeparator
[];
908 void ParseTokenValuePairs(const std::string
& cookie_line
);
909 void SetupAttributes();
913 // These will default to 0, but that should never be valid since the
914 // 0th index is the user supplied token/value, not an attribute.
915 // We're really never going to have more than like 8 attributes, so we
916 // could fit these into 3 bits each if we're worried about size...
918 size_t domain_index_
;
919 size_t mac_key_index_
;
920 size_t mac_algorithm_index_
;
921 size_t expires_index_
;
922 size_t maxage_index_
;
923 size_t secure_index_
;
924 size_t httponly_index_
;
926 DISALLOW_COPY_AND_ASSIGN(ParsedCookie
);
929 typedef base::RefCountedThreadSafe
<CookieMonster::PersistentCookieStore
>
930 RefcountedPersistentCookieStore
;
932 class CookieMonster::PersistentCookieStore
933 : public RefcountedPersistentCookieStore
{
935 virtual ~PersistentCookieStore() {}
937 typedef base::Callback
<void(const std::vector
<
938 CookieMonster::CanonicalCookie
*>&)> LoadedCallback
;
940 // Initializes the store and retrieves the existing cookies. This will be
941 // called only once at startup. The callback will return all the cookies
942 // that are not yet returned to CookieMonster by previous priority loads.
943 virtual void Load(const LoadedCallback
& loaded_callback
) = 0;
945 // Does a priority load of all cookies for the domain key (eTLD+1). The
946 // callback will return all the cookies that are not yet returned by previous
947 // loads, which includes cookies for the requested domain key if they are not
948 // already returned, plus all cookies that are chain-loaded and not yet
949 // returned to CookieMonster.
950 virtual void LoadCookiesForKey(const std::string
& key
,
951 const LoadedCallback
& loaded_callback
) = 0;
953 virtual void AddCookie(const CanonicalCookie
& cc
) = 0;
954 virtual void UpdateCookieAccessTime(const CanonicalCookie
& cc
) = 0;
955 virtual void DeleteCookie(const CanonicalCookie
& cc
) = 0;
957 // Sets the value of the user preference whether the persistent storage
958 // must be deleted upon destruction.
959 virtual void SetClearLocalStateOnExit(bool clear_local_state
) = 0;
961 // Flushes the store and posts |callback| when complete.
962 virtual void Flush(const base::Closure
& callback
) = 0;
965 PersistentCookieStore() {}
968 DISALLOW_COPY_AND_ASSIGN(PersistentCookieStore
);
971 class CookieList
: public std::vector
<CookieMonster::CanonicalCookie
> {
976 #endif // NET_COOKIES_COOKIE_MONSTER_H_