• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     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 GetCallbackProperty(napi_env env, napi_value obj, napi_ref &property, int argNum);
62 bool GetIntProperty(napi_env env, napi_value obj, int32_t &property);
63 bool GetOptionIntProperty(napi_env env, napi_value obj, int32_t &property, bool &hasProperty);
64 bool GetLongIntProperty(napi_env env, napi_value obj, int64_t &property);
65 bool GetBoolProperty(napi_env env, napi_value obj, bool &property);
66 bool GetStringProperty(napi_env env, napi_value obj, std::string &property);
67 bool GetStringArrayProperty(napi_env env, napi_value obj, std::vector<std::string> &property, bool allowEmpty);
68 bool GetStringArrayPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName,
69     std::vector<std::string> &property, bool allowEmpty);
70 bool GetStringPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName, std::string &property);
71 bool GetOptionalStringPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName,
72     std::string &property);
73 bool GetOptionalStringPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName,
74     std::string &property, bool &hasProperty);
75 bool GetOptionalNumberPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName,
76     int32_t &numberProperty, bool &hasProperty);
77 void SetInt32ToJsProperty(napi_env env, int32_t number, const std::string &propertyName, napi_value &dataJs);
78 bool IsOptionalPropertyExist(napi_env env, napi_value obj, const std::string &propertyName);
79 bool CompareOnAndOffRef(const napi_env env, napi_ref subscriberRef, napi_ref unsubscriberRef);
80 bool IsSystemApp(napi_env env);
81 bool ParseBusinessError(napi_env env, napi_value value, BusinessError &error);
82 bool GetNamedJsFunction(napi_env env, napi_value object, const std::string &name, napi_ref &callback);
83 napi_value CreateStringArray(napi_env env, const std::vector<std::string> &strVec);
84 napi_value CreateUint8Array(napi_env env, const uint8_t *data, size_t length);
85 napi_status ParseUint8TypedArray(napi_env env, napi_value value, uint8_t **data, size_t *length);
86 napi_status ParseUint8ArrayToNativeUint8Array(napi_env env, napi_value value, uint8_t **data, size_t *length);
87 napi_status ParseUint8TypedArrayToVector(napi_env env, napi_value value, std::vector<uint8_t> &vec);
88 napi_status ParseUint8TypedArrayToUint64(napi_env env, napi_value value, uint64_t &result);
89 void NapiCallVoidFunction(napi_env env, napi_value *argv, size_t argc, napi_ref funcRef);
90 napi_value CreateAuthResult(
91     napi_env env, const std::vector<uint8_t> &authData, int32_t remainTimes, int32_t freezingTime);
92 void ReleaseNapiRefAsync(napi_env env, napi_ref napiRef);
93 void ReleaseNapiRefArray(napi_env env, const std::vector<napi_ref> &napiRefVec);
94 bool JsObjectToNativeString(napi_env env, napi_value jsData, std::string &nativeData);
95 napi_value NativeStringToJsObject(napi_env env, const std::string &nativeData);
96 bool GetSelfTargetVersion(uint32_t &targetVersion);
97 
98 struct NapiCallbackRef {
NapiCallbackRefNapiCallbackRef99     NapiCallbackRef(napi_env env, napi_ref callbackRef) : env(env), callbackRef(callbackRef) {}
100     ~NapiCallbackRef();
101     napi_env env;
102     napi_ref callbackRef = nullptr;
103 };
104 } // namespace AccountJsKit
105 } // namespace OHOS
106 
107 #endif // OS_ACCOUNT_INTERFACES_KITS_COMMON_INCLUDE_NAPI_ACCOUNT_COMMON_H
108