• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "common_error_define.h"
26 #include "napi_common_define.h"
27 
28 namespace OHOS::UpdateEngine {
29 class NapiCommonUtils {
30 public:
31     static int32_t GetBool(napi_env env, napi_value arg, const std::string &attrName, bool &value);
32     static int32_t GetInt32(napi_env env, napi_value arg, const std::string &attrName, int32_t &intValue);
33     static int32_t GetString(napi_env env, napi_value arg, const std::string &attrName, std::string &strValue);
34     static int32_t GetString(napi_env env, napi_value arg, std::string &strValue);
35 
36     static int32_t SetBool(napi_env env, napi_value arg, const std::string &attrName, bool value);
37     static int32_t SetInt32(napi_env env, napi_value arg, const std::string &attrName, int32_t intValue);
38     static int32_t SetInt64(napi_env env, napi_value arg, const std::string &attrName, int64_t intValue);
39     static int32_t SetString(napi_env env, napi_value arg, const std::string &attrName, const std::string &string);
40     static int32_t SetUint32(napi_env env, napi_value arg, const std::string &attrName, uint32_t intValue);
41 
42     static napi_value CreateUint32(napi_env env, uint32_t code);
43     static napi_value CreateStringUtf8(napi_env env, const std::string &str);
44     static ClientStatus CreateReference(napi_env env, napi_value arg, uint32_t refcount, napi_ref &reference);
45     static void CreateProperty(napi_env env, napi_value exports, const std::string &name,
46         const std::vector<std::pair<std::string, napi_value>> &properties);
47 
48     static ClientStatus IsTypeOf(napi_env env, napi_value arg, napi_valuetype type);
49     static void NapiThrowParamError(napi_env env, std::vector<std::pair<std::string, std::string>> &paramInfos);
50     static napi_value BuildThrowError(napi_env env, const BusinessError &businessError);
51     static int32_t BuildBusinessError(napi_env env, napi_value &obj, const BusinessError &businessError);
52     static int32_t ConvertToErrorCode(CallResult callResult);
53 
54 protected:
55     static ClientStatus CheckNapiObjectType(napi_env env, const napi_value &arg);
56 
57 private:
58     static void BuildErrorMessages(napi_env env, napi_value &obj, const std::string &name,
59         const std::vector<ErrorMessage> &errorMessages);
60     static bool IsCommonError(CallResult callResult);
61     static std::string GetParamNames(std::vector<std::pair<std::string, std::string>> &strVector);
62     static std::string GetParamTypes(std::vector<std::pair<std::string, std::string>> &strVector);
63     static std::string ConvertVectorToStr(std::vector<std::pair<std::string, std::string>> &strVector, bool isFirst);
64     static size_t GetValidDataCount(const std::vector<ErrorMessage> &list);
65 };
66 } // namespace OHOS::UpdateEngine
67 #endif // NAPI_COMMON_UTILS_H