// 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 #include #include #include #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 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 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 GetContext() { return v8::Local::New(isolate_, context_); } // helpers for promise resolution and rejection static void ResolvePromise(Promise promise, RT result) { if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { base::PostTask(FROM_HERE, {content::BrowserThread::UI}, base::BindOnce([](Promise promise, RT 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::PostTask( FROM_HERE, {content::BrowserThread::UI}, base::BindOnce([](Promise promise) { promise.Resolve(); }, std::move(promise))); } else { promise.Resolve(); } } static void RejectPromise(Promise promise, base::StringPiece errmsg) { if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { base::PostTask(FROM_HERE, {content::BrowserThread::UI}, base::BindOnce( [](Promise 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 ResolvedPromise(v8::Isolate* isolate, RT result) { Promise resolved(isolate); resolved.Resolve(result); return resolved.GetHandle(); } static v8::Local ResolvedPromise(v8::Isolate* isolate) { Promise resolved(isolate); resolved.Resolve(); return resolved.GetHandle(); } v8::Local GetHandle() const { return GetInner()->GetPromise(); } v8::Maybe Resolve() { static_assert(std::is_same(), "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::New(isolate(), GetContext())); return GetInner()->Resolve(GetContext(), v8::Undefined(isolate())); } v8::Maybe Reject() { v8::HandleScope handle_scope(isolate()); v8::MicrotasksScope script_scope(isolate(), v8::MicrotasksScope::kRunMicrotasks); v8::Context::Scope context_scope( v8::Local::New(isolate(), GetContext())); return GetInner()->Reject(GetContext(), v8::Undefined(isolate())); } template v8::MaybeLocal Then( base::OnceCallback cb) { static_assert(sizeof...(ResolveType) <= 1, "A promise's 'Then' callback should only receive at most one " "parameter"); static_assert( std::is_same>>(), "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::New(isolate(), GetContext())); v8::Local value = gin::ConvertToV8(isolate(), std::move(cb)); v8::Local handler = v8::Local::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 Resolve(const RT& value) { static_assert(!std::is_same(), "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::New(isolate(), GetContext())); return GetInner()->Resolve(GetContext(), mate::ConvertToV8(isolate(), value)); } v8::Maybe ResolveWithGin(const RT& value) { static_assert(!std::is_same(), "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::New(isolate(), GetContext())); return GetInner()->Resolve(GetContext(), gin::ConvertToV8(isolate(), value)); } v8::Maybe RejectWithErrorMessage(base::StringPiece string) { v8::HandleScope handle_scope(isolate()); v8::MicrotasksScope script_scope(isolate(), v8::MicrotasksScope::kRunMicrotasks); v8::Context::Scope context_scope( v8::Local::New(isolate(), GetContext())); v8::Local error = v8::Exception::Error(gin::StringToV8(isolate(), string)); return GetInner()->Reject(GetContext(), (error)); } private: v8::Local GetInner() const { return resolver_.Get(isolate()); } v8::Isolate* isolate_; v8::Global context_; v8::Global resolver_; DISALLOW_COPY_AND_ASSIGN(Promise); }; } // namespace util } // namespace electron namespace mate { template struct Converter> { static v8::Local ToV8(v8::Isolate* isolate, const electron::util::Promise& val); // TODO(MarshallOfSound): Implement FromV8 to allow promise chaining // in native land // static bool FromV8(v8::Isolate* isolate, // v8::Local val, // Promise* out); }; } // namespace mate #endif // SHELL_COMMON_PROMISE_UTIL_H_