1 /* 2 * Copyright (c) 2022-2024 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 ThreadLockInfo { 30 std::mutex mutex; 31 std::condition_variable condition; 32 int32_t count = 0; 33 }; 34 35 struct CommonAsyncContext { CommonAsyncContextCommonAsyncContext36 CommonAsyncContext() {}; envCommonAsyncContext37 CommonAsyncContext(napi_env napiEnv, bool throwAble = false) : env(napiEnv), throwErr(throwAble) {} 38 virtual ~CommonAsyncContext (); 39 napi_env env = nullptr; 40 napi_async_work work = nullptr; 41 napi_deferred deferred = nullptr; 42 napi_ref callbackRef = nullptr; 43 napi_status status = napi_ok; 44 ErrCode errCode = ERR_OK; 45 std::string errMsg; 46 std::string nativeErrMsg; 47 bool throwErr = false; 48 }; 49 50 struct BusinessError { 51 int32_t code = 0; 52 std::string data; 53 }; 54 55 struct NapiRefArrayContext { 56 napi_env env; 57 std::vector<napi_ref> napiRefVec; 58 }; 59 60 struct NapiCallbackRef { NapiCallbackRefNapiCallbackRef61 NapiCallbackRef(napi_env env, napi_ref callbackRef) : env(env), callbackRef(callbackRef) {} 62 ~NapiCallbackRef(); 63 napi_env env; 64 napi_ref callbackRef = nullptr; 65 }; 66 67 class NapiAsyncContext { 68 public: NapiAsyncContext()69 NapiAsyncContext() {}; NapiAsyncContext(napi_env env,napi_ref ref)70 NapiAsyncContext(napi_env env, napi_ref ref) : callbackRef(std::make_shared<NapiCallbackRef>(env, ref)) {}; NapiAsyncContext(std::shared_ptr<NapiCallbackRef> & callbackRef)71 NapiAsyncContext(std::shared_ptr<NapiCallbackRef> &callbackRef) : callbackRef(callbackRef) {}; 72 NapiAsyncContext(napi_env env, bool throwAble = false) env(env)73 : env(env), throwErr(throwAble) {}; 74 napi_env env = nullptr; 75 napi_async_work work = nullptr; 76 napi_deferred deferred = nullptr; 77 std::shared_ptr<NapiCallbackRef> callbackRef = nullptr; 78 napi_status status = napi_ok; 79 ErrCode errCode = ERR_OK; 80 std::string errMsg; 81 std::string nativeErrMsg; 82 bool throwErr = false; 83 }; 84 85 void ProcessCallbackOrPromise(napi_env env, const CommonAsyncContext *asyncContext, napi_value err, napi_value data); 86 void ProcessCallbackOrPromise(napi_env env, const NapiAsyncContext *asyncContext, napi_value err, napi_value data); 87 void ReturnCallbackOrPromise(napi_env env, const CommonAsyncContext *asyncContext, napi_value err, napi_value data); 88 bool GetCallbackProperty(napi_env env, napi_value obj, napi_ref &property, int argNum); 89 bool GetIntProperty(napi_env env, napi_value obj, int32_t &property); 90 bool GetOptionIntProperty(napi_env env, napi_value obj, int32_t &property, bool &hasProperty); 91 bool GetLongIntProperty(napi_env env, napi_value obj, int64_t &property); 92 bool GetBoolProperty(napi_env env, napi_value obj, bool &property); 93 bool GetStringProperty(napi_env env, napi_value obj, std::string &property); 94 bool GetStringArrayProperty(napi_env env, napi_value obj, std::vector<std::string> &property, bool allowEmpty); 95 bool GetStringArrayPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName, 96 std::vector<std::string> &property, bool allowEmpty); 97 bool GetStringPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName, std::string &property); 98 bool GetOptionalStringPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName, 99 std::string &property); 100 bool GetOptionalStringPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName, 101 std::string &property, bool &hasProperty); 102 bool GetOptionalNumberPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName, 103 int32_t &numberProperty, bool &hasProperty); 104 void SetInt32ToJsProperty(napi_env env, int32_t number, const std::string &propertyName, napi_value &dataJs); 105 bool IsOptionalPropertyExist(napi_env env, napi_value obj, const std::string &propertyName); 106 bool CompareOnAndOffRef(const napi_env env, napi_ref subscriberRef, napi_ref unsubscriberRef); 107 bool IsSystemApp(napi_env env); 108 bool ParseBusinessError(napi_env env, napi_value value, BusinessError &error); 109 bool GetNamedJsFunction(napi_env env, napi_value object, const std::string &name, napi_ref &callback); 110 napi_value CreateStringArray(napi_env env, const std::vector<std::string> &strVec); 111 napi_value CreateUint8Array(napi_env env, const uint8_t *data, size_t length); 112 napi_status ParseUint8TypedArray(napi_env env, napi_value value, uint8_t **data, size_t *length); 113 napi_status ParseUint8ArrayToNativeUint8Array(napi_env env, napi_value value, uint8_t **data, size_t *length); 114 napi_status ParseUint8TypedArrayToVector(napi_env env, napi_value value, std::vector<uint8_t> &vec); 115 napi_status ParseUint8TypedArrayToUint64(napi_env env, napi_value value, uint64_t &result); 116 void NapiCallVoidFunction(napi_env env, napi_value *argv, size_t argc, napi_ref funcRef); 117 napi_value CreateAuthResult( 118 napi_env env, const std::vector<uint8_t> &authData, int32_t remainTimes, int32_t freezingTime); 119 void ReleaseNapiRefAsync(napi_env env, napi_ref napiRef); 120 void ReleaseNapiRefArray(napi_env env, const std::vector<napi_ref> &napiRefVec); 121 bool JsObjectToNativeString(napi_env env, napi_value jsData, std::string &nativeData); 122 napi_value NativeStringToJsObject(napi_env env, const std::string &nativeData); 123 bool GetSelfTargetVersion(uint32_t &targetVersion); 124 } // namespace AccountJsKit 125 } // namespace OHOS 126 127 #endif // OS_ACCOUNT_INTERFACES_KITS_COMMON_INCLUDE_NAPI_ACCOUNT_COMMON_H 128