Chromium Code Reviews| 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 43 | 43 |
| 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 specfic 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 task 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 // task 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; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 90 // This behavior is the same as the behavior in Firefox v 3.6.10. | 90 // This behavior is the same as the behavior in Firefox v 3.6.10. |
| 91 | 91 |
| 92 // NOTE(deanm): | 92 // NOTE(deanm): |
| 93 // I benchmarked hash_multimap vs multimap. We're going to be query-heavy | 93 // I benchmarked hash_multimap vs multimap. We're going to be query-heavy |
| 94 // so it would seem like hashing would help. However they were very | 94 // so it would seem like hashing would help. However they were very |
| 95 // close, with multimap being a tiny bit faster. I think this is because | 95 // close, with multimap being a tiny bit faster. I think this is because |
| 96 // our map is at max around 1000 entries, and the additional complexity | 96 // our map is at max around 1000 entries, and the additional complexity |
| 97 // for the hashing might not overcome the O(log(1000)) for querying | 97 // for the hashing might not overcome the O(log(1000)) for querying |
| 98 // a multimap. Also, multimap is standard, another reason to use it. | 98 // a multimap. Also, multimap is standard, another reason to use it. |
| 99 // TODO(rdsmith): This benchmark should be re-done now that we're allowing | 99 // TODO(rdsmith): This benchmark should be re-done now that we're allowing |
| 100 // subtantially more entries in the map. | 100 // substantially more entries in the map. |
| 101 typedef std::multimap<std::string, CanonicalCookie*> CookieMap; | 101 using CookieMap = |
| 102 typedef std::pair<CookieMap::iterator, CookieMap::iterator> CookieMapItPair; | 102 std::multimap<std::string, std::unique_ptr<CanonicalCookie>>; |
| 103 typedef std::vector<CookieMap::iterator> CookieItVector; | 103 using CookieMapItPair = std::pair<CookieMap::iterator, CookieMap::iterator>; |
| 104 using CookieItVector = std::vector<CookieMap::iterator>; | |
| 104 | 105 |
| 105 // Cookie garbage collection thresholds. Based off of the Mozilla defaults. | 106 // Cookie garbage collection thresholds. Based off of the Mozilla defaults. |
| 106 // When the number of cookies gets to k{Domain,}MaxCookies | 107 // When the number of cookies gets to k{Domain,}MaxCookies |
| 107 // purge down to k{Domain,}MaxCookies - k{Domain,}PurgeCookies. | 108 // purge down to k{Domain,}MaxCookies - k{Domain,}PurgeCookies. |
| 108 // It might seem scary to have a high purge value, but really it's not. | 109 // It might seem scary to have a high purge value, but really it's not. |
| 109 // You just make sure that you increase the max to cover the increase | 110 // You just make sure that you increase the max to cover the increase |
| 110 // in purge, and we would have been purging the same number of cookies. | 111 // in purge, and we would have been purging the same number of cookies. |
| 111 // We're just going through the garbage collection process less often. | 112 // We're just going through the garbage collection process less often. |
| 112 // Note that the DOMAIN values are per eTLD+1; see comment for the | 113 // Note that the DOMAIN values are per eTLD+1; see comment for the |
| 113 // CookieMap typedef. So, e.g., the maximum number of cookies allowed for | 114 // CookieMap typedef. So, e.g., the maximum number of cookies allowed for |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 236 class DeleteSessionCookiesTask; | 237 class DeleteSessionCookiesTask; |
| 237 | 238 |
| 238 // Testing support. | 239 // Testing support. |
| 239 // For SetCookieWithCreationTime. | 240 // For SetCookieWithCreationTime. |
| 240 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, | 241 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, |
| 241 TestCookieDeleteAllCreatedBetweenTimestamps); | 242 TestCookieDeleteAllCreatedBetweenTimestamps); |
| 242 FRIEND_TEST_ALL_PREFIXES( | 243 FRIEND_TEST_ALL_PREFIXES( |
| 243 CookieMonsterTest, | 244 CookieMonsterTest, |
| 244 TestCookieDeleteAllCreatedBetweenTimestampsWithPredicate); | 245 TestCookieDeleteAllCreatedBetweenTimestampsWithPredicate); |
| 245 | 246 |
| 246 // For gargage collection constants. | 247 // For garbage collection constants. |
| 247 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection); | 248 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection); |
| 248 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestTotalGarbageCollection); | 249 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestTotalGarbageCollection); |
| 249 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GarbageCollectionTriggers); | 250 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GarbageCollectionTriggers); |
| 250 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes); | 251 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes); |
| 251 | 252 |
| 252 // For validation of key values. | 253 // For validation of key values. |
| 253 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree); | 254 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree); |
| 254 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport); | 255 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport); |
| 255 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GetKey); | 256 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GetKey); |
| 256 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGetKey); | 257 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGetKey); |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 444 void FetchAllCookies(); | 445 void FetchAllCookies(); |
| 445 | 446 |
| 446 // Whether all cookies should be fetched as soon as any is requested. | 447 // Whether all cookies should be fetched as soon as any is requested. |
| 447 bool ShouldFetchAllCookiesWhenFetchingAnyCookie(); | 448 bool ShouldFetchAllCookiesWhenFetchingAnyCookie(); |
| 448 | 449 |
| 449 // Stores cookies loaded from the backing store and invokes any deferred | 450 // Stores cookies loaded from the backing store and invokes any deferred |
| 450 // calls. |beginning_time| should be the moment PersistentCookieStore::Load | 451 // calls. |beginning_time| should be the moment PersistentCookieStore::Load |
| 451 // was invoked and is used for reporting histogram_time_blocked_on_load_. | 452 // was invoked and is used for reporting histogram_time_blocked_on_load_. |
| 452 // See PersistentCookieStore::Load for details on the contents of cookies. | 453 // See PersistentCookieStore::Load for details on the contents of cookies. |
| 453 void OnLoaded(base::TimeTicks beginning_time, | 454 void OnLoaded(base::TimeTicks beginning_time, |
| 454 const std::vector<CanonicalCookie*>& cookies); | 455 std::vector<std::unique_ptr<CanonicalCookie>> cookies); |
|
davidben
2016/10/10 23:05:00
Wow. Okay, passing a const-ref to an array of T*s
| |
| 455 | 456 |
| 456 // Stores cookies loaded from the backing store and invokes the deferred | 457 // Stores cookies loaded from the backing store and invokes the deferred |
| 457 // task(s) pending loading of cookies associated with the domain key | 458 // task(s) pending loading of cookies associated with the domain key |
| 458 // (eTLD+1). Called when all cookies for the domain key(eTLD+1) have been | 459 // (eTLD+1). Called when all cookies for the domain key(eTLD+1) have been |
| 459 // loaded from DB. See PersistentCookieStore::Load for details on the contents | 460 // loaded from DB. See PersistentCookieStore::Load for details on the contents |
| 460 // of cookies. | 461 // of cookies. |
| 461 void OnKeyLoaded(const std::string& key, | 462 void OnKeyLoaded(const std::string& key, |
| 462 const std::vector<CanonicalCookie*>& cookies); | 463 std::vector<std::unique_ptr<CanonicalCookie>> cookies); |
| 463 | 464 |
| 464 // Stores the loaded cookies. | 465 // Stores the loaded cookies. |
| 465 void StoreLoadedCookies(const std::vector<CanonicalCookie*>& cookies); | 466 void StoreLoadedCookies( |
| 467 std::vector<std::unique_ptr<CanonicalCookie>> cookies); | |
| 466 | 468 |
| 467 // Invokes deferred calls. | 469 // Invokes deferred calls. |
| 468 void InvokeQueue(); | 470 void InvokeQueue(); |
| 469 | 471 |
| 470 // Checks that |cookies_| matches our invariants, and tries to repair any | 472 // Checks that |cookies_| matches our invariants, and tries to repair any |
| 471 // inconsistencies. (In other words, it does not have duplicate cookies). | 473 // inconsistencies. (In other words, it does not have duplicate cookies). |
| 472 void EnsureCookiesMapIsValid(); | 474 void EnsureCookiesMapIsValid(); |
| 473 | 475 |
| 474 // Checks for any duplicate cookies for CookieMap key |key| which lie between | 476 // Checks for any duplicate cookies for CookieMap key |key| which lie between |
| 475 // |begin| and |end|. If any are found, all but the most recent are deleted. | 477 // |begin| and |end|. If any are found, all but the most recent are deleted. |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 498 // find the cookie in cookies_; see the comment before the CookieMap typedef | 500 // find the cookie in cookies_; see the comment before the CookieMap typedef |
| 499 // for details. | 501 // for details. |
| 500 // NOTE: There should never be more than a single matching equivalent cookie. | 502 // NOTE: There should never be more than a single matching equivalent cookie. |
| 501 bool DeleteAnyEquivalentCookie(const std::string& key, | 503 bool DeleteAnyEquivalentCookie(const std::string& key, |
| 502 const CanonicalCookie& ecc, | 504 const CanonicalCookie& ecc, |
| 503 const GURL& source_url, | 505 const GURL& source_url, |
| 504 bool skip_httponly, | 506 bool skip_httponly, |
| 505 bool already_expired, | 507 bool already_expired, |
| 506 bool enforce_strict_secure); | 508 bool enforce_strict_secure); |
| 507 | 509 |
| 508 // Takes ownership of *cc. Returns an iterator that points to the inserted | 510 // Inserts |cc| into cookies_. Returns an iterator that points to the inserted |
| 509 // cookie in cookies_. Guarantee: all iterators to cookies_ remain valid. | 511 // cookie in cookies_. Guarantee: all iterators to cookies_ remain valid. |
| 510 CookieMap::iterator InternalInsertCookie(const std::string& key, | 512 CookieMap::iterator InternalInsertCookie(const std::string& key, |
| 511 CanonicalCookie* cc, | 513 std::unique_ptr<CanonicalCookie> cc, |
| 512 const GURL& source_url, | 514 const GURL& source_url, |
| 513 bool sync_to_store); | 515 bool sync_to_store); |
| 514 | 516 |
| 515 // Helper function that sets cookies with more control. | 517 // Helper function that sets cookies with more control. |
| 516 // Not exposed as we don't want callers to have the ability | 518 // Not exposed as we don't want callers to have the ability |
| 517 // to specify (potentially duplicate) creation times. | 519 // to specify (potentially duplicate) creation times. |
| 518 bool SetCookieWithCreationTimeAndOptions(const GURL& url, | 520 bool SetCookieWithCreationTimeAndOptions(const GURL& url, |
| 519 const std::string& cookie_line, | 521 const std::string& cookie_line, |
| 520 const base::Time& creation_time, | 522 const base::Time& creation_time, |
| 521 const CookieOptions& options); | 523 const CookieOptions& options); |
| 522 | 524 |
| 523 // Helper function that sets a canonical cookie, deleting equivalents and | 525 // Helper function that sets a canonical cookie, deleting equivalents and |
| 524 // performing garbage collection. | 526 // performing garbage collection. |
| 525 // |source_url| is the URL that's attempting to set the cookie. | 527 // |source_url| is the URL that's attempting to set the cookie. |
| 526 bool SetCanonicalCookie(std::unique_ptr<CanonicalCookie> cc, | 528 bool SetCanonicalCookie(std::unique_ptr<CanonicalCookie> cc, |
| 527 const GURL& source_url, | 529 const GURL& source_url, |
| 528 const CookieOptions& options); | 530 const CookieOptions& options); |
| 529 | 531 |
| 530 // Helper function calling SetCanonicalCookie() for all cookies in |list|. | 532 // Helper function calling SetCanonicalCookie() for all cookies in |list|. |
| 531 bool SetCanonicalCookies(const CookieList& list); | 533 bool SetCanonicalCookies(const CookieList& list); |
| 532 | 534 |
| 533 void InternalUpdateCookieAccessTime(CanonicalCookie* cc, | 535 void InternalUpdateCookieAccessTime(CanonicalCookie* cc, |
| 534 const base::Time& current_time); | 536 const base::Time& current_time); |
| 535 | 537 |
| 536 // |deletion_cause| argument is used for collecting statistics and choosing | 538 // |deletion_cause| argument is used for collecting statistics and choosing |
| 537 // the correct CookieStore::ChangeCause for OnCookieChanged | 539 // the correct CookieStore::ChangeCause for OnCookieChanged |
| 538 // notifications. Guarantee: All iterators to cookies_ except to the | 540 // notifications. Guarantee: All iterators to cookies_ except to the |
| 539 // deleted entry remain vaild. | 541 // deleted entry remain valid. |
| 540 void InternalDeleteCookie(CookieMap::iterator it, | 542 void InternalDeleteCookie(CookieMap::iterator it, |
| 541 bool sync_to_store, | 543 bool sync_to_store, |
| 542 DeletionCause deletion_cause); | 544 DeletionCause deletion_cause); |
| 543 | 545 |
| 544 // If the number of cookies for CookieMap key |key|, or globally, are | 546 // If the number of cookies for CookieMap key |key|, or globally, are |
| 545 // over the preset maximums above, garbage collect, first for the host and | 547 // over the preset maximums above, garbage collect, first for the host and |
| 546 // then globally. See comments above garbage collection threshold | 548 // then globally. See comments above garbage collection threshold |
| 547 // constants for details. | 549 // constants for details. |
| 548 // | 550 // |
| 549 // Returns the number of cookies deleted (useful for debugging). | 551 // Returns the number of cookies deleted (useful for debugging). |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 707 std::set<int64_t> creation_times_; | 709 std::set<int64_t> creation_times_; |
| 708 | 710 |
| 709 std::vector<std::string> cookieable_schemes_; | 711 std::vector<std::string> cookieable_schemes_; |
| 710 | 712 |
| 711 scoped_refptr<CookieMonsterDelegate> delegate_; | 713 scoped_refptr<CookieMonsterDelegate> delegate_; |
| 712 | 714 |
| 713 base::Time last_statistic_record_time_; | 715 base::Time last_statistic_record_time_; |
| 714 | 716 |
| 715 bool persist_session_cookies_; | 717 bool persist_session_cookies_; |
| 716 | 718 |
| 717 typedef std::map<std::pair<GURL, std::string>, | 719 using CookieChangedHookMap = |
| 718 linked_ptr<CookieChangedCallbackList>> CookieChangedHookMap; | 720 std::map<std::pair<GURL, std::string>, |
| 721 std::unique_ptr<CookieChangedCallbackList>>; | |
| 719 CookieChangedHookMap hook_map_; | 722 CookieChangedHookMap hook_map_; |
| 720 | 723 |
| 721 base::ThreadChecker thread_checker_; | 724 base::ThreadChecker thread_checker_; |
| 722 | 725 |
| 723 base::WeakPtrFactory<CookieMonster> weak_ptr_factory_; | 726 base::WeakPtrFactory<CookieMonster> weak_ptr_factory_; |
| 724 | 727 |
| 725 DISALLOW_COPY_AND_ASSIGN(CookieMonster); | 728 DISALLOW_COPY_AND_ASSIGN(CookieMonster); |
| 726 }; | 729 }; |
| 727 | 730 |
| 728 class NET_EXPORT CookieMonsterDelegate | 731 class NET_EXPORT CookieMonsterDelegate |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 747 friend class base::RefCountedThreadSafe<CookieMonsterDelegate>; | 750 friend class base::RefCountedThreadSafe<CookieMonsterDelegate>; |
| 748 virtual ~CookieMonsterDelegate() {} | 751 virtual ~CookieMonsterDelegate() {} |
| 749 }; | 752 }; |
| 750 | 753 |
| 751 typedef base::RefCountedThreadSafe<CookieMonster::PersistentCookieStore> | 754 typedef base::RefCountedThreadSafe<CookieMonster::PersistentCookieStore> |
| 752 RefcountedPersistentCookieStore; | 755 RefcountedPersistentCookieStore; |
| 753 | 756 |
| 754 class NET_EXPORT CookieMonster::PersistentCookieStore | 757 class NET_EXPORT CookieMonster::PersistentCookieStore |
| 755 : public RefcountedPersistentCookieStore { | 758 : public RefcountedPersistentCookieStore { |
| 756 public: | 759 public: |
| 757 typedef base::Callback<void(const std::vector<CanonicalCookie*>&)> | 760 typedef base::Callback<void(std::vector<std::unique_ptr<CanonicalCookie>>)> |
| 758 LoadedCallback; | 761 LoadedCallback; |
| 759 | 762 |
| 760 // TODO(erikchen): Depending on the results of the cookie monster Finch | 763 // TODO(erikchen): Depending on the results of the cookie monster Finch |
| 761 // experiment, update the name and description of this method. The behavior | 764 // experiment, update the name and description of this method. The behavior |
| 762 // of this method doesn't change, but it has different semantics for the two | 765 // of this method doesn't change, but it has different semantics for the two |
| 763 // different logic paths. See http://crbug.com/473483. | 766 // different logic paths. See http://crbug.com/473483. |
| 764 // Initializes the store and retrieves the existing cookies. This will be | 767 // Initializes the store and retrieves the existing cookies. This will be |
| 765 // called only once at startup. The callback will return all the cookies | 768 // called only once at startup. The callback will return all the cookies |
| 766 // that are not yet returned to CookieMonster by previous priority loads. | 769 // that are not yet returned to CookieMonster by previous priority loads. |
| 767 // | 770 // |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 794 virtual ~PersistentCookieStore() {} | 797 virtual ~PersistentCookieStore() {} |
| 795 | 798 |
| 796 private: | 799 private: |
| 797 friend class base::RefCountedThreadSafe<PersistentCookieStore>; | 800 friend class base::RefCountedThreadSafe<PersistentCookieStore>; |
| 798 DISALLOW_COPY_AND_ASSIGN(PersistentCookieStore); | 801 DISALLOW_COPY_AND_ASSIGN(PersistentCookieStore); |
| 799 }; | 802 }; |
| 800 | 803 |
| 801 } // namespace net | 804 } // namespace net |
| 802 | 805 |
| 803 #endif // NET_COOKIES_COOKIE_MONSTER_H_ | 806 #endif // NET_COOKIES_COOKIE_MONSTER_H_ |
| OLD | NEW |