OLD | NEW |
---|---|
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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 | 76 |
77 // In steady state, most cookie requests can be satisfied by the in memory | 77 // In steady state, most cookie requests can be satisfied by the in memory |
78 // cookie monster store. If the cookie request cannot be satisfied by the in | 78 // cookie monster store. If the cookie request cannot be satisfied by the in |
79 // memory store, the relevant cookies must be fetched from the persistent | 79 // memory store, the relevant cookies must be fetched from the persistent |
80 // store. The task is queued in CookieMonster::tasks_pending_ if it requires | 80 // store. The task is queued in CookieMonster::tasks_pending_ if it requires |
81 // all cookies to be loaded from the backend, or tasks_pending_for_key_ if it | 81 // all cookies to be loaded from the backend, or tasks_pending_for_key_ if it |
82 // only requires all cookies associated with an eTLD+1. | 82 // only requires all cookies associated with an eTLD+1. |
83 // | 83 // |
84 // On the browser critical paths (e.g. for loading initial web pages in a | 84 // On the browser critical paths (e.g. for loading initial web pages in a |
85 // session restore) it may take too long to wait for the full load. If a cookie | 85 // session restore) it may take too long to wait for the full load. If a cookie |
86 // request is for a specific URL, DoCookieTaskForURL is called, which triggers a | 86 // request is for a specific URL, DoCookieCallbackForURL is called, which |
87 // priority load if the key is not loaded yet by calling PersistentCookieStore | 87 // triggers a priority load if the key is not loaded yet by calling |
88 // :: LoadCookiesForKey. The request is queued in | 88 // PersistentCookieStore::LoadCookiesForKey. The request is queued in |
89 // CookieMonster::tasks_pending_for_key_ and executed upon receiving | 89 // CookieMonster::tasks_pending_for_key_ and executed upon receiving |
90 // notification of key load completion via CookieMonster::OnKeyLoaded(). If | 90 // notification of key load completion via CookieMonster::OnKeyLoaded(). If |
91 // multiple requests for the same eTLD+1 are received before key load | 91 // multiple requests for the same eTLD+1 are received before key load |
92 // completion, only the first request calls | 92 // completion, only the first request calls |
93 // PersistentCookieStore::LoadCookiesForKey, all subsequent requests are queued | 93 // PersistentCookieStore::LoadCookiesForKey, all subsequent requests are queued |
94 // in CookieMonster::tasks_pending_for_key_ and executed upon receiving | 94 // in CookieMonster::tasks_pending_for_key_ and executed upon receiving |
95 // notification of key load completion triggered by the first request for the | 95 // notification of key load completion triggered by the first request for the |
96 // same eTLD+1. | 96 // same eTLD+1. |
97 | 97 |
98 static const int kMinutesInTenYears = 10 * 365 * 24 * 60; | 98 static const int kMinutesInTenYears = 10 * 365 * 24 * 60; |
99 | 99 |
100 namespace { | 100 namespace { |
101 | 101 |
102 const char kFetchWhenNecessaryName[] = "FetchWhenNecessary"; | 102 const char kFetchWhenNecessaryName[] = "FetchWhenNecessary"; |
103 const char kAlwaysFetchName[] = "AlwaysFetch"; | 103 const char kAlwaysFetchName[] = "AlwaysFetch"; |
104 const char kCookieMonsterFetchStrategyName[] = "CookieMonsterFetchStrategy"; | 104 const char kCookieMonsterFetchStrategyName[] = "CookieMonsterFetchStrategy"; |
105 | 105 |
106 void WrapGetCookieListCallback( | |
107 base::OnceCallback<net::CookieList()> function, | |
108 base::OnceCallback<void(const net::CookieList&)> response) { | |
109 std::move(response).Run(std::move(function).Run()); | |
mmenke
2017/07/07 16:54:58
The old code null checked all of these callbacks.
Randy Smith (Not in Mondays)
2017/07/08 15:02:08
Yeah, it should; in fact, try jobs fail if it does
| |
110 } | |
111 | |
112 void WrapSetCookiesCallback(base::OnceCallback<bool()> function, | |
113 base::OnceCallback<void(bool)> response) { | |
114 std::move(response).Run(std::move(function).Run()); | |
115 } | |
116 | |
117 void WrapGetCookiesCallback( | |
118 base::OnceCallback<std::string()> function, | |
119 base::OnceCallback<void(const std::string&)> response) { | |
120 std::move(response).Run(std::move(function).Run()); | |
121 } | |
122 | |
123 void ConditionalDeleteCallback(base::WeakPtr<net::CookieMonster> cookie_monster, | |
124 base::OnceClosure callback) { | |
125 if (cookie_monster) | |
126 std::move(callback).Run(); | |
127 } | |
128 | |
129 void WrapDeleteCallback(base::WeakPtr<net::CookieMonster> cookie_monster, | |
130 base::OnceCallback<uint32_t()> function, | |
131 base::OnceCallback<void(uint32_t)> response) { | |
132 uint32_t num_deleted = std::move(function).Run(); | |
133 | |
134 // This funciton is always called directly from a CookieMonster routine, | |
135 // so unconditional use of the weak pointer here is safe. | |
136 cookie_monster->FlushStore( | |
137 base::BindOnce(&ConditionalDeleteCallback, cookie_monster, | |
138 base::BindOnce(std::move(response), num_deleted))); | |
139 } | |
140 | |
141 void WrapDeleteCookieCallback(base::WeakPtr<net::CookieMonster> cookie_monster, | |
142 base::OnceCallback<void()> function, | |
143 base::OnceCallback<void()> response) { | |
144 std::move(function).Run(); | |
145 | |
146 // This funciton is always called directly from a CookieMonster routine, | |
147 // so unconditional use of the weak pointer here is safe. | |
148 cookie_monster->FlushStore(base::BindOnce( | |
149 &ConditionalDeleteCallback, cookie_monster, std::move(response))); | |
150 } | |
151 | |
106 } // namespace | 152 } // namespace |
107 | 153 |
108 namespace net { | 154 namespace net { |
109 | 155 |
110 // See comments at declaration of these variables in cookie_monster.h | 156 // See comments at declaration of these variables in cookie_monster.h |
111 // for details. | 157 // for details. |
112 const size_t CookieMonster::kDomainMaxCookies = 180; | 158 const size_t CookieMonster::kDomainMaxCookies = 180; |
113 const size_t CookieMonster::kDomainPurgeCookies = 30; | 159 const size_t CookieMonster::kDomainPurgeCookies = 30; |
114 const size_t CookieMonster::kMaxCookies = 3300; | 160 const size_t CookieMonster::kMaxCookies = 3300; |
115 const size_t CookieMonster::kPurgeCookies = 300; | 161 const size_t CookieMonster::kPurgeCookies = 300; |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
379 channel_id_service_(channel_id_service), | 425 channel_id_service_(channel_id_service), |
380 last_statistic_record_time_(base::Time::Now()), | 426 last_statistic_record_time_(base::Time::Now()), |
381 persist_session_cookies_(false), | 427 persist_session_cookies_(false), |
382 weak_ptr_factory_(this) { | 428 weak_ptr_factory_(this) { |
383 InitializeHistograms(); | 429 InitializeHistograms(); |
384 cookieable_schemes_.insert( | 430 cookieable_schemes_.insert( |
385 cookieable_schemes_.begin(), kDefaultCookieableSchemes, | 431 cookieable_schemes_.begin(), kDefaultCookieableSchemes, |
386 kDefaultCookieableSchemes + kDefaultCookieableSchemesCount); | 432 kDefaultCookieableSchemes + kDefaultCookieableSchemesCount); |
387 } | 433 } |
388 | 434 |
389 // Task classes for queueing the coming request. | |
390 | |
391 class CookieMonster::CookieMonsterTask | |
392 : public base::RefCountedThreadSafe<CookieMonsterTask> { | |
393 public: | |
394 // Runs the task and invokes the client callback on the thread that | |
395 // originally constructed the task. | |
396 virtual void Run() = 0; | |
397 | |
398 protected: | |
399 explicit CookieMonsterTask(CookieMonster* cookie_monster); | |
400 virtual ~CookieMonsterTask(); | |
401 | |
402 CookieMonster* cookie_monster() { return cookie_monster_; } | |
403 | |
404 private: | |
405 friend class base::RefCountedThreadSafe<CookieMonsterTask>; | |
406 | |
407 CookieMonster* cookie_monster_; | |
408 | |
409 DISALLOW_COPY_AND_ASSIGN(CookieMonsterTask); | |
410 }; | |
411 | |
412 CookieMonster::CookieMonsterTask::CookieMonsterTask( | |
413 CookieMonster* cookie_monster) | |
414 : cookie_monster_(cookie_monster) {} | |
415 | |
416 CookieMonster::CookieMonsterTask::~CookieMonsterTask() { | |
417 } | |
418 | |
419 // Task class for SetCookieWithDetails call. | |
420 class CookieMonster::SetCookieWithDetailsTask : public CookieMonsterTask { | |
421 public: | |
422 SetCookieWithDetailsTask(CookieMonster* cookie_monster, | |
423 const GURL& url, | |
424 const std::string& name, | |
425 const std::string& value, | |
426 const std::string& domain, | |
427 const std::string& path, | |
428 base::Time creation_time, | |
429 base::Time expiration_time, | |
430 base::Time last_access_time, | |
431 bool secure, | |
432 bool http_only, | |
433 CookieSameSite same_site, | |
434 CookiePriority priority, | |
435 SetCookiesCallback callback) | |
436 : CookieMonsterTask(cookie_monster), | |
437 url_(url), | |
438 name_(name), | |
439 value_(value), | |
440 domain_(domain), | |
441 path_(path), | |
442 creation_time_(creation_time), | |
443 expiration_time_(expiration_time), | |
444 last_access_time_(last_access_time), | |
445 secure_(secure), | |
446 http_only_(http_only), | |
447 same_site_(same_site), | |
448 priority_(priority), | |
449 callback_(std::move(callback)) {} | |
450 | |
451 // CookieMonsterTask: | |
452 void Run() override; | |
453 | |
454 protected: | |
455 ~SetCookieWithDetailsTask() override {} | |
456 | |
457 private: | |
458 GURL url_; | |
459 std::string name_; | |
460 std::string value_; | |
461 std::string domain_; | |
462 std::string path_; | |
463 base::Time creation_time_; | |
464 base::Time expiration_time_; | |
465 base::Time last_access_time_; | |
466 bool secure_; | |
467 bool http_only_; | |
468 CookieSameSite same_site_; | |
469 CookiePriority priority_; | |
470 SetCookiesCallback callback_; | |
471 | |
472 DISALLOW_COPY_AND_ASSIGN(SetCookieWithDetailsTask); | |
473 }; | |
474 | |
475 void CookieMonster::SetCookieWithDetailsTask::Run() { | |
476 bool success = this->cookie_monster()->SetCookieWithDetails( | |
477 url_, name_, value_, domain_, path_, creation_time_, expiration_time_, | |
478 last_access_time_, secure_, http_only_, same_site_, priority_); | |
479 if (!callback_.is_null()) | |
480 std::move(callback_).Run(success); | |
481 } | |
482 | |
483 // Task class for GetAllCookies call. | |
484 class CookieMonster::GetAllCookiesTask : public CookieMonsterTask { | |
485 public: | |
486 GetAllCookiesTask(CookieMonster* cookie_monster, | |
487 GetCookieListCallback callback) | |
488 : CookieMonsterTask(cookie_monster), callback_(std::move(callback)) {} | |
489 | |
490 // CookieMonsterTask | |
491 void Run() override; | |
492 | |
493 protected: | |
494 ~GetAllCookiesTask() override {} | |
495 | |
496 private: | |
497 GetCookieListCallback callback_; | |
498 | |
499 DISALLOW_COPY_AND_ASSIGN(GetAllCookiesTask); | |
500 }; | |
501 | |
502 void CookieMonster::GetAllCookiesTask::Run() { | |
503 if (!callback_.is_null()) { | |
504 CookieList cookies = this->cookie_monster()->GetAllCookies(); | |
505 std::move(callback_).Run(cookies); | |
506 } | |
507 } | |
508 | |
509 // Task class for GetCookieListWithOptionsAsync call. | |
510 class CookieMonster::GetCookieListWithOptionsTask : public CookieMonsterTask { | |
511 public: | |
512 GetCookieListWithOptionsTask(CookieMonster* cookie_monster, | |
513 const GURL& url, | |
514 const CookieOptions& options, | |
515 GetCookieListCallback callback) | |
516 : CookieMonsterTask(cookie_monster), | |
517 url_(url), | |
518 options_(options), | |
519 callback_(std::move(callback)) {} | |
520 | |
521 // CookieMonsterTask: | |
522 void Run() override; | |
523 | |
524 protected: | |
525 ~GetCookieListWithOptionsTask() override {} | |
526 | |
527 private: | |
528 GURL url_; | |
529 CookieOptions options_; | |
530 GetCookieListCallback callback_; | |
531 | |
532 DISALLOW_COPY_AND_ASSIGN(GetCookieListWithOptionsTask); | |
533 }; | |
534 | |
535 void CookieMonster::GetCookieListWithOptionsTask::Run() { | |
536 if (!callback_.is_null()) { | |
537 CookieList cookies = | |
538 this->cookie_monster()->GetCookieListWithOptions(url_, options_); | |
539 std::move(callback_).Run(cookies); | |
540 } | |
541 } | |
542 | |
543 template <typename Result> | |
544 struct CallbackType { | |
545 typedef base::OnceCallback<void(Result)> Type; | |
546 }; | |
547 | |
548 template <> | |
549 struct CallbackType<void> { | |
550 typedef base::OnceClosure Type; | |
551 }; | |
552 | |
553 // Base task class for Delete*Task. | |
554 template <typename Result> | |
555 class CookieMonster::DeleteTask : public CookieMonsterTask { | |
556 public: | |
557 DeleteTask(CookieMonster* cookie_monster, | |
558 typename CallbackType<Result>::Type callback) | |
559 : CookieMonsterTask(cookie_monster), callback_(std::move(callback)) {} | |
560 | |
561 // CookieMonsterTask: | |
562 void Run() override; | |
563 | |
564 protected: | |
565 ~DeleteTask() override; | |
566 | |
567 private: | |
568 // Runs the delete task and returns a result. | |
569 virtual Result RunDeleteTask() = 0; | |
570 // Runs the delete task and then returns a callback to be called after | |
571 // flushing the persistent store. | |
572 // TODO(mmenke): This seems like a pretty ugly and needlessly confusing API. | |
573 // Simplify it? | |
574 base::OnceClosure RunDeleteTaskAndBindCallback(); | |
575 | |
576 typename CallbackType<Result>::Type callback_; | |
577 | |
578 DISALLOW_COPY_AND_ASSIGN(DeleteTask); | |
579 }; | |
580 | |
581 template <typename Result> | |
582 CookieMonster::DeleteTask<Result>::~DeleteTask() { | |
583 } | |
584 | |
585 template <typename Result> | |
586 base::OnceClosure | |
587 CookieMonster::DeleteTask<Result>::RunDeleteTaskAndBindCallback() { | |
588 Result result = RunDeleteTask(); | |
589 if (callback_.is_null()) | |
590 return base::OnceClosure(); | |
591 return base::BindOnce(std::move(callback_), result); | |
592 } | |
593 | |
594 template <> | |
595 base::OnceClosure | |
596 CookieMonster::DeleteTask<void>::RunDeleteTaskAndBindCallback() { | |
597 RunDeleteTask(); | |
598 return std::move(callback_); | |
599 } | |
600 | |
601 template <typename Result> | |
602 void CookieMonster::DeleteTask<Result>::Run() { | |
603 base::OnceClosure callback = RunDeleteTaskAndBindCallback(); | |
604 if (!callback.is_null()) { | |
605 callback = | |
606 base::BindOnce(&CookieMonster::RunCallback, | |
607 this->cookie_monster()->weak_ptr_factory_.GetWeakPtr(), | |
608 std::move(callback)); | |
609 } | |
610 this->cookie_monster()->FlushStore(std::move(callback)); | |
611 } | |
612 | |
613 // Task class for DeleteAllCreatedBetween call. | |
614 class CookieMonster::DeleteAllCreatedBetweenTask : public DeleteTask<uint32_t> { | |
615 public: | |
616 DeleteAllCreatedBetweenTask(CookieMonster* cookie_monster, | |
617 const Time& delete_begin, | |
618 const Time& delete_end, | |
619 DeleteCallback callback) | |
620 : DeleteTask<uint32_t>(cookie_monster, std::move(callback)), | |
621 delete_begin_(delete_begin), | |
622 delete_end_(delete_end) {} | |
623 | |
624 // DeleteTask: | |
625 uint32_t RunDeleteTask() override; | |
626 | |
627 protected: | |
628 ~DeleteAllCreatedBetweenTask() override {} | |
629 | |
630 private: | |
631 Time delete_begin_; | |
632 Time delete_end_; | |
633 | |
634 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenTask); | |
635 }; | |
636 | |
637 uint32_t CookieMonster::DeleteAllCreatedBetweenTask::RunDeleteTask() { | |
638 return this->cookie_monster()->DeleteAllCreatedBetween(delete_begin_, | |
639 delete_end_); | |
640 } | |
641 | |
642 // Task class for DeleteAllCreatedBetweenWithPredicate call. | |
643 class CookieMonster::DeleteAllCreatedBetweenWithPredicateTask | |
644 : public DeleteTask<uint32_t> { | |
645 public: | |
646 DeleteAllCreatedBetweenWithPredicateTask( | |
647 CookieMonster* cookie_monster, | |
648 Time delete_begin, | |
649 Time delete_end, | |
650 base::Callback<bool(const CanonicalCookie&)> predicate, | |
651 DeleteCallback callback) | |
652 : DeleteTask<uint32_t>(cookie_monster, std::move(callback)), | |
653 delete_begin_(delete_begin), | |
654 delete_end_(delete_end), | |
655 predicate_(predicate) {} | |
656 | |
657 // DeleteTask: | |
658 uint32_t RunDeleteTask() override; | |
659 | |
660 protected: | |
661 ~DeleteAllCreatedBetweenWithPredicateTask() override {} | |
662 | |
663 private: | |
664 Time delete_begin_; | |
665 Time delete_end_; | |
666 base::Callback<bool(const CanonicalCookie&)> predicate_; | |
667 | |
668 DISALLOW_COPY_AND_ASSIGN(DeleteAllCreatedBetweenWithPredicateTask); | |
669 }; | |
670 | |
671 uint32_t | |
672 CookieMonster::DeleteAllCreatedBetweenWithPredicateTask::RunDeleteTask() { | |
673 return this->cookie_monster()->DeleteAllCreatedBetweenWithPredicate( | |
674 delete_begin_, delete_end_, predicate_); | |
675 } | |
676 | |
677 // Task class for DeleteCanonicalCookie call. | |
678 class CookieMonster::DeleteCanonicalCookieTask : public DeleteTask<uint32_t> { | |
679 public: | |
680 DeleteCanonicalCookieTask(CookieMonster* cookie_monster, | |
681 const CanonicalCookie& cookie, | |
682 DeleteCallback callback) | |
683 : DeleteTask<uint32_t>(cookie_monster, std::move(callback)), | |
684 cookie_(cookie) {} | |
685 | |
686 // DeleteTask: | |
687 uint32_t RunDeleteTask() override; | |
688 | |
689 protected: | |
690 ~DeleteCanonicalCookieTask() override {} | |
691 | |
692 private: | |
693 CanonicalCookie cookie_; | |
694 | |
695 DISALLOW_COPY_AND_ASSIGN(DeleteCanonicalCookieTask); | |
696 }; | |
697 | |
698 uint32_t CookieMonster::DeleteCanonicalCookieTask::RunDeleteTask() { | |
699 return this->cookie_monster()->DeleteCanonicalCookie(cookie_); | |
700 } | |
701 | |
702 // Task class for SetCanonicalCookie call. | |
703 class CookieMonster::SetCanonicalCookieTask : public CookieMonsterTask { | |
704 public: | |
705 SetCanonicalCookieTask(CookieMonster* cookie_monster, | |
706 std::unique_ptr<CanonicalCookie> cookie, | |
707 bool secure_source, | |
708 bool modify_http_only, | |
709 SetCookiesCallback callback) | |
710 : CookieMonsterTask(cookie_monster), | |
711 cookie_(std::move(cookie)), | |
712 secure_source_(secure_source), | |
713 modify_http_only_(modify_http_only), | |
714 callback_(std::move(callback)) {} | |
715 | |
716 // CookieMonsterTask: | |
717 void Run() override; | |
718 | |
719 protected: | |
720 ~SetCanonicalCookieTask() override {} | |
721 | |
722 private: | |
723 std::unique_ptr<CanonicalCookie> cookie_; | |
724 bool secure_source_; | |
725 bool modify_http_only_; | |
726 SetCookiesCallback callback_; | |
727 | |
728 DISALLOW_COPY_AND_ASSIGN(SetCanonicalCookieTask); | |
729 }; | |
730 | |
731 void CookieMonster::SetCanonicalCookieTask::Run() { | |
732 bool result = this->cookie_monster()->SetCanonicalCookie( | |
733 std::move(cookie_), secure_source_, modify_http_only_); | |
734 if (!callback_.is_null()) | |
735 std::move(callback_).Run(result); | |
736 } | |
737 | |
738 // Task class for SetCookieWithOptions call. | |
739 class CookieMonster::SetCookieWithOptionsTask : public CookieMonsterTask { | |
740 public: | |
741 SetCookieWithOptionsTask(CookieMonster* cookie_monster, | |
742 const GURL& url, | |
743 const std::string& cookie_line, | |
744 const CookieOptions& options, | |
745 SetCookiesCallback callback) | |
746 : CookieMonsterTask(cookie_monster), | |
747 url_(url), | |
748 cookie_line_(cookie_line), | |
749 options_(options), | |
750 callback_(std::move(callback)) {} | |
751 | |
752 // CookieMonsterTask: | |
753 void Run() override; | |
754 | |
755 protected: | |
756 ~SetCookieWithOptionsTask() override {} | |
757 | |
758 private: | |
759 GURL url_; | |
760 std::string cookie_line_; | |
761 CookieOptions options_; | |
762 SetCookiesCallback callback_; | |
763 | |
764 DISALLOW_COPY_AND_ASSIGN(SetCookieWithOptionsTask); | |
765 }; | |
766 | |
767 void CookieMonster::SetCookieWithOptionsTask::Run() { | |
768 bool result = this->cookie_monster()->SetCookieWithOptions(url_, cookie_line_, | |
769 options_); | |
770 if (!callback_.is_null()) | |
771 std::move(callback_).Run(result); | |
772 } | |
773 | |
774 // Task class for SetAllCookies call. | |
775 class CookieMonster::SetAllCookiesTask : public CookieMonsterTask { | |
776 public: | |
777 SetAllCookiesTask(CookieMonster* cookie_monster, | |
778 const CookieList& list, | |
779 SetCookiesCallback callback) | |
780 : CookieMonsterTask(cookie_monster), | |
781 list_(list), | |
782 callback_(std::move(callback)) {} | |
783 | |
784 // CookieMonsterTask: | |
785 void Run() override; | |
786 | |
787 protected: | |
788 ~SetAllCookiesTask() override {} | |
789 | |
790 private: | |
791 CookieList list_; | |
792 SetCookiesCallback callback_; | |
793 | |
794 DISALLOW_COPY_AND_ASSIGN(SetAllCookiesTask); | |
795 }; | |
796 | |
797 void CookieMonster::SetAllCookiesTask::Run() { | |
Randy Smith (Not in Mondays)
2017/07/07 20:09:54
Location of the code I refer to in the other comme
| |
798 CookieList positive_diff; | |
799 CookieList negative_diff; | |
800 CookieList old_cookies = this->cookie_monster()->GetAllCookies(); | |
801 this->cookie_monster()->ComputeCookieDiff(&old_cookies, &list_, | |
802 &positive_diff, &negative_diff); | |
803 | |
804 for (CookieList::const_iterator it = negative_diff.begin(); | |
805 it != negative_diff.end(); ++it) { | |
806 this->cookie_monster()->DeleteCanonicalCookie(*it); | |
807 } | |
808 | |
809 bool result = true; | |
810 if (positive_diff.size() > 0) | |
811 result = this->cookie_monster()->SetAllCookies(list_); | |
812 | |
813 if (!callback_.is_null()) | |
814 std::move(callback_).Run(result); | |
815 } | |
816 | |
817 // Task class for GetCookiesWithOptions call. | |
818 class CookieMonster::GetCookiesWithOptionsTask : public CookieMonsterTask { | |
819 public: | |
820 GetCookiesWithOptionsTask(CookieMonster* cookie_monster, | |
821 const GURL& url, | |
822 const CookieOptions& options, | |
823 GetCookiesCallback callback) | |
824 : CookieMonsterTask(cookie_monster), | |
825 url_(url), | |
826 options_(options), | |
827 callback_(std::move(callback)) {} | |
828 | |
829 // CookieMonsterTask: | |
830 void Run() override; | |
831 | |
832 protected: | |
833 ~GetCookiesWithOptionsTask() override {} | |
834 | |
835 private: | |
836 GURL url_; | |
837 CookieOptions options_; | |
838 GetCookiesCallback callback_; | |
839 | |
840 DISALLOW_COPY_AND_ASSIGN(GetCookiesWithOptionsTask); | |
841 }; | |
842 | |
843 void CookieMonster::GetCookiesWithOptionsTask::Run() { | |
844 std::string cookie = | |
845 this->cookie_monster()->GetCookiesWithOptions(url_, options_); | |
846 if (!callback_.is_null()) | |
847 std::move(callback_).Run(cookie); | |
848 } | |
849 | |
850 // Task class for DeleteCookie call. | |
851 class CookieMonster::DeleteCookieTask : public DeleteTask<void> { | |
852 public: | |
853 DeleteCookieTask(CookieMonster* cookie_monster, | |
854 const GURL& url, | |
855 const std::string& cookie_name, | |
856 base::OnceClosure callback) | |
857 : DeleteTask<void>(cookie_monster, std::move(callback)), | |
858 url_(url), | |
859 cookie_name_(cookie_name) {} | |
860 | |
861 // DeleteTask: | |
862 void RunDeleteTask() override; | |
863 | |
864 protected: | |
865 ~DeleteCookieTask() override {} | |
866 | |
867 private: | |
868 GURL url_; | |
869 std::string cookie_name_; | |
870 | |
871 DISALLOW_COPY_AND_ASSIGN(DeleteCookieTask); | |
872 }; | |
873 | |
874 void CookieMonster::DeleteCookieTask::RunDeleteTask() { | |
875 this->cookie_monster()->DeleteCookie(url_, cookie_name_); | |
876 } | |
877 | |
878 // Task class for DeleteSessionCookies call. | |
879 class CookieMonster::DeleteSessionCookiesTask : public DeleteTask<uint32_t> { | |
880 public: | |
881 DeleteSessionCookiesTask(CookieMonster* cookie_monster, | |
882 DeleteCallback callback) | |
883 : DeleteTask<uint32_t>(cookie_monster, std::move(callback)) {} | |
884 | |
885 // DeleteTask: | |
886 uint32_t RunDeleteTask() override; | |
887 | |
888 protected: | |
889 ~DeleteSessionCookiesTask() override {} | |
890 | |
891 private: | |
892 DISALLOW_COPY_AND_ASSIGN(DeleteSessionCookiesTask); | |
893 }; | |
894 | |
895 uint32_t CookieMonster::DeleteSessionCookiesTask::RunDeleteTask() { | |
896 return this->cookie_monster()->DeleteSessionCookies(); | |
897 } | |
898 | |
899 // Asynchronous CookieMonster API | 435 // Asynchronous CookieMonster API |
900 | 436 |
901 void CookieMonster::SetCookieWithDetailsAsync(const GURL& url, | 437 void CookieMonster::SetCookieWithDetailsAsync(const GURL& url, |
902 const std::string& name, | 438 const std::string& name, |
903 const std::string& value, | 439 const std::string& value, |
904 const std::string& domain, | 440 const std::string& domain, |
905 const std::string& path, | 441 const std::string& path, |
906 Time creation_time, | 442 Time creation_time, |
907 Time expiration_time, | 443 Time expiration_time, |
908 Time last_access_time, | 444 Time last_access_time, |
909 bool secure, | 445 bool secure, |
910 bool http_only, | 446 bool http_only, |
911 CookieSameSite same_site, | 447 CookieSameSite same_site, |
912 CookiePriority priority, | 448 CookiePriority priority, |
913 SetCookiesCallback callback) { | 449 SetCookiesCallback callback) { |
914 scoped_refptr<SetCookieWithDetailsTask> task = new SetCookieWithDetailsTask( | 450 DoCookieCallbackForURL( |
915 this, url, name, value, domain, path, creation_time, expiration_time, | 451 base::BindOnce( |
mmenke
2017/07/07 16:54:58
Not suggesting it in this CL, but wonder if we cou
Randy Smith (Not in Mondays)
2017/07/08 15:02:08
I need a word for "Yeah, but if I go in that direc
mmenke
2017/07/10 16:27:28
I'm not going to twist your arm on this.
Note tha
mmenke
2017/07/10 16:39:49
Also note that in one of the delete case, we have
Randy Smith (Not in Mondays)
2017/07/11 15:47:07
Huh. Good point.
Hold off on doing another rev
mmenke
2017/07/11 15:50:54
Another option is to land this, and then do anothe
| |
916 last_access_time, secure, http_only, same_site, priority, | 452 &WrapSetCookiesCallback, |
917 std::move(callback)); | 453 base::BindOnce( |
918 DoCookieTaskForURL(task, url); | 454 // base::Unretained is safe as DoCookieCallbackForURL stores |
455 // the callback on |*this|, so the callback will not outlive | |
456 // the object. | |
457 &CookieMonster::SetCookieWithDetails, base::Unretained(this), url, | |
458 name, value, domain, path, creation_time, expiration_time, | |
459 last_access_time, secure, http_only, same_site, priority), | |
460 std::move(callback)), | |
461 url); | |
919 } | 462 } |
920 | 463 |
921 void CookieMonster::FlushStore(base::OnceClosure callback) { | 464 void CookieMonster::FlushStore(base::OnceClosure callback) { |
922 DCHECK(thread_checker_.CalledOnValidThread()); | 465 DCHECK(thread_checker_.CalledOnValidThread()); |
923 | 466 |
924 if (initialized_ && store_.get()) { | 467 if (initialized_ && store_.get()) { |
925 if (channel_id_service_) { | 468 if (channel_id_service_) { |
926 channel_id_service_->GetChannelIDStore()->Flush(); | 469 channel_id_service_->GetChannelIDStore()->Flush(); |
927 } | 470 } |
928 store_->Flush(std::move(callback)); | 471 store_->Flush(std::move(callback)); |
929 } else if (!callback.is_null()) { | 472 } else if (!callback.is_null()) { |
930 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 473 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
931 std::move(callback)); | 474 std::move(callback)); |
932 } | 475 } |
933 } | 476 } |
934 | 477 |
935 void CookieMonster::SetForceKeepSessionState() { | 478 void CookieMonster::SetForceKeepSessionState() { |
936 DCHECK(thread_checker_.CalledOnValidThread()); | 479 DCHECK(thread_checker_.CalledOnValidThread()); |
937 | 480 |
938 if (store_) | 481 if (store_) |
939 store_->SetForceKeepSessionState(); | 482 store_->SetForceKeepSessionState(); |
940 } | 483 } |
941 | 484 |
942 void CookieMonster::SetAllCookiesAsync(const CookieList& list, | 485 void CookieMonster::SetAllCookiesAsync(const CookieList& list, |
943 SetCookiesCallback callback) { | 486 SetCookiesCallback callback) { |
944 scoped_refptr<SetAllCookiesTask> task = | 487 DoCookieCallback(base::BindOnce( |
945 new SetAllCookiesTask(this, list, std::move(callback)); | 488 &WrapSetCookiesCallback, |
946 DoCookieTask(task); | 489 base::BindOnce( |
490 // base::Unretained is safe as DoCookieCallbackForURL stores | |
491 // the callback on |*this|, so the callback will not outlive | |
492 // the object. | |
493 &CookieMonster::SetAllCookies, base::Unretained(this), list), | |
494 std::move(callback))); | |
947 } | 495 } |
948 | 496 |
949 void CookieMonster::SetCanonicalCookieAsync( | 497 void CookieMonster::SetCanonicalCookieAsync( |
950 std::unique_ptr<CanonicalCookie> cookie, | 498 std::unique_ptr<CanonicalCookie> cookie, |
951 bool secure_source, | 499 bool secure_source, |
952 bool modify_http_only, | 500 bool modify_http_only, |
953 SetCookiesCallback callback) { | 501 SetCookiesCallback callback) { |
954 DCHECK(cookie->IsCanonical()); | 502 DCHECK(cookie->IsCanonical()); |
955 scoped_refptr<SetCanonicalCookieTask> task = | |
956 new SetCanonicalCookieTask(this, std::move(cookie), secure_source, | |
957 modify_http_only, std::move(callback)); | |
958 | 503 |
959 // TODO(rdsmith): Switch to DoCookieTaskForURL (or the equivalent). | 504 // TODO(rdsmith): Switch to DoCookieCallbackForURL (or the equivalent). |
960 // This is tricky because we don't have the scheme in this routine | 505 // This is tricky because we don't have the scheme in this routine |
961 // and DoCookieTaskForURL uses cookie_util::GetEffectiveDomain(scheme, host) | 506 // and DoCookieCallbackForURL uses |
507 // cookie_util::GetEffectiveDomain(scheme, host) | |
962 // to generate the database key to block behind. | 508 // to generate the database key to block behind. |
963 DoCookieTask(task); | 509 DoCookieCallback(base::BindOnce( |
510 &WrapSetCookiesCallback, | |
511 base::BindOnce( | |
512 // base::Unretained is safe as DoCookieCallbackForURL stores | |
513 // the callback on |*this|, so the callback will not outlive | |
514 // the object. | |
515 &CookieMonster::SetCanonicalCookie, base::Unretained(this), | |
516 std::move(cookie), secure_source, modify_http_only), | |
517 std::move(callback))); | |
964 } | 518 } |
965 | 519 |
966 void CookieMonster::SetCookieWithOptionsAsync(const GURL& url, | 520 void CookieMonster::SetCookieWithOptionsAsync(const GURL& url, |
967 const std::string& cookie_line, | 521 const std::string& cookie_line, |
968 const CookieOptions& options, | 522 const CookieOptions& options, |
969 SetCookiesCallback callback) { | 523 SetCookiesCallback callback) { |
970 scoped_refptr<SetCookieWithOptionsTask> task = new SetCookieWithOptionsTask( | 524 DoCookieCallbackForURL( |
971 this, url, cookie_line, options, std::move(callback)); | 525 base::BindOnce( |
972 | 526 &WrapSetCookiesCallback, |
973 DoCookieTaskForURL(task, url); | 527 base::BindOnce( |
528 // base::Unretained is safe as DoCookieCallbackForURL stores | |
529 // the callback on |*this|, so the callback will not outlive | |
530 // the object. | |
531 &CookieMonster::SetCookieWithOptions, base::Unretained(this), url, | |
532 cookie_line, options), | |
533 std::move(callback)), | |
534 url); | |
974 } | 535 } |
975 | 536 |
976 void CookieMonster::GetCookiesWithOptionsAsync(const GURL& url, | 537 void CookieMonster::GetCookiesWithOptionsAsync(const GURL& url, |
977 const CookieOptions& options, | 538 const CookieOptions& options, |
978 GetCookiesCallback callback) { | 539 GetCookiesCallback callback) { |
979 scoped_refptr<GetCookiesWithOptionsTask> task = | 540 DoCookieCallbackForURL( |
980 new GetCookiesWithOptionsTask(this, url, options, std::move(callback)); | 541 base::BindOnce( |
981 | 542 &WrapGetCookiesCallback, |
982 DoCookieTaskForURL(task, url); | 543 base::BindOnce( |
544 // base::Unretained is safe as DoCookieCallbackForURL stores | |
545 // the callback on |*this|, so the callback will not outlive | |
546 // the object. | |
547 &CookieMonster::GetCookiesWithOptions, base::Unretained(this), | |
548 url, options), | |
549 std::move(callback)), | |
550 url); | |
983 } | 551 } |
984 | 552 |
985 void CookieMonster::GetCookieListWithOptionsAsync( | 553 void CookieMonster::GetCookieListWithOptionsAsync( |
986 const GURL& url, | 554 const GURL& url, |
987 const CookieOptions& options, | 555 const CookieOptions& options, |
988 GetCookieListCallback callback) { | 556 GetCookieListCallback callback) { |
989 scoped_refptr<GetCookieListWithOptionsTask> task = | 557 DoCookieCallbackForURL( |
990 new GetCookieListWithOptionsTask(this, url, options, std::move(callback)); | 558 base::BindOnce( |
991 | 559 &WrapGetCookieListCallback, |
992 DoCookieTaskForURL(task, url); | 560 base::BindOnce( |
561 // base::Unretained is safe as DoCookieCallbackForURL stores | |
562 // the callback on |*this|, so the callback will not outlive | |
563 // the object. | |
564 &CookieMonster::GetCookieListWithOptions, base::Unretained(this), | |
565 url, options), | |
566 std::move(callback)), | |
567 url); | |
993 } | 568 } |
994 | 569 |
995 void CookieMonster::GetAllCookiesAsync(GetCookieListCallback callback) { | 570 void CookieMonster::GetAllCookiesAsync(GetCookieListCallback callback) { |
996 scoped_refptr<GetAllCookiesTask> task = | 571 DoCookieCallback(base::BindOnce( |
997 new GetAllCookiesTask(this, std::move(callback)); | 572 &WrapGetCookieListCallback, |
998 | 573 base::BindOnce( |
999 DoCookieTask(task); | 574 // base::Unretained is safe as DoCookieCallbackForURL stores |
575 // the callback on |*this|, so the callback will not outlive | |
576 // the object. | |
577 &CookieMonster::GetAllCookies, base::Unretained(this)), | |
578 std::move(callback))); | |
1000 } | 579 } |
1001 | 580 |
1002 void CookieMonster::DeleteCookieAsync(const GURL& url, | 581 void CookieMonster::DeleteCookieAsync(const GURL& url, |
1003 const std::string& cookie_name, | 582 const std::string& cookie_name, |
1004 base::OnceClosure callback) { | 583 base::OnceClosure callback) { |
1005 scoped_refptr<DeleteCookieTask> task = | 584 DoCookieCallbackForURL( |
1006 new DeleteCookieTask(this, url, cookie_name, std::move(callback)); | 585 base::BindOnce( |
1007 | 586 &WrapDeleteCookieCallback, weak_ptr_factory_.GetWeakPtr(), |
1008 DoCookieTaskForURL(task, url); | 587 base::BindOnce( |
588 // base::Unretained is safe as DoCookieCallbackForURL stores | |
589 // the callback on |*this|, so the callback will not outlive | |
590 // the object. | |
591 &CookieMonster::DeleteCookie, base::Unretained(this), url, | |
592 cookie_name), | |
593 std::move(callback)), | |
594 url); | |
1009 } | 595 } |
1010 | 596 |
1011 void CookieMonster::DeleteCanonicalCookieAsync(const CanonicalCookie& cookie, | 597 void CookieMonster::DeleteCanonicalCookieAsync(const CanonicalCookie& cookie, |
1012 DeleteCallback callback) { | 598 DeleteCallback callback) { |
1013 scoped_refptr<DeleteCanonicalCookieTask> task = | 599 DoCookieCallback(base::BindOnce( |
1014 new DeleteCanonicalCookieTask(this, cookie, std::move(callback)); | 600 &WrapDeleteCallback, weak_ptr_factory_.GetWeakPtr(), |
1015 | 601 base::BindOnce( |
1016 DoCookieTask(task); | 602 // base::Unretained is safe as DoCookieCallbackForURL stores |
603 // the callback on |*this|, so the callback will not outlive | |
604 // the object. | |
605 &CookieMonster::DeleteCanonicalCookie, base::Unretained(this), | |
606 cookie), | |
607 std::move(callback))); | |
1017 } | 608 } |
1018 | 609 |
1019 void CookieMonster::DeleteAllCreatedBetweenAsync(const Time& delete_begin, | 610 void CookieMonster::DeleteAllCreatedBetweenAsync(const Time& delete_begin, |
1020 const Time& delete_end, | 611 const Time& delete_end, |
1021 DeleteCallback callback) { | 612 DeleteCallback callback) { |
1022 scoped_refptr<DeleteAllCreatedBetweenTask> task = | 613 DoCookieCallback(base::BindOnce( |
1023 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end, | 614 &WrapDeleteCallback, weak_ptr_factory_.GetWeakPtr(), |
1024 std::move(callback)); | 615 base::BindOnce( |
1025 | 616 // base::Unretained is safe as DoCookieCallbackForURL stores |
1026 DoCookieTask(task); | 617 // the callback on |*this|, so the callback will not outlive |
618 // the object. | |
619 &CookieMonster::DeleteAllCreatedBetween, base::Unretained(this), | |
620 delete_begin, delete_end), | |
621 std::move(callback))); | |
1027 } | 622 } |
1028 | 623 |
1029 void CookieMonster::DeleteAllCreatedBetweenWithPredicateAsync( | 624 void CookieMonster::DeleteAllCreatedBetweenWithPredicateAsync( |
1030 const Time& delete_begin, | 625 const Time& delete_begin, |
1031 const Time& delete_end, | 626 const Time& delete_end, |
1032 const base::Callback<bool(const CanonicalCookie&)>& predicate, | 627 const base::Callback<bool(const CanonicalCookie&)>& predicate, |
1033 DeleteCallback callback) { | 628 DeleteCallback callback) { |
1034 if (predicate.is_null()) { | 629 if (predicate.is_null()) { |
1035 std::move(callback).Run(0); | 630 std::move(callback).Run(0); |
1036 return; | 631 return; |
1037 } | 632 } |
1038 scoped_refptr<DeleteAllCreatedBetweenWithPredicateTask> task = | 633 |
1039 new DeleteAllCreatedBetweenWithPredicateTask( | 634 DoCookieCallback(base::BindOnce( |
1040 this, delete_begin, delete_end, predicate, std::move(callback)); | 635 &WrapDeleteCallback, weak_ptr_factory_.GetWeakPtr(), |
1041 DoCookieTask(task); | 636 base::BindOnce( |
637 // base::Unretained is safe as DoCookieCallbackForURL stores | |
638 // the callback on |*this|, so the callback will not outlive | |
639 // the object. | |
640 &CookieMonster::DeleteAllCreatedBetweenWithPredicate, | |
641 base::Unretained(this), delete_begin, delete_end, predicate), | |
642 std::move(callback))); | |
1042 } | 643 } |
1043 | 644 |
1044 void CookieMonster::DeleteSessionCookiesAsync( | 645 void CookieMonster::DeleteSessionCookiesAsync( |
1045 CookieStore::DeleteCallback callback) { | 646 CookieStore::DeleteCallback callback) { |
1046 scoped_refptr<DeleteSessionCookiesTask> task = | 647 DoCookieCallback(base::BindOnce( |
1047 new DeleteSessionCookiesTask(this, std::move(callback)); | 648 &WrapDeleteCallback, weak_ptr_factory_.GetWeakPtr(), |
1048 | 649 base::BindOnce( |
1049 DoCookieTask(task); | 650 // base::Unretained is safe as DoCookieCallbackForURL stores |
651 // the callback on |*this|, so the callback will not outlive | |
652 // the object. | |
653 &CookieMonster::DeleteSessionCookies, base::Unretained(this)), | |
654 std::move(callback))); | |
1050 } | 655 } |
1051 | 656 |
1052 void CookieMonster::SetCookieableSchemes( | 657 void CookieMonster::SetCookieableSchemes( |
1053 const std::vector<std::string>& schemes) { | 658 const std::vector<std::string>& schemes) { |
1054 DCHECK(thread_checker_.CalledOnValidThread()); | 659 DCHECK(thread_checker_.CalledOnValidThread()); |
1055 | 660 |
1056 // Calls to this method will have no effect if made after a WebView or | 661 // Calls to this method will have no effect if made after a WebView or |
1057 // CookieManager instance has been created. | 662 // CookieManager instance has been created. |
1058 if (initialized_) | 663 if (initialized_) |
1059 return; | 664 return; |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1434 | 1039 |
1435 auto tasks_pending_for_key = tasks_pending_for_key_.find(key); | 1040 auto tasks_pending_for_key = tasks_pending_for_key_.find(key); |
1436 | 1041 |
1437 // TODO(mmenke): Can this be turned into a DCHECK? | 1042 // TODO(mmenke): Can this be turned into a DCHECK? |
1438 if (tasks_pending_for_key == tasks_pending_for_key_.end()) | 1043 if (tasks_pending_for_key == tasks_pending_for_key_.end()) |
1439 return; | 1044 return; |
1440 | 1045 |
1441 // Run all tasks for the key. Note that running a task can result in multiple | 1046 // Run all tasks for the key. Note that running a task can result in multiple |
1442 // tasks being added to the back of the deque. | 1047 // tasks being added to the back of the deque. |
1443 while (!tasks_pending_for_key->second.empty()) { | 1048 while (!tasks_pending_for_key->second.empty()) { |
1444 scoped_refptr<CookieMonsterTask> task = | 1049 base::OnceClosure task = std::move(tasks_pending_for_key->second.front()); |
1445 tasks_pending_for_key->second.front(); | |
1446 tasks_pending_for_key->second.pop_front(); | 1050 tasks_pending_for_key->second.pop_front(); |
1447 | 1051 std::move(task).Run(); |
1448 task->Run(); | |
1449 } | 1052 } |
1450 | 1053 |
1451 tasks_pending_for_key_.erase(tasks_pending_for_key); | 1054 tasks_pending_for_key_.erase(tasks_pending_for_key); |
1452 | 1055 |
1453 // This has to be done last, in case running a task queues a new task for the | 1056 // This has to be done last, in case running a task queues a new task for the |
1454 // key, to ensure tasks are run in the correct order. | 1057 // key, to ensure tasks are run in the correct order. |
1455 keys_loaded_.insert(key); | 1058 keys_loaded_.insert(key); |
1456 } | 1059 } |
1457 | 1060 |
1458 void CookieMonster::StoreLoadedCookies( | 1061 void CookieMonster::StoreLoadedCookies( |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1518 void CookieMonster::InvokeQueue() { | 1121 void CookieMonster::InvokeQueue() { |
1519 DCHECK(thread_checker_.CalledOnValidThread()); | 1122 DCHECK(thread_checker_.CalledOnValidThread()); |
1520 | 1123 |
1521 // Move all per-key tasks into the global queue, if there are any. This is | 1124 // Move all per-key tasks into the global queue, if there are any. This is |
1522 // protection about a race where the store learns about all cookies loading | 1125 // protection about a race where the store learns about all cookies loading |
1523 // before it learned about the cookies for a key loading. | 1126 // before it learned about the cookies for a key loading. |
1524 | 1127 |
1525 // Needed to prevent any recursively queued tasks from going back into the | 1128 // Needed to prevent any recursively queued tasks from going back into the |
1526 // per-key queues. | 1129 // per-key queues. |
1527 seen_global_task_ = true; | 1130 seen_global_task_ = true; |
1528 for (const auto& tasks_for_key : tasks_pending_for_key_) { | 1131 for (auto& tasks_for_key : tasks_pending_for_key_) { |
1529 tasks_pending_.insert(tasks_pending_.begin(), tasks_for_key.second.begin(), | 1132 tasks_pending_.insert(tasks_pending_.begin(), |
1530 tasks_for_key.second.end()); | 1133 std::make_move_iterator(tasks_for_key.second.begin()), |
1134 std::make_move_iterator(tasks_for_key.second.end())); | |
1531 } | 1135 } |
1532 tasks_pending_for_key_.clear(); | 1136 tasks_pending_for_key_.clear(); |
1533 | 1137 |
1534 while (!tasks_pending_.empty()) { | 1138 while (!tasks_pending_.empty()) { |
1535 scoped_refptr<CookieMonsterTask> request_task = tasks_pending_.front(); | 1139 base::OnceClosure request_task = std::move(tasks_pending_.front()); |
1536 tasks_pending_.pop_front(); | 1140 tasks_pending_.pop_front(); |
1537 request_task->Run(); | 1141 std::move(request_task).Run(); |
1538 } | 1142 } |
1539 | 1143 |
1540 DCHECK(tasks_pending_for_key_.empty()); | 1144 DCHECK(tasks_pending_for_key_.empty()); |
1541 | 1145 |
1542 finished_fetching_all_cookies_ = true; | 1146 finished_fetching_all_cookies_ = true; |
1543 creation_times_.clear(); | 1147 creation_times_.clear(); |
1544 keys_loaded_.clear(); | 1148 keys_loaded_.clear(); |
1545 } | 1149 } |
1546 | 1150 |
1547 void CookieMonster::EnsureCookiesMapIsValid() { | 1151 void CookieMonster::EnsureCookiesMapIsValid() { |
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1874 // 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 |
1875 // 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, |
1876 // 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 |
1877 // 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, |
1878 // and we will purge the expired cookies in GetCookies(). | 1482 // and we will purge the expired cookies in GetCookies(). |
1879 GarbageCollect(creation_date, key); | 1483 GarbageCollect(creation_date, key); |
1880 | 1484 |
1881 return true; | 1485 return true; |
1882 } | 1486 } |
1883 | 1487 |
1884 bool CookieMonster::SetAllCookies(const CookieList& list) { | 1488 bool CookieMonster::SetAllCookies(CookieList list) { |
1885 DCHECK(thread_checker_.CalledOnValidThread()); | 1489 DCHECK(thread_checker_.CalledOnValidThread()); |
1490 CookieList positive_diff; | |
1491 CookieList negative_diff; | |
1492 CookieList old_cookies = GetAllCookies(); | |
1493 ComputeCookieDiff(&old_cookies, &list, &positive_diff, &negative_diff); | |
1494 | |
1495 for (CookieList::const_iterator it = negative_diff.begin(); | |
1496 it != negative_diff.end(); ++it) { | |
1497 DeleteCanonicalCookie(*it); | |
1498 } | |
1499 | |
1500 if (positive_diff.size() == 0) | |
1501 return true; | |
mmenke
2017/07/07 16:54:58
This doesn't seem to belong in this CL. I'm not a
Randy Smith (Not in Mondays)
2017/07/07 20:09:54
To be clear, this isn't new code--it's just a refa
mmenke
2017/07/07 20:15:12
Sorry, completely missed that.
Randy Smith (Not in Mondays)
2017/07/08 15:02:08
Yeah, so did I on my first pass, then the tests bo
| |
1502 | |
1886 for (const auto& cookie : list) { | 1503 for (const auto& cookie : list) { |
1887 const std::string key(GetKey(cookie.Domain())); | 1504 const std::string key(GetKey(cookie.Domain())); |
1888 Time creation_time = cookie.CreationDate(); | 1505 Time creation_time = cookie.CreationDate(); |
1889 bool already_expired = cookie.IsExpired(creation_time); | 1506 bool already_expired = cookie.IsExpired(creation_time); |
1890 | 1507 |
1891 bool result = | 1508 bool result = |
1892 DeleteAnyEquivalentCookie(key, cookie, true, false, already_expired); | 1509 DeleteAnyEquivalentCookie(key, cookie, true, false, already_expired); |
1893 DCHECK(!result); | 1510 DCHECK(!result); |
1894 | 1511 |
1895 if (already_expired) | 1512 if (already_expired) |
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2381 } | 1998 } |
2382 | 1999 |
2383 // The system resolution is not high enough, so we can have multiple | 2000 // The system resolution is not high enough, so we can have multiple |
2384 // set cookies that result in the same system time. When this happens, we | 2001 // set cookies that result in the same system time. When this happens, we |
2385 // increment by one Time unit. Let's hope computers don't get too fast. | 2002 // increment by one Time unit. Let's hope computers don't get too fast. |
2386 Time CookieMonster::CurrentTime() { | 2003 Time CookieMonster::CurrentTime() { |
2387 return std::max(Time::Now(), Time::FromInternalValue( | 2004 return std::max(Time::Now(), Time::FromInternalValue( |
2388 last_time_seen_.ToInternalValue() + 1)); | 2005 last_time_seen_.ToInternalValue() + 1)); |
2389 } | 2006 } |
2390 | 2007 |
2391 void CookieMonster::DoCookieTask( | 2008 void CookieMonster::DoCookieCallback(base::OnceClosure callback) { |
2392 const scoped_refptr<CookieMonsterTask>& task_item) { | |
2393 DCHECK(thread_checker_.CalledOnValidThread()); | 2009 DCHECK(thread_checker_.CalledOnValidThread()); |
2394 | 2010 |
2395 MarkCookieStoreAsInitialized(); | 2011 MarkCookieStoreAsInitialized(); |
2396 FetchAllCookiesIfNecessary(); | 2012 FetchAllCookiesIfNecessary(); |
2397 seen_global_task_ = true; | 2013 seen_global_task_ = true; |
2398 | 2014 |
2399 if (!finished_fetching_all_cookies_ && store_.get()) { | 2015 if (!finished_fetching_all_cookies_ && store_.get()) { |
2400 tasks_pending_.push_back(task_item); | 2016 tasks_pending_.push_back(std::move(callback)); |
2401 return; | 2017 return; |
2402 } | 2018 } |
2403 | 2019 |
2404 task_item->Run(); | 2020 std::move(callback).Run(); |
2405 } | 2021 } |
2406 | 2022 |
2407 void CookieMonster::DoCookieTaskForURL( | 2023 void CookieMonster::DoCookieCallbackForURL(base::OnceClosure callback, |
2408 const scoped_refptr<CookieMonsterTask>& task_item, | 2024 const GURL& url) { |
2409 const GURL& url) { | |
2410 MarkCookieStoreAsInitialized(); | 2025 MarkCookieStoreAsInitialized(); |
2411 if (ShouldFetchAllCookiesWhenFetchingAnyCookie()) | 2026 if (ShouldFetchAllCookiesWhenFetchingAnyCookie()) |
2412 FetchAllCookiesIfNecessary(); | 2027 FetchAllCookiesIfNecessary(); |
2413 | 2028 |
2414 // If cookies for the requested domain key (eTLD+1) have been loaded from DB | 2029 // If cookies for the requested domain key (eTLD+1) have been loaded from DB |
2415 // then run the task, otherwise load from DB. | 2030 // then run the task, otherwise load from DB. |
2416 if (!finished_fetching_all_cookies_ && store_.get()) { | 2031 if (!finished_fetching_all_cookies_ && store_.get()) { |
2417 // If a global task has been previously seen, queue the task as a global | 2032 // If a global task has been previously seen, queue the task as a global |
2418 // task. Note that the CookieMonster may be in the middle of executing | 2033 // task. Note that the CookieMonster may be in the middle of executing |
2419 // the global queue, |tasks_pending_| may be empty, which is why another | 2034 // the global queue, |tasks_pending_| may be empty, which is why another |
2420 // bool is needed. | 2035 // bool is needed. |
2421 if (seen_global_task_) { | 2036 if (seen_global_task_) { |
2422 tasks_pending_.push_back(task_item); | 2037 tasks_pending_.push_back(std::move(callback)); |
2423 return; | 2038 return; |
2424 } | 2039 } |
2425 | 2040 |
2426 // Checks if the domain key has been loaded. | 2041 // Checks if the domain key has been loaded. |
2427 std::string key(cookie_util::GetEffectiveDomain(url.scheme(), url.host())); | 2042 std::string key(cookie_util::GetEffectiveDomain(url.scheme(), url.host())); |
2428 if (keys_loaded_.find(key) == keys_loaded_.end()) { | 2043 if (keys_loaded_.find(key) == keys_loaded_.end()) { |
2429 std::map<std::string, | 2044 std::map<std::string, std::deque<base::OnceClosure>>::iterator it = |
2430 std::deque<scoped_refptr<CookieMonsterTask>>>::iterator it = | |
2431 tasks_pending_for_key_.find(key); | 2045 tasks_pending_for_key_.find(key); |
2432 if (it == tasks_pending_for_key_.end()) { | 2046 if (it == tasks_pending_for_key_.end()) { |
2433 store_->LoadCookiesForKey( | 2047 store_->LoadCookiesForKey( |
2434 key, base::Bind(&CookieMonster::OnKeyLoaded, | 2048 key, base::Bind(&CookieMonster::OnKeyLoaded, |
2435 weak_ptr_factory_.GetWeakPtr(), key)); | 2049 weak_ptr_factory_.GetWeakPtr(), key)); |
2436 it = tasks_pending_for_key_ | 2050 it = tasks_pending_for_key_ |
2437 .insert(std::make_pair( | 2051 .insert(std::make_pair(key, std::deque<base::OnceClosure>())) |
2438 key, std::deque<scoped_refptr<CookieMonsterTask>>())) | |
2439 .first; | 2052 .first; |
2440 } | 2053 } |
2441 it->second.push_back(task_item); | 2054 it->second.push_back(std::move(callback)); |
2442 return; | 2055 return; |
2443 } | 2056 } |
2444 } | 2057 } |
2445 | 2058 |
2446 task_item->Run(); | 2059 std::move(callback).Run(); |
2447 } | 2060 } |
2448 | 2061 |
2449 void CookieMonster::ComputeCookieDiff(CookieList* old_cookies, | 2062 void CookieMonster::ComputeCookieDiff(CookieList* old_cookies, |
2450 CookieList* new_cookies, | 2063 CookieList* new_cookies, |
2451 CookieList* cookies_to_add, | 2064 CookieList* cookies_to_add, |
2452 CookieList* cookies_to_delete) { | 2065 CookieList* cookies_to_delete) { |
2453 DCHECK(thread_checker_.CalledOnValidThread()); | 2066 DCHECK(thread_checker_.CalledOnValidThread()); |
2454 | 2067 |
2455 DCHECK(old_cookies); | 2068 DCHECK(old_cookies); |
2456 DCHECK(new_cookies); | 2069 DCHECK(new_cookies); |
(...skipping 17 matching lines...) Expand all Loading... | |
2474 PartialDiffCookieSorter); | 2087 PartialDiffCookieSorter); |
2475 | 2088 |
2476 // Select any new cookie for addition (or update) if no old cookie is exactly | 2089 // Select any new cookie for addition (or update) if no old cookie is exactly |
2477 // equivalent. | 2090 // equivalent. |
2478 std::set_difference(new_cookies->begin(), new_cookies->end(), | 2091 std::set_difference(new_cookies->begin(), new_cookies->end(), |
2479 old_cookies->begin(), old_cookies->end(), | 2092 old_cookies->begin(), old_cookies->end(), |
2480 std::inserter(*cookies_to_add, cookies_to_add->begin()), | 2093 std::inserter(*cookies_to_add, cookies_to_add->begin()), |
2481 FullDiffCookieSorter); | 2094 FullDiffCookieSorter); |
2482 } | 2095 } |
2483 | 2096 |
2484 void CookieMonster::RunCallback(base::OnceClosure callback) { | |
2485 DCHECK(thread_checker_.CalledOnValidThread()); | |
2486 std::move(callback).Run(); | |
2487 } | |
2488 | |
2489 void CookieMonster::RunCookieChangedCallbacks(const CanonicalCookie& cookie, | 2097 void CookieMonster::RunCookieChangedCallbacks(const CanonicalCookie& cookie, |
2490 ChangeCause cause) { | 2098 ChangeCause cause) { |
2491 DCHECK(thread_checker_.CalledOnValidThread()); | 2099 DCHECK(thread_checker_.CalledOnValidThread()); |
2492 | 2100 |
2493 CookieOptions opts; | 2101 CookieOptions opts; |
2494 opts.set_include_httponly(); | 2102 opts.set_include_httponly(); |
2495 opts.set_same_site_cookie_mode( | 2103 opts.set_same_site_cookie_mode( |
2496 CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX); | 2104 CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX); |
2497 // Note that the callbacks in hook_map_ are wrapped with RunAsync(), so they | 2105 // Note that the callbacks in hook_map_ are wrapped with RunAsync(), so they |
2498 // are guaranteed to not take long - they just post a RunAsync task back to | 2106 // are guaranteed to not take long - they just post a RunAsync task back to |
2499 // the appropriate thread's message loop and return. | 2107 // the appropriate thread's message loop and return. |
2500 // TODO(mmenke): Consider running these synchronously? | 2108 // TODO(mmenke): Consider running these synchronously? |
2501 for (CookieChangedHookMap::iterator it = hook_map_.begin(); | 2109 for (CookieChangedHookMap::iterator it = hook_map_.begin(); |
2502 it != hook_map_.end(); ++it) { | 2110 it != hook_map_.end(); ++it) { |
2503 std::pair<GURL, std::string> key = it->first; | 2111 std::pair<GURL, std::string> key = it->first; |
2504 if (cookie.IncludeForRequestURL(key.first, opts) && | 2112 if (cookie.IncludeForRequestURL(key.first, opts) && |
2505 cookie.Name() == key.second) { | 2113 cookie.Name() == key.second) { |
2506 it->second->Notify(cookie, cause); | 2114 it->second->Notify(cookie, cause); |
2507 } | 2115 } |
2508 } | 2116 } |
2509 } | 2117 } |
2510 | 2118 |
2511 } // namespace net | 2119 } // namespace net |
OLD | NEW |