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() {};
37     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     bool throwErr = false;
47 };
48 
49 struct BusinessError {
50     int32_t code = 0;
51     std::string data;
52 };
53 
54 struct NapiRefArrayContext {
55     napi_env env;
56     std::vector<napi_ref> napiRefVec;
57 };
58 
59 void ProcessCallbackOrPromise(napi_env env, const CommonAsyncContext *asyncContext, napi_value err, napi_value data);
60 void ReturnCallbackOrPromise(napi_env env, const CommonAsyncContext *asyncContext, napi_value err, napi_value data);
61 bool CreateExecEnv(napi_env env, uv_loop_s **loop, uv_work_t **work);
62 bool GetCallbackProperty(napi_env env, napi_value obj, napi_ref &property, int argNum);
63 bool GetIntProperty(napi_env env, napi_value obj, int32_t &property);
64 bool GetOptionIntProperty(napi_env env, napi_value obj, int32_t &property, bool &hasProperty);
65 bool GetLongIntProperty(napi_env env, napi_value obj, int64_t &property);
66 bool GetBoolProperty(napi_env env, napi_value obj, bool &property);
67 bool GetStringProperty(napi_env env, napi_value obj, std::string &property);
68 bool GetStringArrayProperty(napi_env env, napi_value obj, std::vector<std::string> &property, bool allowEmpty);
69 bool GetStringArrayPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName,
70     std::vector<std::string> &property, bool allowEmpty);
71 bool GetStringPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName, std::string &property);
72 bool GetOptionalStringPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName,
73     std::string &property);
74 bool GetOptionalStringPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName,
75     std::string &property, bool &hasProperty);
76 bool GetOptionalNumberPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName,
77     int32_t &numberProperty, bool &hasProperty);
78 void SetInt32ToJsProperty(napi_env env, int32_t number, const std::string &propertyName, napi_value &dataJs);
79 bool IsOptionalPropertyExist(napi_env env, napi_value obj, const std::string &propertyName);
80 bool CompareOnAndOffRef(const napi_env env, napi_ref subscriberRef, napi_ref unsubscriberRef);
81 bool IsSystemApp(napi_env env);
82 bool ParseBusinessError(napi_env env, napi_value value, BusinessError &error);
83 bool GetNamedJsFunction(napi_env env, napi_value object, const std::string &name, napi_ref &callback);
84 napi_value CreateStringArray(napi_env env, const std::vector<std::string> &strVec);
85 napi_value CreateUint8Array(napi_env env, const uint8_t *data, size_t length);
86 napi_status ParseUint8TypedArray(napi_env env, napi_value value, uint8_t **data, size_t *length);
87 napi_status ParseUint8ArrayToNativeUint8Array(napi_env env, napi_value value, uint8_t **data, size_t *length);
88 napi_status ParseUint8TypedArrayToVector(napi_env env, napi_value value, std::vector<uint8_t> &vec);
89 napi_status ParseUint8TypedArrayToUint64(napi_env env, napi_value value, uint64_t &result);
90 void NapiCallVoidFunction(napi_env env, napi_value *argv, size_t argc, napi_ref funcRef);
91 napi_value CreateAuthResult(
92     napi_env env, const std::vector<uint8_t> &authData, int32_t remainTimes, int32_t freezingTime);
93 void ReleaseNapiRefAsync(napi_env env, napi_ref napiRef);
94 void ReleaseNapiRefArray(napi_env env, const std::vector<napi_ref> &napiRefVec);
95 bool InitUvWorkCallbackEnv(uv_work_t *work, napi_handle_scope &scope);
96 bool JsObjectToNativeString(napi_env env, napi_value jsData, std::string &nativeData);
97 napi_value NativeStringToJsObject(napi_env env, const std::string &nativeData);
98 bool GetSelfTargetVersion(uint32_t &targetVersion);
99 
100 struct NapiCallbackRef {
NapiCallbackRefNapiCallbackRef101     NapiCallbackRef(napi_env env, napi_ref callbackRef) : env(env), callbackRef(callbackRef) {}
102     ~NapiCallbackRef();
103     napi_env env;
104     napi_ref callbackRef = nullptr;
105 };
106 } // namespace AccountJsKit
107 } // namespace OHOS
108 
109 #endif // OS_ACCOUNT_INTERFACES_KITS_COMMON_INCLUDE_NAPI_ACCOUNT_COMMON_H
110