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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 WrapGetCookieListCallback( | |
106 base::OnceCallback<net::CookieList()> function, | |
107 base::OnceCallback<void(const net::CookieList&)> response) { | |
108 if (response.is_null()) | |
109 return; | |
110 std::move(response).Run(std::move(function).Run()); | |
111 } | |
112 | |
113 void WrapSetCookiesCallback(base::OnceCallback<bool()> function, | |
114 base::OnceCallback<void(bool)> response) { | |
115 bool result = std::move(function).Run(); | |
116 if (response.is_null()) | |
117 return; | |
118 std::move(response).Run(result); | |
119 } | |
120 | |
121 void WrapGetCookiesCallback( | |
122 base::OnceCallback<std::string()> function, | |
123 base::OnceCallback<void(const std::string&)> response) { | |
124 std::string result = std::move(function).Run(); | |
125 if (response.is_null()) | |
126 return; | |
127 std::move(response).Run(result); | |
128 } | |
129 | |
130 void ConditionalDeleteCallback(base::WeakPtr<net::CookieMonster> cookie_monster, | |
131 base::OnceClosure callback) { | |
132 if (cookie_monster) | |
133 std::move(callback).Run(); | |
134 } | |
135 | |
136 void WrapDeleteCallback(base::WeakPtr<net::CookieMonster> cookie_monster, | |
137 base::OnceCallback<uint32_t()> function, | |
138 base::OnceCallback<void(uint32_t)> response) { | |
139 uint32_t num_deleted = std::move(function).Run(); | |
140 | |
141 // This function is always called directly from a CookieMonster routine, | |
142 // so unconditional use of the weak pointer here is safe. | |
143 cookie_monster->FlushStore( | |
144 response.is_null() | |
145 ? base::OnceClosure() | |
146 : base::BindOnce(&ConditionalDeleteCallback, cookie_monster, | |
147 base::BindOnce(std::move(response), num_deleted))); | |
148 } | |
149 | |
150 void WrapDeleteCookieCallback(base::WeakPtr<net::CookieMonster> cookie_monster, | |
151 base::OnceCallback<void()> function, | |
152 base::OnceCallback<void()> response) { | |
153 std::move(function).Run(); | |
154 | |
155 // This function is always called directly from a CookieMonster routine, | |
156 // so unconditional use of the weak pointer here is safe. | |
157 cookie_monster->FlushStore( | |
158 response.is_null() ? base::OnceClosure() | |
159 : base::BindOnce(&ConditionalDeleteCallback, | |
160 cookie_monster, std::move(response))); | |
161 } | |
162 | |
105 } // namespace | 163 } // namespace |
106 | 164 |
107 namespace net { | 165 namespace net { |
108 | 166 |
109 // See comments at declaration of these variables in cookie_monster.h | 167 // See comments at declaration of these variables in cookie_monster.h |
110 // for details. | 168 // for details. |
111 const size_t CookieMonster::kDomainMaxCookies = 180; | 169 const size_t CookieMonster::kDomainMaxCookies = 180; |
112 const size_t CookieMonster::kDomainPurgeCookies = 30; | 170 const size_t CookieMonster::kDomainPurgeCookies = 30; |
113 const size_t CookieMonster::kMaxCookies = 3300; | 171 const size_t CookieMonster::kMaxCookies = 3300; |
114 const size_t CookieMonster::kPurgeCookies = 300; | 172 const size_t CookieMonster::kPurgeCookies = 300; |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
378 channel_id_service_(channel_id_service), | 436 channel_id_service_(channel_id_service), |
379 last_statistic_record_time_(base::Time::Now()), | 437 last_statistic_record_time_(base::Time::Now()), |
380 persist_session_cookies_(false), | 438 persist_session_cookies_(false), |
381 weak_ptr_factory_(this) { | 439 weak_ptr_factory_(this) { |
382 InitializeHistograms(); | 440 InitializeHistograms(); |
383 cookieable_schemes_.insert( | 441 cookieable_schemes_.insert( |
384 cookieable_schemes_.begin(), kDefaultCookieableSchemes, | 442 cookieable_schemes_.begin(), kDefaultCookieableSchemes, |
385 kDefaultCookieableSchemes + kDefaultCookieableSchemesCount); | 443 kDefaultCookieableSchemes + kDefaultCookieableSchemesCount); |
386 } | 444 } |
387 | 445 |
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 | 446 // Asynchronous CookieMonster API |
899 | 447 |
900 void CookieMonster::SetCookieWithDetailsAsync(const GURL& url, | 448 void CookieMonster::SetCookieWithDetailsAsync(const GURL& url, |
901 const std::string& name, | 449 const std::string& name, |
902 const std::string& value, | 450 const std::string& value, |
903 const std::string& domain, | 451 const std::string& domain, |
904 const std::string& path, | 452 const std::string& path, |
905 Time creation_time, | 453 Time creation_time, |
906 Time expiration_time, | 454 Time expiration_time, |
907 Time last_access_time, | 455 Time last_access_time, |
908 bool secure, | 456 bool secure, |
909 bool http_only, | 457 bool http_only, |
910 CookieSameSite same_site, | 458 CookieSameSite same_site, |
911 CookiePriority priority, | 459 CookiePriority priority, |
912 SetCookiesCallback callback) { | 460 SetCookiesCallback callback) { |
913 scoped_refptr<SetCookieWithDetailsTask> task = new SetCookieWithDetailsTask( | 461 DoCookieCallbackForURL( |
914 this, url, name, value, domain, path, creation_time, expiration_time, | 462 base::BindOnce( |
915 last_access_time, secure, http_only, same_site, priority, | 463 &WrapSetCookiesCallback, |
916 std::move(callback)); | 464 base::BindOnce( |
917 DoCookieTaskForURL(task, url); | 465 // base::Unretained is safe as DoCookieCallbackForURL stores |
466 // the callback on |*this|, so the callback will not outlive | |
467 // the object. | |
468 &CookieMonster::SetCookieWithDetails, base::Unretained(this), url, | |
469 name, value, domain, path, creation_time, expiration_time, | |
470 last_access_time, secure, http_only, same_site, priority), | |
471 std::move(callback)), | |
472 url); | |
918 } | 473 } |
919 | 474 |
920 void CookieMonster::FlushStore(base::OnceClosure callback) { | 475 void CookieMonster::FlushStore(base::OnceClosure callback) { |
921 DCHECK(thread_checker_.CalledOnValidThread()); | 476 DCHECK(thread_checker_.CalledOnValidThread()); |
922 | 477 |
923 if (initialized_ && store_.get()) { | 478 if (initialized_ && store_.get()) { |
924 if (channel_id_service_) { | 479 if (channel_id_service_) { |
925 channel_id_service_->GetChannelIDStore()->Flush(); | 480 channel_id_service_->GetChannelIDStore()->Flush(); |
926 } | 481 } |
927 store_->Flush(std::move(callback)); | 482 store_->Flush(std::move(callback)); |
928 } else if (!callback.is_null()) { | 483 } else if (!callback.is_null()) { |
929 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 484 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
930 std::move(callback)); | 485 std::move(callback)); |
931 } | 486 } |
932 } | 487 } |
933 | 488 |
934 void CookieMonster::SetForceKeepSessionState() { | 489 void CookieMonster::SetForceKeepSessionState() { |
935 DCHECK(thread_checker_.CalledOnValidThread()); | 490 DCHECK(thread_checker_.CalledOnValidThread()); |
936 | 491 |
937 if (store_) | 492 if (store_) |
938 store_->SetForceKeepSessionState(); | 493 store_->SetForceKeepSessionState(); |
939 } | 494 } |
940 | 495 |
941 void CookieMonster::SetAllCookiesAsync(const CookieList& list, | 496 void CookieMonster::SetAllCookiesAsync(const CookieList& list, |
942 SetCookiesCallback callback) { | 497 SetCookiesCallback callback) { |
943 scoped_refptr<SetAllCookiesTask> task = | 498 DoCookieCallback(base::BindOnce( |
944 new SetAllCookiesTask(this, list, std::move(callback)); | 499 &WrapSetCookiesCallback, |
945 DoCookieTask(task); | 500 base::BindOnce( |
501 // base::Unretained is safe as DoCookieCallbackForURL stores | |
502 // the callback on |*this|, so the callback will not outlive | |
503 // the object. | |
504 &CookieMonster::SetAllCookies, base::Unretained(this), list), | |
505 std::move(callback))); | |
946 } | 506 } |
947 | 507 |
948 void CookieMonster::SetCanonicalCookieAsync( | 508 void CookieMonster::SetCanonicalCookieAsync( |
949 std::unique_ptr<CanonicalCookie> cookie, | 509 std::unique_ptr<CanonicalCookie> cookie, |
950 bool secure_source, | 510 bool secure_source, |
951 bool modify_http_only, | 511 bool modify_http_only, |
952 SetCookiesCallback callback) { | 512 SetCookiesCallback callback) { |
953 DCHECK(cookie->IsCanonical()); | 513 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 | 514 |
958 // TODO(rdsmith): Switch to DoCookieTaskForURL (or the equivalent). | 515 // TODO(rdsmith): Switch to DoCookieCallbackForURL (or the equivalent). |
959 // This is tricky because we don't have the scheme in this routine | 516 // This is tricky because we don't have the scheme in this routine |
960 // and DoCookieTaskForURL uses cookie_util::GetEffectiveDomain(scheme, host) | 517 // and DoCookieCallbackForURL uses |
518 // cookie_util::GetEffectiveDomain(scheme, host) | |
961 // to generate the database key to block behind. | 519 // to generate the database key to block behind. |
962 DoCookieTask(task); | 520 DoCookieCallback(base::BindOnce( |
521 &WrapSetCookiesCallback, | |
522 base::BindOnce( | |
523 // base::Unretained is safe as DoCookieCallbackForURL stores | |
524 // the callback on |*this|, so the callback will not outlive | |
525 // the object. | |
526 &CookieMonster::SetCanonicalCookie, base::Unretained(this), | |
527 std::move(cookie), secure_source, modify_http_only), | |
528 std::move(callback))); | |
963 } | 529 } |
964 | 530 |
965 void CookieMonster::SetCookieWithOptionsAsync(const GURL& url, | 531 void CookieMonster::SetCookieWithOptionsAsync(const GURL& url, |
966 const std::string& cookie_line, | 532 const std::string& cookie_line, |
967 const CookieOptions& options, | 533 const CookieOptions& options, |
968 SetCookiesCallback callback) { | 534 SetCookiesCallback callback) { |
969 scoped_refptr<SetCookieWithOptionsTask> task = new SetCookieWithOptionsTask( | 535 DoCookieCallbackForURL( |
970 this, url, cookie_line, options, std::move(callback)); | 536 base::BindOnce( |
971 | 537 &WrapSetCookiesCallback, |
972 DoCookieTaskForURL(task, url); | 538 base::BindOnce( |
539 // base::Unretained is safe as DoCookieCallbackForURL stores | |
540 // the callback on |*this|, so the callback will not outlive | |
541 // the object. | |
542 &CookieMonster::SetCookieWithOptions, base::Unretained(this), url, | |
543 cookie_line, options), | |
544 std::move(callback)), | |
545 url); | |
973 } | 546 } |
974 | 547 |
975 void CookieMonster::GetCookiesWithOptionsAsync(const GURL& url, | 548 void CookieMonster::GetCookiesWithOptionsAsync(const GURL& url, |
976 const CookieOptions& options, | 549 const CookieOptions& options, |
977 GetCookiesCallback callback) { | 550 GetCookiesCallback callback) { |
978 scoped_refptr<GetCookiesWithOptionsTask> task = | 551 DoCookieCallbackForURL( |
979 new GetCookiesWithOptionsTask(this, url, options, std::move(callback)); | 552 base::BindOnce( |
980 | 553 &WrapGetCookiesCallback, |
981 DoCookieTaskForURL(task, url); | 554 base::BindOnce( |
555 // base::Unretained is safe as DoCookieCallbackForURL stores | |
556 // the callback on |*this|, so the callback will not outlive | |
557 // the object. | |
558 &CookieMonster::GetCookiesWithOptions, base::Unretained(this), | |
559 url, options), | |
560 std::move(callback)), | |
561 url); | |
982 } | 562 } |
983 | 563 |
984 void CookieMonster::GetCookieListWithOptionsAsync( | 564 void CookieMonster::GetCookieListWithOptionsAsync( |
985 const GURL& url, | 565 const GURL& url, |
986 const CookieOptions& options, | 566 const CookieOptions& options, |
987 GetCookieListCallback callback) { | 567 GetCookieListCallback callback) { |
988 scoped_refptr<GetCookieListWithOptionsTask> task = | 568 DoCookieCallbackForURL( |
989 new GetCookieListWithOptionsTask(this, url, options, std::move(callback)); | 569 base::BindOnce( |
990 | 570 &WrapGetCookieListCallback, |
991 DoCookieTaskForURL(task, url); | 571 base::BindOnce( |
572 // base::Unretained is safe as DoCookieCallbackForURL stores | |
573 // the callback on |*this|, so the callback will not outlive | |
574 // the object. | |
575 &CookieMonster::GetCookieListWithOptions, base::Unretained(this), | |
576 url, options), | |
577 std::move(callback)), | |
578 url); | |
992 } | 579 } |
993 | 580 |
994 void CookieMonster::GetAllCookiesAsync(GetCookieListCallback callback) { | 581 void CookieMonster::GetAllCookiesAsync(GetCookieListCallback callback) { |
995 scoped_refptr<GetAllCookiesTask> task = | 582 DoCookieCallback(base::BindOnce( |
996 new GetAllCookiesTask(this, std::move(callback)); | 583 &WrapGetCookieListCallback, |
997 | 584 base::BindOnce( |
998 DoCookieTask(task); | 585 // base::Unretained is safe as DoCookieCallbackForURL stores |
586 // the callback on |*this|, so the callback will not outlive | |
587 // the object. | |
588 &CookieMonster::GetAllCookies, base::Unretained(this)), | |
589 std::move(callback))); | |
999 } | 590 } |
1000 | 591 |
1001 void CookieMonster::DeleteCookieAsync(const GURL& url, | 592 void CookieMonster::DeleteCookieAsync(const GURL& url, |
1002 const std::string& cookie_name, | 593 const std::string& cookie_name, |
1003 base::OnceClosure callback) { | 594 base::OnceClosure callback) { |
1004 scoped_refptr<DeleteCookieTask> task = | 595 DoCookieCallbackForURL( |
1005 new DeleteCookieTask(this, url, cookie_name, std::move(callback)); | 596 base::BindOnce( |
1006 | 597 &WrapDeleteCookieCallback, weak_ptr_factory_.GetWeakPtr(), |
1007 DoCookieTaskForURL(task, url); | 598 base::BindOnce( |
599 // base::Unretained is safe as DoCookieCallbackForURL stores | |
600 // the callback on |*this|, so the callback will not outlive | |
601 // the object. | |
602 &CookieMonster::DeleteCookie, base::Unretained(this), url, | |
603 cookie_name), | |
604 std::move(callback)), | |
605 url); | |
1008 } | 606 } |
1009 | 607 |
1010 void CookieMonster::DeleteCanonicalCookieAsync(const CanonicalCookie& cookie, | 608 void CookieMonster::DeleteCanonicalCookieAsync(const CanonicalCookie& cookie, |
1011 DeleteCallback callback) { | 609 DeleteCallback callback) { |
1012 scoped_refptr<DeleteCanonicalCookieTask> task = | 610 DoCookieCallback(base::BindOnce( |
1013 new DeleteCanonicalCookieTask(this, cookie, std::move(callback)); | 611 &WrapDeleteCallback, weak_ptr_factory_.GetWeakPtr(), |
1014 | 612 base::BindOnce( |
1015 DoCookieTask(task); | 613 // base::Unretained is safe as DoCookieCallbackForURL stores |
614 // the callback on |*this|, so the callback will not outlive | |
615 // the object. | |
616 &CookieMonster::DeleteCanonicalCookie, base::Unretained(this), | |
617 cookie), | |
618 std::move(callback))); | |
1016 } | 619 } |
1017 | 620 |
1018 void CookieMonster::DeleteAllCreatedBetweenAsync(const Time& delete_begin, | 621 void CookieMonster::DeleteAllCreatedBetweenAsync(const Time& delete_begin, |
1019 const Time& delete_end, | 622 const Time& delete_end, |
1020 DeleteCallback callback) { | 623 DeleteCallback callback) { |
1021 scoped_refptr<DeleteAllCreatedBetweenTask> task = | 624 DoCookieCallback(base::BindOnce( |
1022 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end, | 625 &WrapDeleteCallback, weak_ptr_factory_.GetWeakPtr(), |
1023 std::move(callback)); | 626 base::BindOnce( |
1024 | 627 // base::Unretained is safe as DoCookieCallbackForURL stores |
1025 DoCookieTask(task); | 628 // the callback on |*this|, so the callback will not outlive |
629 // the object. | |
630 &CookieMonster::DeleteAllCreatedBetween, base::Unretained(this), | |
631 delete_begin, delete_end), | |
632 std::move(callback))); | |
1026 } | 633 } |
1027 | 634 |
1028 void CookieMonster::DeleteAllCreatedBetweenWithPredicateAsync( | 635 void CookieMonster::DeleteAllCreatedBetweenWithPredicateAsync( |
1029 const Time& delete_begin, | 636 const Time& delete_begin, |
1030 const Time& delete_end, | 637 const Time& delete_end, |
1031 const base::Callback<bool(const CanonicalCookie&)>& predicate, | 638 const base::Callback<bool(const CanonicalCookie&)>& predicate, |
1032 DeleteCallback callback) { | 639 DeleteCallback callback) { |
1033 if (predicate.is_null()) { | 640 if (predicate.is_null()) { |
1034 std::move(callback).Run(0); | 641 std::move(callback).Run(0); |
1035 return; | 642 return; |
1036 } | 643 } |
1037 scoped_refptr<DeleteAllCreatedBetweenWithPredicateTask> task = | 644 |
1038 new DeleteAllCreatedBetweenWithPredicateTask( | 645 DoCookieCallback(base::BindOnce( |
1039 this, delete_begin, delete_end, predicate, std::move(callback)); | 646 &WrapDeleteCallback, weak_ptr_factory_.GetWeakPtr(), |
1040 DoCookieTask(task); | 647 base::BindOnce( |
648 // base::Unretained is safe as DoCookieCallbackForURL stores | |
649 // the callback on |*this|, so the callback will not outlive | |
650 // the object. | |
651 &CookieMonster::DeleteAllCreatedBetweenWithPredicate, | |
652 base::Unretained(this), delete_begin, delete_end, predicate), | |
653 std::move(callback))); | |
1041 } | 654 } |
1042 | 655 |
1043 void CookieMonster::DeleteSessionCookiesAsync( | 656 void CookieMonster::DeleteSessionCookiesAsync( |
1044 CookieStore::DeleteCallback callback) { | 657 CookieStore::DeleteCallback callback) { |
1045 scoped_refptr<DeleteSessionCookiesTask> task = | 658 DoCookieCallback(base::BindOnce( |
1046 new DeleteSessionCookiesTask(this, std::move(callback)); | 659 &WrapDeleteCallback, weak_ptr_factory_.GetWeakPtr(), |
1047 | 660 base::BindOnce( |
1048 DoCookieTask(task); | 661 // base::Unretained is safe as DoCookieCallbackForURL stores |
662 // the callback on |*this|, so the callback will not outlive | |
663 // the object. | |
664 &CookieMonster::DeleteSessionCookies, base::Unretained(this)), | |
665 std::move(callback))); | |
1049 } | 666 } |
1050 | 667 |
1051 void CookieMonster::SetCookieableSchemes( | 668 void CookieMonster::SetCookieableSchemes( |
1052 const std::vector<std::string>& schemes) { | 669 const std::vector<std::string>& schemes) { |
1053 DCHECK(thread_checker_.CalledOnValidThread()); | 670 DCHECK(thread_checker_.CalledOnValidThread()); |
1054 | 671 |
1055 // Calls to this method will have no effect if made after a WebView or | 672 // Calls to this method will have no effect if made after a WebView or |
1056 // CookieManager instance has been created. | 673 // CookieManager instance has been created. |
1057 if (initialized_) | 674 if (initialized_) |
1058 return; | 675 return; |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1433 | 1050 |
1434 auto tasks_pending_for_key = tasks_pending_for_key_.find(key); | 1051 auto tasks_pending_for_key = tasks_pending_for_key_.find(key); |
1435 | 1052 |
1436 // TODO(mmenke): Can this be turned into a DCHECK? | 1053 // TODO(mmenke): Can this be turned into a DCHECK? |
1437 if (tasks_pending_for_key == tasks_pending_for_key_.end()) | 1054 if (tasks_pending_for_key == tasks_pending_for_key_.end()) |
1438 return; | 1055 return; |
1439 | 1056 |
1440 // Run all tasks for the key. Note that running a task can result in multiple | 1057 // 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. | 1058 // tasks being added to the back of the deque. |
1442 while (!tasks_pending_for_key->second.empty()) { | 1059 while (!tasks_pending_for_key->second.empty()) { |
1443 scoped_refptr<CookieMonsterTask> task = | 1060 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(); | 1061 tasks_pending_for_key->second.pop_front(); |
1446 | 1062 std::move(task).Run(); |
1447 task->Run(); | |
1448 } | 1063 } |
1449 | 1064 |
1450 tasks_pending_for_key_.erase(tasks_pending_for_key); | 1065 tasks_pending_for_key_.erase(tasks_pending_for_key); |
1451 | 1066 |
1452 // This has to be done last, in case running a task queues a new task for the | 1067 // 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. | 1068 // key, to ensure tasks are run in the correct order. |
1454 keys_loaded_.insert(key); | 1069 keys_loaded_.insert(key); |
1455 } | 1070 } |
1456 | 1071 |
1457 void CookieMonster::StoreLoadedCookies( | 1072 void CookieMonster::StoreLoadedCookies( |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1517 void CookieMonster::InvokeQueue() { | 1132 void CookieMonster::InvokeQueue() { |
1518 DCHECK(thread_checker_.CalledOnValidThread()); | 1133 DCHECK(thread_checker_.CalledOnValidThread()); |
1519 | 1134 |
1520 // Move all per-key tasks into the global queue, if there are any. This is | 1135 // 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 | 1136 // protection about a race where the store learns about all cookies loading |
1522 // before it learned about the cookies for a key loading. | 1137 // before it learned about the cookies for a key loading. |
1523 | 1138 |
1524 // Needed to prevent any recursively queued tasks from going back into the | 1139 // Needed to prevent any recursively queued tasks from going back into the |
1525 // per-key queues. | 1140 // per-key queues. |
1526 seen_global_task_ = true; | 1141 seen_global_task_ = true; |
1527 for (const auto& tasks_for_key : tasks_pending_for_key_) { | 1142 for (auto& tasks_for_key : tasks_pending_for_key_) { |
1528 tasks_pending_.insert(tasks_pending_.begin(), tasks_for_key.second.begin(), | 1143 tasks_pending_.insert(tasks_pending_.begin(), |
1529 tasks_for_key.second.end()); | 1144 std::make_move_iterator(tasks_for_key.second.begin()), |
1145 std::make_move_iterator(tasks_for_key.second.end())); | |
1530 } | 1146 } |
1531 tasks_pending_for_key_.clear(); | 1147 tasks_pending_for_key_.clear(); |
1532 | 1148 |
1533 while (!tasks_pending_.empty()) { | 1149 while (!tasks_pending_.empty()) { |
1534 scoped_refptr<CookieMonsterTask> request_task = tasks_pending_.front(); | 1150 base::OnceClosure request_task = std::move(tasks_pending_.front()); |
1535 tasks_pending_.pop_front(); | 1151 tasks_pending_.pop_front(); |
1536 request_task->Run(); | 1152 std::move(request_task).Run(); |
1537 } | 1153 } |
1538 | 1154 |
1539 DCHECK(tasks_pending_for_key_.empty()); | 1155 DCHECK(tasks_pending_for_key_.empty()); |
1540 | 1156 |
1541 finished_fetching_all_cookies_ = true; | 1157 finished_fetching_all_cookies_ = true; |
1542 creation_times_.clear(); | 1158 creation_times_.clear(); |
1543 keys_loaded_.clear(); | 1159 keys_loaded_.clear(); |
1544 } | 1160 } |
1545 | 1161 |
1546 void CookieMonster::EnsureCookiesMapIsValid() { | 1162 void CookieMonster::EnsureCookiesMapIsValid() { |
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1873 // We assume that hopefully setting a cookie will be less common than | 1489 // 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, | 1490 // 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 | 1491 // 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, | 1492 // 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(). | 1493 // and we will purge the expired cookies in GetCookies(). |
1878 GarbageCollect(creation_date, key); | 1494 GarbageCollect(creation_date, key); |
1879 | 1495 |
1880 return true; | 1496 return true; |
1881 } | 1497 } |
1882 | 1498 |
1883 bool CookieMonster::SetAllCookies(const CookieList& list) { | 1499 bool CookieMonster::SetAllCookies(CookieList list) { |
1884 DCHECK(thread_checker_.CalledOnValidThread()); | 1500 DCHECK(thread_checker_.CalledOnValidThread()); |
1501 CookieList positive_diff; | |
1502 CookieList negative_diff; | |
1503 CookieList old_cookies = GetAllCookies(); | |
1504 ComputeCookieDiff(&old_cookies, &list, &positive_diff, &negative_diff); | |
1505 | |
1506 for (CookieList::const_iterator it = negative_diff.begin(); | |
1507 it != negative_diff.end(); ++it) { | |
1508 DeleteCanonicalCookie(*it); | |
1509 } | |
1510 | |
1511 if (positive_diff.size() == 0) | |
1512 return true; | |
1513 | |
1885 for (const auto& cookie : list) { | 1514 for (const auto& cookie : list) { |
Randy Smith (Not in Mondays)
2017/07/08 15:05:22
I'll note that using |list| rather than |positive_
| |
1886 const std::string key(GetKey(cookie.Domain())); | 1515 const std::string key(GetKey(cookie.Domain())); |
1887 Time creation_time = cookie.CreationDate(); | 1516 Time creation_time = cookie.CreationDate(); |
1888 bool already_expired = cookie.IsExpired(creation_time); | 1517 bool already_expired = cookie.IsExpired(creation_time); |
1889 | 1518 |
1890 bool result = | 1519 bool result = |
1891 DeleteAnyEquivalentCookie(key, cookie, true, false, already_expired); | 1520 DeleteAnyEquivalentCookie(key, cookie, true, false, already_expired); |
1892 DCHECK(!result); | 1521 DCHECK(!result); |
1893 | 1522 |
1894 if (already_expired) | 1523 if (already_expired) |
1895 continue; | 1524 continue; |
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2380 } | 2009 } |
2381 | 2010 |
2382 // The system resolution is not high enough, so we can have multiple | 2011 // The system resolution is not high enough, so we can have multiple |
2383 // set cookies that result in the same system time. When this happens, we | 2012 // set cookies that result in the same system time. When this happens, we |
2384 // increment by one Time unit. Let's hope computers don't get too fast. | 2013 // increment by one Time unit. Let's hope computers don't get too fast. |
2385 Time CookieMonster::CurrentTime() { | 2014 Time CookieMonster::CurrentTime() { |
2386 return std::max(Time::Now(), Time::FromInternalValue( | 2015 return std::max(Time::Now(), Time::FromInternalValue( |
2387 last_time_seen_.ToInternalValue() + 1)); | 2016 last_time_seen_.ToInternalValue() + 1)); |
2388 } | 2017 } |
2389 | 2018 |
2390 void CookieMonster::DoCookieTask( | 2019 void CookieMonster::DoCookieCallback(base::OnceClosure callback) { |
2391 const scoped_refptr<CookieMonsterTask>& task_item) { | |
2392 DCHECK(thread_checker_.CalledOnValidThread()); | 2020 DCHECK(thread_checker_.CalledOnValidThread()); |
2393 | 2021 |
2394 MarkCookieStoreAsInitialized(); | 2022 MarkCookieStoreAsInitialized(); |
2395 FetchAllCookiesIfNecessary(); | 2023 FetchAllCookiesIfNecessary(); |
2396 seen_global_task_ = true; | 2024 seen_global_task_ = true; |
2397 | 2025 |
2398 if (!finished_fetching_all_cookies_ && store_.get()) { | 2026 if (!finished_fetching_all_cookies_ && store_.get()) { |
2399 tasks_pending_.push_back(task_item); | 2027 tasks_pending_.push_back(std::move(callback)); |
2400 return; | 2028 return; |
2401 } | 2029 } |
2402 | 2030 |
2403 task_item->Run(); | 2031 std::move(callback).Run(); |
2404 } | 2032 } |
2405 | 2033 |
2406 void CookieMonster::DoCookieTaskForURL( | 2034 void CookieMonster::DoCookieCallbackForURL(base::OnceClosure callback, |
2407 const scoped_refptr<CookieMonsterTask>& task_item, | 2035 const GURL& url) { |
2408 const GURL& url) { | |
2409 MarkCookieStoreAsInitialized(); | 2036 MarkCookieStoreAsInitialized(); |
2410 if (ShouldFetchAllCookiesWhenFetchingAnyCookie()) | 2037 if (ShouldFetchAllCookiesWhenFetchingAnyCookie()) |
2411 FetchAllCookiesIfNecessary(); | 2038 FetchAllCookiesIfNecessary(); |
2412 | 2039 |
2413 // If cookies for the requested domain key (eTLD+1) have been loaded from DB | 2040 // If cookies for the requested domain key (eTLD+1) have been loaded from DB |
2414 // then run the task, otherwise load from DB. | 2041 // then run the task, otherwise load from DB. |
2415 if (!finished_fetching_all_cookies_ && store_.get()) { | 2042 if (!finished_fetching_all_cookies_ && store_.get()) { |
2416 // If a global task has been previously seen, queue the task as a global | 2043 // If a global task has been previously seen, queue the task as a global |
2417 // task. Note that the CookieMonster may be in the middle of executing | 2044 // task. Note that the CookieMonster may be in the middle of executing |
2418 // the global queue, |tasks_pending_| may be empty, which is why another | 2045 // the global queue, |tasks_pending_| may be empty, which is why another |
2419 // bool is needed. | 2046 // bool is needed. |
2420 if (seen_global_task_) { | 2047 if (seen_global_task_) { |
2421 tasks_pending_.push_back(task_item); | 2048 tasks_pending_.push_back(std::move(callback)); |
2422 return; | 2049 return; |
2423 } | 2050 } |
2424 | 2051 |
2425 // Checks if the domain key has been loaded. | 2052 // Checks if the domain key has been loaded. |
2426 std::string key(cookie_util::GetEffectiveDomain(url.scheme(), url.host())); | 2053 std::string key(cookie_util::GetEffectiveDomain(url.scheme(), url.host())); |
2427 if (keys_loaded_.find(key) == keys_loaded_.end()) { | 2054 if (keys_loaded_.find(key) == keys_loaded_.end()) { |
2428 std::map<std::string, | 2055 std::map<std::string, std::deque<base::OnceClosure>>::iterator it = |
2429 std::deque<scoped_refptr<CookieMonsterTask>>>::iterator it = | |
2430 tasks_pending_for_key_.find(key); | 2056 tasks_pending_for_key_.find(key); |
2431 if (it == tasks_pending_for_key_.end()) { | 2057 if (it == tasks_pending_for_key_.end()) { |
2432 store_->LoadCookiesForKey( | 2058 store_->LoadCookiesForKey( |
2433 key, base::Bind(&CookieMonster::OnKeyLoaded, | 2059 key, base::Bind(&CookieMonster::OnKeyLoaded, |
2434 weak_ptr_factory_.GetWeakPtr(), key)); | 2060 weak_ptr_factory_.GetWeakPtr(), key)); |
2435 it = tasks_pending_for_key_ | 2061 it = tasks_pending_for_key_ |
2436 .insert(std::make_pair( | 2062 .insert(std::make_pair(key, std::deque<base::OnceClosure>())) |
2437 key, std::deque<scoped_refptr<CookieMonsterTask>>())) | |
2438 .first; | 2063 .first; |
2439 } | 2064 } |
2440 it->second.push_back(task_item); | 2065 it->second.push_back(std::move(callback)); |
2441 return; | 2066 return; |
2442 } | 2067 } |
2443 } | 2068 } |
2444 | 2069 |
2445 task_item->Run(); | 2070 std::move(callback).Run(); |
2446 } | 2071 } |
2447 | 2072 |
2448 void CookieMonster::ComputeCookieDiff(CookieList* old_cookies, | 2073 void CookieMonster::ComputeCookieDiff(CookieList* old_cookies, |
2449 CookieList* new_cookies, | 2074 CookieList* new_cookies, |
2450 CookieList* cookies_to_add, | 2075 CookieList* cookies_to_add, |
2451 CookieList* cookies_to_delete) { | 2076 CookieList* cookies_to_delete) { |
2452 DCHECK(thread_checker_.CalledOnValidThread()); | 2077 DCHECK(thread_checker_.CalledOnValidThread()); |
2453 | 2078 |
2454 DCHECK(old_cookies); | 2079 DCHECK(old_cookies); |
2455 DCHECK(new_cookies); | 2080 DCHECK(new_cookies); |
(...skipping 17 matching lines...) Expand all Loading... | |
2473 PartialDiffCookieSorter); | 2098 PartialDiffCookieSorter); |
2474 | 2099 |
2475 // Select any new cookie for addition (or update) if no old cookie is exactly | 2100 // Select any new cookie for addition (or update) if no old cookie is exactly |
2476 // equivalent. | 2101 // equivalent. |
2477 std::set_difference(new_cookies->begin(), new_cookies->end(), | 2102 std::set_difference(new_cookies->begin(), new_cookies->end(), |
2478 old_cookies->begin(), old_cookies->end(), | 2103 old_cookies->begin(), old_cookies->end(), |
2479 std::inserter(*cookies_to_add, cookies_to_add->begin()), | 2104 std::inserter(*cookies_to_add, cookies_to_add->begin()), |
2480 FullDiffCookieSorter); | 2105 FullDiffCookieSorter); |
2481 } | 2106 } |
2482 | 2107 |
2483 void CookieMonster::RunCallback(base::OnceClosure callback) { | |
2484 DCHECK(thread_checker_.CalledOnValidThread()); | |
2485 std::move(callback).Run(); | |
2486 } | |
2487 | |
2488 void CookieMonster::RunCookieChangedCallbacks(const CanonicalCookie& cookie, | 2108 void CookieMonster::RunCookieChangedCallbacks(const CanonicalCookie& cookie, |
2489 ChangeCause cause) { | 2109 ChangeCause cause) { |
2490 DCHECK(thread_checker_.CalledOnValidThread()); | 2110 DCHECK(thread_checker_.CalledOnValidThread()); |
2491 | 2111 |
2492 CookieOptions opts; | 2112 CookieOptions opts; |
2493 opts.set_include_httponly(); | 2113 opts.set_include_httponly(); |
2494 opts.set_same_site_cookie_mode( | 2114 opts.set_same_site_cookie_mode( |
2495 CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX); | 2115 CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX); |
2496 // Note that the callbacks in hook_map_ are wrapped with RunAsync(), so they | 2116 // Note that the callbacks in hook_map_ are wrapped with RunAsync(), so they |
2497 // are guaranteed to not take long - they just post a RunAsync task back to | 2117 // are guaranteed to not take long - they just post a RunAsync task back to |
2498 // the appropriate thread's message loop and return. | 2118 // the appropriate thread's message loop and return. |
2499 // TODO(mmenke): Consider running these synchronously? | 2119 // TODO(mmenke): Consider running these synchronously? |
2500 for (CookieChangedHookMap::iterator it = hook_map_.begin(); | 2120 for (CookieChangedHookMap::iterator it = hook_map_.begin(); |
2501 it != hook_map_.end(); ++it) { | 2121 it != hook_map_.end(); ++it) { |
2502 std::pair<GURL, std::string> key = it->first; | 2122 std::pair<GURL, std::string> key = it->first; |
2503 if (cookie.IncludeForRequestURL(key.first, opts) && | 2123 if (cookie.IncludeForRequestURL(key.first, opts) && |
2504 cookie.Name() == key.second) { | 2124 cookie.Name() == key.second) { |
2505 it->second->Notify(cookie, cause); | 2125 it->second->Notify(cookie, cause); |
2506 } | 2126 } |
2507 } | 2127 } |
2508 } | 2128 } |
2509 | 2129 |
2510 } // namespace net | 2130 } // namespace net |
OLD | NEW |