| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_BASE_COOKIE_MONSTER_H_ | 7 #ifndef NET_BASE_COOKIE_MONSTER_H_ |
| 8 #define NET_BASE_COOKIE_MONSTER_H_ | 8 #define NET_BASE_COOKIE_MONSTER_H_ |
| 9 #pragma once | 9 #pragma once |
| 10 | 10 |
| 11 #include <deque> |
| 11 #include <map> | 12 #include <map> |
| 12 #include <queue> | 13 #include <queue> |
| 14 #include <set> |
| 13 #include <string> | 15 #include <string> |
| 14 #include <utility> | 16 #include <utility> |
| 15 #include <vector> | 17 #include <vector> |
| 16 | 18 |
| 17 #include "base/basictypes.h" | 19 #include "base/basictypes.h" |
| 20 #include "base/callback.h" |
| 18 #include "base/gtest_prod_util.h" | 21 #include "base/gtest_prod_util.h" |
| 19 #include "base/memory/ref_counted.h" | 22 #include "base/memory/ref_counted.h" |
| 20 #include "base/memory/scoped_ptr.h" | 23 #include "base/memory/scoped_ptr.h" |
| 21 #include "base/synchronization/lock.h" | 24 #include "base/synchronization/lock.h" |
| 22 #include "base/task.h" | 25 #include "base/task.h" |
| 23 #include "base/time.h" | 26 #include "base/time.h" |
| 24 #include "net/base/cookie_store.h" | 27 #include "net/base/cookie_store.h" |
| 25 #include "net/base/net_export.h" | 28 #include "net/base/net_export.h" |
| 26 | 29 |
| 27 class GURL; | 30 class GURL; |
| 28 | 31 |
| 29 namespace base { | 32 namespace base { |
| 30 class Histogram; | 33 class Histogram; |
| 31 class TimeTicks; | 34 class TimeTicks; |
| 32 } | 35 } // namespace base |
| 33 | 36 |
| 34 namespace net { | 37 namespace net { |
| 35 | 38 |
| 36 class CookieList; | 39 class CookieList; |
| 37 | 40 |
| 38 // The cookie monster is the system for storing and retrieving cookies. It has | 41 // The cookie monster is the system for storing and retrieving cookies. It has |
| 39 // an in-memory list of all cookies, and synchronizes non-session cookies to an | 42 // an in-memory list of all cookies, and synchronizes non-session cookies to an |
| 40 // optional permanent storage that implements the PersistentCookieStore | 43 // optional permanent storage that implements the PersistentCookieStore |
| 41 // interface. | 44 // interface. |
| 42 // | 45 // |
| 43 // This class IS thread-safe. Normally, it is only used on the I/O thread, but | 46 // This class IS thread-safe. Normally, it is only used on the I/O thread, but |
| 44 // is also accessed directly through Automation for UI testing. | 47 // is also accessed directly through Automation for UI testing. |
| 45 // | 48 // |
| 46 // Several methods exist in asynchronous forms. Calls may be deferred if all | 49 // All cookie tasks are handled asynchronously. Tasks may be deferred if |
| 47 // affected cookies are not yet loaded from the backing store. Otherwise, the | 50 // all affected cookies are not yet loaded from the backing store. Otherwise, |
| 48 // callback may be invoked immediately (prior to return of the asynchronous | 51 // the callback may be invoked immediately (prior to return of the asynchronous |
| 49 // function). | 52 // function). |
| 50 // | 53 // |
| 54 // A cookie task is either pending loading of the entire cookie store, or |
| 55 // loading of cookies for a specfic domain key(eTLD+1). In the former case, the |
| 56 // cookie task will be queued in queue_ while PersistentCookieStore chain loads |
| 57 // the cookie store on DB thread. In the latter case, the cookie task will be |
| 58 // queued in tasks_queued_ while PermanentCookieStore loads cookies for the |
| 59 // specified domain key(eTLD+1) on DB thread. |
| 60 // |
| 51 // Callbacks are guaranteed to be invoked on the calling thread. | 61 // Callbacks are guaranteed to be invoked on the calling thread. |
| 52 // | 62 // |
| 53 // TODO(deanm) Implement CookieMonster, the cookie database. | 63 // TODO(deanm) Implement CookieMonster, the cookie database. |
| 54 // - Verify that our domain enforcement and non-dotted handling is correct | 64 // - Verify that our domain enforcement and non-dotted handling is correct |
| 55 class NET_EXPORT CookieMonster : public CookieStore { | 65 class NET_EXPORT CookieMonster : public CookieStore { |
| 56 public: | 66 public: |
| 57 class CanonicalCookie; | 67 class CanonicalCookie; |
| 58 class Delegate; | 68 class Delegate; |
| 59 class ParsedCookie; | 69 class ParsedCookie; |
| 60 class PersistentCookieStore; | 70 class PersistentCookieStore; |
| (...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 } | 453 } |
| 444 } | 454 } |
| 445 | 455 |
| 446 // Initializes the backing store and reads existing cookies from it. | 456 // Initializes the backing store and reads existing cookies from it. |
| 447 // Should only be called by InitIfNecessary(). | 457 // Should only be called by InitIfNecessary(). |
| 448 void InitStore(); | 458 void InitStore(); |
| 449 | 459 |
| 450 // Stores cookies loaded from the backing store and invokes any deferred | 460 // Stores cookies loaded from the backing store and invokes any deferred |
| 451 // calls. |beginning_time| should be the moment PersistentCookieStore::Load | 461 // calls. |beginning_time| should be the moment PersistentCookieStore::Load |
| 452 // was invoked and is used for reporting histogram_time_load_. | 462 // was invoked and is used for reporting histogram_time_load_. |
| 463 // See PersistentCookieStore::Load for details on the contents of cookies. |
| 453 void OnLoaded(base::TimeTicks beginning_time, | 464 void OnLoaded(base::TimeTicks beginning_time, |
| 454 const std::vector<CanonicalCookie*>& cookies); | 465 const std::vector<CanonicalCookie*>& cookies); |
| 455 | 466 |
| 467 // Stores cookies loaded from the backing store and invokes the deferred |
| 468 // task(s) pending loading of cookies associated with the domain key |
| 469 // (eTLD+1). Called when all cookies for the domain key(eTLD+1) have been |
| 470 // loaded from DB. See PersistentCookieStore::Load for details on the contents |
| 471 // of cookies. |
| 472 void OnKeyLoaded( |
| 473 const std::string& key, |
| 474 const std::vector<CanonicalCookie*>& cookies); |
| 475 |
| 456 // Stores the loaded cookies. | 476 // Stores the loaded cookies. |
| 457 void StoreLoadedCookies(const std::vector<CanonicalCookie*>& cookies); | 477 void StoreLoadedCookies(const std::vector<CanonicalCookie*>& cookies); |
| 458 | 478 |
| 459 // Invokes deferred calls. | 479 // Invokes deferred calls. |
| 460 void InvokeQueue(); | 480 void InvokeQueue(); |
| 461 | 481 |
| 462 // Checks that |cookies_| matches our invariants, and tries to repair any | 482 // Checks that |cookies_| matches our invariants, and tries to repair any |
| 463 // inconsistencies. (In other words, it does not have duplicate cookies). | 483 // inconsistencies. (In other words, it does not have duplicate cookies). |
| 464 void EnsureCookiesMapIsValid(); | 484 void EnsureCookiesMapIsValid(); |
| 465 | 485 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 void RecordPeriodicStats(const base::Time& current_time); | 581 void RecordPeriodicStats(const base::Time& current_time); |
| 562 | 582 |
| 563 // Initialize the above variables; should only be called from | 583 // Initialize the above variables; should only be called from |
| 564 // the constructor. | 584 // the constructor. |
| 565 void InitializeHistograms(); | 585 void InitializeHistograms(); |
| 566 | 586 |
| 567 // The resolution of our time isn't enough, so we do something | 587 // The resolution of our time isn't enough, so we do something |
| 568 // ugly and increment when we've seen the same time twice. | 588 // ugly and increment when we've seen the same time twice. |
| 569 base::Time CurrentTime(); | 589 base::Time CurrentTime(); |
| 570 | 590 |
| 571 // Run the cookie request task if cookie loaded, otherwise added the task | 591 // Runs the task if, or defers the task until, the full cookie database is |
| 572 // to task queue. | 592 // loaded. |
| 573 void DoCookieTask(const scoped_refptr<CookieMonsterTask>& task_item); | 593 void DoCookieTask(const scoped_refptr<CookieMonsterTask>& task_item); |
| 574 | 594 |
| 595 // Runs the task if, or defers the task until, the cookies for the given URL |
| 596 // are loaded. |
| 597 void DoCookieTaskForURL(const scoped_refptr<CookieMonsterTask>& task_item, |
| 598 const GURL& url); |
| 599 |
| 575 // Histogram variables; see CookieMonster::InitializeHistograms() in | 600 // Histogram variables; see CookieMonster::InitializeHistograms() in |
| 576 // cookie_monster.cc for details. | 601 // cookie_monster.cc for details. |
| 577 base::Histogram* histogram_expiration_duration_minutes_; | 602 base::Histogram* histogram_expiration_duration_minutes_; |
| 578 base::Histogram* histogram_between_access_interval_minutes_; | 603 base::Histogram* histogram_between_access_interval_minutes_; |
| 579 base::Histogram* histogram_evicted_last_access_minutes_; | 604 base::Histogram* histogram_evicted_last_access_minutes_; |
| 580 base::Histogram* histogram_count_; | 605 base::Histogram* histogram_count_; |
| 581 base::Histogram* histogram_domain_count_; | 606 base::Histogram* histogram_domain_count_; |
| 582 base::Histogram* histogram_etldp1_count_; | 607 base::Histogram* histogram_etldp1_count_; |
| 583 base::Histogram* histogram_domain_per_etldp1_count_; | 608 base::Histogram* histogram_domain_per_etldp1_count_; |
| 584 base::Histogram* histogram_number_duplicate_db_cookies_; | 609 base::Histogram* histogram_number_duplicate_db_cookies_; |
| 585 base::Histogram* histogram_cookie_deletion_cause_; | 610 base::Histogram* histogram_cookie_deletion_cause_; |
| 586 base::Histogram* histogram_time_get_; | 611 base::Histogram* histogram_time_get_; |
| 587 base::Histogram* histogram_time_mac_; | 612 base::Histogram* histogram_time_mac_; |
| 588 base::Histogram* histogram_time_load_; | 613 base::Histogram* histogram_time_load_; |
| 589 | 614 |
| 590 CookieMap cookies_; | 615 CookieMap cookies_; |
| 591 | 616 |
| 592 // Indicates whether the cookie store has been initialized. This happens | 617 // Indicates whether the cookie store has been initialized. This happens |
| 593 // lazily in InitStoreIfNecessary(). | 618 // lazily in InitStoreIfNecessary(). |
| 594 bool initialized_; | 619 bool initialized_; |
| 595 | 620 |
| 596 // Indicates whether loading from the backend store is completed and | 621 // Indicates whether loading from the backend store is completed and |
| 597 // calls may be immediately processed. | 622 // calls may be immediately processed. |
| 598 bool loaded_; | 623 bool loaded_; |
| 599 | 624 |
| 600 // Queues calls to CookieMonster until loading from the backend store is | 625 // List of domain keys that have been loaded from the DB. |
| 601 // completed. | 626 std::set<std::string> keys_loaded_; |
| 627 |
| 628 // Map of domain keys to their associated task queues. These tasks are blocked |
| 629 // until all cookies for the associated domain key eTLD+1 are loaded from the |
| 630 // backend store. |
| 631 std::map<std::string, std::deque<scoped_refptr<CookieMonsterTask> > > |
| 632 tasks_queued_; |
| 633 |
| 634 // Queues tasks that are blocked until all cookies are loaded from the backend |
| 635 // store. |
| 602 std::queue<scoped_refptr<CookieMonsterTask> > queue_; | 636 std::queue<scoped_refptr<CookieMonsterTask> > queue_; |
| 603 | 637 |
| 604 // Indicates whether this cookie monster uses the new effective domain | 638 // Indicates whether this cookie monster uses the new effective domain |
| 605 // key scheme or not. | 639 // key scheme or not. |
| 606 ExpiryAndKeyScheme expiry_and_key_scheme_; | 640 ExpiryAndKeyScheme expiry_and_key_scheme_; |
| 607 | 641 |
| 608 scoped_refptr<PersistentCookieStore> store_; | 642 scoped_refptr<PersistentCookieStore> store_; |
| 609 | 643 |
| 610 base::Time last_time_seen_; | 644 base::Time last_time_seen_; |
| 611 | 645 |
| 612 // Minimum delay after updating a cookie's LastAccessDate before we will | 646 // Minimum delay after updating a cookie's LastAccessDate before we will |
| 613 // update it again. | 647 // update it again. |
| 614 const base::TimeDelta last_access_threshold_; | 648 const base::TimeDelta last_access_threshold_; |
| 615 | 649 |
| 616 // Approximate date of access time of least recently accessed cookie | 650 // Approximate date of access time of least recently accessed cookie |
| 617 // in |cookies_|. Note that this is not guaranteed to be accurate, only a) | 651 // in |cookies_|. Note that this is not guaranteed to be accurate, only a) |
| 618 // to be before or equal to the actual time, and b) to be accurate | 652 // to be before or equal to the actual time, and b) to be accurate |
| 619 // immediately after a garbage collection that scans through all the cookies. | 653 // immediately after a garbage collection that scans through all the cookies. |
| 620 // This value is used to determine whether global garbage collection might | 654 // This value is used to determine whether global garbage collection might |
| 621 // find cookies to purge. | 655 // find cookies to purge. |
| 622 // Note: The default Time() constructor will create a value that compares | 656 // Note: The default Time() constructor will create a value that compares |
| 623 // earlier than any other time value, which is is wanted. Thus this | 657 // earlier than any other time value, which is wanted. Thus this |
| 624 // value is not initialized. | 658 // value is not initialized. |
| 625 base::Time earliest_access_time_; | 659 base::Time earliest_access_time_; |
| 626 | 660 |
| 661 // During loading, holds the set of all loaded cookie creation times. Used to |
| 662 // avoid ever letting cookies with duplicate creation times into the store; |
| 663 // that way we don't have to worry about what sections of code are safe |
| 664 // to call while it's in that state. |
| 665 std::set<int64> creation_times_; |
| 666 |
| 627 std::vector<std::string> cookieable_schemes_; | 667 std::vector<std::string> cookieable_schemes_; |
| 628 | 668 |
| 629 scoped_refptr<Delegate> delegate_; | 669 scoped_refptr<Delegate> delegate_; |
| 630 | 670 |
| 631 // Lock for thread-safety | 671 // Lock for thread-safety |
| 632 base::Lock lock_; | 672 base::Lock lock_; |
| 633 | 673 |
| 634 base::Time last_statistic_record_time_; | 674 base::Time last_statistic_record_time_; |
| 635 | 675 |
| 636 bool keep_expired_cookies_; | 676 bool keep_expired_cookies_; |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 912 | 952 |
| 913 class CookieMonster::PersistentCookieStore | 953 class CookieMonster::PersistentCookieStore |
| 914 : public RefcountedPersistentCookieStore { | 954 : public RefcountedPersistentCookieStore { |
| 915 public: | 955 public: |
| 916 virtual ~PersistentCookieStore() {} | 956 virtual ~PersistentCookieStore() {} |
| 917 | 957 |
| 918 typedef base::Callback<void(const std::vector< | 958 typedef base::Callback<void(const std::vector< |
| 919 CookieMonster::CanonicalCookie*>&)> LoadedCallback; | 959 CookieMonster::CanonicalCookie*>&)> LoadedCallback; |
| 920 | 960 |
| 921 // Initializes the store and retrieves the existing cookies. This will be | 961 // Initializes the store and retrieves the existing cookies. This will be |
| 922 // called only once at startup. | 962 // called only once at startup. The callback will return all the cookies |
| 923 virtual bool Load(const LoadedCallback& loaded_callback) = 0; | 963 // that are not yet returned to CookieMonster by previous priority loads. |
| 964 virtual void Load(const LoadedCallback& loaded_callback) = 0; |
| 965 |
| 966 // Does a priority load of all cookies for the domain key (eTLD+1). The |
| 967 // callback will return all the cookies that are not yet returned by previous |
| 968 // loads, which includes cookies for the requested domain key if they are not |
| 969 // already returned, plus all cookies that are chain-loaded and not yet |
| 970 // returned to CookieMonster. |
| 971 virtual void LoadCookiesForKey(const std::string& key, |
| 972 const LoadedCallback& loaded_callback) = 0; |
| 924 | 973 |
| 925 virtual void AddCookie(const CanonicalCookie& cc) = 0; | 974 virtual void AddCookie(const CanonicalCookie& cc) = 0; |
| 926 virtual void UpdateCookieAccessTime(const CanonicalCookie& cc) = 0; | 975 virtual void UpdateCookieAccessTime(const CanonicalCookie& cc) = 0; |
| 927 virtual void DeleteCookie(const CanonicalCookie& cc) = 0; | 976 virtual void DeleteCookie(const CanonicalCookie& cc) = 0; |
| 928 | 977 |
| 929 // Sets the value of the user preference whether the persistent storage | 978 // Sets the value of the user preference whether the persistent storage |
| 930 // must be deleted upon destruction. | 979 // must be deleted upon destruction. |
| 931 virtual void SetClearLocalStateOnExit(bool clear_local_state) = 0; | 980 virtual void SetClearLocalStateOnExit(bool clear_local_state) = 0; |
| 932 | 981 |
| 933 // Flush the store and post the given Task when complete. | 982 // Flush the store and post the given Task when complete. |
| 934 virtual void Flush(Task* completion_task) = 0; | 983 virtual void Flush(Task* completion_task) = 0; |
| 935 | 984 |
| 936 protected: | 985 protected: |
| 937 PersistentCookieStore() {} | 986 PersistentCookieStore() {} |
| 938 | 987 |
| 939 private: | 988 private: |
| 940 DISALLOW_COPY_AND_ASSIGN(PersistentCookieStore); | 989 DISALLOW_COPY_AND_ASSIGN(PersistentCookieStore); |
| 941 }; | 990 }; |
| 942 | 991 |
| 943 class CookieList : public std::vector<CookieMonster::CanonicalCookie> { | 992 class CookieList : public std::vector<CookieMonster::CanonicalCookie> { |
| 944 }; | 993 }; |
| 945 | 994 |
| 946 } // namespace net | 995 } // namespace net |
| 947 | 996 |
| 948 #endif // NET_BASE_COOKIE_MONSTER_H_ | 997 #endif // NET_BASE_COOKIE_MONSTER_H_ |
| OLD | NEW |