2019-11-01 06:10:32 +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.
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_PROMISE_H_
|
|
|
|
#define ELECTRON_SHELL_COMMON_GIN_HELPER_PROMISE_H_
|
2019-11-01 06:10:32 +00:00
|
|
|
|
|
|
|
#include <string>
|
2024-01-11 01:00:37 +00:00
|
|
|
#include <string_view>
|
2019-11-01 06:10:32 +00:00
|
|
|
#include <tuple>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
|
2023-05-11 20:07:39 +00:00
|
|
|
#include "base/memory/raw_ptr.h"
|
2024-09-17 04:08:40 +00:00
|
|
|
#include "base/memory/scoped_refptr.h"
|
|
|
|
#include "base/task/task_runner.h"
|
2019-11-01 06:10:32 +00:00
|
|
|
#include "shell/common/gin_converters/std_converter.h"
|
2020-06-17 17:08:10 +00:00
|
|
|
#include "shell/common/gin_helper/microtasks_scope.h"
|
2024-07-29 17:42:57 +00:00
|
|
|
#include "v8/include/v8-context.h"
|
2019-11-01 06:10:32 +00:00
|
|
|
|
|
|
|
namespace gin_helper {
|
|
|
|
|
|
|
|
// A wrapper around the v8::Promise.
|
|
|
|
//
|
|
|
|
// This is the non-template base class to share code between templates
|
|
|
|
// instances.
|
|
|
|
//
|
|
|
|
// 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 PromiseBase {
|
|
|
|
public:
|
|
|
|
explicit PromiseBase(v8::Isolate* isolate);
|
|
|
|
PromiseBase(v8::Isolate* isolate, v8::Local<v8::Promise::Resolver> handle);
|
2023-03-27 17:00:55 +00:00
|
|
|
PromiseBase();
|
2019-11-01 06:10:32 +00:00
|
|
|
~PromiseBase();
|
|
|
|
|
2021-11-03 11:41:45 +00:00
|
|
|
// disable copy
|
|
|
|
PromiseBase(const PromiseBase&) = delete;
|
|
|
|
PromiseBase& operator=(const PromiseBase&) = delete;
|
|
|
|
|
2019-11-01 06:10:32 +00:00
|
|
|
// Support moving.
|
|
|
|
PromiseBase(PromiseBase&&);
|
|
|
|
PromiseBase& operator=(PromiseBase&&);
|
|
|
|
|
|
|
|
// Helper for rejecting promise with error message.
|
2024-09-17 04:08:40 +00:00
|
|
|
static void RejectPromise(PromiseBase&& promise, std::string_view errmsg);
|
2019-11-01 06:10:32 +00:00
|
|
|
|
|
|
|
v8::Maybe<bool> Reject();
|
|
|
|
v8::Maybe<bool> Reject(v8::Local<v8::Value> except);
|
2024-01-11 01:00:37 +00:00
|
|
|
v8::Maybe<bool> RejectWithErrorMessage(std::string_view message);
|
2019-11-01 06:10:32 +00:00
|
|
|
|
|
|
|
v8::Local<v8::Context> GetContext() const;
|
|
|
|
v8::Local<v8::Promise> GetHandle() const;
|
|
|
|
|
|
|
|
v8::Isolate* isolate() const { return isolate_; }
|
|
|
|
|
|
|
|
protected:
|
2024-09-17 04:08:40 +00:00
|
|
|
struct SettleScope {
|
|
|
|
explicit SettleScope(const PromiseBase& base);
|
|
|
|
~SettleScope();
|
|
|
|
|
|
|
|
v8::HandleScope handle_scope_;
|
|
|
|
v8::Local<v8::Context> context_;
|
|
|
|
gin_helper::MicrotasksScope microtasks_scope_;
|
|
|
|
v8::Context::Scope context_scope_;
|
|
|
|
};
|
|
|
|
|
2019-11-01 06:10:32 +00:00
|
|
|
v8::Local<v8::Promise::Resolver> GetInner() const;
|
|
|
|
|
2024-09-17 04:08:40 +00:00
|
|
|
static scoped_refptr<base::TaskRunner> GetTaskRunner();
|
|
|
|
|
2019-11-01 06:10:32 +00:00
|
|
|
private:
|
2023-05-11 20:07:39 +00:00
|
|
|
raw_ptr<v8::Isolate> isolate_;
|
2019-11-01 06:10:32 +00:00
|
|
|
v8::Global<v8::Context> context_;
|
|
|
|
v8::Global<v8::Promise::Resolver> resolver_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Template implementation that returns values.
|
|
|
|
template <typename RT>
|
|
|
|
class Promise : public PromiseBase {
|
|
|
|
public:
|
|
|
|
using PromiseBase::PromiseBase;
|
|
|
|
|
|
|
|
// Helper for resolving the promise with |result|.
|
|
|
|
static void ResolvePromise(Promise<RT> promise, RT result) {
|
2024-09-17 04:08:40 +00:00
|
|
|
if (auto task_runner = GetTaskRunner()) {
|
|
|
|
task_runner->PostTask(
|
2022-05-17 16:48:40 +00:00
|
|
|
FROM_HERE, base::BindOnce([](Promise<RT> promise,
|
2019-11-01 06:10:32 +00:00
|
|
|
RT result) { promise.Resolve(result); },
|
|
|
|
std::move(promise), std::move(result)));
|
|
|
|
} else {
|
|
|
|
promise.Resolve(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns an already-resolved promise.
|
|
|
|
static v8::Local<v8::Promise> ResolvedPromise(v8::Isolate* isolate,
|
|
|
|
RT result) {
|
|
|
|
Promise<RT> resolved(isolate);
|
|
|
|
resolved.Resolve(result);
|
|
|
|
return resolved.GetHandle();
|
|
|
|
}
|
|
|
|
|
2021-11-01 21:08:31 +00:00
|
|
|
// Convert to another type.
|
|
|
|
template <typename NT>
|
|
|
|
Promise<NT> As() {
|
2024-09-17 04:08:40 +00:00
|
|
|
return Promise<NT>{isolate(), GetInner()};
|
2021-11-01 21:08:31 +00:00
|
|
|
}
|
|
|
|
|
2019-11-01 06:10:32 +00:00
|
|
|
v8::Maybe<bool> Resolve(const RT& value) {
|
2024-09-17 04:08:40 +00:00
|
|
|
SettleScope settle_scope{*this};
|
|
|
|
return GetInner()->Resolve(settle_scope.context_,
|
|
|
|
gin::ConvertToV8(isolate(), value));
|
2019-11-01 06:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... ResolveType>
|
|
|
|
v8::MaybeLocal<v8::Promise> Then(
|
|
|
|
base::OnceCallback<void(ResolveType...)> cb) {
|
|
|
|
static_assert(sizeof...(ResolveType) <= 1,
|
|
|
|
"A promise's 'Then' callback should only receive at most one "
|
|
|
|
"parameter");
|
|
|
|
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");
|
2024-09-17 04:08:40 +00:00
|
|
|
SettleScope settle_scope{*this};
|
2019-11-01 06:10:32 +00:00
|
|
|
v8::Local<v8::Value> value = gin::ConvertToV8(isolate(), std::move(cb));
|
2021-05-12 07:38:21 +00:00
|
|
|
v8::Local<v8::Function> handler = value.As<v8::Function>();
|
2024-09-17 04:08:40 +00:00
|
|
|
return GetHandle()->Then(settle_scope.context_, handler);
|
2019-11-01 06:10:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Template implementation that returns nothing.
|
|
|
|
template <>
|
|
|
|
class Promise<void> : public PromiseBase {
|
|
|
|
public:
|
|
|
|
using PromiseBase::PromiseBase;
|
|
|
|
|
|
|
|
// Helper for resolving the empty promise.
|
|
|
|
static void ResolvePromise(Promise<void> promise);
|
|
|
|
|
|
|
|
// Returns an already-resolved promise.
|
|
|
|
static v8::Local<v8::Promise> ResolvedPromise(v8::Isolate* isolate);
|
|
|
|
|
|
|
|
v8::Maybe<bool> Resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace gin_helper
|
|
|
|
|
|
|
|
namespace gin {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct Converter<gin_helper::Promise<T>> {
|
|
|
|
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
|
|
|
const gin_helper::Promise<T>& val) {
|
|
|
|
return val.GetHandle();
|
|
|
|
}
|
|
|
|
// 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 gin
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#endif // ELECTRON_SHELL_COMMON_GIN_HELPER_PROMISE_H_
|