promise_util.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (c) 2018 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ATOM_COMMON_PROMISE_UTIL_H_
  5. #define ATOM_COMMON_PROMISE_UTIL_H_
  6. #include <string>
  7. #include "atom/common/api/locker.h"
  8. #include "atom/common/native_mate_converters/callback.h"
  9. #include "content/public/browser/browser_thread.h"
  10. #include "native_mate/converter.h"
  11. namespace atom {
  12. namespace util {
  13. // A wrapper around the v8::Promise.
  14. //
  15. // This is a move-only type that should always be `std::move`d when passed to
  16. // callbacks, and it should be destroyed on the same thread of creation.
  17. class Promise {
  18. public:
  19. // Create a new promise.
  20. explicit Promise(v8::Isolate* isolate);
  21. // Wrap an existing v8 promise.
  22. Promise(v8::Isolate* isolate, v8::Local<v8::Promise::Resolver> handle);
  23. ~Promise();
  24. // Support moving.
  25. Promise(Promise&&);
  26. Promise& operator=(Promise&&);
  27. v8::Isolate* isolate() const { return isolate_; }
  28. v8::Local<v8::Context> GetContext() {
  29. return v8::Local<v8::Context>::New(isolate_, context_);
  30. }
  31. v8::Local<v8::Promise> GetHandle() const;
  32. v8::Maybe<bool> Resolve() {
  33. v8::HandleScope handle_scope(isolate());
  34. v8::MicrotasksScope script_scope(isolate(),
  35. v8::MicrotasksScope::kRunMicrotasks);
  36. v8::Context::Scope context_scope(
  37. v8::Local<v8::Context>::New(isolate(), GetContext()));
  38. return GetInner()->Resolve(GetContext(), v8::Undefined(isolate()));
  39. }
  40. v8::Maybe<bool> Reject() {
  41. v8::HandleScope handle_scope(isolate());
  42. v8::MicrotasksScope script_scope(isolate(),
  43. v8::MicrotasksScope::kRunMicrotasks);
  44. v8::Context::Scope context_scope(
  45. v8::Local<v8::Context>::New(isolate(), GetContext()));
  46. return GetInner()->Reject(GetContext(), v8::Undefined(isolate()));
  47. }
  48. template <typename ReturnType, typename... ArgTypes>
  49. v8::MaybeLocal<v8::Promise> Then(base::Callback<ReturnType(ArgTypes...)> cb) {
  50. v8::HandleScope handle_scope(isolate());
  51. v8::Context::Scope context_scope(
  52. v8::Local<v8::Context>::New(isolate(), GetContext()));
  53. v8::Local<v8::Value> value = mate::ConvertToV8(isolate(), cb);
  54. v8::Local<v8::Function> handler = v8::Local<v8::Function>::Cast(value);
  55. return GetHandle()->Then(GetContext(), handler);
  56. }
  57. // Promise resolution is a microtask
  58. // We use the MicrotasksRunner to trigger the running of pending microtasks
  59. template <typename T>
  60. v8::Maybe<bool> Resolve(const T& value) {
  61. v8::HandleScope handle_scope(isolate());
  62. v8::MicrotasksScope script_scope(isolate(),
  63. v8::MicrotasksScope::kRunMicrotasks);
  64. v8::Context::Scope context_scope(
  65. v8::Local<v8::Context>::New(isolate(), GetContext()));
  66. return GetInner()->Resolve(GetContext(),
  67. mate::ConvertToV8(isolate(), value));
  68. }
  69. template <typename T>
  70. v8::Maybe<bool> Reject(const T& value) {
  71. v8::HandleScope handle_scope(isolate());
  72. v8::MicrotasksScope script_scope(isolate(),
  73. v8::MicrotasksScope::kRunMicrotasks);
  74. v8::Context::Scope context_scope(
  75. v8::Local<v8::Context>::New(isolate(), GetContext()));
  76. return GetInner()->Reject(GetContext(),
  77. mate::ConvertToV8(isolate(), value));
  78. }
  79. v8::Maybe<bool> RejectWithErrorMessage(const std::string& error);
  80. private:
  81. friend class CopyablePromise;
  82. v8::Local<v8::Promise::Resolver> GetInner() const {
  83. return resolver_.Get(isolate());
  84. }
  85. v8::Isolate* isolate_;
  86. v8::Global<v8::Context> context_;
  87. v8::Global<v8::Promise::Resolver> resolver_;
  88. DISALLOW_COPY_AND_ASSIGN(Promise);
  89. };
  90. // A wrapper of Promise that can be copied.
  91. //
  92. // This class should only be used when we have to pass Promise to a Chromium API
  93. // that does not take OnceCallback.
  94. class CopyablePromise {
  95. public:
  96. explicit CopyablePromise(const Promise& promise);
  97. CopyablePromise(const CopyablePromise&);
  98. ~CopyablePromise();
  99. Promise GetPromise() const;
  100. private:
  101. using CopyablePersistent =
  102. v8::CopyablePersistentTraits<v8::Promise::Resolver>::CopyablePersistent;
  103. v8::Isolate* isolate_;
  104. CopyablePersistent handle_;
  105. };
  106. } // namespace util
  107. } // namespace atom
  108. namespace mate {
  109. template <>
  110. struct Converter<atom::util::Promise> {
  111. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  112. const atom::util::Promise& val);
  113. // TODO(MarshallOfSound): Implement FromV8 to allow promise chaining
  114. // in native land
  115. // static bool FromV8(v8::Isolate* isolate,
  116. // v8::Local<v8::Value> val,
  117. // Promise* out);
  118. };
  119. } // namespace mate
  120. #endif // ATOM_COMMON_PROMISE_UTIL_H_