[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_COOKIES_COOKIE_STORE_TEST_CALLBACKS_H_ |
| 6 | #define NET_COOKIES_COOKIE_STORE_TEST_CALLBACKS_H_ |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Maks Orlovich | 36ebc4c | 2019-03-20 01:57:56 | [diff] [blame] | 11 | #include "base/bind.h" |
Alexander Timin | 0439ffe | 2018-10-30 18:10:00 | [diff] [blame] | 12 | #include "base/memory/scoped_refptr.h" |
xunjieli | b2bec81 | 2016-01-27 23:08:09 | [diff] [blame] | 13 | #include "base/run_loop.h" |
Alexander Timin | 0439ffe | 2018-10-30 18:10:00 | [diff] [blame] | 14 | #include "base/single_thread_task_runner.h" |
mmenke | 9fa44f2d | 2016-01-22 23:36:39 | [diff] [blame] | 15 | #include "net/cookies/canonical_cookie.h" |
Lily Chen | e2e9ae01 | 2019-10-09 20:02:54 | [diff] [blame] | 16 | #include "net/cookies/cookie_constants.h" |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 17 | #include "net/cookies/cookie_store.h" |
| 18 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 19 | namespace base { |
| 20 | class Thread; |
| 21 | } |
| 22 | |
| 23 | namespace net { |
| 24 | |
| 25 | // Defines common behaviour for the callbacks from GetCookies, SetCookies, etc. |
| 26 | // Asserts that the current thread is the expected invocation thread, sends a |
| 27 | // quit to the thread in which it was constructed. |
| 28 | class CookieCallback { |
| 29 | public: |
xunjieli | b2bec81 | 2016-01-27 23:08:09 | [diff] [blame] | 30 | // Waits until the callback is invoked. |
| 31 | void WaitUntilDone(); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 32 | |
Maks Orlovich | 36ebc4c | 2019-03-20 01:57:56 | [diff] [blame] | 33 | // Returns whether the callback was invoked. Should only be used on the thread |
| 34 | // the callback runs on. |
| 35 | bool was_run() const; |
| 36 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 37 | protected: |
xunjieli | b2bec81 | 2016-01-27 23:08:09 | [diff] [blame] | 38 | // Constructs a callback that expects to be called in the given thread. |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 39 | explicit CookieCallback(base::Thread* run_in_thread); |
| 40 | |
| 41 | // Constructs a callback that expects to be called in current thread and will |
| 42 | // send a QUIT to the constructing thread. |
| 43 | CookieCallback(); |
| 44 | |
xunjieli | b2bec81 | 2016-01-27 23:08:09 | [diff] [blame] | 45 | ~CookieCallback(); |
| 46 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 47 | // Tests whether the current thread was the caller's thread. |
| 48 | // Sends a QUIT to the constructing thread. |
| 49 | void CallbackEpilogue(); |
| 50 | |
| 51 | private: |
Maks Orlovich | 36ebc4c | 2019-03-20 01:57:56 | [diff] [blame] | 52 | void ValidateThread() const; |
| 53 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 54 | base::Thread* run_in_thread_; |
Alexander Timin | 0439ffe | 2018-10-30 18:10:00 | [diff] [blame] | 55 | scoped_refptr<base::SingleThreadTaskRunner> run_in_task_runner_; |
xunjieli | b2bec81 | 2016-01-27 23:08:09 | [diff] [blame] | 56 | base::RunLoop loop_to_quit_; |
Maks Orlovich | 36ebc4c | 2019-03-20 01:57:56 | [diff] [blame] | 57 | bool was_run_; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | // Callback implementations for the asynchronous CookieStore methods. |
| 61 | |
[email protected] | 314bad2f | 2014-04-01 08:46:44 | [diff] [blame] | 62 | template <typename T> |
| 63 | class ResultSavingCookieCallback : public CookieCallback { |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 64 | public: |
[email protected] | 314bad2f | 2014-04-01 08:46:44 | [diff] [blame] | 65 | ResultSavingCookieCallback() { |
| 66 | } |
| 67 | explicit ResultSavingCookieCallback(base::Thread* run_in_thread) |
| 68 | : CookieCallback(run_in_thread) { |
| 69 | } |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 70 | |
[email protected] | 314bad2f | 2014-04-01 08:46:44 | [diff] [blame] | 71 | void Run(T result) { |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 72 | result_ = result; |
| 73 | CallbackEpilogue(); |
| 74 | } |
| 75 | |
Maks Orlovich | 36ebc4c | 2019-03-20 01:57:56 | [diff] [blame] | 76 | // Makes a callback that will invoke Run. Assumes that |this| will be kept |
| 77 | // alive till the time the callback is used. |
| 78 | base::OnceCallback<void(T)> MakeCallback() { |
| 79 | return base::BindOnce(&ResultSavingCookieCallback<T>::Run, |
| 80 | base::Unretained(this)); |
| 81 | } |
| 82 | |
[email protected] | 314bad2f | 2014-04-01 08:46:44 | [diff] [blame] | 83 | const T& result() { return result_; } |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 84 | |
| 85 | private: |
[email protected] | 314bad2f | 2014-04-01 08:46:44 | [diff] [blame] | 86 | T result_; |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 87 | }; |
| 88 | |
[email protected] | f6c58823 | 2013-05-22 12:43:40 | [diff] [blame] | 89 | class NoResultCookieCallback : public CookieCallback { |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 90 | public: |
[email protected] | f6c58823 | 2013-05-22 12:43:40 | [diff] [blame] | 91 | NoResultCookieCallback(); |
| 92 | explicit NoResultCookieCallback(base::Thread* run_in_thread); |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 93 | |
Maks Orlovich | f9d0a99a | 2019-04-29 18:38:22 | [diff] [blame] | 94 | // Makes a callback that will invoke Run. Assumes that |this| will be kept |
| 95 | // alive till the time the callback is used. |
| 96 | base::OnceCallback<void()> MakeCallback() { |
| 97 | return base::BindOnce(&NoResultCookieCallback::Run, base::Unretained(this)); |
| 98 | } |
| 99 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 100 | void Run() { |
| 101 | CallbackEpilogue(); |
| 102 | } |
| 103 | }; |
| 104 | |
mmenke | 9fa44f2d | 2016-01-22 23:36:39 | [diff] [blame] | 105 | class GetCookieListCallback : public CookieCallback { |
| 106 | public: |
| 107 | GetCookieListCallback(); |
| 108 | explicit GetCookieListCallback(base::Thread* run_in_thread); |
| 109 | |
| 110 | ~GetCookieListCallback(); |
| 111 | |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 112 | void Run(const CookieStatusList& cookies, |
| 113 | const CookieStatusList& excluded_cookies); |
mmenke | 9fa44f2d | 2016-01-22 23:36:39 | [diff] [blame] | 114 | |
Maks Orlovich | 36ebc4c | 2019-03-20 01:57:56 | [diff] [blame] | 115 | // Makes a callback that will invoke Run. Assumes that |this| will be kept |
| 116 | // alive till the time the callback is used. |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 117 | base::OnceCallback<void(const CookieStatusList&, const CookieStatusList&)> |
Maks Orlovich | 36ebc4c | 2019-03-20 01:57:56 | [diff] [blame] | 118 | MakeCallback() { |
| 119 | return base::BindOnce(&GetCookieListCallback::Run, base::Unretained(this)); |
| 120 | } |
| 121 | |
mmenke | 9fa44f2d | 2016-01-22 23:36:39 | [diff] [blame] | 122 | const CookieList& cookies() { return cookies_; } |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 123 | const CookieStatusList& cookies_with_statuses() { |
| 124 | return cookies_with_statuses_; |
| 125 | } |
Aaron Tagliaboschi | 29033b7f | 2019-01-31 19:58:20 | [diff] [blame] | 126 | const CookieStatusList& excluded_cookies() { return excluded_cookies_; } |
mmenke | 9fa44f2d | 2016-01-22 23:36:39 | [diff] [blame] | 127 | |
| 128 | private: |
| 129 | CookieList cookies_; |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 130 | CookieStatusList cookies_with_statuses_; |
Aaron Tagliaboschi | 29033b7f | 2019-01-31 19:58:20 | [diff] [blame] | 131 | CookieStatusList excluded_cookies_; |
mmenke | 9fa44f2d | 2016-01-22 23:36:39 | [diff] [blame] | 132 | }; |
| 133 | |
Lily Chen | f068a76 | 2019-08-21 21:10:50 | [diff] [blame] | 134 | class GetAllCookiesCallback : public CookieCallback { |
| 135 | public: |
| 136 | GetAllCookiesCallback(); |
| 137 | explicit GetAllCookiesCallback(base::Thread* run_in_thread); |
| 138 | |
| 139 | ~GetAllCookiesCallback(); |
| 140 | |
| 141 | void Run(const CookieList& cookies); |
| 142 | |
| 143 | // Makes a callback that will invoke Run. Assumes that |this| will be kept |
| 144 | // alive till the time the callback is used. |
| 145 | base::OnceCallback<void(const CookieList&)> MakeCallback() { |
| 146 | return base::BindOnce(&GetAllCookiesCallback::Run, base::Unretained(this)); |
| 147 | } |
| 148 | |
| 149 | const CookieList& cookies() { return cookies_; } |
| 150 | |
| 151 | private: |
| 152 | CookieList cookies_; |
| 153 | }; |
| 154 | |
Lily Chen | e2e9ae01 | 2019-10-09 20:02:54 | [diff] [blame] | 155 | class GetAllCookiesWithAccessSemanticsCallback : public CookieCallback { |
| 156 | public: |
| 157 | GetAllCookiesWithAccessSemanticsCallback(); |
| 158 | explicit GetAllCookiesWithAccessSemanticsCallback( |
| 159 | base::Thread* run_in_thread); |
| 160 | |
| 161 | ~GetAllCookiesWithAccessSemanticsCallback(); |
| 162 | |
| 163 | void Run(const CookieList& cookies, |
| 164 | const std::vector<CookieAccessSemantics>& access_semantics_list); |
| 165 | |
| 166 | // Makes a callback that will invoke Run. Assumes that |this| will be kept |
| 167 | // alive till the time the callback is used. |
| 168 | base::OnceCallback<void(const CookieList&, |
| 169 | const std::vector<CookieAccessSemantics>&)> |
| 170 | MakeCallback() { |
| 171 | return base::BindOnce(&GetAllCookiesWithAccessSemanticsCallback::Run, |
| 172 | base::Unretained(this)); |
| 173 | } |
| 174 | |
| 175 | const CookieList& cookies() { return cookies_; } |
| 176 | const std::vector<CookieAccessSemantics>& access_semantics_list() { |
| 177 | return access_semantics_list_; |
| 178 | } |
| 179 | |
| 180 | private: |
| 181 | CookieList cookies_; |
| 182 | std::vector<CookieAccessSemantics> access_semantics_list_; |
| 183 | }; |
| 184 | |
[email protected] | 63ee33bd | 2012-03-15 09:29:58 | [diff] [blame] | 185 | } // namespace net |
| 186 | |
| 187 | #endif // NET_COOKIES_COOKIE_STORE_TEST_CALLBACKS_H_ |