blob: 54b00b565d700b6a1dd7ccca259dbc5edf973f3d [file] [log] [blame]
[email protected]63ee33bd2012-03-15 09:29:581// 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]63ee33bd2012-03-15 09:29:587
8#include <string>
9#include <vector>
10
Maks Orlovich36ebc4c2019-03-20 01:57:5611#include "base/bind.h"
Alexander Timin0439ffe2018-10-30 18:10:0012#include "base/memory/scoped_refptr.h"
xunjielib2bec812016-01-27 23:08:0913#include "base/run_loop.h"
Alexander Timin0439ffe2018-10-30 18:10:0014#include "base/single_thread_task_runner.h"
mmenke9fa44f2d2016-01-22 23:36:3915#include "net/cookies/canonical_cookie.h"
Lily Chene2e9ae012019-10-09 20:02:5416#include "net/cookies/cookie_constants.h"
[email protected]63ee33bd2012-03-15 09:29:5817#include "net/cookies/cookie_store.h"
18
[email protected]63ee33bd2012-03-15 09:29:5819namespace base {
20class Thread;
21}
22
23namespace 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.
28class CookieCallback {
29 public:
xunjielib2bec812016-01-27 23:08:0930 // Waits until the callback is invoked.
31 void WaitUntilDone();
[email protected]63ee33bd2012-03-15 09:29:5832
Maks Orlovich36ebc4c2019-03-20 01:57:5633 // 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]63ee33bd2012-03-15 09:29:5837 protected:
xunjielib2bec812016-01-27 23:08:0938 // Constructs a callback that expects to be called in the given thread.
[email protected]63ee33bd2012-03-15 09:29:5839 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
xunjielib2bec812016-01-27 23:08:0945 ~CookieCallback();
46
[email protected]63ee33bd2012-03-15 09:29:5847 // 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 Orlovich36ebc4c2019-03-20 01:57:5652 void ValidateThread() const;
53
[email protected]63ee33bd2012-03-15 09:29:5854 base::Thread* run_in_thread_;
Alexander Timin0439ffe2018-10-30 18:10:0055 scoped_refptr<base::SingleThreadTaskRunner> run_in_task_runner_;
xunjielib2bec812016-01-27 23:08:0956 base::RunLoop loop_to_quit_;
Maks Orlovich36ebc4c2019-03-20 01:57:5657 bool was_run_;
[email protected]63ee33bd2012-03-15 09:29:5858};
59
60// Callback implementations for the asynchronous CookieStore methods.
61
[email protected]314bad2f2014-04-01 08:46:4462template <typename T>
63class ResultSavingCookieCallback : public CookieCallback {
[email protected]63ee33bd2012-03-15 09:29:5864 public:
[email protected]314bad2f2014-04-01 08:46:4465 ResultSavingCookieCallback() {
66 }
67 explicit ResultSavingCookieCallback(base::Thread* run_in_thread)
68 : CookieCallback(run_in_thread) {
69 }
[email protected]63ee33bd2012-03-15 09:29:5870
[email protected]314bad2f2014-04-01 08:46:4471 void Run(T result) {
[email protected]63ee33bd2012-03-15 09:29:5872 result_ = result;
73 CallbackEpilogue();
74 }
75
Maks Orlovich36ebc4c2019-03-20 01:57:5676 // 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]314bad2f2014-04-01 08:46:4483 const T& result() { return result_; }
[email protected]63ee33bd2012-03-15 09:29:5884
85 private:
[email protected]314bad2f2014-04-01 08:46:4486 T result_;
[email protected]63ee33bd2012-03-15 09:29:5887};
88
[email protected]f6c588232013-05-22 12:43:4089class NoResultCookieCallback : public CookieCallback {
[email protected]63ee33bd2012-03-15 09:29:5890 public:
[email protected]f6c588232013-05-22 12:43:4091 NoResultCookieCallback();
92 explicit NoResultCookieCallback(base::Thread* run_in_thread);
[email protected]63ee33bd2012-03-15 09:29:5893
Maks Orlovichf9d0a99a2019-04-29 18:38:2294 // 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]63ee33bd2012-03-15 09:29:58100 void Run() {
101 CallbackEpilogue();
102 }
103};
104
mmenke9fa44f2d2016-01-22 23:36:39105class GetCookieListCallback : public CookieCallback {
106 public:
107 GetCookieListCallback();
108 explicit GetCookieListCallback(base::Thread* run_in_thread);
109
110 ~GetCookieListCallback();
111
Lily Chenf068a762019-08-21 21:10:50112 void Run(const CookieStatusList& cookies,
113 const CookieStatusList& excluded_cookies);
mmenke9fa44f2d2016-01-22 23:36:39114
Maks Orlovich36ebc4c2019-03-20 01:57:56115 // Makes a callback that will invoke Run. Assumes that |this| will be kept
116 // alive till the time the callback is used.
Lily Chenf068a762019-08-21 21:10:50117 base::OnceCallback<void(const CookieStatusList&, const CookieStatusList&)>
Maks Orlovich36ebc4c2019-03-20 01:57:56118 MakeCallback() {
119 return base::BindOnce(&GetCookieListCallback::Run, base::Unretained(this));
120 }
121
mmenke9fa44f2d2016-01-22 23:36:39122 const CookieList& cookies() { return cookies_; }
Lily Chenf068a762019-08-21 21:10:50123 const CookieStatusList& cookies_with_statuses() {
124 return cookies_with_statuses_;
125 }
Aaron Tagliaboschi29033b7f2019-01-31 19:58:20126 const CookieStatusList& excluded_cookies() { return excluded_cookies_; }
mmenke9fa44f2d2016-01-22 23:36:39127
128 private:
129 CookieList cookies_;
Lily Chenf068a762019-08-21 21:10:50130 CookieStatusList cookies_with_statuses_;
Aaron Tagliaboschi29033b7f2019-01-31 19:58:20131 CookieStatusList excluded_cookies_;
mmenke9fa44f2d2016-01-22 23:36:39132};
133
Lily Chenf068a762019-08-21 21:10:50134class 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 Chene2e9ae012019-10-09 20:02:54155class 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]63ee33bd2012-03-15 09:29:58185} // namespace net
186
187#endif // NET_COOKIES_COOKIE_STORE_TEST_CALLBACKS_H_