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