refactor: migrates util::Promise to gin (#20871)
* refactor: use gin in Promise * refactor: separate Promise impl that returns nothing * refactor: use Promise<void> for promise that returns nothing * fix: methods should be able to run on both browser and renderer process * fix: should not pass base::StringPiece across threads * refactor: no more need to use different ResolvePromise for empty Promise * refactor: move Promise to gin_helper
This commit is contained in:
parent
bff113760a
commit
eaf2c61bef
48 changed files with 483 additions and 479 deletions
|
@ -9,9 +9,9 @@
|
|||
#include "shell/common/gin_converters/gurl_converter.h"
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
#include "shell/common/gin_helper/error_thrower.h"
|
||||
#include "shell/common/gin_helper/promise.h"
|
||||
#include "shell/common/node_includes.h"
|
||||
#include "shell/common/platform_util.h"
|
||||
#include "shell/common/promise_util.h"
|
||||
|
||||
#if defined(OS_WIN)
|
||||
#include "base/win/scoped_com_initializer.h"
|
||||
|
@ -44,7 +44,7 @@ struct Converter<base::win::ShortcutOperation> {
|
|||
|
||||
namespace {
|
||||
|
||||
void OnOpenExternalFinished(electron::util::Promise<void*> promise,
|
||||
void OnOpenExternalFinished(gin_helper::Promise<void> promise,
|
||||
const std::string& error) {
|
||||
if (error.empty())
|
||||
promise.Resolve();
|
||||
|
@ -53,7 +53,7 @@ void OnOpenExternalFinished(electron::util::Promise<void*> promise,
|
|||
}
|
||||
|
||||
v8::Local<v8::Promise> OpenExternal(const GURL& url, gin::Arguments* args) {
|
||||
electron::util::Promise<void*> promise(args->isolate());
|
||||
gin_helper::Promise<void> promise(args->isolate());
|
||||
v8::Local<v8::Promise> handle = promise.GetHandle();
|
||||
|
||||
platform_util::OpenExternalOptions options;
|
||||
|
|
|
@ -25,9 +25,9 @@
|
|||
#include "shell/common/gin_converters/file_path_converter.h"
|
||||
#include "shell/common/gin_helper/dictionary.h"
|
||||
#include "shell/common/gin_helper/locker.h"
|
||||
#include "shell/common/gin_helper/promise.h"
|
||||
#include "shell/common/heap_snapshot.h"
|
||||
#include "shell/common/node_includes.h"
|
||||
#include "shell/common/promise_util.h"
|
||||
#include "third_party/blink/renderer/platform/heap/process_heap.h" // nogncheck
|
||||
|
||||
namespace electron {
|
||||
|
@ -231,7 +231,7 @@ v8::Local<v8::Value> ElectronBindings::GetSystemMemoryInfo(
|
|||
// static
|
||||
v8::Local<v8::Promise> ElectronBindings::GetProcessMemoryInfo(
|
||||
v8::Isolate* isolate) {
|
||||
util::Promise<gin_helper::Dictionary> promise(isolate);
|
||||
gin_helper::Promise<gin_helper::Dictionary> promise(isolate);
|
||||
v8::Local<v8::Promise> handle = promise.GetHandle();
|
||||
|
||||
if (gin_helper::Locker::IsBrowserProcess() && !Browser::Get()->is_ready()) {
|
||||
|
@ -265,7 +265,7 @@ v8::Local<v8::Value> ElectronBindings::GetBlinkMemoryInfo(
|
|||
// static
|
||||
void ElectronBindings::DidReceiveMemoryDump(
|
||||
v8::Global<v8::Context> context,
|
||||
util::Promise<gin_helper::Dictionary> promise,
|
||||
gin_helper::Promise<gin_helper::Dictionary> promise,
|
||||
bool success,
|
||||
std::unique_ptr<memory_instrumentation::GlobalMemoryDump> global_dump) {
|
||||
v8::Isolate* isolate = promise.isolate();
|
||||
|
@ -292,7 +292,7 @@ void ElectronBindings::DidReceiveMemoryDump(
|
|||
#endif
|
||||
dict.Set("private", osdump.private_footprint_kb);
|
||||
dict.Set("shared", osdump.shared_footprint_kb);
|
||||
promise.ResolveWithGin(dict);
|
||||
promise.Resolve(dict);
|
||||
resolved = true;
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
#include "base/memory/scoped_refptr.h"
|
||||
#include "base/process/process_metrics.h"
|
||||
#include "base/strings/string16.h"
|
||||
#include "shell/common/promise_util.h"
|
||||
#include "shell/common/gin_helper/promise.h"
|
||||
#include "uv.h" // NOLINT(build/include)
|
||||
|
||||
namespace gin_helper {
|
||||
|
@ -70,7 +70,7 @@ class ElectronBindings {
|
|||
|
||||
static void DidReceiveMemoryDump(
|
||||
v8::Global<v8::Context> context,
|
||||
util::Promise<gin_helper::Dictionary> promise,
|
||||
gin_helper::Promise<gin_helper::Dictionary> promise,
|
||||
bool success,
|
||||
std::unique_ptr<memory_instrumentation::GlobalMemoryDump> dump);
|
||||
|
||||
|
|
100
shell/common/gin_helper/promise.cc
Normal file
100
shell/common/gin_helper/promise.cc
Normal file
|
@ -0,0 +1,100 @@
|
|||
// Copyright (c) 2018 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/common/gin_helper/promise.h"
|
||||
|
||||
namespace gin_helper {
|
||||
|
||||
PromiseBase::PromiseBase(v8::Isolate* isolate)
|
||||
: PromiseBase(isolate,
|
||||
v8::Promise::Resolver::New(isolate->GetCurrentContext())
|
||||
.ToLocalChecked()) {}
|
||||
|
||||
PromiseBase::PromiseBase(v8::Isolate* isolate,
|
||||
v8::Local<v8::Promise::Resolver> handle)
|
||||
: isolate_(isolate),
|
||||
context_(isolate, isolate->GetCurrentContext()),
|
||||
resolver_(isolate, handle) {}
|
||||
|
||||
PromiseBase::PromiseBase(PromiseBase&&) = default;
|
||||
|
||||
PromiseBase::~PromiseBase() = default;
|
||||
|
||||
PromiseBase& PromiseBase::operator=(PromiseBase&&) = default;
|
||||
|
||||
v8::Maybe<bool> PromiseBase::Reject() {
|
||||
gin_helper::Locker locker(isolate());
|
||||
v8::HandleScope handle_scope(isolate());
|
||||
v8::MicrotasksScope script_scope(isolate(),
|
||||
v8::MicrotasksScope::kRunMicrotasks);
|
||||
v8::Context::Scope context_scope(GetContext());
|
||||
|
||||
return GetInner()->Reject(GetContext(), v8::Undefined(isolate()));
|
||||
}
|
||||
|
||||
v8::Maybe<bool> PromiseBase::Reject(v8::Local<v8::Value> except) {
|
||||
gin_helper::Locker locker(isolate());
|
||||
v8::HandleScope handle_scope(isolate());
|
||||
v8::MicrotasksScope script_scope(isolate(),
|
||||
v8::MicrotasksScope::kRunMicrotasks);
|
||||
v8::Context::Scope context_scope(GetContext());
|
||||
|
||||
return GetInner()->Reject(GetContext(), except);
|
||||
}
|
||||
|
||||
v8::Maybe<bool> PromiseBase::RejectWithErrorMessage(base::StringPiece message) {
|
||||
gin_helper::Locker locker(isolate());
|
||||
v8::HandleScope handle_scope(isolate());
|
||||
v8::MicrotasksScope script_scope(isolate(),
|
||||
v8::MicrotasksScope::kRunMicrotasks);
|
||||
v8::Context::Scope context_scope(GetContext());
|
||||
|
||||
v8::Local<v8::Value> error =
|
||||
v8::Exception::Error(gin::StringToV8(isolate(), message));
|
||||
return GetInner()->Reject(GetContext(), (error));
|
||||
}
|
||||
|
||||
v8::Local<v8::Context> PromiseBase::GetContext() const {
|
||||
return v8::Local<v8::Context>::New(isolate_, context_);
|
||||
}
|
||||
|
||||
v8::Local<v8::Promise> PromiseBase::GetHandle() const {
|
||||
return GetInner()->GetPromise();
|
||||
}
|
||||
|
||||
v8::Local<v8::Promise::Resolver> PromiseBase::GetInner() const {
|
||||
return resolver_.Get(isolate());
|
||||
}
|
||||
|
||||
// static
|
||||
void Promise<void>::ResolvePromise(Promise<void> promise) {
|
||||
if (gin_helper::Locker::IsBrowserProcess() &&
|
||||
!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
|
||||
base::PostTask(
|
||||
FROM_HERE, {content::BrowserThread::UI},
|
||||
base::BindOnce([](Promise<void> promise) { promise.Resolve(); },
|
||||
std::move(promise)));
|
||||
} else {
|
||||
promise.Resolve();
|
||||
}
|
||||
}
|
||||
|
||||
// static
|
||||
v8::Local<v8::Promise> Promise<void>::ResolvedPromise(v8::Isolate* isolate) {
|
||||
Promise<void> resolved(isolate);
|
||||
resolved.Resolve();
|
||||
return resolved.GetHandle();
|
||||
}
|
||||
|
||||
v8::Maybe<bool> Promise<void>::Resolve() {
|
||||
gin_helper::Locker locker(isolate());
|
||||
v8::HandleScope handle_scope(isolate());
|
||||
v8::MicrotasksScope script_scope(isolate(),
|
||||
v8::MicrotasksScope::kRunMicrotasks);
|
||||
v8::Context::Scope context_scope(GetContext());
|
||||
|
||||
return GetInner()->Resolve(GetContext(), v8::Undefined(isolate()));
|
||||
}
|
||||
|
||||
} // namespace gin_helper
|
176
shell/common/gin_helper/promise.h
Normal file
176
shell/common/gin_helper/promise.h
Normal file
|
@ -0,0 +1,176 @@
|
|||
// Copyright (c) 2018 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_COMMON_GIN_HELPER_PROMISE_H_
|
||||
#define SHELL_COMMON_GIN_HELPER_PROMISE_H_
|
||||
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "base/strings/string_piece.h"
|
||||
#include "base/task/post_task.h"
|
||||
#include "content/public/browser/browser_task_traits.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "shell/common/gin_converters/std_converter.h"
|
||||
#include "shell/common/gin_helper/locker.h"
|
||||
|
||||
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);
|
||||
~PromiseBase();
|
||||
|
||||
// Support moving.
|
||||
PromiseBase(PromiseBase&&);
|
||||
PromiseBase& operator=(PromiseBase&&);
|
||||
|
||||
// Helper for rejecting promise with error message.
|
||||
//
|
||||
// Note: The parameter type is PromiseBase&& so it can take the instances of
|
||||
// Promise<T> type.
|
||||
static void RejectPromise(PromiseBase&& promise, base::StringPiece errmsg) {
|
||||
if (gin_helper::Locker::IsBrowserProcess() &&
|
||||
!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
|
||||
base::PostTask(
|
||||
FROM_HERE, {content::BrowserThread::UI},
|
||||
base::BindOnce(
|
||||
// Note that this callback can not take StringPiece,
|
||||
// as StringPiece only references string internally and
|
||||
// will blow when a temporary string is passed.
|
||||
[](PromiseBase&& promise, std::string str) {
|
||||
promise.RejectWithErrorMessage(str);
|
||||
},
|
||||
std::move(promise), std::string(errmsg.data(), errmsg.size())));
|
||||
} else {
|
||||
promise.RejectWithErrorMessage(errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
v8::Maybe<bool> Reject();
|
||||
v8::Maybe<bool> Reject(v8::Local<v8::Value> except);
|
||||
v8::Maybe<bool> RejectWithErrorMessage(base::StringPiece message);
|
||||
|
||||
v8::Local<v8::Context> GetContext() const;
|
||||
v8::Local<v8::Promise> GetHandle() const;
|
||||
|
||||
v8::Isolate* isolate() const { return isolate_; }
|
||||
|
||||
protected:
|
||||
v8::Local<v8::Promise::Resolver> GetInner() const;
|
||||
|
||||
private:
|
||||
v8::Isolate* isolate_;
|
||||
v8::Global<v8::Context> context_;
|
||||
v8::Global<v8::Promise::Resolver> resolver_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(PromiseBase);
|
||||
};
|
||||
|
||||
// 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) {
|
||||
if (gin_helper::Locker::IsBrowserProcess() &&
|
||||
!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
|
||||
base::PostTask(FROM_HERE, {content::BrowserThread::UI},
|
||||
base::BindOnce([](Promise<RT> promise,
|
||||
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();
|
||||
}
|
||||
|
||||
// Promise resolution is a microtask
|
||||
// We use the MicrotasksRunner to trigger the running of pending microtasks
|
||||
v8::Maybe<bool> Resolve(const RT& value) {
|
||||
gin_helper::Locker locker(isolate());
|
||||
v8::HandleScope handle_scope(isolate());
|
||||
v8::MicrotasksScope script_scope(isolate(),
|
||||
v8::MicrotasksScope::kRunMicrotasks);
|
||||
v8::Context::Scope context_scope(GetContext());
|
||||
|
||||
return GetInner()->Resolve(GetContext(),
|
||||
gin::ConvertToV8(isolate(), value));
|
||||
}
|
||||
|
||||
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");
|
||||
gin_helper::Locker locker(isolate());
|
||||
v8::HandleScope handle_scope(isolate());
|
||||
v8::Context::Scope context_scope(GetContext());
|
||||
|
||||
v8::Local<v8::Value> value = gin::ConvertToV8(isolate(), std::move(cb));
|
||||
v8::Local<v8::Function> handler = v8::Local<v8::Function>::Cast(value);
|
||||
|
||||
return GetHandle()->Then(GetContext(), handler);
|
||||
}
|
||||
};
|
||||
|
||||
// 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
|
||||
|
||||
#endif // SHELL_COMMON_GIN_HELPER_PROMISE_H_
|
|
@ -1,23 +0,0 @@
|
|||
// Copyright (c) 2018 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "shell/common/promise_util.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
template <typename T>
|
||||
v8::Local<v8::Value> mate::Converter<electron::util::Promise<T>>::ToV8(
|
||||
v8::Isolate*,
|
||||
const electron::util::Promise<T>& val) {
|
||||
return val.GetHandle();
|
||||
}
|
||||
|
||||
template <>
|
||||
v8::Local<v8::Value> mate::Converter<electron::util::Promise<void*>>::ToV8(
|
||||
v8::Isolate*,
|
||||
const electron::util::Promise<void*>& val) {
|
||||
return val.GetHandle();
|
||||
}
|
||||
|
||||
} // namespace mate
|
|
@ -1,243 +0,0 @@
|
|||
// Copyright (c) 2018 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_COMMON_PROMISE_UTIL_H_
|
||||
#define SHELL_COMMON_PROMISE_UTIL_H_
|
||||
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include "base/strings/string_piece.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"
|
||||
#include "shell/common/gin_converters/std_converter.h"
|
||||
|
||||
namespace electron {
|
||||
|
||||
namespace util {
|
||||
|
||||
// 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.
|
||||
template <typename RT>
|
||||
class Promise {
|
||||
public:
|
||||
// Create a new promise.
|
||||
explicit Promise(v8::Isolate* isolate)
|
||||
: Promise(isolate,
|
||||
v8::Promise::Resolver::New(isolate->GetCurrentContext())
|
||||
.ToLocalChecked()) {}
|
||||
|
||||
// Wrap an existing v8 promise.
|
||||
Promise(v8::Isolate* isolate, v8::Local<v8::Promise::Resolver> handle)
|
||||
: isolate_(isolate),
|
||||
context_(isolate, isolate->GetCurrentContext()),
|
||||
resolver_(isolate, handle) {}
|
||||
|
||||
~Promise() = default;
|
||||
|
||||
// Support moving.
|
||||
Promise(Promise&&) = default;
|
||||
Promise& operator=(Promise&&) = default;
|
||||
|
||||
v8::Isolate* isolate() const { return isolate_; }
|
||||
v8::Local<v8::Context> GetContext() {
|
||||
return v8::Local<v8::Context>::New(isolate_, context_);
|
||||
}
|
||||
|
||||
// helpers for promise resolution and rejection
|
||||
|
||||
static void ResolvePromise(Promise<RT> promise, RT result) {
|
||||
if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
|
||||
base::PostTask(
|
||||
FROM_HERE, {content::BrowserThread::UI},
|
||||
base::BindOnce([](Promise<RT> promise,
|
||||
RT result) { promise.ResolveWithGin(result); },
|
||||
std::move(promise), std::move(result)));
|
||||
} else {
|
||||
promise.ResolveWithGin(result);
|
||||
}
|
||||
}
|
||||
|
||||
static void ResolveEmptyPromise(Promise<RT> promise) {
|
||||
if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
|
||||
base::PostTask(
|
||||
FROM_HERE, {content::BrowserThread::UI},
|
||||
base::BindOnce([](Promise<RT> promise) { promise.Resolve(); },
|
||||
std::move(promise)));
|
||||
} else {
|
||||
promise.Resolve();
|
||||
}
|
||||
}
|
||||
|
||||
static void RejectPromise(Promise<RT> promise, base::StringPiece errmsg) {
|
||||
if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
|
||||
base::PostTask(FROM_HERE, {content::BrowserThread::UI},
|
||||
base::BindOnce(
|
||||
[](Promise<RT> promise, base::StringPiece err) {
|
||||
promise.RejectWithErrorMessage(err);
|
||||
},
|
||||
std::move(promise), std::move(errmsg)));
|
||||
} else {
|
||||
promise.RejectWithErrorMessage(errmsg);
|
||||
}
|
||||
}
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
static v8::Local<v8::Promise> ResolvedPromise(v8::Isolate* isolate) {
|
||||
Promise<void*> resolved(isolate);
|
||||
resolved.Resolve();
|
||||
return resolved.GetHandle();
|
||||
}
|
||||
|
||||
v8::Local<v8::Promise> GetHandle() const { return GetInner()->GetPromise(); }
|
||||
|
||||
v8::Maybe<bool> Resolve() {
|
||||
static_assert(std::is_same<void*, RT>(),
|
||||
"Can only resolve void* promises with no value");
|
||||
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()));
|
||||
}
|
||||
|
||||
v8::Maybe<bool> Reject() {
|
||||
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()));
|
||||
}
|
||||
|
||||
v8::Maybe<bool> Reject(v8::Local<v8::Value> exception) {
|
||||
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(), exception);
|
||||
}
|
||||
|
||||
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");
|
||||
v8::HandleScope handle_scope(isolate());
|
||||
v8::Context::Scope context_scope(
|
||||
v8::Local<v8::Context>::New(isolate(), GetContext()));
|
||||
|
||||
v8::Local<v8::Value> value = gin::ConvertToV8(isolate(), std::move(cb));
|
||||
v8::Local<v8::Function> handler = v8::Local<v8::Function>::Cast(value);
|
||||
|
||||
return GetHandle()->Then(GetContext(), handler);
|
||||
}
|
||||
|
||||
// Promise resolution is a microtask
|
||||
// We use the MicrotasksRunner to trigger the running of pending microtasks
|
||||
v8::Maybe<bool> Resolve(const RT& value) {
|
||||
static_assert(!std::is_same<void*, RT>(),
|
||||
"void* promises can not be resolved with a value");
|
||||
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(),
|
||||
mate::ConvertToV8(isolate(), value));
|
||||
}
|
||||
|
||||
v8::Maybe<bool> ResolveWithGin(const RT& value) {
|
||||
static_assert(!std::is_same<void*, RT>(),
|
||||
"void* promises can not be resolved with a value");
|
||||
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(),
|
||||
gin::ConvertToV8(isolate(), value));
|
||||
}
|
||||
|
||||
v8::Maybe<bool> RejectWithErrorMessage(base::StringPiece 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::Value> error =
|
||||
v8::Exception::Error(gin::StringToV8(isolate(), string));
|
||||
return GetInner()->Reject(GetContext(), (error));
|
||||
}
|
||||
|
||||
private:
|
||||
v8::Local<v8::Promise::Resolver> GetInner() const {
|
||||
return resolver_.Get(isolate());
|
||||
}
|
||||
|
||||
v8::Isolate* isolate_;
|
||||
v8::Global<v8::Context> context_;
|
||||
v8::Global<v8::Promise::Resolver> resolver_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(Promise);
|
||||
};
|
||||
|
||||
} // namespace util
|
||||
|
||||
} // namespace electron
|
||||
|
||||
namespace mate {
|
||||
|
||||
template <typename T>
|
||||
struct Converter<electron::util::Promise<T>> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const electron::util::Promise<T>& val);
|
||||
// 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
|
||||
|
||||
namespace gin {
|
||||
|
||||
template <typename T>
|
||||
struct Converter<electron::util::Promise<T>> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const electron::util::Promise<T>& val) {
|
||||
return mate::ConvertToV8(isolate, val);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace gin
|
||||
|
||||
#endif // SHELL_COMMON_PROMISE_UTIL_H_
|
Loading…
Add table
Add a link
Reference in a new issue