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

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

Issue 2971323002: Switch cookie async mechanism over to using callbacks. (Closed)
Patch Set: Sync'd to p485987 Created 3 years, 5 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 75
76 // In steady state, most cookie requests can be satisfied by the in memory 76 // In steady state, most cookie requests can be satisfied by the in memory
77 // cookie monster store. If the cookie request cannot be satisfied by the in 77 // cookie monster store. If the cookie request cannot be satisfied by the in
78 // memory store, the relevant cookies must be fetched from the persistent 78 // memory store, the relevant cookies must be fetched from the persistent
79 // store. The task is queued in CookieMonster::tasks_pending_ if it requires 79 // store. The task is queued in CookieMonster::tasks_pending_ if it requires
80 // all cookies to be loaded from the backend, or tasks_pending_for_key_ if it 80 // all cookies to be loaded from the backend, or tasks_pending_for_key_ if it
81 // only requires all cookies associated with an eTLD+1. 81 // only requires all cookies associated with an eTLD+1.
82 // 82 //
83 // On the browser critical paths (e.g. for loading initial web pages in a 83 // On the browser critical paths (e.g. for loading initial web pages in a
84 // session restore) it may take too long to wait for the full load. If a cookie 84 // session restore) it may take too long to wait for the full load. If a cookie
85 // request is for a specific URL, DoCookieTaskForURL is called, which triggers a 85 // request is for a specific URL, DoCookieCallbackForURL is called, which
86 // priority load if the key is not loaded yet by calling PersistentCookieStore 86 // triggers a priority load if the key is not loaded yet by calling
87 // :: LoadCookiesForKey. The request is queued in 87 // PersistentCookieStore::LoadCookiesForKey. The request is queued in
88 // CookieMonster::tasks_pending_for_key_ and executed upon receiving 88 // CookieMonster::tasks_pending_for_key_ and executed upon receiving
89 // notification of key load completion via CookieMonster::OnKeyLoaded(). If 89 // notification of key load completion via CookieMonster::OnKeyLoaded(). If
90 // multiple requests for the same eTLD+1 are received before key load 90 // multiple requests for the same eTLD+1 are received before key load
91 // completion, only the first request calls 91 // completion, only the first request calls
92 // PersistentCookieStore::LoadCookiesForKey, all subsequent requests are queued 92 // PersistentCookieStore::LoadCookiesForKey, all subsequent requests are queued
93 // in CookieMonster::tasks_pending_for_key_ and executed upon receiving 93 // in CookieMonster::tasks_pending_for_key_ and executed upon receiving
94 // notification of key load completion triggered by the first request for the 94 // notification of key load completion triggered by the first request for the
95 // same eTLD+1. 95 // same eTLD+1.
96 96
97 static const int kMinutesInTenYears = 10 * 365 * 24 * 60; 97 static const int kMinutesInTenYears = 10 * 365 * 24 * 60;
98 98
99 namespace { 99 namespace {
100 100
101 const char kFetchWhenNecessaryName[] = "FetchWhenNecessary"; 101 const char kFetchWhenNecessaryName[] = "FetchWhenNecessary";
102 const char kAlwaysFetchName[] = "AlwaysFetch"; 102 const char kAlwaysFetchName[] = "AlwaysFetch";
103 const char kCookieMonsterFetchStrategyName[] = "CookieMonsterFetchStrategy"; 103 const char kCookieMonsterFetchStrategyName[] = "CookieMonsterFetchStrategy";
104 104
105 void ConditionalDeleteCallback(base::WeakPtr<net::CookieMonster> cookie_monster,
mmenke 2017/07/12 18:29:04 Suggest renaming these so it's clearer they call c
Randy Smith (Not in Mondays) 2017/07/12 19:47:35 Done.
106 base::OnceClosure callback) {
107 if (cookie_monster && !callback.is_null())
mmenke 2017/07/12 18:29:04 nit: For all of these, can just use callback inst
Randy Smith (Not in Mondays) 2017/07/12 19:47:34 Done.
108 std::move(callback).Run();
109 }
110
111 void ConditionalCookieCallback(base::OnceClosure callback) {
112 if (!callback.is_null())
113 std::move(callback).Run();
114 }
115
116 template <typename T>
117 void ConditionalCookieCallback(base::OnceCallback<void(const T&)> callback,
118 T result) {
mmenke 2017/07/12 18:29:04 If these two are called with strings, or cookies,
Randy Smith (Not in Mondays) 2017/07/12 19:47:35 Done.
119 if (!callback.is_null())
120 std::move(callback).Run(result);
121 }
122
123 template <typename T>
124 void ConditionalCookieCallback(base::OnceCallback<void(T)> callback, T result) {
125 if (!callback.is_null())
126 std::move(callback).Run(result);
127 }
128
105 } // namespace 129 } // namespace
106 130
107 namespace net { 131 namespace net {
108 132
109 // See comments at declaration of these variables in cookie_monster.h 133 // See comments at declaration of these variables in cookie_monster.h
110 // for details. 134 // for details.
111 const size_t CookieMonster::kDomainMaxCookies = 180; 135 const size_t CookieMonster::kDomainMaxCookies = 180;
112 const size_t CookieMonster::kDomainPurgeCookies = 30; 136 const size_t CookieMonster::kDomainPurgeCookies = 30;
113 const size_t CookieMonster::kMaxCookies = 3300; 137 const size_t CookieMonster::kMaxCookies = 3300;
114 const size_t CookieMonster::kPurgeCookies = 300; 138 const size_t CookieMonster::kPurgeCookies = 300;
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 channel_id_service_(channel_id_service), 402 channel_id_service_(channel_id_service),
379 last_statistic_record_time_(base::Time::Now()), 403 last_statistic_record_time_(base::Time::Now()),
380 persist_session_cookies_(false), 404 persist_session_cookies_(false),
381 weak_ptr_factory_(this) { 405 weak_ptr_factory_(this) {
382 InitializeHistograms(); 406 InitializeHistograms();
383 cookieable_schemes_.insert( 407 cookieable_schemes_.insert(
384 cookieable_schemes_.begin(), kDefaultCookieableSchemes, 408 cookieable_schemes_.begin(), kDefaultCookieableSchemes,
385 kDefaultCookieableSchemes + kDefaultCookieableSchemesCount); 409 kDefaultCookieableSchemes + kDefaultCookieableSchemesCount);
386 } 410 }
387 411
388 // Task classes for queueing the coming request.
389
390 class CookieMonster::CookieMonsterTask
391 : public base::RefCountedThreadSafe<CookieMonsterTask> {
392 public:
393 // Runs the task and invokes the client callback on the thread that
394 // originally constructed the task.
395 virtual void Run() = 0;
396
397 protected:
398 explicit CookieMonsterTask(CookieMonster* cookie_monster);
399 virtual ~CookieMonsterTask();
400
401 CookieMonster* cookie_monster() { return cookie_monster_; }
402
403 private:
404 friend class base::RefCountedThreadSafe<CookieMonsterTask>;
405
406 CookieMonster* cookie_monster_;
407
408 DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask);
409 };
410
411 CookieMonster::CookieMonsterTask::CookieMonsterTask(
412 CookieMonster* cookie_monster)
413 : cookie_monster_(cookie_monster) {}
414
415 CookieMonster::CookieMonsterTask::~CookieMonsterTask() {
416 }
417
418 // Task class for SetCookieWithDetails call.
419 class CookieMonster::SetCookieWithDetailsTask : public CookieMonsterTask {
420 public:
421 SetCookieWithDetailsTask(CookieMonster* cookie_monster,
422 const GURL& url,
423 const std::string& name,
424 const std::string& value,
425 const std::string& domain,
426 const std::string& path,
427 base::Time creation_time,
428 base::Time expiration_time,
429 base::Time last_access_time,
430 bool secure,
431 bool http_only,
432 CookieSameSite same_site,
433 CookiePriority priority,
434 SetCookiesCallback callback)
435 : CookieMonsterTask(cookie_monster),
436 url_(url),
437 name_(name),
438 value_(value),
439 domain_(domain),
440 path_(path),
441 creation_time_(creation_time),
442 expiration_time_(expiration_time),
443 last_access_time_(last_access_time),
444 secure_(secure),
445 http_only_(http_only),
446 same_site_(same_site),
447 priority_(priority),
448 callback_(std::move(callback)) {}
449
450 // CookieMonsterTask:
451 void Run() override;
452
453 protected:
454 ~SetCookieWithDetailsTask() override {}
455
456 private:
457 GURL url_;
458 std::string name_;
459 std::string value_;
460 std::string domain_;
461 std::string path_;
462 base::Time creation_time_;
463 base::Time expiration_time_;
464 base::Time last_access_time_;
465 bool secure_;
466 bool http_only_;
467 CookieSameSite same_site_;
468 CookiePriority priority_;
469 SetCookiesCallback callback_;
470
471 DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask);
472 };
473
474 void CookieMonster::SetCookieWithDetailsTask::Run() {
475 bool success = this->cookie_monster()->SetCookieWithDetails(
476 url_, name_, value_, domain_, path_, creation_time_, expiration_time_,
477 last_access_time_, secure_, http_only_, same_site_, priority_);
478 if (!callback_.is_null())
479 std::move(callback_).Run(success);
480 }
481
482 // Task class for GetAllCookies call.
483 class CookieMonster::GetAllCookiesTask : public CookieMonsterTask {
484 public:
485 GetAllCookiesTask(CookieMonster* cookie_monster,
486 GetCookieListCallback callback)
487 : CookieMonsterTask(cookie_monster), callback_(std::move(callback)) {}
488
489 // CookieMonsterTask
490 void Run() override;
491
492 protected:
493 ~GetAllCookiesTask() override {}
494
495 private:
496 GetCookieListCallback callback_;
497
498 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask);
499 };
500
501 void CookieMonster::GetAllCookiesTask::Run() {
502 if (!callback_.is_null()) {
503 CookieList cookies = this->cookie_monster()->GetAllCookies();
504 std::move(callback_).Run(cookies);
505 }
506 }
507
508 // Task class for GetCookieListWithOptionsAsync call.
509 class CookieMonster::GetCookieListWithOptionsTask : public CookieMonsterTask {
510 public:
511 GetCookieListWithOptionsTask(CookieMonster* cookie_monster,
512 const GURL& url,
513 const CookieOptions& options,
514 GetCookieListCallback callback)
515 : CookieMonsterTask(cookie_monster),
516 url_(url),
517 options_(options),
518 callback_(std::move(callback)) {}
519
520 // CookieMonsterTask:
521 void Run() override;
522
523 protected:
524 ~GetCookieListWithOptionsTask() override {}
525
526 private:
527 GURL url_;
528 CookieOptions options_;
529 GetCookieListCallback callback_;
530
531 DISALLOW_COPY_AND_ASSIGN(GetCookieListWithOptionsTask);
532 };
533
534 void CookieMonster::GetCookieListWithOptionsTask::Run() {
535 if (!callback_.is_null()) {
536 CookieList cookies =
537 this->cookie_monster()->GetCookieListWithOptions(url_, options_);
538 std::move(callback_).Run(cookies);
539 }
540 }
541
542 template <typename Result>
543 struct CallbackType {
544 typedef base::OnceCallback<void(Result)> Type;
545 };
546
547 template <>
548 struct CallbackType<void> {
549 typedef base::OnceClosure Type;
550 };
551
552 // Base task class for Delete*Task.
553 template <typename Result>
554 class CookieMonster::DeleteTask : public CookieMonsterTask {
555 public:
556 DeleteTask(CookieMonster* cookie_monster,
557 typename CallbackType<Result>::Type callback)
558 : CookieMonsterTask(cookie_monster), callback_(std::move(callback)) {}
559
560 // CookieMonsterTask:
561 void Run() override;
562
563 protected:
564 ~DeleteTask() override;
565
566 private:
567 // Runs the delete task and returns a result.
568 virtual Result RunDeleteTask() = 0;
569 // Runs the delete task and then returns a callback to be called after
570 // flushing the persistent store.
571 // TODO(mmenke): This seems like a pretty ugly and needlessly confusing API.
572 // Simplify it?
573 base::OnceClosure RunDeleteTaskAndBindCallback();
574
575 typename CallbackType<Result>::Type callback_;
576
577 DISALLOW_COPY_AND_ASSIGN(DeleteTask);
578 };
579
580 template <typename Result>
581 CookieMonster::DeleteTask<Result>::~DeleteTask() {
582 }
583
584 template <typename Result>
585 base::OnceClosure
586 CookieMonster::DeleteTask<Result>::RunDeleteTaskAndBindCallback() {
587 Result result = RunDeleteTask();
588 if (callback_.is_null())
589 return base::OnceClosure();
590 return base::BindOnce(std::move(callback_), result);
591 }
592
593 template <>
594 base::OnceClosure
595 CookieMonster::DeleteTask<void>::RunDeleteTaskAndBindCallback() {
596 RunDeleteTask();
597 return std::move(callback_);
598 }
599
600 template <typename Result>
601 void CookieMonster::DeleteTask<Result>::Run() {
602 base::OnceClosure callback = RunDeleteTaskAndBindCallback();
603 if (!callback.is_null()) {
604 callback =
605 base::BindOnce(&CookieMonster::RunCallback,
606 this->cookie_monster()->weak_ptr_factory_.GetWeakPtr(),
607 std::move(callback));
608 }
609 this->cookie_monster()->FlushStore(std::move(callback));
610 }
611
612 // Task class for DeleteAllCreatedBetween call.
613 class CookieMonster::DeleteAllCreatedBetweenTask : public DeleteTask<uint32_t> {
614 public:
615 DeleteAllCreatedBetweenTask(CookieMonster* cookie_monster,
616 const Time& delete_begin,
617 const Time& delete_end,
618 DeleteCallback callback)
619 : DeleteTask<uint32_t>(cookie_monster, std::move(callback)),
620 delete_begin_(delete_begin),
621 delete_end_(delete_end) {}
622
623 // DeleteTask:
624 uint32_t RunDeleteTask() override;
625
626 protected:
627 ~DeleteAllCreatedBetweenTask() override {}
628
629 private:
630 Time delete_begin_;
631 Time delete_end_;
632
633 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask);
634 };
635
636 uint32_t CookieMonster::DeleteAllCreatedBetweenTask::RunDeleteTask() {
637 return this->cookie_monster()->DeleteAllCreatedBetween(delete_begin_,
638 delete_end_);
639 }
640
641 // Task class for DeleteAllCreatedBetweenWithPredicate call.
642 class CookieMonster::DeleteAllCreatedBetweenWithPredicateTask
643 : public DeleteTask<uint32_t> {
644 public:
645 DeleteAllCreatedBetweenWithPredicateTask(
646 CookieMonster* cookie_monster,
647 Time delete_begin,
648 Time delete_end,
649 base::Callback<bool(const CanonicalCookie&)> predicate,
650 DeleteCallback callback)
651 : DeleteTask<uint32_t>(cookie_monster, std::move(callback)),
652 delete_begin_(delete_begin),
653 delete_end_(delete_end),
654 predicate_(predicate) {}
655
656 // DeleteTask:
657 uint32_t RunDeleteTask() override;
658
659 protected:
660 ~DeleteAllCreatedBetweenWithPredicateTask() override {}
661
662 private:
663 Time delete_begin_;
664 Time delete_end_;
665 base::Callback<bool(const CanonicalCookie&)> predicate_;
666
667 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenWithPredicateTask);
668 };
669
670 uint32_t
671 CookieMonster::DeleteAllCreatedBetweenWithPredicateTask::RunDeleteTask() {
672 return this->cookie_monster()->DeleteAllCreatedBetweenWithPredicate(
673 delete_begin_, delete_end_, predicate_);
674 }
675
676 // Task class for DeleteCanonicalCookie call.
677 class CookieMonster::DeleteCanonicalCookieTask : public DeleteTask<uint32_t> {
678 public:
679 DeleteCanonicalCookieTask(CookieMonster* cookie_monster,
680 const CanonicalCookie& cookie,
681 DeleteCallback callback)
682 : DeleteTask<uint32_t>(cookie_monster, std::move(callback)),
683 cookie_(cookie) {}
684
685 // DeleteTask:
686 uint32_t RunDeleteTask() override;
687
688 protected:
689 ~DeleteCanonicalCookieTask() override {}
690
691 private:
692 CanonicalCookie cookie_;
693
694 DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask);
695 };
696
697 uint32_t CookieMonster::DeleteCanonicalCookieTask::RunDeleteTask() {
698 return this->cookie_monster()->DeleteCanonicalCookie(cookie_);
699 }
700
701 // Task class for SetCanonicalCookie call.
702 class CookieMonster::SetCanonicalCookieTask : public CookieMonsterTask {
703 public:
704 SetCanonicalCookieTask(CookieMonster* cookie_monster,
705 std::unique_ptr<CanonicalCookie> cookie,
706 bool secure_source,
707 bool modify_http_only,
708 SetCookiesCallback callback)
709 : CookieMonsterTask(cookie_monster),
710 cookie_(std::move(cookie)),
711 secure_source_(secure_source),
712 modify_http_only_(modify_http_only),
713 callback_(std::move(callback)) {}
714
715 // CookieMonsterTask:
716 void Run() override;
717
718 protected:
719 ~SetCanonicalCookieTask() override {}
720
721 private:
722 std::unique_ptr<CanonicalCookie> cookie_;
723 bool secure_source_;
724 bool modify_http_only_;
725 SetCookiesCallback callback_;
726
727 DISALLOW_COPY_AND_ASSIGN(SetCanonicalCookieTask);
728 };
729
730 void CookieMonster::SetCanonicalCookieTask::Run() {
731 bool result = this->cookie_monster()->SetCanonicalCookie(
732 std::move(cookie_), secure_source_, modify_http_only_);
733 if (!callback_.is_null())
734 std::move(callback_).Run(result);
735 }
736
737 // Task class for SetCookieWithOptions call.
738 class CookieMonster::SetCookieWithOptionsTask : public CookieMonsterTask {
739 public:
740 SetCookieWithOptionsTask(CookieMonster* cookie_monster,
741 const GURL& url,
742 const std::string& cookie_line,
743 const CookieOptions& options,
744 SetCookiesCallback callback)
745 : CookieMonsterTask(cookie_monster),
746 url_(url),
747 cookie_line_(cookie_line),
748 options_(options),
749 callback_(std::move(callback)) {}
750
751 // CookieMonsterTask:
752 void Run() override;
753
754 protected:
755 ~SetCookieWithOptionsTask() override {}
756
757 private:
758 GURL url_;
759 std::string cookie_line_;
760 CookieOptions options_;
761 SetCookiesCallback callback_;
762
763 DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask);
764 };
765
766 void CookieMonster::SetCookieWithOptionsTask::Run() {
767 bool result = this->cookie_monster()->SetCookieWithOptions(url_, cookie_line_,
768 options_);
769 if (!callback_.is_null())
770 std::move(callback_).Run(result);
771 }
772
773 // Task class for SetAllCookies call.
774 class CookieMonster::SetAllCookiesTask : public CookieMonsterTask {
775 public:
776 SetAllCookiesTask(CookieMonster* cookie_monster,
777 const CookieList& list,
778 SetCookiesCallback callback)
779 : CookieMonsterTask(cookie_monster),
780 list_(list),
781 callback_(std::move(callback)) {}
782
783 // CookieMonsterTask:
784 void Run() override;
785
786 protected:
787 ~SetAllCookiesTask() override {}
788
789 private:
790 CookieList list_;
791 SetCookiesCallback callback_;
792
793 DISALLOW_COPY_AND_ASSIGN(SetAllCookiesTask);
794 };
795
796 void CookieMonster::SetAllCookiesTask::Run() {
797 CookieList positive_diff;
798 CookieList negative_diff;
799 CookieList old_cookies = this->cookie_monster()->GetAllCookies();
800 this->cookie_monster()->ComputeCookieDiff(&old_cookies, &list_,
801 &positive_diff, &negative_diff);
802
803 for (CookieList::const_iterator it = negative_diff.begin();
804 it != negative_diff.end(); ++it) {
805 this->cookie_monster()->DeleteCanonicalCookie(*it);
806 }
807
808 bool result = true;
809 if (positive_diff.size() > 0)
810 result = this->cookie_monster()->SetAllCookies(list_);
811
812 if (!callback_.is_null())
813 std::move(callback_).Run(result);
814 }
815
816 // Task class for GetCookiesWithOptions call.
817 class CookieMonster::GetCookiesWithOptionsTask : public CookieMonsterTask {
818 public:
819 GetCookiesWithOptionsTask(CookieMonster* cookie_monster,
820 const GURL& url,
821 const CookieOptions& options,
822 GetCookiesCallback callback)
823 : CookieMonsterTask(cookie_monster),
824 url_(url),
825 options_(options),
826 callback_(std::move(callback)) {}
827
828 // CookieMonsterTask:
829 void Run() override;
830
831 protected:
832 ~GetCookiesWithOptionsTask() override {}
833
834 private:
835 GURL url_;
836 CookieOptions options_;
837 GetCookiesCallback callback_;
838
839 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask);
840 };
841
842 void CookieMonster::GetCookiesWithOptionsTask::Run() {
843 std::string cookie =
844 this->cookie_monster()->GetCookiesWithOptions(url_, options_);
845 if (!callback_.is_null())
846 std::move(callback_).Run(cookie);
847 }
848
849 // Task class for DeleteCookie call.
850 class CookieMonster::DeleteCookieTask : public DeleteTask<void> {
851 public:
852 DeleteCookieTask(CookieMonster* cookie_monster,
853 const GURL& url,
854 const std::string& cookie_name,
855 base::OnceClosure callback)
856 : DeleteTask<void>(cookie_monster, std::move(callback)),
857 url_(url),
858 cookie_name_(cookie_name) {}
859
860 // DeleteTask:
861 void RunDeleteTask() override;
862
863 protected:
864 ~DeleteCookieTask() override {}
865
866 private:
867 GURL url_;
868 std::string cookie_name_;
869
870 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask);
871 };
872
873 void CookieMonster::DeleteCookieTask::RunDeleteTask() {
874 this->cookie_monster()->DeleteCookie(url_, cookie_name_);
875 }
876
877 // Task class for DeleteSessionCookies call.
878 class CookieMonster::DeleteSessionCookiesTask : public DeleteTask<uint32_t> {
879 public:
880 DeleteSessionCookiesTask(CookieMonster* cookie_monster,
881 DeleteCallback callback)
882 : DeleteTask<uint32_t>(cookie_monster, std::move(callback)) {}
883
884 // DeleteTask:
885 uint32_t RunDeleteTask() override;
886
887 protected:
888 ~DeleteSessionCookiesTask() override {}
889
890 private:
891 DISALLOW_COPY_AND_ASSIGN(DeleteSessionCookiesTask);
892 };
893
894 uint32_t CookieMonster::DeleteSessionCookiesTask::RunDeleteTask() {
895 return this->cookie_monster()->DeleteSessionCookies();
896 }
897
898 // Asynchronous CookieMonster API 412 // Asynchronous CookieMonster API
899 413
900 void CookieMonster::SetCookieWithDetailsAsync(const GURL& url, 414 void CookieMonster::SetCookieWithDetailsAsync(const GURL& url,
901 const std::string& name, 415 const std::string& name,
902 const std::string& value, 416 const std::string& value,
903 const std::string& domain, 417 const std::string& domain,
904 const std::string& path, 418 const std::string& path,
905 Time creation_time, 419 Time creation_time,
906 Time expiration_time, 420 Time expiration_time,
907 Time last_access_time, 421 Time last_access_time,
908 bool secure, 422 bool secure,
909 bool http_only, 423 bool http_only,
910 CookieSameSite same_site, 424 CookieSameSite same_site,
911 CookiePriority priority, 425 CookiePriority priority,
912 SetCookiesCallback callback) { 426 SetCookiesCallback callback) {
913 scoped_refptr<SetCookieWithDetailsTask> task = new SetCookieWithDetailsTask( 427 DoCookieCallbackForURL(
914 this, url, name, value, domain, path, creation_time, expiration_time, 428 base::BindOnce(
915 last_access_time, secure, http_only, same_site, priority, 429 // base::Unretained is safe as DoCookieCallbackForURL stores
916 std::move(callback)); 430 // the callback on |*this|, so the callback will not outlive
917 DoCookieTaskForURL(task, url); 431 // the object.
432 &CookieMonster::SetCookieWithDetails, base::Unretained(this), url,
433 name, value, domain, path, creation_time, expiration_time,
434 last_access_time, secure, http_only, same_site, priority,
435 std::move(callback)),
436 url);
918 } 437 }
919 438
920 void CookieMonster::FlushStore(base::OnceClosure callback) { 439 void CookieMonster::FlushStore(base::OnceClosure callback) {
921 DCHECK(thread_checker_.CalledOnValidThread()); 440 DCHECK(thread_checker_.CalledOnValidThread());
922 441
923 if (initialized_ && store_.get()) { 442 if (initialized_ && store_.get()) {
924 if (channel_id_service_) { 443 if (channel_id_service_) {
925 channel_id_service_->GetChannelIDStore()->Flush(); 444 channel_id_service_->GetChannelIDStore()->Flush();
926 } 445 }
927 store_->Flush(std::move(callback)); 446 store_->Flush(std::move(callback));
928 } else if (!callback.is_null()) { 447 } else if (!callback.is_null()) {
929 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 448 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
930 std::move(callback)); 449 std::move(callback));
931 } 450 }
932 } 451 }
933 452
934 void CookieMonster::SetForceKeepSessionState() { 453 void CookieMonster::SetForceKeepSessionState() {
935 DCHECK(thread_checker_.CalledOnValidThread()); 454 DCHECK(thread_checker_.CalledOnValidThread());
936 455
937 if (store_) 456 if (store_)
938 store_->SetForceKeepSessionState(); 457 store_->SetForceKeepSessionState();
939 } 458 }
940 459
941 void CookieMonster::SetAllCookiesAsync(const CookieList& list, 460 void CookieMonster::SetAllCookiesAsync(const CookieList& list,
942 SetCookiesCallback callback) { 461 SetCookiesCallback callback) {
943 scoped_refptr<SetAllCookiesTask> task = 462 DoCookieCallback(base::BindOnce(
944 new SetAllCookiesTask(this, list, std::move(callback)); 463 // base::Unretained is safe as DoCookieCallbackForURL stores
945 DoCookieTask(task); 464 // the callback on |*this|, so the callback will not outlive
465 // the object.
466 &CookieMonster::SetAllCookies, base::Unretained(this), list,
467 std::move(callback)));
946 } 468 }
947 469
948 void CookieMonster::SetCanonicalCookieAsync( 470 void CookieMonster::SetCanonicalCookieAsync(
949 std::unique_ptr<CanonicalCookie> cookie, 471 std::unique_ptr<CanonicalCookie> cookie,
950 bool secure_source, 472 bool secure_source,
951 bool modify_http_only, 473 bool modify_http_only,
952 SetCookiesCallback callback) { 474 SetCookiesCallback callback) {
953 DCHECK(cookie->IsCanonical()); 475 DCHECK(cookie->IsCanonical());
954 scoped_refptr<SetCanonicalCookieTask> task =
955 new SetCanonicalCookieTask(this, std::move(cookie), secure_source,
956 modify_http_only, std::move(callback));
957 476
958 // TODO(rdsmith): Switch to DoCookieTaskForURL (or the equivalent). 477 // TODO(rdsmith): Switch to DoCookieCallbackForURL (or the equivalent).
959 // This is tricky because we don't have the scheme in this routine 478 // This is tricky because we don't have the scheme in this routine
960 // and DoCookieTaskForURL uses cookie_util::GetEffectiveDomain(scheme, host) 479 // and DoCookieCallbackForURL uses
480 // cookie_util::GetEffectiveDomain(scheme, host)
961 // to generate the database key to block behind. 481 // to generate the database key to block behind.
962 DoCookieTask(task); 482 DoCookieCallback(base::BindOnce(
483 // base::Unretained is safe as DoCookieCallbackForURL stores
484 // the callback on |*this|, so the callback will not outlive
485 // the object.
486 &CookieMonster::SetCanonicalCookie, base::Unretained(this),
487 std::move(cookie), secure_source, modify_http_only, std::move(callback)));
963 } 488 }
964 489
965 void CookieMonster::SetCookieWithOptionsAsync(const GURL& url, 490 void CookieMonster::SetCookieWithOptionsAsync(const GURL& url,
966 const std::string& cookie_line, 491 const std::string& cookie_line,
967 const CookieOptions& options, 492 const CookieOptions& options,
968 SetCookiesCallback callback) { 493 SetCookiesCallback callback) {
969 scoped_refptr<SetCookieWithOptionsTask> task = new SetCookieWithOptionsTask( 494 DoCookieCallbackForURL(
970 this, url, cookie_line, options, std::move(callback)); 495 base::BindOnce(
971 496 // base::Unretained is safe as DoCookieCallbackForURL stores
972 DoCookieTaskForURL(task, url); 497 // the callback on |*this|, so the callback will not outlive
498 // the object.
499 &CookieMonster::SetCookieWithOptions, base::Unretained(this), url,
500 cookie_line, options, std::move(callback)),
501 url);
973 } 502 }
974 503
975 void CookieMonster::GetCookiesWithOptionsAsync(const GURL& url, 504 void CookieMonster::GetCookiesWithOptionsAsync(const GURL& url,
976 const CookieOptions& options, 505 const CookieOptions& options,
977 GetCookiesCallback callback) { 506 GetCookiesCallback callback) {
978 scoped_refptr<GetCookiesWithOptionsTask> task = 507 DoCookieCallbackForURL(
979 new GetCookiesWithOptionsTask(this, url, options, std::move(callback)); 508 base::BindOnce(
980 509 // base::Unretained is safe as DoCookieCallbackForURL stores
981 DoCookieTaskForURL(task, url); 510 // the callback on |*this|, so the callback will not outlive
511 // the object.
512 &CookieMonster::GetCookiesWithOptions, base::Unretained(this), url,
513 options, std::move(callback)),
514 url);
982 } 515 }
983 516
984 void CookieMonster::GetCookieListWithOptionsAsync( 517 void CookieMonster::GetCookieListWithOptionsAsync(
985 const GURL& url, 518 const GURL& url,
986 const CookieOptions& options, 519 const CookieOptions& options,
987 GetCookieListCallback callback) { 520 GetCookieListCallback callback) {
988 scoped_refptr<GetCookieListWithOptionsTask> task = 521 DoCookieCallbackForURL(
989 new GetCookieListWithOptionsTask(this, url, options, std::move(callback)); 522 base::BindOnce(
990 523 // base::Unretained is safe as DoCookieCallbackForURL stores
991 DoCookieTaskForURL(task, url); 524 // the callback on |*this|, so the callback will not outlive
525 // the object.
526 &CookieMonster::GetCookieListWithOptions, base::Unretained(this), url,
527 options, std::move(callback)),
528 url);
992 } 529 }
993 530
994 void CookieMonster::GetAllCookiesAsync(GetCookieListCallback callback) { 531 void CookieMonster::GetAllCookiesAsync(GetCookieListCallback callback) {
995 scoped_refptr<GetAllCookiesTask> task = 532 DoCookieCallback(base::BindOnce(
996 new GetAllCookiesTask(this, std::move(callback)); 533 // base::Unretained is safe as DoCookieCallbackForURL stores
997 534 // the callback on |*this|, so the callback will not outlive
998 DoCookieTask(task); 535 // the object.
536 &CookieMonster::GetAllCookies, base::Unretained(this),
537 std::move(callback)));
999 } 538 }
1000 539
1001 void CookieMonster::DeleteCookieAsync(const GURL& url, 540 void CookieMonster::DeleteCookieAsync(const GURL& url,
1002 const std::string& cookie_name, 541 const std::string& cookie_name,
1003 base::OnceClosure callback) { 542 base::OnceClosure callback) {
1004 scoped_refptr<DeleteCookieTask> task = 543 DoCookieCallbackForURL(
1005 new DeleteCookieTask(this, url, cookie_name, std::move(callback)); 544 base::BindOnce(
1006 545 // base::Unretained is safe as DoCookieCallbackForURL stores
1007 DoCookieTaskForURL(task, url); 546 // the callback on |*this|, so the callback will not outlive
547 // the object.
548 &CookieMonster::DeleteCookie, base::Unretained(this), url,
549 cookie_name, std::move(callback)),
550 url);
1008 } 551 }
1009 552
1010 void CookieMonster::DeleteCanonicalCookieAsync(const CanonicalCookie& cookie, 553 void CookieMonster::DeleteCanonicalCookieAsync(const CanonicalCookie& cookie,
1011 DeleteCallback callback) { 554 DeleteCallback callback) {
1012 scoped_refptr<DeleteCanonicalCookieTask> task = 555 DoCookieCallback(base::BindOnce(
1013 new DeleteCanonicalCookieTask(this, cookie, std::move(callback)); 556 // base::Unretained is safe as DoCookieCallbackForURL stores
1014 557 // the callback on |*this|, so the callback will not outlive
1015 DoCookieTask(task); 558 // the object.
559 &CookieMonster::DeleteCanonicalCookie, base::Unretained(this), cookie,
560 std::move(callback)));
1016 } 561 }
1017 562
1018 void CookieMonster::DeleteAllCreatedBetweenAsync(const Time& delete_begin, 563 void CookieMonster::DeleteAllCreatedBetweenAsync(const Time& delete_begin,
1019 const Time& delete_end, 564 const Time& delete_end,
1020 DeleteCallback callback) { 565 DeleteCallback callback) {
1021 scoped_refptr<DeleteAllCreatedBetweenTask> task = 566 DoCookieCallback(base::BindOnce(
1022 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end, 567 // base::Unretained is safe as DoCookieCallbackForURL stores
1023 std::move(callback)); 568 // the callback on |*this|, so the callback will not outlive
1024 569 // the object.
1025 DoCookieTask(task); 570 &CookieMonster::DeleteAllCreatedBetween, base::Unretained(this),
571 delete_begin, delete_end, std::move(callback)));
1026 } 572 }
1027 573
1028 void CookieMonster::DeleteAllCreatedBetweenWithPredicateAsync( 574 void CookieMonster::DeleteAllCreatedBetweenWithPredicateAsync(
1029 const Time& delete_begin, 575 const Time& delete_begin,
1030 const Time& delete_end, 576 const Time& delete_end,
1031 const base::Callback<bool(const CanonicalCookie&)>& predicate, 577 const base::Callback<bool(const CanonicalCookie&)>& predicate,
1032 DeleteCallback callback) { 578 DeleteCallback callback) {
1033 if (predicate.is_null()) { 579 if (predicate.is_null()) {
1034 std::move(callback).Run(0); 580 ConditionalCookieCallback(std::move(callback), 0u);
1035 return; 581 return;
1036 } 582 }
1037 scoped_refptr<DeleteAllCreatedBetweenWithPredicateTask> task = 583
1038 new DeleteAllCreatedBetweenWithPredicateTask( 584 DoCookieCallback(base::BindOnce(
1039 this, delete_begin, delete_end, predicate, std::move(callback)); 585 // base::Unretained is safe as DoCookieCallbackForURL stores
1040 DoCookieTask(task); 586 // the callback on |*this|, so the callback will not outlive
587 // the object.
588 &CookieMonster::DeleteAllCreatedBetweenWithPredicate,
589 base::Unretained(this), delete_begin, delete_end, predicate,
590 std::move(callback)));
1041 } 591 }
1042 592
1043 void CookieMonster::DeleteSessionCookiesAsync( 593 void CookieMonster::DeleteSessionCookiesAsync(
1044 CookieStore::DeleteCallback callback) { 594 CookieStore::DeleteCallback callback) {
1045 scoped_refptr<DeleteSessionCookiesTask> task = 595 DoCookieCallback(base::BindOnce(
1046 new DeleteSessionCookiesTask(this, std::move(callback)); 596 // base::Unretained is safe as DoCookieCallbackForURL stores
1047 597 // the callback on |*this|, so the callback will not outlive
1048 DoCookieTask(task); 598 // the object.
599 &CookieMonster::DeleteSessionCookies, base::Unretained(this),
600 std::move(callback)));
1049 } 601 }
1050 602
1051 void CookieMonster::SetCookieableSchemes( 603 void CookieMonster::SetCookieableSchemes(
1052 const std::vector<std::string>& schemes) { 604 const std::vector<std::string>& schemes) {
1053 DCHECK(thread_checker_.CalledOnValidThread()); 605 DCHECK(thread_checker_.CalledOnValidThread());
1054 606
1055 // Calls to this method will have no effect if made after a WebView or 607 // Calls to this method will have no effect if made after a WebView or
1056 // CookieManager instance has been created. 608 // CookieManager instance has been created.
1057 if (initialized_) 609 if (initialized_)
1058 return; 610 return;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1102 // CookieChanged callbacks when the CookieStore is destroyed? 654 // CookieChanged callbacks when the CookieStore is destroyed?
1103 for (CookieMap::iterator cookie_it = cookies_.begin(); 655 for (CookieMap::iterator cookie_it = cookies_.begin();
1104 cookie_it != cookies_.end();) { 656 cookie_it != cookies_.end();) {
1105 CookieMap::iterator current_cookie_it = cookie_it; 657 CookieMap::iterator current_cookie_it = cookie_it;
1106 ++cookie_it; 658 ++cookie_it;
1107 InternalDeleteCookie(current_cookie_it, false /* sync_to_store */, 659 InternalDeleteCookie(current_cookie_it, false /* sync_to_store */,
1108 DELETE_COOKIE_DONT_RECORD); 660 DELETE_COOKIE_DONT_RECORD);
1109 } 661 }
1110 } 662 }
1111 663
1112 bool CookieMonster::SetCookieWithDetails(const GURL& url, 664 void CookieMonster::SetCookieWithDetails(const GURL& url,
1113 const std::string& name, 665 const std::string& name,
1114 const std::string& value, 666 const std::string& value,
1115 const std::string& domain, 667 const std::string& domain,
1116 const std::string& path, 668 const std::string& path,
1117 base::Time creation_time, 669 base::Time creation_time,
1118 base::Time expiration_time, 670 base::Time expiration_time,
1119 base::Time last_access_time, 671 base::Time last_access_time,
1120 bool secure, 672 bool secure,
1121 bool http_only, 673 bool http_only,
1122 CookieSameSite same_site, 674 CookieSameSite same_site,
1123 CookiePriority priority) { 675 CookiePriority priority,
676 SetCookiesCallback callback) {
1124 DCHECK(thread_checker_.CalledOnValidThread()); 677 DCHECK(thread_checker_.CalledOnValidThread());
1125 678
1126 if (!HasCookieableScheme(url)) 679 if (!HasCookieableScheme(url)) {
1127 return false; 680 ConditionalCookieCallback(std::move(callback), false);
681 return;
682 }
1128 683
1129 // Validate consistency of passed arguments. 684 // Validate consistency of passed arguments.
1130 if (ParsedCookie::ParseTokenString(name) != name || 685 if (ParsedCookie::ParseTokenString(name) != name ||
1131 ParsedCookie::ParseValueString(value) != value || 686 ParsedCookie::ParseValueString(value) != value ||
1132 ParsedCookie::ParseValueString(domain) != domain || 687 ParsedCookie::ParseValueString(domain) != domain ||
1133 ParsedCookie::ParseValueString(path) != path) { 688 ParsedCookie::ParseValueString(path) != path) {
1134 return false; 689 ConditionalCookieCallback(std::move(callback), false);
690 return;
1135 } 691 }
1136 692
1137 std::string cookie_domain; 693 std::string cookie_domain;
1138 if (!cookie_util::GetCookieDomainWithString(url, domain, &cookie_domain)) 694 if (!cookie_util::GetCookieDomainWithString(url, domain, &cookie_domain)) {
1139 return false; 695 ConditionalCookieCallback(std::move(callback), false);
696 return;
697 }
1140 698
1141 std::string cookie_path = CanonicalCookie::CanonPathWithString(url, path); 699 std::string cookie_path = CanonicalCookie::CanonPathWithString(url, path);
1142 if (!path.empty() && cookie_path != path) 700 if (!path.empty() && cookie_path != path) {
1143 return false; 701 ConditionalCookieCallback(std::move(callback), false);
702 return;
703 }
1144 704
1145 // Canonicalize path again to make sure it escapes characters as needed. 705 // Canonicalize path again to make sure it escapes characters as needed.
1146 url::Component path_component(0, cookie_path.length()); 706 url::Component path_component(0, cookie_path.length());
1147 url::RawCanonOutputT<char> canon_path; 707 url::RawCanonOutputT<char> canon_path;
1148 url::Component canon_path_component; 708 url::Component canon_path_component;
1149 url::CanonicalizePath(cookie_path.data(), path_component, &canon_path, 709 url::CanonicalizePath(cookie_path.data(), path_component, &canon_path,
1150 &canon_path_component); 710 &canon_path_component);
1151 cookie_path = std::string(canon_path.data() + canon_path_component.begin, 711 cookie_path = std::string(canon_path.data() + canon_path_component.begin,
1152 canon_path_component.len); 712 canon_path_component.len);
1153 713
1154 std::unique_ptr<CanonicalCookie> cc(base::MakeUnique<CanonicalCookie>( 714 std::unique_ptr<CanonicalCookie> cc(base::MakeUnique<CanonicalCookie>(
1155 name, value, cookie_domain, cookie_path, creation_time, expiration_time, 715 name, value, cookie_domain, cookie_path, creation_time, expiration_time,
1156 last_access_time, secure, http_only, same_site, priority)); 716 last_access_time, secure, http_only, same_site, priority));
1157 717
1158 return SetCanonicalCookie(std::move(cc), url.SchemeIsCryptographic(), true); 718 SetCanonicalCookie(std::move(cc), url.SchemeIsCryptographic(), true,
719 std::move(callback));
1159 } 720 }
1160 721
1161 CookieList CookieMonster::GetAllCookies() { 722 void CookieMonster::GetAllCookies(GetCookieListCallback callback) {
1162 DCHECK(thread_checker_.CalledOnValidThread()); 723 DCHECK(thread_checker_.CalledOnValidThread());
1163 724
1164 // This function is being called to scrape the cookie list for management UI 725 // This function is being called to scrape the cookie list for management UI
1165 // or similar. We shouldn't show expired cookies in this list since it will 726 // or similar. We shouldn't show expired cookies in this list since it will
1166 // just be confusing to users, and this function is called rarely enough (and 727 // just be confusing to users, and this function is called rarely enough (and
1167 // is already slow enough) that it's OK to take the time to garbage collect 728 // is already slow enough) that it's OK to take the time to garbage collect
1168 // the expired cookies now. 729 // the expired cookies now.
1169 // 730 //
1170 // Note that this does not prune cookies to be below our limits (if we've 731 // Note that this does not prune cookies to be below our limits (if we've
1171 // exceeded them) the way that calling GarbageCollect() would. 732 // exceeded them) the way that calling GarbageCollect() would.
1172 GarbageCollectExpired( 733 GarbageCollectExpired(
1173 Time::Now(), CookieMapItPair(cookies_.begin(), cookies_.end()), NULL); 734 Time::Now(), CookieMapItPair(cookies_.begin(), cookies_.end()), NULL);
1174 735
1175 // Copy the CanonicalCookie pointers from the map so that we can use the same 736 // Copy the CanonicalCookie pointers from the map so that we can use the same
1176 // sorter as elsewhere, then copy the result out. 737 // sorter as elsewhere, then copy the result out.
1177 std::vector<CanonicalCookie*> cookie_ptrs; 738 std::vector<CanonicalCookie*> cookie_ptrs;
1178 cookie_ptrs.reserve(cookies_.size()); 739 cookie_ptrs.reserve(cookies_.size());
1179 for (const auto& cookie : cookies_) 740 for (const auto& cookie : cookies_)
1180 cookie_ptrs.push_back(cookie.second.get()); 741 cookie_ptrs.push_back(cookie.second.get());
1181 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); 742 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
1182 743
1183 CookieList cookie_list; 744 CookieList cookie_list;
1184 cookie_list.reserve(cookie_ptrs.size()); 745 cookie_list.reserve(cookie_ptrs.size());
1185 for (auto* cookie_ptr : cookie_ptrs) 746 for (auto* cookie_ptr : cookie_ptrs)
1186 cookie_list.push_back(*cookie_ptr); 747 cookie_list.push_back(*cookie_ptr);
1187 748
1188 return cookie_list; 749 ConditionalCookieCallback(std::move(callback), cookie_list);
750 return;
mmenke 2017/07/12 18:29:04 return not needed
Randy Smith (Not in Mondays) 2017/07/12 19:47:35 Oy vais. Thank you. Fixed through file.
1189 } 751 }
1190 752
1191 CookieList CookieMonster::GetCookieListWithOptions( 753 void CookieMonster::GetCookieListWithOptions(const GURL& url,
1192 const GURL& url, 754 const CookieOptions& options,
1193 const CookieOptions& options) { 755 GetCookieListCallback callback) {
1194 DCHECK(thread_checker_.CalledOnValidThread()); 756 DCHECK(thread_checker_.CalledOnValidThread());
1195 757
1196 CookieList cookies; 758 CookieList cookies;
1197 if (!HasCookieableScheme(url)) 759 if (HasCookieableScheme(url)) {
1198 return cookies; 760 std::vector<CanonicalCookie*> cookie_ptrs;
761 FindCookiesForHostAndDomain(url, options, &cookie_ptrs);
762 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter);
1199 763
1200 std::vector<CanonicalCookie*> cookie_ptrs; 764 cookies.reserve(cookie_ptrs.size());
1201 FindCookiesForHostAndDomain(url, options, &cookie_ptrs); 765 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin();
1202 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); 766 it != cookie_ptrs.end(); it++)
1203 767 cookies.push_back(**it);
1204 cookies.reserve(cookie_ptrs.size()); 768 }
1205 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); 769 ConditionalCookieCallback(std::move(callback), cookies);
1206 it != cookie_ptrs.end(); it++) 770 return;
mmenke 2017/07/12 18:29:03 -return
Randy Smith (Not in Mondays) 2017/07/12 19:47:34 Done.
1207 cookies.push_back(**it);
1208
1209 return cookies;
1210 } 771 }
1211 772
1212 uint32_t CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin, 773 void CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin,
1213 const Time& delete_end) { 774 const Time& delete_end,
775 DeleteCallback callback) {
1214 DCHECK(thread_checker_.CalledOnValidThread()); 776 DCHECK(thread_checker_.CalledOnValidThread());
1215 777
1216 uint32_t num_deleted = 0; 778 uint32_t num_deleted = 0;
1217 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { 779 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1218 CookieMap::iterator curit = it; 780 CookieMap::iterator curit = it;
1219 CanonicalCookie* cc = curit->second.get(); 781 CanonicalCookie* cc = curit->second.get();
1220 ++it; 782 ++it;
1221 783
1222 if (cc->CreationDate() >= delete_begin && 784 if (cc->CreationDate() >= delete_begin &&
1223 (delete_end.is_null() || cc->CreationDate() < delete_end)) { 785 (delete_end.is_null() || cc->CreationDate() < delete_end)) {
1224 InternalDeleteCookie(curit, true, /*sync_to_store*/ 786 InternalDeleteCookie(curit, true, /*sync_to_store*/
1225 DELETE_COOKIE_CREATED_BETWEEN); 787 DELETE_COOKIE_CREATED_BETWEEN);
1226 ++num_deleted; 788 ++num_deleted;
1227 } 789 }
1228 } 790 }
1229 791
1230 return num_deleted; 792 FlushStore(base::BindOnce(
793 &ConditionalDeleteCallback, weak_ptr_factory_.GetWeakPtr(),
794 callback.is_null() ? base::OnceClosure()
795 : base::BindOnce(std::move(callback), num_deleted)));
796 return;
mmenke 2017/07/12 18:29:04 -return
Randy Smith (Not in Mondays) 2017/07/12 19:47:35 Done.
1231 } 797 }
1232 798
1233 uint32_t CookieMonster::DeleteAllCreatedBetweenWithPredicate( 799 void CookieMonster::DeleteAllCreatedBetweenWithPredicate(
1234 const base::Time& delete_begin, 800 const base::Time& delete_begin,
1235 const base::Time& delete_end, 801 const base::Time& delete_end,
1236 const base::Callback<bool(const CanonicalCookie&)>& predicate) { 802 const base::Callback<bool(const CanonicalCookie&)>& predicate,
803 DeleteCallback callback) {
1237 uint32_t num_deleted = 0; 804 uint32_t num_deleted = 0;
1238 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { 805 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1239 CookieMap::iterator curit = it; 806 CookieMap::iterator curit = it;
1240 CanonicalCookie* cc = curit->second.get(); 807 CanonicalCookie* cc = curit->second.get();
1241 ++it; 808 ++it;
1242 809
1243 if (cc->CreationDate() >= delete_begin && 810 if (cc->CreationDate() >= delete_begin &&
1244 // The assumption that null |delete_end| is equivalent to 811 // The assumption that null |delete_end| is equivalent to
1245 // Time::Max() is confusing. 812 // Time::Max() is confusing.
1246 (delete_end.is_null() || cc->CreationDate() < delete_end) && 813 (delete_end.is_null() || cc->CreationDate() < delete_end) &&
1247 predicate.Run(*cc)) { 814 predicate.Run(*cc)) {
1248 InternalDeleteCookie(curit, true, /*sync_to_store*/ 815 InternalDeleteCookie(curit, true, /*sync_to_store*/
1249 DELETE_COOKIE_CREATED_BETWEEN_WITH_PREDICATE); 816 DELETE_COOKIE_CREATED_BETWEEN_WITH_PREDICATE);
1250 ++num_deleted; 817 ++num_deleted;
1251 } 818 }
1252 } 819 }
1253 820
1254 return num_deleted; 821 FlushStore(base::BindOnce(
822 &ConditionalDeleteCallback, weak_ptr_factory_.GetWeakPtr(),
823 callback.is_null() ? base::OnceClosure()
824 : base::BindOnce(std::move(callback), num_deleted)));
825 return;
1255 } 826 }
1256 827
1257 bool CookieMonster::SetCookieWithOptions(const GURL& url, 828 void CookieMonster::SetCookieWithOptions(const GURL& url,
1258 const std::string& cookie_line, 829 const std::string& cookie_line,
1259 const CookieOptions& options) { 830 const CookieOptions& options,
831 SetCookiesCallback callback) {
1260 DCHECK(thread_checker_.CalledOnValidThread()); 832 DCHECK(thread_checker_.CalledOnValidThread());
1261 833
1262 if (!HasCookieableScheme(url)) { 834 if (!HasCookieableScheme(url)) {
1263 return false; 835 ConditionalCookieCallback(std::move(callback), false);
836 return;
1264 } 837 }
1265 838
1266 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options); 839 SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options,
840 std::move(callback));
841 return;
mmenke 2017/07/12 18:29:03 -return
Randy Smith (Not in Mondays) 2017/07/12 19:47:35 Done.
1267 } 842 }
1268 843
1269 std::string CookieMonster::GetCookiesWithOptions(const GURL& url, 844 void CookieMonster::GetCookiesWithOptions(const GURL& url,
1270 const CookieOptions& options) { 845 const CookieOptions& options,
846 GetCookiesCallback callback) {
1271 DCHECK(thread_checker_.CalledOnValidThread()); 847 DCHECK(thread_checker_.CalledOnValidThread());
1272 848
1273 if (!HasCookieableScheme(url)) 849 if (!HasCookieableScheme(url)) {
1274 return std::string(); 850 ConditionalCookieCallback(std::move(callback), std::string());
851 return;
852 }
1275 853
1276 std::vector<CanonicalCookie*> cookies; 854 std::vector<CanonicalCookie*> cookies;
1277 FindCookiesForHostAndDomain(url, options, &cookies); 855 FindCookiesForHostAndDomain(url, options, &cookies);
1278 std::sort(cookies.begin(), cookies.end(), CookieSorter); 856 std::sort(cookies.begin(), cookies.end(), CookieSorter);
1279 857
1280 std::string cookie_line = BuildCookieLine(cookies); 858 std::string cookie_line = BuildCookieLine(cookies);
1281 859
1282 VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line; 860 VLOG(kVlogGetCookies) << "GetCookies() result: " << cookie_line;
1283 861
1284 return cookie_line; 862 ConditionalCookieCallback(std::move(callback), cookie_line);
mmenke 2017/07/12 18:29:04 Optional: Could combine the paths like you did wi
Randy Smith (Not in Mondays) 2017/07/12 19:47:35 Done.
863 return;
mmenke 2017/07/12 18:29:04 -return
Randy Smith (Not in Mondays) 2017/07/12 19:47:35 Done.
1285 } 864 }
1286 865
1287 void CookieMonster::DeleteCookie(const GURL& url, 866 void CookieMonster::DeleteCookie(const GURL& url,
1288 const std::string& cookie_name) { 867 const std::string& cookie_name,
868 base::OnceClosure callback) {
1289 DCHECK(thread_checker_.CalledOnValidThread()); 869 DCHECK(thread_checker_.CalledOnValidThread());
1290 870
1291 if (!HasCookieableScheme(url)) 871 if (!HasCookieableScheme(url)) {
872 // TODO(rdsmith): Would be good to provide a failure indication here.
mmenke 2017/07/12 18:29:04 Could just make all delete callbacks report number
Randy Smith (Not in Mondays) 2017/07/12 19:47:34 Perfectly reasonable suggestion, but not in this C
mmenke 2017/07/12 20:00:11 Sure, was not suggesting it for this CL. :)
873 ConditionalCookieCallback(std::move(callback));
1292 return; 874 return;
875 }
1293 876
1294 CookieOptions options; 877 CookieOptions options;
1295 options.set_include_httponly(); 878 options.set_include_httponly();
1296 options.set_same_site_cookie_mode( 879 options.set_same_site_cookie_mode(
1297 CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX); 880 CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX);
1298 // Get the cookies for this host and its domain(s). 881 // Get the cookies for this host and its domain(s).
1299 std::vector<CanonicalCookie*> cookies; 882 std::vector<CanonicalCookie*> cookies;
1300 FindCookiesForHostAndDomain(url, options, &cookies); 883 FindCookiesForHostAndDomain(url, options, &cookies);
1301 std::set<CanonicalCookie*> matching_cookies; 884 std::set<CanonicalCookie*> matching_cookies;
1302 885
1303 for (auto* cookie : cookies) { 886 for (auto* cookie : cookies) {
1304 if (cookie->Name() != cookie_name) 887 if (cookie->Name() != cookie_name)
1305 continue; 888 continue;
1306 if (!cookie->IsOnPath(url.path())) 889 if (!cookie->IsOnPath(url.path()))
1307 continue; 890 continue;
1308 matching_cookies.insert(cookie); 891 matching_cookies.insert(cookie);
1309 } 892 }
1310 893
1311 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { 894 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1312 CookieMap::iterator curit = it; 895 CookieMap::iterator curit = it;
1313 ++it; 896 ++it;
1314 if (matching_cookies.find(curit->second.get()) != matching_cookies.end()) { 897 if (matching_cookies.find(curit->second.get()) != matching_cookies.end()) {
1315 InternalDeleteCookie(curit, true, DELETE_COOKIE_SINGLE); 898 InternalDeleteCookie(curit, true, DELETE_COOKIE_SINGLE);
1316 } 899 }
1317 } 900 }
901
902 FlushStore(base::BindOnce(&ConditionalDeleteCallback,
903 weak_ptr_factory_.GetWeakPtr(),
904 std::move(callback)));
mmenke 2017/07/12 18:29:03 The next method has a null callback check, but thi
Randy Smith (Not in Mondays) 2017/07/12 19:47:35 As per our offline conversation, put in a comment
1318 } 905 }
1319 906
1320 uint32_t CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) { 907 void CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie,
908 DeleteCallback callback) {
1321 DCHECK(thread_checker_.CalledOnValidThread()); 909 DCHECK(thread_checker_.CalledOnValidThread());
1322 910
911 uint32_t result = 0u;
1323 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); 912 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain()));
1324 its.first != its.second; ++its.first) { 913 its.first != its.second; ++its.first) {
1325 // The creation date acts as the unique index... 914 // The creation date acts as the unique index...
1326 if (its.first->second->CreationDate() == cookie.CreationDate()) { 915 if (its.first->second->CreationDate() == cookie.CreationDate()) {
1327 InternalDeleteCookie(its.first, true, DELETE_COOKIE_CANONICAL); 916 InternalDeleteCookie(its.first, true, DELETE_COOKIE_CANONICAL);
1328 return 1u; 917 result = 1u;
918 break;
1329 } 919 }
1330 } 920 }
1331 return 0u; 921 FlushStore(base::BindOnce(
922 &ConditionalDeleteCallback, weak_ptr_factory_.GetWeakPtr(),
923 callback.is_null() ? base::OnceClosure()
924 : base::BindOnce(std::move(callback), result)));
1332 } 925 }
1333 926
1334 bool CookieMonster::SetCookieWithCreationTime(const GURL& url, 927 void CookieMonster::SetCookieWithCreationTimeForTesting(
1335 const std::string& cookie_line, 928 const GURL& url,
1336 const base::Time& creation_time) { 929 const std::string& cookie_line,
930 const base::Time& creation_time,
931 SetCookiesCallback callback) {
1337 DCHECK(thread_checker_.CalledOnValidThread()); 932 DCHECK(thread_checker_.CalledOnValidThread());
1338 DCHECK(!store_.get()) << "This method is only to be used by unit-tests."; 933 DCHECK(!store_.get()) << "This method is only to be used by unit-tests.";
1339 934
1340 if (!HasCookieableScheme(url)) { 935 if (!HasCookieableScheme(url)) {
1341 return false; 936 ConditionalCookieCallback(std::move(callback), false);
937 return;
1342 } 938 }
1343 939
1344 MarkCookieStoreAsInitialized(); 940 MarkCookieStoreAsInitialized();
1345 if (ShouldFetchAllCookiesWhenFetchingAnyCookie()) 941 if (ShouldFetchAllCookiesWhenFetchingAnyCookie())
1346 FetchAllCookiesIfNecessary(); 942 FetchAllCookiesIfNecessary();
1347 943
1348 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time, 944 return SetCookieWithCreationTimeAndOptions(
1349 CookieOptions()); 945 url, cookie_line, creation_time, CookieOptions(), std::move(callback));
1350 } 946 }
1351 947
1352 uint32_t CookieMonster::DeleteSessionCookies() { 948 void CookieMonster::DeleteSessionCookies(DeleteCallback callback) {
1353 DCHECK(thread_checker_.CalledOnValidThread()); 949 DCHECK(thread_checker_.CalledOnValidThread());
1354 950
1355 uint32_t num_deleted = 0; 951 uint32_t num_deleted = 0;
1356 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { 952 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) {
1357 CookieMap::iterator curit = it; 953 CookieMap::iterator curit = it;
1358 CanonicalCookie* cc = curit->second.get(); 954 CanonicalCookie* cc = curit->second.get();
1359 ++it; 955 ++it;
1360 956
1361 if (!cc->IsPersistent()) { 957 if (!cc->IsPersistent()) {
1362 InternalDeleteCookie(curit, true, /*sync_to_store*/ 958 InternalDeleteCookie(curit, true, /*sync_to_store*/
1363 DELETE_COOKIE_EXPIRED); 959 DELETE_COOKIE_EXPIRED);
1364 ++num_deleted; 960 ++num_deleted;
1365 } 961 }
1366 } 962 }
1367 963
1368 return num_deleted; 964 FlushStore(base::BindOnce(
965 &ConditionalDeleteCallback, weak_ptr_factory_.GetWeakPtr(),
966 callback.is_null() ? base::OnceClosure()
967 : base::BindOnce(std::move(callback), num_deleted)));
968 return;
mmenke 2017/07/12 18:29:04 -return
Randy Smith (Not in Mondays) 2017/07/12 19:47:34 Done.
1369 } 969 }
1370 970
1371 void CookieMonster::MarkCookieStoreAsInitialized() { 971 void CookieMonster::MarkCookieStoreAsInitialized() {
1372 DCHECK(thread_checker_.CalledOnValidThread()); 972 DCHECK(thread_checker_.CalledOnValidThread());
1373 initialized_ = true; 973 initialized_ = true;
1374 } 974 }
1375 975
1376 void CookieMonster::FetchAllCookiesIfNecessary() { 976 void CookieMonster::FetchAllCookiesIfNecessary() {
1377 DCHECK(thread_checker_.CalledOnValidThread()); 977 DCHECK(thread_checker_.CalledOnValidThread());
1378 if (store_.get() && !started_fetching_all_cookies_) { 978 if (store_.get() && !started_fetching_all_cookies_) {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1433 1033
1434 auto tasks_pending_for_key = tasks_pending_for_key_.find(key); 1034 auto tasks_pending_for_key = tasks_pending_for_key_.find(key);
1435 1035
1436 // TODO(mmenke): Can this be turned into a DCHECK? 1036 // TODO(mmenke): Can this be turned into a DCHECK?
1437 if (tasks_pending_for_key == tasks_pending_for_key_.end()) 1037 if (tasks_pending_for_key == tasks_pending_for_key_.end())
1438 return; 1038 return;
1439 1039
1440 // Run all tasks for the key. Note that running a task can result in multiple 1040 // Run all tasks for the key. Note that running a task can result in multiple
1441 // tasks being added to the back of the deque. 1041 // tasks being added to the back of the deque.
1442 while (!tasks_pending_for_key->second.empty()) { 1042 while (!tasks_pending_for_key->second.empty()) {
1443 scoped_refptr<CookieMonsterTask> task = 1043 base::OnceClosure task = std::move(tasks_pending_for_key->second.front());
1444 tasks_pending_for_key->second.front();
1445 tasks_pending_for_key->second.pop_front(); 1044 tasks_pending_for_key->second.pop_front();
1446 1045 std::move(task).Run();
1447 task->Run();
1448 } 1046 }
1449 1047
1450 tasks_pending_for_key_.erase(tasks_pending_for_key); 1048 tasks_pending_for_key_.erase(tasks_pending_for_key);
1451 1049
1452 // This has to be done last, in case running a task queues a new task for the 1050 // This has to be done last, in case running a task queues a new task for the
1453 // key, to ensure tasks are run in the correct order. 1051 // key, to ensure tasks are run in the correct order.
1454 keys_loaded_.insert(key); 1052 keys_loaded_.insert(key);
1455 } 1053 }
1456 1054
1457 void CookieMonster::StoreLoadedCookies( 1055 void CookieMonster::StoreLoadedCookies(
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1517 void CookieMonster::InvokeQueue() { 1115 void CookieMonster::InvokeQueue() {
1518 DCHECK(thread_checker_.CalledOnValidThread()); 1116 DCHECK(thread_checker_.CalledOnValidThread());
1519 1117
1520 // Move all per-key tasks into the global queue, if there are any. This is 1118 // Move all per-key tasks into the global queue, if there are any. This is
1521 // protection about a race where the store learns about all cookies loading 1119 // protection about a race where the store learns about all cookies loading
1522 // before it learned about the cookies for a key loading. 1120 // before it learned about the cookies for a key loading.
1523 1121
1524 // Needed to prevent any recursively queued tasks from going back into the 1122 // Needed to prevent any recursively queued tasks from going back into the
1525 // per-key queues. 1123 // per-key queues.
1526 seen_global_task_ = true; 1124 seen_global_task_ = true;
1527 for (const auto& tasks_for_key : tasks_pending_for_key_) { 1125 for (auto& tasks_for_key : tasks_pending_for_key_) {
1528 tasks_pending_.insert(tasks_pending_.begin(), tasks_for_key.second.begin(), 1126 tasks_pending_.insert(tasks_pending_.begin(),
1529 tasks_for_key.second.end()); 1127 std::make_move_iterator(tasks_for_key.second.begin()),
1128 std::make_move_iterator(tasks_for_key.second.end()));
1530 } 1129 }
1531 tasks_pending_for_key_.clear(); 1130 tasks_pending_for_key_.clear();
1532 1131
1533 while (!tasks_pending_.empty()) { 1132 while (!tasks_pending_.empty()) {
1534 scoped_refptr<CookieMonsterTask> request_task = tasks_pending_.front(); 1133 base::OnceClosure request_task = std::move(tasks_pending_.front());
1535 tasks_pending_.pop_front(); 1134 tasks_pending_.pop_front();
1536 request_task->Run(); 1135 std::move(request_task).Run();
1537 } 1136 }
1538 1137
1539 DCHECK(tasks_pending_for_key_.empty()); 1138 DCHECK(tasks_pending_for_key_.empty());
1540 1139
1541 finished_fetching_all_cookies_ = true; 1140 finished_fetching_all_cookies_ = true;
1542 creation_times_.clear(); 1141 creation_times_.clear();
1543 keys_loaded_.clear(); 1142 keys_loaded_.clear();
1544 } 1143 }
1545 1144
1546 void CookieMonster::EnsureCookiesMapIsValid() { 1145 void CookieMonster::EnsureCookiesMapIsValid() {
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
1772 : 0; 1371 : 0;
1773 type_sample |= cc_ptr->IsHttpOnly() ? 1 << COOKIE_TYPE_HTTPONLY : 0; 1372 type_sample |= cc_ptr->IsHttpOnly() ? 1 << COOKIE_TYPE_HTTPONLY : 0;
1774 type_sample |= cc_ptr->IsSecure() ? 1 << COOKIE_TYPE_SECURE : 0; 1373 type_sample |= cc_ptr->IsSecure() ? 1 << COOKIE_TYPE_SECURE : 0;
1775 histogram_cookie_type_->Add(type_sample); 1374 histogram_cookie_type_->Add(type_sample);
1776 1375
1777 RunCookieChangedCallbacks(*cc_ptr, CookieStore::ChangeCause::INSERTED); 1376 RunCookieChangedCallbacks(*cc_ptr, CookieStore::ChangeCause::INSERTED);
1778 1377
1779 return inserted; 1378 return inserted;
1780 } 1379 }
1781 1380
1782 bool CookieMonster::SetCookieWithCreationTimeAndOptions( 1381 void CookieMonster::SetCookieWithCreationTimeAndOptions(
1783 const GURL& url, 1382 const GURL& url,
1784 const std::string& cookie_line, 1383 const std::string& cookie_line,
1785 const Time& creation_time_or_null, 1384 const Time& creation_time_or_null,
1786 const CookieOptions& options) { 1385 const CookieOptions& options,
1386 SetCookiesCallback callback) {
1787 DCHECK(thread_checker_.CalledOnValidThread()); 1387 DCHECK(thread_checker_.CalledOnValidThread());
1788 1388
1789 VLOG(kVlogSetCookies) << "SetCookie() line: " << cookie_line; 1389 VLOG(kVlogSetCookies) << "SetCookie() line: " << cookie_line;
1790 1390
1791 Time creation_time = creation_time_or_null; 1391 Time creation_time = creation_time_or_null;
1792 if (creation_time.is_null()) { 1392 if (creation_time.is_null()) {
1793 creation_time = CurrentTime(); 1393 creation_time = CurrentTime();
1794 last_time_seen_ = creation_time; 1394 last_time_seen_ = creation_time;
1795 } 1395 }
1796 1396
1797 std::unique_ptr<CanonicalCookie> cc( 1397 std::unique_ptr<CanonicalCookie> cc(
1798 CanonicalCookie::Create(url, cookie_line, creation_time, options)); 1398 CanonicalCookie::Create(url, cookie_line, creation_time, options));
1799 1399
1800 if (!cc.get()) { 1400 if (!cc.get()) {
1801 VLOG(kVlogSetCookies) << "WARNING: Failed to allocate CanonicalCookie"; 1401 VLOG(kVlogSetCookies) << "WARNING: Failed to allocate CanonicalCookie";
1802 return false; 1402 ConditionalCookieCallback(std::move(callback), false);
1403 return;
1803 } 1404 }
1804 return SetCanonicalCookie(std::move(cc), url.SchemeIsCryptographic(), 1405 SetCanonicalCookie(std::move(cc), url.SchemeIsCryptographic(),
1805 !options.exclude_httponly()); 1406 !options.exclude_httponly(), std::move(callback));
1806 } 1407 }
1807 1408
1808 bool CookieMonster::SetCanonicalCookie(std::unique_ptr<CanonicalCookie> cc, 1409 void CookieMonster::SetCanonicalCookie(std::unique_ptr<CanonicalCookie> cc,
1809 bool secure_source, 1410 bool secure_source,
1810 bool modify_http_only) { 1411 bool modify_http_only,
1412 SetCookiesCallback callback) {
1811 DCHECK(thread_checker_.CalledOnValidThread()); 1413 DCHECK(thread_checker_.CalledOnValidThread());
1812 1414
1813 if (cc->IsSecure() && !secure_source) 1415 if (cc->IsSecure() && !secure_source) {
1814 return false; 1416 ConditionalCookieCallback(std::move(callback), false);
1417 return;
1418 }
1815 1419
1816 const std::string key(GetKey(cc->Domain())); 1420 const std::string key(GetKey(cc->Domain()));
1817 1421
1818 // TODO(mmenke): This class assumes each cookie to have a unique creation 1422 // TODO(mmenke): This class assumes each cookie to have a unique creation
1819 // time. Allowing the caller to set the creation time violates that 1423 // time. Allowing the caller to set the creation time violates that
1820 // assumption. Worth fixing? Worth noting that time changes between browser 1424 // assumption. Worth fixing? Worth noting that time changes between browser
1821 // restarts can cause the same issue. 1425 // restarts can cause the same issue.
1822 base::Time creation_date = cc->CreationDate(); 1426 base::Time creation_date = cc->CreationDate();
1823 if (creation_date.is_null()) { 1427 if (creation_date.is_null()) {
1824 creation_date = CurrentTime(); 1428 creation_date = CurrentTime();
1825 cc->SetCreationDate(creation_date); 1429 cc->SetCreationDate(creation_date);
1826 last_time_seen_ = creation_date; 1430 last_time_seen_ = creation_date;
1827 } 1431 }
1828 bool already_expired = cc->IsExpired(creation_date); 1432 bool already_expired = cc->IsExpired(creation_date);
1829 1433
1830 if (DeleteAnyEquivalentCookie(key, *cc, secure_source, !modify_http_only, 1434 if (DeleteAnyEquivalentCookie(key, *cc, secure_source, !modify_http_only,
1831 already_expired)) { 1435 already_expired)) {
1832 std::string error; 1436 std::string error;
1833 error = 1437 error =
1834 "SetCookie() not clobbering httponly cookie or secure cookie for " 1438 "SetCookie() not clobbering httponly cookie or secure cookie for "
1835 "insecure scheme"; 1439 "insecure scheme";
1836 1440
1837 VLOG(kVlogSetCookies) << error; 1441 VLOG(kVlogSetCookies) << error;
1838 return false; 1442 ConditionalCookieCallback(std::move(callback), false);
1443 return;
1839 } 1444 }
1840 1445
1841 VLOG(kVlogSetCookies) << "SetCookie() key: " << key 1446 VLOG(kVlogSetCookies) << "SetCookie() key: " << key
1842 << " cc: " << cc->DebugString(); 1447 << " cc: " << cc->DebugString();
1843 1448
1844 // Realize that we might be setting an expired cookie, and the only point 1449 // Realize that we might be setting an expired cookie, and the only point
1845 // was to delete the cookie which we've already done. 1450 // was to delete the cookie which we've already done.
1846 if (!already_expired) { 1451 if (!already_expired) {
1847 // See InitializeHistograms() for details. 1452 // See InitializeHistograms() for details.
1848 if (cc->IsPersistent()) { 1453 if (cc->IsPersistent()) {
(...skipping 21 matching lines...) Expand all
1870 VLOG(kVlogSetCookies) << "SetCookie() not storing already expired cookie."; 1475 VLOG(kVlogSetCookies) << "SetCookie() not storing already expired cookie.";
1871 } 1476 }
1872 1477
1873 // We assume that hopefully setting a cookie will be less common than 1478 // We assume that hopefully setting a cookie will be less common than
1874 // querying a cookie. Since setting a cookie can put us over our limits, 1479 // querying a cookie. Since setting a cookie can put us over our limits,
1875 // make sure that we garbage collect... We can also make the assumption that 1480 // make sure that we garbage collect... We can also make the assumption that
1876 // if a cookie was set, in the common case it will be used soon after, 1481 // if a cookie was set, in the common case it will be used soon after,
1877 // and we will purge the expired cookies in GetCookies(). 1482 // and we will purge the expired cookies in GetCookies().
1878 GarbageCollect(creation_date, key); 1483 GarbageCollect(creation_date, key);
1879 1484
1880 return true; 1485 ConditionalCookieCallback(std::move(callback), true);
1881 } 1486 }
1882 1487
1883 bool CookieMonster::SetAllCookies(const CookieList& list) { 1488 void CookieMonster::SetAllCookies(CookieList list,
1489 SetCookiesCallback callback) {
1884 DCHECK(thread_checker_.CalledOnValidThread()); 1490 DCHECK(thread_checker_.CalledOnValidThread());
1491 CookieList positive_diff;
1492 CookieList negative_diff;
1493
1494 CookieList old_cookies;
1495 old_cookies.reserve(cookies_.size());
1496 for (const auto& cookie : cookies_)
1497 old_cookies.push_back(*cookie.second.get());
1498
1499 ComputeCookieDiff(&old_cookies, &list, &positive_diff, &negative_diff);
1500
1501 for (const auto& cookie_to_delete : negative_diff) {
1502 for (CookieMapItPair its =
1503 cookies_.equal_range(GetKey(cookie_to_delete.Domain()));
1504 its.first != its.second; ++its.first) {
1505 // The creation date acts as the unique index...
1506 if (its.first->second->CreationDate() ==
1507 cookie_to_delete.CreationDate()) {
1508 // TODO(rdsmith): DELETE_COOKIE_CANONICAL is incorrect and should
1509 // be changed.
1510 InternalDeleteCookie(its.first, true, DELETE_COOKIE_CANONICAL);
1511 break;
1512 }
1513 }
1514 }
1515
1516 if (positive_diff.size() == 0) {
mmenke 2017/07/12 18:29:03 This is old code, and I'm not asking you to fix it
Randy Smith (Not in Mondays) 2017/07/12 19:47:35 As noted in the review request message, this funct
1517 ConditionalCookieCallback(std::move(callback), true);
1518 return;
1519 }
1520
1885 for (const auto& cookie : list) { 1521 for (const auto& cookie : list) {
1886 const std::string key(GetKey(cookie.Domain())); 1522 const std::string key(GetKey(cookie.Domain()));
1887 Time creation_time = cookie.CreationDate(); 1523 Time creation_time = cookie.CreationDate();
1888 bool already_expired = cookie.IsExpired(creation_time); 1524 bool already_expired = cookie.IsExpired(creation_time);
1889 1525
1890 bool result = 1526 bool result =
1891 DeleteAnyEquivalentCookie(key, cookie, true, false, already_expired); 1527 DeleteAnyEquivalentCookie(key, cookie, true, false, already_expired);
1892 DCHECK(!result); 1528 DCHECK(!result);
1893 1529
1894 if (already_expired) 1530 if (already_expired)
1895 continue; 1531 continue;
1896 1532
1897 if (cookie.IsPersistent()) { 1533 if (cookie.IsPersistent()) {
1898 histogram_expiration_duration_minutes_->Add( 1534 histogram_expiration_duration_minutes_->Add(
1899 (cookie.ExpiryDate() - creation_time).InMinutes()); 1535 (cookie.ExpiryDate() - creation_time).InMinutes());
1900 } 1536 }
1901 1537
1902 InternalInsertCookie(key, base::MakeUnique<CanonicalCookie>(cookie), true); 1538 InternalInsertCookie(key, base::MakeUnique<CanonicalCookie>(cookie), true);
1903 GarbageCollect(creation_time, key); 1539 GarbageCollect(creation_time, key);
1904 } 1540 }
1905 1541
1906 // TODO(rdsmith): If this function always returns the same value, it 1542 // TODO(rdsmith): If this function always returns the same value, it
1907 // shouldn't have a return value. But it should also be deleted (see 1543 // shouldn't have a return value. But it should also be deleted (see
1908 // https://codereview.chromium.org/2882063002/#msg64), which would 1544 // https://codereview.chromium.org/2882063002/#msg64), which would
1909 // solve the return value problem. 1545 // solve the return value problem.
1910 return true; 1546 ConditionalCookieCallback(std::move(callback), true);
1547 return;
mmenke 2017/07/12 18:29:04 -return
Randy Smith (Not in Mondays) 2017/07/12 19:47:34 Done.
1911 } 1548 }
1912 1549
1913 void CookieMonster::InternalUpdateCookieAccessTime(CanonicalCookie* cc, 1550 void CookieMonster::InternalUpdateCookieAccessTime(CanonicalCookie* cc,
1914 const Time& current) { 1551 const Time& current) {
1915 DCHECK(thread_checker_.CalledOnValidThread()); 1552 DCHECK(thread_checker_.CalledOnValidThread());
1916 1553
1917 // Based off the Mozilla code. When a cookie has been accessed recently, 1554 // Based off the Mozilla code. When a cookie has been accessed recently,
1918 // don't bother updating its access time again. This reduces the number of 1555 // don't bother updating its access time again. This reduces the number of
1919 // updates we do during pageload, which in turn reduces the chance our storage 1556 // updates we do during pageload, which in turn reduces the chance our storage
1920 // backend will hit its batch thresholds and be forced to update. 1557 // backend will hit its batch thresholds and be forced to update.
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after
2363 } 2000 }
2364 2001
2365 // The system resolution is not high enough, so we can have multiple 2002 // The system resolution is not high enough, so we can have multiple
2366 // set cookies that result in the same system time. When this happens, we 2003 // set cookies that result in the same system time. When this happens, we
2367 // increment by one Time unit. Let's hope computers don't get too fast. 2004 // increment by one Time unit. Let's hope computers don't get too fast.
2368 Time CookieMonster::CurrentTime() { 2005 Time CookieMonster::CurrentTime() {
2369 return std::max(Time::Now(), Time::FromInternalValue( 2006 return std::max(Time::Now(), Time::FromInternalValue(
2370 last_time_seen_.ToInternalValue() + 1)); 2007 last_time_seen_.ToInternalValue() + 1));
2371 } 2008 }
2372 2009
2373 void CookieMonster::DoCookieTask( 2010 void CookieMonster::DoCookieCallback(base::OnceClosure callback) {
2374 const scoped_refptr<CookieMonsterTask>& task_item) {
2375 DCHECK(thread_checker_.CalledOnValidThread()); 2011 DCHECK(thread_checker_.CalledOnValidThread());
2376 2012
2377 MarkCookieStoreAsInitialized(); 2013 MarkCookieStoreAsInitialized();
2378 FetchAllCookiesIfNecessary(); 2014 FetchAllCookiesIfNecessary();
2379 seen_global_task_ = true; 2015 seen_global_task_ = true;
2380 2016
2381 if (!finished_fetching_all_cookies_ && store_.get()) { 2017 if (!finished_fetching_all_cookies_ && store_.get()) {
2382 tasks_pending_.push_back(task_item); 2018 tasks_pending_.push_back(std::move(callback));
2383 return; 2019 return;
2384 } 2020 }
2385 2021
2386 task_item->Run(); 2022 ConditionalCookieCallback(std::move(callback));
mmenke 2017/07/12 18:29:03 I don't think these can be nullptr?
Randy Smith (Not in Mondays) 2017/07/12 19:47:35 Ah, good point. Done.
2387 } 2023 }
2388 2024
2389 void CookieMonster::DoCookieTaskForURL( 2025 void CookieMonster::DoCookieCallbackForURL(base::OnceClosure callback,
2390 const scoped_refptr<CookieMonsterTask>& task_item, 2026 const GURL& url) {
2391 const GURL& url) {
2392 MarkCookieStoreAsInitialized(); 2027 MarkCookieStoreAsInitialized();
2393 if (ShouldFetchAllCookiesWhenFetchingAnyCookie()) 2028 if (ShouldFetchAllCookiesWhenFetchingAnyCookie())
2394 FetchAllCookiesIfNecessary(); 2029 FetchAllCookiesIfNecessary();
2395 2030
2396 // If cookies for the requested domain key (eTLD+1) have been loaded from DB 2031 // If cookies for the requested domain key (eTLD+1) have been loaded from DB
2397 // then run the task, otherwise load from DB. 2032 // then run the task, otherwise load from DB.
2398 if (!finished_fetching_all_cookies_ && store_.get()) { 2033 if (!finished_fetching_all_cookies_ && store_.get()) {
2399 // If a global task has been previously seen, queue the task as a global 2034 // If a global task has been previously seen, queue the task as a global
2400 // task. Note that the CookieMonster may be in the middle of executing 2035 // task. Note that the CookieMonster may be in the middle of executing
2401 // the global queue, |tasks_pending_| may be empty, which is why another 2036 // the global queue, |tasks_pending_| may be empty, which is why another
2402 // bool is needed. 2037 // bool is needed.
2403 if (seen_global_task_) { 2038 if (seen_global_task_) {
2404 tasks_pending_.push_back(task_item); 2039 tasks_pending_.push_back(std::move(callback));
2405 return; 2040 return;
2406 } 2041 }
2407 2042
2408 // Checks if the domain key has been loaded. 2043 // Checks if the domain key has been loaded.
2409 std::string key(cookie_util::GetEffectiveDomain(url.scheme(), url.host())); 2044 std::string key(cookie_util::GetEffectiveDomain(url.scheme(), url.host()));
2410 if (keys_loaded_.find(key) == keys_loaded_.end()) { 2045 if (keys_loaded_.find(key) == keys_loaded_.end()) {
2411 std::map<std::string, 2046 std::map<std::string, std::deque<base::OnceClosure>>::iterator it =
2412 std::deque<scoped_refptr<CookieMonsterTask>>>::iterator it =
2413 tasks_pending_for_key_.find(key); 2047 tasks_pending_for_key_.find(key);
2414 if (it == tasks_pending_for_key_.end()) { 2048 if (it == tasks_pending_for_key_.end()) {
2415 store_->LoadCookiesForKey( 2049 store_->LoadCookiesForKey(
2416 key, base::Bind(&CookieMonster::OnKeyLoaded, 2050 key, base::Bind(&CookieMonster::OnKeyLoaded,
2417 weak_ptr_factory_.GetWeakPtr(), key)); 2051 weak_ptr_factory_.GetWeakPtr(), key));
2418 it = tasks_pending_for_key_ 2052 it = tasks_pending_for_key_
2419 .insert(std::make_pair( 2053 .insert(std::make_pair(key, std::deque<base::OnceClosure>()))
2420 key, std::deque<scoped_refptr<CookieMonsterTask>>()))
2421 .first; 2054 .first;
2422 } 2055 }
2423 it->second.push_back(task_item); 2056 it->second.push_back(std::move(callback));
2424 return; 2057 return;
2425 } 2058 }
2426 } 2059 }
2427 2060
2428 task_item->Run(); 2061 ConditionalCookieCallback(std::move(callback));
mmenke 2017/07/12 18:29:04 ditto
Randy Smith (Not in Mondays) 2017/07/12 19:47:35 Done.
2429 } 2062 }
2430 2063
2431 void CookieMonster::ComputeCookieDiff(CookieList* old_cookies, 2064 void CookieMonster::ComputeCookieDiff(CookieList* old_cookies,
2432 CookieList* new_cookies, 2065 CookieList* new_cookies,
2433 CookieList* cookies_to_add, 2066 CookieList* cookies_to_add,
2434 CookieList* cookies_to_delete) { 2067 CookieList* cookies_to_delete) {
2435 DCHECK(thread_checker_.CalledOnValidThread()); 2068 DCHECK(thread_checker_.CalledOnValidThread());
2436 2069
2437 DCHECK(old_cookies); 2070 DCHECK(old_cookies);
2438 DCHECK(new_cookies); 2071 DCHECK(new_cookies);
(...skipping 17 matching lines...) Expand all
2456 PartialDiffCookieSorter); 2089 PartialDiffCookieSorter);
2457 2090
2458 // Select any new cookie for addition (or update) if no old cookie is exactly 2091 // Select any new cookie for addition (or update) if no old cookie is exactly
2459 // equivalent. 2092 // equivalent.
2460 std::set_difference(new_cookies->begin(), new_cookies->end(), 2093 std::set_difference(new_cookies->begin(), new_cookies->end(),
2461 old_cookies->begin(), old_cookies->end(), 2094 old_cookies->begin(), old_cookies->end(),
2462 std::inserter(*cookies_to_add, cookies_to_add->begin()), 2095 std::inserter(*cookies_to_add, cookies_to_add->begin()),
2463 FullDiffCookieSorter); 2096 FullDiffCookieSorter);
2464 } 2097 }
2465 2098
2466 void CookieMonster::RunCallback(base::OnceClosure callback) {
2467 DCHECK(thread_checker_.CalledOnValidThread());
2468 std::move(callback).Run();
2469 }
2470
2471 void CookieMonster::RunCookieChangedCallbacks(const CanonicalCookie& cookie, 2099 void CookieMonster::RunCookieChangedCallbacks(const CanonicalCookie& cookie,
2472 ChangeCause cause) { 2100 ChangeCause cause) {
2473 DCHECK(thread_checker_.CalledOnValidThread()); 2101 DCHECK(thread_checker_.CalledOnValidThread());
2474 2102
2475 CookieOptions opts; 2103 CookieOptions opts;
2476 opts.set_include_httponly(); 2104 opts.set_include_httponly();
2477 opts.set_same_site_cookie_mode( 2105 opts.set_same_site_cookie_mode(
2478 CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX); 2106 CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX);
2479 // Note that the callbacks in hook_map_ are wrapped with RunAsync(), so they 2107 // Note that the callbacks in hook_map_ are wrapped with RunAsync(), so they
2480 // are guaranteed to not take long - they just post a RunAsync task back to 2108 // are guaranteed to not take long - they just post a RunAsync task back to
2481 // the appropriate thread's message loop and return. 2109 // the appropriate thread's message loop and return.
2482 // TODO(mmenke): Consider running these synchronously? 2110 // TODO(mmenke): Consider running these synchronously?
2483 for (CookieChangedHookMap::iterator it = hook_map_.begin(); 2111 for (CookieChangedHookMap::iterator it = hook_map_.begin();
2484 it != hook_map_.end(); ++it) { 2112 it != hook_map_.end(); ++it) {
2485 std::pair<GURL, std::string> key = it->first; 2113 std::pair<GURL, std::string> key = it->first;
2486 if (cookie.IncludeForRequestURL(key.first, opts) && 2114 if (cookie.IncludeForRequestURL(key.first, opts) &&
2487 cookie.Name() == key.second) { 2115 cookie.Name() == key.second) {
2488 it->second->Notify(cookie, cause); 2116 it->second->Notify(cookie, cause);
2489 } 2117 }
2490 } 2118 }
2491 } 2119 }
2492 2120
2493 } // namespace net 2121 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698