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