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
34namespace OHOS::Ace::Napi {
35
36struct 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
44enum 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
58enum class ResourceStruct { CONSTANT, DYNAMIC_V1, DYNAMIC_V2 };
59
60size_t GetParamLen(napi_env env, napi_value param);
61
62bool GetNapiString(napi_env env, napi_value value, std::string& retStr, napi_valuetype& valueType);
63
64bool NapiStringToString(napi_env env, napi_value value, std::string& retStr);
65
66void NapiThrow(napi_env env, const std::string& message, int32_t errCode);
67
68napi_value ParseCurve(napi_env env, napi_value value, std::string& curveTypeString, std::vector<float>& curveValue);
69
70napi_value CreateNapiString(napi_env env, const std::string& rawStr);
71
72ResourceStruct CheckResourceStruct(napi_env env, napi_value value);
73
74void CompleteResourceParam(napi_env env, napi_value value);
75
76void CompleteResourceParamV1(napi_env env, napi_value value);
77
78void CompleteResourceParamV2(napi_env env, napi_value value);
79
80void ModifyResourceParam(napi_env env, napi_value value, const ResourceType& resType, const std::string& resName);
81
82bool ConvertResourceType(const std::string& typeName, ResourceType& resType);
83
84void PreFixEmptyBundleName(napi_env env, napi_value value);
85
86bool ParseDollarResource(
87    napi_env env, napi_value value, ResourceType& resType, std::string& resName, std::string& moduleName);
88
89void ParseCurveInfo(const std::string& curveString, std::string& curveTypeString, std::vector<float>& curveValue);
90
91bool ParseColor(napi_env env, napi_value value, Color& info);
92
93bool ParseResourceParam(napi_env env, napi_value value, ResourceInfo& info);
94
95bool ParseString(const ResourceInfo& info, std::string& result);
96
97std::string ErrorToMessage(int32_t code);
98
99bool GetSingleParam(napi_env env, napi_callback_info info, napi_value* argv, napi_valuetype& valueType);
100
101std::optional<Color> GetOptionalColor(napi_env env, napi_value argv, napi_valuetype& valueType);
102
103napi_valuetype GetValueType(napi_env env, napi_value value);
104RefPtr<ResourceWrapper> CreateResourceWrapper(const ResourceInfo& info);
105std::optional<std::string> GetStringFromValueUtf8(napi_env env, napi_value value);
106bool ParseIntegerToString(const ResourceInfo& info, std::string& result);
107bool ParseColorFromResourceObject(napi_env env, napi_value value, Color& colorResult);
108
109napi_value GetReturnObject(napi_env env, std::string callbackString);
110bool HasProperty(napi_env env, napi_value value, const std::string& targetStr);
111bool ParseNapiDimension(napi_env env, CalcDimension& result, napi_value napiValue, DimensionUnit defaultUnit);
112bool ParseNapiDimensionNG(
113    napi_env env, CalcDimension& result, napi_value napiValue, DimensionUnit defaultUnit, bool isSupportPercent);
114bool ParseNapiColor(napi_env env, napi_value value, Color& result);
115bool ParseStyle(napi_env env, napi_value value, std::optional<BorderStyle>& style);
116bool 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