Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(227)

Side by Side Diff: net/base/cookie_monster.cc

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

Powered by Google App Engine
This is Rietveld 408576698