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 "googleurl/src/gurl.h" | 60 #include "googleurl/src/gurl.h" |
59 #include "googleurl/src/url_canon.h" | 61 #include "googleurl/src/url_canon.h" |
60 #include "net/base/net_util.h" | 62 #include "net/base/net_util.h" |
61 #include "net/base/registry_controlled_domain.h" | 63 #include "net/base/registry_controlled_domain.h" |
62 | 64 |
63 using base::Time; | 65 using base::Time; |
64 using base::TimeDelta; | 66 using base::TimeDelta; |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
412 } | 414 } |
413 } | 415 } |
414 | 416 |
415 } // namespace | 417 } // namespace |
416 | 418 |
417 // static | 419 // static |
418 bool CookieMonster::enable_file_scheme_ = false; | 420 bool CookieMonster::enable_file_scheme_ = false; |
419 | 421 |
420 CookieMonster::CookieMonster(PersistentCookieStore* store, Delegate* delegate) | 422 CookieMonster::CookieMonster(PersistentCookieStore* store, Delegate* delegate) |
421 : initialized_(false), | 423 : initialized_(false), |
424 loaded_(false), | |
422 expiry_and_key_scheme_(expiry_and_key_default_), | 425 expiry_and_key_scheme_(expiry_and_key_default_), |
423 store_(store), | 426 store_(store), |
424 last_access_threshold_( | 427 last_access_threshold_( |
425 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)), | 428 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)), |
426 delegate_(delegate), | 429 delegate_(delegate), |
427 last_statistic_record_time_(Time::Now()), | 430 last_statistic_record_time_(Time::Now()), |
428 keep_expired_cookies_(false) { | 431 keep_expired_cookies_(false) { |
429 InitializeHistograms(); | 432 InitializeHistograms(); |
430 SetDefaultCookieableSchemes(); | 433 SetDefaultCookieableSchemes(); |
431 } | 434 } |
432 | 435 |
433 CookieMonster::CookieMonster(PersistentCookieStore* store, | 436 CookieMonster::CookieMonster(PersistentCookieStore* store, |
434 Delegate* delegate, | 437 Delegate* delegate, |
435 int last_access_threshold_milliseconds) | 438 int last_access_threshold_milliseconds) |
436 : initialized_(false), | 439 : initialized_(false), |
440 loaded_(false), | |
437 expiry_and_key_scheme_(expiry_and_key_default_), | 441 expiry_and_key_scheme_(expiry_and_key_default_), |
438 store_(store), | 442 store_(store), |
439 last_access_threshold_(base::TimeDelta::FromMilliseconds( | 443 last_access_threshold_(base::TimeDelta::FromMilliseconds( |
440 last_access_threshold_milliseconds)), | 444 last_access_threshold_milliseconds)), |
441 delegate_(delegate), | 445 delegate_(delegate), |
442 last_statistic_record_time_(base::Time::Now()), | 446 last_statistic_record_time_(base::Time::Now()), |
443 keep_expired_cookies_(false) { | 447 keep_expired_cookies_(false) { |
444 InitializeHistograms(); | 448 InitializeHistograms(); |
445 SetDefaultCookieableSchemes(); | 449 SetDefaultCookieableSchemes(); |
446 } | 450 } |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
560 // the following check would be reasonable: | 564 // the following check would be reasonable: |
561 // NOTREACHED() << "Cookie exploded expiration failed: " << time_string; | 565 // NOTREACHED() << "Cookie exploded expiration failed: " << time_string; |
562 | 566 |
563 return Time(); | 567 return Time(); |
564 } | 568 } |
565 | 569 |
566 bool CookieMonster::DomainIsHostOnly(const std::string& domain_string) { | 570 bool CookieMonster::DomainIsHostOnly(const std::string& domain_string) { |
567 return (domain_string.empty() || domain_string[0] != '.'); | 571 return (domain_string.empty() || domain_string[0] != '.'); |
568 } | 572 } |
569 | 573 |
574 // Task classes for queueing the coming request. | |
575 | |
576 class CookieMonsterTask | |
577 : public base::RefCountedThreadSafe<CookieMonsterTask> { | |
578 public: | |
579 virtual void Run() = 0; | |
erikwright (departed)
2011/08/12 18:18:42
// Runs the task and invokes the client callback o
ycxiao
2011/08/12 21:47:43
Done.
| |
580 | |
581 protected: | |
582 CookieMonsterTask(CookieMonster* cookie_monster); | |
583 virtual ~CookieMonsterTask(); | |
584 | |
585 void InvokeCallback(base::Closure callback); | |
erikwright (departed)
2011/08/12 18:18:42
// Invokes the callback immediately, if the curren
ycxiao
2011/08/12 21:47:43
Done.
| |
586 CookieMonster* cookie_monster() { | |
587 return cookie_monster_; | |
588 } | |
589 | |
590 friend class base::RefCountedThreadSafe<CookieMonsterTask>; | |
591 | |
592 private: | |
593 CookieMonster* cookie_monster_; | |
594 scoped_refptr<base::MessageLoopProxy> thread_; | |
595 | |
596 DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask); | |
597 }; | |
598 | |
599 CookieMonsterTask::CookieMonsterTask(CookieMonster* cookie_monster) | |
600 : cookie_monster_(cookie_monster), | |
601 thread_(base::MessageLoopProxy::CreateForCurrentThread()) { } | |
602 | |
603 CookieMonsterTask::~CookieMonsterTask() { } | |
604 | |
605 void CookieMonsterTask::InvokeCallback(base::Closure callback) { | |
606 if (!callback.is_null()) { | |
607 if (thread_->BelongsToCurrentThread()) { | |
608 callback.Run(); | |
609 } else { | |
610 thread_->PostTask(FROM_HERE, | |
611 base::Bind(&CookieMonsterTask::InvokeCallback, | |
612 this, callback)); | |
613 } | |
614 } | |
615 } | |
616 | |
617 // Task class for SetCookieWithDetails call. | |
618 class SetCookieWithDetailsTask : public CookieMonsterTask { | |
619 public: | |
620 SetCookieWithDetailsTask( | |
621 CookieMonster* cookie_monster, | |
622 const GURL& url, const std::string& name, const std::string& value, | |
623 const std::string& domain, const std::string& path, | |
624 const base::Time& expiration_time, bool secure, bool http_only, | |
625 const CookieMonster::SetCookiesCallback& callback) | |
626 : CookieMonsterTask(cookie_monster), | |
627 url_(url), | |
628 name_(name), | |
629 value_(value), | |
630 domain_(domain), | |
631 path_(path), | |
632 expiration_time_(expiration_time), | |
633 secure_(secure), | |
634 http_only_(http_only), | |
635 callback_(callback) { } | |
636 | |
637 virtual void Run() OVERRIDE; | |
638 | |
639 private: | |
640 GURL url_; | |
641 std::string name_; | |
642 std::string value_; | |
643 std::string domain_; | |
644 std::string path_; | |
645 base::Time expiration_time_; | |
646 bool secure_; | |
647 bool http_only_; | |
648 CookieMonster::SetCookiesCallback callback_; | |
649 | |
650 DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask); | |
651 }; | |
652 | |
653 void SetCookieWithDetailsTask::Run() { | |
654 bool success = this->cookie_monster()-> | |
655 SetCookieWithDetails(url_, name_, value_, domain_, path_, | |
656 expiration_time_, secure_, http_only_); | |
657 if (!callback_.is_null()) { | |
658 this->InvokeCallback(base::Bind(&CookieMonster::SetCookiesCallback::Run, | |
659 base::Unretained(&callback_), success)); | |
660 } | |
661 } | |
662 | |
663 // Task class for GetAllCookies call. | |
664 class GetAllCookiesTask : public CookieMonsterTask { | |
665 public: | |
666 GetAllCookiesTask(CookieMonster* cookie_monster, | |
667 const CookieMonster::GetCookieListCallback& callback) | |
668 : CookieMonsterTask(cookie_monster), | |
669 callback_(callback) { } | |
670 | |
671 virtual void Run() OVERRIDE; | |
672 | |
673 private: | |
674 CookieMonster::GetCookieListCallback callback_; | |
675 | |
676 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask); | |
677 }; | |
678 | |
679 void GetAllCookiesTask::Run() { | |
680 if (!callback_.is_null()) { | |
681 CookieList cookies = this->cookie_monster()->GetAllCookies(); | |
682 this->InvokeCallback(base::Bind(&CookieMonster::GetCookieListCallback::Run, | |
683 base::Unretained(&callback_), cookies)); | |
684 } | |
685 } | |
686 | |
687 // Task class for GetAllCookiesForURLWithOptions call. | |
688 class GetAllCookiesForURLWithOptionsTask : public CookieMonsterTask { | |
689 public: | |
690 GetAllCookiesForURLWithOptionsTask( | |
691 CookieMonster* cookie_monster, | |
692 const GURL& url, | |
693 const CookieOptions& options, | |
694 const CookieMonster::GetCookieListCallback& callback) | |
695 : CookieMonsterTask(cookie_monster), | |
696 url_(url), | |
697 options_(options), | |
698 callback_(callback) { } | |
699 | |
700 virtual void Run() OVERRIDE; | |
701 | |
702 private: | |
703 GURL url_; | |
704 CookieOptions options_; | |
705 CookieMonster::GetCookieListCallback callback_; | |
706 | |
707 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesForURLWithOptionsTask); | |
708 }; | |
709 | |
710 void GetAllCookiesForURLWithOptionsTask::Run() { | |
711 if (!callback_.is_null()) { | |
712 CookieList cookies = this->cookie_monster()-> | |
713 GetAllCookiesForURLWithOptions(url_, options_); | |
714 this->InvokeCallback(base::Bind(&CookieMonster::GetCookieListCallback::Run, | |
715 base::Unretained(&callback_), cookies)); | |
716 } | |
717 } | |
718 | |
719 // Task class for DeleteAll call. | |
720 class DeleteAllTask : public CookieMonsterTask { | |
721 public: | |
722 DeleteAllTask(CookieMonster* cookie_monster, | |
723 bool sync_to_store, | |
724 const CookieMonster::DeleteCallback& callback) | |
725 : CookieMonsterTask(cookie_monster), | |
726 sync_to_store_(sync_to_store), | |
727 callback_(callback) { } | |
728 | |
729 virtual void Run() OVERRIDE; | |
730 | |
731 private: | |
732 bool sync_to_store_; | |
733 CookieMonster::DeleteCallback callback_; | |
734 | |
735 DISALLOW_COPY_AND_ASSIGN(DeleteAllTask); | |
736 }; | |
737 | |
738 void DeleteAllTask::Run() { | |
739 int num_deleted = this->cookie_monster()->DeleteAll(sync_to_store_); | |
740 if (!callback_.is_null()) { | |
741 this->InvokeCallback(base::Bind(&CookieMonster::DeleteCallback::Run, | |
742 base::Unretained(&callback_), num_deleted)); | |
743 } | |
744 } | |
745 | |
746 // Task class for DeleteAllCreatedBetween call. | |
747 class DeleteAllCreatedBetweenTask : public CookieMonsterTask { | |
748 public: | |
749 DeleteAllCreatedBetweenTask( | |
750 CookieMonster* cookie_monster, | |
751 const Time& delete_begin, | |
752 const Time& delete_end, | |
753 bool sync_to_store, | |
754 const CookieMonster::DeleteCallback& callback) | |
755 : CookieMonsterTask(cookie_monster), | |
756 delete_begin_(delete_begin), | |
757 delete_end_(delete_end), | |
758 sync_to_store_(sync_to_store), | |
759 callback_(callback) { } | |
760 | |
761 virtual void Run() OVERRIDE; | |
762 | |
763 private: | |
764 Time delete_begin_; | |
765 Time delete_end_; | |
766 bool sync_to_store_; | |
767 CookieMonster::DeleteCallback callback_; | |
768 | |
769 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask); | |
770 }; | |
771 | |
772 void DeleteAllCreatedBetweenTask::Run() { | |
773 int num_deleted = this->cookie_monster()-> | |
774 DeleteAllCreatedBetween(delete_begin_, delete_end_, sync_to_store_); | |
775 if (!callback_.is_null()) { | |
776 this->InvokeCallback(base::Bind(&CookieMonster::DeleteCallback::Run, | |
777 base::Unretained(&callback_), num_deleted)); | |
778 } | |
779 } | |
780 | |
781 // Task class for DeleteAllForHost call. | |
782 class DeleteAllForHostTask : public CookieMonsterTask { | |
783 public: | |
784 DeleteAllForHostTask(CookieMonster* cookie_monster, | |
785 const GURL& url, | |
786 const CookieMonster::DeleteCallback& callback) | |
787 : CookieMonsterTask(cookie_monster), | |
788 url_(url), | |
789 callback_(callback) { } | |
790 | |
791 virtual void Run() OVERRIDE; | |
792 | |
793 private: | |
794 GURL url_; | |
795 CookieMonster::DeleteCallback callback_; | |
796 | |
797 DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask); | |
798 }; | |
799 | |
800 void DeleteAllForHostTask::Run() { | |
801 int num_deleted = this->cookie_monster()->DeleteAllForHost(url_); | |
802 if (!callback_.is_null()) { | |
803 this->InvokeCallback(base::Bind(&CookieMonster::DeleteCallback::Run, | |
804 base::Unretained(&callback_), num_deleted)); | |
805 } | |
806 } | |
807 | |
808 // Task class for DeleteCanonicalCookie call. | |
809 class DeleteCanonicalCookieTask : public CookieMonsterTask { | |
810 public: | |
811 DeleteCanonicalCookieTask( | |
812 CookieMonster* cookie_monster, | |
813 const CookieMonster::CanonicalCookie& cookie, | |
814 const CookieMonster::DeleteCookieCallback& callback) | |
815 : CookieMonsterTask(cookie_monster), | |
816 cookie_(cookie), | |
817 callback_(callback) { } | |
818 | |
819 virtual void Run() OVERRIDE; | |
820 | |
821 private: | |
822 CookieMonster::CanonicalCookie cookie_; | |
823 CookieMonster::DeleteCookieCallback callback_; | |
824 | |
825 DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask); | |
826 }; | |
827 | |
828 void DeleteCanonicalCookieTask::Run() { | |
829 bool result = this->cookie_monster()->DeleteCanonicalCookie(cookie_); | |
830 if (!callback_.is_null()) { | |
831 this->InvokeCallback(base::Bind(&CookieMonster::DeleteCookieCallback::Run, | |
832 base::Unretained(&callback_), result)); | |
833 } | |
834 } | |
835 | |
836 // Task class for SetCookieWithOptions call. | |
837 class SetCookieWithOptionsTask : public CookieMonsterTask { | |
838 public: | |
839 SetCookieWithOptionsTask(CookieMonster* cookie_monster, | |
840 const GURL& url, | |
841 const std::string& cookie_line, | |
842 const CookieOptions& options, | |
843 const CookieMonster::SetCookiesCallback& callback) | |
844 : CookieMonsterTask(cookie_monster), | |
845 url_(url), | |
846 cookie_line_(cookie_line), | |
847 options_(options), | |
848 callback_(callback) { } | |
849 | |
850 virtual void Run() OVERRIDE; | |
851 | |
852 private: | |
853 GURL url_; | |
854 std::string cookie_line_; | |
855 CookieOptions options_; | |
856 CookieMonster::SetCookiesCallback callback_; | |
857 | |
858 DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask); | |
859 }; | |
860 | |
861 void SetCookieWithOptionsTask::Run() { | |
862 bool result = this->cookie_monster()-> | |
863 SetCookieWithOptions(url_, cookie_line_, options_); | |
864 if (!callback_.is_null()) { | |
865 this->InvokeCallback(base::Bind(&CookieMonster::SetCookiesCallback::Run, | |
866 base::Unretained(&callback_), result)); | |
867 } | |
868 } | |
869 | |
870 // Task class for GetCookiesWithOptions call. | |
871 class GetCookiesWithOptionsTask : public CookieMonsterTask { | |
872 public: | |
873 GetCookiesWithOptionsTask(CookieMonster* cookie_monster, | |
874 GURL url, | |
875 const CookieOptions& options, | |
876 const CookieMonster::GetCookiesCallback& callback) | |
877 : CookieMonsterTask(cookie_monster), | |
878 url_(url), | |
879 options_(options), | |
880 callback_(callback) { } | |
881 | |
882 virtual void Run() OVERRIDE; | |
883 | |
884 private: | |
885 GURL url_; | |
886 CookieOptions options_; | |
887 CookieMonster::GetCookiesCallback callback_; | |
888 | |
889 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask); | |
890 }; | |
891 | |
892 void GetCookiesWithOptionsTask::Run() { | |
893 std::string cookie = this->cookie_monster()-> | |
894 GetCookiesWithOptions(url_, options_); | |
895 if (!callback_.is_null()) { | |
896 this->InvokeCallback(base::Bind(&CookieMonster::GetCookiesCallback::Run, | |
897 base::Unretained(&callback_), cookie)); | |
898 } | |
899 } | |
900 | |
901 // Task class for GetCookiesWithInfo call. | |
902 class GetCookiesWithInfoTask : public CookieMonsterTask { | |
903 public: | |
904 GetCookiesWithInfoTask(CookieMonster* cookie_monster, | |
905 GURL url, | |
906 const CookieOptions& options, | |
907 const CookieMonster::GetCookieInfoCallback& callback) | |
908 : CookieMonsterTask(cookie_monster), | |
909 url_(url), | |
910 options_(options), | |
911 callback_(callback) { } | |
912 | |
913 virtual void Run() OVERRIDE; | |
914 | |
915 private: | |
916 GURL url_; | |
917 CookieOptions options_; | |
918 CookieMonster::GetCookieInfoCallback callback_; | |
919 | |
920 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithInfoTask); | |
921 }; | |
922 | |
923 void GetCookiesWithInfoTask::Run() { | |
924 if (!callback_.is_null()) { | |
925 std::string cookie_line; | |
926 std::vector<CookieMonster::CookieInfo> cookie_infos; | |
927 this->cookie_monster()-> | |
928 GetCookiesWithInfo(url_, options_, &cookie_line, &cookie_infos); | |
929 this->InvokeCallback(base::Bind(&CookieMonster::GetCookieInfoCallback::Run, | |
930 base::Unretained(&callback_), | |
931 base::Unretained(&cookie_line), | |
932 base::Unretained(&cookie_infos))); | |
933 } | |
934 } | |
935 | |
936 // Task class for DeleteCookie call. | |
937 class DeleteCookieTask : public CookieMonsterTask { | |
938 public: | |
939 DeleteCookieTask(CookieMonster* cookie_monster, | |
940 GURL url, | |
941 const std::string& cookie_name, | |
942 const base::Closure& callback) | |
943 : CookieMonsterTask(cookie_monster), | |
944 url_(url), | |
945 cookie_name_(cookie_name), | |
946 callback_(callback) { } | |
947 | |
948 virtual void Run() OVERRIDE; | |
949 | |
950 private: | |
951 GURL url_; | |
952 std::string cookie_name_; | |
953 base::Closure callback_; | |
954 | |
955 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask); | |
956 }; | |
957 | |
958 void DeleteCookieTask::Run() { | |
959 this->cookie_monster()->DeleteCookie(url_, cookie_name_); | |
960 if (!callback_.is_null()) { | |
961 this->InvokeCallback(callback_); | |
962 } | |
963 } | |
964 | |
965 // Asynchronous CookieMonster API | |
966 | |
967 void CookieMonster::SetCookieWithDetailsAsync( | |
968 const GURL& url, const std::string& name, const std::string& value, | |
969 const std::string& domain, const std::string& path, | |
970 const base::Time& expiration_time, bool secure, bool http_only, | |
971 const SetCookiesCallback& callback) { | |
972 scoped_refptr<SetCookieWithDetailsTask> task = | |
973 new SetCookieWithDetailsTask(this, url, name, value, domain, path, | |
974 expiration_time, secure, http_only, | |
975 callback); | |
976 | |
977 DoCookieTask(task); | |
978 } | |
979 | |
980 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) { | |
981 scoped_refptr<GetAllCookiesTask> task = | |
982 new GetAllCookiesTask(this, callback); | |
983 | |
984 DoCookieTask(task); | |
985 } | |
986 | |
987 | |
988 void CookieMonster::GetAllCookiesForURLWithOptionsAsync( | |
989 const GURL& url, | |
990 const CookieOptions& options, | |
991 const GetCookieListCallback& callback) { | |
992 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = | |
993 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); | |
994 | |
995 DoCookieTask(task); | |
996 } | |
997 | |
998 void CookieMonster::GetAllCookiesForURLAsync( | |
999 const GURL& url, const GetCookieListCallback& callback) { | |
1000 CookieOptions options; | |
1001 options.set_include_httponly(); | |
1002 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = | |
1003 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); | |
1004 | |
1005 DoCookieTask(task); | |
1006 } | |
1007 | |
1008 void CookieMonster::DeleteAllAsync(bool sync_to_store, | |
1009 const DeleteCallback& callback) { | |
1010 scoped_refptr<DeleteAllTask> task = | |
1011 new DeleteAllTask(this, sync_to_store, callback); | |
1012 | |
1013 DoCookieTask(task); | |
1014 } | |
1015 | |
1016 void CookieMonster::DeleteAllCreatedBetweenAsync( | |
1017 const Time& delete_begin, const Time& delete_end, | |
1018 bool sync_to_store, | |
1019 const DeleteCallback& callback) { | |
1020 scoped_refptr<DeleteAllCreatedBetweenTask> task = | |
1021 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end, | |
1022 sync_to_store, callback); | |
1023 | |
1024 DoCookieTask(task); | |
1025 } | |
1026 | |
1027 void CookieMonster::DeleteAllForHostAsync( | |
1028 const GURL& url, const DeleteCallback& callback) { | |
1029 scoped_refptr<DeleteAllForHostTask> task = | |
1030 new DeleteAllForHostTask(this, url, callback); | |
1031 | |
1032 DoCookieTask(task); | |
1033 } | |
1034 | |
1035 void CookieMonster::DeleteCanonicalCookieAsync( | |
1036 const CanonicalCookie& cookie, | |
1037 const DeleteCookieCallback& callback) { | |
1038 scoped_refptr<DeleteCanonicalCookieTask> task = | |
1039 new DeleteCanonicalCookieTask(this, cookie, callback); | |
1040 | |
1041 DoCookieTask(task); | |
1042 } | |
1043 | |
1044 void CookieMonster::SetCookieWithOptionsAsync( | |
1045 const GURL& url, | |
1046 const std::string& cookie_line, | |
1047 const CookieOptions& options, | |
1048 const SetCookiesCallback& callback) { | |
1049 scoped_refptr<SetCookieWithOptionsTask> task = | |
1050 new SetCookieWithOptionsTask(this, url, cookie_line, options, callback); | |
1051 | |
1052 DoCookieTask(task); | |
1053 } | |
1054 | |
1055 void CookieMonster::GetCookiesWithOptionsAsync( | |
1056 const GURL& url, | |
1057 const CookieOptions& options, | |
1058 const GetCookiesCallback& callback) { | |
1059 scoped_refptr<GetCookiesWithOptionsTask> task = | |
1060 new GetCookiesWithOptionsTask(this, url, options, callback); | |
1061 | |
1062 DoCookieTask(task); | |
1063 } | |
1064 | |
1065 void CookieMonster::GetCookiesWithInfoAsync( | |
1066 const GURL& url, | |
1067 const CookieOptions& options, | |
1068 const GetCookieInfoCallback& callback) { | |
1069 scoped_refptr<GetCookiesWithInfoTask> task = | |
1070 new GetCookiesWithInfoTask(this, url, options, callback); | |
1071 | |
1072 DoCookieTask(task); | |
1073 } | |
1074 | |
1075 void CookieMonster::DeleteCookieAsync(const GURL& url, | |
1076 const std::string& cookie_name, | |
1077 const base::Closure& callback) { | |
1078 scoped_refptr<DeleteCookieTask> task = | |
1079 new DeleteCookieTask(this, url, cookie_name, callback); | |
1080 | |
1081 DoCookieTask(task); | |
1082 } | |
1083 | |
1084 void CookieMonster::DoCookieTask(scoped_refptr<CookieMonsterTask> task_item) { | |
1085 InitIfNecessary(); | |
1086 bool loaded = false; | |
1087 { | |
1088 base::AutoLock autolock(lock_); | |
1089 loaded = loaded_; | |
1090 } | |
1091 if (loaded) { | |
1092 task_item->Run(); | |
1093 } else { | |
1094 base::Closure task = base::Bind(&CookieMonsterTask::Run, task_item.get()); | |
erikwright (departed)
2011/08/12 18:18:42
If possible, do the base::Bind directly in the par
ycxiao
2011/08/12 21:47:43
Done.
| |
1095 base::AutoLock autolock(lock_); | |
1096 queue_.push(task); | |
1097 } | |
1098 } | |
1099 | |
570 bool CookieMonster::SetCookieWithDetails( | 1100 bool CookieMonster::SetCookieWithDetails( |
571 const GURL& url, const std::string& name, const std::string& value, | 1101 const GURL& url, const std::string& name, const std::string& value, |
572 const std::string& domain, const std::string& path, | 1102 const std::string& domain, const std::string& path, |
573 const base::Time& expiration_time, bool secure, bool http_only) { | 1103 const base::Time& expiration_time, bool secure, bool http_only) { |
574 base::AutoLock autolock(lock_); | 1104 base::AutoLock autolock(lock_); |
575 | 1105 |
576 if (!HasCookieableScheme(url)) | 1106 if (!HasCookieableScheme(url)) |
577 return false; | 1107 return false; |
578 | 1108 |
579 InitIfNecessary(); | |
580 | |
581 Time creation_time = CurrentTime(); | 1109 Time creation_time = CurrentTime(); |
582 last_time_seen_ = creation_time; | 1110 last_time_seen_ = creation_time; |
583 | 1111 |
584 // TODO(abarth): Take these values as parameters. | 1112 // TODO(abarth): Take these values as parameters. |
585 std::string mac_key; | 1113 std::string mac_key; |
586 std::string mac_algorithm; | 1114 std::string mac_algorithm; |
587 | 1115 |
588 scoped_ptr<CanonicalCookie> cc; | 1116 scoped_ptr<CanonicalCookie> cc; |
589 cc.reset(CanonicalCookie::Create( | 1117 cc.reset(CanonicalCookie::Create( |
590 url, name, value, domain, path, | 1118 url, name, value, domain, path, |
591 mac_key, mac_algorithm, | 1119 mac_key, mac_algorithm, |
592 creation_time, expiration_time, | 1120 creation_time, expiration_time, |
593 secure, http_only)); | 1121 secure, http_only)); |
594 | 1122 |
595 if (!cc.get()) | 1123 if (!cc.get()) |
596 return false; | 1124 return false; |
597 | 1125 |
598 CookieOptions options; | 1126 CookieOptions options; |
599 options.set_include_httponly(); | 1127 options.set_include_httponly(); |
600 return SetCanonicalCookie(&cc, creation_time, options); | 1128 return SetCanonicalCookie(&cc, creation_time, options); |
601 } | 1129 } |
602 | 1130 |
603 void CookieMonster::SetCookieWithDetailsAsync( | 1131 bool CookieMonster::InitializeFrom(const CookieList& list) { |
604 const GURL& url, const std::string& name, const std::string& value, | |
605 const std::string& domain, const std::string& path, | |
606 const base::Time& expiration_time, bool secure, bool http_only, | |
607 const SetCookiesCallback& callback) { | |
608 bool success_ = SetCookieWithDetails(url, name, value, domain, path, | |
609 expiration_time, secure, http_only); | |
610 if (!callback.is_null()) | |
611 callback.Run(success_); | |
612 } | |
613 | |
614 bool CookieMonster::InitializeFrom(CookieMonster* cookie_monster) { | |
615 net::CookieList list = cookie_monster->GetAllCookies(); | |
616 | |
617 base::AutoLock autolock(lock_); | 1132 base::AutoLock autolock(lock_); |
618 InitIfNecessary(); | 1133 InitIfNecessary(); |
619 for (net::CookieList::const_iterator iter = list.begin(); | 1134 for (net::CookieList::const_iterator iter = list.begin(); |
620 iter != list.end(); ++iter) { | 1135 iter != list.end(); ++iter) { |
621 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie; | 1136 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie; |
622 cookie.reset(new net::CookieMonster::CanonicalCookie(*iter)); | 1137 cookie.reset(new net::CookieMonster::CanonicalCookie(*iter)); |
623 net::CookieOptions options; | 1138 net::CookieOptions options; |
624 options.set_include_httponly(); | 1139 options.set_include_httponly(); |
625 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), | 1140 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), |
626 options)) { | 1141 options)) { |
627 return false; | 1142 return false; |
628 } | 1143 } |
629 } | 1144 } |
630 return true; | 1145 return true; |
631 } | 1146 } |
632 | 1147 |
633 CookieList CookieMonster::GetAllCookies() { | 1148 CookieList CookieMonster::GetAllCookies() { |
634 base::AutoLock autolock(lock_); | 1149 base::AutoLock autolock(lock_); |
635 InitIfNecessary(); | |
636 | 1150 |
637 // This function is being called to scrape the cookie list for management UI | 1151 // This function is being called to scrape the cookie list for management UI |
638 // or similar. We shouldn't show expired cookies in this list since it will | 1152 // or similar. We shouldn't show expired cookies in this list since it will |
639 // just be confusing to users, and this function is called rarely enough (and | 1153 // just be confusing to users, and this function is called rarely enough (and |
640 // is already slow enough) that it's OK to take the time to garbage collect | 1154 // is already slow enough) that it's OK to take the time to garbage collect |
641 // the expired cookies now. | 1155 // the expired cookies now. |
642 // | 1156 // |
643 // Note that this does not prune cookies to be below our limits (if we've | 1157 // Note that this does not prune cookies to be below our limits (if we've |
644 // exceeded them) the way that calling GarbageCollect() would. | 1158 // exceeded them) the way that calling GarbageCollect() would. |
645 GarbageCollectExpired(Time::Now(), | 1159 GarbageCollectExpired(Time::Now(), |
(...skipping 10 matching lines...) Expand all Loading... | |
656 | 1170 |
657 CookieList cookie_list; | 1171 CookieList cookie_list; |
658 cookie_list.reserve(cookie_ptrs.size()); | 1172 cookie_list.reserve(cookie_ptrs.size()); |
659 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); | 1173 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); |
660 it != cookie_ptrs.end(); ++it) | 1174 it != cookie_ptrs.end(); ++it) |
661 cookie_list.push_back(**it); | 1175 cookie_list.push_back(**it); |
662 | 1176 |
663 return cookie_list; | 1177 return cookie_list; |
664 } | 1178 } |
665 | 1179 |
666 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) { | |
667 if (!callback.is_null()) | |
668 callback.Run(GetAllCookies()); | |
669 } | |
670 | |
671 CookieList CookieMonster::GetAllCookiesForURLWithOptions( | 1180 CookieList CookieMonster::GetAllCookiesForURLWithOptions( |
672 const GURL& url, | 1181 const GURL& url, |
673 const CookieOptions& options) { | 1182 const CookieOptions& options) { |
674 base::AutoLock autolock(lock_); | 1183 base::AutoLock autolock(lock_); |
675 InitIfNecessary(); | |
676 | 1184 |
677 std::vector<CanonicalCookie*> cookie_ptrs; | 1185 std::vector<CanonicalCookie*> cookie_ptrs; |
678 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs); | 1186 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs); |
679 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); | 1187 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); |
680 | 1188 |
681 CookieList cookies; | 1189 CookieList cookies; |
682 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); | 1190 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); |
683 it != cookie_ptrs.end(); it++) | 1191 it != cookie_ptrs.end(); it++) |
684 cookies.push_back(**it); | 1192 cookies.push_back(**it); |
685 | 1193 |
686 return cookies; | 1194 return cookies; |
687 } | 1195 } |
688 | 1196 |
689 void CookieMonster::GetAllCookiesForURLWithOptionsAsync( | |
690 const GURL& url, | |
691 const CookieOptions& options, | |
692 const GetCookieListCallback& callback) { | |
693 if (!callback.is_null()) | |
694 callback.Run(GetAllCookiesForURLWithOptions(url, options)); | |
695 } | |
696 | |
697 CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) { | 1197 CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) { |
698 CookieOptions options; | 1198 CookieOptions options; |
699 options.set_include_httponly(); | 1199 options.set_include_httponly(); |
700 | 1200 |
701 return GetAllCookiesForURLWithOptions(url, options); | 1201 return GetAllCookiesForURLWithOptions(url, options); |
702 } | 1202 } |
703 | 1203 |
704 void CookieMonster::GetAllCookiesForURLAsync( | |
705 const GURL& url, const GetCookieListCallback& callback) { | |
706 if (!callback.is_null()) | |
707 callback.Run(GetAllCookiesForURL(url)); | |
708 } | |
709 | |
710 int CookieMonster::DeleteAll(bool sync_to_store) { | 1204 int CookieMonster::DeleteAll(bool sync_to_store) { |
711 base::AutoLock autolock(lock_); | 1205 base::AutoLock autolock(lock_); |
712 if (sync_to_store) | |
713 InitIfNecessary(); | |
714 | 1206 |
715 int num_deleted = 0; | 1207 int num_deleted = 0; |
716 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { | 1208 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
717 CookieMap::iterator curit = it; | 1209 CookieMap::iterator curit = it; |
718 ++it; | 1210 ++it; |
719 InternalDeleteCookie(curit, sync_to_store, | 1211 InternalDeleteCookie(curit, sync_to_store, |
720 sync_to_store ? DELETE_COOKIE_EXPLICIT : | 1212 sync_to_store ? DELETE_COOKIE_EXPLICIT : |
721 DELETE_COOKIE_DONT_RECORD /* Destruction. */); | 1213 DELETE_COOKIE_DONT_RECORD /* Destruction. */); |
722 ++num_deleted; | 1214 ++num_deleted; |
723 } | 1215 } |
724 | 1216 |
725 return num_deleted; | 1217 return num_deleted; |
726 } | 1218 } |
727 | 1219 |
728 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin, | 1220 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin, |
729 const Time& delete_end, | 1221 const Time& delete_end, |
730 bool sync_to_store) { | 1222 bool sync_to_store) { |
731 base::AutoLock autolock(lock_); | 1223 base::AutoLock autolock(lock_); |
732 InitIfNecessary(); | |
733 | 1224 |
734 int num_deleted = 0; | 1225 int num_deleted = 0; |
735 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { | 1226 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
736 CookieMap::iterator curit = it; | 1227 CookieMap::iterator curit = it; |
737 CanonicalCookie* cc = curit->second; | 1228 CanonicalCookie* cc = curit->second; |
738 ++it; | 1229 ++it; |
739 | 1230 |
740 if (cc->CreationDate() >= delete_begin && | 1231 if (cc->CreationDate() >= delete_begin && |
741 (delete_end.is_null() || cc->CreationDate() < delete_end)) { | 1232 (delete_end.is_null() || cc->CreationDate() < delete_end)) { |
742 InternalDeleteCookie(curit, sync_to_store, DELETE_COOKIE_EXPLICIT); | 1233 InternalDeleteCookie(curit, sync_to_store, DELETE_COOKIE_EXPLICIT); |
743 ++num_deleted; | 1234 ++num_deleted; |
744 } | 1235 } |
745 } | 1236 } |
746 | 1237 |
747 return num_deleted; | 1238 return num_deleted; |
748 } | 1239 } |
749 | 1240 |
750 void CookieMonster::DeleteAllCreatedBetweenAsync( | |
751 const Time& delete_begin, const Time& delete_end, | |
752 bool sync_to_store, | |
753 const DeleteCallback& callback) { | |
754 int num_deleted = DeleteAllCreatedBetween( | |
755 delete_begin, delete_end, sync_to_store); | |
756 if (!callback.is_null()) | |
757 callback.Run(num_deleted); | |
758 } | |
759 | |
760 int CookieMonster::DeleteAllForHost(const GURL& url) { | 1241 int CookieMonster::DeleteAllForHost(const GURL& url) { |
761 base::AutoLock autolock(lock_); | 1242 base::AutoLock autolock(lock_); |
762 InitIfNecessary(); | |
763 | 1243 |
764 if (!HasCookieableScheme(url)) | 1244 if (!HasCookieableScheme(url)) |
765 return 0; | 1245 return 0; |
766 | 1246 |
767 const std::string scheme(url.scheme()); | 1247 const std::string scheme(url.scheme()); |
768 const std::string host(url.host()); | 1248 const std::string host(url.host()); |
769 | 1249 |
770 // We store host cookies in the store by their canonical host name; | 1250 // We store host cookies in the store by their canonical host name; |
771 // domain cookies are stored with a leading ".". So this is a pretty | 1251 // domain cookies are stored with a leading ".". So this is a pretty |
772 // simple lookup and per-cookie delete. | 1252 // simple lookup and per-cookie delete. |
773 int num_deleted = 0; | 1253 int num_deleted = 0; |
774 for (CookieMapItPair its = cookies_.equal_range(GetKey(host)); | 1254 for (CookieMapItPair its = cookies_.equal_range(GetKey(host)); |
775 its.first != its.second;) { | 1255 its.first != its.second;) { |
776 CookieMap::iterator curit = its.first; | 1256 CookieMap::iterator curit = its.first; |
777 ++its.first; | 1257 ++its.first; |
778 | 1258 |
779 const CanonicalCookie* const cc = curit->second; | 1259 const CanonicalCookie* const cc = curit->second; |
780 | 1260 |
781 // Delete only on a match as a host cookie. | 1261 // Delete only on a match as a host cookie. |
782 if (cc->IsHostCookie() && cc->IsDomainMatch(scheme, host)) { | 1262 if (cc->IsHostCookie() && cc->IsDomainMatch(scheme, host)) { |
783 num_deleted++; | 1263 num_deleted++; |
784 | 1264 |
785 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); | 1265 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); |
786 } | 1266 } |
787 } | 1267 } |
788 return num_deleted; | 1268 return num_deleted; |
789 } | 1269 } |
790 | 1270 |
791 void CookieMonster::DeleteAllForHostAsync( | |
792 const GURL& url, const DeleteCallback& callback) { | |
793 int num_deleted = DeleteAllForHost(url); | |
794 if (!callback.is_null()) | |
795 callback.Run(num_deleted); | |
796 } | |
797 | |
798 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) { | 1271 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) { |
799 base::AutoLock autolock(lock_); | 1272 base::AutoLock autolock(lock_); |
800 InitIfNecessary(); | |
801 | 1273 |
802 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); | 1274 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); |
803 its.first != its.second; ++its.first) { | 1275 its.first != its.second; ++its.first) { |
804 // The creation date acts as our unique index... | 1276 // The creation date acts as our unique index... |
805 if (its.first->second->CreationDate() == cookie.CreationDate()) { | 1277 if (its.first->second->CreationDate() == cookie.CreationDate()) { |
806 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT); | 1278 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT); |
807 return true; | 1279 return true; |
808 } | 1280 } |
809 } | 1281 } |
810 return false; | 1282 return false; |
811 } | 1283 } |
812 | 1284 |
813 void CookieMonster::DeleteCanonicalCookieAsync( | |
814 const CanonicalCookie& cookie, | |
815 const DeleteCookieCallback& callback) { | |
816 bool result = DeleteCanonicalCookie(cookie); | |
817 if (!callback.is_null()) | |
818 callback.Run(result); | |
819 } | |
820 | |
821 void CookieMonster::SetCookieableSchemes( | 1285 void CookieMonster::SetCookieableSchemes( |
822 const char* schemes[], size_t num_schemes) { | 1286 const char* schemes[], size_t num_schemes) { |
823 base::AutoLock autolock(lock_); | 1287 base::AutoLock autolock(lock_); |
824 | 1288 |
825 // Cookieable Schemes must be set before first use of function. | 1289 // Cookieable Schemes must be set before first use of function. |
826 DCHECK(!initialized_); | 1290 DCHECK(!initialized_); |
827 | 1291 |
828 cookieable_schemes_.clear(); | 1292 cookieable_schemes_.clear(); |
829 cookieable_schemes_.insert(cookieable_schemes_.end(), | 1293 cookieable_schemes_.insert(cookieable_schemes_.end(), |
830 schemes, schemes + num_schemes); | 1294 schemes, schemes + num_schemes); |
(...skipping 28 matching lines...) Expand all Loading... | |
859 | 1323 |
860 bool CookieMonster::SetCookieWithOptions(const GURL& url, | 1324 bool CookieMonster::SetCookieWithOptions(const GURL& url, |
861 const std::string& cookie_line, | 1325 const std::string& cookie_line, |
862 const CookieOptions& options) { | 1326 const CookieOptions& options) { |
863 base::AutoLock autolock(lock_); | 1327 base::AutoLock autolock(lock_); |
864 | 1328 |
865 if (!HasCookieableScheme(url)) { | 1329 if (!HasCookieableScheme(url)) { |
866 return false; | 1330 return false; |
867 } | 1331 } |
868 | 1332 |
869 InitIfNecessary(); | |
870 | |
871 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options); | 1333 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options); |
872 } | 1334 } |
873 | 1335 |
874 void CookieMonster::SetCookieWithOptionsAsync( | |
875 const GURL& url, | |
876 const std::string& cookie_line, | |
877 const CookieOptions& options, | |
878 const SetCookiesCallback& callback) { | |
879 bool result = SetCookieWithOptions(url, cookie_line, options); | |
880 if (!callback.is_null()) | |
881 callback.Run(result); | |
882 } | |
883 | |
884 std::string CookieMonster::GetCookiesWithOptions(const GURL& url, | 1336 std::string CookieMonster::GetCookiesWithOptions(const GURL& url, |
885 const CookieOptions& options) { | 1337 const CookieOptions& options) { |
886 base::AutoLock autolock(lock_); | 1338 base::AutoLock autolock(lock_); |
887 InitIfNecessary(); | |
888 | 1339 |
889 if (!HasCookieableScheme(url)) | 1340 if (!HasCookieableScheme(url)) |
890 return std::string(); | 1341 return std::string(); |
891 | 1342 |
892 TimeTicks start_time(TimeTicks::Now()); | 1343 TimeTicks start_time(TimeTicks::Now()); |
893 | 1344 |
894 std::vector<CanonicalCookie*> cookies; | 1345 std::vector<CanonicalCookie*> cookies; |
895 FindCookiesForHostAndDomain(url, options, true, &cookies); | 1346 FindCookiesForHostAndDomain(url, options, true, &cookies); |
896 std::sort(cookies.begin(), cookies.end(), CookieSorter); | 1347 std::sort(cookies.begin(), cookies.end(), CookieSorter); |
897 | 1348 |
898 std::string cookie_line = BuildCookieLine(cookies); | 1349 std::string cookie_line = BuildCookieLine(cookies); |
899 | 1350 |
900 histogram_time_get_->AddTime(TimeTicks::Now() - start_time); | 1351 histogram_time_get_->AddTime(TimeTicks::Now() - start_time); |
901 | 1352 |
902 VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line; | 1353 VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line; |
903 | 1354 |
904 return cookie_line; | 1355 return cookie_line; |
905 } | 1356 } |
906 | 1357 |
907 void CookieMonster::GetCookiesWithOptionsAsync( | |
908 const GURL& url, const CookieOptions& options, | |
909 const GetCookiesCallback& callback) { | |
910 std::string cookie = GetCookiesWithOptions(url, options); | |
911 if (!callback.is_null()) | |
912 callback.Run(cookie); | |
913 } | |
914 | |
915 void CookieMonster::GetCookiesWithInfo(const GURL& url, | 1358 void CookieMonster::GetCookiesWithInfo(const GURL& url, |
916 const CookieOptions& options, | 1359 const CookieOptions& options, |
917 std::string* cookie_line, | 1360 std::string* cookie_line, |
918 std::vector<CookieInfo>* cookie_infos) { | 1361 std::vector<CookieInfo>* cookie_infos) { |
919 DCHECK(cookie_line->empty()); | 1362 DCHECK(cookie_line->empty()); |
920 DCHECK(cookie_infos->empty()); | 1363 DCHECK(cookie_infos->empty()); |
921 | 1364 |
922 base::AutoLock autolock(lock_); | 1365 base::AutoLock autolock(lock_); |
923 InitIfNecessary(); | |
924 | 1366 |
925 if (!HasCookieableScheme(url)) | 1367 if (!HasCookieableScheme(url)) |
926 return; | 1368 return; |
927 | 1369 |
928 TimeTicks start_time(TimeTicks::Now()); | 1370 TimeTicks start_time(TimeTicks::Now()); |
929 | 1371 |
930 std::vector<CanonicalCookie*> cookies; | 1372 std::vector<CanonicalCookie*> cookies; |
931 FindCookiesForHostAndDomain(url, options, true, &cookies); | 1373 FindCookiesForHostAndDomain(url, options, true, &cookies); |
932 std::sort(cookies.begin(), cookies.end(), CookieSorter); | 1374 std::sort(cookies.begin(), cookies.end(), CookieSorter); |
933 *cookie_line = BuildCookieLine(cookies); | 1375 *cookie_line = BuildCookieLine(cookies); |
934 | 1376 |
935 histogram_time_get_->AddTime(TimeTicks::Now() - start_time); | 1377 histogram_time_get_->AddTime(TimeTicks::Now() - start_time); |
936 | 1378 |
937 TimeTicks mac_start_time = TimeTicks::Now(); | 1379 TimeTicks mac_start_time = TimeTicks::Now(); |
938 BuildCookieInfoList(cookies, cookie_infos); | 1380 BuildCookieInfoList(cookies, cookie_infos); |
939 histogram_time_mac_->AddTime(TimeTicks::Now() - mac_start_time); | 1381 histogram_time_mac_->AddTime(TimeTicks::Now() - mac_start_time); |
940 } | 1382 } |
941 | 1383 |
942 void CookieMonster::GetCookiesWithInfoAsync( | |
943 const GURL& url, | |
944 const CookieOptions& options, | |
945 const GetCookieInfoCallback& callback) { | |
946 std::string cookie_line; | |
947 std::vector<CookieInfo> cookie_infos; | |
948 GetCookiesWithInfo(url, options, &cookie_line, &cookie_infos); | |
949 | |
950 if (!callback.is_null()) | |
951 callback.Run(&cookie_line, &cookie_infos); | |
952 } | |
953 | |
954 void CookieMonster::DeleteCookie(const GURL& url, | 1384 void CookieMonster::DeleteCookie(const GURL& url, |
955 const std::string& cookie_name) { | 1385 const std::string& cookie_name) { |
956 base::AutoLock autolock(lock_); | 1386 base::AutoLock autolock(lock_); |
957 InitIfNecessary(); | |
958 | 1387 |
959 if (!HasCookieableScheme(url)) | 1388 if (!HasCookieableScheme(url)) |
960 return; | 1389 return; |
961 | 1390 |
962 CookieOptions options; | 1391 CookieOptions options; |
963 options.set_include_httponly(); | 1392 options.set_include_httponly(); |
964 // Get the cookies for this host and its domain(s). | 1393 // Get the cookies for this host and its domain(s). |
965 std::vector<CanonicalCookie*> cookies; | 1394 std::vector<CanonicalCookie*> cookies; |
966 FindCookiesForHostAndDomain(url, options, true, &cookies); | 1395 FindCookiesForHostAndDomain(url, options, true, &cookies); |
967 std::set<CanonicalCookie*> matching_cookies; | 1396 std::set<CanonicalCookie*> matching_cookies; |
968 | 1397 |
969 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); | 1398 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); |
970 it != cookies.end(); ++it) { | 1399 it != cookies.end(); ++it) { |
971 if ((*it)->Name() != cookie_name) | 1400 if ((*it)->Name() != cookie_name) |
972 continue; | 1401 continue; |
973 if (url.path().find((*it)->Path())) | 1402 if (url.path().find((*it)->Path())) |
974 continue; | 1403 continue; |
975 matching_cookies.insert(*it); | 1404 matching_cookies.insert(*it); |
976 } | 1405 } |
977 | 1406 |
978 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { | 1407 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
979 CookieMap::iterator curit = it; | 1408 CookieMap::iterator curit = it; |
980 ++it; | 1409 ++it; |
981 if (matching_cookies.find(curit->second) != matching_cookies.end()) { | 1410 if (matching_cookies.find(curit->second) != matching_cookies.end()) { |
982 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); | 1411 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); |
983 } | 1412 } |
984 } | 1413 } |
985 } | 1414 } |
986 | 1415 |
987 void CookieMonster::DeleteCookieAsync(const GURL& url, | |
988 const std::string& cookie_name, | |
989 const base::Closure& callback) { | |
990 DeleteCookie(url, cookie_name); | |
991 if (!callback.is_null()) | |
992 callback.Run(); | |
993 } | |
994 | |
995 CookieMonster* CookieMonster::GetCookieMonster() { | 1416 CookieMonster* CookieMonster::GetCookieMonster() { |
996 return this; | 1417 return this; |
997 } | 1418 } |
998 | 1419 |
999 CookieMonster::~CookieMonster() { | 1420 CookieMonster::~CookieMonster() { |
1000 DeleteAll(false); | 1421 DeleteAll(false); |
1001 } | 1422 } |
1002 | 1423 |
1003 bool CookieMonster::SetCookieWithCreationTime(const GURL& url, | 1424 bool CookieMonster::SetCookieWithCreationTime(const GURL& url, |
1004 const std::string& cookie_line, | 1425 const std::string& cookie_line, |
1005 const base::Time& creation_time) { | 1426 const base::Time& creation_time) { |
1427 DCHECK(!store_) << "This method is only to be used by unit-tests."; | |
1006 base::AutoLock autolock(lock_); | 1428 base::AutoLock autolock(lock_); |
1007 | 1429 |
1008 if (!HasCookieableScheme(url)) { | 1430 if (!HasCookieableScheme(url)) { |
1009 return false; | 1431 return false; |
1010 } | 1432 } |
1011 | 1433 |
1012 InitIfNecessary(); | 1434 InitIfNecessary(); |
1013 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time, | 1435 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time, |
1014 CookieOptions()); | 1436 CookieOptions()); |
1015 } | 1437 } |
1016 | 1438 |
1017 void CookieMonster::InitStore() { | 1439 void CookieMonster::InitStore() { |
1018 DCHECK(store_) << "Store must exist to initialize"; | 1440 DCHECK(store_) << "Store must exist to initialize"; |
1441 store_->Load(base::Bind(&CookieMonster::OnLoaded, this)); | |
1442 } | |
1019 | 1443 |
1020 TimeTicks beginning_time(TimeTicks::Now()); | 1444 void CookieMonster::OnLoaded(const std::vector<CanonicalCookie*>& cookies) { |
1445 StoreLoadedCookies(cookies); | |
1446 // Invoke the task queue of cookie request. | |
1447 InvokeQueue(); | |
1448 } | |
1021 | 1449 |
1450 void CookieMonster::StoreLoadedCookies( | |
1451 const std::vector<CanonicalCookie*>& cookies) { | |
1022 // Initialize the store and sync in any saved persistent cookies. We don't | 1452 // Initialize the store and sync in any saved persistent cookies. We don't |
1023 // care if it's expired, insert it so it can be garbage collected, removed, | 1453 // care if it's expired, insert it so it can be garbage collected, removed, |
1024 // and sync'd. | 1454 // and sync'd. |
1025 std::vector<CanonicalCookie*> cookies; | 1455 base::AutoLock autolock(lock_); |
1026 // Reserve space for the maximum amount of cookies a database should have. | 1456 TimeTicks beginning_time(TimeTicks::Now()); |
1027 // This prevents multiple vector growth / copies as we append cookies. | |
1028 cookies.reserve(kMaxCookies); | |
1029 store_->Load(&cookies); | |
1030 | 1457 |
1031 // Avoid ever letting cookies with duplicate creation times into the store; | 1458 // Avoid ever letting cookies with duplicate creation times into the store; |
1032 // that way we don't have to worry about what sections of code are safe | 1459 // that way we don't have to worry about what sections of code are safe |
1033 // to call while it's in that state. | 1460 // to call while it's in that state. |
1034 std::set<int64> creation_times; | 1461 std::set<int64> creation_times; |
1035 | 1462 |
1036 // Presumably later than any access time in the store. | 1463 // Presumably later than any access time in the store. |
1037 Time earliest_access_time; | 1464 Time earliest_access_time; |
1038 | 1465 |
1039 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); | 1466 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); |
(...skipping 22 matching lines...) Expand all Loading... | |
1062 | 1489 |
1063 // After importing cookies from the PersistentCookieStore, verify that | 1490 // After importing cookies from the PersistentCookieStore, verify that |
1064 // none of our other constraints are violated. | 1491 // none of our other constraints are violated. |
1065 // | 1492 // |
1066 // In particular, the backing store might have given us duplicate cookies. | 1493 // In particular, the backing store might have given us duplicate cookies. |
1067 EnsureCookiesMapIsValid(); | 1494 EnsureCookiesMapIsValid(); |
1068 | 1495 |
1069 histogram_time_load_->AddTime(TimeTicks::Now() - beginning_time); | 1496 histogram_time_load_->AddTime(TimeTicks::Now() - beginning_time); |
1070 } | 1497 } |
1071 | 1498 |
1499 void CookieMonster::InvokeQueue() { | |
1500 while (true) { | |
1501 base::Closure request_task; | |
1502 { | |
1503 base::AutoLock autolock(lock_); | |
1504 if (queue_.empty()) { | |
1505 loaded_ = true; | |
1506 break; | |
1507 } | |
1508 request_task = queue_.front(); | |
1509 queue_.pop(); | |
1510 } | |
1511 request_task.Run(); | |
1512 } | |
1513 } | |
1514 | |
1072 void CookieMonster::EnsureCookiesMapIsValid() { | 1515 void CookieMonster::EnsureCookiesMapIsValid() { |
1073 lock_.AssertAcquired(); | 1516 lock_.AssertAcquired(); |
1074 | 1517 |
1075 int num_duplicates_trimmed = 0; | 1518 int num_duplicates_trimmed = 0; |
1076 | 1519 |
1077 // Iterate through all the of the cookies, grouped by host. | 1520 // Iterate through all the of the cookies, grouped by host. |
1078 CookieMap::iterator prev_range_end = cookies_.begin(); | 1521 CookieMap::iterator prev_range_end = cookies_.begin(); |
1079 while (prev_range_end != cookies_.end()) { | 1522 while (prev_range_end != cookies_.end()) { |
1080 CookieMap::iterator cur_range_begin = prev_range_end; | 1523 CookieMap::iterator cur_range_begin = prev_range_end; |
1081 const std::string key = cur_range_begin->first; // Keep a copy. | 1524 const std::string key = cur_range_begin->first; // Keep a copy. |
(...skipping 1063 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2145 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) | 2588 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) |
2146 // Mobile apps can sometimes be shut down without any warning, so the session | 2589 // Mobile apps can sometimes be shut down without any warning, so the session |
2147 // cookie has to be persistent and given a default expiration time. | 2590 // cookie has to be persistent and given a default expiration time. |
2148 expiry_date_ = base::Time::Now() + | 2591 expiry_date_ = base::Time::Now() + |
2149 base::TimeDelta::FromDays(kPersistentSessionCookieExpiryInDays); | 2592 base::TimeDelta::FromDays(kPersistentSessionCookieExpiryInDays); |
2150 has_expires_ = true; | 2593 has_expires_ = true; |
2151 #endif | 2594 #endif |
2152 } | 2595 } |
2153 | 2596 |
2154 CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create( | 2597 CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create( |
2598 const GURL& url, | |
2599 const ParsedCookie& pc) { | |
2600 if (!pc.IsValid()) { | |
2601 return NULL; | |
2602 } | |
2603 | |
2604 std::string domain_string; | |
2605 if (!GetCookieDomain(url, pc, &domain_string)) { | |
2606 return NULL; | |
2607 } | |
2608 std::string path_string = CanonPath(url, pc); | |
2609 std::string mac_key = pc.HasMACKey() ? pc.MACKey() : std::string(); | |
2610 std::string mac_algorithm = pc.HasMACAlgorithm() ? | |
2611 pc.MACAlgorithm() : std::string(); | |
2612 Time creation_time = Time::Now(); | |
2613 Time expiration_time; | |
2614 if (pc.HasExpires()) | |
2615 expiration_time = net::CookieMonster::ParseCookieTime(pc.Expires()); | |
2616 | |
2617 return (Create(url, pc.Name(), pc.Value(), domain_string, path_string, | |
2618 mac_key, mac_algorithm, creation_time, expiration_time, | |
2619 pc.IsSecure(), pc.IsHttpOnly())); | |
2620 } | |
2621 | |
2622 CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create( | |
2155 const GURL& url, | 2623 const GURL& url, |
2156 const std::string& name, | 2624 const std::string& name, |
2157 const std::string& value, | 2625 const std::string& value, |
2158 const std::string& domain, | 2626 const std::string& domain, |
2159 const std::string& path, | 2627 const std::string& path, |
2160 const std::string& mac_key, | 2628 const std::string& mac_key, |
2161 const std::string& mac_algorithm, | 2629 const std::string& mac_algorithm, |
2162 const base::Time& creation, | 2630 const base::Time& creation, |
2163 const base::Time& expiration, | 2631 const base::Time& expiration, |
2164 bool secure, | 2632 bool secure, |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2276 std::string CookieMonster::CanonicalCookie::DebugString() const { | 2744 std::string CookieMonster::CanonicalCookie::DebugString() const { |
2277 return base::StringPrintf( | 2745 return base::StringPrintf( |
2278 "name: %s value: %s domain: %s path: %s creation: %" | 2746 "name: %s value: %s domain: %s path: %s creation: %" |
2279 PRId64, | 2747 PRId64, |
2280 name_.c_str(), value_.c_str(), | 2748 name_.c_str(), value_.c_str(), |
2281 domain_.c_str(), path_.c_str(), | 2749 domain_.c_str(), path_.c_str(), |
2282 static_cast<int64>(creation_date_.ToTimeT())); | 2750 static_cast<int64>(creation_date_.ToTimeT())); |
2283 } | 2751 } |
2284 | 2752 |
2285 } // namespace | 2753 } // namespace |
OLD | NEW |