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

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

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

Powered by Google App Engine
This is Rietveld 408576698