| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 } | 496 } |
| 497 | 497 |
| 498 bool CookieMonster::DomainIsHostOnly(const std::string& domain_string) { | 498 bool CookieMonster::DomainIsHostOnly(const std::string& domain_string) { |
| 499 return (domain_string.empty() || domain_string[0] != '.'); | 499 return (domain_string.empty() || domain_string[0] != '.'); |
| 500 } | 500 } |
| 501 | 501 |
| 502 bool CookieMonster::SetCookieWithDetails( | 502 bool CookieMonster::SetCookieWithDetails( |
| 503 const GURL& url, const std::string& name, const std::string& value, | 503 const GURL& url, const std::string& name, const std::string& value, |
| 504 const std::string& domain, const std::string& path, | 504 const std::string& domain, const std::string& path, |
| 505 const base::Time& expiration_time, bool secure, bool http_only) { | 505 const base::Time& expiration_time, bool secure, bool http_only) { |
| 506 AutoLock autolock(lock_); | 506 base::AutoLock autolock(lock_); |
| 507 | 507 |
| 508 if (!HasCookieableScheme(url)) | 508 if (!HasCookieableScheme(url)) |
| 509 return false; | 509 return false; |
| 510 | 510 |
| 511 InitIfNecessary(); | 511 InitIfNecessary(); |
| 512 | 512 |
| 513 Time creation_time = CurrentTime(); | 513 Time creation_time = CurrentTime(); |
| 514 last_time_seen_ = creation_time; | 514 last_time_seen_ = creation_time; |
| 515 | 515 |
| 516 scoped_ptr<CanonicalCookie> cc; | 516 scoped_ptr<CanonicalCookie> cc; |
| 517 cc.reset(CanonicalCookie::Create( | 517 cc.reset(CanonicalCookie::Create( |
| 518 url, name, value, domain, path, | 518 url, name, value, domain, path, |
| 519 creation_time, expiration_time, | 519 creation_time, expiration_time, |
| 520 secure, http_only)); | 520 secure, http_only)); |
| 521 | 521 |
| 522 if (!cc.get()) | 522 if (!cc.get()) |
| 523 return false; | 523 return false; |
| 524 | 524 |
| 525 CookieOptions options; | 525 CookieOptions options; |
| 526 options.set_include_httponly(); | 526 options.set_include_httponly(); |
| 527 return SetCanonicalCookie(&cc, creation_time, options); | 527 return SetCanonicalCookie(&cc, creation_time, options); |
| 528 } | 528 } |
| 529 | 529 |
| 530 | 530 |
| 531 CookieList CookieMonster::GetAllCookies() { | 531 CookieList CookieMonster::GetAllCookies() { |
| 532 AutoLock autolock(lock_); | 532 base::AutoLock autolock(lock_); |
| 533 InitIfNecessary(); | 533 InitIfNecessary(); |
| 534 | 534 |
| 535 // This function is being called to scrape the cookie list for management UI | 535 // This function is being called to scrape the cookie list for management UI |
| 536 // or similar. We shouldn't show expired cookies in this list since it will | 536 // or similar. We shouldn't show expired cookies in this list since it will |
| 537 // just be confusing to users, and this function is called rarely enough (and | 537 // just be confusing to users, and this function is called rarely enough (and |
| 538 // is already slow enough) that it's OK to take the time to garbage collect | 538 // is already slow enough) that it's OK to take the time to garbage collect |
| 539 // the expired cookies now. | 539 // the expired cookies now. |
| 540 // | 540 // |
| 541 // Note that this does not prune cookies to be below our limits (if we've | 541 // Note that this does not prune cookies to be below our limits (if we've |
| 542 // exceeded them) the way that calling GarbageCollect() would. | 542 // exceeded them) the way that calling GarbageCollect() would. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 557 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); | 557 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); |
| 558 it != cookie_ptrs.end(); ++it) | 558 it != cookie_ptrs.end(); ++it) |
| 559 cookie_list.push_back(**it); | 559 cookie_list.push_back(**it); |
| 560 | 560 |
| 561 return cookie_list; | 561 return cookie_list; |
| 562 } | 562 } |
| 563 | 563 |
| 564 CookieList CookieMonster::GetAllCookiesForURLWithOptions( | 564 CookieList CookieMonster::GetAllCookiesForURLWithOptions( |
| 565 const GURL& url, | 565 const GURL& url, |
| 566 const CookieOptions& options) { | 566 const CookieOptions& options) { |
| 567 AutoLock autolock(lock_); | 567 base::AutoLock autolock(lock_); |
| 568 InitIfNecessary(); | 568 InitIfNecessary(); |
| 569 | 569 |
| 570 std::vector<CanonicalCookie*> cookie_ptrs; | 570 std::vector<CanonicalCookie*> cookie_ptrs; |
| 571 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs); | 571 FindCookiesForHostAndDomain(url, options, false, &cookie_ptrs); |
| 572 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); | 572 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); |
| 573 | 573 |
| 574 CookieList cookies; | 574 CookieList cookies; |
| 575 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); | 575 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); |
| 576 it != cookie_ptrs.end(); it++) | 576 it != cookie_ptrs.end(); it++) |
| 577 cookies.push_back(**it); | 577 cookies.push_back(**it); |
| 578 | 578 |
| 579 return cookies; | 579 return cookies; |
| 580 } | 580 } |
| 581 | 581 |
| 582 CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) { | 582 CookieList CookieMonster::GetAllCookiesForURL(const GURL& url) { |
| 583 CookieOptions options; | 583 CookieOptions options; |
| 584 options.set_include_httponly(); | 584 options.set_include_httponly(); |
| 585 | 585 |
| 586 return GetAllCookiesForURLWithOptions(url, options); | 586 return GetAllCookiesForURLWithOptions(url, options); |
| 587 } | 587 } |
| 588 | 588 |
| 589 int CookieMonster::DeleteAll(bool sync_to_store) { | 589 int CookieMonster::DeleteAll(bool sync_to_store) { |
| 590 AutoLock autolock(lock_); | 590 base::AutoLock autolock(lock_); |
| 591 if (sync_to_store) | 591 if (sync_to_store) |
| 592 InitIfNecessary(); | 592 InitIfNecessary(); |
| 593 | 593 |
| 594 int num_deleted = 0; | 594 int num_deleted = 0; |
| 595 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { | 595 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 596 CookieMap::iterator curit = it; | 596 CookieMap::iterator curit = it; |
| 597 ++it; | 597 ++it; |
| 598 InternalDeleteCookie(curit, sync_to_store, | 598 InternalDeleteCookie(curit, sync_to_store, |
| 599 sync_to_store ? DELETE_COOKIE_EXPLICIT : | 599 sync_to_store ? DELETE_COOKIE_EXPLICIT : |
| 600 DELETE_COOKIE_DONT_RECORD /* Destruction. */); | 600 DELETE_COOKIE_DONT_RECORD /* Destruction. */); |
| 601 ++num_deleted; | 601 ++num_deleted; |
| 602 } | 602 } |
| 603 | 603 |
| 604 return num_deleted; | 604 return num_deleted; |
| 605 } | 605 } |
| 606 | 606 |
| 607 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin, | 607 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin, |
| 608 const Time& delete_end, | 608 const Time& delete_end, |
| 609 bool sync_to_store) { | 609 bool sync_to_store) { |
| 610 AutoLock autolock(lock_); | 610 base::AutoLock autolock(lock_); |
| 611 InitIfNecessary(); | 611 InitIfNecessary(); |
| 612 | 612 |
| 613 int num_deleted = 0; | 613 int num_deleted = 0; |
| 614 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { | 614 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 615 CookieMap::iterator curit = it; | 615 CookieMap::iterator curit = it; |
| 616 CanonicalCookie* cc = curit->second; | 616 CanonicalCookie* cc = curit->second; |
| 617 ++it; | 617 ++it; |
| 618 | 618 |
| 619 if (cc->CreationDate() >= delete_begin && | 619 if (cc->CreationDate() >= delete_begin && |
| 620 (delete_end.is_null() || cc->CreationDate() < delete_end)) { | 620 (delete_end.is_null() || cc->CreationDate() < delete_end)) { |
| 621 InternalDeleteCookie(curit, sync_to_store, DELETE_COOKIE_EXPLICIT); | 621 InternalDeleteCookie(curit, sync_to_store, DELETE_COOKIE_EXPLICIT); |
| 622 ++num_deleted; | 622 ++num_deleted; |
| 623 } | 623 } |
| 624 } | 624 } |
| 625 | 625 |
| 626 return num_deleted; | 626 return num_deleted; |
| 627 } | 627 } |
| 628 | 628 |
| 629 int CookieMonster::DeleteAllCreatedAfter(const Time& delete_begin, | 629 int CookieMonster::DeleteAllCreatedAfter(const Time& delete_begin, |
| 630 bool sync_to_store) { | 630 bool sync_to_store) { |
| 631 return DeleteAllCreatedBetween(delete_begin, Time(), sync_to_store); | 631 return DeleteAllCreatedBetween(delete_begin, Time(), sync_to_store); |
| 632 } | 632 } |
| 633 | 633 |
| 634 int CookieMonster::DeleteAllForHost(const GURL& url) { | 634 int CookieMonster::DeleteAllForHost(const GURL& url) { |
| 635 AutoLock autolock(lock_); | 635 base::AutoLock autolock(lock_); |
| 636 InitIfNecessary(); | 636 InitIfNecessary(); |
| 637 | 637 |
| 638 if (!HasCookieableScheme(url)) | 638 if (!HasCookieableScheme(url)) |
| 639 return 0; | 639 return 0; |
| 640 | 640 |
| 641 const std::string scheme(url.scheme()); | 641 const std::string scheme(url.scheme()); |
| 642 const std::string host(url.host()); | 642 const std::string host(url.host()); |
| 643 | 643 |
| 644 // We store host cookies in the store by their canonical host name; | 644 // We store host cookies in the store by their canonical host name; |
| 645 // domain cookies are stored with a leading ".". So this is a pretty | 645 // domain cookies are stored with a leading ".". So this is a pretty |
| (...skipping 10 matching lines...) Expand all Loading... |
| 656 if (cc->IsHostCookie() && cc->IsDomainMatch(scheme, host)) { | 656 if (cc->IsHostCookie() && cc->IsDomainMatch(scheme, host)) { |
| 657 num_deleted++; | 657 num_deleted++; |
| 658 | 658 |
| 659 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); | 659 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); |
| 660 } | 660 } |
| 661 } | 661 } |
| 662 return num_deleted; | 662 return num_deleted; |
| 663 } | 663 } |
| 664 | 664 |
| 665 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) { | 665 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) { |
| 666 AutoLock autolock(lock_); | 666 base::AutoLock autolock(lock_); |
| 667 InitIfNecessary(); | 667 InitIfNecessary(); |
| 668 | 668 |
| 669 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); | 669 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); |
| 670 its.first != its.second; ++its.first) { | 670 its.first != its.second; ++its.first) { |
| 671 // The creation date acts as our unique index... | 671 // The creation date acts as our unique index... |
| 672 if (its.first->second->CreationDate() == cookie.CreationDate()) { | 672 if (its.first->second->CreationDate() == cookie.CreationDate()) { |
| 673 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT); | 673 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT); |
| 674 return true; | 674 return true; |
| 675 } | 675 } |
| 676 } | 676 } |
| 677 return false; | 677 return false; |
| 678 } | 678 } |
| 679 | 679 |
| 680 void CookieMonster::SetCookieableSchemes( | 680 void CookieMonster::SetCookieableSchemes( |
| 681 const char* schemes[], size_t num_schemes) { | 681 const char* schemes[], size_t num_schemes) { |
| 682 AutoLock autolock(lock_); | 682 base::AutoLock autolock(lock_); |
| 683 | 683 |
| 684 // Cookieable Schemes must be set before first use of function. | 684 // Cookieable Schemes must be set before first use of function. |
| 685 DCHECK(!initialized_); | 685 DCHECK(!initialized_); |
| 686 | 686 |
| 687 cookieable_schemes_.clear(); | 687 cookieable_schemes_.clear(); |
| 688 cookieable_schemes_.insert(cookieable_schemes_.end(), | 688 cookieable_schemes_.insert(cookieable_schemes_.end(), |
| 689 schemes, schemes + num_schemes); | 689 schemes, schemes + num_schemes); |
| 690 } | 690 } |
| 691 | 691 |
| 692 void CookieMonster::SetExpiryAndKeyScheme(ExpiryAndKeyScheme key_scheme) { | 692 void CookieMonster::SetExpiryAndKeyScheme(ExpiryAndKeyScheme key_scheme) { |
| 693 DCHECK(!initialized_); | 693 DCHECK(!initialized_); |
| 694 expiry_and_key_scheme_ = key_scheme; | 694 expiry_and_key_scheme_ = key_scheme; |
| 695 } | 695 } |
| 696 | 696 |
| 697 void CookieMonster::SetClearPersistentStoreOnExit(bool clear_local_store) { | 697 void CookieMonster::SetClearPersistentStoreOnExit(bool clear_local_store) { |
| 698 if(store_) | 698 if(store_) |
| 699 store_->SetClearLocalStateOnExit(clear_local_store); | 699 store_->SetClearLocalStateOnExit(clear_local_store); |
| 700 } | 700 } |
| 701 | 701 |
| 702 // static | 702 // static |
| 703 void CookieMonster::EnableFileScheme() { | 703 void CookieMonster::EnableFileScheme() { |
| 704 enable_file_scheme_ = true; | 704 enable_file_scheme_ = true; |
| 705 } | 705 } |
| 706 | 706 |
| 707 void CookieMonster::FlushStore(Task* completion_task) { | 707 void CookieMonster::FlushStore(Task* completion_task) { |
| 708 AutoLock autolock(lock_); | 708 base::AutoLock autolock(lock_); |
| 709 if (initialized_ && store_) | 709 if (initialized_ && store_) |
| 710 store_->Flush(completion_task); | 710 store_->Flush(completion_task); |
| 711 else if (completion_task) | 711 else if (completion_task) |
| 712 MessageLoop::current()->PostTask(FROM_HERE, completion_task); | 712 MessageLoop::current()->PostTask(FROM_HERE, completion_task); |
| 713 } | 713 } |
| 714 | 714 |
| 715 bool CookieMonster::SetCookieWithOptions(const GURL& url, | 715 bool CookieMonster::SetCookieWithOptions(const GURL& url, |
| 716 const std::string& cookie_line, | 716 const std::string& cookie_line, |
| 717 const CookieOptions& options) { | 717 const CookieOptions& options) { |
| 718 AutoLock autolock(lock_); | 718 base::AutoLock autolock(lock_); |
| 719 | 719 |
| 720 if (!HasCookieableScheme(url)) { | 720 if (!HasCookieableScheme(url)) { |
| 721 return false; | 721 return false; |
| 722 } | 722 } |
| 723 | 723 |
| 724 InitIfNecessary(); | 724 InitIfNecessary(); |
| 725 | 725 |
| 726 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options); | 726 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options); |
| 727 } | 727 } |
| 728 | 728 |
| 729 std::string CookieMonster::GetCookiesWithOptions(const GURL& url, | 729 std::string CookieMonster::GetCookiesWithOptions(const GURL& url, |
| 730 const CookieOptions& options) { | 730 const CookieOptions& options) { |
| 731 AutoLock autolock(lock_); | 731 base::AutoLock autolock(lock_); |
| 732 InitIfNecessary(); | 732 InitIfNecessary(); |
| 733 | 733 |
| 734 if (!HasCookieableScheme(url)) { | 734 if (!HasCookieableScheme(url)) { |
| 735 return std::string(); | 735 return std::string(); |
| 736 } | 736 } |
| 737 | 737 |
| 738 TimeTicks start_time(TimeTicks::Now()); | 738 TimeTicks start_time(TimeTicks::Now()); |
| 739 | 739 |
| 740 // Get the cookies for this host and its domain(s). | 740 // Get the cookies for this host and its domain(s). |
| 741 std::vector<CanonicalCookie*> cookies; | 741 std::vector<CanonicalCookie*> cookies; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 757 | 757 |
| 758 histogram_time_get_->AddTime(TimeTicks::Now() - start_time); | 758 histogram_time_get_->AddTime(TimeTicks::Now() - start_time); |
| 759 | 759 |
| 760 VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line; | 760 VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line; |
| 761 | 761 |
| 762 return cookie_line; | 762 return cookie_line; |
| 763 } | 763 } |
| 764 | 764 |
| 765 void CookieMonster::DeleteCookie(const GURL& url, | 765 void CookieMonster::DeleteCookie(const GURL& url, |
| 766 const std::string& cookie_name) { | 766 const std::string& cookie_name) { |
| 767 AutoLock autolock(lock_); | 767 base::AutoLock autolock(lock_); |
| 768 InitIfNecessary(); | 768 InitIfNecessary(); |
| 769 | 769 |
| 770 if (!HasCookieableScheme(url)) | 770 if (!HasCookieableScheme(url)) |
| 771 return; | 771 return; |
| 772 | 772 |
| 773 CookieOptions options; | 773 CookieOptions options; |
| 774 options.set_include_httponly(); | 774 options.set_include_httponly(); |
| 775 // Get the cookies for this host and its domain(s). | 775 // Get the cookies for this host and its domain(s). |
| 776 std::vector<CanonicalCookie*> cookies; | 776 std::vector<CanonicalCookie*> cookies; |
| 777 FindCookiesForHostAndDomain(url, options, true, &cookies); | 777 FindCookiesForHostAndDomain(url, options, true, &cookies); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 799 return this; | 799 return this; |
| 800 } | 800 } |
| 801 | 801 |
| 802 CookieMonster::~CookieMonster() { | 802 CookieMonster::~CookieMonster() { |
| 803 DeleteAll(false); | 803 DeleteAll(false); |
| 804 } | 804 } |
| 805 | 805 |
| 806 bool CookieMonster::SetCookieWithCreationTime(const GURL& url, | 806 bool CookieMonster::SetCookieWithCreationTime(const GURL& url, |
| 807 const std::string& cookie_line, | 807 const std::string& cookie_line, |
| 808 const base::Time& creation_time) { | 808 const base::Time& creation_time) { |
| 809 AutoLock autolock(lock_); | 809 base::AutoLock autolock(lock_); |
| 810 | 810 |
| 811 if (!HasCookieableScheme(url)) { | 811 if (!HasCookieableScheme(url)) { |
| 812 return false; | 812 return false; |
| 813 } | 813 } |
| 814 | 814 |
| 815 InitIfNecessary(); | 815 InitIfNecessary(); |
| 816 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time, | 816 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time, |
| 817 CookieOptions()); | 817 CookieOptions()); |
| 818 } | 818 } |
| 819 | 819 |
| (...skipping 1195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2015 std::string CookieMonster::CanonicalCookie::DebugString() const { | 2015 std::string CookieMonster::CanonicalCookie::DebugString() const { |
| 2016 return base::StringPrintf( | 2016 return base::StringPrintf( |
| 2017 "name: %s value: %s domain: %s path: %s creation: %" | 2017 "name: %s value: %s domain: %s path: %s creation: %" |
| 2018 PRId64, | 2018 PRId64, |
| 2019 name_.c_str(), value_.c_str(), | 2019 name_.c_str(), value_.c_str(), |
| 2020 domain_.c_str(), path_.c_str(), | 2020 domain_.c_str(), path_.c_str(), |
| 2021 static_cast<int64>(creation_date_.ToTimeT())); | 2021 static_cast<int64>(creation_date_.ToTimeT())); |
| 2022 } | 2022 } |
| 2023 | 2023 |
| 2024 } // namespace | 2024 } // namespace |
| OLD | NEW |