1 /*
2  * Copyright (c) 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 NAPI_COMMON_UTILS_H
17 #define NAPI_COMMON_UTILS_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "js_native_api.h"
23 #include "js_native_api_types.h"
24 
25 #include "business_error.h"
26 #include "call_result.h"
27 #include "error_message.h"
28 #include "napi_common_define.h"
29 
30 namespace OHOS::UpdateEngine {
31 class NapiCommonUtils {
32 public:
33     static int32_t GetBool(napi_env env, napi_value arg, const std::string &attrName, bool &value);
34     static int32_t GetInt32(napi_env env, napi_value arg, const std::string &attrName, int32_t &intValue);
35     static int32_t GetInt32(napi_env env, napi_value arg, int32_t &intValue);
36     static int32_t GetUInt32(napi_env env, napi_value arg, uint32_t &uintValue);
37     static int32_t GetString(napi_env env, napi_value arg, const std::string &attrName, std::string &strValue);
38     static int32_t GetString(napi_env env, napi_value arg, std::string &strValue);
39 
40     static int32_t SetBool(napi_env env, napi_value arg, const std::string &attrName, bool value);
41     static int32_t SetInt32(napi_env env, napi_value arg, const std::string &attrName, int32_t intValue);
42     static int32_t SetInt64(napi_env env, napi_value arg, const std::string &attrName, int64_t intValue);
43     static int32_t SetString(napi_env env, napi_value arg, const std::string &attrName, const std::string &string);
44     static int32_t SetUint32(napi_env env, napi_value arg, const std::string &attrName, uint32_t intValue);
45 
46     static napi_value CreateUint32(napi_env env, uint32_t code);
47     static napi_value CreateStringUtf8(napi_env env, const std::string &str);
48     static ClientStatus CreateReference(napi_env env, napi_value arg, uint32_t refcount, napi_ref &reference);
49     static void CreateProperty(napi_env env, napi_value exports, const std::string &name,
50         const std::vector<std::pair<std::string, napi_value>> &properties);
51 
52     static ClientStatus IsTypeOf(napi_env env, napi_value arg, napi_valuetype type);
53     static void NapiThrowParamError(napi_env env, std::vector<std::pair<std::string, std::string>> &paramInfos);
54     static void NapiThrowNotSystemAppError(napi_env env);
55     static bool IsCallerValid();
56     static bool IsPermissionGranted(const std::string &permission);
57     static void NapiThrowPermissionError(napi_env env);
58     static napi_value BuildThrowError(napi_env env, const BusinessError &businessError);
59     static int32_t BuildBusinessError(napi_env env, napi_value &obj, const BusinessError &businessError);
60     static int32_t ConvertToErrorCode(CallResult callResult);
61 
62     static napi_value BuildCallFuncResult(napi_env env, const BusinessError &businessError);
63 
64 protected:
65     static ClientStatus CheckNapiObjectType(napi_env env, const napi_value &arg);
66 
67 private:
68     static void BuildErrorMessages(napi_env env, napi_value &obj, const std::string &name,
69         const std::vector<ErrorMessage> &errorMessages);
70     static bool IsCommonError(CallResult callResult);
71     static std::string GetParamNames(std::vector<std::pair<std::string, std::string>> &strVector);
72     static std::string GetParamTypes(std::vector<std::pair<std::string, std::string>> &strVector);
73     static std::string ConvertVectorToStr(std::vector<std::pair<std::string, std::string>> &strVector, bool isFirst);
74     static size_t GetValidDataCount(const std::vector<ErrorMessage> &list);
75 };
76 } // namespace OHOS::UpdateEngine
77 #endif // NAPI_COMMON_UTILS_H