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 // Portions of this code based on Mozilla: | 5 // Portions of this code based on Mozilla: |
6 // (netwerk/cookie/src/nsCookieService.cpp) | 6 // (netwerk/cookie/src/nsCookieService.cpp) |
7 /* ***** BEGIN LICENSE BLOCK ***** | 7 /* ***** BEGIN LICENSE BLOCK ***** |
8 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 8 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
9 * | 9 * |
10 * The contents of this file are subject to the Mozilla Public License Version | 10 * The contents of this file are subject to the Mozilla Public License Version |
(...skipping 30 matching lines...) Expand all Loading... |
41 * the terms of any one of the MPL, the GPL or the LGPL. | 41 * the terms of any one of the MPL, the GPL or the LGPL. |
42 * | 42 * |
43 * ***** END LICENSE BLOCK ***** */ | 43 * ***** END LICENSE BLOCK ***** */ |
44 | 44 |
45 #include "net/base/cookie_monster.h" | 45 #include "net/base/cookie_monster.h" |
46 | 46 |
47 #include <algorithm> | 47 #include <algorithm> |
48 #include <set> | 48 #include <set> |
49 | 49 |
50 #include "base/basictypes.h" | 50 #include "base/basictypes.h" |
| 51 #include "base/bind.h" |
51 #include "base/format_macros.h" | 52 #include "base/format_macros.h" |
52 #include "base/logging.h" | 53 #include "base/logging.h" |
53 #include "base/memory/scoped_ptr.h" | 54 #include "base/memory/scoped_ptr.h" |
54 #include "base/message_loop.h" | 55 #include "base/message_loop.h" |
| 56 #include "base/message_loop_proxy.h" |
55 #include "base/metrics/histogram.h" | 57 #include "base/metrics/histogram.h" |
56 #include "base/string_tokenizer.h" | 58 #include "base/string_tokenizer.h" |
57 #include "base/string_util.h" | 59 #include "base/string_util.h" |
58 #include "base/stringprintf.h" | 60 #include "base/stringprintf.h" |
59 #include "googleurl/src/gurl.h" | 61 #include "googleurl/src/gurl.h" |
60 #include "googleurl/src/url_canon.h" | 62 #include "googleurl/src/url_canon.h" |
61 #include "net/base/net_util.h" | 63 #include "net/base/net_util.h" |
62 #include "net/base/registry_controlled_domain.h" | 64 #include "net/base/registry_controlled_domain.h" |
63 | 65 |
64 using base::Time; | 66 using base::Time; |
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 } | 415 } |
414 } | 416 } |
415 | 417 |
416 } // namespace | 418 } // namespace |
417 | 419 |
418 // static | 420 // static |
419 bool CookieMonster::enable_file_scheme_ = false; | 421 bool CookieMonster::enable_file_scheme_ = false; |
420 | 422 |
421 CookieMonster::CookieMonster(PersistentCookieStore* store, Delegate* delegate) | 423 CookieMonster::CookieMonster(PersistentCookieStore* store, Delegate* delegate) |
422 : initialized_(false), | 424 : initialized_(false), |
| 425 loaded_(false), |
423 expiry_and_key_scheme_(expiry_and_key_default_), | 426 expiry_and_key_scheme_(expiry_and_key_default_), |
424 store_(store), | 427 store_(store), |
425 last_access_threshold_( | 428 last_access_threshold_( |
426 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)), | 429 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)), |
427 delegate_(delegate), | 430 delegate_(delegate), |
428 last_statistic_record_time_(Time::Now()), | 431 last_statistic_record_time_(Time::Now()), |
429 keep_expired_cookies_(false) { | 432 keep_expired_cookies_(false) { |
430 InitializeHistograms(); | 433 InitializeHistograms(); |
431 SetDefaultCookieableSchemes(); | 434 SetDefaultCookieableSchemes(); |
432 } | 435 } |
433 | 436 |
434 CookieMonster::CookieMonster(PersistentCookieStore* store, | 437 CookieMonster::CookieMonster(PersistentCookieStore* store, |
435 Delegate* delegate, | 438 Delegate* delegate, |
436 int last_access_threshold_milliseconds) | 439 int last_access_threshold_milliseconds) |
437 : initialized_(false), | 440 : initialized_(false), |
| 441 loaded_(false), |
438 expiry_and_key_scheme_(expiry_and_key_default_), | 442 expiry_and_key_scheme_(expiry_and_key_default_), |
439 store_(store), | 443 store_(store), |
440 last_access_threshold_(base::TimeDelta::FromMilliseconds( | 444 last_access_threshold_(base::TimeDelta::FromMilliseconds( |
441 last_access_threshold_milliseconds)), | 445 last_access_threshold_milliseconds)), |
442 delegate_(delegate), | 446 delegate_(delegate), |
443 last_statistic_record_time_(base::Time::Now()), | 447 last_statistic_record_time_(base::Time::Now()), |
444 keep_expired_cookies_(false) { | 448 keep_expired_cookies_(false) { |
445 InitializeHistograms(); | 449 InitializeHistograms(); |
446 SetDefaultCookieableSchemes(); | 450 SetDefaultCookieableSchemes(); |
447 } | 451 } |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
561 // the following check would be reasonable: | 565 // the following check would be reasonable: |
562 // NOTREACHED() << "Cookie exploded expiration failed: " << time_string; | 566 // NOTREACHED() << "Cookie exploded expiration failed: " << time_string; |
563 | 567 |
564 return Time(); | 568 return Time(); |
565 } | 569 } |
566 | 570 |
567 bool CookieMonster::DomainIsHostOnly(const std::string& domain_string) { | 571 bool CookieMonster::DomainIsHostOnly(const std::string& domain_string) { |
568 return (domain_string.empty() || domain_string[0] != '.'); | 572 return (domain_string.empty() || domain_string[0] != '.'); |
569 } | 573 } |
570 | 574 |
| 575 // Task classes for queueing the coming request. |
| 576 |
| 577 class CookieMonster::CookieMonsterTask |
| 578 : public base::RefCountedThreadSafe<CookieMonsterTask> { |
| 579 public: |
| 580 // Runs the task and invokes the client callback on the thread that |
| 581 // originally constructed the task. |
| 582 virtual void Run() = 0; |
| 583 |
| 584 protected: |
| 585 explicit CookieMonsterTask(CookieMonster* cookie_monster); |
| 586 virtual ~CookieMonsterTask(); |
| 587 |
| 588 // Invokes the callback immediately, if the current thread is the one |
| 589 // that originated the task, or queues the callback for execution on the |
| 590 // appropriate thread. Maintains a reference to this CookieMonsterTask |
| 591 // instance until the callback completes. |
| 592 void InvokeCallback(base::Closure callback); |
| 593 |
| 594 CookieMonster* cookie_monster() { |
| 595 return cookie_monster_; |
| 596 } |
| 597 |
| 598 friend class base::RefCountedThreadSafe<CookieMonsterTask>; |
| 599 |
| 600 private: |
| 601 CookieMonster* cookie_monster_; |
| 602 scoped_refptr<base::MessageLoopProxy> thread_; |
| 603 |
| 604 DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask); |
| 605 }; |
| 606 |
| 607 CookieMonster::CookieMonsterTask::CookieMonsterTask( |
| 608 CookieMonster* cookie_monster) |
| 609 : cookie_monster_(cookie_monster), |
| 610 thread_(base::MessageLoopProxy::current()) { } |
| 611 |
| 612 CookieMonster::CookieMonsterTask::~CookieMonsterTask() { } |
| 613 |
| 614 // Unfortunately, one cannot re-bind a Callback with parameters into a closure. |
| 615 // Therefore, the closure passed to InvokeCallback is a clumsy binding of |
| 616 // Callback::Run on a wrapped Callback instance. Since Callback is not |
| 617 // reference counted, we bind to an instance that is a member of the |
| 618 // CookieMonsterTask subclass. Then, we cannot simply post the callback to a |
| 619 // message loop because the underlying instance may be destroyed (along with the |
| 620 // CookieMonsterTask instance) in the interim. Therefore, we post a callback |
| 621 // bound to the CookieMonsterTask, which *is* reference counted (thus preventing |
| 622 // destruction of the original callback), and which invokes the closure (which |
| 623 // invokes the original callback with the returned data). |
| 624 void CookieMonster::CookieMonsterTask::InvokeCallback(base::Closure callback) { |
| 625 if (thread_->BelongsToCurrentThread()) { |
| 626 callback.Run(); |
| 627 } else { |
| 628 thread_->PostTask(FROM_HERE, base::Bind( |
| 629 &CookieMonster::CookieMonsterTask::InvokeCallback, this, callback)); |
| 630 } |
| 631 } |
| 632 |
| 633 // Task class for SetCookieWithDetails call. |
| 634 class CookieMonster::SetCookieWithDetailsTask |
| 635 : public CookieMonster::CookieMonsterTask { |
| 636 public: |
| 637 SetCookieWithDetailsTask( |
| 638 CookieMonster* cookie_monster, |
| 639 const GURL& url, const std::string& name, const std::string& value, |
| 640 const std::string& domain, const std::string& path, |
| 641 const base::Time& expiration_time, bool secure, bool http_only, |
| 642 const CookieMonster::SetCookiesCallback& callback) |
| 643 : CookieMonsterTask(cookie_monster), |
| 644 url_(url), |
| 645 name_(name), |
| 646 value_(value), |
| 647 domain_(domain), |
| 648 path_(path), |
| 649 expiration_time_(expiration_time), |
| 650 secure_(secure), |
| 651 http_only_(http_only), |
| 652 callback_(callback) { } |
| 653 |
| 654 virtual void Run() OVERRIDE; |
| 655 |
| 656 private: |
| 657 GURL url_; |
| 658 std::string name_; |
| 659 std::string value_; |
| 660 std::string domain_; |
| 661 std::string path_; |
| 662 base::Time expiration_time_; |
| 663 bool secure_; |
| 664 bool http_only_; |
| 665 CookieMonster::SetCookiesCallback callback_; |
| 666 |
| 667 DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask); |
| 668 }; |
| 669 |
| 670 void CookieMonster::SetCookieWithDetailsTask::Run() { |
| 671 bool success = this->cookie_monster()-> |
| 672 SetCookieWithDetails(url_, name_, value_, domain_, path_, |
| 673 expiration_time_, secure_, http_only_); |
| 674 if (!callback_.is_null()) { |
| 675 this->InvokeCallback(base::Bind(&CookieMonster::SetCookiesCallback::Run, |
| 676 base::Unretained(&callback_), success)); |
| 677 } |
| 678 } |
| 679 |
| 680 // Task class for GetAllCookies call. |
| 681 class CookieMonster::GetAllCookiesTask |
| 682 : public CookieMonster::CookieMonsterTask { |
| 683 public: |
| 684 GetAllCookiesTask(CookieMonster* cookie_monster, |
| 685 const CookieMonster::GetCookieListCallback& callback) |
| 686 : CookieMonsterTask(cookie_monster), |
| 687 callback_(callback) { } |
| 688 |
| 689 virtual void Run() OVERRIDE; |
| 690 |
| 691 private: |
| 692 CookieMonster::GetCookieListCallback callback_; |
| 693 |
| 694 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask); |
| 695 }; |
| 696 |
| 697 void CookieMonster::GetAllCookiesTask::Run() { |
| 698 if (!callback_.is_null()) { |
| 699 CookieList cookies = this->cookie_monster()->GetAllCookies(); |
| 700 this->InvokeCallback(base::Bind(&CookieMonster::GetCookieListCallback::Run, |
| 701 base::Unretained(&callback_), cookies)); |
| 702 } |
| 703 } |
| 704 |
| 705 // Task class for GetAllCookiesForURLWithOptions call. |
| 706 class CookieMonster::GetAllCookiesForURLWithOptionsTask |
| 707 : public CookieMonster::CookieMonsterTask { |
| 708 public: |
| 709 GetAllCookiesForURLWithOptionsTask( |
| 710 CookieMonster* cookie_monster, |
| 711 const GURL& url, |
| 712 const CookieOptions& options, |
| 713 const CookieMonster::GetCookieListCallback& callback) |
| 714 : CookieMonsterTask(cookie_monster), |
| 715 url_(url), |
| 716 options_(options), |
| 717 callback_(callback) { } |
| 718 |
| 719 virtual void Run() OVERRIDE; |
| 720 |
| 721 private: |
| 722 GURL url_; |
| 723 CookieOptions options_; |
| 724 CookieMonster::GetCookieListCallback callback_; |
| 725 |
| 726 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesForURLWithOptionsTask); |
| 727 }; |
| 728 |
| 729 void CookieMonster::GetAllCookiesForURLWithOptionsTask::Run() { |
| 730 if (!callback_.is_null()) { |
| 731 CookieList cookies = this->cookie_monster()-> |
| 732 GetAllCookiesForURLWithOptions(url_, options_); |
| 733 this->InvokeCallback(base::Bind(&CookieMonster::GetCookieListCallback::Run, |
| 734 base::Unretained(&callback_), cookies)); |
| 735 } |
| 736 } |
| 737 |
| 738 // Task class for DeleteAll call. |
| 739 class CookieMonster::DeleteAllTask : public CookieMonster::CookieMonsterTask { |
| 740 public: |
| 741 DeleteAllTask(CookieMonster* cookie_monster, |
| 742 const CookieMonster::DeleteCallback& callback) |
| 743 : CookieMonsterTask(cookie_monster), |
| 744 callback_(callback) { } |
| 745 |
| 746 virtual void Run() OVERRIDE; |
| 747 |
| 748 private: |
| 749 CookieMonster::DeleteCallback callback_; |
| 750 |
| 751 DISALLOW_COPY_AND_ASSIGN(DeleteAllTask); |
| 752 }; |
| 753 |
| 754 void CookieMonster::DeleteAllTask::Run() { |
| 755 int num_deleted = this->cookie_monster()->DeleteAll(true); |
| 756 if (!callback_.is_null()) { |
| 757 this->InvokeCallback(base::Bind(&CookieMonster::DeleteCallback::Run, |
| 758 base::Unretained(&callback_), num_deleted)); |
| 759 } |
| 760 } |
| 761 |
| 762 // Task class for DeleteAllCreatedBetween call. |
| 763 class CookieMonster::DeleteAllCreatedBetweenTask |
| 764 : public CookieMonster::CookieMonsterTask { |
| 765 public: |
| 766 DeleteAllCreatedBetweenTask( |
| 767 CookieMonster* cookie_monster, |
| 768 const Time& delete_begin, |
| 769 const Time& delete_end, |
| 770 const CookieMonster::DeleteCallback& callback) |
| 771 : CookieMonsterTask(cookie_monster), |
| 772 delete_begin_(delete_begin), |
| 773 delete_end_(delete_end), |
| 774 callback_(callback) { } |
| 775 |
| 776 virtual void Run() OVERRIDE; |
| 777 |
| 778 private: |
| 779 Time delete_begin_; |
| 780 Time delete_end_; |
| 781 CookieMonster::DeleteCallback callback_; |
| 782 |
| 783 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask); |
| 784 }; |
| 785 |
| 786 void CookieMonster::DeleteAllCreatedBetweenTask::Run() { |
| 787 int num_deleted = this->cookie_monster()-> |
| 788 DeleteAllCreatedBetween(delete_begin_, delete_end_); |
| 789 if (!callback_.is_null()) { |
| 790 this->InvokeCallback(base::Bind(&CookieMonster::DeleteCallback::Run, |
| 791 base::Unretained(&callback_), num_deleted)); |
| 792 } |
| 793 } |
| 794 |
| 795 // Task class for DeleteAllForHost call. |
| 796 class CookieMonster::DeleteAllForHostTask |
| 797 : public CookieMonster::CookieMonsterTask { |
| 798 public: |
| 799 DeleteAllForHostTask(CookieMonster* cookie_monster, |
| 800 const GURL& url, |
| 801 const CookieMonster::DeleteCallback& callback) |
| 802 : CookieMonsterTask(cookie_monster), |
| 803 url_(url), |
| 804 callback_(callback) { } |
| 805 |
| 806 virtual void Run() OVERRIDE; |
| 807 |
| 808 private: |
| 809 GURL url_; |
| 810 CookieMonster::DeleteCallback callback_; |
| 811 |
| 812 DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask); |
| 813 }; |
| 814 |
| 815 void CookieMonster::DeleteAllForHostTask::Run() { |
| 816 int num_deleted = this->cookie_monster()->DeleteAllForHost(url_); |
| 817 if (!callback_.is_null()) { |
| 818 this->InvokeCallback(base::Bind(&CookieMonster::DeleteCallback::Run, |
| 819 base::Unretained(&callback_), num_deleted)); |
| 820 } |
| 821 } |
| 822 |
| 823 // Task class for DeleteCanonicalCookie call. |
| 824 class CookieMonster::DeleteCanonicalCookieTask |
| 825 : public CookieMonster::CookieMonsterTask { |
| 826 public: |
| 827 DeleteCanonicalCookieTask( |
| 828 CookieMonster* cookie_monster, |
| 829 const CookieMonster::CanonicalCookie& cookie, |
| 830 const CookieMonster::DeleteCookieCallback& callback) |
| 831 : CookieMonsterTask(cookie_monster), |
| 832 cookie_(cookie), |
| 833 callback_(callback) { } |
| 834 |
| 835 virtual void Run() OVERRIDE; |
| 836 |
| 837 private: |
| 838 CookieMonster::CanonicalCookie cookie_; |
| 839 CookieMonster::DeleteCookieCallback callback_; |
| 840 |
| 841 DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask); |
| 842 }; |
| 843 |
| 844 void CookieMonster::DeleteCanonicalCookieTask::Run() { |
| 845 bool result = this->cookie_monster()->DeleteCanonicalCookie(cookie_); |
| 846 if (!callback_.is_null()) { |
| 847 this->InvokeCallback(base::Bind(&CookieMonster::DeleteCookieCallback::Run, |
| 848 base::Unretained(&callback_), result)); |
| 849 } |
| 850 } |
| 851 |
| 852 // Task class for SetCookieWithOptions call. |
| 853 class CookieMonster::SetCookieWithOptionsTask |
| 854 : public CookieMonster::CookieMonsterTask { |
| 855 public: |
| 856 SetCookieWithOptionsTask(CookieMonster* cookie_monster, |
| 857 const GURL& url, |
| 858 const std::string& cookie_line, |
| 859 const CookieOptions& options, |
| 860 const CookieMonster::SetCookiesCallback& callback) |
| 861 : CookieMonsterTask(cookie_monster), |
| 862 url_(url), |
| 863 cookie_line_(cookie_line), |
| 864 options_(options), |
| 865 callback_(callback) { } |
| 866 |
| 867 virtual void Run() OVERRIDE; |
| 868 |
| 869 private: |
| 870 GURL url_; |
| 871 std::string cookie_line_; |
| 872 CookieOptions options_; |
| 873 CookieMonster::SetCookiesCallback callback_; |
| 874 |
| 875 DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask); |
| 876 }; |
| 877 |
| 878 void CookieMonster::SetCookieWithOptionsTask::Run() { |
| 879 bool result = this->cookie_monster()-> |
| 880 SetCookieWithOptions(url_, cookie_line_, options_); |
| 881 if (!callback_.is_null()) { |
| 882 this->InvokeCallback(base::Bind(&CookieMonster::SetCookiesCallback::Run, |
| 883 base::Unretained(&callback_), result)); |
| 884 } |
| 885 } |
| 886 |
| 887 // Task class for GetCookiesWithOptions call. |
| 888 class CookieMonster::GetCookiesWithOptionsTask |
| 889 : public CookieMonster::CookieMonsterTask { |
| 890 public: |
| 891 GetCookiesWithOptionsTask(CookieMonster* cookie_monster, |
| 892 GURL url, |
| 893 const CookieOptions& options, |
| 894 const CookieMonster::GetCookiesCallback& callback) |
| 895 : CookieMonsterTask(cookie_monster), |
| 896 url_(url), |
| 897 options_(options), |
| 898 callback_(callback) { } |
| 899 |
| 900 virtual void Run() OVERRIDE; |
| 901 |
| 902 private: |
| 903 GURL url_; |
| 904 CookieOptions options_; |
| 905 CookieMonster::GetCookiesCallback callback_; |
| 906 |
| 907 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask); |
| 908 }; |
| 909 |
| 910 void CookieMonster::GetCookiesWithOptionsTask::Run() { |
| 911 std::string cookie = this->cookie_monster()-> |
| 912 GetCookiesWithOptions(url_, options_); |
| 913 if (!callback_.is_null()) { |
| 914 this->InvokeCallback(base::Bind(&CookieMonster::GetCookiesCallback::Run, |
| 915 base::Unretained(&callback_), cookie)); |
| 916 } |
| 917 } |
| 918 |
| 919 // Task class for GetCookiesWithInfo call. |
| 920 class CookieMonster::GetCookiesWithInfoTask |
| 921 : public CookieMonster::CookieMonsterTask { |
| 922 public: |
| 923 GetCookiesWithInfoTask(CookieMonster* cookie_monster, |
| 924 const GURL& url, |
| 925 const CookieOptions& options, |
| 926 const CookieMonster::GetCookieInfoCallback& callback) |
| 927 : CookieMonsterTask(cookie_monster), |
| 928 url_(url), |
| 929 options_(options), |
| 930 callback_(callback) { } |
| 931 |
| 932 virtual void Run() OVERRIDE; |
| 933 |
| 934 private: |
| 935 GURL url_; |
| 936 CookieOptions options_; |
| 937 CookieMonster::GetCookieInfoCallback callback_; |
| 938 |
| 939 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithInfoTask); |
| 940 }; |
| 941 |
| 942 void CookieMonster::GetCookiesWithInfoTask::Run() { |
| 943 if (!callback_.is_null()) { |
| 944 std::string cookie_line; |
| 945 std::vector<CookieMonster::CookieInfo> cookie_infos; |
| 946 this->cookie_monster()-> |
| 947 GetCookiesWithInfo(url_, options_, &cookie_line, &cookie_infos); |
| 948 this->InvokeCallback(base::Bind(&CookieMonster::GetCookieInfoCallback::Run, |
| 949 base::Unretained(&callback_), |
| 950 cookie_line, cookie_infos)); |
| 951 } |
| 952 } |
| 953 |
| 954 // Task class for DeleteCookie call. |
| 955 class CookieMonster::DeleteCookieTask |
| 956 : public CookieMonster::CookieMonsterTask { |
| 957 public: |
| 958 DeleteCookieTask(CookieMonster* cookie_monster, |
| 959 GURL url, |
| 960 const std::string& cookie_name, |
| 961 const base::Closure& callback) |
| 962 : CookieMonsterTask(cookie_monster), |
| 963 url_(url), |
| 964 cookie_name_(cookie_name), |
| 965 callback_(callback) { } |
| 966 |
| 967 virtual void Run() OVERRIDE; |
| 968 |
| 969 private: |
| 970 GURL url_; |
| 971 std::string cookie_name_; |
| 972 base::Closure callback_; |
| 973 |
| 974 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask); |
| 975 }; |
| 976 |
| 977 void CookieMonster::DeleteCookieTask::Run() { |
| 978 this->cookie_monster()->DeleteCookie(url_, cookie_name_); |
| 979 if (!callback_.is_null()) { |
| 980 this->InvokeCallback(callback_); |
| 981 } |
| 982 } |
| 983 |
| 984 // Asynchronous CookieMonster API |
| 985 |
| 986 void CookieMonster::SetCookieWithDetailsAsync( |
| 987 const GURL& url, const std::string& name, const std::string& value, |
| 988 const std::string& domain, const std::string& path, |
| 989 const base::Time& expiration_time, bool secure, bool http_only, |
| 990 const SetCookiesCallback& callback) { |
| 991 scoped_refptr<SetCookieWithDetailsTask> task = |
| 992 new SetCookieWithDetailsTask(this, url, name, value, domain, path, |
| 993 expiration_time, secure, http_only, |
| 994 callback); |
| 995 |
| 996 DoCookieTask(task); |
| 997 } |
| 998 |
| 999 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) { |
| 1000 scoped_refptr<GetAllCookiesTask> task = |
| 1001 new GetAllCookiesTask(this, callback); |
| 1002 |
| 1003 DoCookieTask(task); |
| 1004 } |
| 1005 |
| 1006 |
| 1007 void CookieMonster::GetAllCookiesForURLWithOptionsAsync( |
| 1008 const GURL& url, |
| 1009 const CookieOptions& options, |
| 1010 const GetCookieListCallback& callback) { |
| 1011 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = |
| 1012 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); |
| 1013 |
| 1014 DoCookieTask(task); |
| 1015 } |
| 1016 |
| 1017 void CookieMonster::GetAllCookiesForURLAsync( |
| 1018 const GURL& url, const GetCookieListCallback& callback) { |
| 1019 CookieOptions options; |
| 1020 options.set_include_httponly(); |
| 1021 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = |
| 1022 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); |
| 1023 |
| 1024 DoCookieTask(task); |
| 1025 } |
| 1026 |
| 1027 void CookieMonster::DeleteAllAsync(const DeleteCallback& callback) { |
| 1028 scoped_refptr<DeleteAllTask> task = |
| 1029 new DeleteAllTask(this, callback); |
| 1030 |
| 1031 DoCookieTask(task); |
| 1032 } |
| 1033 |
| 1034 void CookieMonster::DeleteAllCreatedBetweenAsync( |
| 1035 const Time& delete_begin, const Time& delete_end, |
| 1036 const DeleteCallback& callback) { |
| 1037 scoped_refptr<DeleteAllCreatedBetweenTask> task = |
| 1038 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end, |
| 1039 callback); |
| 1040 |
| 1041 DoCookieTask(task); |
| 1042 } |
| 1043 |
| 1044 void CookieMonster::DeleteAllForHostAsync( |
| 1045 const GURL& url, const DeleteCallback& callback) { |
| 1046 scoped_refptr<DeleteAllForHostTask> task = |
| 1047 new DeleteAllForHostTask(this, url, callback); |
| 1048 |
| 1049 DoCookieTask(task); |
| 1050 } |
| 1051 |
| 1052 void CookieMonster::DeleteCanonicalCookieAsync( |
| 1053 const CanonicalCookie& cookie, |
| 1054 const DeleteCookieCallback& callback) { |
| 1055 scoped_refptr<DeleteCanonicalCookieTask> task = |
| 1056 new DeleteCanonicalCookieTask(this, cookie, callback); |
| 1057 |
| 1058 DoCookieTask(task); |
| 1059 } |
| 1060 |
| 1061 void CookieMonster::SetCookieWithOptionsAsync( |
| 1062 const GURL& url, |
| 1063 const std::string& cookie_line, |
| 1064 const CookieOptions& options, |
| 1065 const SetCookiesCallback& callback) { |
| 1066 scoped_refptr<SetCookieWithOptionsTask> task = |
| 1067 new SetCookieWithOptionsTask(this, url, cookie_line, options, callback); |
| 1068 |
| 1069 DoCookieTask(task); |
| 1070 } |
| 1071 |
| 1072 void CookieMonster::GetCookiesWithOptionsAsync( |
| 1073 const GURL& url, |
| 1074 const CookieOptions& options, |
| 1075 const GetCookiesCallback& callback) { |
| 1076 scoped_refptr<GetCookiesWithOptionsTask> task = |
| 1077 new GetCookiesWithOptionsTask(this, url, options, callback); |
| 1078 |
| 1079 DoCookieTask(task); |
| 1080 } |
| 1081 |
| 1082 void CookieMonster::GetCookiesWithInfoAsync( |
| 1083 const GURL& url, |
| 1084 const CookieOptions& options, |
| 1085 const GetCookieInfoCallback& callback) { |
| 1086 scoped_refptr<GetCookiesWithInfoTask> task = |
| 1087 new GetCookiesWithInfoTask(this, url, options, callback); |
| 1088 |
| 1089 DoCookieTask(task); |
| 1090 } |
| 1091 |
| 1092 void CookieMonster::DeleteCookieAsync(const GURL& url, |
| 1093 const std::string& cookie_name, |
| 1094 const base::Closure& callback) { |
| 1095 scoped_refptr<DeleteCookieTask> task = |
| 1096 new DeleteCookieTask(this, url, cookie_name, callback); |
| 1097 |
| 1098 DoCookieTask(task); |
| 1099 } |
| 1100 |
| 1101 void CookieMonster::DoCookieTask( |
| 1102 const scoped_refptr<CookieMonsterTask>& task_item) { |
| 1103 InitIfNecessary(); |
| 1104 |
| 1105 { |
| 1106 base::AutoLock autolock(lock_); |
| 1107 if (!loaded_) { |
| 1108 queue_.push(task_item); |
| 1109 return; |
| 1110 } |
| 1111 } |
| 1112 |
| 1113 task_item->Run(); |
| 1114 } |
| 1115 |
571 bool CookieMonster::SetCookieWithDetails( | 1116 bool CookieMonster::SetCookieWithDetails( |
572 const GURL& url, const std::string& name, const std::string& value, | 1117 const GURL& url, const std::string& name, const std::string& value, |
573 const std::string& domain, const std::string& path, | 1118 const std::string& domain, const std::string& path, |
574 const base::Time& expiration_time, bool secure, bool http_only) { | 1119 const base::Time& expiration_time, bool secure, bool http_only) { |
575 base::AutoLock autolock(lock_); | 1120 base::AutoLock autolock(lock_); |
576 | 1121 |
577 if (!HasCookieableScheme(url)) | 1122 if (!HasCookieableScheme(url)) |
578 return false; | 1123 return false; |
579 | 1124 |
580 InitIfNecessary(); | |
581 | |
582 Time creation_time = CurrentTime(); | 1125 Time creation_time = CurrentTime(); |
583 last_time_seen_ = creation_time; | 1126 last_time_seen_ = creation_time; |
584 | 1127 |
585 // TODO(abarth): Take these values as parameters. | 1128 // TODO(abarth): Take these values as parameters. |
586 std::string mac_key; | 1129 std::string mac_key; |
587 std::string mac_algorithm; | 1130 std::string mac_algorithm; |
588 | 1131 |
589 scoped_ptr<CanonicalCookie> cc; | 1132 scoped_ptr<CanonicalCookie> cc; |
590 cc.reset(CanonicalCookie::Create( | 1133 cc.reset(CanonicalCookie::Create( |
591 url, name, value, domain, path, | 1134 url, name, value, domain, path, |
592 mac_key, mac_algorithm, | 1135 mac_key, mac_algorithm, |
593 creation_time, expiration_time, | 1136 creation_time, expiration_time, |
594 secure, http_only)); | 1137 secure, http_only)); |
595 | 1138 |
596 if (!cc.get()) | 1139 if (!cc.get()) |
597 return false; | 1140 return false; |
598 | 1141 |
599 CookieOptions options; | 1142 CookieOptions options; |
600 options.set_include_httponly(); | 1143 options.set_include_httponly(); |
601 return SetCanonicalCookie(&cc, creation_time, options); | 1144 return SetCanonicalCookie(&cc, creation_time, options); |
602 } | 1145 } |
603 | 1146 |
604 void CookieMonster::SetCookieWithDetailsAsync( | |
605 const GURL& url, const std::string& name, const std::string& value, | |
606 const std::string& domain, const std::string& path, | |
607 const base::Time& expiration_time, bool secure, bool http_only, | |
608 const SetCookiesCallback& callback) { | |
609 bool success_ = SetCookieWithDetails(url, name, value, domain, path, | |
610 expiration_time, secure, http_only); | |
611 if (!callback.is_null()) | |
612 callback.Run(success_); | |
613 } | |
614 | |
615 bool CookieMonster::InitializeFrom(const CookieList& list) { | 1147 bool CookieMonster::InitializeFrom(const CookieList& list) { |
616 base::AutoLock autolock(lock_); | 1148 base::AutoLock autolock(lock_); |
617 InitIfNecessary(); | 1149 InitIfNecessary(); |
618 for (net::CookieList::const_iterator iter = list.begin(); | 1150 for (net::CookieList::const_iterator iter = list.begin(); |
619 iter != list.end(); ++iter) { | 1151 iter != list.end(); ++iter) { |
620 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie; | 1152 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie; |
621 cookie.reset(new net::CookieMonster::CanonicalCookie(*iter)); | 1153 cookie.reset(new net::CookieMonster::CanonicalCookie(*iter)); |
622 net::CookieOptions options; | 1154 net::CookieOptions options; |
623 options.set_include_httponly(); | 1155 options.set_include_httponly(); |
624 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), | 1156 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), |
625 options)) { | 1157 options)) { |
626 return false; | 1158 return false; |
627 } | 1159 } |
628 } | 1160 } |
629 return true; | 1161 return true; |
630 } | 1162 } |
631 | 1163 |
632 CookieList CookieMonster::GetAllCookies() { | 1164 CookieList CookieMonster::GetAllCookies() { |
633 base::AutoLock autolock(lock_); | 1165 base::AutoLock autolock(lock_); |
634 InitIfNecessary(); | |
635 | 1166 |
636 // This function is being called to scrape the cookie list for management UI | 1167 // This function is being called to scrape the cookie list for management UI |
637 // or similar. We shouldn't show expired cookies in this list since it will | 1168 // or similar. We shouldn't show expired cookies in this list since it will |
638 // just be confusing to users, and this function is called rarely enough (and | 1169 // just be confusing to users, and this function is called rarely enough (and |
639 // is already slow enough) that it's OK to take the time to garbage collect | 1170 // is already slow enough) that it's OK to take the time to garbage collect |
640 // the expired cookies now. | 1171 // the expired cookies now. |
641 // | 1172 // |
642 // Note that this does not prune cookies to be below our limits (if we've | 1173 // Note that this does not prune cookies to be below our limits (if we've |
643 // exceeded them) the way that calling GarbageCollect() would. | 1174 // exceeded them) the way that calling GarbageCollect() would. |
644 GarbageCollectExpired(Time::Now(), | 1175 GarbageCollectExpired(Time::Now(), |
(...skipping 10 matching lines...) Expand all Loading... |
655 | 1186 |
656 CookieList cookie_list; | 1187 CookieList cookie_list; |
657 cookie_list.reserve(cookie_ptrs.size()); | 1188 cookie_list.reserve(cookie_ptrs.size()); |
658 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); | 1189 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); |
659 it != cookie_ptrs.end(); ++it) | 1190 it != cookie_ptrs.end(); ++it) |
660 cookie_list.push_back(**it); | 1191 cookie_list.push_back(**it); |
661 | 1192 |
662 return cookie_list; | 1193 return cookie_list; |
663 } | 1194 } |
664 | 1195 |
665 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) { | |
666 if (!callback.is_null()) | |
667 callback.Run(GetAllCookies()); | |
668 } | |
669 | |
670 CookieList CookieMonster::GetAllCookiesForURLWithOptions( | 1196 CookieList CookieMonster::GetAllCookiesForURLWithOptions( |
671 const GURL& url, | 1197 const GURL& url, |
672 const CookieOptions& options) { | 1198 const CookieOptions& options) { |
673 base::AutoLock autolock(lock_); | 1199 base::AutoLock autolock(lock_); |
674 InitIfNecessary(); | |
675 | 1200 |
676 std::vector<CanonicalCookie*> cookie_ptrs; | 1201 std::vector<CanonicalCookie*> cookie_ptrs; |
677 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs); | 1202 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs); |
678 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); | 1203 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); |
679 | 1204 |
680 CookieList cookies; | 1205 CookieList cookies; |
681 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); | 1206 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); |
682 it != cookie_ptrs.end(); it++) | 1207 it != cookie_ptrs.end(); it++) |
683 cookies.push_back(**it); | 1208 cookies.push_back(**it); |
684 | 1209 |
685 return cookies; | 1210 return cookies; |
686 } | 1211 } |
687 | 1212 |
688 void CookieMonster::GetAllCookiesForURLWithOptionsAsync( | |
689 const GURL& url, | |
690 const CookieOptions& options, | |
691 const GetCookieListCallback& callback) { | |
692 if (!callback.is_null()) | |
693 callback.Run(GetAllCookiesForURLWithOptions(url, options)); | |
694 } | |
695 | |
696 CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) { | 1213 CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) { |
697 CookieOptions options; | 1214 CookieOptions options; |
698 options.set_include_httponly(); | 1215 options.set_include_httponly(); |
699 | 1216 |
700 return GetAllCookiesForURLWithOptions(url, options); | 1217 return GetAllCookiesForURLWithOptions(url, options); |
701 } | 1218 } |
702 | 1219 |
703 void CookieMonster::GetAllCookiesForURLAsync( | |
704 const GURL& url, const GetCookieListCallback& callback) { | |
705 if (!callback.is_null()) | |
706 callback.Run(GetAllCookiesForURL(url)); | |
707 } | |
708 | |
709 int CookieMonster::DeleteAll(bool sync_to_store) { | 1220 int CookieMonster::DeleteAll(bool sync_to_store) { |
710 base::AutoLock autolock(lock_); | 1221 base::AutoLock autolock(lock_); |
711 if (sync_to_store) | |
712 InitIfNecessary(); | |
713 | 1222 |
714 int num_deleted = 0; | 1223 int num_deleted = 0; |
715 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { | 1224 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
716 CookieMap::iterator curit = it; | 1225 CookieMap::iterator curit = it; |
717 ++it; | 1226 ++it; |
718 InternalDeleteCookie(curit, sync_to_store, | 1227 InternalDeleteCookie(curit, sync_to_store, |
719 sync_to_store ? DELETE_COOKIE_EXPLICIT : | 1228 sync_to_store ? DELETE_COOKIE_EXPLICIT : |
720 DELETE_COOKIE_DONT_RECORD /* Destruction. */); | 1229 DELETE_COOKIE_DONT_RECORD /* Destruction. */); |
721 ++num_deleted; | 1230 ++num_deleted; |
722 } | 1231 } |
723 | 1232 |
724 return num_deleted; | 1233 return num_deleted; |
725 } | 1234 } |
726 | 1235 |
727 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin, | 1236 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin, |
728 const Time& delete_end, | 1237 const Time& delete_end) { |
729 bool sync_to_store) { | |
730 base::AutoLock autolock(lock_); | 1238 base::AutoLock autolock(lock_); |
731 InitIfNecessary(); | |
732 | 1239 |
733 int num_deleted = 0; | 1240 int num_deleted = 0; |
734 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { | 1241 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
735 CookieMap::iterator curit = it; | 1242 CookieMap::iterator curit = it; |
736 CanonicalCookie* cc = curit->second; | 1243 CanonicalCookie* cc = curit->second; |
737 ++it; | 1244 ++it; |
738 | 1245 |
739 if (cc->CreationDate() >= delete_begin && | 1246 if (cc->CreationDate() >= delete_begin && |
740 (delete_end.is_null() || cc->CreationDate() < delete_end)) { | 1247 (delete_end.is_null() || cc->CreationDate() < delete_end)) { |
741 InternalDeleteCookie(curit, sync_to_store, DELETE_COOKIE_EXPLICIT); | 1248 InternalDeleteCookie(curit, |
| 1249 true, /*sync_to_store*/ |
| 1250 DELETE_COOKIE_EXPLICIT); |
742 ++num_deleted; | 1251 ++num_deleted; |
743 } | 1252 } |
744 } | 1253 } |
745 | 1254 |
746 return num_deleted; | 1255 return num_deleted; |
747 } | 1256 } |
748 | 1257 |
749 void CookieMonster::DeleteAllCreatedBetweenAsync( | |
750 const Time& delete_begin, const Time& delete_end, | |
751 bool sync_to_store, | |
752 const DeleteCallback& callback) { | |
753 int num_deleted = DeleteAllCreatedBetween( | |
754 delete_begin, delete_end, sync_to_store); | |
755 if (!callback.is_null()) | |
756 callback.Run(num_deleted); | |
757 } | |
758 | |
759 int CookieMonster::DeleteAllForHost(const GURL& url) { | 1258 int CookieMonster::DeleteAllForHost(const GURL& url) { |
760 base::AutoLock autolock(lock_); | 1259 base::AutoLock autolock(lock_); |
761 InitIfNecessary(); | |
762 | 1260 |
763 if (!HasCookieableScheme(url)) | 1261 if (!HasCookieableScheme(url)) |
764 return 0; | 1262 return 0; |
765 | 1263 |
766 const std::string scheme(url.scheme()); | 1264 const std::string scheme(url.scheme()); |
767 const std::string host(url.host()); | 1265 const std::string host(url.host()); |
768 | 1266 |
769 // We store host cookies in the store by their canonical host name; | 1267 // We store host cookies in the store by their canonical host name; |
770 // domain cookies are stored with a leading ".". So this is a pretty | 1268 // domain cookies are stored with a leading ".". So this is a pretty |
771 // simple lookup and per-cookie delete. | 1269 // simple lookup and per-cookie delete. |
772 int num_deleted = 0; | 1270 int num_deleted = 0; |
773 for (CookieMapItPair its = cookies_.equal_range(GetKey(host)); | 1271 for (CookieMapItPair its = cookies_.equal_range(GetKey(host)); |
774 its.first != its.second;) { | 1272 its.first != its.second;) { |
775 CookieMap::iterator curit = its.first; | 1273 CookieMap::iterator curit = its.first; |
776 ++its.first; | 1274 ++its.first; |
777 | 1275 |
778 const CanonicalCookie* const cc = curit->second; | 1276 const CanonicalCookie* const cc = curit->second; |
779 | 1277 |
780 // Delete only on a match as a host cookie. | 1278 // Delete only on a match as a host cookie. |
781 if (cc->IsHostCookie() && cc->IsDomainMatch(scheme, host)) { | 1279 if (cc->IsHostCookie() && cc->IsDomainMatch(scheme, host)) { |
782 num_deleted++; | 1280 num_deleted++; |
783 | 1281 |
784 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); | 1282 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); |
785 } | 1283 } |
786 } | 1284 } |
787 return num_deleted; | 1285 return num_deleted; |
788 } | 1286 } |
789 | 1287 |
790 void CookieMonster::DeleteAllForHostAsync( | |
791 const GURL& url, const DeleteCallback& callback) { | |
792 int num_deleted = DeleteAllForHost(url); | |
793 if (!callback.is_null()) | |
794 callback.Run(num_deleted); | |
795 } | |
796 | |
797 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) { | 1288 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) { |
798 base::AutoLock autolock(lock_); | 1289 base::AutoLock autolock(lock_); |
799 InitIfNecessary(); | |
800 | 1290 |
801 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); | 1291 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); |
802 its.first != its.second; ++its.first) { | 1292 its.first != its.second; ++its.first) { |
803 // The creation date acts as our unique index... | 1293 // The creation date acts as our unique index... |
804 if (its.first->second->CreationDate() == cookie.CreationDate()) { | 1294 if (its.first->second->CreationDate() == cookie.CreationDate()) { |
805 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT); | 1295 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT); |
806 return true; | 1296 return true; |
807 } | 1297 } |
808 } | 1298 } |
809 return false; | 1299 return false; |
810 } | 1300 } |
811 | 1301 |
812 void CookieMonster::DeleteCanonicalCookieAsync( | |
813 const CanonicalCookie& cookie, | |
814 const DeleteCookieCallback& callback) { | |
815 bool result = DeleteCanonicalCookie(cookie); | |
816 if (!callback.is_null()) | |
817 callback.Run(result); | |
818 } | |
819 | |
820 void CookieMonster::SetCookieableSchemes( | 1302 void CookieMonster::SetCookieableSchemes( |
821 const char* schemes[], size_t num_schemes) { | 1303 const char* schemes[], size_t num_schemes) { |
822 base::AutoLock autolock(lock_); | 1304 base::AutoLock autolock(lock_); |
823 | 1305 |
824 // Cookieable Schemes must be set before first use of function. | 1306 // Cookieable Schemes must be set before first use of function. |
825 DCHECK(!initialized_); | 1307 DCHECK(!initialized_); |
826 | 1308 |
827 cookieable_schemes_.clear(); | 1309 cookieable_schemes_.clear(); |
828 cookieable_schemes_.insert(cookieable_schemes_.end(), | 1310 cookieable_schemes_.insert(cookieable_schemes_.end(), |
829 schemes, schemes + num_schemes); | 1311 schemes, schemes + num_schemes); |
(...skipping 28 matching lines...) Expand all Loading... |
858 | 1340 |
859 bool CookieMonster::SetCookieWithOptions(const GURL& url, | 1341 bool CookieMonster::SetCookieWithOptions(const GURL& url, |
860 const std::string& cookie_line, | 1342 const std::string& cookie_line, |
861 const CookieOptions& options) { | 1343 const CookieOptions& options) { |
862 base::AutoLock autolock(lock_); | 1344 base::AutoLock autolock(lock_); |
863 | 1345 |
864 if (!HasCookieableScheme(url)) { | 1346 if (!HasCookieableScheme(url)) { |
865 return false; | 1347 return false; |
866 } | 1348 } |
867 | 1349 |
868 InitIfNecessary(); | |
869 | |
870 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options); | 1350 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options); |
871 } | 1351 } |
872 | 1352 |
873 void CookieMonster::SetCookieWithOptionsAsync( | |
874 const GURL& url, | |
875 const std::string& cookie_line, | |
876 const CookieOptions& options, | |
877 const SetCookiesCallback& callback) { | |
878 bool result = SetCookieWithOptions(url, cookie_line, options); | |
879 if (!callback.is_null()) | |
880 callback.Run(result); | |
881 } | |
882 | |
883 std::string CookieMonster::GetCookiesWithOptions(const GURL& url, | 1353 std::string CookieMonster::GetCookiesWithOptions(const GURL& url, |
884 const CookieOptions& options) { | 1354 const CookieOptions& options) { |
885 base::AutoLock autolock(lock_); | 1355 base::AutoLock autolock(lock_); |
886 InitIfNecessary(); | |
887 | 1356 |
888 if (!HasCookieableScheme(url)) | 1357 if (!HasCookieableScheme(url)) |
889 return std::string(); | 1358 return std::string(); |
890 | 1359 |
891 TimeTicks start_time(TimeTicks::Now()); | 1360 TimeTicks start_time(TimeTicks::Now()); |
892 | 1361 |
893 std::vector<CanonicalCookie*> cookies; | 1362 std::vector<CanonicalCookie*> cookies; |
894 FindCookiesForHostAndDomain(url, options, true, &cookies); | 1363 FindCookiesForHostAndDomain(url, options, true, &cookies); |
895 std::sort(cookies.begin(), cookies.end(), CookieSorter); | 1364 std::sort(cookies.begin(), cookies.end(), CookieSorter); |
896 | 1365 |
897 std::string cookie_line = BuildCookieLine(cookies); | 1366 std::string cookie_line = BuildCookieLine(cookies); |
898 | 1367 |
899 histogram_time_get_->AddTime(TimeTicks::Now() - start_time); | 1368 histogram_time_get_->AddTime(TimeTicks::Now() - start_time); |
900 | 1369 |
901 VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line; | 1370 VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line; |
902 | 1371 |
903 return cookie_line; | 1372 return cookie_line; |
904 } | 1373 } |
905 | 1374 |
906 void CookieMonster::GetCookiesWithOptionsAsync( | |
907 const GURL& url, const CookieOptions& options, | |
908 const GetCookiesCallback& callback) { | |
909 std::string cookie = GetCookiesWithOptions(url, options); | |
910 if (!callback.is_null()) | |
911 callback.Run(cookie); | |
912 } | |
913 | |
914 void CookieMonster::GetCookiesWithInfo(const GURL& url, | 1375 void CookieMonster::GetCookiesWithInfo(const GURL& url, |
915 const CookieOptions& options, | 1376 const CookieOptions& options, |
916 std::string* cookie_line, | 1377 std::string* cookie_line, |
917 std::vector<CookieInfo>* cookie_infos) { | 1378 std::vector<CookieInfo>* cookie_infos) { |
918 DCHECK(cookie_line->empty()); | 1379 DCHECK(cookie_line->empty()); |
919 DCHECK(cookie_infos->empty()); | 1380 DCHECK(cookie_infos->empty()); |
920 | 1381 |
921 base::AutoLock autolock(lock_); | 1382 base::AutoLock autolock(lock_); |
922 InitIfNecessary(); | |
923 | 1383 |
924 if (!HasCookieableScheme(url)) | 1384 if (!HasCookieableScheme(url)) |
925 return; | 1385 return; |
926 | 1386 |
927 TimeTicks start_time(TimeTicks::Now()); | 1387 TimeTicks start_time(TimeTicks::Now()); |
928 | 1388 |
929 std::vector<CanonicalCookie*> cookies; | 1389 std::vector<CanonicalCookie*> cookies; |
930 FindCookiesForHostAndDomain(url, options, true, &cookies); | 1390 FindCookiesForHostAndDomain(url, options, true, &cookies); |
931 std::sort(cookies.begin(), cookies.end(), CookieSorter); | 1391 std::sort(cookies.begin(), cookies.end(), CookieSorter); |
932 *cookie_line = BuildCookieLine(cookies); | 1392 *cookie_line = BuildCookieLine(cookies); |
933 | 1393 |
934 histogram_time_get_->AddTime(TimeTicks::Now() - start_time); | 1394 histogram_time_get_->AddTime(TimeTicks::Now() - start_time); |
935 | 1395 |
936 TimeTicks mac_start_time = TimeTicks::Now(); | 1396 TimeTicks mac_start_time = TimeTicks::Now(); |
937 BuildCookieInfoList(cookies, cookie_infos); | 1397 BuildCookieInfoList(cookies, cookie_infos); |
938 histogram_time_mac_->AddTime(TimeTicks::Now() - mac_start_time); | 1398 histogram_time_mac_->AddTime(TimeTicks::Now() - mac_start_time); |
939 } | 1399 } |
940 | 1400 |
941 void CookieMonster::GetCookiesWithInfoAsync( | |
942 const GURL& url, | |
943 const CookieOptions& options, | |
944 const GetCookieInfoCallback& callback) { | |
945 std::string cookie_line; | |
946 std::vector<CookieInfo> cookie_infos; | |
947 GetCookiesWithInfo(url, options, &cookie_line, &cookie_infos); | |
948 | |
949 if (!callback.is_null()) | |
950 callback.Run(&cookie_line, &cookie_infos); | |
951 } | |
952 | |
953 void CookieMonster::DeleteCookie(const GURL& url, | 1401 void CookieMonster::DeleteCookie(const GURL& url, |
954 const std::string& cookie_name) { | 1402 const std::string& cookie_name) { |
955 base::AutoLock autolock(lock_); | 1403 base::AutoLock autolock(lock_); |
956 InitIfNecessary(); | |
957 | 1404 |
958 if (!HasCookieableScheme(url)) | 1405 if (!HasCookieableScheme(url)) |
959 return; | 1406 return; |
960 | 1407 |
961 CookieOptions options; | 1408 CookieOptions options; |
962 options.set_include_httponly(); | 1409 options.set_include_httponly(); |
963 // Get the cookies for this host and its domain(s). | 1410 // Get the cookies for this host and its domain(s). |
964 std::vector<CanonicalCookie*> cookies; | 1411 std::vector<CanonicalCookie*> cookies; |
965 FindCookiesForHostAndDomain(url, options, true, &cookies); | 1412 FindCookiesForHostAndDomain(url, options, true, &cookies); |
966 std::set<CanonicalCookie*> matching_cookies; | 1413 std::set<CanonicalCookie*> matching_cookies; |
967 | 1414 |
968 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); | 1415 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); |
969 it != cookies.end(); ++it) { | 1416 it != cookies.end(); ++it) { |
970 if ((*it)->Name() != cookie_name) | 1417 if ((*it)->Name() != cookie_name) |
971 continue; | 1418 continue; |
972 if (url.path().find((*it)->Path())) | 1419 if (url.path().find((*it)->Path())) |
973 continue; | 1420 continue; |
974 matching_cookies.insert(*it); | 1421 matching_cookies.insert(*it); |
975 } | 1422 } |
976 | 1423 |
977 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { | 1424 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
978 CookieMap::iterator curit = it; | 1425 CookieMap::iterator curit = it; |
979 ++it; | 1426 ++it; |
980 if (matching_cookies.find(curit->second) != matching_cookies.end()) { | 1427 if (matching_cookies.find(curit->second) != matching_cookies.end()) { |
981 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); | 1428 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); |
982 } | 1429 } |
983 } | 1430 } |
984 } | 1431 } |
985 | 1432 |
986 void CookieMonster::DeleteCookieAsync(const GURL& url, | |
987 const std::string& cookie_name, | |
988 const base::Closure& callback) { | |
989 DeleteCookie(url, cookie_name); | |
990 if (!callback.is_null()) | |
991 callback.Run(); | |
992 } | |
993 | |
994 CookieMonster* CookieMonster::GetCookieMonster() { | 1433 CookieMonster* CookieMonster::GetCookieMonster() { |
995 return this; | 1434 return this; |
996 } | 1435 } |
997 | 1436 |
998 CookieMonster::~CookieMonster() { | 1437 CookieMonster::~CookieMonster() { |
999 DeleteAll(false); | 1438 DeleteAll(false); |
1000 } | 1439 } |
1001 | 1440 |
1002 bool CookieMonster::SetCookieWithCreationTime(const GURL& url, | 1441 bool CookieMonster::SetCookieWithCreationTime(const GURL& url, |
1003 const std::string& cookie_line, | 1442 const std::string& cookie_line, |
1004 const base::Time& creation_time) { | 1443 const base::Time& creation_time) { |
| 1444 DCHECK(!store_) << "This method is only to be used by unit-tests."; |
1005 base::AutoLock autolock(lock_); | 1445 base::AutoLock autolock(lock_); |
1006 | 1446 |
1007 if (!HasCookieableScheme(url)) { | 1447 if (!HasCookieableScheme(url)) { |
1008 return false; | 1448 return false; |
1009 } | 1449 } |
1010 | 1450 |
1011 InitIfNecessary(); | 1451 InitIfNecessary(); |
1012 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time, | 1452 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time, |
1013 CookieOptions()); | 1453 CookieOptions()); |
1014 } | 1454 } |
1015 | 1455 |
1016 void CookieMonster::InitStore() { | 1456 void CookieMonster::InitStore() { |
1017 DCHECK(store_) << "Store must exist to initialize"; | 1457 DCHECK(store_) << "Store must exist to initialize"; |
1018 | 1458 |
1019 TimeTicks beginning_time(TimeTicks::Now()); | 1459 // We bind in the current time so that we can report the wall-clock time for |
| 1460 // loading cookies. |
| 1461 store_->Load(base::Bind(&CookieMonster::OnLoaded, this, TimeTicks::Now())); |
| 1462 } |
1020 | 1463 |
| 1464 void CookieMonster::OnLoaded(TimeTicks beginning_time, |
| 1465 const std::vector<CanonicalCookie*>& cookies) { |
| 1466 StoreLoadedCookies(cookies); |
| 1467 histogram_time_load_->AddTime(TimeTicks::Now() - beginning_time); |
| 1468 |
| 1469 // Invoke the task queue of cookie request. |
| 1470 InvokeQueue(); |
| 1471 } |
| 1472 |
| 1473 void CookieMonster::StoreLoadedCookies( |
| 1474 const std::vector<CanonicalCookie*>& cookies) { |
1021 // Initialize the store and sync in any saved persistent cookies. We don't | 1475 // Initialize the store and sync in any saved persistent cookies. We don't |
1022 // care if it's expired, insert it so it can be garbage collected, removed, | 1476 // care if it's expired, insert it so it can be garbage collected, removed, |
1023 // and sync'd. | 1477 // and sync'd. |
1024 std::vector<CanonicalCookie*> cookies; | 1478 base::AutoLock autolock(lock_); |
1025 // Reserve space for the maximum amount of cookies a database should have. | |
1026 // This prevents multiple vector growth / copies as we append cookies. | |
1027 cookies.reserve(kMaxCookies); | |
1028 store_->Load(&cookies); | |
1029 | 1479 |
1030 // Avoid ever letting cookies with duplicate creation times into the store; | 1480 // Avoid ever letting cookies with duplicate creation times into the store; |
1031 // that way we don't have to worry about what sections of code are safe | 1481 // that way we don't have to worry about what sections of code are safe |
1032 // to call while it's in that state. | 1482 // to call while it's in that state. |
1033 std::set<int64> creation_times; | 1483 std::set<int64> creation_times; |
1034 | 1484 |
1035 // Presumably later than any access time in the store. | 1485 // Presumably later than any access time in the store. |
1036 Time earliest_access_time; | 1486 Time earliest_access_time; |
1037 | 1487 |
1038 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); | 1488 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); |
(...skipping 18 matching lines...) Expand all Loading... |
1057 delete (*it); | 1507 delete (*it); |
1058 } | 1508 } |
1059 } | 1509 } |
1060 earliest_access_time_= earliest_access_time; | 1510 earliest_access_time_= earliest_access_time; |
1061 | 1511 |
1062 // After importing cookies from the PersistentCookieStore, verify that | 1512 // After importing cookies from the PersistentCookieStore, verify that |
1063 // none of our other constraints are violated. | 1513 // none of our other constraints are violated. |
1064 // | 1514 // |
1065 // In particular, the backing store might have given us duplicate cookies. | 1515 // In particular, the backing store might have given us duplicate cookies. |
1066 EnsureCookiesMapIsValid(); | 1516 EnsureCookiesMapIsValid(); |
| 1517 } |
1067 | 1518 |
1068 histogram_time_load_->AddTime(TimeTicks::Now() - beginning_time); | 1519 void CookieMonster::InvokeQueue() { |
| 1520 while (true) { |
| 1521 scoped_refptr<CookieMonsterTask> request_task; |
| 1522 { |
| 1523 base::AutoLock autolock(lock_); |
| 1524 if (queue_.empty()) { |
| 1525 loaded_ = true; |
| 1526 break; |
| 1527 } |
| 1528 request_task = queue_.front(); |
| 1529 queue_.pop(); |
| 1530 } |
| 1531 request_task->Run(); |
| 1532 } |
1069 } | 1533 } |
1070 | 1534 |
1071 void CookieMonster::EnsureCookiesMapIsValid() { | 1535 void CookieMonster::EnsureCookiesMapIsValid() { |
1072 lock_.AssertAcquired(); | 1536 lock_.AssertAcquired(); |
1073 | 1537 |
1074 int num_duplicates_trimmed = 0; | 1538 int num_duplicates_trimmed = 0; |
1075 | 1539 |
1076 // Iterate through all the of the cookies, grouped by host. | 1540 // Iterate through all the of the cookies, grouped by host. |
1077 CookieMap::iterator prev_range_end = cookies_.begin(); | 1541 CookieMap::iterator prev_range_end = cookies_.begin(); |
1078 while (prev_range_end != cookies_.end()) { | 1542 while (prev_range_end != cookies_.end()) { |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1159 signature.name.c_str(), | 1623 signature.name.c_str(), |
1160 signature.domain.c_str(), | 1624 signature.domain.c_str(), |
1161 signature.path.c_str()); | 1625 signature.path.c_str()); |
1162 | 1626 |
1163 // Remove all the cookies identified by |dupes|. It is valid to delete our | 1627 // Remove all the cookies identified by |dupes|. It is valid to delete our |
1164 // list of iterators one at a time, since |cookies_| is a multimap (they | 1628 // list of iterators one at a time, since |cookies_| is a multimap (they |
1165 // don't invalidate existing iterators following deletion). | 1629 // don't invalidate existing iterators following deletion). |
1166 for (CookieSet::iterator dupes_it = dupes.begin(); | 1630 for (CookieSet::iterator dupes_it = dupes.begin(); |
1167 dupes_it != dupes.end(); | 1631 dupes_it != dupes.end(); |
1168 ++dupes_it) { | 1632 ++dupes_it) { |
1169 InternalDeleteCookie(*dupes_it, true /*sync_to_store*/, | 1633 InternalDeleteCookie(*dupes_it, true, |
1170 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE); | 1634 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE); |
1171 } | 1635 } |
1172 } | 1636 } |
1173 DCHECK_EQ(num_duplicates, num_duplicates_found); | 1637 DCHECK_EQ(num_duplicates, num_duplicates_found); |
1174 | 1638 |
1175 return num_duplicates; | 1639 return num_duplicates; |
1176 } | 1640 } |
1177 | 1641 |
1178 // Note: file must be the last scheme. | 1642 // Note: file must be the last scheme. |
1179 const char* CookieMonster::kDefaultCookieableSchemes[] = | 1643 const char* CookieMonster::kDefaultCookieableSchemes[] = |
(...skipping 633 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1813 if (!pairs_.empty()) { | 2277 if (!pairs_.empty()) { |
1814 is_valid_ = true; | 2278 is_valid_ = true; |
1815 SetupAttributes(); | 2279 SetupAttributes(); |
1816 } | 2280 } |
1817 } | 2281 } |
1818 | 2282 |
1819 CookieMonster::ParsedCookie::~ParsedCookie() { | 2283 CookieMonster::ParsedCookie::~ParsedCookie() { |
1820 } | 2284 } |
1821 | 2285 |
1822 // Returns true if |c| occurs in |chars| | 2286 // Returns true if |c| occurs in |chars| |
1823 // TODO maybe make this take an iterator, could check for end also? | 2287 // TODO(erikwright): maybe make this take an iterator, could check for end also? |
1824 static inline bool CharIsA(const char c, const char* chars) { | 2288 static inline bool CharIsA(const char c, const char* chars) { |
1825 return strchr(chars, c) != NULL; | 2289 return strchr(chars, c) != NULL; |
1826 } | 2290 } |
1827 // Seek the iterator to the first occurrence of a character in |chars|. | 2291 // Seek the iterator to the first occurrence of a character in |chars|. |
1828 // Returns true if it hit the end, false otherwise. | 2292 // Returns true if it hit the end, false otherwise. |
1829 static inline bool SeekTo(std::string::const_iterator* it, | 2293 static inline bool SeekTo(std::string::const_iterator* it, |
1830 const std::string::const_iterator& end, | 2294 const std::string::const_iterator& end, |
1831 const char* chars) { | 2295 const char* chars) { |
1832 for (; *it != end && !CharIsA(**it, chars); ++(*it)) {} | 2296 for (; *it != end && !CharIsA(**it, chars); ++(*it)) {} |
1833 return *it == end; | 2297 return *it == end; |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1969 // Parse all token/value pairs and populate pairs_. | 2433 // Parse all token/value pairs and populate pairs_. |
1970 void CookieMonster::ParsedCookie::ParseTokenValuePairs( | 2434 void CookieMonster::ParsedCookie::ParseTokenValuePairs( |
1971 const std::string& cookie_line) { | 2435 const std::string& cookie_line) { |
1972 pairs_.clear(); | 2436 pairs_.clear(); |
1973 | 2437 |
1974 // Ok, here we go. We should be expecting to be starting somewhere | 2438 // Ok, here we go. We should be expecting to be starting somewhere |
1975 // before the cookie line, not including any header name... | 2439 // before the cookie line, not including any header name... |
1976 std::string::const_iterator start = cookie_line.begin(); | 2440 std::string::const_iterator start = cookie_line.begin(); |
1977 std::string::const_iterator it = start; | 2441 std::string::const_iterator it = start; |
1978 | 2442 |
1979 // TODO Make sure we're stripping \r\n in the network code. Then we | 2443 // TODO(erikwright): Make sure we're stripping \r\n in the network code. |
1980 // can log any unexpected terminators. | 2444 // Then we can log any unexpected terminators. |
1981 std::string::const_iterator end = FindFirstTerminator(cookie_line); | 2445 std::string::const_iterator end = FindFirstTerminator(cookie_line); |
1982 | 2446 |
1983 for (int pair_num = 0; pair_num < kMaxPairs && it != end; ++pair_num) { | 2447 for (int pair_num = 0; pair_num < kMaxPairs && it != end; ++pair_num) { |
1984 TokenValuePair pair; | 2448 TokenValuePair pair; |
1985 | 2449 |
1986 std::string::const_iterator token_start, token_end; | 2450 std::string::const_iterator token_start, token_end; |
1987 if (!ParseToken(&it, end, &token_start, &token_end)) | 2451 if (!ParseToken(&it, end, &token_start, &token_end)) |
1988 break; | 2452 break; |
1989 | 2453 |
1990 if (it == end || *it != '=') { | 2454 if (it == end || *it != '=') { |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2300 std::string CookieMonster::CanonicalCookie::DebugString() const { | 2764 std::string CookieMonster::CanonicalCookie::DebugString() const { |
2301 return base::StringPrintf( | 2765 return base::StringPrintf( |
2302 "name: %s value: %s domain: %s path: %s creation: %" | 2766 "name: %s value: %s domain: %s path: %s creation: %" |
2303 PRId64, | 2767 PRId64, |
2304 name_.c_str(), value_.c_str(), | 2768 name_.c_str(), value_.c_str(), |
2305 domain_.c_str(), path_.c_str(), | 2769 domain_.c_str(), path_.c_str(), |
2306 static_cast<int64>(creation_date_.ToTimeT())); | 2770 static_cast<int64>(creation_date_.ToTimeT())); |
2307 } | 2771 } |
2308 | 2772 |
2309 } // namespace | 2773 } // namespace |
OLD | NEW |