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

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"
55 #include "base/metrics/histogram.h" 56 #include "base/metrics/histogram.h"
56 #include "base/string_tokenizer.h" 57 #include "base/string_tokenizer.h"
57 #include "base/string_util.h" 58 #include "base/string_util.h"
58 #include "googleurl/src/gurl.h" 59 #include "googleurl/src/gurl.h"
59 #include "googleurl/src/url_canon.h" 60 #include "googleurl/src/url_canon.h"
60 #include "net/base/net_util.h" 61 #include "net/base/net_util.h"
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 } 413 }
413 } 414 }
414 415
415 } // namespace 416 } // namespace
416 417
417 // static 418 // static
418 bool CookieMonster::enable_file_scheme_ = false; 419 bool CookieMonster::enable_file_scheme_ = false;
419 420
420 CookieMonster::CookieMonster(PersistentCookieStore* store, Delegate* delegate) 421 CookieMonster::CookieMonster(PersistentCookieStore* store, Delegate* delegate)
421 : initialized_(false), 422 : initialized_(false),
423 loaded_(false),
422 expiry_and_key_scheme_(expiry_and_key_default_), 424 expiry_and_key_scheme_(expiry_and_key_default_),
423 store_(store), 425 store_(store),
424 last_access_threshold_( 426 last_access_threshold_(
425 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)), 427 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)),
426 delegate_(delegate), 428 delegate_(delegate),
427 last_statistic_record_time_(Time::Now()), 429 last_statistic_record_time_(Time::Now()),
428 keep_expired_cookies_(false) { 430 keep_expired_cookies_(false) {
429 InitializeHistograms(); 431 InitializeHistograms();
430 SetDefaultCookieableSchemes(); 432 SetDefaultCookieableSchemes();
431 } 433 }
432 434
433 CookieMonster::CookieMonster(PersistentCookieStore* store, 435 CookieMonster::CookieMonster(PersistentCookieStore* store,
434 Delegate* delegate, 436 Delegate* delegate,
435 int last_access_threshold_milliseconds) 437 int last_access_threshold_milliseconds)
436 : initialized_(false), 438 : initialized_(false),
439 loaded_(false),
437 expiry_and_key_scheme_(expiry_and_key_default_), 440 expiry_and_key_scheme_(expiry_and_key_default_),
438 store_(store), 441 store_(store),
439 last_access_threshold_(base::TimeDelta::FromMilliseconds( 442 last_access_threshold_(base::TimeDelta::FromMilliseconds(
440 last_access_threshold_milliseconds)), 443 last_access_threshold_milliseconds)),
441 delegate_(delegate), 444 delegate_(delegate),
442 last_statistic_record_time_(base::Time::Now()), 445 last_statistic_record_time_(base::Time::Now()),
443 keep_expired_cookies_(false) { 446 keep_expired_cookies_(false) {
444 InitializeHistograms(); 447 InitializeHistograms();
445 SetDefaultCookieableSchemes(); 448 SetDefaultCookieableSchemes();
446 } 449 }
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
560 // the following check would be reasonable: 563 // the following check would be reasonable:
561 // NOTREACHED() << "Cookie exploded expiration failed: " << time_string; 564 // NOTREACHED() << "Cookie exploded expiration failed: " << time_string;
562 565
563 return Time(); 566 return Time();
564 } 567 }
565 568
566 bool CookieMonster::DomainIsHostOnly(const std::string& domain_string) { 569 bool CookieMonster::DomainIsHostOnly(const std::string& domain_string) {
567 return (domain_string.empty() || domain_string[0] != '.'); 570 return (domain_string.empty() || domain_string[0] != '.');
568 } 571 }
569 572
573
574
575 void InvokeSetCookiesCallbackOnOtherThread(
erikwright (departed) 2011/08/11 13:58:24 These functions should not be needed if you define
ycxiao 2011/08/12 02:35:24 Since callback is not reference counted thread saf
576 const CookieMonster::SetCookiesCallback& callback,
577 bool success) {
578 if (!callback.is_null())
579 callback.Run(success);
580 }
581
582 void InvokeGetCookieListCallbackOnOtherThread(
583 const CookieMonster::GetCookieListCallback& callback,
584 const CookieList& cookies) {
585 if (!callback.is_null())
586 callback.Run(cookies);
587 }
588
589 void InvokeDeleteCallbackOnOtherThread(
590 const CookieMonster::DeleteCallback& callback,
591 int num_deleted) {
592 if (!callback.is_null())
593 callback.Run(num_deleted);
594 }
595
596 void InvokeGetCookiesCallbackOnOtherThread(
597 const CookieMonster::GetCookiesCallback& callback,
598 const std::string& cookie) {
599 if (!callback.is_null())
600 callback.Run(cookie);
601 }
602
603 void InvokeGetCookieInfoCallbackOnOtherThread(
604 const CookieMonster::GetCookieInfoCallback& callback,
605 const std::string& cookie_line,
606 const std::vector<CookieStore::CookieInfo>& cookie_infos) {
607 if (!callback.is_null()) {
608 std::string line = cookie_line;
609 std::vector<CookieStore::CookieInfo> info = cookie_infos;
610 callback.Run(&line, &info);
611 }
612 }
613
614 // Asynchronous CookieMonster API
615
616 void CookieMonster::SetCookieWithDetailsAsync(
617 const GURL& url, const std::string& name, const std::string& value,
618 const std::string& domain, const std::string& path,
619 const base::Time& expiration_time, bool secure, bool http_only,
620 const SetCookiesCallback& callback) {
621 GURL gurl = url;
622 CookieAPIParameters parameters;
623 parameters.url = &gurl;
624 parameters.name = name;
625 parameters.value = value;
626 parameters.domain = domain;
627 parameters.path =path;
628 parameters.expiration_time = expiration_time;
629 parameters.secure = secure;
630 parameters.http_only = http_only;
631
632 scoped_refptr<base::MessageLoopProxy> thread(
633 base::MessageLoopProxy::CreateForCurrentThread());
634 base::Closure task = base::Bind(&CookieMonster::SetCookieWithDetailsTask,
635 this, callback, parameters, thread);
636 DoCookieTask(task);
637 }
638
639 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) {
640 scoped_refptr<base::MessageLoopProxy> thread(
641 base::MessageLoopProxy::CreateForCurrentThread());
642 base::Closure task = base::Bind(&CookieMonster::GetAllCookiesTask,
643 this, callback, thread);
644 DoCookieTask(task);
645 }
646
647
648 void CookieMonster::GetAllCookiesForURLWithOptionsAsync(
649 const GURL& url,
650 const CookieOptions& options,
651 const GetCookieListCallback& callback) {
652 GURL gurl = url;
653 CookieAPIParameters parameters;
654 parameters.url = &gurl;
655 parameters.options = options;
656
657 scoped_refptr<base::MessageLoopProxy> thread(
658 base::MessageLoopProxy::CreateForCurrentThread());
659 base::Closure task =
660 base::Bind(&CookieMonster::GetAllCookiesForURLWithOptionsTask,
661 this, callback, parameters, thread);
662 DoCookieTask(task);
663 }
664
665 void CookieMonster::GetAllCookiesForURLAsync(
666 const GURL& url, const GetCookieListCallback& callback) {
667 GURL gurl = url;
668 CookieAPIParameters parameters;
669 parameters.url = &gurl;
670
671 scoped_refptr<base::MessageLoopProxy> thread(
672 base::MessageLoopProxy::CreateForCurrentThread());
673 base::Closure task = base::Bind(&CookieMonster::GetAllCookiesForURLTask,
674 this, callback, parameters, thread);
675 DoCookieTask(task);
676 }
677
678 void CookieMonster::DeleteAllAsync(bool sync_to_store,
679 const DeleteCallback& callback) {
680 CookieAPIParameters parameters;
681 parameters.sync_to_store = sync_to_store;
682
683 scoped_refptr<base::MessageLoopProxy> thread(
684 base::MessageLoopProxy::CreateForCurrentThread());
685 base::Closure task = base::Bind(&CookieMonster::DeleteAllTask,
686 this, callback, parameters, thread);
687 DoCookieTask(task);
688 }
689
690 void CookieMonster::DeleteAllCreatedBetweenAsync(
691 const Time& delete_begin, const Time& delete_end,
692 bool sync_to_store,
693 const DeleteCallback& callback) {
694 CookieAPIParameters parameters;
695 parameters.delete_begin = delete_begin;
696 parameters.delete_end = delete_end;
697 parameters.sync_to_store = sync_to_store;
698
699 scoped_refptr<base::MessageLoopProxy> thread(
700 base::MessageLoopProxy::CreateForCurrentThread());
701 base::Closure task = base::Bind(&CookieMonster::DeleteAllCreatedBetweenTask,
702 this, callback, parameters, thread);
703 DoCookieTask(task);
704 }
705
706 void CookieMonster::DeleteAllForHostAsync(
707 const GURL& url, const DeleteCallback& callback) {
708 GURL gurl = url;
709 CookieAPIParameters parameters;
710 parameters.url = &gurl;
711
712 scoped_refptr<base::MessageLoopProxy> thread(
713 base::MessageLoopProxy::CreateForCurrentThread());
714 base::Closure task = base::Bind(&CookieMonster::DeleteAllForHostTask,
715 this, callback, parameters, thread);
716 DoCookieTask(task);
717 }
718
719 void CookieMonster::DeleteCanonicalCookieAsync(
720 const CanonicalCookie& cookie,
721 const DeleteCookieCallback& callback) {
722 CanonicalCookie delete_cookie = cookie;
723 CookieAPIParameters parameters;
724 parameters.cookie = &delete_cookie;
725
726 scoped_refptr<base::MessageLoopProxy> thread(
727 base::MessageLoopProxy::CreateForCurrentThread());
728 base::Closure task = base::Bind(&CookieMonster::DeleteCanonicalCookieTask,
729 this, callback, parameters, thread);
730 DoCookieTask(task);
731 }
732
733 void CookieMonster::SetCookieWithOptionsAsync(
734 const GURL& url,
735 const std::string& cookie_line,
736 const CookieOptions& options,
737 const SetCookiesCallback& callback) {
738 GURL gurl = url;
739 CookieAPIParameters parameters;
740 parameters.url = &gurl;
741 parameters.cookie_line = cookie_line;
742 parameters.options = options;
743
744 scoped_refptr<base::MessageLoopProxy> thread(
745 base::MessageLoopProxy::CreateForCurrentThread());
746 base::Closure task = base::Bind(&CookieMonster::SetCookieWithOptionsTask,
747 this, callback, parameters, thread);
748 DoCookieTask(task);
749 }
750
751 void CookieMonster::GetCookiesWithOptionsAsync(
752 const GURL& url, const CookieOptions& options,
753 const GetCookiesCallback& callback) {
754 GURL gurl = url;
755 CookieAPIParameters parameters;
756 parameters.url = &gurl;
757 parameters.options = options;
758
759 scoped_refptr<base::MessageLoopProxy> thread(
760 base::MessageLoopProxy::CreateForCurrentThread());
761 base::Closure task = base::Bind(&CookieMonster::GetCookiesWithOptionsTask,
762 this, callback, parameters, thread);
763 DoCookieTask(task);
764 }
765
766 void CookieMonster::GetCookiesWithInfoAsync(
767 const GURL& url,
768 const CookieOptions& options,
769 const GetCookieInfoCallback& callback) {
770 GURL gurl = url;
771 CookieAPIParameters parameters;
772 parameters.url = &gurl;
773 parameters.options = options;
774
775 scoped_refptr<base::MessageLoopProxy> thread(
776 base::MessageLoopProxy::CreateForCurrentThread());
777 base::Closure task = base::Bind(&CookieMonster::GetCookiesWithInfoTask,
778 this, callback, parameters, thread);
779 DoCookieTask(task);
780 }
781
782 void CookieMonster::DeleteCookieAsync(const GURL& url,
783 const std::string& cookie_name,
784 const base::Closure& callback) {
785 GURL gurl = url;
786 CookieAPIParameters parameters;
787 parameters.url = &gurl;
788 parameters.name = cookie_name;
789
790 scoped_refptr<base::MessageLoopProxy> thread(
791 base::MessageLoopProxy::CreateForCurrentThread());
792 base::Closure task = base::Bind(&CookieMonster::DeleteCookieTask,
793 this, callback, parameters, thread);
794 DoCookieTask(task);
795 }
796
797 void CookieMonster::DoCookieTask(const base::Closure& task) {
erikwright (departed) 2011/08/11 13:58:24 We discussed offline a more minimal use of locking
ycxiao 2011/08/12 02:35:24 Done.
798 DCHECK(!task.is_null());
799 InitIfNecessary();
800 base::AutoLock autolock(lock_);
801 if (loaded_) {
802 task.Run();
803 } else {
804 queue_.push_back(task);
805 }
806 }
807
808 void CookieMonster::SetCookieWithDetailsTask(
erikwright (departed) 2011/08/11 13:58:24 See comments in .h file regarding CookieMonsterTas
ycxiao 2011/08/12 02:35:24 Done.
809 const SetCookiesCallback& callback,
810 const CookieAPIParameters& para,
811 scoped_refptr<base::MessageLoopProxy> thread) {
812 bool success = SetCookieWithDetails(
813 *para.url, para.name, para.value, para.domain, para.path,
814 para.expiration_time, para.secure, para.http_only);
815 if (!callback.is_null()) {
816 if (thread->BelongsToCurrentThread()) {
817 base::AutoUnlock autounlock(lock_);
818 callback.Run(success);
819 } else {
820 thread->PostTask(
821 FROM_HERE, base::Bind(&InvokeSetCookiesCallbackOnOtherThread,
822 callback, success));
823 }
824 }
825 }
826
827 void CookieMonster::GetAllCookiesTask(
828 const GetCookieListCallback& callback,
829 scoped_refptr<base::MessageLoopProxy> thread) {
830 if (!callback.is_null()) {
831 CookieList cookies = GetAllCookies();
832 if (thread->BelongsToCurrentThread()) {
833 base::AutoUnlock autounlock(lock_);
834 callback.Run(cookies);
835 } else {
836 thread->PostTask(
837 FROM_HERE, base::Bind(&InvokeGetCookieListCallbackOnOtherThread,
838 callback, cookies));
839 }
840 }
841 }
842
843 void CookieMonster::GetAllCookiesForURLWithOptionsTask(
844 const GetCookieListCallback& callback,
845 const CookieAPIParameters& para,
846 scoped_refptr<base::MessageLoopProxy> thread) {
847 if (!callback.is_null()) {
848 CookieList cookies = GetAllCookiesForURLWithOptions(
849 *para.url, para.options);
850 if (thread->BelongsToCurrentThread()) {
851 base::AutoUnlock autounlock(lock_);
852 callback.Run(cookies);
853 } else {
854 thread->PostTask(
855 FROM_HERE, base::Bind(&InvokeGetCookieListCallbackOnOtherThread,
856 callback, cookies));
857 }
858 }
859 }
860
861 void CookieMonster::GetAllCookiesForURLTask(
862 const GetCookieListCallback& callback,
863 const CookieAPIParameters& para,
864 scoped_refptr<base::MessageLoopProxy> thread) {
865 if (!callback.is_null()) {
866 CookieList cookies = GetAllCookiesForURL(*para.url);
867 if (thread->BelongsToCurrentThread()) {
868 base::AutoUnlock autounlock(lock_);
869 callback.Run(cookies);
870 } else {
871 thread->PostTask(
872 FROM_HERE, base::Bind(&InvokeGetCookieListCallbackOnOtherThread,
873 callback, cookies));
874 }
875 }
876 }
877
878 void CookieMonster::DeleteAllTask(
879 const DeleteCallback& callback,
880 const CookieAPIParameters& para,
881 scoped_refptr<base::MessageLoopProxy> thread) {
882 int num_deleted = DeleteAll(para.sync_to_store);
883 if (!callback.is_null()) {
884 if (thread->BelongsToCurrentThread()) {
885 base::AutoUnlock autounlock(lock_);
886 callback.Run(num_deleted);
887 } else {
888 thread->PostTask(
889 FROM_HERE, base::Bind(&InvokeDeleteCallbackOnOtherThread,
890 callback, num_deleted));
891 }
892 }
893 }
894
895 void CookieMonster::DeleteAllCreatedBetweenTask(
896 const DeleteCallback& callback,
897 const CookieAPIParameters& para,
898 scoped_refptr<base::MessageLoopProxy> thread) {
899 int num_deleted = DeleteAllCreatedBetween(
900 para.delete_begin, para.delete_end, para.sync_to_store);
901 if (!callback.is_null()) {
902 if (thread->BelongsToCurrentThread()) {
903 base::AutoUnlock autounlock(lock_);
904 callback.Run(num_deleted);
905 } else {
906 thread->PostTask(
907 FROM_HERE, base::Bind(&InvokeDeleteCallbackOnOtherThread,
908 callback, num_deleted));
909 }
910 }
911 }
912
913 void CookieMonster::DeleteAllForHostTask(
914 const DeleteCallback& callback,
915 const CookieAPIParameters& para,
916 scoped_refptr<base::MessageLoopProxy> thread) {
917 int num_deleted = DeleteAllForHost(*para.url);
918 if (!callback.is_null()) {
919 if (thread->BelongsToCurrentThread()) {
920 base::AutoUnlock autounlock(lock_);
921 callback.Run(num_deleted);
922 } else {
923 thread->PostTask(
924 FROM_HERE, base::Bind(&InvokeDeleteCallbackOnOtherThread,
925 callback, num_deleted));
926 }
927 }
928 }
929
930 void CookieMonster::DeleteCanonicalCookieTask(
931 const DeleteCookieCallback& callback,
932 const CookieAPIParameters& para,
933 scoped_refptr<base::MessageLoopProxy> thread) {
934 bool result = DeleteCanonicalCookie(*para.cookie);
935 if (!callback.is_null()) {
936 if (thread->BelongsToCurrentThread()) {
937 base::AutoUnlock autounlock(lock_);
938 callback.Run(result);
939 } else {
940 thread->PostTask(
941 FROM_HERE, base::Bind(&InvokeSetCookiesCallbackOnOtherThread,
942 callback, result));
943 }
944 }
945 }
946
947 void CookieMonster::SetCookieWithOptionsTask(
948 const SetCookiesCallback& callback,
949 const CookieAPIParameters& para,
950 scoped_refptr<base::MessageLoopProxy> thread) {
951 bool result = SetCookieWithOptions(*para.url, para.cookie_line, para.options);
952 if (!callback.is_null()) {
953 if (thread->BelongsToCurrentThread()) {
954 base::AutoUnlock autounlock(lock_);
955 callback.Run(result);
956 } else {
957 thread->PostTask(
958 FROM_HERE, base::Bind(&InvokeSetCookiesCallbackOnOtherThread,
959 callback, result));
960 }
961 }
962 }
963
964 void CookieMonster::GetCookiesWithOptionsTask(
965 const GetCookiesCallback& callback,
966 const CookieAPIParameters& para,
967 scoped_refptr<base::MessageLoopProxy> thread) {
968 std::string cookie = GetCookiesWithOptions(*para.url, para.options);
969 if (!callback.is_null()) {
970 if (thread->BelongsToCurrentThread()) {
971 base::AutoUnlock autounlock(lock_);
972 callback.Run(cookie);
973 } else {
974 thread->PostTask(
975 FROM_HERE, base::Bind(&InvokeGetCookiesCallbackOnOtherThread,
976 callback, cookie));
977 }
978 }
979 }
980
981 void CookieMonster::GetCookiesWithInfoTask(
982 const GetCookieInfoCallback& callback,
983 const CookieAPIParameters& para,
984 scoped_refptr<base::MessageLoopProxy> thread) {
985 if (!callback.is_null()) {
986 std::string cookie_line;
987 std::vector<CookieInfo> cookie_infos;
988 GetCookiesWithInfo(*para.url, para.options, &cookie_line, &cookie_infos);
989 if (thread->BelongsToCurrentThread()) {
990 base::AutoUnlock autounlock(lock_);
991 callback.Run(&cookie_line, &cookie_infos);
992 } else {
993 thread->PostTask(
994 FROM_HERE, base::Bind(&InvokeGetCookieInfoCallbackOnOtherThread,
995 callback, cookie_line, cookie_infos));
996 }
997 }
998 }
999
1000 void CookieMonster::DeleteCookieTask(
1001 const base::Closure& callback,
1002 const CookieAPIParameters& para,
1003 scoped_refptr<base::MessageLoopProxy> thread) {
1004 DeleteCookie(*para.url, para.name);
1005 if (!callback.is_null()) {
1006 if (thread->BelongsToCurrentThread()) {
1007 base::AutoUnlock autounlock(lock_);
1008 callback.Run();
1009 } else {
1010 thread->PostTask(FROM_HERE, callback);
1011 }
1012 }
1013 }
1014
570 bool CookieMonster::SetCookieWithDetails( 1015 bool CookieMonster::SetCookieWithDetails(
571 const GURL& url, const std::string& name, const std::string& value, 1016 const GURL& url, const std::string& name, const std::string& value,
572 const std::string& domain, const std::string& path, 1017 const std::string& domain, const std::string& path,
573 const base::Time& expiration_time, bool secure, bool http_only) { 1018 const base::Time& expiration_time, bool secure, bool http_only) {
574 base::AutoLock autolock(lock_); 1019 lock_.AssertAcquired();
575 1020
576 if (!HasCookieableScheme(url)) 1021 if (!HasCookieableScheme(url))
577 return false; 1022 return false;
578 1023
579 InitIfNecessary();
580
581 Time creation_time = CurrentTime(); 1024 Time creation_time = CurrentTime();
582 last_time_seen_ = creation_time; 1025 last_time_seen_ = creation_time;
583 1026
584 // TODO(abarth): Take these values as parameters. 1027 // TODO(abarth): Take these values as parameters.
585 std::string mac_key; 1028 std::string mac_key;
586 std::string mac_algorithm; 1029 std::string mac_algorithm;
587 1030
588 scoped_ptr<CanonicalCookie> cc; 1031 scoped_ptr<CanonicalCookie> cc;
589 cc.reset(CanonicalCookie::Create( 1032 cc.reset(CanonicalCookie::Create(
590 url, name, value, domain, path, 1033 url, name, value, domain, path,
591 mac_key, mac_algorithm, 1034 mac_key, mac_algorithm,
592 creation_time, expiration_time, 1035 creation_time, expiration_time,
593 secure, http_only)); 1036 secure, http_only));
594 1037
595 if (!cc.get()) 1038 if (!cc.get())
596 return false; 1039 return false;
597 1040
598 CookieOptions options; 1041 CookieOptions options;
599 options.set_include_httponly(); 1042 options.set_include_httponly();
600 return SetCanonicalCookie(&cc, creation_time, options); 1043 return SetCanonicalCookie(&cc, creation_time, options);
601 } 1044 }
602 1045
603 void CookieMonster::SetCookieWithDetailsAsync( 1046 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_); 1047 base::AutoLock autolock(lock_);
618 InitIfNecessary(); 1048 InitIfNecessary();
619 for (net::CookieList::const_iterator iter = list.begin(); 1049 for (net::CookieList::const_iterator iter = list.begin();
620 iter != list.end(); ++iter) { 1050 iter != list.end(); ++iter) {
621 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie; 1051 scoped_ptr<net::CookieMonster::CanonicalCookie> cookie;
622 cookie.reset(new net::CookieMonster::CanonicalCookie(*iter)); 1052 cookie.reset(new net::CookieMonster::CanonicalCookie(*iter));
623 net::CookieOptions options; 1053 net::CookieOptions options;
624 options.set_include_httponly(); 1054 options.set_include_httponly();
625 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), 1055 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(),
626 options)) { 1056 options)) {
627 return false; 1057 return false;
628 } 1058 }
629 } 1059 }
630 return true; 1060 return true;
631 } 1061 }
632 1062
633 CookieList CookieMonster::GetAllCookies() { 1063 CookieList CookieMonster::GetAllCookies() {
634 base::AutoLock autolock(lock_); 1064 lock_.AssertAcquired();
635 InitIfNecessary();
636 1065
637 // This function is being called to scrape the cookie list for management UI 1066 // 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 1067 // 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 1068 // 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 1069 // is already slow enough) that it's OK to take the time to garbage collect
641 // the expired cookies now. 1070 // the expired cookies now.
642 // 1071 //
643 // Note that this does not prune cookies to be below our limits (if we've 1072 // Note that this does not prune cookies to be below our limits (if we've
644 // exceeded them) the way that calling GarbageCollect() would. 1073 // exceeded them) the way that calling GarbageCollect() would.
645 GarbageCollectExpired(Time::Now(), 1074 GarbageCollectExpired(Time::Now(),
(...skipping 10 matching lines...) Expand all
656 1085
657 CookieList cookie_list; 1086 CookieList cookie_list;
658 cookie_list.reserve(cookie_ptrs.size()); 1087 cookie_list.reserve(cookie_ptrs.size());
659 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); 1088 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
660 it != cookie_ptrs.end(); ++it) 1089 it != cookie_ptrs.end(); ++it)
661 cookie_list.push_back(**it); 1090 cookie_list.push_back(**it);
662 1091
663 return cookie_list; 1092 return cookie_list;
664 } 1093 }
665 1094
666 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) {
667 if (!callback.is_null())
668 callback.Run(GetAllCookies());
669 }
670
671 CookieList CookieMonster::GetAllCookiesForURLWithOptions( 1095 CookieList CookieMonster::GetAllCookiesForURLWithOptions(
672 const GURL& url, 1096 const GURL& url,
673 const CookieOptions& options) { 1097 const CookieOptions& options) {
674 base::AutoLock autolock(lock_); 1098 lock_.AssertAcquired();
675 InitIfNecessary();
676 1099
677 std::vector<CanonicalCookie*> cookie_ptrs; 1100 std::vector<CanonicalCookie*> cookie_ptrs;
678 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs); 1101 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs);
679 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); 1102 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
680 1103
681 CookieList cookies; 1104 CookieList cookies;
682 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); 1105 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
683 it != cookie_ptrs.end(); it++) 1106 it != cookie_ptrs.end(); it++)
684 cookies.push_back(**it); 1107 cookies.push_back(**it);
685 1108
686 return cookies; 1109 return cookies;
687 } 1110 }
688 1111
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) { 1112 CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) {
698 CookieOptions options; 1113 CookieOptions options;
699 options.set_include_httponly(); 1114 options.set_include_httponly();
700 1115
701 return GetAllCookiesForURLWithOptions(url, options); 1116 return GetAllCookiesForURLWithOptions(url, options);
702 } 1117 }
703 1118
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) { 1119 int CookieMonster::DeleteAll(bool sync_to_store) {
711 base::AutoLock autolock(lock_); 1120 lock_.AssertAcquired();
712 if (sync_to_store)
713 InitIfNecessary();
714 1121
715 int num_deleted = 0; 1122 int num_deleted = 0;
716 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { 1123 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
717 CookieMap::iterator curit = it; 1124 CookieMap::iterator curit = it;
718 ++it; 1125 ++it;
719 InternalDeleteCookie(curit, sync_to_store, 1126 InternalDeleteCookie(curit, sync_to_store,
720 sync_to_store ? DELETE_COOKIE_EXPLICIT : 1127 sync_to_store ? DELETE_COOKIE_EXPLICIT :
721 DELETE_COOKIE_DONT_RECORD /* Destruction. */); 1128 DELETE_COOKIE_DONT_RECORD /* Destruction. */);
722 ++num_deleted; 1129 ++num_deleted;
723 } 1130 }
724 1131
725 return num_deleted; 1132 return num_deleted;
726 } 1133 }
727 1134
728 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin, 1135 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin,
729 const Time& delete_end, 1136 const Time& delete_end,
730 bool sync_to_store) { 1137 bool sync_to_store) {
731 base::AutoLock autolock(lock_); 1138 lock_.AssertAcquired();
732 InitIfNecessary();
733 1139
734 int num_deleted = 0; 1140 int num_deleted = 0;
735 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { 1141 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
736 CookieMap::iterator curit = it; 1142 CookieMap::iterator curit = it;
737 CanonicalCookie* cc = curit->second; 1143 CanonicalCookie* cc = curit->second;
738 ++it; 1144 ++it;
739 1145
740 if (cc->CreationDate() >= delete_begin && 1146 if (cc->CreationDate() >= delete_begin &&
741 (delete_end.is_null() || cc->CreationDate() < delete_end)) { 1147 (delete_end.is_null() || cc->CreationDate() < delete_end)) {
742 InternalDeleteCookie(curit, sync_to_store, DELETE_COOKIE_EXPLICIT); 1148 InternalDeleteCookie(curit, sync_to_store, DELETE_COOKIE_EXPLICIT);
743 ++num_deleted; 1149 ++num_deleted;
744 } 1150 }
745 } 1151 }
746 1152
747 return num_deleted; 1153 return num_deleted;
748 } 1154 }
749 1155
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) { 1156 int CookieMonster::DeleteAllForHost(const GURL& url) {
761 base::AutoLock autolock(lock_); 1157 lock_.AssertAcquired();
762 InitIfNecessary();
763 1158
764 if (!HasCookieableScheme(url)) 1159 if (!HasCookieableScheme(url))
765 return 0; 1160 return 0;
766 1161
767 const std::string scheme(url.scheme()); 1162 const std::string scheme(url.scheme());
768 const std::string host(url.host()); 1163 const std::string host(url.host());
769 1164
770 // We store host cookies in the store by their canonical host name; 1165 // 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 1166 // domain cookies are stored with a leading ".". So this is a pretty
772 // simple lookup and per-cookie delete. 1167 // simple lookup and per-cookie delete.
773 int num_deleted = 0; 1168 int num_deleted = 0;
774 for (CookieMapItPair its = cookies_.equal_range(GetKey(host)); 1169 for (CookieMapItPair its = cookies_.equal_range(GetKey(host));
775 its.first != its.second;) { 1170 its.first != its.second;) {
776 CookieMap::iterator curit = its.first; 1171 CookieMap::iterator curit = its.first;
777 ++its.first; 1172 ++its.first;
778 1173
779 const CanonicalCookie* const cc = curit->second; 1174 const CanonicalCookie* const cc = curit->second;
780 1175
781 // Delete only on a match as a host cookie. 1176 // Delete only on a match as a host cookie.
782 if (cc->IsHostCookie() && cc->IsDomainMatch(scheme, host)) { 1177 if (cc->IsHostCookie() && cc->IsDomainMatch(scheme, host)) {
783 num_deleted++; 1178 num_deleted++;
784 1179
785 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); 1180 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT);
786 } 1181 }
787 } 1182 }
788 return num_deleted; 1183 return num_deleted;
789 } 1184 }
790 1185
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) { 1186 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) {
799 base::AutoLock autolock(lock_); 1187 lock_.AssertAcquired();
800 InitIfNecessary();
801 1188
802 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); 1189 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain()));
803 its.first != its.second; ++its.first) { 1190 its.first != its.second; ++its.first) {
804 // The creation date acts as our unique index... 1191 // The creation date acts as our unique index...
805 if (its.first->second->CreationDate() == cookie.CreationDate()) { 1192 if (its.first->second->CreationDate() == cookie.CreationDate()) {
806 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT); 1193 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT);
807 return true; 1194 return true;
808 } 1195 }
809 } 1196 }
810 return false; 1197 return false;
811 } 1198 }
812 1199
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( 1200 void CookieMonster::SetCookieableSchemes(
822 const char* schemes[], size_t num_schemes) { 1201 const char* schemes[], size_t num_schemes) {
823 base::AutoLock autolock(lock_); 1202 base::AutoLock autolock(lock_);
824 1203
825 // Cookieable Schemes must be set before first use of function. 1204 // Cookieable Schemes must be set before first use of function.
826 DCHECK(!initialized_); 1205 DCHECK(!initialized_);
827 1206
828 cookieable_schemes_.clear(); 1207 cookieable_schemes_.clear();
829 cookieable_schemes_.insert(cookieable_schemes_.end(), 1208 cookieable_schemes_.insert(cookieable_schemes_.end(),
830 schemes, schemes + num_schemes); 1209 schemes, schemes + num_schemes);
(...skipping 22 matching lines...) Expand all
853 base::AutoLock autolock(lock_); 1232 base::AutoLock autolock(lock_);
854 if (initialized_ && store_) 1233 if (initialized_ && store_)
855 store_->Flush(completion_task); 1234 store_->Flush(completion_task);
856 else if (completion_task) 1235 else if (completion_task)
857 MessageLoop::current()->PostTask(FROM_HERE, completion_task); 1236 MessageLoop::current()->PostTask(FROM_HERE, completion_task);
858 } 1237 }
859 1238
860 bool CookieMonster::SetCookieWithOptions(const GURL& url, 1239 bool CookieMonster::SetCookieWithOptions(const GURL& url,
861 const std::string& cookie_line, 1240 const std::string& cookie_line,
862 const CookieOptions& options) { 1241 const CookieOptions& options) {
863 base::AutoLock autolock(lock_); 1242 lock_.AssertAcquired();
864 1243
865 if (!HasCookieableScheme(url)) { 1244 if (!HasCookieableScheme(url)) {
866 return false; 1245 return false;
867 } 1246 }
868 1247
869 InitIfNecessary();
870
871 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options); 1248 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options);
872 } 1249 }
873 1250
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, 1251 std::string CookieMonster::GetCookiesWithOptions(const GURL& url,
885 const CookieOptions& options) { 1252 const CookieOptions& options) {
886 base::AutoLock autolock(lock_); 1253 lock_.AssertAcquired();
887 InitIfNecessary();
888 1254
889 if (!HasCookieableScheme(url)) 1255 if (!HasCookieableScheme(url))
890 return std::string(); 1256 return std::string();
891 1257
892 TimeTicks start_time(TimeTicks::Now()); 1258 TimeTicks start_time(TimeTicks::Now());
893 1259
894 std::vector<CanonicalCookie*> cookies; 1260 std::vector<CanonicalCookie*> cookies;
895 FindCookiesForHostAndDomain(url, options, true, &cookies); 1261 FindCookiesForHostAndDomain(url, options, true, &cookies);
896 std::sort(cookies.begin(), cookies.end(), CookieSorter); 1262 std::sort(cookies.begin(), cookies.end(), CookieSorter);
897 1263
898 std::string cookie_line = BuildCookieLine(cookies); 1264 std::string cookie_line = BuildCookieLine(cookies);
899 1265
900 histogram_time_get_->AddTime(TimeTicks::Now() - start_time); 1266 histogram_time_get_->AddTime(TimeTicks::Now() - start_time);
901 1267
902 VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line; 1268 VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line;
903 1269
904 return cookie_line; 1270 return cookie_line;
905 } 1271 }
906 1272
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, 1273 void CookieMonster::GetCookiesWithInfo(const GURL& url,
916 const CookieOptions& options, 1274 const CookieOptions& options,
917 std::string* cookie_line, 1275 std::string* cookie_line,
918 std::vector<CookieInfo>* cookie_infos) { 1276 std::vector<CookieInfo>* cookie_infos) {
919 DCHECK(cookie_line->empty()); 1277 DCHECK(cookie_line->empty());
920 DCHECK(cookie_infos->empty()); 1278 DCHECK(cookie_infos->empty());
921 1279
922 base::AutoLock autolock(lock_); 1280 lock_.AssertAcquired();
923 InitIfNecessary();
924 1281
925 if (!HasCookieableScheme(url)) 1282 if (!HasCookieableScheme(url))
926 return; 1283 return;
927 1284
928 TimeTicks start_time(TimeTicks::Now()); 1285 TimeTicks start_time(TimeTicks::Now());
929 1286
930 std::vector<CanonicalCookie*> cookies; 1287 std::vector<CanonicalCookie*> cookies;
931 FindCookiesForHostAndDomain(url, options, true, &cookies); 1288 FindCookiesForHostAndDomain(url, options, true, &cookies);
932 std::sort(cookies.begin(), cookies.end(), CookieSorter); 1289 std::sort(cookies.begin(), cookies.end(), CookieSorter);
933 *cookie_line = BuildCookieLine(cookies); 1290 *cookie_line = BuildCookieLine(cookies);
934 1291
935 histogram_time_get_->AddTime(TimeTicks::Now() - start_time); 1292 histogram_time_get_->AddTime(TimeTicks::Now() - start_time);
936 1293
937 TimeTicks mac_start_time = TimeTicks::Now(); 1294 TimeTicks mac_start_time = TimeTicks::Now();
938 BuildCookieInfoList(cookies, cookie_infos); 1295 BuildCookieInfoList(cookies, cookie_infos);
939 histogram_time_mac_->AddTime(TimeTicks::Now() - mac_start_time); 1296 histogram_time_mac_->AddTime(TimeTicks::Now() - mac_start_time);
940 } 1297 }
941 1298
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, 1299 void CookieMonster::DeleteCookie(const GURL& url,
955 const std::string& cookie_name) { 1300 const std::string& cookie_name) {
956 base::AutoLock autolock(lock_); 1301 lock_.AssertAcquired();
957 InitIfNecessary();
958 1302
959 if (!HasCookieableScheme(url)) 1303 if (!HasCookieableScheme(url))
960 return; 1304 return;
961 1305
962 CookieOptions options; 1306 CookieOptions options;
963 options.set_include_httponly(); 1307 options.set_include_httponly();
964 // Get the cookies for this host and its domain(s). 1308 // Get the cookies for this host and its domain(s).
965 std::vector<CanonicalCookie*> cookies; 1309 std::vector<CanonicalCookie*> cookies;
966 FindCookiesForHostAndDomain(url, options, true, &cookies); 1310 FindCookiesForHostAndDomain(url, options, true, &cookies);
967 std::set<CanonicalCookie*> matching_cookies; 1311 std::set<CanonicalCookie*> matching_cookies;
968 1312
969 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); 1313 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin();
970 it != cookies.end(); ++it) { 1314 it != cookies.end(); ++it) {
971 if ((*it)->Name() != cookie_name) 1315 if ((*it)->Name() != cookie_name)
972 continue; 1316 continue;
973 if (url.path().find((*it)->Path())) 1317 if (url.path().find((*it)->Path()))
974 continue; 1318 continue;
975 matching_cookies.insert(*it); 1319 matching_cookies.insert(*it);
976 } 1320 }
977 1321
978 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { 1322 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
979 CookieMap::iterator curit = it; 1323 CookieMap::iterator curit = it;
980 ++it; 1324 ++it;
981 if (matching_cookies.find(curit->second) != matching_cookies.end()) { 1325 if (matching_cookies.find(curit->second) != matching_cookies.end()) {
982 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); 1326 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT);
983 } 1327 }
984 } 1328 }
985 } 1329 }
986 1330
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() { 1331 CookieMonster* CookieMonster::GetCookieMonster() {
996 return this; 1332 return this;
997 } 1333 }
998 1334
999 CookieMonster::~CookieMonster() { 1335 CookieMonster::~CookieMonster() {
1336 base::AutoLock autolock(lock_);
1000 DeleteAll(false); 1337 DeleteAll(false);
1001 } 1338 }
1002 1339
1003 bool CookieMonster::SetCookieWithCreationTime(const GURL& url, 1340 bool CookieMonster::SetCookieWithCreationTime(const GURL& url,
1004 const std::string& cookie_line, 1341 const std::string& cookie_line,
1005 const base::Time& creation_time) { 1342 const base::Time& creation_time) {
1343 // Only for CookieMonster unit test usage.
erikwright (departed) 2011/08/11 13:58:24 DCHECK(!store_) << "This method is only to be used
ycxiao 2011/08/12 02:35:24 Done.
1344 DCHECK(!store_);
1006 base::AutoLock autolock(lock_); 1345 base::AutoLock autolock(lock_);
1007 1346
1008 if (!HasCookieableScheme(url)) { 1347 if (!HasCookieableScheme(url)) {
1009 return false; 1348 return false;
1010 } 1349 }
1011 1350
1012 InitIfNecessary(); 1351 InitIfNecessary();
1013 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time, 1352 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time,
1014 CookieOptions()); 1353 CookieOptions());
1015 } 1354 }
1016 1355
1017 void CookieMonster::InitStore() { 1356 void CookieMonster::InitStore() {
1018 DCHECK(store_) << "Store must exist to initialize"; 1357 DCHECK(store_) << "Store must exist to initialize";
1358 store_->Load(base::Bind(&CookieMonster::OnLoaded, this));
1359 }
1019 1360
1020 TimeTicks beginning_time(TimeTicks::Now()); 1361 void CookieMonster::OnLoaded(const std::vector<CanonicalCookie*>& cookies) {
1021
1022 // Initialize the store and sync in any saved persistent cookies. We don't 1362 // 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, 1363 // care if it's expired, insert it so it can be garbage collected, removed,
1024 // and sync'd. 1364 // and sync'd.
1025 std::vector<CanonicalCookie*> cookies; 1365 base::AutoLock autolock(lock_);
1026 // Reserve space for the maximum amount of cookies a database should have. 1366 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 1367
1031 // Avoid ever letting cookies with duplicate creation times into the store; 1368 // 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 1369 // that way we don't have to worry about what sections of code are safe
1033 // to call while it's in that state. 1370 // to call while it's in that state.
1034 std::set<int64> creation_times; 1371 std::set<int64> creation_times;
1035 1372
1036 // Presumably later than any access time in the store. 1373 // Presumably later than any access time in the store.
1037 Time earliest_access_time; 1374 Time earliest_access_time;
1038 1375
1039 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); 1376 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin();
(...skipping 20 matching lines...) Expand all
1060 } 1397 }
1061 earliest_access_time_= earliest_access_time; 1398 earliest_access_time_= earliest_access_time;
1062 1399
1063 // After importing cookies from the PersistentCookieStore, verify that 1400 // After importing cookies from the PersistentCookieStore, verify that
1064 // none of our other constraints are violated. 1401 // none of our other constraints are violated.
1065 // 1402 //
1066 // In particular, the backing store might have given us duplicate cookies. 1403 // In particular, the backing store might have given us duplicate cookies.
1067 EnsureCookiesMapIsValid(); 1404 EnsureCookiesMapIsValid();
1068 1405
1069 histogram_time_load_->AddTime(TimeTicks::Now() - beginning_time); 1406 histogram_time_load_->AddTime(TimeTicks::Now() - beginning_time);
1407
1408 // Invoke the task queue of cookie request.
1409 InvokeQueue();
1410 }
1411
1412 void CookieMonster::InvokeQueue() {
erikwright (departed) 2011/08/11 13:58:24 Refer to offline discussion about minimal lock hol
ycxiao 2011/08/12 02:35:24 Done.
1413 while (true) {
1414 if (queue_.empty()) {
1415 loaded_ = true;
1416 break;
1417 }
1418 queue_.front().Run();
1419 queue_.pop_front();
1420 }
1070 } 1421 }
1071 1422
1072 void CookieMonster::EnsureCookiesMapIsValid() { 1423 void CookieMonster::EnsureCookiesMapIsValid() {
1073 lock_.AssertAcquired(); 1424 lock_.AssertAcquired();
1074 1425
1075 int num_duplicates_trimmed = 0; 1426 int num_duplicates_trimmed = 0;
1076 1427
1077 // Iterate through all the of the cookies, grouped by host. 1428 // Iterate through all the of the cookies, grouped by host.
1078 CookieMap::iterator prev_range_end = cookies_.begin(); 1429 CookieMap::iterator prev_range_end = cookies_.begin();
1079 while (prev_range_end != cookies_.end()) { 1430 while (prev_range_end != cookies_.end()) {
(...skipping 1065 matching lines...) Expand 10 before | Expand all | Expand 10 after
2145 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES) 2496 #if defined(ENABLE_PERSISTENT_SESSION_COOKIES)
2146 // Mobile apps can sometimes be shut down without any warning, so the session 2497 // 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. 2498 // cookie has to be persistent and given a default expiration time.
2148 expiry_date_ = base::Time::Now() + 2499 expiry_date_ = base::Time::Now() +
2149 base::TimeDelta::FromDays(kPersistentSessionCookieExpiryInDays); 2500 base::TimeDelta::FromDays(kPersistentSessionCookieExpiryInDays);
2150 has_expires_ = true; 2501 has_expires_ = true;
2151 #endif 2502 #endif
2152 } 2503 }
2153 2504
2154 CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create( 2505 CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create(
2506 const GURL& url,
2507 const ParsedCookie& pc) {
2508 if (!pc.IsValid()) {
2509 return NULL;
2510 }
2511
2512 std::string domain_string;
2513 if (!GetCookieDomain(url, pc, &domain_string)) {
2514 return NULL;
2515 }
2516 std::string path_string = CanonPath(url, pc);
2517 std::string mac_key = pc.HasMACKey() ? pc.MACKey() : std::string();
2518 std::string mac_algorithm = pc.HasMACAlgorithm() ?
2519 pc.MACAlgorithm() : std::string();
2520 Time creation_time = Time::Now();
2521 Time expiration_time;
2522 if (pc.HasExpires())
2523 expiration_time = net::CookieMonster::ParseCookieTime(pc.Expires());
2524
2525 return (Create(url, pc.Name(), pc.Value(), domain_string, path_string,
2526 mac_key, mac_algorithm, creation_time, expiration_time,
2527 pc.IsSecure(), pc.IsHttpOnly()));
2528 }
2529
2530 CookieMonster::CanonicalCookie* CookieMonster::CanonicalCookie::Create(
2155 const GURL& url, 2531 const GURL& url,
2156 const std::string& name, 2532 const std::string& name,
2157 const std::string& value, 2533 const std::string& value,
2158 const std::string& domain, 2534 const std::string& domain,
2159 const std::string& path, 2535 const std::string& path,
2160 const std::string& mac_key, 2536 const std::string& mac_key,
2161 const std::string& mac_algorithm, 2537 const std::string& mac_algorithm,
2162 const base::Time& creation, 2538 const base::Time& creation,
2163 const base::Time& expiration, 2539 const base::Time& expiration,
2164 bool secure, 2540 bool secure,
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
2276 std::string CookieMonster::CanonicalCookie::DebugString() const { 2652 std::string CookieMonster::CanonicalCookie::DebugString() const {
2277 return base::StringPrintf( 2653 return base::StringPrintf(
2278 "name: %s value: %s domain: %s path: %s creation: %" 2654 "name: %s value: %s domain: %s path: %s creation: %"
2279 PRId64, 2655 PRId64,
2280 name_.c_str(), value_.c_str(), 2656 name_.c_str(), value_.c_str(),
2281 domain_.c_str(), path_.c_str(), 2657 domain_.c_str(), path_.c_str(),
2282 static_cast<int64>(creation_date_.ToTimeT())); 2658 static_cast<int64>(creation_date_.ToTimeT()));
2283 } 2659 }
2284 2660
2285 } // namespace 2661 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698