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-08-23 00:03:28 +00:00
|
|
|
#include <tuple>
|
|
|
|
#include <type_traits>
|
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.
|
2019-08-23 00:03:28 +00:00
|
|
|
template <typename RT>
|
2019-02-21 12:32:44 +00:00
|
|
|
class Promise {
|
2018-06-27 21:06:08 +00:00
|
|
|
public:
|
2019-02-21 12:32:44 +00:00
|
|
|
// Create a new promise.
|
2019-08-23 00:03:28 +00:00
|
|
|
explicit Promise(v8::Isolate* isolate)
|
|
|
|
: Promise(isolate,
|
|
|
|
v8::Promise::Resolver::New(isolate->GetCurrentContext())
|
|
|
|
.ToLocalChecked()) {}
|
2018-06-27 21:06:08 +00:00
|
|
|
|
2019-02-21 12:32:44 +00:00
|
|
|
// Wrap an existing v8 promise.
|
2019-08-23 00:03:28 +00:00
|
|
|
Promise(v8::Isolate* isolate, v8::Local<v8::Promise::Resolver> handle)
|
|
|
|
: isolate_(isolate),
|
|
|
|
context_(isolate, isolate->GetCurrentContext()),
|
|
|
|
resolver_(isolate, handle) {}
|
2019-02-21 12:32:44 +00:00
|
|
|
|
2019-08-23 00:03:28 +00:00
|
|
|
~Promise() = default;
|
2019-02-21 12:32:44 +00:00
|
|
|
|
|
|
|
// Support moving.
|
2019-08-23 00:03:28 +00:00
|
|
|
Promise(Promise&&) = default;
|
|
|
|
Promise& operator=(Promise&&) = default;
|
2019-02-21 12:32:44 +00:00
|
|
|
|
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
|
|
|
|
|
2019-08-23 00:03:28 +00:00
|
|
|
static void ResolvePromise(Promise<RT> promise, RT result) {
|
2019-03-08 02:07:04 +00:00
|
|
|
if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
|
|
|
|
base::PostTaskWithTraits(
|
|
|
|
FROM_HERE, {content::BrowserThread::UI},
|
|
|
|
base::BindOnce(
|
2019-08-23 00:03:28 +00:00
|
|
|
[](Promise<RT> promise, RT result) { promise.Resolve(result); },
|
2019-03-08 02:07:04 +00:00
|
|
|
std::move(promise), std::move(result)));
|
|
|
|
} else {
|
|
|
|
promise.Resolve(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 00:03:28 +00:00
|
|
|
static void ResolveEmptyPromise(Promise<RT> promise) {
|
2019-03-08 02:07:04 +00:00
|
|
|
if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
|
|
|
|
base::PostTaskWithTraits(
|
|
|
|
FROM_HERE, {content::BrowserThread::UI},
|
2019-08-23 00:03:28 +00:00
|
|
|
base::BindOnce([](Promise<RT> promise) { promise.Resolve(); },
|
2019-03-08 02:07:04 +00:00
|
|
|
std::move(promise)));
|
|
|
|
} else {
|
|
|
|
promise.Resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 00:03:28 +00:00
|
|
|
static void RejectPromise(Promise<RT> promise, std::string errmsg) {
|
2019-03-08 02:07:04 +00:00
|
|
|
if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
|
|
|
|
base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::UI},
|
|
|
|
base::BindOnce(
|
2019-08-23 00:03:28 +00:00
|
|
|
[](Promise<RT> promise, std::string errmsg) {
|
2019-03-08 02:07:04 +00:00
|
|
|
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.
|
|
|
|
static v8::Local<v8::Promise> ResolvedPromise(v8::Isolate* isolate,
|
2019-08-23 00:03:28 +00:00
|
|
|
RT result) {
|
|
|
|
Promise<RT> resolved(isolate);
|
2019-05-28 21:15:42 +00:00
|
|
|
resolved.Resolve(result);
|
|
|
|
return resolved.GetHandle();
|
|
|
|
}
|
|
|
|
|
|
|
|
static v8::Local<v8::Promise> ResolvedPromise(v8::Isolate* isolate) {
|
2019-08-23 00:03:28 +00:00
|
|
|
Promise<void*> resolved(isolate);
|
2019-05-28 21:15:42 +00:00
|
|
|
resolved.Resolve();
|
|
|
|
return resolved.GetHandle();
|
|
|
|
}
|
|
|
|
|
2019-08-23 00:03:28 +00:00
|
|
|
v8::Local<v8::Promise> GetHandle() const { return GetInner()->GetPromise(); }
|
2018-06-27 21:06:08 +00:00
|
|
|
|
|
|
|
v8::Maybe<bool> Resolve() {
|
2019-08-23 00:03:28 +00:00
|
|
|
static_assert(std::is_same<void*, RT>(),
|
|
|
|
"Can only resolve void* promises with no 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(), 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
|
|
|
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-08-23 00:03:28 +00:00
|
|
|
static_assert(
|
|
|
|
std::is_same<RT, std::tuple_element_t<0, std::tuple<ResolveType...>>>(),
|
|
|
|
"A promises's 'Then' callback must handle the same type as the "
|
|
|
|
"promises resolve type");
|
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
|
2019-08-23 00:03:28 +00:00
|
|
|
v8::Maybe<bool> Resolve(const RT& value) {
|
|
|
|
static_assert(!std::is_same<void*, RT>(),
|
|
|
|
"void* promises can not be resolved with a 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));
|
|
|
|
}
|
|
|
|
|
2019-08-23 00:03:28 +00:00
|
|
|
v8::Maybe<bool> ResolveWithGin(const RT& value) {
|
|
|
|
static_assert(!std::is_same<void*, RT>(),
|
|
|
|
"void* promises can not be resolved with a 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()));
|
|
|
|
|
2019-08-23 00:03:28 +00:00
|
|
|
return GetInner()->Resolve(GetContext(),
|
|
|
|
gin::ConvertToV8(isolate(), value));
|
2018-06-27 21:06:08 +00:00
|
|
|
}
|
|
|
|
|
2019-08-23 00:03:28 +00:00
|
|
|
v8::Maybe<bool> RejectWithErrorMessage(const std::string& string) {
|
|
|
|
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()));
|
|
|
|
|
|
|
|
v8::Local<v8::String> error_message =
|
|
|
|
v8::String::NewFromUtf8(isolate(), string.c_str(),
|
|
|
|
v8::NewStringType::kNormal,
|
|
|
|
static_cast<int>(string.size()))
|
|
|
|
.ToLocalChecked();
|
|
|
|
v8::Local<v8::Value> error = v8::Exception::Error(error_message);
|
|
|
|
return GetInner()->Reject(GetContext(), (error));
|
|
|
|
}
|
2018-06-27 21:06:08 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace util
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|
2018-06-27 21:06:08 +00:00
|
|
|
|
|
|
|
namespace mate {
|
|
|
|
|
2019-08-23 00:03:28 +00:00
|
|
|
template <typename T>
|
|
|
|
struct Converter<electron::util::Promise<T>> {
|
2018-06-27 21:06:08 +00:00
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
2019-08-23 00:03:28 +00:00
|
|
|
const electron::util::Promise<T>& 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_
|