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

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

Powered by Google App Engine
This is Rietveld 408576698