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> |
11 #include <stdint.h> | 11 #include <stdint.h> |
12 | 12 |
13 #include <deque> | 13 #include <deque> |
14 #include <map> | 14 #include <map> |
15 #include <queue> | 15 #include <queue> |
16 #include <set> | 16 #include <set> |
17 #include <string> | 17 #include <string> |
18 #include <utility> | 18 #include <utility> |
19 #include <vector> | 19 #include <vector> |
20 | 20 |
21 #include "base/callback_forward.h" | 21 #include "base/callback_forward.h" |
22 #include "base/gtest_prod_util.h" | 22 #include "base/gtest_prod_util.h" |
23 #include "base/macros.h" | 23 #include "base/macros.h" |
24 #include "base/memory/linked_ptr.h" | 24 #include "base/memory/linked_ptr.h" |
25 #include "base/memory/ref_counted.h" | 25 #include "base/memory/ref_counted.h" |
26 #include "base/memory/scoped_ptr.h" | 26 #include "base/memory/scoped_ptr.h" |
27 #include "base/synchronization/lock.h" | 27 #include "base/memory/weak_ptr.h" |
| 28 #include "base/threading/thread_checker.h" |
28 #include "base/time/time.h" | 29 #include "base/time/time.h" |
29 #include "net/base/net_export.h" | 30 #include "net/base/net_export.h" |
30 #include "net/cookies/canonical_cookie.h" | 31 #include "net/cookies/canonical_cookie.h" |
31 #include "net/cookies/cookie_constants.h" | 32 #include "net/cookies/cookie_constants.h" |
32 #include "net/cookies/cookie_store.h" | 33 #include "net/cookies/cookie_store.h" |
33 #include "url/gurl.h" | 34 #include "url/gurl.h" |
34 | 35 |
35 namespace base { | 36 namespace base { |
36 class Histogram; | 37 class Histogram; |
37 class HistogramBase; | 38 class HistogramBase; |
38 class TimeTicks; | 39 class TimeTicks; |
39 } // namespace base | 40 } // namespace base |
40 | 41 |
41 namespace net { | 42 namespace net { |
42 | 43 |
43 class CookieMonsterDelegate; | 44 class CookieMonsterDelegate; |
44 class ParsedCookie; | 45 class ParsedCookie; |
45 | 46 |
46 // The cookie monster is the system for storing and retrieving cookies. It has | 47 // The cookie monster is the system for storing and retrieving cookies. It has |
47 // an in-memory list of all cookies, and synchronizes non-session cookies to an | 48 // an in-memory list of all cookies, and synchronizes non-session cookies to an |
48 // optional permanent storage that implements the PersistentCookieStore | 49 // optional permanent storage that implements the PersistentCookieStore |
49 // interface. | 50 // interface. |
50 // | 51 // |
51 // This class IS thread-safe. Normally, it is only used on the I/O thread, but | |
52 // is also accessed directly through Automation for UI testing. | |
53 // | |
54 // Tasks may be deferred if all affected cookies are not yet loaded from the | 52 // Tasks may be deferred if all affected cookies are not yet loaded from the |
55 // backing store. Otherwise, callbacks may be invoked immediately. | 53 // backing store. Otherwise, callbacks may be invoked immediately. |
56 // | 54 // |
57 // A cookie task is either pending loading of the entire cookie store, or | 55 // A cookie task is either pending loading of the entire cookie store, or |
58 // loading of cookies for a specfic domain key(eTLD+1). In the former case, the | 56 // loading of cookies for a specfic domain key(eTLD+1). In the former case, the |
59 // cookie task will be queued in tasks_pending_ while PersistentCookieStore | 57 // cookie task will be queued in tasks_pending_ while PersistentCookieStore |
60 // chain loads the cookie store on DB thread. In the latter case, the cookie | 58 // chain loads the cookie store on DB thread. In the latter case, the cookie |
61 // task will be queued in tasks_pending_for_key_ while PermanentCookieStore | 59 // task will be queued in tasks_pending_for_key_ while PermanentCookieStore |
62 // loads cookies for the specified domain key(eTLD+1) on DB thread. | 60 // loads cookies for the specified domain key(eTLD+1) on DB thread. |
63 // | 61 // |
64 // Callbacks are guaranteed to be invoked on the calling thread. | |
65 // | |
66 // TODO(deanm) Implement CookieMonster, the cookie database. | 62 // TODO(deanm) Implement CookieMonster, the cookie database. |
67 // - Verify that our domain enforcement and non-dotted handling is correct | 63 // - Verify that our domain enforcement and non-dotted handling is correct |
68 class NET_EXPORT CookieMonster : public CookieStore { | 64 class NET_EXPORT CookieMonster : public CookieStore { |
69 public: | 65 public: |
70 class PersistentCookieStore; | 66 class PersistentCookieStore; |
71 | 67 |
72 // Terminology: | 68 // Terminology: |
73 // * The 'top level domain' (TLD) of an internet domain name is | 69 // * The 'top level domain' (TLD) of an internet domain name is |
74 // the terminal "." free substring (e.g. "com" for google.com | 70 // the terminal "." free substring (e.g. "com" for google.com |
75 // or world.std.com). | 71 // or world.std.com). |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 class GetCookieListWithOptionsTask; | 230 class GetCookieListWithOptionsTask; |
235 class SetAllCookiesTask; | 231 class SetAllCookiesTask; |
236 class SetCookieWithDetailsTask; | 232 class SetCookieWithDetailsTask; |
237 class SetCookieWithOptionsTask; | 233 class SetCookieWithOptionsTask; |
238 class DeleteSessionCookiesTask; | 234 class DeleteSessionCookiesTask; |
239 | 235 |
240 // Testing support. | 236 // Testing support. |
241 // For SetCookieWithCreationTime. | 237 // For SetCookieWithCreationTime. |
242 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, | 238 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, |
243 TestCookieDeleteAllCreatedBetweenTimestamps); | 239 TestCookieDeleteAllCreatedBetweenTimestamps); |
244 // For SetCookieWithCreationTime. | |
245 FRIEND_TEST_ALL_PREFIXES(MultiThreadedCookieMonsterTest, | |
246 ThreadCheckDeleteAllCreatedBetweenForHost); | |
247 | 240 |
248 // For gargage collection constants. | 241 // For gargage collection constants. |
249 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection); | 242 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestHostGarbageCollection); |
250 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestTotalGarbageCollection); | 243 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestTotalGarbageCollection); |
251 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GarbageCollectionTriggers); | 244 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, GarbageCollectionTriggers); |
252 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes); | 245 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestGCTimes); |
253 | 246 |
254 // For validation of key values. | 247 // For validation of key values. |
255 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree); | 248 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestDomainTree); |
256 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport); | 249 FRIEND_TEST_ALL_PREFIXES(CookieMonsterTest, TestImport); |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 const base::Time& creation_time); | 428 const base::Time& creation_time); |
436 | 429 |
437 int DeleteSessionCookies(); | 430 int DeleteSessionCookies(); |
438 | 431 |
439 // The first access to the cookie store initializes it. This method should be | 432 // The first access to the cookie store initializes it. This method should be |
440 // called before any access to the cookie store. | 433 // called before any access to the cookie store. |
441 void MarkCookieStoreAsInitialized(); | 434 void MarkCookieStoreAsInitialized(); |
442 | 435 |
443 // Fetches all cookies if the backing store exists and they're not already | 436 // Fetches all cookies if the backing store exists and they're not already |
444 // being fetched. | 437 // being fetched. |
445 // Note: this method should always be called with lock_ held. | |
446 void FetchAllCookiesIfNecessary(); | 438 void FetchAllCookiesIfNecessary(); |
447 | 439 |
448 // Fetches all cookies from the backing store. | 440 // Fetches all cookies from the backing store. |
449 // Note: this method should always be called with lock_ held. | |
450 void FetchAllCookies(); | 441 void FetchAllCookies(); |
451 | 442 |
452 // Whether all cookies should be fetched as soon as any is requested. | 443 // Whether all cookies should be fetched as soon as any is requested. |
453 bool ShouldFetchAllCookiesWhenFetchingAnyCookie(); | 444 bool ShouldFetchAllCookiesWhenFetchingAnyCookie(); |
454 | 445 |
455 // Stores cookies loaded from the backing store and invokes any deferred | 446 // Stores cookies loaded from the backing store and invokes any deferred |
456 // calls. |beginning_time| should be the moment PersistentCookieStore::Load | 447 // calls. |beginning_time| should be the moment PersistentCookieStore::Load |
457 // was invoked and is used for reporting histogram_time_blocked_on_load_. | 448 // was invoked and is used for reporting histogram_time_blocked_on_load_. |
458 // See PersistentCookieStore::Load for details on the contents of cookies. | 449 // See PersistentCookieStore::Load for details on the contents of cookies. |
459 void OnLoaded(base::TimeTicks beginning_time, | 450 void OnLoaded(base::TimeTicks beginning_time, |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
637 // Computes the difference between |old_cookies| and |new_cookies|, and writes | 628 // Computes the difference between |old_cookies| and |new_cookies|, and writes |
638 // the result in |cookies_to_add| and |cookies_to_delete|. | 629 // the result in |cookies_to_add| and |cookies_to_delete|. |
639 // This function has the side effect of changing the order of |old_cookies| | 630 // This function has the side effect of changing the order of |old_cookies| |
640 // and |new_cookies|. |cookies_to_add| and |cookies_to_delete| must be empty, | 631 // and |new_cookies|. |cookies_to_add| and |cookies_to_delete| must be empty, |
641 // and none of the arguments can be null. | 632 // and none of the arguments can be null. |
642 void ComputeCookieDiff(CookieList* old_cookies, | 633 void ComputeCookieDiff(CookieList* old_cookies, |
643 CookieList* new_cookies, | 634 CookieList* new_cookies, |
644 CookieList* cookies_to_add, | 635 CookieList* cookies_to_add, |
645 CookieList* cookies_to_delete); | 636 CookieList* cookies_to_delete); |
646 | 637 |
| 638 // Runs the given callback. Used to avoid running callbacks after the store |
| 639 // has been destroyed. |
| 640 void RunCallback(const base::Closure& callback); |
| 641 |
647 // Run all cookie changed callbacks that are monitoring |cookie|. | 642 // Run all cookie changed callbacks that are monitoring |cookie|. |
648 // |removed| is true if the cookie was deleted. | 643 // |removed| is true if the cookie was deleted. |
649 void RunCallbacks(const CanonicalCookie& cookie, bool removed); | 644 void RunCookieChangedCallbacks(const CanonicalCookie& cookie, bool removed); |
650 | 645 |
651 // Histogram variables; see CookieMonster::InitializeHistograms() in | 646 // Histogram variables; see CookieMonster::InitializeHistograms() in |
652 // cookie_monster.cc for details. | 647 // cookie_monster.cc for details. |
653 base::HistogramBase* histogram_expiration_duration_minutes_; | 648 base::HistogramBase* histogram_expiration_duration_minutes_; |
654 base::HistogramBase* histogram_evicted_last_access_minutes_; | 649 base::HistogramBase* histogram_evicted_last_access_minutes_; |
655 base::HistogramBase* histogram_count_; | 650 base::HistogramBase* histogram_count_; |
656 base::HistogramBase* histogram_cookie_deletion_cause_; | 651 base::HistogramBase* histogram_cookie_deletion_cause_; |
657 base::HistogramBase* histogram_cookie_type_; | 652 base::HistogramBase* histogram_cookie_type_; |
658 base::HistogramBase* histogram_cookie_source_scheme_; | 653 base::HistogramBase* histogram_cookie_source_scheme_; |
659 base::HistogramBase* histogram_cookie_delete_equivalent_; | 654 base::HistogramBase* histogram_cookie_delete_equivalent_; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 // During loading, holds the set of all loaded cookie creation times. Used to | 701 // During loading, holds the set of all loaded cookie creation times. Used to |
707 // avoid ever letting cookies with duplicate creation times into the store; | 702 // avoid ever letting cookies with duplicate creation times into the store; |
708 // that way we don't have to worry about what sections of code are safe | 703 // that way we don't have to worry about what sections of code are safe |
709 // to call while it's in that state. | 704 // to call while it's in that state. |
710 std::set<int64_t> creation_times_; | 705 std::set<int64_t> creation_times_; |
711 | 706 |
712 std::vector<std::string> cookieable_schemes_; | 707 std::vector<std::string> cookieable_schemes_; |
713 | 708 |
714 scoped_refptr<CookieMonsterDelegate> delegate_; | 709 scoped_refptr<CookieMonsterDelegate> delegate_; |
715 | 710 |
716 // Lock for thread-safety | |
717 base::Lock lock_; | |
718 | |
719 base::Time last_statistic_record_time_; | 711 base::Time last_statistic_record_time_; |
720 | 712 |
721 bool persist_session_cookies_; | 713 bool persist_session_cookies_; |
722 | 714 |
723 typedef std::map<std::pair<GURL, std::string>, | 715 typedef std::map<std::pair<GURL, std::string>, |
724 linked_ptr<CookieChangedCallbackList>> CookieChangedHookMap; | 716 linked_ptr<CookieChangedCallbackList>> CookieChangedHookMap; |
725 CookieChangedHookMap hook_map_; | 717 CookieChangedHookMap hook_map_; |
726 | 718 |
| 719 base::ThreadChecker thread_checker_; |
| 720 |
| 721 base::WeakPtrFactory<CookieMonster> weak_ptr_factory_; |
| 722 |
727 DISALLOW_COPY_AND_ASSIGN(CookieMonster); | 723 DISALLOW_COPY_AND_ASSIGN(CookieMonster); |
728 }; | 724 }; |
729 | 725 |
730 class NET_EXPORT CookieMonsterDelegate | 726 class NET_EXPORT CookieMonsterDelegate |
731 : public base::RefCountedThreadSafe<CookieMonsterDelegate> { | 727 : public base::RefCountedThreadSafe<CookieMonsterDelegate> { |
732 public: | 728 public: |
733 // The publicly relevant reasons a cookie might be changed. | 729 // The publicly relevant reasons a cookie might be changed. |
734 enum ChangeCause { | 730 enum ChangeCause { |
735 // The cookie was changed directly by a consumer's action. | 731 // The cookie was changed directly by a consumer's action. |
736 CHANGE_COOKIE_EXPLICIT, | 732 CHANGE_COOKIE_EXPLICIT, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
773 typedef base::Callback<void(const std::vector<CanonicalCookie*>&)> | 769 typedef base::Callback<void(const std::vector<CanonicalCookie*>&)> |
774 LoadedCallback; | 770 LoadedCallback; |
775 | 771 |
776 // TODO(erikchen): Depending on the results of the cookie monster Finch | 772 // TODO(erikchen): Depending on the results of the cookie monster Finch |
777 // experiment, update the name and description of this method. The behavior | 773 // experiment, update the name and description of this method. The behavior |
778 // of this method doesn't change, but it has different semantics for the two | 774 // of this method doesn't change, but it has different semantics for the two |
779 // different logic paths. See http://crbug.com/473483. | 775 // different logic paths. See http://crbug.com/473483. |
780 // Initializes the store and retrieves the existing cookies. This will be | 776 // Initializes the store and retrieves the existing cookies. This will be |
781 // called only once at startup. The callback will return all the cookies | 777 // called only once at startup. The callback will return all the cookies |
782 // that are not yet returned to CookieMonster by previous priority loads. | 778 // that are not yet returned to CookieMonster by previous priority loads. |
| 779 // |
| 780 // |loaded_callback| may not be NULL. |
783 virtual void Load(const LoadedCallback& loaded_callback) = 0; | 781 virtual void Load(const LoadedCallback& loaded_callback) = 0; |
784 | 782 |
785 // Does a priority load of all cookies for the domain key (eTLD+1). The | 783 // Does a priority load of all cookies for the domain key (eTLD+1). The |
786 // callback will return all the cookies that are not yet returned by previous | 784 // callback will return all the cookies that are not yet returned by previous |
787 // loads, which includes cookies for the requested domain key if they are not | 785 // loads, which includes cookies for the requested domain key if they are not |
788 // already returned, plus all cookies that are chain-loaded and not yet | 786 // already returned, plus all cookies that are chain-loaded and not yet |
789 // returned to CookieMonster. | 787 // returned to CookieMonster. |
| 788 // |
| 789 // |loaded_callback| may not be NULL. |
790 virtual void LoadCookiesForKey(const std::string& key, | 790 virtual void LoadCookiesForKey(const std::string& key, |
791 const LoadedCallback& loaded_callback) = 0; | 791 const LoadedCallback& loaded_callback) = 0; |
792 | 792 |
793 virtual void AddCookie(const CanonicalCookie& cc) = 0; | 793 virtual void AddCookie(const CanonicalCookie& cc) = 0; |
794 virtual void UpdateCookieAccessTime(const CanonicalCookie& cc) = 0; | 794 virtual void UpdateCookieAccessTime(const CanonicalCookie& cc) = 0; |
795 virtual void DeleteCookie(const CanonicalCookie& cc) = 0; | 795 virtual void DeleteCookie(const CanonicalCookie& cc) = 0; |
796 | 796 |
797 // Instructs the store to not discard session only cookies on shutdown. | 797 // Instructs the store to not discard session only cookies on shutdown. |
798 virtual void SetForceKeepSessionState() = 0; | 798 virtual void SetForceKeepSessionState() = 0; |
799 | 799 |
800 // Flushes the store and posts |callback| when complete. | 800 // Flushes the store and posts |callback| when complete. |callback| may be |
| 801 // NULL. |
801 virtual void Flush(const base::Closure& callback) = 0; | 802 virtual void Flush(const base::Closure& callback) = 0; |
802 | 803 |
803 protected: | 804 protected: |
804 PersistentCookieStore() {} | 805 PersistentCookieStore() {} |
805 virtual ~PersistentCookieStore() {} | 806 virtual ~PersistentCookieStore() {} |
806 | 807 |
807 private: | 808 private: |
808 friend class base::RefCountedThreadSafe<PersistentCookieStore>; | 809 friend class base::RefCountedThreadSafe<PersistentCookieStore>; |
809 DISALLOW_COPY_AND_ASSIGN(PersistentCookieStore); | 810 DISALLOW_COPY_AND_ASSIGN(PersistentCookieStore); |
810 }; | 811 }; |
811 | 812 |
812 } // namespace net | 813 } // namespace net |
813 | 814 |
814 #endif // NET_COOKIES_COOKIE_MONSTER_H_ | 815 #endif // NET_COOKIES_COOKIE_MONSTER_H_ |
OLD | NEW |