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 16 #ifndef OS_ACCOUNT_INTERFACES_KITS_COMMON_INCLUDE_NAPI_ACCOUNT_COMMON_H 17 #define OS_ACCOUNT_INTERFACES_KITS_COMMON_INCLUDE_NAPI_ACCOUNT_COMMON_H 18 19 #include <mutex> 20 #include <string> 21 #include <vector> 22 #include <uv.h> 23 24 #include "account_error_no.h" 25 #include "napi/native_api.h" 26 27 namespace OHOS { 28 namespace AccountJsKit { 29 struct CommonAsyncContext { CommonAsyncContextCommonAsyncContext30 CommonAsyncContext() {}; CommonAsyncContextCommonAsyncContext31 CommonAsyncContext(napi_env napiEnv) : env(napiEnv) {}; 32 napi_env env = nullptr; 33 napi_async_work work = nullptr; 34 napi_deferred deferred = nullptr; 35 napi_ref callbackRef = nullptr; 36 napi_status status = napi_ok; 37 ErrCode errCode = ERR_OK; 38 std::string errMsg; 39 bool throwErr = false; 40 }; 41 42 struct ThreadLockInfo { 43 std::mutex mutex; 44 std::condition_variable condition; 45 int32_t count = 0; 46 }; 47 48 struct NapiRefArrayContext { 49 napi_env env; 50 std::vector<napi_ref> napiRefVec; 51 }; 52 53 void ProcessCallbackOrPromise(napi_env env, const CommonAsyncContext *asyncContext, napi_value err, napi_value data); 54 void ReleaseNapiRefAsync(napi_env env, napi_ref napiRef); 55 void ReleaseNapiRefArray(napi_env env, const std::vector<napi_ref> &napiRefVec); 56 void NapiCallVoidFunction(napi_env env, napi_value *argv, size_t argc, napi_ref funcRef); 57 bool GetCallbackProperty(napi_env env, napi_value obj, napi_ref &property, int argNum); 58 bool GetIntProperty(napi_env env, napi_value obj, int32_t &property); 59 bool GetLongIntProperty(napi_env env, napi_value obj, int64_t &property); 60 bool GetBoolProperty(napi_env env, napi_value obj, bool &property); 61 bool GetStringProperty(napi_env env, napi_value obj, std::string &property); 62 bool GetStringArrayProperty(napi_env env, napi_value obj, std::vector<std::string> &property, bool allowEmpty); 63 bool GetStringPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName, std::string &property); 64 bool InitUvWorkCallbackEnv(uv_work_t *work, napi_handle_scope &scope); 65 bool GetOptionalStringPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName, 66 std::string &property); 67 napi_value CreateStringArray(napi_env env, const std::vector<std::string> &strVec); 68 napi_value CreateUint8Array(napi_env env, const uint8_t *data, size_t length); 69 napi_status ParseUint8TypedArray(napi_env env, napi_value value, uint8_t **data, size_t *length); 70 napi_status ParseUint8TypedArrayToVector(napi_env env, napi_value value, std::vector<uint8_t> &vec); 71 napi_status ParseUint8TypedArrayToUint64(napi_env env, napi_value value, uint64_t &result); 72 } // namespace AccountJsKit 73 } // namespace OHOS 74 75 #endif // OS_ACCOUNT_INTERFACES_KITS_COMMON_INCLUDE_NAPI_ACCOUNT_COMMON_H 76