// 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 "atom/common/api/locker.h" #include "atom/common/native_mate_converters/callback.h" #include "content/public/browser/browser_thread.h" #include "native_mate/converter.h" namespace atom { namespace util { class Promise : public base::RefCounted { public: explicit Promise(v8::Isolate* isolate); v8::Isolate* isolate() const { return isolate_; } v8::Local GetContext() { return v8::Local::New(isolate_, context_); } virtual 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())); } v8::MaybeLocal Then(base::Closure 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); protected: virtual ~Promise(); friend class base::RefCounted; v8::Isolate* isolate_; v8::Global context_; private: v8::Local GetInner() const { return resolver_.Get(isolate()); } v8::Global resolver_; DISALLOW_COPY_AND_ASSIGN(Promise); }; } // namespace util } // namespace atom namespace mate { template <> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, 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_