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 | |
575 | |
576 void InvokeSetCookiesCallbackOnOtherThread( | |
577 const CookieMonster::SetCookiesCallback& callback, | |
578 bool success) { | |
579 if (!callback.is_null()) | |
580 callback.Run(success); | |
581 } | |
582 | |
583 void InvokeGetCookieListCallbackOnOtherThread( | |
584 const CookieMonster::GetCookieListCallback& callback, | |
585 const CookieList& cookies) { | |
586 if (!callback.is_null()) | |
587 callback.Run(cookies); | |
588 } | |
589 | |
590 void InvokeDeleteCallbackOnOtherThread( | |
591 const CookieMonster::DeleteCallback& callback, | |
592 int num_deleted) { | |
593 if (!callback.is_null()) | |
594 callback.Run(num_deleted); | |
595 } | |
596 | |
597 void InvokeGetCookiesCallbackOnOtherThread( | |
598 const CookieMonster::GetCookiesCallback& callback, | |
599 const std::string& cookie) { | |
600 if (!callback.is_null()) | |
601 callback.Run(cookie); | |
602 } | |
603 | |
604 void InvokeGetCookieInfoCallbackOnOtherThread( | |
605 const CookieMonster::GetCookieInfoCallback& callback, | |
606 const std::string& cookie_line, | |
607 const std::vector<CookieStore::CookieInfo>& cookie_infos) { | |
608 if (!callback.is_null()) { | |
609 std::string line = cookie_line; | |
610 std::vector<CookieStore::CookieInfo> info = cookie_infos; | |
611 callback.Run(&line, &info); | |
612 } | |
613 } | |
614 | |
615 // Task classes for queueing the coming request. | |
616 | |
617 class CookieMonsterTask | |
618 : public base::RefCountedThreadSafe<CookieMonsterTask> { | |
619 public: | |
620 virtual void Run() = 0; | |
621 void InvokeCallback(base::Closure callback); | |
erikwright (departed)
2011/08/12 16:00:34
InvokeCallback and cookie_monster -> protected
ycxiao
2011/08/12 17:51:25
Done.
| |
622 CookieMonster* cookie_monster() { | |
623 return cookie_monster_; | |
624 } | |
625 | |
626 protected: | |
627 CookieMonsterTask(); | |
erikwright (departed)
2011/08/12 16:00:34
Add constructor param for cookie_monster_, initial
ycxiao
2011/08/12 17:51:25
Done.
| |
628 virtual ~CookieMonsterTask(); | |
629 | |
630 CookieMonster* cookie_monster_; | |
erikwright (departed)
2011/08/12 16:00:34
cookie_monster_, thread_ -> private
ycxiao
2011/08/12 17:51:25
Done.
| |
631 scoped_refptr<base::MessageLoopProxy> thread_; | |
632 friend class base::RefCountedThreadSafe<CookieMonsterTask>; | |
633 | |
634 private: | |
635 DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask); | |
636 }; | |
637 | |
638 CookieMonsterTask::CookieMonsterTask() | |
639 : thread_(base::MessageLoopProxy::CreateForCurrentThread()) { } | |
640 | |
641 CookieMonsterTask::~CookieMonsterTask() { } | |
642 | |
643 void CookieMonsterTask::InvokeCallback(base::Closure callback) { | |
644 if (!callback.is_null()) { | |
645 if (thread_->BelongsToCurrentThread()) { | |
646 callback.Run(); | |
647 } else { | |
648 thread_->PostTask(FROM_HERE, callback); | |
649 } | |
650 } | |
651 } | |
652 | |
653 // Task class for SetCookieWithDetails call. | |
654 class SetCookieWithDetailsTask : public CookieMonsterTask { | |
655 public: | |
656 SetCookieWithDetailsTask( | |
657 CookieMonster* cookie_monster, | |
658 const GURL& url, const std::string& name, const std::string& value, | |
659 const std::string& domain, const std::string& path, | |
660 const base::Time& expiration_time, bool secure, bool http_only, | |
661 const CookieMonster::SetCookiesCallback& callback); | |
662 | |
663 virtual ~SetCookieWithDetailsTask() { } | |
erikwright (departed)
2011/08/12 16:00:34
protected or no declaration.
(For each subclass.)
ycxiao
2011/08/12 17:51:25
Done.
| |
664 | |
665 virtual void Run() OVERRIDE; | |
666 | |
667 private: | |
668 GURL url_; | |
669 std::string name_; | |
670 std::string value_; | |
671 std::string domain_; | |
672 std::string path_; | |
673 base::Time expiration_time_; | |
674 bool secure_; | |
675 bool http_only_; | |
676 CookieMonster::SetCookiesCallback callback_; | |
677 | |
678 DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask); | |
679 }; | |
680 | |
681 SetCookieWithDetailsTask::SetCookieWithDetailsTask( | |
erikwright (departed)
2011/08/12 16:00:34
I think it's reasonable to inline the constructor
ycxiao
2011/08/12 17:51:25
Done.
| |
682 CookieMonster* cookie_monster, | |
683 const GURL& url, const std::string& name, const std::string& value, | |
684 const std::string& domain, const std::string& path, | |
685 const base::Time& expiration_time, bool secure, bool http_only, | |
686 const CookieMonster::SetCookiesCallback& callback) | |
687 : url_(url), | |
688 name_(name), | |
689 value_(value), | |
690 domain_(domain), | |
691 path_(path), | |
692 expiration_time_(expiration_time), | |
693 secure_(secure), | |
694 http_only_(http_only), | |
695 callback_(callback) { | |
696 cookie_monster_ = cookie_monster; | |
697 } | |
698 | |
699 void SetCookieWithDetailsTask::Run() { | |
700 bool success = this->cookie_monster()-> | |
701 SetCookieWithDetails(url_, name_, value_, domain_, path_, | |
702 expiration_time_, secure_, http_only_); | |
703 if (!callback_.is_null()) { | |
704 this->InvokeCallback(base::Bind(&InvokeSetCookiesCallbackOnOtherThread, | |
705 callback_, success)); | |
706 } | |
707 } | |
708 | |
709 // Task class for GetAllCookies call. | |
710 class GetAllCookiesTask : public CookieMonsterTask { | |
711 public: | |
712 GetAllCookiesTask(CookieMonster* cookie_monster, | |
713 const CookieMonster::GetCookieListCallback& callback); | |
714 virtual ~GetAllCookiesTask() { } | |
715 | |
716 virtual void Run() OVERRIDE; | |
717 | |
718 private: | |
719 CookieMonster::GetCookieListCallback callback_; | |
720 | |
721 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask); | |
722 }; | |
723 | |
724 GetAllCookiesTask::GetAllCookiesTask( | |
725 CookieMonster* cookie_monster, | |
726 const CookieMonster::GetCookieListCallback& callback) | |
727 : callback_(callback) { | |
728 cookie_monster_ = cookie_monster; | |
729 } | |
730 | |
731 void GetAllCookiesTask::Run() { | |
732 if (!callback_.is_null()) { | |
733 CookieList cookies = this->cookie_monster()->GetAllCookies(); | |
734 this->InvokeCallback(base::Bind(&InvokeGetCookieListCallbackOnOtherThread, | |
735 callback_, cookies)); | |
736 } | |
737 } | |
738 | |
739 // Task class for GetAllCookiesForURLWithOptions call. | |
740 class GetAllCookiesForURLWithOptionsTask : public CookieMonsterTask { | |
741 public: | |
742 GetAllCookiesForURLWithOptionsTask( | |
743 CookieMonster* cookie_monster, | |
744 const GURL& url, | |
745 const CookieOptions& options, | |
746 const CookieMonster::GetCookieListCallback& callback); | |
747 | |
748 virtual ~GetAllCookiesForURLWithOptionsTask() { } | |
749 | |
750 virtual void Run() OVERRIDE; | |
751 | |
752 private: | |
753 GURL url_; | |
754 CookieOptions options_; | |
755 CookieMonster::GetCookieListCallback callback_; | |
756 | |
757 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesForURLWithOptionsTask); | |
758 }; | |
759 | |
760 GetAllCookiesForURLWithOptionsTask::GetAllCookiesForURLWithOptionsTask( | |
761 CookieMonster* cookie_monster, | |
762 const GURL& url, | |
763 const CookieOptions& options, | |
764 const CookieMonster::GetCookieListCallback& callback) | |
765 : url_(url), | |
766 options_(options), | |
767 callback_(callback) { | |
768 cookie_monster_ = cookie_monster; | |
769 } | |
770 | |
771 void GetAllCookiesForURLWithOptionsTask::Run() { | |
772 if (!callback_.is_null()) { | |
773 CookieList cookies = this->cookie_monster()-> | |
774 GetAllCookiesForURLWithOptions(url_, options_); | |
775 this->InvokeCallback(base::Bind(&InvokeGetCookieListCallbackOnOtherThread, | |
776 callback_, cookies)); | |
777 } | |
778 } | |
779 | |
780 // Task class for DeleteAll call. | |
781 class DeleteAllTask : public CookieMonsterTask { | |
782 public: | |
783 DeleteAllTask(CookieMonster* cookie_monster, | |
784 bool sync_to_store, | |
785 const CookieMonster::DeleteCallback& callback); | |
786 | |
787 virtual ~DeleteAllTask() { } | |
788 | |
789 virtual void Run() OVERRIDE; | |
790 | |
791 private: | |
792 bool sync_to_store_; | |
793 CookieMonster::DeleteCallback callback_; | |
794 | |
795 DISALLOW_COPY_AND_ASSIGN(DeleteAllTask); | |
796 }; | |
797 | |
798 DeleteAllTask::DeleteAllTask(CookieMonster* cookie_monster, | |
799 bool sync_to_store, | |
erikwright (departed)
2011/08/12 16:00:34
With BrowsingDataCookieHelper, do we still need sy
ycxiao
2011/08/12 17:51:25
The CookieMonster Unittest use DeleteAllAsync(fals
| |
800 const CookieMonster::DeleteCallback& callback) | |
801 : sync_to_store_(sync_to_store), | |
802 callback_(callback) { | |
803 cookie_monster_ = cookie_monster; | |
804 } | |
805 | |
806 void DeleteAllTask::Run() { | |
807 int num_deleted = this->cookie_monster()->DeleteAll(sync_to_store_); | |
808 if (!callback_.is_null()) { | |
809 this->InvokeCallback(base::Bind(&InvokeDeleteCallbackOnOtherThread, | |
810 callback_, num_deleted)); | |
811 } | |
812 } | |
813 | |
814 // Task class for DeleteAllCreatedBetween call. | |
815 class DeleteAllCreatedBetweenTask : public CookieMonsterTask { | |
816 public: | |
817 DeleteAllCreatedBetweenTask( | |
818 CookieMonster* cookie_monster, | |
819 const Time& delete_begin, | |
820 const Time& delete_end, | |
821 bool sync_to_store, | |
822 const CookieMonster::DeleteCallback& callback); | |
823 | |
824 virtual ~DeleteAllCreatedBetweenTask() { } | |
825 | |
826 virtual void Run() OVERRIDE; | |
827 | |
828 private: | |
829 Time delete_begin_; | |
830 Time delete_end_; | |
831 bool sync_to_store_; | |
832 CookieMonster::DeleteCallback callback_; | |
833 | |
834 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask); | |
835 }; | |
836 | |
837 DeleteAllCreatedBetweenTask::DeleteAllCreatedBetweenTask( | |
838 CookieMonster* cookie_monster, | |
839 const Time& delete_begin, | |
840 const Time& delete_end, | |
841 bool sync_to_store, | |
842 const CookieMonster::DeleteCallback& callback) | |
843 : delete_begin_(delete_begin), | |
844 delete_end_(delete_end), | |
845 sync_to_store_(sync_to_store), | |
846 callback_(callback) { | |
847 cookie_monster_ = cookie_monster; | |
848 } | |
849 | |
850 void DeleteAllCreatedBetweenTask::Run() { | |
851 int num_deleted = this->cookie_monster()-> | |
852 DeleteAllCreatedBetween(delete_begin_, delete_end_, sync_to_store_); | |
853 if (!callback_.is_null()) { | |
854 this->InvokeCallback(base::Bind(&InvokeDeleteCallbackOnOtherThread, | |
855 callback_, num_deleted)); | |
856 } | |
857 } | |
858 | |
859 // Task class for DeleteAllForHost call. | |
860 class DeleteAllForHostTask : public CookieMonsterTask { | |
861 public: | |
862 DeleteAllForHostTask(CookieMonster* cookie_monster, | |
863 const GURL& url, | |
864 const CookieMonster::DeleteCallback& callback); | |
865 | |
866 virtual ~DeleteAllForHostTask() { } | |
867 | |
868 virtual void Run() OVERRIDE; | |
869 | |
870 private: | |
871 GURL url_; | |
872 CookieMonster::DeleteCallback callback_; | |
873 | |
874 DISALLOW_COPY_AND_ASSIGN(DeleteAllForHostTask); | |
875 }; | |
876 | |
877 DeleteAllForHostTask::DeleteAllForHostTask( | |
878 CookieMonster* cookie_monster, | |
879 const GURL& url, | |
880 const CookieMonster::DeleteCallback& callback) | |
881 : url_(url), | |
882 callback_(callback) { | |
883 cookie_monster_ = cookie_monster; | |
884 } | |
885 | |
886 void DeleteAllForHostTask::Run() { | |
887 int num_deleted = this->cookie_monster()->DeleteAllForHost(url_); | |
888 if (!callback_.is_null()) { | |
889 this->InvokeCallback(base::Bind(&InvokeDeleteCallbackOnOtherThread, | |
890 callback_, num_deleted)); | |
891 } | |
892 } | |
893 | |
894 // Task class for DeleteCanonicalCookie call. | |
895 class DeleteCanonicalCookieTask : public CookieMonsterTask { | |
896 public: | |
897 DeleteCanonicalCookieTask( | |
898 CookieMonster* cookie_monster, | |
899 const CookieMonster::CanonicalCookie& cookie, | |
900 const CookieMonster::DeleteCookieCallback& callback); | |
901 virtual ~DeleteCanonicalCookieTask() { } | |
902 | |
903 virtual void Run() OVERRIDE; | |
904 | |
905 private: | |
906 CookieMonster::CanonicalCookie cookie_; | |
907 CookieMonster::DeleteCookieCallback callback_; | |
908 | |
909 DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask); | |
910 }; | |
911 | |
912 DeleteCanonicalCookieTask::DeleteCanonicalCookieTask( | |
913 CookieMonster* cookie_monster, | |
914 const CookieMonster::CanonicalCookie& cookie, | |
915 const CookieMonster::DeleteCookieCallback& callback) | |
916 : cookie_(cookie), | |
917 callback_(callback) { | |
918 cookie_monster_ = cookie_monster; | |
919 } | |
920 | |
921 void DeleteCanonicalCookieTask::Run() { | |
922 bool result = this->cookie_monster()->DeleteCanonicalCookie(cookie_); | |
923 if (!callback_.is_null()) { | |
924 this->InvokeCallback(base::Bind(&InvokeSetCookiesCallbackOnOtherThread, | |
925 callback_, result)); | |
926 } | |
927 } | |
928 | |
929 // Task class for SetCookieWithOptions call. | |
930 class SetCookieWithOptionsTask : public CookieMonsterTask { | |
931 public: | |
932 SetCookieWithOptionsTask(CookieMonster* cookie_monster, | |
933 const GURL& url, | |
934 const std::string& cookie_line, | |
935 const CookieOptions& options, | |
936 const CookieMonster::SetCookiesCallback& callback); | |
937 virtual ~SetCookieWithOptionsTask() { } | |
938 | |
939 virtual void Run() OVERRIDE; | |
940 | |
941 private: | |
942 GURL url_; | |
943 std::string cookie_line_; | |
944 CookieOptions options_; | |
945 CookieMonster::SetCookiesCallback callback_; | |
946 | |
947 DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask); | |
948 }; | |
949 | |
950 SetCookieWithOptionsTask::SetCookieWithOptionsTask( | |
951 CookieMonster* cookie_monster, | |
952 const GURL& url, | |
953 const std::string& cookie_line, | |
954 const CookieOptions& options, | |
955 const CookieMonster::SetCookiesCallback& callback) | |
956 : url_(url), | |
957 cookie_line_(cookie_line), | |
958 options_(options), | |
959 callback_(callback) { | |
960 cookie_monster_ = cookie_monster; | |
961 } | |
962 | |
963 void SetCookieWithOptionsTask::Run() { | |
964 bool result = this->cookie_monster()-> | |
965 SetCookieWithOptions(url_, cookie_line_, options_); | |
966 if (!callback_.is_null()) { | |
967 this->InvokeCallback(base::Bind(&InvokeSetCookiesCallbackOnOtherThread, | |
968 callback_, result)); | |
969 } | |
970 } | |
971 | |
972 // Task class for GetCookiesWithOptions call. | |
973 class GetCookiesWithOptionsTask : public CookieMonsterTask { | |
974 public: | |
975 GetCookiesWithOptionsTask(CookieMonster* cookie_monster, | |
976 GURL url, | |
977 const CookieOptions& options, | |
978 const CookieMonster::GetCookiesCallback& callback); | |
979 virtual ~GetCookiesWithOptionsTask() { } | |
980 | |
981 virtual void Run() OVERRIDE; | |
982 | |
983 private: | |
984 GURL url_; | |
985 CookieOptions options_; | |
986 CookieMonster::GetCookiesCallback callback_; | |
987 | |
988 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask); | |
989 }; | |
990 | |
991 GetCookiesWithOptionsTask::GetCookiesWithOptionsTask( | |
992 CookieMonster* cookie_monster, | |
993 GURL url, | |
994 const CookieOptions& options, | |
995 const CookieMonster::GetCookiesCallback& callback) | |
996 : url_(url), | |
997 options_(options), | |
998 callback_(callback) { | |
999 cookie_monster_ = cookie_monster; | |
1000 } | |
1001 | |
1002 void GetCookiesWithOptionsTask::Run() { | |
1003 std::string cookie = this->cookie_monster()-> | |
1004 GetCookiesWithOptions(url_, options_); | |
1005 if (!callback_.is_null()) { | |
1006 this->InvokeCallback(base::Bind(&InvokeGetCookiesCallbackOnOtherThread, | |
1007 callback_, cookie)); | |
1008 } | |
1009 } | |
1010 | |
1011 // Task class for GetCookiesWithInfo call. | |
1012 class GetCookiesWithInfoTask : public CookieMonsterTask { | |
1013 public: | |
1014 GetCookiesWithInfoTask(CookieMonster* cookie_monster, | |
1015 GURL url, | |
1016 const CookieOptions& options, | |
1017 const CookieMonster::GetCookieInfoCallback& callback); | |
1018 virtual ~GetCookiesWithInfoTask() { } | |
1019 | |
1020 virtual void Run() OVERRIDE; | |
1021 | |
1022 private: | |
1023 GURL url_; | |
1024 CookieOptions options_; | |
1025 CookieMonster::GetCookieInfoCallback callback_; | |
1026 | |
1027 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithInfoTask); | |
1028 }; | |
1029 | |
1030 GetCookiesWithInfoTask::GetCookiesWithInfoTask( | |
1031 CookieMonster* cookie_monster, | |
1032 GURL url, | |
1033 const CookieOptions& options, | |
1034 const CookieMonster::GetCookieInfoCallback& callback) | |
1035 : url_(url), | |
1036 options_(options), | |
1037 callback_(callback) { | |
1038 cookie_monster_ = cookie_monster; | |
1039 } | |
1040 | |
1041 void GetCookiesWithInfoTask::Run() { | |
1042 if (!callback_.is_null()) { | |
1043 std::string cookie_line; | |
1044 std::vector<CookieMonster::CookieInfo> cookie_infos; | |
1045 this->cookie_monster()-> | |
1046 GetCookiesWithInfo(url_, options_, &cookie_line, &cookie_infos); | |
1047 this->InvokeCallback(base::Bind(&InvokeGetCookieInfoCallbackOnOtherThread, | |
1048 callback_, cookie_line, cookie_infos)); | |
1049 } | |
1050 } | |
1051 | |
1052 // Task class for DeleteCookie call. | |
1053 class DeleteCookieTask : public CookieMonsterTask { | |
1054 public: | |
1055 DeleteCookieTask(CookieMonster* cookie_monster, | |
1056 GURL url, | |
1057 const std::string& cookie_name, | |
1058 const base::Closure& callback); | |
1059 virtual ~DeleteCookieTask() { } | |
1060 | |
1061 virtual void Run() OVERRIDE; | |
1062 | |
1063 private: | |
1064 GURL url_; | |
1065 std::string cookie_name_; | |
1066 base::Closure callback_; | |
1067 | |
1068 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask); | |
1069 }; | |
1070 | |
1071 DeleteCookieTask::DeleteCookieTask(CookieMonster* cookie_monster, | |
1072 GURL url, | |
1073 const std::string& cookie_name, | |
1074 const base::Closure& callback) | |
1075 : url_(url), | |
1076 cookie_name_(cookie_name), | |
1077 callback_(callback) { | |
1078 cookie_monster_ = cookie_monster; | |
1079 } | |
1080 | |
1081 void DeleteCookieTask::Run() { | |
1082 this->cookie_monster()->DeleteCookie(url_, cookie_name_); | |
1083 if (!callback_.is_null()) { | |
1084 this->InvokeCallback(callback_); | |
1085 } | |
1086 } | |
1087 | |
1088 // Asynchronous CookieMonster API | |
1089 | |
1090 void CookieMonster::SetCookieWithDetailsAsync( | |
1091 const GURL& url, const std::string& name, const std::string& value, | |
1092 const std::string& domain, const std::string& path, | |
1093 const base::Time& expiration_time, bool secure, bool http_only, | |
1094 const SetCookiesCallback& callback) { | |
1095 scoped_refptr<SetCookieWithDetailsTask> task = | |
1096 new SetCookieWithDetailsTask(this, url, name, value, domain, path, | |
1097 expiration_time, secure, http_only, | |
1098 callback); | |
1099 | |
1100 DoCookieTask(task); | |
1101 } | |
1102 | |
1103 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) { | |
1104 scoped_refptr<GetAllCookiesTask> task = | |
1105 new GetAllCookiesTask(this, callback); | |
1106 | |
1107 DoCookieTask(task); | |
1108 } | |
1109 | |
1110 | |
1111 void CookieMonster::GetAllCookiesForURLWithOptionsAsync( | |
1112 const GURL& url, | |
1113 const CookieOptions& options, | |
1114 const GetCookieListCallback& callback) { | |
1115 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = | |
1116 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); | |
1117 | |
1118 DoCookieTask(task); | |
1119 } | |
1120 | |
1121 void CookieMonster::GetAllCookiesForURLAsync( | |
1122 const GURL& url, const GetCookieListCallback& callback) { | |
1123 CookieOptions options; | |
1124 options.set_include_httponly(); | |
1125 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = | |
1126 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); | |
1127 | |
1128 DoCookieTask(task); | |
1129 } | |
1130 | |
1131 void CookieMonster::DeleteAllAsync(bool sync_to_store, | |
1132 const DeleteCallback& callback) { | |
1133 scoped_refptr<DeleteAllTask> task = | |
1134 new DeleteAllTask(this, sync_to_store, callback); | |
1135 | |
1136 DoCookieTask(task); | |
1137 } | |
1138 | |
1139 void CookieMonster::DeleteAllCreatedBetweenAsync( | |
1140 const Time& delete_begin, const Time& delete_end, | |
1141 bool sync_to_store, | |
1142 const DeleteCallback& callback) { | |
1143 scoped_refptr<DeleteAllCreatedBetweenTask> task = | |
1144 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end, | |
1145 sync_to_store, callback); | |
1146 | |
1147 DoCookieTask(task); | |
1148 } | |
1149 | |
1150 void CookieMonster::DeleteAllForHostAsync( | |
1151 const GURL& url, const DeleteCallback& callback) { | |
1152 scoped_refptr<DeleteAllForHostTask> task = | |
1153 new DeleteAllForHostTask(this, url, callback); | |
1154 | |
1155 DoCookieTask(task); | |
1156 } | |
1157 | |
1158 void CookieMonster::DeleteCanonicalCookieAsync( | |
1159 const CanonicalCookie& cookie, | |
1160 const DeleteCookieCallback& callback) { | |
1161 scoped_refptr<DeleteCanonicalCookieTask> task = | |
1162 new DeleteCanonicalCookieTask(this, cookie, callback); | |
1163 | |
1164 DoCookieTask(task); | |
1165 } | |
1166 | |
1167 void CookieMonster::SetCookieWithOptionsAsync( | |
1168 const GURL& url, | |
1169 const std::string& cookie_line, | |
1170 const CookieOptions& options, | |
1171 const SetCookiesCallback& callback) { | |
1172 scoped_refptr<SetCookieWithOptionsTask> task = | |
1173 new SetCookieWithOptionsTask(this, url, cookie_line, options, callback); | |
1174 | |
1175 DoCookieTask(task); | |
1176 } | |
1177 | |
1178 void CookieMonster::GetCookiesWithOptionsAsync( | |
1179 const GURL& url, | |
1180 const CookieOptions& options, | |
1181 const GetCookiesCallback& callback) { | |
1182 scoped_refptr<GetCookiesWithOptionsTask> task = | |
1183 new GetCookiesWithOptionsTask(this, url, options, callback); | |
1184 | |
1185 DoCookieTask(task); | |
1186 } | |
1187 | |
1188 void CookieMonster::GetCookiesWithInfoAsync( | |
1189 const GURL& url, | |
1190 const CookieOptions& options, | |
1191 const GetCookieInfoCallback& callback) { | |
1192 scoped_refptr<GetCookiesWithInfoTask> task = | |
1193 new GetCookiesWithInfoTask(this, url, options, callback); | |
1194 | |
1195 DoCookieTask(task); | |
1196 } | |
1197 | |
1198 void CookieMonster::DeleteCookieAsync(const GURL& url, | |
1199 const std::string& cookie_name, | |
1200 const base::Closure& callback) { | |
1201 scoped_refptr<DeleteCookieTask> task = | |
1202 new DeleteCookieTask(this, url, cookie_name, callback); | |
1203 | |
1204 DoCookieTask(task); | |
1205 } | |
1206 | |
1207 void CookieMonster::DoCookieTask(scoped_refptr<CookieMonsterTask> task_item) { | |
1208 base::Closure task = base::Bind(&CookieMonsterTask::Run, task_item.get()); | |
1209 DCHECK(!task.is_null()); | |
erikwright (departed)
2011/08/12 16:00:34
Remove the DCHECK, and perhaps move the bind to li
ycxiao
2011/08/12 17:51:25
Done.
| |
1210 InitIfNecessary(); | |
1211 bool loaded = false; | |
1212 { | |
1213 base::AutoLock autolock(lock_); | |
1214 loaded = loaded_; | |
1215 } | |
1216 if (loaded) { | |
1217 task.Run(); | |
1218 } else { | |
1219 base::AutoLock autolock(lock_); | |
1220 queue_.push(task); | |
1221 } | |
1222 } | |
1223 | |
570 bool CookieMonster::SetCookieWithDetails( | 1224 bool CookieMonster::SetCookieWithDetails( |
571 const GURL& url, const std::string& name, const std::string& value, | 1225 const GURL& url, const std::string& name, const std::string& value, |
572 const std::string& domain, const std::string& path, | 1226 const std::string& domain, const std::string& path, |
573 const base::Time& expiration_time, bool secure, bool http_only) { | 1227 const base::Time& expiration_time, bool secure, bool http_only) { |
574 base::AutoLock autolock(lock_); | 1228 base::AutoLock autolock(lock_); |
575 | 1229 |
576 if (!HasCookieableScheme(url)) | 1230 if (!HasCookieableScheme(url)) |
577 return false; | 1231 return false; |
578 | 1232 |
579 InitIfNecessary(); | |
580 | |
581 Time creation_time = CurrentTime(); | 1233 Time creation_time = CurrentTime(); |
582 last_time_seen_ = creation_time; | 1234 last_time_seen_ = creation_time; |
583 | 1235 |
584 // TODO(abarth): Take these values as parameters. | 1236 // TODO(abarth): Take these values as parameters. |
585 std::string mac_key; | 1237 std::string mac_key; |
586 std::string mac_algorithm; | 1238 std::string mac_algorithm; |
587 | 1239 |
588 scoped_ptr<CanonicalCookie> cc; | 1240 scoped_ptr<CanonicalCookie> cc; |
589 cc.reset(CanonicalCookie::Create( | 1241 cc.reset(CanonicalCookie::Create( |
590 url, name, value, domain, path, | 1242 url, name, value, domain, path, |
591 mac_key, mac_algorithm, | 1243 mac_key, mac_algorithm, |
592 creation_time, expiration_time, | 1244 creation_time, expiration_time, |
593 secure, http_only)); | 1245 secure, http_only)); |
594 | 1246 |
595 if (!cc.get()) | 1247 if (!cc.get()) |
596 return false; | 1248 return false; |
597 | 1249 |
598 CookieOptions options; | 1250 CookieOptions options; |
599 options.set_include_httponly(); | 1251 options.set_include_httponly(); |
600 return SetCanonicalCookie(&cc, creation_time, options); | 1252 return SetCanonicalCookie(&cc, creation_time, options); |
601 } | 1253 } |
602 | 1254 |
603 void CookieMonster::SetCookieWithDetailsAsync( | 1255 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_); | 1256 base::AutoLock autolock(lock_); |
618 InitIfNecessary(); | 1257 InitIfNecessary(); |
619 for (net::CookieList::const_iterator iter = list.begin(); | 1258 for (net::CookieList::const_iterator iter = list.begin(); |
620 iter != list.end(); ++iter) { | 1259 iter != list.end(); ++iter) { |
621 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie; | 1260 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie; |
622 cookie.reset(new net::CookieMonster::CanonicalCookie(*iter)); | 1261 cookie.reset(new net::CookieMonster::CanonicalCookie(*iter)); |
623 net::CookieOptions options; | 1262 net::CookieOptions options; |
624 options.set_include_httponly(); | 1263 options.set_include_httponly(); |
625 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), | 1264 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), |
626 options)) { | 1265 options)) { |
627 return false; | 1266 return false; |
628 } | 1267 } |
629 } | 1268 } |
630 return true; | 1269 return true; |
631 } | 1270 } |
632 | 1271 |
633 CookieList CookieMonster::GetAllCookies() { | 1272 CookieList CookieMonster::GetAllCookies() { |
634 base::AutoLock autolock(lock_); | 1273 base::AutoLock autolock(lock_); |
635 InitIfNecessary(); | |
636 | 1274 |
637 // This function is being called to scrape the cookie list for management UI | 1275 // 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 | 1276 // 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 | 1277 // 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 | 1278 // is already slow enough) that it's OK to take the time to garbage collect |
641 // the expired cookies now. | 1279 // the expired cookies now. |
642 // | 1280 // |
643 // Note that this does not prune cookies to be below our limits (if we've | 1281 // Note that this does not prune cookies to be below our limits (if we've |
644 // exceeded them) the way that calling GarbageCollect() would. | 1282 // exceeded them) the way that calling GarbageCollect() would. |
645 GarbageCollectExpired(Time::Now(), | 1283 GarbageCollectExpired(Time::Now(), |
(...skipping 10 matching lines...) Expand all Loading... | |
656 | 1294 |
657 CookieList cookie_list; | 1295 CookieList cookie_list; |
658 cookie_list.reserve(cookie_ptrs.size()); | 1296 cookie_list.reserve(cookie_ptrs.size()); |
659 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); | 1297 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); |
660 it != cookie_ptrs.end(); ++it) | 1298 it != cookie_ptrs.end(); ++it) |
661 cookie_list.push_back(**it); | 1299 cookie_list.push_back(**it); |
662 | 1300 |
663 return cookie_list; | 1301 return cookie_list; |
664 } | 1302 } |
665 | 1303 |
666 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) { | |
667 if (!callback.is_null()) | |
668 callback.Run(GetAllCookies()); | |
669 } | |
670 | |
671 CookieList CookieMonster::GetAllCookiesForURLWithOptions( | 1304 CookieList CookieMonster::GetAllCookiesForURLWithOptions( |
672 const GURL& url, | 1305 const GURL& url, |
673 const CookieOptions& options) { | 1306 const CookieOptions& options) { |
674 base::AutoLock autolock(lock_); | 1307 base::AutoLock autolock(lock_); |
675 InitIfNecessary(); | |
676 | 1308 |
677 std::vector<CanonicalCookie*> cookie_ptrs; | 1309 std::vector<CanonicalCookie*> cookie_ptrs; |
678 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs); | 1310 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs); |
679 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); | 1311 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); |
680 | 1312 |
681 CookieList cookies; | 1313 CookieList cookies; |
682 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); | 1314 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); |
683 it != cookie_ptrs.end(); it++) | 1315 it != cookie_ptrs.end(); it++) |
684 cookies.push_back(**it); | 1316 cookies.push_back(**it); |
685 | 1317 |
686 return cookies; | 1318 return cookies; |
687 } | 1319 } |
688 | 1320 |
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) { | 1321 CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) { |
698 CookieOptions options; | 1322 CookieOptions options; |
699 options.set_include_httponly(); | 1323 options.set_include_httponly(); |
700 | 1324 |
701 return GetAllCookiesForURLWithOptions(url, options); | 1325 return GetAllCookiesForURLWithOptions(url, options); |
702 } | 1326 } |
703 | 1327 |
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) { | 1328 int CookieMonster::DeleteAll(bool sync_to_store) { |
711 base::AutoLock autolock(lock_); | 1329 base::AutoLock autolock(lock_); |
712 if (sync_to_store) | |
713 InitIfNecessary(); | |
714 | 1330 |
715 int num_deleted = 0; | 1331 int num_deleted = 0; |
716 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { | 1332 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
717 CookieMap::iterator curit = it; | 1333 CookieMap::iterator curit = it; |
718 ++it; | 1334 ++it; |
719 InternalDeleteCookie(curit, sync_to_store, | 1335 InternalDeleteCookie(curit, sync_to_store, |
720 sync_to_store ? DELETE_COOKIE_EXPLICIT : | 1336 sync_to_store ? DELETE_COOKIE_EXPLICIT : |
721 DELETE_COOKIE_DONT_RECORD /* Destruction. */); | 1337 DELETE_COOKIE_DONT_RECORD /* Destruction. */); |
722 ++num_deleted; | 1338 ++num_deleted; |
723 } | 1339 } |
724 | 1340 |
725 return num_deleted; | 1341 return num_deleted; |
726 } | 1342 } |
727 | 1343 |
728 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin, | 1344 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin, |
729 const Time& delete_end, | 1345 const Time& delete_end, |
730 bool sync_to_store) { | 1346 bool sync_to_store) { |
731 base::AutoLock autolock(lock_); | 1347 base::AutoLock autolock(lock_); |
732 InitIfNecessary(); | |
733 | 1348 |
734 int num_deleted = 0; | 1349 int num_deleted = 0; |
735 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { | 1350 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
736 CookieMap::iterator curit = it; | 1351 CookieMap::iterator curit = it; |
737 CanonicalCookie* cc = curit->second; | 1352 CanonicalCookie* cc = curit->second; |
738 ++it; | 1353 ++it; |
739 | 1354 |
740 if (cc->CreationDate() >= delete_begin && | 1355 if (cc->CreationDate() >= delete_begin && |
741 (delete_end.is_null() || cc->CreationDate() < delete_end)) { | 1356 (delete_end.is_null() || cc->CreationDate() < delete_end)) { |
742 InternalDeleteCookie(curit, sync_to_store, DELETE_COOKIE_EXPLICIT); | 1357 InternalDeleteCookie(curit, sync_to_store, DELETE_COOKIE_EXPLICIT); |
743 ++num_deleted; | 1358 ++num_deleted; |
744 } | 1359 } |
745 } | 1360 } |
746 | 1361 |
747 return num_deleted; | 1362 return num_deleted; |
748 } | 1363 } |
749 | 1364 |
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) { | 1365 int CookieMonster::DeleteAllForHost(const GURL& url) { |
761 base::AutoLock autolock(lock_); | 1366 base::AutoLock autolock(lock_); |
762 InitIfNecessary(); | |
763 | 1367 |
764 if (!HasCookieableScheme(url)) | 1368 if (!HasCookieableScheme(url)) |
765 return 0; | 1369 return 0; |
766 | 1370 |
767 const std::string scheme(url.scheme()); | 1371 const std::string scheme(url.scheme()); |
768 const std::string host(url.host()); | 1372 const std::string host(url.host()); |
769 | 1373 |
770 // We store host cookies in the store by their canonical host name; | 1374 // 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 | 1375 // domain cookies are stored with a leading ".". So this is a pretty |
772 // simple lookup and per-cookie delete. | 1376 // simple lookup and per-cookie delete. |
773 int num_deleted = 0; | 1377 int num_deleted = 0; |
774 for (CookieMapItPair its = cookies_.equal_range(GetKey(host)); | 1378 for (CookieMapItPair its = cookies_.equal_range(GetKey(host)); |
775 its.first != its.second;) { | 1379 its.first != its.second;) { |
776 CookieMap::iterator curit = its.first; | 1380 CookieMap::iterator curit = its.first; |
777 ++its.first; | 1381 ++its.first; |
778 | 1382 |
779 const CanonicalCookie* const cc = curit->second; | 1383 const CanonicalCookie* const cc = curit->second; |
780 | 1384 |
781 // Delete only on a match as a host cookie. | 1385 // Delete only on a match as a host cookie. |
782 if (cc->IsHostCookie() && cc->IsDomainMatch(scheme, host)) { | 1386 if (cc->IsHostCookie() && cc->IsDomainMatch(scheme, host)) { |
783 num_deleted++; | 1387 num_deleted++; |
784 | 1388 |
785 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); | 1389 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); |
786 } | 1390 } |
787 } | 1391 } |
788 return num_deleted; | 1392 return num_deleted; |
789 } | 1393 } |
790 | 1394 |
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) { | 1395 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) { |
799 base::AutoLock autolock(lock_); | 1396 base::AutoLock autolock(lock_); |
800 InitIfNecessary(); | |
801 | 1397 |
802 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); | 1398 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); |
803 its.first != its.second; ++its.first) { | 1399 its.first != its.second; ++its.first) { |
804 // The creation date acts as our unique index... | 1400 // The creation date acts as our unique index... |
805 if (its.first->second->CreationDate() == cookie.CreationDate()) { | 1401 if (its.first->second->CreationDate() == cookie.CreationDate()) { |
806 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT); | 1402 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT); |
807 return true; | 1403 return true; |
808 } | 1404 } |
809 } | 1405 } |
810 return false; | 1406 return false; |
811 } | 1407 } |
812 | 1408 |
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( | 1409 void CookieMonster::SetCookieableSchemes( |
822 const char* schemes[], size_t num_schemes) { | 1410 const char* schemes[], size_t num_schemes) { |
823 base::AutoLock autolock(lock_); | 1411 base::AutoLock autolock(lock_); |
824 | 1412 |
825 // Cookieable Schemes must be set before first use of function. | 1413 // Cookieable Schemes must be set before first use of function. |
826 DCHECK(!initialized_); | 1414 DCHECK(!initialized_); |
827 | 1415 |
828 cookieable_schemes_.clear(); | 1416 cookieable_schemes_.clear(); |
829 cookieable_schemes_.insert(cookieable_schemes_.end(), | 1417 cookieable_schemes_.insert(cookieable_schemes_.end(), |
830 schemes, schemes + num_schemes); | 1418 schemes, schemes + num_schemes); |
(...skipping 28 matching lines...) Expand all Loading... | |
859 | 1447 |
860 bool CookieMonster::SetCookieWithOptions(const GURL& url, | 1448 bool CookieMonster::SetCookieWithOptions(const GURL& url, |
861 const std::string& cookie_line, | 1449 const std::string& cookie_line, |
862 const CookieOptions& options) { | 1450 const CookieOptions& options) { |
863 base::AutoLock autolock(lock_); | 1451 base::AutoLock autolock(lock_); |
864 | 1452 |
865 if (!HasCookieableScheme(url)) { | 1453 if (!HasCookieableScheme(url)) { |
866 return false; | 1454 return false; |
867 } | 1455 } |
868 | 1456 |
869 InitIfNecessary(); | |
870 | |
871 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options); | 1457 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options); |
872 } | 1458 } |
873 | 1459 |
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, | 1460 std::string CookieMonster::GetCookiesWithOptions(const GURL& url, |
885 const CookieOptions& options) { | 1461 const CookieOptions& options) { |
886 base::AutoLock autolock(lock_); | 1462 base::AutoLock autolock(lock_); |
887 InitIfNecessary(); | |
888 | 1463 |
889 if (!HasCookieableScheme(url)) | 1464 if (!HasCookieableScheme(url)) |
890 return std::string(); | 1465 return std::string(); |
891 | 1466 |
892 TimeTicks start_time(TimeTicks::Now()); | 1467 TimeTicks start_time(TimeTicks::Now()); |
893 | 1468 |
894 std::vector<CanonicalCookie*> cookies; | 1469 std::vector<CanonicalCookie*> cookies; |
895 FindCookiesForHostAndDomain(url, options, true, &cookies); | 1470 FindCookiesForHostAndDomain(url, options, true, &cookies); |
896 std::sort(cookies.begin(), cookies.end(), CookieSorter); | 1471 std::sort(cookies.begin(), cookies.end(), CookieSorter); |
897 | 1472 |
898 std::string cookie_line = BuildCookieLine(cookies); | 1473 std::string cookie_line = BuildCookieLine(cookies); |
899 | 1474 |
900 histogram_time_get_->AddTime(TimeTicks::Now() - start_time); | 1475 histogram_time_get_->AddTime(TimeTicks::Now() - start_time); |
901 | 1476 |
902 VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line; | 1477 VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line; |
903 | 1478 |
904 return cookie_line; | 1479 return cookie_line; |
905 } | 1480 } |
906 | 1481 |
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, | 1482 void CookieMonster::GetCookiesWithInfo(const GURL& url, |
916 const CookieOptions& options, | 1483 const CookieOptions& options, |
917 std::string* cookie_line, | 1484 std::string* cookie_line, |
918 std::vector<CookieInfo>* cookie_infos) { | 1485 std::vector<CookieInfo>* cookie_infos) { |
919 DCHECK(cookie_line->empty()); | 1486 DCHECK(cookie_line->empty()); |
920 DCHECK(cookie_infos->empty()); | 1487 DCHECK(cookie_infos->empty()); |
921 | 1488 |
922 base::AutoLock autolock(lock_); | 1489 base::AutoLock autolock(lock_); |
923 InitIfNecessary(); | |
924 | 1490 |
925 if (!HasCookieableScheme(url)) | 1491 if (!HasCookieableScheme(url)) |
926 return; | 1492 return; |
927 | 1493 |
928 TimeTicks start_time(TimeTicks::Now()); | 1494 TimeTicks start_time(TimeTicks::Now()); |
929 | 1495 |
930 std::vector<CanonicalCookie*> cookies; | 1496 std::vector<CanonicalCookie*> cookies; |
931 FindCookiesForHostAndDomain(url, options, true, &cookies); | 1497 FindCookiesForHostAndDomain(url, options, true, &cookies); |
932 std::sort(cookies.begin(), cookies.end(), CookieSorter); | 1498 std::sort(cookies.begin(), cookies.end(), CookieSorter); |
933 *cookie_line = BuildCookieLine(cookies); | 1499 *cookie_line = BuildCookieLine(cookies); |
934 | 1500 |
935 histogram_time_get_->AddTime(TimeTicks::Now() - start_time); | 1501 histogram_time_get_->AddTime(TimeTicks::Now() - start_time); |
936 | 1502 |
937 TimeTicks mac_start_time = TimeTicks::Now(); | 1503 TimeTicks mac_start_time = TimeTicks::Now(); |
938 BuildCookieInfoList(cookies, cookie_infos); | 1504 BuildCookieInfoList(cookies, cookie_infos); |
939 histogram_time_mac_->AddTime(TimeTicks::Now() - mac_start_time); | 1505 histogram_time_mac_->AddTime(TimeTicks::Now() - mac_start_time); |
940 } | 1506 } |
941 | 1507 |
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, | 1508 void CookieMonster::DeleteCookie(const GURL& url, |
955 const std::string& cookie_name) { | 1509 const std::string& cookie_name) { |
956 base::AutoLock autolock(lock_); | 1510 base::AutoLock autolock(lock_); |
957 InitIfNecessary(); | |
958 | 1511 |
959 if (!HasCookieableScheme(url)) | 1512 if (!HasCookieableScheme(url)) |
960 return; | 1513 return; |
961 | 1514 |
962 CookieOptions options; | 1515 CookieOptions options; |
963 options.set_include_httponly(); | 1516 options.set_include_httponly(); |
964 // Get the cookies for this host and its domain(s). | 1517 // Get the cookies for this host and its domain(s). |
965 std::vector<CanonicalCookie*> cookies; | 1518 std::vector<CanonicalCookie*> cookies; |
966 FindCookiesForHostAndDomain(url, options, true, &cookies); | 1519 FindCookiesForHostAndDomain(url, options, true, &cookies); |
967 std::set<CanonicalCookie*> matching_cookies; | 1520 std::set<CanonicalCookie*> matching_cookies; |
968 | 1521 |
969 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); | 1522 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); |
970 it != cookies.end(); ++it) { | 1523 it != cookies.end(); ++it) { |
971 if ((*it)->Name() != cookie_name) | 1524 if ((*it)->Name() != cookie_name) |
972 continue; | 1525 continue; |
973 if (url.path().find((*it)->Path())) | 1526 if (url.path().find((*it)->Path())) |
974 continue; | 1527 continue; |
975 matching_cookies.insert(*it); | 1528 matching_cookies.insert(*it); |
976 } | 1529 } |
977 | 1530 |
978 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { | 1531 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
979 CookieMap::iterator curit = it; | 1532 CookieMap::iterator curit = it; |
980 ++it; | 1533 ++it; |
981 if (matching_cookies.find(curit->second) != matching_cookies.end()) { | 1534 if (matching_cookies.find(curit->second) != matching_cookies.end()) { |
982 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); | 1535 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); |
983 } | 1536 } |
984 } | 1537 } |
985 } | 1538 } |
986 | 1539 |
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() { | 1540 CookieMonster* CookieMonster::GetCookieMonster() { |
996 return this; | 1541 return this; |
997 } | 1542 } |
998 | 1543 |
999 CookieMonster::~CookieMonster() { | 1544 CookieMonster::~CookieMonster() { |
1000 DeleteAll(false); | 1545 DeleteAll(false); |
1001 } | 1546 } |
1002 | 1547 |
1003 bool CookieMonster::SetCookieWithCreationTime(const GURL& url, | 1548 bool CookieMonster::SetCookieWithCreationTime(const GURL& url, |
1004 const std::string& cookie_line, | 1549 const std::string& cookie_line, |
1005 const base::Time& creation_time) { | 1550 const base::Time& creation_time) { |
1551 DCHECK(!store_) << "This method is only to be used by unit-tests."; | |
1006 base::AutoLock autolock(lock_); | 1552 base::AutoLock autolock(lock_); |
1007 | 1553 |
1008 if (!HasCookieableScheme(url)) { | 1554 if (!HasCookieableScheme(url)) { |
1009 return false; | 1555 return false; |
1010 } | 1556 } |
1011 | 1557 |
1012 InitIfNecessary(); | 1558 InitIfNecessary(); |
1013 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time, | 1559 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time, |
1014 CookieOptions()); | 1560 CookieOptions()); |
1015 } | 1561 } |
1016 | 1562 |
1017 void CookieMonster::InitStore() { | 1563 void CookieMonster::InitStore() { |
1018 DCHECK(store_) << "Store must exist to initialize"; | 1564 DCHECK(store_) << "Store must exist to initialize"; |
1565 store_->Load(base::Bind(&CookieMonster::OnLoaded, this)); | |
1566 } | |
1019 | 1567 |
1020 TimeTicks beginning_time(TimeTicks::Now()); | 1568 void CookieMonster::OnLoaded(const std::vector<CanonicalCookie*>& cookies) { |
1569 StoreLoadedCookies(cookies); | |
1570 // Invoke the task queue of cookie request. | |
1571 InvokeQueue(); | |
1572 } | |
1021 | 1573 |
1574 void CookieMonster::StoreLoadedCookies( | |
1575 const std::vector<CanonicalCookie*>& cookies) { | |
1022 // Initialize the store and sync in any saved persistent cookies. We don't | 1576 // 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, | 1577 // care if it's expired, insert it so it can be garbage collected, removed, |
1024 // and sync'd. | 1578 // and sync'd. |
1025 std::vector<CanonicalCookie*> cookies; | 1579 base::AutoLock autolock(lock_); |
1026 // Reserve space for the maximum amount of cookies a database should have. | 1580 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 | 1581 |
1031 // Avoid ever letting cookies with duplicate creation times into the store; | 1582 // 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 | 1583 // that way we don't have to worry about what sections of code are safe |
1033 // to call while it's in that state. | 1584 // to call while it's in that state. |
1034 std::set<int64> creation_times; | 1585 std::set<int64> creation_times; |
1035 | 1586 |
1036 // Presumably later than any access time in the store. | 1587 // Presumably later than any access time in the store. |
1037 Time earliest_access_time; | 1588 Time earliest_access_time; |
1038 | 1589 |
1039 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); | 1590 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); |
(...skipping 22 matching lines...) Expand all Loading... | |
1062 | 1613 |
1063 // After importing cookies from the PersistentCookieStore, verify that | 1614 // After importing cookies from the PersistentCookieStore, verify that |
1064 // none of our other constraints are violated. | 1615 // none of our other constraints are violated. |
1065 // | 1616 // |
1066 // In particular, the backing store might have given us duplicate cookies. | 1617 // In particular, the backing store might have given us duplicate cookies. |
1067 EnsureCookiesMapIsValid(); | 1618 EnsureCookiesMapIsValid(); |
1068 | 1619 |
1069 histogram_time_load_->AddTime(TimeTicks::Now() - beginning_time); | 1620 histogram_time_load_->AddTime(TimeTicks::Now() - beginning_time); |
1070 } | 1621 } |
1071 | 1622 |
1623 void CookieMonster::InvokeQueue() { | |
1624 while (true) { | |
1625 base::Closure request_task; | |
1626 { | |
1627 base::AutoLock autolock(lock_); | |
1628 if (queue_.empty()) { | |
1629 loaded_ = true; | |
1630 break; | |
1631 } | |
1632 request_task = queue_.front(); | |
1633 queue_.pop(); | |
1634 } | |
1635 request_task.Run(); | |
1636 } | |
1637 } | |
1638 | |
1072 void CookieMonster::EnsureCookiesMapIsValid() { | 1639 void CookieMonster::EnsureCookiesMapIsValid() { |
1073 lock_.AssertAcquired(); | 1640 lock_.AssertAcquired(); |
1074 | 1641 |
1075 int num_duplicates_trimmed = 0; | 1642 int num_duplicates_trimmed = 0; |
1076 | 1643 |
1077 // Iterate through all the of the cookies, grouped by host. | 1644 // Iterate through all the of the cookies, grouped by host. |
1078 CookieMap::iterator prev_range_end = cookies_.begin(); | 1645 CookieMap::iterator prev_range_end = cookies_.begin(); |
1079 while (prev_range_end != cookies_.end()) { | 1646 while (prev_range_end != cookies_.end()) { |
1080 CookieMap::iterator cur_range_begin = prev_range_end; | 1647 CookieMap::iterator cur_range_begin = prev_range_end; |
1081 const std::string key = cur_range_begin->first; // Keep a copy. | 1648 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) | 2712 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) |
2146 // Mobile apps can sometimes be shut down without any warning, so the session | 2713 // 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. | 2714 // cookie has to be persistent and given a default expiration time. |
2148 expiry_date_ = base::Time::Now() + | 2715 expiry_date_ = base::Time::Now() + |
2149 base::TimeDelta::FromDays(kPersistentSessionCookieExpiryInDays); | 2716 base::TimeDelta::FromDays(kPersistentSessionCookieExpiryInDays); |
2150 has_expires_ = true; | 2717 has_expires_ = true; |
2151 #endif | 2718 #endif |
2152 } | 2719 } |
2153 | 2720 |
2154 CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create( | 2721 CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create( |
2722 const GURL& url, | |
2723 const ParsedCookie& pc) { | |
2724 if (!pc.IsValid()) { | |
2725 return NULL; | |
2726 } | |
2727 | |
2728 std::string domain_string; | |
2729 if (!GetCookieDomain(url, pc, &domain_string)) { | |
2730 return NULL; | |
2731 } | |
2732 std::string path_string = CanonPath(url, pc); | |
2733 std::string mac_key = pc.HasMACKey() ? pc.MACKey() : std::string(); | |
2734 std::string mac_algorithm = pc.HasMACAlgorithm() ? | |
2735 pc.MACAlgorithm() : std::string(); | |
2736 Time creation_time = Time::Now(); | |
2737 Time expiration_time; | |
2738 if (pc.HasExpires()) | |
2739 expiration_time = net::CookieMonster::ParseCookieTime(pc.Expires()); | |
2740 | |
2741 return (Create(url, pc.Name(), pc.Value(), domain_string, path_string, | |
2742 mac_key, mac_algorithm, creation_time, expiration_time, | |
2743 pc.IsSecure(), pc.IsHttpOnly())); | |
2744 } | |
2745 | |
2746 CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create( | |
2155 const GURL& url, | 2747 const GURL& url, |
2156 const std::string& name, | 2748 const std::string& name, |
2157 const std::string& value, | 2749 const std::string& value, |
2158 const std::string& domain, | 2750 const std::string& domain, |
2159 const std::string& path, | 2751 const std::string& path, |
2160 const std::string& mac_key, | 2752 const std::string& mac_key, |
2161 const std::string& mac_algorithm, | 2753 const std::string& mac_algorithm, |
2162 const base::Time& creation, | 2754 const base::Time& creation, |
2163 const base::Time& expiration, | 2755 const base::Time& expiration, |
2164 bool secure, | 2756 bool secure, |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2276 std::string CookieMonster::CanonicalCookie::DebugString() const { | 2868 std::string CookieMonster::CanonicalCookie::DebugString() const { |
2277 return base::StringPrintf( | 2869 return base::StringPrintf( |
2278 "name: %s value: %s domain: %s path: %s creation: %" | 2870 "name: %s value: %s domain: %s path: %s creation: %" |
2279 PRId64, | 2871 PRId64, |
2280 name_.c_str(), value_.c_str(), | 2872 name_.c_str(), value_.c_str(), |
2281 domain_.c_str(), path_.c_str(), | 2873 domain_.c_str(), path_.c_str(), |
2282 static_cast<int64>(creation_date_.ToTimeT())); | 2874 static_cast<int64>(creation_date_.ToTimeT())); |
2283 } | 2875 } |
2284 | 2876 |
2285 } // namespace | 2877 } // namespace |
OLD | NEW |