// 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 ATOM_COMMON_PROMISE_UTIL_H_ #define ATOM_COMMON_PROMISE_UTIL_H_ #include #include #include "atom/common/api/locker.h" #include "atom/common/native_mate_converters/callback.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" namespace atom { 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. class Promise { public: // Create a new promise. explicit Promise(v8::Isolate* isolate); // Wrap an existing v8 promise. Promise(v8::Isolate* isolate, v8::Local handle); ~Promise(); // Support moving. Promise(Promise&&); Promise& operator=(Promise&&); v8::Isolate* isolate() const { return isolate_; } v8::Local GetContext() { return v8::Local::New(isolate_, context_); } // helpers for promise resolution and rejection template static void ResolvePromise(Promise promise, T result) { if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { base::PostTaskWithTraits( FROM_HERE, {content::BrowserThread::UI}, base::BindOnce( [](Promise promise, T 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::PostTaskWithTraits( FROM_HERE, {content::BrowserThread::UI}, base::BindOnce([](Promise promise) { promise.Resolve(); }, std::move(promise))); } else { promise.Resolve(); } } static void RejectPromise(Promise promise, std::string errmsg) { if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::UI}, base::BindOnce( [](Promise promise, std::string errmsg) { promise.RejectWithErrorMessage(errmsg); }, std::move(promise), std::move(errmsg))); } else { promise.RejectWithErrorMessage(errmsg); } } v8::Local GetHandle() const; v8::Maybe Resolve() { 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::Callback cb) { v8::HandleScope handle_scope(isolate()); v8::Context::Scope context_scope( v8::Local::New(isolate(), GetContext())); v8::Local value = mate::ConvertToV8(isolate(), 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 template v8::Maybe Resolve(const T& 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)); } template v8::Maybe Reject(const T& 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()->Reject(GetContext(), mate::ConvertToV8(isolate(), value)); } v8::Maybe RejectWithErrorMessage(const std::string& error); private: friend class CopyablePromise; v8::Local GetInner() const { return resolver_.Get(isolate()); } v8::Isolate* isolate_; v8::Global context_; v8::Global resolver_; DISALLOW_COPY_AND_ASSIGN(Promise); }; // A wrapper of Promise that can be copied. // // This class should only be used when we have to pass Promise to a Chromium API // that does not take OnceCallback. class CopyablePromise { public: explicit CopyablePromise(const Promise& promise); CopyablePromise(const CopyablePromise&); ~CopyablePromise(); template static void ResolveCopyablePromise(const CopyablePromise& promise, T result) { if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { base::PostTaskWithTraits( FROM_HERE, {content::BrowserThread::UI}, base::BindOnce(Promise::ResolvePromise, promise.GetPromise(), std::move(result))); } else { promise.GetPromise().Resolve(result); } } static void ResolveEmptyCopyablePromise(const CopyablePromise& promise) { if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { base::PostTaskWithTraits( FROM_HERE, {content::BrowserThread::UI}, base::BindOnce(Promise::ResolveEmptyPromise, promise.GetPromise())); } else { promise.GetPromise().Resolve(); } } static void RejectCopyablePromise(const CopyablePromise& promise, std::string errmsg) { if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) { base::PostTaskWithTraits( FROM_HERE, {content::BrowserThread::UI}, base::BindOnce(Promise::RejectPromise, promise.GetPromise(), std::move(errmsg))); } else { promise.GetPromise().RejectWithErrorMessage(errmsg); } } Promise GetPromise() const; private: using CopyablePersistent = v8::CopyablePersistentTraits::CopyablePersistent; v8::Isolate* isolate_; CopyablePersistent handle_; }; } // namespace util } // namespace atom namespace mate { template <> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, const atom::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 // ATOM_COMMON_PROMISE_UTIL_H_