123b3eb3cSopenharmony_ci/* 223b3eb3cSopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License. 523b3eb3cSopenharmony_ci * You may obtain a copy of the License at 623b3eb3cSopenharmony_ci * 723b3eb3cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 823b3eb3cSopenharmony_ci * 923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and 1323b3eb3cSopenharmony_ci * limitations under the License. 1423b3eb3cSopenharmony_ci */ 1523b3eb3cSopenharmony_ci 1623b3eb3cSopenharmony_ci#ifndef FOUNDATION_ACE_INTERFACES_NAPI_KITS_UTILS_H 1723b3eb3cSopenharmony_ci#define FOUNDATION_ACE_INTERFACES_NAPI_KITS_UTILS_H 1823b3eb3cSopenharmony_ci 1923b3eb3cSopenharmony_ci#include <chrono> 2023b3eb3cSopenharmony_ci#include <cmath> 2123b3eb3cSopenharmony_ci#include <cstdint> 2223b3eb3cSopenharmony_ci#include <regex> 2323b3eb3cSopenharmony_ci#include <vector> 2423b3eb3cSopenharmony_ci 2523b3eb3cSopenharmony_ci#include "native_engine/native_value.h" 2623b3eb3cSopenharmony_ci 2723b3eb3cSopenharmony_ci#include "base/log/log.h" 2823b3eb3cSopenharmony_ci#include "bridge/common/utils/utils.h" 2923b3eb3cSopenharmony_ci#include "core/common/container.h" 3023b3eb3cSopenharmony_ci#include "core/common/resource/resource_wrapper.h" 3123b3eb3cSopenharmony_ci#include "core/components/common/properties/color.h" 3223b3eb3cSopenharmony_ci#include "securec.h" 3323b3eb3cSopenharmony_ci 3423b3eb3cSopenharmony_cinamespace OHOS::Ace::Napi { 3523b3eb3cSopenharmony_ci 3623b3eb3cSopenharmony_cistruct ResourceInfo { 3723b3eb3cSopenharmony_ci int32_t resId = 0; 3823b3eb3cSopenharmony_ci int32_t type = 0; 3923b3eb3cSopenharmony_ci std::vector<std::string> params; 4023b3eb3cSopenharmony_ci std::optional<std::string> bundleName = std::nullopt; 4123b3eb3cSopenharmony_ci std::optional<std::string> moduleName = std::nullopt; 4223b3eb3cSopenharmony_ci}; 4323b3eb3cSopenharmony_ci 4423b3eb3cSopenharmony_cienum class ResourceType : uint32_t { 4523b3eb3cSopenharmony_ci COLOR = 10001, 4623b3eb3cSopenharmony_ci FLOAT, 4723b3eb3cSopenharmony_ci STRING, 4823b3eb3cSopenharmony_ci PLURAL, 4923b3eb3cSopenharmony_ci BOOLEAN, 5023b3eb3cSopenharmony_ci INTARRAY, 5123b3eb3cSopenharmony_ci INTEGER, 5223b3eb3cSopenharmony_ci PATTERN, 5323b3eb3cSopenharmony_ci STRARRAY, 5423b3eb3cSopenharmony_ci MEDIA = 20000, 5523b3eb3cSopenharmony_ci RAWFILE = 30000 5623b3eb3cSopenharmony_ci}; 5723b3eb3cSopenharmony_ci 5823b3eb3cSopenharmony_cienum class ResourceStruct { CONSTANT, DYNAMIC_V1, DYNAMIC_V2 }; 5923b3eb3cSopenharmony_ci 6023b3eb3cSopenharmony_cisize_t GetParamLen(napi_env env, napi_value param); 6123b3eb3cSopenharmony_ci 6223b3eb3cSopenharmony_cibool GetNapiString(napi_env env, napi_value value, std::string& retStr, napi_valuetype& valueType); 6323b3eb3cSopenharmony_ci 6423b3eb3cSopenharmony_cibool NapiStringToString(napi_env env, napi_value value, std::string& retStr); 6523b3eb3cSopenharmony_ci 6623b3eb3cSopenharmony_civoid NapiThrow(napi_env env, const std::string& message, int32_t errCode); 6723b3eb3cSopenharmony_ci 6823b3eb3cSopenharmony_cinapi_value ParseCurve(napi_env env, napi_value value, std::string& curveTypeString, std::vector<float>& curveValue); 6923b3eb3cSopenharmony_ci 7023b3eb3cSopenharmony_cinapi_value CreateNapiString(napi_env env, const std::string& rawStr); 7123b3eb3cSopenharmony_ci 7223b3eb3cSopenharmony_ciResourceStruct CheckResourceStruct(napi_env env, napi_value value); 7323b3eb3cSopenharmony_ci 7423b3eb3cSopenharmony_civoid CompleteResourceParam(napi_env env, napi_value value); 7523b3eb3cSopenharmony_ci 7623b3eb3cSopenharmony_civoid CompleteResourceParamV1(napi_env env, napi_value value); 7723b3eb3cSopenharmony_ci 7823b3eb3cSopenharmony_civoid CompleteResourceParamV2(napi_env env, napi_value value); 7923b3eb3cSopenharmony_ci 8023b3eb3cSopenharmony_civoid ModifyResourceParam(napi_env env, napi_value value, const ResourceType& resType, const std::string& resName); 8123b3eb3cSopenharmony_ci 8223b3eb3cSopenharmony_cibool ConvertResourceType(const std::string& typeName, ResourceType& resType); 8323b3eb3cSopenharmony_ci 8423b3eb3cSopenharmony_civoid PreFixEmptyBundleName(napi_env env, napi_value value); 8523b3eb3cSopenharmony_ci 8623b3eb3cSopenharmony_cibool ParseDollarResource( 8723b3eb3cSopenharmony_ci napi_env env, napi_value value, ResourceType& resType, std::string& resName, std::string& moduleName); 8823b3eb3cSopenharmony_ci 8923b3eb3cSopenharmony_civoid ParseCurveInfo(const std::string& curveString, std::string& curveTypeString, std::vector<float>& curveValue); 9023b3eb3cSopenharmony_ci 9123b3eb3cSopenharmony_cibool ParseColor(napi_env env, napi_value value, Color& info); 9223b3eb3cSopenharmony_ci 9323b3eb3cSopenharmony_cibool ParseResourceParam(napi_env env, napi_value value, ResourceInfo& info); 9423b3eb3cSopenharmony_ci 9523b3eb3cSopenharmony_cibool ParseString(const ResourceInfo& info, std::string& result); 9623b3eb3cSopenharmony_ci 9723b3eb3cSopenharmony_cistd::string ErrorToMessage(int32_t code); 9823b3eb3cSopenharmony_ci 9923b3eb3cSopenharmony_cibool GetSingleParam(napi_env env, napi_callback_info info, napi_value* argv, napi_valuetype& valueType); 10023b3eb3cSopenharmony_ci 10123b3eb3cSopenharmony_cistd::optional<Color> GetOptionalColor(napi_env env, napi_value argv, napi_valuetype& valueType); 10223b3eb3cSopenharmony_ci 10323b3eb3cSopenharmony_cinapi_valuetype GetValueType(napi_env env, napi_value value); 10423b3eb3cSopenharmony_ciRefPtr<ResourceWrapper> CreateResourceWrapper(const ResourceInfo& info); 10523b3eb3cSopenharmony_cistd::optional<std::string> GetStringFromValueUtf8(napi_env env, napi_value value); 10623b3eb3cSopenharmony_cibool ParseIntegerToString(const ResourceInfo& info, std::string& result); 10723b3eb3cSopenharmony_cibool ParseColorFromResourceObject(napi_env env, napi_value value, Color& colorResult); 10823b3eb3cSopenharmony_ci 10923b3eb3cSopenharmony_cinapi_value GetReturnObject(napi_env env, std::string callbackString); 11023b3eb3cSopenharmony_cibool HasProperty(napi_env env, napi_value value, const std::string& targetStr); 11123b3eb3cSopenharmony_cibool ParseNapiDimension(napi_env env, CalcDimension& result, napi_value napiValue, DimensionUnit defaultUnit); 11223b3eb3cSopenharmony_cibool ParseNapiDimensionNG( 11323b3eb3cSopenharmony_ci napi_env env, CalcDimension& result, napi_value napiValue, DimensionUnit defaultUnit, bool isSupportPercent); 11423b3eb3cSopenharmony_cibool ParseNapiColor(napi_env env, napi_value value, Color& result); 11523b3eb3cSopenharmony_cibool ParseStyle(napi_env env, napi_value value, std::optional<BorderStyle>& style); 11623b3eb3cSopenharmony_cibool ParseShadowColorStrategy(napi_env env, napi_value value, ShadowColorStrategy& strategy); 11723b3eb3cSopenharmony_ci} // namespace OHOS::Ace::Napi 11823b3eb3cSopenharmony_ci 11923b3eb3cSopenharmony_ci#endif // FOUNDATION_ACE_INTERFACES_NAPI_KITS_UTILS_H 120