• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 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 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 GetStringPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName, std::string &property);
69 bool GetOptionalStringPropertyByKey(napi_env env, napi_value obj, const std::string &propertyName,
70     std::string &property);
71 bool CompareOnAndOffRef(const napi_env env, napi_ref subscriberRef, napi_ref unsubscriberRef);
72 bool IsSystemApp(napi_env env);
73 bool ParseBusinessError(napi_env env, napi_value value, BusinessError &error);
74 bool GetNamedJsFunction(napi_env env, napi_value object, const std::string &name, napi_ref &callback);
75 napi_value CreateStringArray(napi_env env, const std::vector<std::string> &strVec);
76 napi_value CreateUint8Array(napi_env env, const uint8_t *data, size_t length);
77 napi_status ParseUint8TypedArray(napi_env env, napi_value value, uint8_t **data, size_t *length);
78 napi_status ParseUint8TypedArrayToVector(napi_env env, napi_value value, std::vector<uint8_t> &vec);
79 napi_status ParseUint8TypedArrayToUint64(napi_env env, napi_value value, uint64_t &result);
80 void NapiCallVoidFunction(napi_env env, napi_value *argv, size_t argc, napi_ref funcRef);
81 napi_value CreateAuthResult(
82     napi_env env, const std::vector<uint8_t> &authData, int32_t remainTimes, int32_t freezingTime);
83 void ReleaseNapiRefAsync(napi_env env, napi_ref napiRef);
84 void ReleaseNapiRefArray(napi_env env, const std::vector<napi_ref> &napiRefVec);
85 bool InitUvWorkCallbackEnv(uv_work_t *work, napi_handle_scope &scope);
86 } // namespace AccountJsKit
87 } // namespace OHOS
88 
89 #endif // OS_ACCOUNT_INTERFACES_KITS_COMMON_INCLUDE_NAPI_ACCOUNT_COMMON_H
90