OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Brought to you by the letter D and the number 2. | 5 // Brought to you by the letter D and the number 2. |
6 | 6 |
7 #ifndef NET_COOKIES_COOKIE_MONSTER_H_ | 7 #ifndef NET_COOKIES_COOKIE_MONSTER_H_ |
8 #define NET_COOKIES_COOKIE_MONSTER_H_ | 8 #define NET_COOKIES_COOKIE_MONSTER_H_ |
9 | 9 |
10 #include <stddef.h> | 10 #include <stddef.h> |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 // The cookie monster is the system for storing and retrieving cookies. It has | 44 // The cookie monster is the system for storing and retrieving cookies. It has |
45 // an in-memory list of all cookies, and synchronizes non-session cookies to an | 45 // an in-memory list of all cookies, and synchronizes non-session cookies to an |
46 // optional permanent storage that implements the PersistentCookieStore | 46 // optional permanent storage that implements the PersistentCookieStore |
47 // interface. | 47 // interface. |
48 // | 48 // |
49 // Tasks may be deferred if all affected cookies are not yet loaded from the | 49 // Tasks may be deferred if all affected cookies are not yet loaded from the |
50 // backing store. Otherwise, callbacks may be invoked immediately. | 50 // backing store. Otherwise, callbacks may be invoked immediately. |
51 // | 51 // |
52 // A cookie task is either pending loading of the entire cookie store, or | 52 // A cookie task is either pending loading of the entire cookie store, or |
53 // loading of cookies for a specific domain key(eTLD+1). In the former case, the | 53 // loading of cookies for a specific domain key(eTLD+1). In the former case, the |
54 // cookie task will be queued in tasks_pending_ while PersistentCookieStore | 54 // cookie callback will be queued in tasks_pending_ while PersistentCookieStore |
55 // chain loads the cookie store on DB thread. In the latter case, the cookie | 55 // chain loads the cookie store on DB thread. In the latter case, the cookie |
56 // task will be queued in tasks_pending_for_key_ while PermanentCookieStore | 56 // callback will be queued in tasks_pending_for_key_ while PermanentCookieStore |
57 // loads cookies for the specified domain key(eTLD+1) on DB thread. | 57 // loads cookies for the specified domain key(eTLD+1) on DB thread. |
58 // | 58 // |
59 // TODO(deanm) Implement CookieMonster, the cookie database. | 59 // TODO(deanm) Implement CookieMonster, the cookie database. |
60 // - Verify that our domain enforcement and non-dotted handling is correct | 60 // - Verify that our domain enforcement and non-dotted handling is correct |
61 class NET_EXPORT CookieMonster : public CookieStore { | 61 class NET_EXPORT CookieMonster : public CookieStore { |
62 public: | 62 public: |
63 class PersistentCookieStore; | 63 class PersistentCookieStore; |
64 | 64 |
65 // Terminology: | 65 // Terminology: |
66 // * The 'top level domain' (TLD) of an internet domain name is | 66 // * The 'top level domain' (TLD) of an internet domain name is |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 static const char* const kDefaultCookieableSchemes[]; | 221 static const char* const kDefaultCookieableSchemes[]; |
222 static const int kDefaultCookieableSchemesCount; | 222 static const int kDefaultCookieableSchemesCount; |
223 | 223 |
224 std::unique_ptr<CookieChangedSubscription> AddCallbackForCookie( | 224 std::unique_ptr<CookieChangedSubscription> AddCallbackForCookie( |
225 const GURL& url, | 225 const GURL& url, |
226 const std::string& name, | 226 const std::string& name, |
227 const CookieChangedCallback& callback) override; | 227 const CookieChangedCallback& callback) override; |
228 | 228 |
229 bool IsEphemeral() override; | 229 bool IsEphemeral() override; |
230 | 230 |
| 231 void SetCookieWithCreationTimeForTesting(const GURL& url, |
| 232 const std::string& cookie_line, |
| 233 const base::Time& creation_time, |
| 234 SetCookiesCallback callback); |
| 235 |
231 private: | 236 private: |
232 CookieMonster(PersistentCookieStore* store, | 237 CookieMonster(PersistentCookieStore* store, |
233 CookieMonsterDelegate* delegate, | 238 CookieMonsterDelegate* delegate, |
234 ChannelIDService* channel_id_service, | 239 ChannelIDService* channel_id_service, |
235 base::TimeDelta last_access_threshold); | 240 base::TimeDelta last_access_threshold); |
236 | 241 |
237 // For queueing the cookie monster calls. | |
238 class CookieMonsterTask; | |
239 template <typename Result> | |
240 class DeleteTask; | |
241 class DeleteAllCreatedBetweenTask; | |
242 class DeleteAllCreatedBetweenWithPredicateTask; | |
243 class DeleteCookieTask; | |
244 class DeleteCanonicalCookieTask; | |
245 class GetCookieListForURLWithOptionsTask; | |
246 class GetAllCookiesTask; | |
247 class GetCookiesWithOptionsTask; | |
248 class GetCookieListWithOptionsTask; | |
249 class SetAllCookiesTask; | |
250 class SetCookieWithDetailsTask; | |
251 class SetCookieWithOptionsTask; | |
252 class SetCanonicalCookieTask; | |
253 class DeleteSessionCookiesTask; | |
254 | |
255 // Testing support. | |
256 // For SetCookieWithCreationTime. | |
257 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, | |
258 TestCookieDeleteAllCreatedBetweenTimestamps); | |
259 FRIEND_TEST_ALL_PREFIXES( | |
260 CookieMonsterTest, | |
261 TestCookieDeleteAllCreatedBetweenTimestampsWithPredicate); | |
262 | |
263 // For garbage collection constants. | 242 // For garbage collection constants. |
264 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection); | 243 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection); |
265 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GarbageCollectionTriggers); | 244 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GarbageCollectionTriggers); |
266 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, | 245 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, |
267 GarbageCollectWithSecureCookiesOnly); | 246 GarbageCollectWithSecureCookiesOnly); |
268 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes); | 247 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes); |
269 | 248 |
270 // For validation of key values. | 249 // For validation of key values. |
271 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree); | 250 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree); |
272 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport); | 251 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport); |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 // The number of days since last access that cookies will not be subject | 389 // The number of days since last access that cookies will not be subject |
411 // to global garbage collection. | 390 // to global garbage collection. |
412 static const int kSafeFromGlobalPurgeDays; | 391 static const int kSafeFromGlobalPurgeDays; |
413 | 392 |
414 // Record statistics every kRecordStatisticsIntervalSeconds of uptime. | 393 // Record statistics every kRecordStatisticsIntervalSeconds of uptime. |
415 static const int kRecordStatisticsIntervalSeconds = 10 * 60; | 394 static const int kRecordStatisticsIntervalSeconds = 10 * 60; |
416 | 395 |
417 // The following are synchronous calls to which the asynchronous methods | 396 // The following are synchronous calls to which the asynchronous methods |
418 // delegate either immediately (if the store is loaded) or through a deferred | 397 // delegate either immediately (if the store is loaded) or through a deferred |
419 // task (if the store is not yet loaded). | 398 // task (if the store is not yet loaded). |
420 bool SetCookieWithDetails(const GURL& url, | 399 void SetCookieWithDetails(const GURL& url, |
421 const std::string& name, | 400 const std::string& name, |
422 const std::string& value, | 401 const std::string& value, |
423 const std::string& domain, | 402 const std::string& domain, |
424 const std::string& path, | 403 const std::string& path, |
425 base::Time creation_time, | 404 base::Time creation_time, |
426 base::Time expiration_time, | 405 base::Time expiration_time, |
427 base::Time last_access_time, | 406 base::Time last_access_time, |
428 bool secure, | 407 bool secure, |
429 bool http_only, | 408 bool http_only, |
430 CookieSameSite same_site, | 409 CookieSameSite same_site, |
431 CookiePriority priority); | 410 CookiePriority priority, |
| 411 SetCookiesCallback callback); |
432 | 412 |
433 // Sets a canonical cookie, deletes equivalents and performs garbage | 413 // Sets a canonical cookie, deletes equivalents and performs garbage |
434 // collection. |source_secure| indicates if the cookie is being set | 414 // collection. |source_secure| indicates if the cookie is being set |
435 // from a secure source (e.g. a cryptographic scheme). | 415 // from a secure source (e.g. a cryptographic scheme). |
436 // |modify_http_only| indicates if this setting operation is allowed | 416 // |modify_http_only| indicates if this setting operation is allowed |
437 // to affect http_only cookies. | 417 // to affect http_only cookies. |
438 bool SetCanonicalCookie(std::unique_ptr<CanonicalCookie> cookie, | 418 void SetCanonicalCookie(std::unique_ptr<CanonicalCookie> cookie, |
439 bool secure_source, | 419 bool secure_source, |
440 bool can_modify_httponly); | 420 bool can_modify_httponly, |
| 421 SetCookiesCallback callback); |
441 | 422 |
442 CookieList GetAllCookies(); | 423 void GetAllCookies(GetCookieListCallback callback); |
443 | 424 |
444 CookieList GetCookieListWithOptions(const GURL& url, | 425 void GetCookieListWithOptions(const GURL& url, |
445 const CookieOptions& options); | 426 const CookieOptions& options, |
| 427 GetCookieListCallback callback); |
446 | 428 |
447 uint32_t DeleteAllCreatedBetween(const base::Time& delete_begin, | 429 void DeleteAllCreatedBetween(const base::Time& delete_begin, |
448 const base::Time& delete_end); | 430 const base::Time& delete_end, |
| 431 DeleteCallback callback); |
449 | 432 |
450 // Predicate will be called with the calling thread. | 433 // Predicate will be called with the calling thread. |
451 uint32_t DeleteAllCreatedBetweenWithPredicate( | 434 void DeleteAllCreatedBetweenWithPredicate( |
452 const base::Time& delete_begin, | 435 const base::Time& delete_begin, |
453 const base::Time& delete_end, | 436 const base::Time& delete_end, |
454 const base::Callback<bool(const CanonicalCookie&)>& predicate); | 437 const base::Callback<bool(const CanonicalCookie&)>& predicate, |
| 438 DeleteCallback callback); |
455 | 439 |
456 bool SetCookieWithOptions(const GURL& url, | 440 void SetCookieWithOptions(const GURL& url, |
457 const std::string& cookie_line, | 441 const std::string& cookie_line, |
458 const CookieOptions& options); | 442 const CookieOptions& options, |
| 443 SetCookiesCallback callback); |
459 | 444 |
460 std::string GetCookiesWithOptions(const GURL& url, | 445 void GetCookiesWithOptions(const GURL& url, |
461 const CookieOptions& options); | 446 const CookieOptions& options, |
| 447 GetCookiesCallback callback); |
462 | 448 |
463 void DeleteCookie(const GURL& url, const std::string& cookie_name); | 449 void DeleteCookie(const GURL& url, |
| 450 const std::string& cookie_name, |
| 451 base::OnceClosure callback); |
464 | 452 |
465 uint32_t DeleteCanonicalCookie(const CanonicalCookie& cookie); | 453 void DeleteCanonicalCookie(const CanonicalCookie& cookie, |
| 454 DeleteCallback callback); |
466 | 455 |
467 bool SetCookieWithCreationTime(const GURL& url, | 456 void DeleteSessionCookies(DeleteCallback callback); |
468 const std::string& cookie_line, | |
469 const base::Time& creation_time); | |
470 | |
471 uint32_t DeleteSessionCookies(); | |
472 | 457 |
473 // The first access to the cookie store initializes it. This method should be | 458 // The first access to the cookie store initializes it. This method should be |
474 // called before any access to the cookie store. | 459 // called before any access to the cookie store. |
475 void MarkCookieStoreAsInitialized(); | 460 void MarkCookieStoreAsInitialized(); |
476 | 461 |
477 // Fetches all cookies if the backing store exists and they're not already | 462 // Fetches all cookies if the backing store exists and they're not already |
478 // being fetched. | 463 // being fetched. |
479 void FetchAllCookiesIfNecessary(); | 464 void FetchAllCookiesIfNecessary(); |
480 | 465 |
481 // Fetches all cookies from the backing store. | 466 // Fetches all cookies from the backing store. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 | 530 |
546 // Inserts |cc| into cookies_. Returns an iterator that points to the inserted | 531 // Inserts |cc| into cookies_. Returns an iterator that points to the inserted |
547 // cookie in cookies_. Guarantee: all iterators to cookies_ remain valid. | 532 // cookie in cookies_. Guarantee: all iterators to cookies_ remain valid. |
548 CookieMap::iterator InternalInsertCookie(const std::string& key, | 533 CookieMap::iterator InternalInsertCookie(const std::string& key, |
549 std::unique_ptr<CanonicalCookie> cc, | 534 std::unique_ptr<CanonicalCookie> cc, |
550 bool sync_to_store); | 535 bool sync_to_store); |
551 | 536 |
552 // Helper function that sets cookies with more control. | 537 // Helper function that sets cookies with more control. |
553 // Not exposed as we don't want callers to have the ability | 538 // Not exposed as we don't want callers to have the ability |
554 // to specify (potentially duplicate) creation times. | 539 // to specify (potentially duplicate) creation times. |
555 bool SetCookieWithCreationTimeAndOptions(const GURL& url, | 540 void SetCookieWithCreationTimeAndOptions(const GURL& url, |
556 const std::string& cookie_line, | 541 const std::string& cookie_line, |
557 const base::Time& creation_time, | 542 const base::Time& creation_time, |
558 const CookieOptions& options); | 543 const CookieOptions& options, |
| 544 SetCookiesCallback callback); |
559 | 545 |
560 // Sets all cookies from |list| after deleting any equivalent cookie. | 546 // Sets all cookies from |list| after deleting any equivalent cookie. |
561 // For data gathering purposes, this routine is treated as if it is | 547 // For data gathering purposes, this routine is treated as if it is |
562 // restoring saved cookies; some statistics are not gathered in this case. | 548 // restoring saved cookies; some statistics are not gathered in this case. |
563 bool SetAllCookies(const CookieList& list); | 549 void SetAllCookies(CookieList list, SetCookiesCallback callback); |
564 | 550 |
565 void InternalUpdateCookieAccessTime(CanonicalCookie* cc, | 551 void InternalUpdateCookieAccessTime(CanonicalCookie* cc, |
566 const base::Time& current_time); | 552 const base::Time& current_time); |
567 | 553 |
568 // |deletion_cause| argument is used for collecting statistics and choosing | 554 // |deletion_cause| argument is used for collecting statistics and choosing |
569 // the correct CookieStore::ChangeCause for OnCookieChanged | 555 // the correct CookieStore::ChangeCause for OnCookieChanged |
570 // notifications. Guarantee: All iterators to cookies_ except to the | 556 // notifications. Guarantee: All iterators to cookies_ except to the |
571 // deleted entry remain valid. | 557 // deleted entry remain valid. |
572 void InternalDeleteCookie(CookieMap::iterator it, | 558 void InternalDeleteCookie(CookieMap::iterator it, |
573 bool sync_to_store, | 559 bool sync_to_store, |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
637 void RecordPeriodicStats(const base::Time& current_time); | 623 void RecordPeriodicStats(const base::Time& current_time); |
638 | 624 |
639 // Initialize the above variables; should only be called from | 625 // Initialize the above variables; should only be called from |
640 // the constructor. | 626 // the constructor. |
641 void InitializeHistograms(); | 627 void InitializeHistograms(); |
642 | 628 |
643 // The resolution of our time isn't enough, so we do something | 629 // The resolution of our time isn't enough, so we do something |
644 // ugly and increment when we've seen the same time twice. | 630 // ugly and increment when we've seen the same time twice. |
645 base::Time CurrentTime(); | 631 base::Time CurrentTime(); |
646 | 632 |
647 // Runs the task if, or defers the task until, the full cookie database is | 633 // Runs the callback if, or defers the callback until, the full cookie |
648 // loaded. | 634 // database is loaded. |
649 void DoCookieTask(const scoped_refptr<CookieMonsterTask>& task_item); | 635 void DoCookieCallback(base::OnceClosure callback); |
650 | 636 |
651 // Runs the task if, or defers the task until, the cookies for the given URL | 637 // Runs the callback if, or defers the callback until, the cookies for the |
652 // are loaded. | 638 // given URL are loaded. |
653 void DoCookieTaskForURL(const scoped_refptr<CookieMonsterTask>& task_item, | 639 void DoCookieCallbackForURL(base::OnceClosure callback, const GURL& url); |
654 const GURL& url); | |
655 | 640 |
656 // Computes the difference between |old_cookies| and |new_cookies|, and writes | 641 // Computes the difference between |old_cookies| and |new_cookies|, and writes |
657 // the result in |cookies_to_add| and |cookies_to_delete|. | 642 // the result in |cookies_to_add| and |cookies_to_delete|. |
658 // This function has the side effect of changing the order of |old_cookies| | 643 // This function has the side effect of changing the order of |old_cookies| |
659 // and |new_cookies|. |cookies_to_add| and |cookies_to_delete| must be empty, | 644 // and |new_cookies|. |cookies_to_add| and |cookies_to_delete| must be empty, |
660 // and none of the arguments can be null. | 645 // and none of the arguments can be null. |
661 void ComputeCookieDiff(CookieList* old_cookies, | 646 void ComputeCookieDiff(CookieList* old_cookies, |
662 CookieList* new_cookies, | 647 CookieList* new_cookies, |
663 CookieList* cookies_to_add, | 648 CookieList* cookies_to_add, |
664 CookieList* cookies_to_delete); | 649 CookieList* cookies_to_delete); |
665 | 650 |
666 // Runs the given callback. Used to avoid running callbacks after the store | |
667 // has been destroyed. | |
668 void RunCallback(base::OnceClosure callback); | |
669 | |
670 // Run all cookie changed callbacks that are monitoring |cookie|. | 651 // Run all cookie changed callbacks that are monitoring |cookie|. |
671 // |removed| is true if the cookie was deleted. | 652 // |removed| is true if the cookie was deleted. |
672 void RunCookieChangedCallbacks(const CanonicalCookie& cookie, | 653 void RunCookieChangedCallbacks(const CanonicalCookie& cookie, |
673 CookieStore::ChangeCause cause); | 654 CookieStore::ChangeCause cause); |
674 | 655 |
675 // Histogram variables; see CookieMonster::InitializeHistograms() in | 656 // Histogram variables; see CookieMonster::InitializeHistograms() in |
676 // cookie_monster.cc for details. | 657 // cookie_monster.cc for details. |
677 base::HistogramBase* histogram_expiration_duration_minutes_; | 658 base::HistogramBase* histogram_expiration_duration_minutes_; |
678 base::HistogramBase* histogram_count_; | 659 base::HistogramBase* histogram_count_; |
679 base::HistogramBase* histogram_cookie_type_; | 660 base::HistogramBase* histogram_cookie_type_; |
(...skipping 12 matching lines...) Expand all Loading... |
692 bool finished_fetching_all_cookies_; | 673 bool finished_fetching_all_cookies_; |
693 // The strategy to use for fetching cookies. | 674 // The strategy to use for fetching cookies. |
694 FetchStrategy fetch_strategy_; | 675 FetchStrategy fetch_strategy_; |
695 | 676 |
696 // List of domain keys that have been loaded from the DB. | 677 // List of domain keys that have been loaded from the DB. |
697 std::set<std::string> keys_loaded_; | 678 std::set<std::string> keys_loaded_; |
698 | 679 |
699 // Map of domain keys to their associated task queues. These tasks are blocked | 680 // Map of domain keys to their associated task queues. These tasks are blocked |
700 // until all cookies for the associated domain key eTLD+1 are loaded from the | 681 // until all cookies for the associated domain key eTLD+1 are loaded from the |
701 // backend store. | 682 // backend store. |
702 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask>>> | 683 std::map<std::string, std::deque<base::OnceClosure>> tasks_pending_for_key_; |
703 tasks_pending_for_key_; | |
704 | 684 |
705 // Queues tasks that are blocked until all cookies are loaded from the backend | 685 // Queues tasks that are blocked until all cookies are loaded from the backend |
706 // store. | 686 // store. |
707 std::deque<scoped_refptr<CookieMonsterTask>> tasks_pending_; | 687 std::deque<base::OnceClosure> tasks_pending_; |
708 | 688 |
709 // Once a global cookie task has been seen, all per-key tasks must be put in | 689 // Once a global cookie task has been seen, all per-key tasks must be put in |
710 // |tasks_pending_| instead of |tasks_pending_for_key_| to ensure a reasonable | 690 // |tasks_pending_| instead of |tasks_pending_for_key_| to ensure a reasonable |
711 // view of the cookie store. This more to ensure fancy cookie export/import | 691 // view of the cookie store. This is more to ensure fancy cookie export/import |
712 // code has a consistent view of the CookieStore, rather than out of concern | 692 // code has a consistent view of the CookieStore, rather than out of concern |
713 // for typical use. | 693 // for typical use. |
714 bool seen_global_task_; | 694 bool seen_global_task_; |
715 | 695 |
716 scoped_refptr<PersistentCookieStore> store_; | 696 scoped_refptr<PersistentCookieStore> store_; |
717 | 697 |
718 base::Time last_time_seen_; | 698 base::Time last_time_seen_; |
719 | 699 |
720 // Minimum delay after updating a cookie's LastAccessDate before we will | 700 // Minimum delay after updating a cookie's LastAccessDate before we will |
721 // update it again. | 701 // update it again. |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
828 virtual ~PersistentCookieStore() {} | 808 virtual ~PersistentCookieStore() {} |
829 | 809 |
830 private: | 810 private: |
831 friend class base::RefCountedThreadSafe<PersistentCookieStore>; | 811 friend class base::RefCountedThreadSafe<PersistentCookieStore>; |
832 DISALLOW_COPY_AND_ASSIGN(PersistentCookieStore); | 812 DISALLOW_COPY_AND_ASSIGN(PersistentCookieStore); |
833 }; | 813 }; |
834 | 814 |
835 } // namespace net | 815 } // namespace net |
836 | 816 |
837 #endif // NET_COOKIES_COOKIE_MONSTER_H_ | 817 #endif // NET_COOKIES_COOKIE_MONSTER_H_ |
OLD | NEW |