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