2018-06-27 21:06:08 +00:00
|
|
|
// Copyright (c) 2018 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:56:58 +00:00
|
|
|
#ifndef SHELL_COMMON_PROMISE_UTIL_H_
|
|
|
|
#define SHELL_COMMON_PROMISE_UTIL_H_
|
2018-06-27 21:06:08 +00:00
|
|
|
|
|
|
|
#include <string>
|
2019-03-08 02:07:04 +00:00
|
|
|
#include <utility>
|
2018-06-27 21:06:08 +00:00
|
|
|
|
2019-03-08 02:07:04 +00:00
|
|
|
#include "base/task/post_task.h"
|
|
|
|
#include "content/public/browser/browser_task_traits.h"
|
2018-06-27 21:06:08 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
#include "native_mate/converter.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/api/locker.h"
|
|
|
|
#include "shell/common/native_mate_converters/callback.h"
|
|
|
|
#include "shell/common/native_mate_converters/once_callback.h"
|
2018-06-27 21:06:08 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2018-06-27 21:06:08 +00:00
|
|
|
|
|
|
|
namespace util {
|
|
|
|
|
2019-02-21 12:32:44 +00:00
|
|
|
// A wrapper around the v8::Promise.
|
|
|
|
//
|
|
|
|
// This is a move-only type that should always be `std::move`d when passed to
|
|
|
|
// callbacks, and it should be destroyed on the same thread of creation.
|
|
|
|
class Promise {
|
2018-06-27 21:06:08 +00:00
|
|
|
public:
|
2019-02-21 12:32:44 +00:00
|
|
|
// Create a new promise.
|
2018-06-28 21:20:11 +00:00
|
|
|
explicit Promise(v8::Isolate* isolate);
|
2018-06-27 21:06:08 +00:00
|
|
|
|
2019-02-21 12:32:44 +00:00
|
|
|
// Wrap an existing v8 promise.
|
|
|
|
Promise(v8::Isolate* isolate, v8::Local<v8::Promise::Resolver> handle);
|
|
|
|
|
|
|
|
~Promise();
|
|
|
|
|
|
|
|
// Support moving.
|
|
|
|
Promise(Promise&&);
|
|
|
|
Promise& operator=(Promise&&);
|
|
|
|
|
2018-06-27 21:06:08 +00:00
|
|
|
v8::Isolate* isolate() const { return isolate_; }
|
2019-01-15 17:54:59 +00:00
|
|
|
v8::Local<v8::Context> GetContext() {
|
|
|
|
return v8::Local<v8::Context>::New(isolate_, context_);
|
|
|
|
}
|
2018-06-27 21:06:08 +00:00
|
|
|
|
2019-03-08 02:07:04 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 21:15:42 +00:00
|
|
|
// Returns an already-resolved promise.
|
|
|
|
template <typename T>
|
|
|
|
static v8::Local<v8::Promise> ResolvedPromise(v8::Isolate* isolate,
|
|
|
|
T result) {
|
|
|
|
Promise resolved(isolate);
|
|
|
|
resolved.Resolve(result);
|
|
|
|
return resolved.GetHandle();
|
|
|
|
}
|
|
|
|
|
|
|
|
static v8::Local<v8::Promise> ResolvedPromise(v8::Isolate* isolate) {
|
|
|
|
Promise resolved(isolate);
|
|
|
|
resolved.Resolve();
|
|
|
|
return resolved.GetHandle();
|
|
|
|
}
|
|
|
|
|
2019-02-21 12:32:44 +00:00
|
|
|
v8::Local<v8::Promise> GetHandle() const;
|
2018-06-27 21:06:08 +00:00
|
|
|
|
|
|
|
v8::Maybe<bool> Resolve() {
|
2019-01-15 17:54:59 +00:00
|
|
|
v8::HandleScope handle_scope(isolate());
|
|
|
|
v8::MicrotasksScope script_scope(isolate(),
|
|
|
|
v8::MicrotasksScope::kRunMicrotasks);
|
|
|
|
v8::Context::Scope context_scope(
|
|
|
|
v8::Local<v8::Context>::New(isolate(), GetContext()));
|
|
|
|
|
|
|
|
return GetInner()->Resolve(GetContext(), v8::Undefined(isolate()));
|
2018-06-27 21:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
v8::Maybe<bool> Reject() {
|
2019-01-15 17:54:59 +00:00
|
|
|
v8::HandleScope handle_scope(isolate());
|
|
|
|
v8::MicrotasksScope script_scope(isolate(),
|
|
|
|
v8::MicrotasksScope::kRunMicrotasks);
|
|
|
|
v8::Context::Scope context_scope(
|
|
|
|
v8::Local<v8::Context>::New(isolate(), GetContext()));
|
|
|
|
|
|
|
|
return GetInner()->Reject(GetContext(), v8::Undefined(isolate()));
|
2018-06-27 21:06:08 +00:00
|
|
|
}
|
|
|
|
|
2019-05-14 22:46:53 +00:00
|
|
|
// Please note that using Then is effectively the same as calling .then
|
|
|
|
// in javascript. This means (a) it is not type safe and (b) please note
|
|
|
|
// it is NOT type safe.
|
|
|
|
// If the base::Callback you provide here is of type void(boolean) and you
|
|
|
|
// resolve the promise with a string, Electron will compile successfully and
|
|
|
|
// then that promise will be rejected as soon as you try to use it as the
|
|
|
|
// mate converters doing work behind the scenes will throw an error for you.
|
|
|
|
// This can be really hard to trace so until either
|
|
|
|
// * This helper becomes typesafe (by templating the class instead of each
|
|
|
|
// method)
|
|
|
|
// * or the world goes mad
|
|
|
|
// Please try your hardest not to use this method
|
|
|
|
// The world thanks you
|
|
|
|
template <typename... ResolveType>
|
2019-05-02 23:49:27 +00:00
|
|
|
v8::MaybeLocal<v8::Promise> Then(
|
2019-05-14 22:46:53 +00:00
|
|
|
base::OnceCallback<void(ResolveType...)> cb) {
|
|
|
|
static_assert(sizeof...(ResolveType) <= 1,
|
|
|
|
"A promise's 'Then' callback should only receive at most one "
|
|
|
|
"parameter");
|
2019-02-18 14:25:28 +00:00
|
|
|
v8::HandleScope handle_scope(isolate());
|
|
|
|
v8::Context::Scope context_scope(
|
|
|
|
v8::Local<v8::Context>::New(isolate(), GetContext()));
|
|
|
|
|
|
|
|
v8::Local<v8::Value> value = mate::ConvertToV8(isolate(), cb);
|
|
|
|
v8::Local<v8::Function> handler = v8::Local<v8::Function>::Cast(value);
|
|
|
|
|
|
|
|
return GetHandle()->Then(GetContext(), handler);
|
|
|
|
}
|
|
|
|
|
2018-10-15 15:26:47 +00:00
|
|
|
// Promise resolution is a microtask
|
|
|
|
// We use the MicrotasksRunner to trigger the running of pending microtasks
|
2018-06-27 21:06:08 +00:00
|
|
|
template <typename T>
|
2018-09-27 14:59:23 +00:00
|
|
|
v8::Maybe<bool> Resolve(const T& value) {
|
2019-01-15 17:54:59 +00:00
|
|
|
v8::HandleScope handle_scope(isolate());
|
|
|
|
v8::MicrotasksScope script_scope(isolate(),
|
|
|
|
v8::MicrotasksScope::kRunMicrotasks);
|
|
|
|
v8::Context::Scope context_scope(
|
|
|
|
v8::Local<v8::Context>::New(isolate(), GetContext()));
|
|
|
|
|
|
|
|
return GetInner()->Resolve(GetContext(),
|
2018-06-27 21:06:08 +00:00
|
|
|
mate::ConvertToV8(isolate(), value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2018-09-27 14:59:23 +00:00
|
|
|
v8::Maybe<bool> Reject(const T& value) {
|
2019-01-15 17:54:59 +00:00
|
|
|
v8::HandleScope handle_scope(isolate());
|
|
|
|
v8::MicrotasksScope script_scope(isolate(),
|
|
|
|
v8::MicrotasksScope::kRunMicrotasks);
|
|
|
|
v8::Context::Scope context_scope(
|
|
|
|
v8::Local<v8::Context>::New(isolate(), GetContext()));
|
|
|
|
|
|
|
|
return GetInner()->Reject(GetContext(),
|
2018-06-27 21:06:08 +00:00
|
|
|
mate::ConvertToV8(isolate(), value));
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Maybe<bool> RejectWithErrorMessage(const std::string& error);
|
|
|
|
|
|
|
|
private:
|
2019-02-21 12:32:44 +00:00
|
|
|
friend class CopyablePromise;
|
|
|
|
|
2018-06-27 21:06:08 +00:00
|
|
|
v8::Local<v8::Promise::Resolver> GetInner() const {
|
|
|
|
return resolver_.Get(isolate());
|
|
|
|
}
|
|
|
|
|
2019-02-21 12:32:44 +00:00
|
|
|
v8::Isolate* isolate_;
|
|
|
|
v8::Global<v8::Context> context_;
|
2018-06-27 21:06:08 +00:00
|
|
|
v8::Global<v8::Promise::Resolver> resolver_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(Promise);
|
|
|
|
};
|
|
|
|
|
2019-02-21 12:32:44 +00:00
|
|
|
// A wrapper of Promise that can be copied.
|
|
|
|
//
|
|
|
|
// This class should only be used when we have to pass Promise to a Chromium API
|
|
|
|
// that does not take OnceCallback.
|
|
|
|
class CopyablePromise {
|
|
|
|
public:
|
|
|
|
explicit CopyablePromise(const Promise& promise);
|
|
|
|
CopyablePromise(const CopyablePromise&);
|
|
|
|
~CopyablePromise();
|
|
|
|
|
2019-03-08 02:07:04 +00:00
|
|
|
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},
|
2019-03-08 20:51:12 +00:00
|
|
|
base::BindOnce(Promise::ResolvePromise<T>, promise.GetPromise(),
|
2019-03-08 02:07:04 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 12:32:44 +00:00
|
|
|
Promise GetPromise() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
using CopyablePersistent =
|
|
|
|
v8::CopyablePersistentTraits<v8::Promise::Resolver>::CopyablePersistent;
|
|
|
|
|
|
|
|
v8::Isolate* isolate_;
|
|
|
|
CopyablePersistent handle_;
|
|
|
|
};
|
|
|
|
|
2018-06-27 21:06:08 +00:00
|
|
|
} // namespace util
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|
2018-06-27 21:06:08 +00:00
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
|
|
|
template <>
|
2019-06-19 21:23:04 +00:00
|
|
|
struct Converter<electron::util::Promise> {
|
2018-06-27 21:06:08 +00:00
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
2019-06-19 21:23:04 +00:00
|
|
|
const electron::util::Promise& val);
|
2018-06-27 21:06:08 +00:00
|
|
|
// TODO(MarshallOfSound): Implement FromV8 to allow promise chaining
|
|
|
|
// in native land
|
|
|
|
// static bool FromV8(v8::Isolate* isolate,
|
|
|
|
// v8::Local<v8::Value> val,
|
|
|
|
// Promise* out);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mate
|
|
|
|
|
2019-06-19 20:56:58 +00:00
|
|
|
#endif // SHELL_COMMON_PROMISE_UTIL_H_
|