Roll src/third_party/skia e1a828c:3e3b58d
[chromium-blink-merge.git] / net / cookies / cookie_monster.h
blob0f00490ce7d76968ffd92241a32fa47654ef0183
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_
10 #include <deque>
11 #include <map>
12 #include <queue>
13 #include <set>
14 #include <string>
15 #include <utility>
16 #include <vector>
18 #include "base/basictypes.h"
19 #include "base/callback_forward.h"
20 #include "base/gtest_prod_util.h"
21 #include "base/memory/linked_ptr.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/time.h"
26 #include "net/base/net_export.h"
27 #include "net/cookies/canonical_cookie.h"
28 #include "net/cookies/cookie_constants.h"
29 #include "net/cookies/cookie_store.h"
30 #include "url/gurl.h"
32 namespace base {
33 class Histogram;
34 class HistogramBase;
35 class TimeTicks;
36 } // namespace base
38 namespace net {
40 class CookieMonsterDelegate;
41 class ParsedCookie;
43 // The cookie monster is the system for storing and retrieving cookies. It has
44 // an in-memory list of all cookies, and synchronizes non-session cookies to an
45 // optional permanent storage that implements the PersistentCookieStore
46 // interface.
48 // This class IS thread-safe. Normally, it is only used on the I/O thread, but
49 // is also accessed directly through Automation for UI testing.
51 // All cookie tasks are handled asynchronously. Tasks may be deferred if
52 // all affected cookies are not yet loaded from the backing store. Otherwise,
53 // the callback may be invoked immediately (prior to return of the asynchronous
54 // function).
56 // A cookie task is either pending loading of the entire cookie store, or
57 // loading of cookies for a specfic domain key(eTLD+1). In the former case, the
58 // cookie task will be queued in tasks_pending_ while PersistentCookieStore
59 // chain loads the cookie store on DB thread. In the latter case, the cookie
60 // task will be queued in tasks_pending_for_key_ while PermanentCookieStore
61 // loads cookies for the specified domain key(eTLD+1) on DB thread.
63 // Callbacks are guaranteed to be invoked on the calling thread.
65 // TODO(deanm) Implement CookieMonster, the cookie database.
66 // - Verify that our domain enforcement and non-dotted handling is correct
67 class NET_EXPORT CookieMonster : public CookieStore {
68 public:
69 class PersistentCookieStore;
70 typedef CookieMonsterDelegate Delegate;
72 // Terminology:
73 // * The 'top level domain' (TLD) of an internet domain name is
74 // the terminal "." free substring (e.g. "com" for google.com
75 // or world.std.com).
76 // * The 'effective top level domain' (eTLD) is the longest
77 // "." initiated terminal substring of an internet domain name
78 // that is controlled by a general domain registrar.
79 // (e.g. "co.uk" for news.bbc.co.uk).
80 // * The 'effective top level domain plus one' (eTLD+1) is the
81 // shortest "." delimited terminal substring of an internet
82 // domain name that is not controlled by a general domain
83 // registrar (e.g. "bbc.co.uk" for news.bbc.co.uk, or
84 // "google.com" for news.google.com). The general assumption
85 // is that all hosts and domains under an eTLD+1 share some
86 // administrative control.
88 // CookieMap is the central data structure of the CookieMonster. It
89 // is a map whose values are pointers to CanonicalCookie data
90 // structures (the data structures are owned by the CookieMonster
91 // and must be destroyed when removed from the map). The key is based on the
92 // effective domain of the cookies. If the domain of the cookie has an
93 // eTLD+1, that is the key for the map. If the domain of the cookie does not
94 // have an eTLD+1, the key of the map is the host the cookie applies to (it is
95 // not legal to have domain cookies without an eTLD+1). This rule
96 // excludes cookies for, e.g, ".com", ".co.uk", or ".internalnetwork".
97 // This behavior is the same as the behavior in Firefox v 3.6.10.
99 // NOTE(deanm):
100 // I benchmarked hash_multimap vs multimap. We're going to be query-heavy
101 // so it would seem like hashing would help. However they were very
102 // close, with multimap being a tiny bit faster. I think this is because
103 // our map is at max around 1000 entries, and the additional complexity
104 // for the hashing might not overcome the O(log(1000)) for querying
105 // a multimap. Also, multimap is standard, another reason to use it.
106 // TODO(rdsmith): This benchmark should be re-done now that we're allowing
107 // subtantially more entries in the map.
108 typedef std::multimap<std::string, CanonicalCookie*> CookieMap;
109 typedef std::pair<CookieMap::iterator, CookieMap::iterator> CookieMapItPair;
110 typedef std::vector<CookieMap::iterator> CookieItVector;
112 // Cookie garbage collection thresholds. Based off of the Mozilla defaults.
113 // When the number of cookies gets to k{Domain,}MaxCookies
114 // purge down to k{Domain,}MaxCookies - k{Domain,}PurgeCookies.
115 // It might seem scary to have a high purge value, but really it's not.
116 // You just make sure that you increase the max to cover the increase
117 // in purge, and we would have been purging the same number of cookies.
118 // We're just going through the garbage collection process less often.
119 // Note that the DOMAIN values are per eTLD+1; see comment for the
120 // CookieMap typedef. So, e.g., the maximum number of cookies allowed for
121 // google.com and all of its subdomains will be 150-180.
123 // Any cookies accessed more recently than kSafeFromGlobalPurgeDays will not
124 // be evicted by global garbage collection, even if we have more than
125 // kMaxCookies. This does not affect domain garbage collection.
126 static const size_t kDomainMaxCookies;
127 static const size_t kDomainPurgeCookies;
128 static const size_t kMaxCookies;
129 static const size_t kPurgeCookies;
131 // Quota for cookies with {low, medium, high} priorities within a domain.
132 static const size_t kDomainCookiesQuotaLow;
133 static const size_t kDomainCookiesQuotaMedium;
134 static const size_t kDomainCookiesQuotaHigh;
136 // The store passed in should not have had Init() called on it yet. This
137 // class will take care of initializing it. The backing store is NOT owned by
138 // this class, but it must remain valid for the duration of the cookie
139 // monster's existence. If |store| is NULL, then no backing store will be
140 // updated. If |delegate| is non-NULL, it will be notified on
141 // creation/deletion of cookies.
142 CookieMonster(PersistentCookieStore* store, CookieMonsterDelegate* delegate);
144 // Only used during unit testing.
145 CookieMonster(PersistentCookieStore* store,
146 CookieMonsterDelegate* delegate,
147 int last_access_threshold_milliseconds);
149 // Helper function that adds all cookies from |list| into this instance,
150 // overwriting any equivalent cookies.
151 bool ImportCookies(const CookieList& list);
153 typedef base::Callback<void(const CookieList& cookies)> GetCookieListCallback;
154 typedef base::Callback<void(bool success)> DeleteCookieCallback;
155 typedef base::Callback<void(bool cookies_exist)> HasCookiesForETLDP1Callback;
157 // Sets a cookie given explicit user-provided cookie attributes. The cookie
158 // name, value, domain, etc. are each provided as separate strings. This
159 // function expects each attribute to be well-formed. It will check for
160 // disallowed characters (e.g. the ';' character is disallowed within the
161 // cookie value attribute) and will return false without setting the cookie
162 // if such characters are found.
163 void SetCookieWithDetailsAsync(const GURL& url,
164 const std::string& name,
165 const std::string& value,
166 const std::string& domain,
167 const std::string& path,
168 const base::Time& expiration_time,
169 bool secure,
170 bool http_only,
171 CookiePriority priority,
172 const SetCookiesCallback& callback);
175 // Returns all the cookies, for use in management UI, etc. This does not mark
176 // the cookies as having been accessed.
177 // The returned cookies are ordered by longest path, then by earliest
178 // creation date.
179 void GetAllCookiesAsync(const GetCookieListCallback& callback);
181 // Returns all the cookies, for use in management UI, etc. Filters results
182 // using given url scheme, host / domain and path and options. This does not
183 // mark the cookies as having been accessed.
184 // The returned cookies are ordered by longest path, then earliest
185 // creation date.
186 void GetAllCookiesForURLWithOptionsAsync(
187 const GURL& url,
188 const CookieOptions& options,
189 const GetCookieListCallback& callback);
191 // Deletes all of the cookies.
192 void DeleteAllAsync(const DeleteCallback& callback);
194 // Deletes all cookies that match the host of the given URL
195 // regardless of path. This includes all http_only and secure cookies,
196 // but does not include any domain cookies that may apply to this host.
197 // Returns the number of cookies deleted.
198 void DeleteAllForHostAsync(const GURL& url,
199 const DeleteCallback& callback);
201 // Deletes one specific cookie.
202 void DeleteCanonicalCookieAsync(const CanonicalCookie& cookie,
203 const DeleteCookieCallback& callback);
205 // Checks whether for a given ETLD+1, there currently exist any cookies.
206 void HasCookiesForETLDP1Async(const std::string& etldp1,
207 const HasCookiesForETLDP1Callback& callback);
209 // Resets the list of cookieable schemes to the supplied schemes.
210 // If this this method is called, it must be called before first use of
211 // the instance (i.e. as part of the instance initialization process).
212 void SetCookieableSchemes(const char* const schemes[], size_t num_schemes);
214 // Resets the list of cookieable schemes to kDefaultCookieableSchemes with or
215 // without 'file' being included.
217 // There are some unknowns about how to correctly handle file:// cookies,
218 // and our implementation for this is not robust enough. This allows you
219 // to enable support, but it should only be used for testing. Bug 1157243.
220 void SetEnableFileScheme(bool accept);
222 // Instructs the cookie monster to not delete expired cookies. This is used
223 // in cases where the cookie monster is used as a data structure to keep
224 // arbitrary cookies.
225 void SetKeepExpiredCookies();
227 // Protects session cookies from deletion on shutdown.
228 void SetForceKeepSessionState();
230 // Flush the backing store (if any) to disk and post the given callback when
231 // done.
232 // WARNING: THE CALLBACK WILL RUN ON A RANDOM THREAD. IT MUST BE THREAD SAFE.
233 // It may be posted to the current thread, or it may run on the thread that
234 // actually does the flushing. Your Task should generally post a notification
235 // to the thread you actually want to be notified on.
236 void FlushStore(const base::Closure& callback);
238 // CookieStore implementation.
240 // Sets the cookies specified by |cookie_list| returned from |url|
241 // with options |options| in effect.
242 void SetCookieWithOptionsAsync(const GURL& url,
243 const std::string& cookie_line,
244 const CookieOptions& options,
245 const SetCookiesCallback& callback) override;
247 // Gets all cookies that apply to |url| given |options|.
248 // The returned cookies are ordered by longest path, then earliest
249 // creation date.
250 void GetCookiesWithOptionsAsync(const GURL& url,
251 const CookieOptions& options,
252 const GetCookiesCallback& callback) override;
254 // Invokes GetAllCookiesForURLWithOptions with options set to include HTTP
255 // only cookies.
256 void GetAllCookiesForURLAsync(const GURL& url,
257 const GetCookieListCallback& callback) override;
259 // Deletes all cookies with that might apply to |url| that has |cookie_name|.
260 void DeleteCookieAsync(const GURL& url,
261 const std::string& cookie_name,
262 const base::Closure& callback) override;
264 // Deletes all of the cookies that have a creation_date greater than or equal
265 // to |delete_begin| and less than |delete_end|.
266 // Returns the number of cookies that have been deleted.
267 void DeleteAllCreatedBetweenAsync(const base::Time& delete_begin,
268 const base::Time& delete_end,
269 const DeleteCallback& callback) override;
271 // Deletes all of the cookies that match the host of the given URL
272 // regardless of path and that have a creation_date greater than or
273 // equal to |delete_begin| and less then |delete_end|. This includes
274 // all http_only and secure cookies, but does not include any domain
275 // cookies that may apply to this host.
276 // Returns the number of cookies deleted.
277 void DeleteAllCreatedBetweenForHostAsync(
278 const base::Time delete_begin,
279 const base::Time delete_end,
280 const GURL& url,
281 const DeleteCallback& callback) override;
283 void DeleteSessionCookiesAsync(const DeleteCallback&) override;
285 CookieMonster* GetCookieMonster() override;
287 // Enables writing session cookies into the cookie database. If this this
288 // method is called, it must be called before first use of the instance
289 // (i.e. as part of the instance initialization process).
290 void SetPersistSessionCookies(bool persist_session_cookies);
292 // Debugging method to perform various validation checks on the map.
293 // Currently just checking that there are no null CanonicalCookie pointers
294 // in the map.
295 // Argument |arg| is to allow retaining of arbitrary data if the CHECKs
296 // in the function trip. TODO(rdsmith):Remove hack.
297 void ValidateMap(int arg);
299 // Determines if the scheme of the URL is a scheme that cookies will be
300 // stored for.
301 bool IsCookieableScheme(const std::string& scheme);
303 // The default list of schemes the cookie monster can handle.
304 static const char* const kDefaultCookieableSchemes[];
305 static const int kDefaultCookieableSchemesCount;
307 // Copies all keys for the given |key| to another cookie monster |other|.
308 // Both |other| and |this| must be loaded for this operation to succeed.
309 // Furthermore, there may not be any cookies stored in |other| for |key|.
310 // Returns false if any of these conditions is not met.
311 bool CopyCookiesForKeyToOtherCookieMonster(std::string key,
312 CookieMonster* other);
314 // Find the key (for lookup in cookies_) based on the given domain.
315 // See comment on keys before the CookieMap typedef.
316 std::string GetKey(const std::string& domain) const;
318 scoped_ptr<CookieChangedSubscription> AddCallbackForCookie(
319 const GURL& url,
320 const std::string& name,
321 const CookieChangedCallback& callback) override;
323 bool loaded();
325 private:
326 // For queueing the cookie monster calls.
327 class CookieMonsterTask;
328 template <typename Result> class DeleteTask;
329 class DeleteAllCreatedBetweenTask;
330 class DeleteAllCreatedBetweenForHostTask;
331 class DeleteAllForHostTask;
332 class DeleteAllTask;
333 class DeleteCookieTask;
334 class DeleteCanonicalCookieTask;
335 class GetAllCookiesForURLWithOptionsTask;
336 class GetAllCookiesTask;
337 class GetCookiesWithOptionsTask;
338 class SetCookieWithDetailsTask;
339 class SetCookieWithOptionsTask;
340 class DeleteSessionCookiesTask;
341 class HasCookiesForETLDP1Task;
343 // Testing support.
344 // For SetCookieWithCreationTime.
345 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest,
346 TestCookieDeleteAllCreatedBetweenTimestamps);
347 // For SetCookieWithCreationTime.
348 FRIEND_TEST_ALL_PREFIXES(MultiThreadedCookieMonsterTest,
349 ThreadCheckDeleteAllCreatedBetweenForHost);
351 // For gargage collection constants.
352 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection);
353 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestTotalGarbageCollection);
354 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GarbageCollectionTriggers);
355 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes);
357 // For validation of key values.
358 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree);
359 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport);
360 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GetKey);
361 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGetKey);
363 // For FindCookiesForKey.
364 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, ShortLivedSessionCookies);
366 // Internal reasons for deletion, used to populate informative histograms
367 // and to provide a public cause for onCookieChange notifications.
369 // If you add or remove causes from this list, please be sure to also update
370 // the CookieMonsterDelegate::ChangeCause mapping inside ChangeCauseMapping.
371 // Moreover, these are used as array indexes, so avoid reordering to keep the
372 // histogram buckets consistent. New items (if necessary) should be added
373 // at the end of the list, just before DELETE_COOKIE_LAST_ENTRY.
374 enum DeletionCause {
375 DELETE_COOKIE_EXPLICIT = 0,
376 DELETE_COOKIE_OVERWRITE,
377 DELETE_COOKIE_EXPIRED,
378 DELETE_COOKIE_EVICTED,
379 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE,
380 DELETE_COOKIE_DONT_RECORD, // e.g. For final cleanup after flush to store.
381 DELETE_COOKIE_EVICTED_DOMAIN,
382 DELETE_COOKIE_EVICTED_GLOBAL,
384 // Cookies evicted during domain level garbage collection that
385 // were accessed longer ago than kSafeFromGlobalPurgeDays
386 DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE,
388 // Cookies evicted during domain level garbage collection that
389 // were accessed more recently than kSafeFromGlobalPurgeDays
390 // (and thus would have been preserved by global garbage collection).
391 DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE,
393 // A common idiom is to remove a cookie by overwriting it with an
394 // already-expired expiration date. This captures that case.
395 DELETE_COOKIE_EXPIRED_OVERWRITE,
397 // Cookies are not allowed to contain control characters in the name or
398 // value. However, we used to allow them, so we are now evicting any such
399 // cookies as we load them. See http://crbug.com/238041.
400 DELETE_COOKIE_CONTROL_CHAR,
402 DELETE_COOKIE_LAST_ENTRY
405 // The number of days since last access that cookies will not be subject
406 // to global garbage collection.
407 static const int kSafeFromGlobalPurgeDays;
409 // Record statistics every kRecordStatisticsIntervalSeconds of uptime.
410 static const int kRecordStatisticsIntervalSeconds = 10 * 60;
412 ~CookieMonster() override;
414 // The following are synchronous calls to which the asynchronous methods
415 // delegate either immediately (if the store is loaded) or through a deferred
416 // task (if the store is not yet loaded).
417 bool SetCookieWithDetails(const GURL& url,
418 const std::string& name,
419 const std::string& value,
420 const std::string& domain,
421 const std::string& path,
422 const base::Time& expiration_time,
423 bool secure,
424 bool http_only,
425 CookiePriority priority);
427 CookieList GetAllCookies();
429 CookieList GetAllCookiesForURLWithOptions(const GURL& url,
430 const CookieOptions& options);
432 CookieList GetAllCookiesForURL(const GURL& url);
434 int DeleteAll(bool sync_to_store);
436 int DeleteAllCreatedBetween(const base::Time& delete_begin,
437 const base::Time& delete_end);
439 int DeleteAllForHost(const GURL& url);
440 int DeleteAllCreatedBetweenForHost(const base::Time delete_begin,
441 const base::Time delete_end,
442 const GURL& url);
444 bool DeleteCanonicalCookie(const CanonicalCookie& cookie);
446 bool SetCookieWithOptions(const GURL& url,
447 const std::string& cookie_line,
448 const CookieOptions& options);
450 std::string GetCookiesWithOptions(const GURL& url,
451 const CookieOptions& options);
453 void DeleteCookie(const GURL& url, const std::string& cookie_name);
455 bool SetCookieWithCreationTime(const GURL& url,
456 const std::string& cookie_line,
457 const base::Time& creation_time);
459 int DeleteSessionCookies();
461 bool HasCookiesForETLDP1(const std::string& etldp1);
463 // Called by all non-static functions to ensure that the cookies store has
464 // been initialized. This is not done during creating so it doesn't block
465 // the window showing.
466 // Note: this method should always be called with lock_ held.
467 void InitIfNecessary() {
468 if (!initialized_) {
469 if (store_.get()) {
470 InitStore();
471 } else {
472 loaded_ = true;
473 ReportLoaded();
475 initialized_ = true;
479 // Initializes the backing store and reads existing cookies from it.
480 // Should only be called by InitIfNecessary().
481 void InitStore();
483 // Reports to the delegate that the cookie monster was loaded.
484 void ReportLoaded();
486 // Stores cookies loaded from the backing store and invokes any deferred
487 // calls. |beginning_time| should be the moment PersistentCookieStore::Load
488 // was invoked and is used for reporting histogram_time_blocked_on_load_.
489 // See PersistentCookieStore::Load for details on the contents of cookies.
490 void OnLoaded(base::TimeTicks beginning_time,
491 const std::vector<CanonicalCookie*>& cookies);
493 // Stores cookies loaded from the backing store and invokes the deferred
494 // task(s) pending loading of cookies associated with the domain key
495 // (eTLD+1). Called when all cookies for the domain key(eTLD+1) have been
496 // loaded from DB. See PersistentCookieStore::Load for details on the contents
497 // of cookies.
498 void OnKeyLoaded(
499 const std::string& key,
500 const std::vector<CanonicalCookie*>& cookies);
502 // Stores the loaded cookies.
503 void StoreLoadedCookies(const std::vector<CanonicalCookie*>& cookies);
505 // Invokes deferred calls.
506 void InvokeQueue();
508 // Checks that |cookies_| matches our invariants, and tries to repair any
509 // inconsistencies. (In other words, it does not have duplicate cookies).
510 void EnsureCookiesMapIsValid();
512 // Checks for any duplicate cookies for CookieMap key |key| which lie between
513 // |begin| and |end|. If any are found, all but the most recent are deleted.
514 // Returns the number of duplicate cookies that were deleted.
515 int TrimDuplicateCookiesForKey(const std::string& key,
516 CookieMap::iterator begin,
517 CookieMap::iterator end);
519 void SetDefaultCookieableSchemes();
521 void FindCookiesForHostAndDomain(const GURL& url,
522 const CookieOptions& options,
523 bool update_access_time,
524 std::vector<CanonicalCookie*>* cookies);
526 void FindCookiesForKey(const std::string& key,
527 const GURL& url,
528 const CookieOptions& options,
529 const base::Time& current,
530 bool update_access_time,
531 std::vector<CanonicalCookie*>* cookies);
533 // Delete any cookies that are equivalent to |ecc| (same path, domain, etc).
534 // If |skip_httponly| is true, httponly cookies will not be deleted. The
535 // return value with be true if |skip_httponly| skipped an httponly cookie.
536 // |key| is the key to find the cookie in cookies_; see the comment before
537 // the CookieMap typedef for details.
538 // NOTE: There should never be more than a single matching equivalent cookie.
539 bool DeleteAnyEquivalentCookie(const std::string& key,
540 const CanonicalCookie& ecc,
541 bool skip_httponly,
542 bool already_expired);
544 // Takes ownership of *cc. Returns an iterator that points to the inserted
545 // cookie in cookies_. Guarantee: all iterators to cookies_ remain valid.
546 CookieMap::iterator InternalInsertCookie(const std::string& key,
547 CanonicalCookie* cc,
548 bool sync_to_store);
550 // Helper function that sets cookies with more control.
551 // Not exposed as we don't want callers to have the ability
552 // to specify (potentially duplicate) creation times.
553 bool SetCookieWithCreationTimeAndOptions(const GURL& url,
554 const std::string& cookie_line,
555 const base::Time& creation_time,
556 const CookieOptions& options);
558 // Helper function that sets a canonical cookie, deleting equivalents and
559 // performing garbage collection.
560 bool SetCanonicalCookie(scoped_ptr<CanonicalCookie>* cc,
561 const base::Time& creation_time,
562 const CookieOptions& options);
564 void InternalUpdateCookieAccessTime(CanonicalCookie* cc,
565 const base::Time& current_time);
567 // |deletion_cause| argument is used for collecting statistics and choosing
568 // the correct CookieMonsterDelegate::ChangeCause for OnCookieChanged
569 // notifications. Guarantee: All iterators to cookies_ except to the
570 // deleted entry remain vaild.
571 void InternalDeleteCookie(CookieMap::iterator it, bool sync_to_store,
572 DeletionCause deletion_cause);
574 // If the number of cookies for CookieMap key |key|, or globally, are
575 // over the preset maximums above, garbage collect, first for the host and
576 // then globally. See comments above garbage collection threshold
577 // constants for details.
579 // Returns the number of cookies deleted (useful for debugging).
580 int GarbageCollect(const base::Time& current, const std::string& key);
582 // Helper for GarbageCollect(); can be called directly as well. Deletes
583 // all expired cookies in |itpair|. If |cookie_its| is non-NULL, it is
584 // populated with all the non-expired cookies from |itpair|.
586 // Returns the number of cookies deleted.
587 int GarbageCollectExpired(const base::Time& current,
588 const CookieMapItPair& itpair,
589 std::vector<CookieMap::iterator>* cookie_its);
591 // Helper for GarbageCollect(). Deletes all cookies in the range specified by
592 // [|it_begin|, |it_end|). Returns the number of cookies deleted.
593 int GarbageCollectDeleteRange(const base::Time& current,
594 DeletionCause cause,
595 CookieItVector::iterator cookie_its_begin,
596 CookieItVector::iterator cookie_its_end);
598 bool HasCookieableScheme(const GURL& url);
600 // Statistics support
602 // This function should be called repeatedly, and will record
603 // statistics if a sufficient time period has passed.
604 void RecordPeriodicStats(const base::Time& current_time);
606 // Initialize the above variables; should only be called from
607 // the constructor.
608 void InitializeHistograms();
610 // The resolution of our time isn't enough, so we do something
611 // ugly and increment when we've seen the same time twice.
612 base::Time CurrentTime();
614 // Runs the task if, or defers the task until, the full cookie database is
615 // loaded.
616 void DoCookieTask(const scoped_refptr<CookieMonsterTask>& task_item);
618 // Runs the task if, or defers the task until, the cookies for the given URL
619 // are loaded.
620 void DoCookieTaskForURL(const scoped_refptr<CookieMonsterTask>& task_item,
621 const GURL& url);
623 // Run all cookie changed callbacks that are monitoring |cookie|.
624 // |removed| is true if the cookie was deleted.
625 void RunCallbacks(const CanonicalCookie& cookie, bool removed);
627 // Histogram variables; see CookieMonster::InitializeHistograms() in
628 // cookie_monster.cc for details.
629 base::HistogramBase* histogram_expiration_duration_minutes_;
630 base::HistogramBase* histogram_between_access_interval_minutes_;
631 base::HistogramBase* histogram_evicted_last_access_minutes_;
632 base::HistogramBase* histogram_count_;
633 base::HistogramBase* histogram_domain_count_;
634 base::HistogramBase* histogram_etldp1_count_;
635 base::HistogramBase* histogram_domain_per_etldp1_count_;
636 base::HistogramBase* histogram_number_duplicate_db_cookies_;
637 base::HistogramBase* histogram_cookie_deletion_cause_;
638 base::HistogramBase* histogram_time_mac_;
639 base::HistogramBase* histogram_time_blocked_on_load_;
641 CookieMap cookies_;
643 // Indicates whether the cookie store has been initialized. This happens
644 // lazily in InitStoreIfNecessary().
645 bool initialized_;
647 // Indicates whether loading from the backend store is completed and
648 // calls may be immediately processed.
649 bool loaded_;
651 // List of domain keys that have been loaded from the DB.
652 std::set<std::string> keys_loaded_;
654 // Map of domain keys to their associated task queues. These tasks are blocked
655 // until all cookies for the associated domain key eTLD+1 are loaded from the
656 // backend store.
657 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > >
658 tasks_pending_for_key_;
660 // Queues tasks that are blocked until all cookies are loaded from the backend
661 // store.
662 std::queue<scoped_refptr<CookieMonsterTask> > tasks_pending_;
664 scoped_refptr<PersistentCookieStore> store_;
666 base::Time last_time_seen_;
668 // Minimum delay after updating a cookie's LastAccessDate before we will
669 // update it again.
670 const base::TimeDelta last_access_threshold_;
672 // Approximate date of access time of least recently accessed cookie
673 // in |cookies_|. Note that this is not guaranteed to be accurate, only a)
674 // to be before or equal to the actual time, and b) to be accurate
675 // immediately after a garbage collection that scans through all the cookies.
676 // This value is used to determine whether global garbage collection might
677 // find cookies to purge.
678 // Note: The default Time() constructor will create a value that compares
679 // earlier than any other time value, which is wanted. Thus this
680 // value is not initialized.
681 base::Time earliest_access_time_;
683 // During loading, holds the set of all loaded cookie creation times. Used to
684 // avoid ever letting cookies with duplicate creation times into the store;
685 // that way we don't have to worry about what sections of code are safe
686 // to call while it's in that state.
687 std::set<int64> creation_times_;
689 std::vector<std::string> cookieable_schemes_;
691 scoped_refptr<CookieMonsterDelegate> delegate_;
693 // Lock for thread-safety
694 base::Lock lock_;
696 base::Time last_statistic_record_time_;
698 bool keep_expired_cookies_;
699 bool persist_session_cookies_;
701 // Static setting for whether or not file scheme cookies are allows when
702 // a new CookieMonster is created, or the accepted schemes on a CookieMonster
703 // instance are reset back to defaults.
704 static bool default_enable_file_scheme_;
706 typedef std::map<std::pair<GURL, std::string>,
707 linked_ptr<CookieChangedCallbackList>> CookieChangedHookMap;
708 CookieChangedHookMap hook_map_;
710 DISALLOW_COPY_AND_ASSIGN(CookieMonster);
713 class NET_EXPORT CookieMonsterDelegate
714 : public base::RefCountedThreadSafe<CookieMonsterDelegate> {
715 public:
716 // The publicly relevant reasons a cookie might be changed.
717 enum ChangeCause {
718 // The cookie was changed directly by a consumer's action.
719 CHANGE_COOKIE_EXPLICIT,
720 // The cookie was automatically removed due to an insert operation that
721 // overwrote it.
722 CHANGE_COOKIE_OVERWRITE,
723 // The cookie was automatically removed as it expired.
724 CHANGE_COOKIE_EXPIRED,
725 // The cookie was automatically evicted during garbage collection.
726 CHANGE_COOKIE_EVICTED,
727 // The cookie was overwritten with an already-expired expiration date.
728 CHANGE_COOKIE_EXPIRED_OVERWRITE
731 // Will be called when a cookie is added or removed. The function is passed
732 // the respective |cookie| which was added to or removed from the cookies.
733 // If |removed| is true, the cookie was deleted, and |cause| will be set
734 // to the reason for its removal. If |removed| is false, the cookie was
735 // added, and |cause| will be set to CHANGE_COOKIE_EXPLICIT.
737 // As a special case, note that updating a cookie's properties is implemented
738 // as a two step process: the cookie to be updated is first removed entirely,
739 // generating a notification with cause CHANGE_COOKIE_OVERWRITE. Afterwards,
740 // a new cookie is written with the updated values, generating a notification
741 // with cause CHANGE_COOKIE_EXPLICIT.
742 virtual void OnCookieChanged(const CanonicalCookie& cookie,
743 bool removed,
744 ChangeCause cause) = 0;
745 // Indicates that the cookie store has fully loaded.
746 virtual void OnLoaded() = 0;
748 protected:
749 friend class base::RefCountedThreadSafe<CookieMonsterDelegate>;
750 virtual ~CookieMonsterDelegate() {}
753 typedef base::RefCountedThreadSafe<CookieMonster::PersistentCookieStore>
754 RefcountedPersistentCookieStore;
756 class NET_EXPORT CookieMonster::PersistentCookieStore
757 : public RefcountedPersistentCookieStore {
758 public:
759 typedef base::Callback<void(const std::vector<CanonicalCookie*>&)>
760 LoadedCallback;
762 // Initializes the store and retrieves the existing cookies. This will be
763 // called only once at startup. The callback will return all the cookies
764 // that are not yet returned to CookieMonster by previous priority loads.
765 virtual void Load(const LoadedCallback& loaded_callback) = 0;
767 // Does a priority load of all cookies for the domain key (eTLD+1). The
768 // callback will return all the cookies that are not yet returned by previous
769 // loads, which includes cookies for the requested domain key if they are not
770 // already returned, plus all cookies that are chain-loaded and not yet
771 // returned to CookieMonster.
772 virtual void LoadCookiesForKey(const std::string& key,
773 const LoadedCallback& loaded_callback) = 0;
775 virtual void AddCookie(const CanonicalCookie& cc) = 0;
776 virtual void UpdateCookieAccessTime(const CanonicalCookie& cc) = 0;
777 virtual void DeleteCookie(const CanonicalCookie& cc) = 0;
779 // Instructs the store to not discard session only cookies on shutdown.
780 virtual void SetForceKeepSessionState() = 0;
782 // Flushes the store and posts |callback| when complete.
783 virtual void Flush(const base::Closure& callback) = 0;
785 protected:
786 PersistentCookieStore() {}
787 virtual ~PersistentCookieStore() {}
789 private:
790 friend class base::RefCountedThreadSafe<PersistentCookieStore>;
791 DISALLOW_COPY_AND_ASSIGN(PersistentCookieStore);
794 } // namespace net
796 #endif // NET_COOKIES_COOKIE_MONSTER_H_