1 /* 2 * Copyright (c) 2022 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 NAPI_QUEUE_H 16 #define NAPI_QUEUE_H 17 #include <functional> 18 #include <memory> 19 #include <string> 20 21 #include "napi/native_api.h" 22 #include "napi/native_common.h" 23 #include "napi/native_node_api.h" 24 #include "object_error.h" 25 26 namespace OHOS::ObjectStore { 27 using NapiCbInfoParser = std::function<void(size_t argc, napi_value *argv)>; 28 using NapiAsyncExecute = std::function<void(void)>; 29 using NapiAsyncComplete = std::function<void(napi_value &)>; 30 static constexpr size_t ARGC_MAX = 6; 31 struct ContextBase { 32 virtual ~ContextBase(); 33 void GetCbInfo( 34 napi_env env, napi_callback_info info, NapiCbInfoParser parse = NapiCbInfoParser(), bool sync = false); 35 36 inline void GetCbInfoSync(napi_env env, napi_callback_info info, NapiCbInfoParser parse = NapiCbInfoParser()) 37 { 38 /* sync = true, means no callback, not AsyncWork. */ 39 GetCbInfo(env, info, parse, true); 40 } 41 SetErrorContextBase42 void SetError(std::shared_ptr<Error> err) 43 { 44 error = err; 45 } 46 47 napi_env env = nullptr; 48 napi_value output = nullptr; 49 napi_status status = napi_invalid_arg; 50 std::string message; 51 std::shared_ptr<Error> error; 52 napi_value self = nullptr; 53 void *native = nullptr; 54 55 private: 56 napi_deferred deferred = nullptr; 57 napi_async_work work = nullptr; 58 napi_ref callbackRef = nullptr; 59 napi_ref selfRef = nullptr; 60 61 NapiAsyncExecute execute = nullptr; 62 NapiAsyncComplete complete = nullptr; 63 std::shared_ptr<ContextBase> hold; /* cross thread data */ 64 65 friend class NapiQueue; 66 }; 67 68 /* check condition related to argc/argv, return and logging. */ 69 #define CHECK_ARGS_RETURN_VOID(ctxt, condition, message, err) \ 70 do { \ 71 if (!(condition)) { \ 72 (ctxt)->status = napi_invalid_arg; \ 73 (ctxt)->error = err; \ 74 LOG_ERROR("test (" #condition ") failed: " message); \ 75 return; \ 76 } \ 77 } while (0) 78 79 #define CHECK_STATUS_RETURN_VOID(ctxt, error) \ 80 do { \ 81 if ((ctxt)->status != napi_ok) { \ 82 (ctxt)->message = error; \ 83 LOG_ERROR("test (ctxt->status == napi_ok) failed: " error); \ 84 return; \ 85 } \ 86 } while (0) 87 88 class NapiQueue { 89 public: 90 static napi_value AsyncWork(napi_env env, std::shared_ptr<ContextBase> ctxt, const std::string &name, 91 NapiAsyncExecute execute = NapiAsyncExecute(), NapiAsyncComplete complete = NapiAsyncComplete()); 92 static void SetBusinessError(napi_env env, napi_value *businessError, std::shared_ptr<Error> error); 93 94 private: 95 enum { 96 /* AsyncCallback / Promise output result index */ 97 RESULT_ERROR = 0, 98 RESULT_DATA = 1, 99 RESULT_ALL = 2 100 }; 101 static void GenerateOutput(ContextBase *ctxt); 102 }; 103 } // namespace OHOS::ObjectStore 104 #endif // OHOS_NAPI_QUEUE_H 105