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_ref callbackRef = nullptr; 58 napi_ref selfRef = nullptr; 59 60 NapiAsyncExecute execute = nullptr; 61 NapiAsyncComplete complete = nullptr; 62 std::shared_ptr<ContextBase> hold; /* cross thread data */ 63 64 friend class NapiQueue; 65 }; 66 67 /* check condition related to argc/argv, return and logging. */ 68 #define INVALID_ARGS_RETURN_ERROR(ctxt, condition, message, err) \ 69 do { \ 70 if (!(condition)) { \ 71 (ctxt)->status = napi_invalid_arg; \ 72 (ctxt)->error = err; \ 73 LOG_ERROR("test (" #condition ") failed: " message); \ 74 return; \ 75 } \ 76 } while (0) 77 78 #define INVALID_STATUS_RETURN_ERROR(ctxt, error) \ 79 do { \ 80 if ((ctxt)->status != napi_ok) { \ 81 (ctxt)->message = error; \ 82 LOG_ERROR("test (ctxt->status == napi_ok) failed: " error); \ 83 return; \ 84 } \ 85 } while (0) 86 87 class NapiQueue { 88 public: 89 static napi_value AsyncWork(napi_env env, std::shared_ptr<ContextBase> contextBase, const std::string &name, 90 NapiAsyncExecute execute = NapiAsyncExecute(), NapiAsyncComplete complete = NapiAsyncComplete()); 91 static void SetBusinessError(napi_env env, napi_value *businessError, std::shared_ptr<Error> error); 92 93 private: 94 enum { 95 /* AsyncCallback / Promise output result index */ 96 RESULT_ERROR = 0, 97 RESULT_DATA = 1, 98 RESULT_ALL = 2 99 }; 100 struct AsyncContext { 101 std::shared_ptr<ContextBase> ctxt; 102 NapiAsyncExecute execute = nullptr; 103 NapiAsyncComplete complete = nullptr; 104 napi_deferred deferred = nullptr; 105 napi_async_work work = nullptr; 106 }; 107 static void GenerateOutput(ContextBase *ctxt); 108 }; 109 } // namespace OHOS::ObjectStore 110 #endif // OHOS_NAPI_QUEUE_H 111