1 /*
2  * Copyright (c) 2021-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 FOUNDATION_ACE_INTERFACES_NAPI_KITS_UTILS_H
17 #define FOUNDATION_ACE_INTERFACES_NAPI_KITS_UTILS_H
18 
19 #include <chrono>
20 #include <cmath>
21 #include <cstdint>
22 #include <regex>
23 #include <vector>
24 
25 #include "native_engine/native_value.h"
26 
27 #include "base/log/log.h"
28 #include "bridge/common/utils/utils.h"
29 #include "core/common/container.h"
30 #include "core/common/resource/resource_wrapper.h"
31 #include "core/components/common/properties/color.h"
32 #include "securec.h"
33 
34 namespace OHOS::Ace::Napi {
35 
36 struct ResourceInfo {
37     int32_t resId = 0;
38     int32_t type = 0;
39     std::vector<std::string> params;
40     std::optional<std::string> bundleName = std::nullopt;
41     std::optional<std::string> moduleName = std::nullopt;
42 };
43 
44 enum class ResourceType : uint32_t {
45     COLOR = 10001,
46     FLOAT,
47     STRING,
48     PLURAL,
49     BOOLEAN,
50     INTARRAY,
51     INTEGER,
52     PATTERN,
53     STRARRAY,
54     MEDIA = 20000,
55     RAWFILE = 30000
56 };
57 
58 enum class ResourceStruct { CONSTANT, DYNAMIC_V1, DYNAMIC_V2 };
59 
60 size_t GetParamLen(napi_env env, napi_value param);
61 
62 bool GetNapiString(napi_env env, napi_value value, std::string& retStr, napi_valuetype& valueType);
63 
64 bool NapiStringToString(napi_env env, napi_value value, std::string& retStr);
65 
66 void NapiThrow(napi_env env, const std::string& message, int32_t errCode);
67 
68 napi_value ParseCurve(napi_env env, napi_value value, std::string& curveTypeString, std::vector<float>& curveValue);
69 
70 napi_value CreateNapiString(napi_env env, const std::string& rawStr);
71 
72 ResourceStruct CheckResourceStruct(napi_env env, napi_value value);
73 
74 void CompleteResourceParam(napi_env env, napi_value value);
75 
76 void CompleteResourceParamV1(napi_env env, napi_value value);
77 
78 void CompleteResourceParamV2(napi_env env, napi_value value);
79 
80 void ModifyResourceParam(napi_env env, napi_value value, const ResourceType& resType, const std::string& resName);
81 
82 bool ConvertResourceType(const std::string& typeName, ResourceType& resType);
83 
84 void PreFixEmptyBundleName(napi_env env, napi_value value);
85 
86 bool ParseDollarResource(
87     napi_env env, napi_value value, ResourceType& resType, std::string& resName, std::string& moduleName);
88 
89 void ParseCurveInfo(const std::string& curveString, std::string& curveTypeString, std::vector<float>& curveValue);
90 
91 bool ParseColor(napi_env env, napi_value value, Color& info);
92 
93 bool ParseResourceParam(napi_env env, napi_value value, ResourceInfo& info);
94 
95 bool ParseString(const ResourceInfo& info, std::string& result);
96 
97 std::string ErrorToMessage(int32_t code);
98 
99 bool GetSingleParam(napi_env env, napi_callback_info info, napi_value* argv, napi_valuetype& valueType);
100 
101 std::optional<Color> GetOptionalColor(napi_env env, napi_value argv, napi_valuetype& valueType);
102 
103 napi_valuetype GetValueType(napi_env env, napi_value value);
104 RefPtr<ResourceWrapper> CreateResourceWrapper(const ResourceInfo& info);
105 std::optional<std::string> GetStringFromValueUtf8(napi_env env, napi_value value);
106 bool ParseIntegerToString(const ResourceInfo& info, std::string& result);
107 bool ParseColorFromResourceObject(napi_env env, napi_value value, Color& colorResult);
108 
109 napi_value GetReturnObject(napi_env env, std::string callbackString);
110 bool HasProperty(napi_env env, napi_value value, const std::string& targetStr);
111 bool ParseNapiDimension(napi_env env, CalcDimension& result, napi_value napiValue, DimensionUnit defaultUnit);
112 bool ParseNapiDimensionNG(
113     napi_env env, CalcDimension& result, napi_value napiValue, DimensionUnit defaultUnit, bool isSupportPercent);
114 bool ParseNapiColor(napi_env env, napi_value value, Color& result);
115 bool ParseStyle(napi_env env, napi_value value, std::optional<BorderStyle>& style);
116 bool ParseShadowColorStrategy(napi_env env, napi_value value, ShadowColorStrategy& strategy);
117 } // namespace OHOS::Ace::Napi
118 
119 #endif // FOUNDATION_ACE_INTERFACES_NAPI_KITS_UTILS_H
120