11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci#ifndef SRC_ASYNC_WRAP_H_
231cb0ef41Sopenharmony_ci#define SRC_ASYNC_WRAP_H_
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci#include "base_object.h"
281cb0ef41Sopenharmony_ci#include "v8.h"
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci#include <cstdint>
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cinamespace node {
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci#define NODE_ASYNC_NON_CRYPTO_PROVIDER_TYPES(V)                               \
351cb0ef41Sopenharmony_ci  V(NONE)                                                                     \
361cb0ef41Sopenharmony_ci  V(DIRHANDLE)                                                                \
371cb0ef41Sopenharmony_ci  V(DNSCHANNEL)                                                               \
381cb0ef41Sopenharmony_ci  V(ELDHISTOGRAM)                                                             \
391cb0ef41Sopenharmony_ci  V(FILEHANDLE)                                                               \
401cb0ef41Sopenharmony_ci  V(FILEHANDLECLOSEREQ)                                                       \
411cb0ef41Sopenharmony_ci  V(FIXEDSIZEBLOBCOPY)                                                        \
421cb0ef41Sopenharmony_ci  V(FSEVENTWRAP)                                                              \
431cb0ef41Sopenharmony_ci  V(FSREQCALLBACK)                                                            \
441cb0ef41Sopenharmony_ci  V(FSREQPROMISE)                                                             \
451cb0ef41Sopenharmony_ci  V(GETADDRINFOREQWRAP)                                                       \
461cb0ef41Sopenharmony_ci  V(GETNAMEINFOREQWRAP)                                                       \
471cb0ef41Sopenharmony_ci  V(HEAPSNAPSHOT)                                                             \
481cb0ef41Sopenharmony_ci  V(HTTP2SESSION)                                                             \
491cb0ef41Sopenharmony_ci  V(HTTP2STREAM)                                                              \
501cb0ef41Sopenharmony_ci  V(HTTP2PING)                                                                \
511cb0ef41Sopenharmony_ci  V(HTTP2SETTINGS)                                                            \
521cb0ef41Sopenharmony_ci  V(HTTPINCOMINGMESSAGE)                                                      \
531cb0ef41Sopenharmony_ci  V(HTTPCLIENTREQUEST)                                                        \
541cb0ef41Sopenharmony_ci  V(JSSTREAM)                                                                 \
551cb0ef41Sopenharmony_ci  V(JSUDPWRAP)                                                                \
561cb0ef41Sopenharmony_ci  V(MESSAGEPORT)                                                              \
571cb0ef41Sopenharmony_ci  V(PIPECONNECTWRAP)                                                          \
581cb0ef41Sopenharmony_ci  V(PIPESERVERWRAP)                                                           \
591cb0ef41Sopenharmony_ci  V(PIPEWRAP)                                                                 \
601cb0ef41Sopenharmony_ci  V(PROCESSWRAP)                                                              \
611cb0ef41Sopenharmony_ci  V(PROMISE)                                                                  \
621cb0ef41Sopenharmony_ci  V(QUERYWRAP)                                                                \
631cb0ef41Sopenharmony_ci  V(QUIC_LOGSTREAM)                                                           \
641cb0ef41Sopenharmony_ci  V(QUIC_PACKET)                                                              \
651cb0ef41Sopenharmony_ci  V(SHUTDOWNWRAP)                                                             \
661cb0ef41Sopenharmony_ci  V(SIGNALWRAP)                                                               \
671cb0ef41Sopenharmony_ci  V(STATWATCHER)                                                              \
681cb0ef41Sopenharmony_ci  V(STREAMPIPE)                                                               \
691cb0ef41Sopenharmony_ci  V(TCPCONNECTWRAP)                                                           \
701cb0ef41Sopenharmony_ci  V(TCPSERVERWRAP)                                                            \
711cb0ef41Sopenharmony_ci  V(TCPWRAP)                                                                  \
721cb0ef41Sopenharmony_ci  V(TTYWRAP)                                                                  \
731cb0ef41Sopenharmony_ci  V(UDPSENDWRAP)                                                              \
741cb0ef41Sopenharmony_ci  V(UDPWRAP)                                                                  \
751cb0ef41Sopenharmony_ci  V(SIGINTWATCHDOG)                                                           \
761cb0ef41Sopenharmony_ci  V(WORKER)                                                                   \
771cb0ef41Sopenharmony_ci  V(WORKERHEAPSNAPSHOT)                                                       \
781cb0ef41Sopenharmony_ci  V(WRITEWRAP)                                                                \
791cb0ef41Sopenharmony_ci  V(ZLIB)
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci#if HAVE_OPENSSL
821cb0ef41Sopenharmony_ci#define NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V)                                   \
831cb0ef41Sopenharmony_ci  V(CHECKPRIMEREQUEST)                                                        \
841cb0ef41Sopenharmony_ci  V(PBKDF2REQUEST)                                                            \
851cb0ef41Sopenharmony_ci  V(KEYPAIRGENREQUEST)                                                        \
861cb0ef41Sopenharmony_ci  V(KEYGENREQUEST)                                                            \
871cb0ef41Sopenharmony_ci  V(KEYEXPORTREQUEST)                                                         \
881cb0ef41Sopenharmony_ci  V(CIPHERREQUEST)                                                            \
891cb0ef41Sopenharmony_ci  V(DERIVEBITSREQUEST)                                                        \
901cb0ef41Sopenharmony_ci  V(HASHREQUEST)                                                              \
911cb0ef41Sopenharmony_ci  V(RANDOMBYTESREQUEST)                                                       \
921cb0ef41Sopenharmony_ci  V(RANDOMPRIMEREQUEST)                                                       \
931cb0ef41Sopenharmony_ci  V(SCRYPTREQUEST)                                                            \
941cb0ef41Sopenharmony_ci  V(SIGNREQUEST)                                                              \
951cb0ef41Sopenharmony_ci  V(TLSWRAP)                                                                  \
961cb0ef41Sopenharmony_ci  V(VERIFYREQUEST)
971cb0ef41Sopenharmony_ci#else
981cb0ef41Sopenharmony_ci#define NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V)
991cb0ef41Sopenharmony_ci#endif  // HAVE_OPENSSL
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci#if HAVE_INSPECTOR
1021cb0ef41Sopenharmony_ci#define NODE_ASYNC_INSPECTOR_PROVIDER_TYPES(V)                                \
1031cb0ef41Sopenharmony_ci  V(INSPECTORJSBINDING)
1041cb0ef41Sopenharmony_ci#else
1051cb0ef41Sopenharmony_ci#define NODE_ASYNC_INSPECTOR_PROVIDER_TYPES(V)
1061cb0ef41Sopenharmony_ci#endif  // HAVE_INSPECTOR
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci#define NODE_ASYNC_PROVIDER_TYPES(V)                                          \
1091cb0ef41Sopenharmony_ci  NODE_ASYNC_NON_CRYPTO_PROVIDER_TYPES(V)                                     \
1101cb0ef41Sopenharmony_ci  NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V)                                         \
1111cb0ef41Sopenharmony_ci  NODE_ASYNC_INSPECTOR_PROVIDER_TYPES(V)
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ciclass Environment;
1141cb0ef41Sopenharmony_ciclass DestroyParam;
1151cb0ef41Sopenharmony_ciclass ExternalReferenceRegistry;
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ciclass AsyncWrap : public BaseObject {
1181cb0ef41Sopenharmony_ci public:
1191cb0ef41Sopenharmony_ci  enum ProviderType {
1201cb0ef41Sopenharmony_ci#define V(PROVIDER)                                                           \
1211cb0ef41Sopenharmony_ci    PROVIDER_ ## PROVIDER,
1221cb0ef41Sopenharmony_ci    NODE_ASYNC_PROVIDER_TYPES(V)
1231cb0ef41Sopenharmony_ci#undef V
1241cb0ef41Sopenharmony_ci    PROVIDERS_LENGTH,
1251cb0ef41Sopenharmony_ci  };
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci  AsyncWrap(Environment* env,
1281cb0ef41Sopenharmony_ci            v8::Local<v8::Object> object,
1291cb0ef41Sopenharmony_ci            ProviderType provider,
1301cb0ef41Sopenharmony_ci            double execution_async_id = kInvalidAsyncId);
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  // This constructor creates a reusable instance where user is responsible
1331cb0ef41Sopenharmony_ci  // to call set_provider_type() and AsyncReset() before use.
1341cb0ef41Sopenharmony_ci  AsyncWrap(Environment* env, v8::Local<v8::Object> object);
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ci  ~AsyncWrap() override;
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci  AsyncWrap() = delete;
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci  static constexpr double kInvalidAsyncId = -1;
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci  static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
1431cb0ef41Sopenharmony_ci      Environment* env);
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci  static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
1461cb0ef41Sopenharmony_ci  static void Initialize(v8::Local<v8::Object> target,
1471cb0ef41Sopenharmony_ci                         v8::Local<v8::Value> unused,
1481cb0ef41Sopenharmony_ci                         v8::Local<v8::Context> context,
1491cb0ef41Sopenharmony_ci                         void* priv);
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci  static void GetAsyncId(const v8::FunctionCallbackInfo<v8::Value>& args);
1521cb0ef41Sopenharmony_ci  static void PushAsyncContext(const v8::FunctionCallbackInfo<v8::Value>& args);
1531cb0ef41Sopenharmony_ci  static void PopAsyncContext(const v8::FunctionCallbackInfo<v8::Value>& args);
1541cb0ef41Sopenharmony_ci  static void ExecutionAsyncResource(
1551cb0ef41Sopenharmony_ci      const v8::FunctionCallbackInfo<v8::Value>& args);
1561cb0ef41Sopenharmony_ci  static void ClearAsyncIdStack(
1571cb0ef41Sopenharmony_ci      const v8::FunctionCallbackInfo<v8::Value>& args);
1581cb0ef41Sopenharmony_ci  static void AsyncReset(const v8::FunctionCallbackInfo<v8::Value>& args);
1591cb0ef41Sopenharmony_ci  static void GetProviderType(const v8::FunctionCallbackInfo<v8::Value>& args);
1601cb0ef41Sopenharmony_ci  static void QueueDestroyAsyncId(
1611cb0ef41Sopenharmony_ci    const v8::FunctionCallbackInfo<v8::Value>& args);
1621cb0ef41Sopenharmony_ci  static void SetCallbackTrampoline(
1631cb0ef41Sopenharmony_ci    const v8::FunctionCallbackInfo<v8::Value>& args);
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci  static void EmitAsyncInit(Environment* env,
1661cb0ef41Sopenharmony_ci                            v8::Local<v8::Object> object,
1671cb0ef41Sopenharmony_ci                            v8::Local<v8::String> type,
1681cb0ef41Sopenharmony_ci                            double async_id,
1691cb0ef41Sopenharmony_ci                            double trigger_async_id);
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci  static void EmitDestroy(Environment* env, double async_id);
1721cb0ef41Sopenharmony_ci  static void EmitBefore(Environment* env, double async_id);
1731cb0ef41Sopenharmony_ci  static void EmitAfter(Environment* env, double async_id);
1741cb0ef41Sopenharmony_ci  static void EmitPromiseResolve(Environment* env, double async_id);
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_ci  void EmitDestroy(bool from_gc = false);
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ci  void EmitTraceEventBefore();
1791cb0ef41Sopenharmony_ci  static void EmitTraceEventAfter(ProviderType type, double async_id);
1801cb0ef41Sopenharmony_ci  void EmitTraceEventDestroy();
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ci  static void DestroyAsyncIdsCallback(Environment* env);
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci  inline ProviderType provider_type() const;
1851cb0ef41Sopenharmony_ci  inline ProviderType set_provider_type(ProviderType provider);
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_ci  inline double get_async_id() const;
1881cb0ef41Sopenharmony_ci  inline double get_trigger_async_id() const;
1891cb0ef41Sopenharmony_ci
1901cb0ef41Sopenharmony_ci  void AsyncReset(v8::Local<v8::Object> resource,
1911cb0ef41Sopenharmony_ci                  double execution_async_id = kInvalidAsyncId,
1921cb0ef41Sopenharmony_ci                  bool silent = false);
1931cb0ef41Sopenharmony_ci
1941cb0ef41Sopenharmony_ci  // Only call these within a valid HandleScope.
1951cb0ef41Sopenharmony_ci  v8::MaybeLocal<v8::Value> MakeCallback(const v8::Local<v8::Function> cb,
1961cb0ef41Sopenharmony_ci                                         int argc,
1971cb0ef41Sopenharmony_ci                                         v8::Local<v8::Value>* argv);
1981cb0ef41Sopenharmony_ci  inline v8::MaybeLocal<v8::Value> MakeCallback(
1991cb0ef41Sopenharmony_ci      const v8::Local<v8::Symbol> symbol,
2001cb0ef41Sopenharmony_ci      int argc,
2011cb0ef41Sopenharmony_ci      v8::Local<v8::Value>* argv);
2021cb0ef41Sopenharmony_ci  inline v8::MaybeLocal<v8::Value> MakeCallback(
2031cb0ef41Sopenharmony_ci      const v8::Local<v8::String> symbol,
2041cb0ef41Sopenharmony_ci      int argc,
2051cb0ef41Sopenharmony_ci      v8::Local<v8::Value>* argv);
2061cb0ef41Sopenharmony_ci  inline v8::MaybeLocal<v8::Value> MakeCallback(
2071cb0ef41Sopenharmony_ci      const v8::Local<v8::Name> symbol,
2081cb0ef41Sopenharmony_ci      int argc,
2091cb0ef41Sopenharmony_ci      v8::Local<v8::Value>* argv);
2101cb0ef41Sopenharmony_ci
2111cb0ef41Sopenharmony_ci  virtual std::string diagnostic_name() const;
2121cb0ef41Sopenharmony_ci  const char* MemoryInfoName() const override;
2131cb0ef41Sopenharmony_ci
2141cb0ef41Sopenharmony_ci  static void WeakCallback(const v8::WeakCallbackInfo<DestroyParam> &info);
2151cb0ef41Sopenharmony_ci
2161cb0ef41Sopenharmony_ci  // Returns the object that 'owns' an async wrap. For example, for a
2171cb0ef41Sopenharmony_ci  // TCP connection handle, this is the corresponding net.Socket.
2181cb0ef41Sopenharmony_ci  v8::Local<v8::Object> GetOwner();
2191cb0ef41Sopenharmony_ci  static v8::Local<v8::Object> GetOwner(Environment* env,
2201cb0ef41Sopenharmony_ci                                        v8::Local<v8::Object> obj);
2211cb0ef41Sopenharmony_ci
2221cb0ef41Sopenharmony_ci  bool IsDoneInitializing() const override;
2231cb0ef41Sopenharmony_ci
2241cb0ef41Sopenharmony_ci private:
2251cb0ef41Sopenharmony_ci  friend class PromiseWrap;
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ci  AsyncWrap(Environment* env,
2281cb0ef41Sopenharmony_ci            v8::Local<v8::Object> promise,
2291cb0ef41Sopenharmony_ci            ProviderType provider,
2301cb0ef41Sopenharmony_ci            double execution_async_id,
2311cb0ef41Sopenharmony_ci            bool silent);
2321cb0ef41Sopenharmony_ci  AsyncWrap(Environment* env,
2331cb0ef41Sopenharmony_ci            v8::Local<v8::Object> promise,
2341cb0ef41Sopenharmony_ci            ProviderType provider,
2351cb0ef41Sopenharmony_ci            double execution_async_id,
2361cb0ef41Sopenharmony_ci            double trigger_async_id);
2371cb0ef41Sopenharmony_ci  ProviderType provider_type_ = PROVIDER_NONE;
2381cb0ef41Sopenharmony_ci  bool init_hook_ran_ = false;
2391cb0ef41Sopenharmony_ci  // Because the values may be Reset(), cannot be made const.
2401cb0ef41Sopenharmony_ci  double async_id_ = kInvalidAsyncId;
2411cb0ef41Sopenharmony_ci  double trigger_async_id_ = kInvalidAsyncId;
2421cb0ef41Sopenharmony_ci};
2431cb0ef41Sopenharmony_ci
2441cb0ef41Sopenharmony_ci}  // namespace node
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_ci#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
2471cb0ef41Sopenharmony_ci
2481cb0ef41Sopenharmony_ci#endif  // SRC_ASYNC_WRAP_H_
249