• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/components/common/properties/color.h"
31 #include "securec.h"
32 
33 namespace OHOS::Ace::Napi {
34 
35 struct ResourceInfo {
36     int32_t resId = 0;
37     int32_t type = 0;
38     std::vector<std::string> params;
39     std::optional<std::string> bundleName = std::nullopt;
40     std::optional<std::string> moduleName = std::nullopt;
41 };
42 
43 enum class ResourceType : uint32_t {
44     COLOR = 10001,
45     FLOAT,
46     STRING,
47     PLURAL,
48     BOOLEAN,
49     INTARRAY,
50     INTEGER,
51     PATTERN,
52     STRARRAY,
53     MEDIA = 20000,
54     RAWFILE = 30000
55 };
56 
57 size_t GetParamLen(napi_env env, napi_value param);
58 bool GetNapiString(napi_env env, napi_value value, std::string& retStr, napi_valuetype& valueType);
59 
60 void NapiThrow(napi_env env, const std::string& message, int32_t errCode);
61 
62 napi_value ParseCurve(napi_env env, napi_value value, std::string& curveTypeString, std::vector<float>& curveValue);
63 
64 napi_value CreateNapiString(napi_env env, std::string rawStr);
65 
66 void CompleteResourceParam(napi_env env, napi_value value);
67 
68 void ModifyResourceParam(napi_env env, napi_value value, const ResourceType& resType, const std::string& resName);
69 
70 bool ConvertResourceType(const std::string& typeName, ResourceType& resType);
71 
72 bool ParseDollarResource(napi_env env, napi_value value, ResourceType& resType, std::string& resName);
73 
74 void ParseCurveInfo(const std::string& curveString, std::string& curveTypeString, std::vector<float>& curveValue);
75 
76 bool ParseColor(napi_env env, napi_value value, Color& info);
77 
78 bool ParseResourceParam(napi_env env, napi_value value, ResourceInfo& info);
79 
80 bool ParseString(const ResourceInfo& info, std::string& result);
81 
82 std::string ErrorToMessage(int32_t code);
83 
84 bool GetSingleParam(napi_env env, napi_callback_info info, napi_value* argv, napi_valuetype& valueType);
85 
86 std::optional<Color> GetOptionalColor(napi_env env, napi_value argv, napi_valuetype& valueType);
87 } // namespace OHOS::Ace::Napi
88 
89 #endif // FOUNDATION_ACE_INTERFACES_NAPI_KITS_UTILS_H
90