xref: /aosp_15_r20/external/cronet/net/cookies/cookie_store_test_callbacks.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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_
7 
8 #include <vector>
9 
10 #include "base/functional/bind.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/memory/scoped_refptr.h"
13 #include "base/run_loop.h"
14 #include "base/task/single_thread_task_runner.h"
15 #include "net/cookies/canonical_cookie.h"
16 #include "net/cookies/cookie_constants.h"
17 #include "net/cookies/cookie_store.h"
18 
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:
30   // Waits until the callback is invoked.
31   void WaitUntilDone();
32 
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 
37  protected:
38   // Constructs a callback that expects to be called in the given thread.
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 
45   ~CookieCallback();
46 
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:
52   void ValidateThread() const;
53 
54   raw_ptr<base::Thread> run_in_thread_;
55   scoped_refptr<base::SingleThreadTaskRunner> run_in_task_runner_;
56   base::RunLoop loop_to_quit_;
57   bool was_run_ = false;
58 };
59 
60 // Callback implementations for the asynchronous CookieStore methods.
61 
62 template <typename T>
63 class ResultSavingCookieCallback : public CookieCallback {
64  public:
65   ResultSavingCookieCallback() = default;
ResultSavingCookieCallback(base::Thread * run_in_thread)66   explicit ResultSavingCookieCallback(base::Thread* run_in_thread)
67       : CookieCallback(run_in_thread) {
68   }
69 
Run(T result)70   void Run(T result) {
71     result_ = result;
72     CallbackEpilogue();
73   }
74 
75   // Makes a callback that will invoke Run. Assumes that |this| will be kept
76   // alive till the time the callback is used.
MakeCallback()77   base::OnceCallback<void(T)> MakeCallback() {
78     return base::BindOnce(&ResultSavingCookieCallback<T>::Run,
79                           base::Unretained(this));
80   }
81 
result()82   const T& result() { return result_; }
83 
84  private:
85   T result_;
86 };
87 
88 class NoResultCookieCallback : public CookieCallback {
89  public:
90   NoResultCookieCallback();
91   explicit NoResultCookieCallback(base::Thread* run_in_thread);
92 
93   // Makes a callback that will invoke Run. Assumes that |this| will be kept
94   // alive till the time the callback is used.
MakeCallback()95   base::OnceCallback<void()> MakeCallback() {
96     return base::BindOnce(&NoResultCookieCallback::Run, base::Unretained(this));
97   }
98 
Run()99   void Run() {
100     CallbackEpilogue();
101   }
102 };
103 
104 class GetCookieListCallback : public CookieCallback {
105  public:
106   GetCookieListCallback();
107   explicit GetCookieListCallback(base::Thread* run_in_thread);
108 
109   ~GetCookieListCallback();
110 
111   void Run(const CookieAccessResultList& cookies,
112            const CookieAccessResultList& excluded_cookies);
113 
114   // Makes a callback that will invoke Run. Assumes that |this| will be kept
115   // alive till the time the callback is used.
116   base::OnceCallback<void(const CookieAccessResultList&,
117                           const CookieAccessResultList&)>
MakeCallback()118   MakeCallback() {
119     return base::BindOnce(&GetCookieListCallback::Run, base::Unretained(this));
120   }
121 
cookies()122   const CookieList& cookies() { return cookies_; }
cookies_with_access_results()123   const CookieAccessResultList& cookies_with_access_results() {
124     return cookies_with_access_results_;
125   }
excluded_cookies()126   const CookieAccessResultList& excluded_cookies() { return excluded_cookies_; }
127 
128  private:
129   CookieList cookies_;
130   CookieAccessResultList cookies_with_access_results_;
131   CookieAccessResultList excluded_cookies_;
132 };
133 
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.
MakeCallback()145   base::OnceCallback<void(const CookieList&)> MakeCallback() {
146     return base::BindOnce(&GetAllCookiesCallback::Run, base::Unretained(this));
147   }
148 
cookies()149   const CookieList& cookies() { return cookies_; }
150 
151  private:
152   CookieList cookies_;
153 };
154 
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>&)>
MakeCallback()170   MakeCallback() {
171     return base::BindOnce(&GetAllCookiesWithAccessSemanticsCallback::Run,
172                           base::Unretained(this));
173   }
174 
cookies()175   const CookieList& cookies() { return cookies_; }
access_semantics_list()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 
185 }  // namespace net
186 
187 #endif  // NET_COOKIES_COOKIE_STORE_TEST_CALLBACKS_H_
188