chore: add static promise resolution helpers (#17223)

* chore: add static promise resolution helpers

* address feedback

* address feedback from review
This commit is contained in:
Shelley Vohr 2019-03-07 18:07:04 -08:00 коммит произвёл Samuel Attard
Родитель 61fc95417b
Коммит a4418a9014
2 изменённых файлов: 91 добавлений и 35 удалений

Просмотреть файл

@ -136,26 +136,6 @@ inline net::CookieStore* GetCookieStore(
return getter->GetURLRequestContext()->cookie_store();
}
void ResolvePromiseWithCookies(util::Promise promise,
const net::CookieList& cookie_list) {
promise.Resolve(cookie_list);
}
void ResolvePromise(util::Promise promise) {
promise.Resolve();
}
// Resolve |promise| in UI thread.
void ResolvePromiseInUI(util::Promise promise) {
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI},
base::BindOnce(ResolvePromise, std::move(promise)));
}
// Run |callback| on UI thread.
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,
util::Promise promise,
@ -168,7 +148,8 @@ void FilterCookies(std::unique_ptr<base::DictionaryValue> filter,
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(ResolvePromiseWithCookies, std::move(promise), result));
base::BindOnce(util::Promise::ResolvePromise<net::CookieList>,
std::move(promise), std::move(result)));
}
// Receives cookies matching |filter| in IO thread.
@ -195,23 +176,22 @@ void RemoveCookieOnIO(scoped_refptr<net::URLRequestContextGetter> getter,
const std::string& name,
util::Promise promise) {
GetCookieStore(getter)->DeleteCookieAsync(
url, name, base::BindOnce(ResolvePromiseInUI, std::move(promise)));
}
// Resolves/rejects the |promise| in UI thread.
void SettlePromiseInUI(util::Promise promise, const std::string& errmsg) {
if (errmsg.empty()) {
promise.Resolve();
} else {
promise.RejectWithErrorMessage(errmsg);
}
url, name,
base::BindOnce(util::Promise::ResolveEmptyPromise, std::move(promise)));
}
// Callback of SetCookie.
void OnSetCookie(util::Promise promise, bool success) {
const std::string errmsg = success ? "" : "Setting cookie failed";
RunCallbackInUI(
base::BindOnce(SettlePromiseInUI, std::move(promise), errmsg));
if (success) {
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(util::Promise::ResolveEmptyPromise, std::move(promise)));
} else {
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(util::Promise::RejectPromise, std::move(promise),
"Setting cookie failed"));
}
}
// Flushes cookie store in IO thread.
@ -219,7 +199,7 @@ void FlushCookieStoreOnIOThread(
scoped_refptr<net::URLRequestContextGetter> getter,
util::Promise promise) {
GetCookieStore(getter)->FlushStore(
base::BindOnce(ResolvePromiseInUI, std::move(promise)));
base::BindOnce(util::Promise::ResolveEmptyPromise, std::move(promise)));
}
// Sets cookie with |details| in IO thread.

Просмотреть файл

@ -6,9 +6,12 @@
#define ATOM_COMMON_PROMISE_UTIL_H_
#include <string>
#include <utility>
#include "atom/common/api/locker.h"
#include "atom/common/native_mate_converters/callback.h"
#include "base/task/post_task.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "native_mate/converter.h"
@ -39,6 +42,45 @@ class Promise {
return v8::Local<v8::Context>::New(isolate_, context_);
}
// helpers for promise resolution and rejection
template <typename T>
static void ResolvePromise(Promise promise, T result) {
if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(
[](Promise promise, T result) { promise.Resolve(result); },
std::move(promise), std::move(result)));
} else {
promise.Resolve(result);
}
}
static void ResolveEmptyPromise(Promise promise) {
if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce([](Promise promise) { promise.Resolve(); },
std::move(promise)));
} else {
promise.Resolve();
}
}
static void RejectPromise(Promise promise, std::string errmsg) {
if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(
[](Promise promise, std::string errmsg) {
promise.RejectWithErrorMessage(errmsg);
},
std::move(promise), std::move(errmsg)));
} else {
promise.RejectWithErrorMessage(errmsg);
}
}
v8::Local<v8::Promise> GetHandle() const;
v8::Maybe<bool> Resolve() {
@ -125,6 +167,40 @@ class CopyablePromise {
CopyablePromise(const CopyablePromise&);
~CopyablePromise();
template <typename T>
static void ResolveCopyablePromise(const CopyablePromise& promise, T result) {
if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(Promise::ResolvePromise, promise.GetPromise(),
std::move(result)));
} else {
promise.GetPromise().Resolve(result);
}
}
static void ResolveEmptyCopyablePromise(const CopyablePromise& promise) {
if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(Promise::ResolveEmptyPromise, promise.GetPromise()));
} else {
promise.GetPromise().Resolve();
}
}
static void RejectCopyablePromise(const CopyablePromise& promise,
std::string errmsg) {
if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
base::PostTaskWithTraits(
FROM_HERE, {content::BrowserThread::UI},
base::BindOnce(Promise::RejectPromise, promise.GetPromise(),
std::move(errmsg)));
} else {
promise.GetPromise().RejectWithErrorMessage(errmsg);
}
}
Promise GetPromise() const;
private: