refactor: reduce code duplication in gin_helper::Promise (#43716)
* refactor: move scope scaffolding into SettletScope idea stolen from SpellCheckScope * refactor: move impl of PromiseBase::RejectPromise() to the cc file * chore: remove unused #include
This commit is contained in:
parent
05dfd14913
commit
b6bf277b6f
11 changed files with 86 additions and 86 deletions
|
@ -12,12 +12,10 @@
|
|||
#include <utility>
|
||||
|
||||
#include "base/memory/raw_ptr.h"
|
||||
#include "content/public/browser/browser_task_traits.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "base/memory/scoped_refptr.h"
|
||||
#include "base/task/task_runner.h"
|
||||
#include "shell/common/gin_converters/std_converter.h"
|
||||
#include "shell/common/gin_helper/locker.h"
|
||||
#include "shell/common/gin_helper/microtasks_scope.h"
|
||||
#include "shell/common/process_util.h"
|
||||
#include "v8/include/v8-context.h"
|
||||
|
||||
namespace gin_helper {
|
||||
|
@ -45,27 +43,7 @@ class 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,
|
||||
const std::string_view errmsg) {
|
||||
if (electron::IsBrowserProcess() &&
|
||||
!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
|
||||
content::GetUIThreadTaskRunner({})->PostTask(
|
||||
FROM_HERE,
|
||||
base::BindOnce(
|
||||
// Note that this callback can not take std::string_view,
|
||||
// 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}));
|
||||
} else {
|
||||
promise.RejectWithErrorMessage(errmsg);
|
||||
}
|
||||
}
|
||||
static void RejectPromise(PromiseBase&& promise, std::string_view errmsg);
|
||||
|
||||
v8::Maybe<bool> Reject();
|
||||
v8::Maybe<bool> Reject(v8::Local<v8::Value> except);
|
||||
|
@ -77,8 +55,20 @@ class PromiseBase {
|
|||
v8::Isolate* isolate() const { return isolate_; }
|
||||
|
||||
protected:
|
||||
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_;
|
||||
};
|
||||
|
||||
v8::Local<v8::Promise::Resolver> GetInner() const;
|
||||
|
||||
static scoped_refptr<base::TaskRunner> GetTaskRunner();
|
||||
|
||||
private:
|
||||
raw_ptr<v8::Isolate> isolate_;
|
||||
v8::Global<v8::Context> context_;
|
||||
|
@ -93,9 +83,8 @@ class Promise : public PromiseBase {
|
|||
|
||||
// Helper for resolving the promise with |result|.
|
||||
static void ResolvePromise(Promise<RT> promise, RT result) {
|
||||
if (electron::IsBrowserProcess() &&
|
||||
!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
|
||||
content::GetUIThreadTaskRunner({})->PostTask(
|
||||
if (auto task_runner = GetTaskRunner()) {
|
||||
task_runner->PostTask(
|
||||
FROM_HERE, base::BindOnce([](Promise<RT> promise,
|
||||
RT result) { promise.Resolve(result); },
|
||||
std::move(promise), std::move(result)));
|
||||
|
@ -115,21 +104,13 @@ class Promise : public PromiseBase {
|
|||
// Convert to another type.
|
||||
template <typename NT>
|
||||
Promise<NT> As() {
|
||||
return Promise<NT>(isolate(), GetInner());
|
||||
return Promise<NT>{isolate(), GetInner()};
|
||||
}
|
||||
|
||||
// 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::Local<v8::Context> context = GetContext();
|
||||
gin_helper::MicrotasksScope microtasks_scope{
|
||||
isolate(), context->GetMicrotaskQueue(), false,
|
||||
v8::MicrotasksScope::kRunMicrotasks};
|
||||
v8::Context::Scope context_scope(context);
|
||||
|
||||
return GetInner()->Resolve(context, gin::ConvertToV8(isolate(), value));
|
||||
SettleScope settle_scope{*this};
|
||||
return GetInner()->Resolve(settle_scope.context_,
|
||||
gin::ConvertToV8(isolate(), value));
|
||||
}
|
||||
|
||||
template <typename... ResolveType>
|
||||
|
@ -142,15 +123,10 @@ class Promise : public PromiseBase {
|
|||
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::Local<v8::Context> context = GetContext();
|
||||
v8::Context::Scope context_scope(context);
|
||||
|
||||
SettleScope settle_scope{*this};
|
||||
v8::Local<v8::Value> value = gin::ConvertToV8(isolate(), std::move(cb));
|
||||
v8::Local<v8::Function> handler = value.As<v8::Function>();
|
||||
|
||||
return GetHandle()->Then(context, handler);
|
||||
return GetHandle()->Then(settle_scope.context_, handler);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue