chore: make util::Promise a move-only type (#17071)
This commit is contained in:
parent
a40d826b11
commit
32a4de4a68
29 changed files with 325 additions and 260 deletions
|
@ -136,29 +136,29 @@ inline net::CookieStore* GetCookieStore(
|
|||
return getter->GetURLRequestContext()->cookie_store();
|
||||
}
|
||||
|
||||
void ResolvePromiseWithCookies(scoped_refptr<util::Promise> promise,
|
||||
net::CookieList cookieList) {
|
||||
promise->Resolve(cookieList);
|
||||
void ResolvePromiseWithCookies(util::Promise promise,
|
||||
const net::CookieList& cookie_list) {
|
||||
promise.Resolve(cookie_list);
|
||||
}
|
||||
|
||||
void ResolvePromise(scoped_refptr<util::Promise> promise) {
|
||||
promise->Resolve();
|
||||
void ResolvePromise(util::Promise promise) {
|
||||
promise.Resolve();
|
||||
}
|
||||
|
||||
// Resolve |promise| in UI thread.
|
||||
void ResolvePromiseInUI(scoped_refptr<util::Promise> promise) {
|
||||
void ResolvePromiseInUI(util::Promise promise) {
|
||||
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI},
|
||||
base::BindOnce(ResolvePromise, std::move(promise)));
|
||||
}
|
||||
|
||||
// Run |callback| on UI thread.
|
||||
void RunCallbackInUI(const base::Closure& callback) {
|
||||
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI}, callback);
|
||||
void RunCallbackInUI(base::OnceClosure callback) {
|
||||
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI}, std::move(callback));
|
||||
}
|
||||
|
||||
// Remove cookies from |list| not matching |filter|, and pass it to |callback|.
|
||||
void FilterCookies(std::unique_ptr<base::DictionaryValue> filter,
|
||||
scoped_refptr<util::Promise> promise,
|
||||
util::Promise promise,
|
||||
const net::CookieList& list) {
|
||||
net::CookieList result;
|
||||
for (const auto& cookie : list) {
|
||||
|
@ -174,50 +174,50 @@ void FilterCookies(std::unique_ptr<base::DictionaryValue> filter,
|
|||
// Receives cookies matching |filter| in IO thread.
|
||||
void GetCookiesOnIO(scoped_refptr<net::URLRequestContextGetter> getter,
|
||||
std::unique_ptr<base::DictionaryValue> filter,
|
||||
scoped_refptr<util::Promise> promise) {
|
||||
util::Promise promise) {
|
||||
std::string url;
|
||||
filter->GetString("url", &url);
|
||||
|
||||
auto filtered_callback =
|
||||
base::Bind(FilterCookies, base::Passed(&filter), std::move(promise));
|
||||
base::BindOnce(FilterCookies, std::move(filter), std::move(promise));
|
||||
|
||||
// Empty url will match all url cookies.
|
||||
if (url.empty())
|
||||
GetCookieStore(getter)->GetAllCookiesAsync(filtered_callback);
|
||||
GetCookieStore(getter)->GetAllCookiesAsync(std::move(filtered_callback));
|
||||
else
|
||||
GetCookieStore(getter)->GetAllCookiesForURLAsync(GURL(url),
|
||||
filtered_callback);
|
||||
GetCookieStore(getter)->GetAllCookiesForURLAsync(
|
||||
GURL(url), std::move(filtered_callback));
|
||||
}
|
||||
|
||||
// Removes cookie with |url| and |name| in IO thread.
|
||||
void RemoveCookieOnIO(scoped_refptr<net::URLRequestContextGetter> getter,
|
||||
const GURL& url,
|
||||
const std::string& name,
|
||||
scoped_refptr<util::Promise> promise) {
|
||||
util::Promise promise) {
|
||||
GetCookieStore(getter)->DeleteCookieAsync(
|
||||
url, name, base::BindOnce(ResolvePromiseInUI, std::move(promise)));
|
||||
}
|
||||
|
||||
// Resolves/rejects the |promise| in UI thread.
|
||||
void SettlePromiseInUI(scoped_refptr<util::Promise> promise,
|
||||
const std::string& errmsg) {
|
||||
void SettlePromiseInUI(util::Promise promise, const std::string& errmsg) {
|
||||
if (errmsg.empty()) {
|
||||
promise->Resolve();
|
||||
promise.Resolve();
|
||||
} else {
|
||||
promise->RejectWithErrorMessage(errmsg);
|
||||
promise.RejectWithErrorMessage(errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
// Callback of SetCookie.
|
||||
void OnSetCookie(scoped_refptr<util::Promise> promise, bool success) {
|
||||
void OnSetCookie(util::Promise promise, bool success) {
|
||||
const std::string errmsg = success ? "" : "Setting cookie failed";
|
||||
RunCallbackInUI(base::Bind(SettlePromiseInUI, std::move(promise), errmsg));
|
||||
RunCallbackInUI(
|
||||
base::BindOnce(SettlePromiseInUI, std::move(promise), errmsg));
|
||||
}
|
||||
|
||||
// Flushes cookie store in IO thread.
|
||||
void FlushCookieStoreOnIOThread(
|
||||
scoped_refptr<net::URLRequestContextGetter> getter,
|
||||
scoped_refptr<util::Promise> promise) {
|
||||
util::Promise promise) {
|
||||
GetCookieStore(getter)->FlushStore(
|
||||
base::BindOnce(ResolvePromiseInUI, std::move(promise)));
|
||||
}
|
||||
|
@ -225,7 +225,7 @@ void FlushCookieStoreOnIOThread(
|
|||
// Sets cookie with |details| in IO thread.
|
||||
void SetCookieOnIO(scoped_refptr<net::URLRequestContextGetter> getter,
|
||||
std::unique_ptr<base::DictionaryValue> details,
|
||||
scoped_refptr<util::Promise> promise) {
|
||||
util::Promise promise) {
|
||||
std::string url, name, value, domain, path;
|
||||
bool secure = false;
|
||||
bool http_only = false;
|
||||
|
@ -297,7 +297,8 @@ Cookies::Cookies(v8::Isolate* isolate, AtomBrowserContext* browser_context)
|
|||
Cookies::~Cookies() {}
|
||||
|
||||
v8::Local<v8::Promise> Cookies::Get(const base::DictionaryValue& filter) {
|
||||
scoped_refptr<util::Promise> promise = new util::Promise(isolate());
|
||||
util::Promise promise(isolate());
|
||||
v8::Local<v8::Promise> handle = promise.GetHandle();
|
||||
|
||||
auto copy = base::DictionaryValue::From(
|
||||
base::Value::ToUniquePtrValue(filter.Clone()));
|
||||
|
@ -305,26 +306,28 @@ v8::Local<v8::Promise> Cookies::Get(const base::DictionaryValue& filter) {
|
|||
base::PostTaskWithTraits(
|
||||
FROM_HERE, {BrowserThread::IO},
|
||||
base::BindOnce(GetCookiesOnIO, base::RetainedRef(getter), std::move(copy),
|
||||
promise));
|
||||
std::move(promise)));
|
||||
|
||||
return promise->GetHandle();
|
||||
return handle;
|
||||
}
|
||||
|
||||
v8::Local<v8::Promise> Cookies::Remove(const GURL& url,
|
||||
const std::string& name) {
|
||||
scoped_refptr<util::Promise> promise = new util::Promise(isolate());
|
||||
util::Promise promise(isolate());
|
||||
v8::Local<v8::Promise> handle = promise.GetHandle();
|
||||
|
||||
auto* getter = browser_context_->GetRequestContext();
|
||||
base::PostTaskWithTraits(
|
||||
FROM_HERE, {BrowserThread::IO},
|
||||
base::BindOnce(RemoveCookieOnIO, base::RetainedRef(getter), url, name,
|
||||
promise));
|
||||
std::move(promise)));
|
||||
|
||||
return promise->GetHandle();
|
||||
return handle;
|
||||
}
|
||||
|
||||
v8::Local<v8::Promise> Cookies::Set(const base::DictionaryValue& details) {
|
||||
scoped_refptr<util::Promise> promise = new util::Promise(isolate());
|
||||
util::Promise promise(isolate());
|
||||
v8::Local<v8::Promise> handle = promise.GetHandle();
|
||||
|
||||
auto copy = base::DictionaryValue::From(
|
||||
base::Value::ToUniquePtrValue(details.Clone()));
|
||||
|
@ -332,20 +335,22 @@ v8::Local<v8::Promise> Cookies::Set(const base::DictionaryValue& details) {
|
|||
base::PostTaskWithTraits(
|
||||
FROM_HERE, {BrowserThread::IO},
|
||||
base::BindOnce(SetCookieOnIO, base::RetainedRef(getter), std::move(copy),
|
||||
promise));
|
||||
std::move(promise)));
|
||||
|
||||
return promise->GetHandle();
|
||||
return handle;
|
||||
}
|
||||
|
||||
v8::Local<v8::Promise> Cookies::FlushStore() {
|
||||
scoped_refptr<util::Promise> promise = new util::Promise(isolate());
|
||||
util::Promise promise(isolate());
|
||||
v8::Local<v8::Promise> handle = promise.GetHandle();
|
||||
|
||||
auto* getter = browser_context_->GetRequestContext();
|
||||
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::IO},
|
||||
base::BindOnce(FlushCookieStoreOnIOThread,
|
||||
base::RetainedRef(getter), promise));
|
||||
base::PostTaskWithTraits(
|
||||
FROM_HERE, {BrowserThread::IO},
|
||||
base::BindOnce(FlushCookieStoreOnIOThread, base::RetainedRef(getter),
|
||||
std::move(promise)));
|
||||
|
||||
return promise->GetHandle();
|
||||
return handle;
|
||||
}
|
||||
|
||||
void Cookies::OnCookieChanged(const CookieDetails* details) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue