1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef OHOS_NAPI_QUEUE_H 16 #define OHOS_NAPI_QUEUE_H 17 #include <functional> 18 #include <memory> 19 #include <string> 20 21 #include "calendar_log.h" 22 #include "napi/native_api.h" 23 #include "napi/native_common.h" 24 #include "napi/native_node_api.h" 25 26 namespace OHOS::CalendarApi { 27 constexpr size_t ARGC_MAX = 6; 28 using NapiCbInfoParser = std::function<void(size_t argc, napi_value* argv)>; 29 using NapiAsyncExecute = std::function<void(void)>; 30 using NapiAsyncComplete = std::function<void(napi_value&)>; 31 32 struct ContextBase { 33 virtual ~ContextBase(); 34 void GetCbInfo( 35 napi_env env, napi_callback_info info, NapiCbInfoParser parse = NapiCbInfoParser(), bool sync = false); 36 37 inline void GetCbInfoSync(napi_env env, napi_callback_info info, NapiCbInfoParser parse = NapiCbInfoParser()) 38 { 39 /* sync = true, means no callback, not AsyncWork. */ 40 GetCbInfo(env, info, parse, true); 41 } 42 43 napi_env env = nullptr; 44 napi_value output = nullptr; 45 napi_status status = napi_invalid_arg; 46 std::string error; 47 48 napi_value self = nullptr; 49 void* native = nullptr; 50 51 private: 52 napi_ref callbackRef = nullptr; 53 napi_ref selfRef = nullptr; 54 friend class NapiQueue; 55 }; 56 57 /* check condition related to argc/argv, return and logging. */ 58 #define CHECK_ARGS_RETURN_VOID(ctxt, condition, message) \ 59 do { \ 60 if (!(condition)) { \ 61 (ctxt)->status = napi_invalid_arg; \ 62 (ctxt)->error = std::string(message); \ 63 LOG_ERROR("test (" #condition ") failed: " message); \ 64 return; \ 65 } \ 66 } while (0) 67 68 #define CHECK_STATUS_RETURN_VOID(ctxt, message) \ 69 do { \ 70 if ((ctxt)->status != napi_ok) { \ 71 (ctxt)->error = std::string(message); \ 72 LOG_ERROR("test (ctxt->status %{public}d) failed: " message, (ctxt)->status); \ 73 return; \ 74 } \ 75 } while (0) 76 77 /* check condition, return and logging if condition not true. */ 78 #define CHECK_RETURN(condition, message, retVal) \ 79 do { \ 80 if (!(condition)) { \ 81 LOG_ERROR("test (" #condition ") failed: " message); \ 82 return retVal; \ 83 } \ 84 } while (0) 85 86 #define CHECK_RETURN_VOID(condition, message) \ 87 do { \ 88 if (!(condition)) { \ 89 LOG_ERROR("test (" #condition ") failed: " message); \ 90 return; \ 91 } \ 92 } while (0) 93 94 class NapiQueue { 95 public: 96 static napi_value AsyncWork(napi_env env, std::shared_ptr<ContextBase> ctxt, const std::string& name, 97 NapiAsyncExecute execute = NapiAsyncExecute(), NapiAsyncComplete complete = NapiAsyncComplete()); 98 99 private: 100 enum { 101 /* AsyncCallback / Promise output result index */ 102 RESULT_ERROR = 0, 103 RESULT_DATA = 1, 104 RESULT_ALL = 2 105 }; 106 107 struct AsyncContext { 108 napi_env env = nullptr; 109 std::shared_ptr<ContextBase> ctx; 110 NapiAsyncExecute execute = nullptr; 111 NapiAsyncComplete complete = nullptr; 112 napi_deferred deferred = nullptr; 113 napi_async_work work = nullptr; ~AsyncContextAsyncContext114 ~AsyncContext() 115 { 116 execute = nullptr; 117 complete = nullptr; 118 ctx = nullptr; 119 if (env != nullptr) { 120 if (work != nullptr) { 121 napi_delete_async_work(env, work); 122 } 123 } 124 } 125 }; 126 static void GenerateOutput(AsyncContext &ctx, napi_value output); 127 }; 128 } // namespace Calendar::CalendarApi 129 #endif // OHOS_NAPI_QUEUE_H 130