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#include "napi_utils.h" 1723b3eb3cSopenharmony_ci#include "core/common/resource/resource_manager.h" 1823b3eb3cSopenharmony_ci 1923b3eb3cSopenharmony_cinamespace OHOS::Ace::Napi { 2023b3eb3cSopenharmony_ciusing namespace OHOS::Ace; 2123b3eb3cSopenharmony_cinamespace { 2223b3eb3cSopenharmony_ci 2323b3eb3cSopenharmony_ciconst std::regex RESOURCE_APP_STRING_PLACEHOLDER(R"(\%((\d+)(\$)){0,1}([dsf]))", std::regex::icase); 2423b3eb3cSopenharmony_ciconstexpr int32_t NAPI_BUF_LENGTH = 256; 2523b3eb3cSopenharmony_ciconstexpr int32_t UNKNOWN_RESOURCE_ID = -1; 2623b3eb3cSopenharmony_ciconstexpr char BUNDLE_NAME[] = "bundleName"; 2723b3eb3cSopenharmony_cistd::vector<std::string> RESOURCE_HEADS = { "app", "sys" }; 2823b3eb3cSopenharmony_ci} // namespace 2923b3eb3cSopenharmony_ci 3023b3eb3cSopenharmony_cistatic const std::unordered_map<int32_t, std::string> ERROR_CODE_TO_MSG { 3123b3eb3cSopenharmony_ci { ERROR_CODE_PERMISSION_DENIED, "Permission denied. " }, 3223b3eb3cSopenharmony_ci { ERROR_CODE_PARAM_INVALID, "Parameter error. " }, 3323b3eb3cSopenharmony_ci { ERROR_CODE_SYSTEMCAP_ERROR, "Capability not supported. " }, 3423b3eb3cSopenharmony_ci { ERROR_CODE_INTERNAL_ERROR, "Internal error. " }, 3523b3eb3cSopenharmony_ci { ERROR_CODE_URI_ERROR, "Uri error. " }, 3623b3eb3cSopenharmony_ci { ERROR_CODE_PAGE_STACK_FULL, "Page stack error. " }, 3723b3eb3cSopenharmony_ci { ERROR_CODE_URI_ERROR_LITE, "Uri error. " }, 3823b3eb3cSopenharmony_ci { ERROR_CODE_DIALOG_CONTENT_ERROR, "Dialog content error. " }, 3923b3eb3cSopenharmony_ci { ERROR_CODE_DIALOG_CONTENT_ALREADY_EXIST, "Dialog content already exist. " }, 4023b3eb3cSopenharmony_ci { ERROR_CODE_DIALOG_CONTENT_NOT_FOUND, "Dialog content not found. " }, 4123b3eb3cSopenharmony_ci { ERROR_CODE_TOAST_NOT_FOUND, "Toast not found. " } 4223b3eb3cSopenharmony_ci}; 4323b3eb3cSopenharmony_ci 4423b3eb3cSopenharmony_civoid NapiThrow(napi_env env, const std::string& message, int32_t errCode) 4523b3eb3cSopenharmony_ci{ 4623b3eb3cSopenharmony_ci napi_value code = nullptr; 4723b3eb3cSopenharmony_ci std::string strCode = std::to_string(errCode); 4823b3eb3cSopenharmony_ci napi_create_string_utf8(env, strCode.c_str(), strCode.length(), &code); 4923b3eb3cSopenharmony_ci 5023b3eb3cSopenharmony_ci napi_value msg = nullptr; 5123b3eb3cSopenharmony_ci auto iter = ERROR_CODE_TO_MSG.find(errCode); 5223b3eb3cSopenharmony_ci std::string strMsg = (iter != ERROR_CODE_TO_MSG.end() ? iter->second : "") + message; 5323b3eb3cSopenharmony_ci LOGE("napi throw errCode %d strMsg %s", errCode, strMsg.c_str()); 5423b3eb3cSopenharmony_ci napi_create_string_utf8(env, strMsg.c_str(), strMsg.length(), &msg); 5523b3eb3cSopenharmony_ci 5623b3eb3cSopenharmony_ci napi_value error = nullptr; 5723b3eb3cSopenharmony_ci napi_create_error(env, code, msg, &error); 5823b3eb3cSopenharmony_ci napi_throw(env, error); 5923b3eb3cSopenharmony_ci} 6023b3eb3cSopenharmony_ci 6123b3eb3cSopenharmony_civoid ReplaceHolder(std::string& originStr, const std::vector<std::string>& params, uint32_t containCount) 6223b3eb3cSopenharmony_ci{ 6323b3eb3cSopenharmony_ci auto size = static_cast<uint32_t>(params.size()); 6423b3eb3cSopenharmony_ci if (containCount == size) { 6523b3eb3cSopenharmony_ci return; 6623b3eb3cSopenharmony_ci } 6723b3eb3cSopenharmony_ci std::string::const_iterator start = originStr.begin(); 6823b3eb3cSopenharmony_ci std::string::const_iterator end = originStr.end(); 6923b3eb3cSopenharmony_ci std::smatch matches; 7023b3eb3cSopenharmony_ci bool shortHolderType = false; 7123b3eb3cSopenharmony_ci bool firstMatch = true; 7223b3eb3cSopenharmony_ci uint32_t searchTime = 0; 7323b3eb3cSopenharmony_ci while (std::regex_search(start, end, matches, RESOURCE_APP_STRING_PLACEHOLDER)) { 7423b3eb3cSopenharmony_ci std::string pos = matches[2]; 7523b3eb3cSopenharmony_ci std::string type = matches[4]; 7623b3eb3cSopenharmony_ci if (firstMatch) { 7723b3eb3cSopenharmony_ci firstMatch = false; 7823b3eb3cSopenharmony_ci shortHolderType = pos.length() == 0; 7923b3eb3cSopenharmony_ci } else { 8023b3eb3cSopenharmony_ci if (static_cast<uint32_t>(shortHolderType) ^ ((uint32_t)(pos.length() == 0))) { 8123b3eb3cSopenharmony_ci LOGE("wrong place holder,stop parse string"); 8223b3eb3cSopenharmony_ci return; 8323b3eb3cSopenharmony_ci } 8423b3eb3cSopenharmony_ci } 8523b3eb3cSopenharmony_ci 8623b3eb3cSopenharmony_ci std::string replaceContentStr; 8723b3eb3cSopenharmony_ci std::string::size_type index = 0; 8823b3eb3cSopenharmony_ci if (shortHolderType) { 8923b3eb3cSopenharmony_ci index = static_cast<std::string::size_type>(searchTime + containCount); 9023b3eb3cSopenharmony_ci } else { 9123b3eb3cSopenharmony_ci int32_t indexTmp = StringUtils::StringToInt(pos) + static_cast<int32_t>(containCount) - 1; 9223b3eb3cSopenharmony_ci if (indexTmp >= 0) { 9323b3eb3cSopenharmony_ci index = static_cast<std::string::size_type>(indexTmp); 9423b3eb3cSopenharmony_ci } else { 9523b3eb3cSopenharmony_ci LOGE("indexTmp err:%{public}d", indexTmp); 9623b3eb3cSopenharmony_ci } 9723b3eb3cSopenharmony_ci } 9823b3eb3cSopenharmony_ci if (static_cast<uint32_t>(index) < size) { 9923b3eb3cSopenharmony_ci replaceContentStr = params[index]; 10023b3eb3cSopenharmony_ci } else { 10123b3eb3cSopenharmony_ci LOGE("index = %{public}d size = %{public}d", static_cast<uint32_t>(index), size); 10223b3eb3cSopenharmony_ci } 10323b3eb3cSopenharmony_ci originStr.replace(matches[0].first - originStr.begin(), matches[0].length(), replaceContentStr); 10423b3eb3cSopenharmony_ci start = originStr.begin() + matches.prefix().length() + replaceContentStr.length(); 10523b3eb3cSopenharmony_ci end = originStr.end(); 10623b3eb3cSopenharmony_ci searchTime++; 10723b3eb3cSopenharmony_ci } 10823b3eb3cSopenharmony_ci} 10923b3eb3cSopenharmony_ci 11023b3eb3cSopenharmony_cisize_t GetParamLen(napi_env env, napi_value param) 11123b3eb3cSopenharmony_ci{ 11223b3eb3cSopenharmony_ci size_t buffSize = 0; 11323b3eb3cSopenharmony_ci napi_status status = napi_get_value_string_utf8(env, param, nullptr, 0, &buffSize); 11423b3eb3cSopenharmony_ci if (status != napi_ok || buffSize == 0) { 11523b3eb3cSopenharmony_ci return 0; 11623b3eb3cSopenharmony_ci } 11723b3eb3cSopenharmony_ci return buffSize; 11823b3eb3cSopenharmony_ci} 11923b3eb3cSopenharmony_ci 12023b3eb3cSopenharmony_cibool NapiStringToString(napi_env env, napi_value value, std::string& retStr) 12123b3eb3cSopenharmony_ci{ 12223b3eb3cSopenharmony_ci size_t ret = 0; 12323b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 12423b3eb3cSopenharmony_ci napi_typeof(env, value, &valueType); 12523b3eb3cSopenharmony_ci if (valueType != napi_string) { 12623b3eb3cSopenharmony_ci return false; 12723b3eb3cSopenharmony_ci } 12823b3eb3cSopenharmony_ci if (GetParamLen(env, value) == 0) { 12923b3eb3cSopenharmony_ci return false; 13023b3eb3cSopenharmony_ci } 13123b3eb3cSopenharmony_ci size_t valueLen = GetParamLen(env, value) + 1; 13223b3eb3cSopenharmony_ci std::unique_ptr<char[]> buffer = std::make_unique<char[]>(valueLen); 13323b3eb3cSopenharmony_ci napi_get_value_string_utf8(env, value, buffer.get(), valueLen, &ret); 13423b3eb3cSopenharmony_ci retStr = buffer.get(); 13523b3eb3cSopenharmony_ci return true; 13623b3eb3cSopenharmony_ci} 13723b3eb3cSopenharmony_ci 13823b3eb3cSopenharmony_cibool GetNapiString(napi_env env, napi_value value, std::string& retStr, napi_valuetype& valueType) 13923b3eb3cSopenharmony_ci{ 14023b3eb3cSopenharmony_ci if (NapiStringToString(env, value, retStr)) { 14123b3eb3cSopenharmony_ci return true; 14223b3eb3cSopenharmony_ci } 14323b3eb3cSopenharmony_ci napi_typeof(env, value, &valueType); 14423b3eb3cSopenharmony_ci if (valueType == napi_object) { 14523b3eb3cSopenharmony_ci ResourceInfo recv; 14623b3eb3cSopenharmony_ci if (ParseResourceParam(env, value, recv)) { 14723b3eb3cSopenharmony_ci ParseString(recv, retStr); 14823b3eb3cSopenharmony_ci return true; 14923b3eb3cSopenharmony_ci } 15023b3eb3cSopenharmony_ci } 15123b3eb3cSopenharmony_ci return false; 15223b3eb3cSopenharmony_ci} 15323b3eb3cSopenharmony_ci 15423b3eb3cSopenharmony_ciRefPtr<ThemeConstants> GetThemeConstants(const std::optional<std::string>& bundleName = std::nullopt, 15523b3eb3cSopenharmony_ci const std::optional<std::string>& moduleName = std::nullopt) 15623b3eb3cSopenharmony_ci{ 15723b3eb3cSopenharmony_ci auto container = Container::Current(); 15823b3eb3cSopenharmony_ci if (!container) { 15923b3eb3cSopenharmony_ci LOGW("container is null"); 16023b3eb3cSopenharmony_ci return nullptr; 16123b3eb3cSopenharmony_ci } 16223b3eb3cSopenharmony_ci auto pipelineContext = container->GetPipelineContext(); 16323b3eb3cSopenharmony_ci if (!pipelineContext) { 16423b3eb3cSopenharmony_ci LOGE("pipelineContext is null!"); 16523b3eb3cSopenharmony_ci return nullptr; 16623b3eb3cSopenharmony_ci } 16723b3eb3cSopenharmony_ci auto themeManager = pipelineContext->GetThemeManager(); 16823b3eb3cSopenharmony_ci if (!themeManager) { 16923b3eb3cSopenharmony_ci LOGE("themeManager is null!"); 17023b3eb3cSopenharmony_ci return nullptr; 17123b3eb3cSopenharmony_ci } 17223b3eb3cSopenharmony_ci if (bundleName.has_value() && moduleName.has_value()) { 17323b3eb3cSopenharmony_ci return themeManager->GetThemeConstants(bundleName.value_or(""), moduleName.value_or("")); 17423b3eb3cSopenharmony_ci } 17523b3eb3cSopenharmony_ci return themeManager->GetThemeConstants(); 17623b3eb3cSopenharmony_ci} 17723b3eb3cSopenharmony_ci 17823b3eb3cSopenharmony_ciRefPtr<ResourceWrapper> CreateResourceWrapper(const ResourceInfo& info) 17923b3eb3cSopenharmony_ci{ 18023b3eb3cSopenharmony_ci auto bundleName = info.bundleName; 18123b3eb3cSopenharmony_ci auto moduleName = info.moduleName; 18223b3eb3cSopenharmony_ci 18323b3eb3cSopenharmony_ci RefPtr<ResourceAdapter> resourceAdapter = nullptr; 18423b3eb3cSopenharmony_ci RefPtr<ThemeConstants> themeConstants = nullptr; 18523b3eb3cSopenharmony_ci if (SystemProperties::GetResourceDecoupling()) { 18623b3eb3cSopenharmony_ci if (bundleName.has_value() && moduleName.has_value()) { 18723b3eb3cSopenharmony_ci auto resourceObject = AceType::MakeRefPtr<ResourceObject>(bundleName.value_or(""), moduleName.value_or("")); 18823b3eb3cSopenharmony_ci resourceAdapter = ResourceManager::GetInstance().GetOrCreateResourceAdapter(resourceObject); 18923b3eb3cSopenharmony_ci } else { 19023b3eb3cSopenharmony_ci resourceAdapter = ResourceManager::GetInstance().GetResourceAdapter(); 19123b3eb3cSopenharmony_ci } 19223b3eb3cSopenharmony_ci if (!resourceAdapter) { 19323b3eb3cSopenharmony_ci return nullptr; 19423b3eb3cSopenharmony_ci } 19523b3eb3cSopenharmony_ci } else { 19623b3eb3cSopenharmony_ci themeConstants = GetThemeConstants(info.bundleName, info.moduleName); 19723b3eb3cSopenharmony_ci if (!themeConstants) { 19823b3eb3cSopenharmony_ci return nullptr; 19923b3eb3cSopenharmony_ci } 20023b3eb3cSopenharmony_ci } 20123b3eb3cSopenharmony_ci auto resourceWrapper = AceType::MakeRefPtr<ResourceWrapper>(themeConstants, resourceAdapter); 20223b3eb3cSopenharmony_ci return resourceWrapper; 20323b3eb3cSopenharmony_ci} 20423b3eb3cSopenharmony_ci 20523b3eb3cSopenharmony_cinapi_value CreateNapiString(napi_env env, const std::string& rawStr) 20623b3eb3cSopenharmony_ci{ 20723b3eb3cSopenharmony_ci napi_value retVal = nullptr; 20823b3eb3cSopenharmony_ci napi_create_string_utf8(env, rawStr.c_str(), rawStr.length(), &retVal); 20923b3eb3cSopenharmony_ci return retVal; 21023b3eb3cSopenharmony_ci} 21123b3eb3cSopenharmony_ci 21223b3eb3cSopenharmony_cibool ConvertResourceType(const std::string& typeName, ResourceType& resType) 21323b3eb3cSopenharmony_ci{ 21423b3eb3cSopenharmony_ci static const std::unordered_map<std::string, ResourceType> resTypeMap { 21523b3eb3cSopenharmony_ci { "color", ResourceType::COLOR }, 21623b3eb3cSopenharmony_ci { "media", ResourceType::MEDIA }, 21723b3eb3cSopenharmony_ci { "float", ResourceType::FLOAT }, 21823b3eb3cSopenharmony_ci { "string", ResourceType::STRING }, 21923b3eb3cSopenharmony_ci { "plural", ResourceType::PLURAL }, 22023b3eb3cSopenharmony_ci { "pattern", ResourceType::PATTERN }, 22123b3eb3cSopenharmony_ci { "boolean", ResourceType::BOOLEAN }, 22223b3eb3cSopenharmony_ci { "integer", ResourceType::INTEGER }, 22323b3eb3cSopenharmony_ci { "strarray", ResourceType::STRARRAY }, 22423b3eb3cSopenharmony_ci { "intarray", ResourceType::INTARRAY }, 22523b3eb3cSopenharmony_ci }; 22623b3eb3cSopenharmony_ci auto it = resTypeMap.find(typeName); 22723b3eb3cSopenharmony_ci if (it == resTypeMap.end()) { 22823b3eb3cSopenharmony_ci return false; 22923b3eb3cSopenharmony_ci } 23023b3eb3cSopenharmony_ci resType = it->second; 23123b3eb3cSopenharmony_ci return true; 23223b3eb3cSopenharmony_ci} 23323b3eb3cSopenharmony_ci 23423b3eb3cSopenharmony_cibool ParseDollarResource( 23523b3eb3cSopenharmony_ci napi_env env, napi_value value, ResourceType& resType, std::string& resName, std::string& moduleName) 23623b3eb3cSopenharmony_ci{ 23723b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 23823b3eb3cSopenharmony_ci napi_typeof(env, value, &valueType); 23923b3eb3cSopenharmony_ci if (valueType != napi_string) { 24023b3eb3cSopenharmony_ci return false; 24123b3eb3cSopenharmony_ci } 24223b3eb3cSopenharmony_ci std::string resPath; 24323b3eb3cSopenharmony_ci if (!GetNapiString(env, value, resPath, valueType)) { 24423b3eb3cSopenharmony_ci return false; 24523b3eb3cSopenharmony_ci } 24623b3eb3cSopenharmony_ci std::vector<std::string> tokens; 24723b3eb3cSopenharmony_ci StringUtils::StringSplitter(resPath, '.', tokens); 24823b3eb3cSopenharmony_ci // $r format like app.xxx.xxx, has 3 paragraph 24923b3eb3cSopenharmony_ci if (static_cast<int32_t>(tokens.size()) != 3) { 25023b3eb3cSopenharmony_ci return false; 25123b3eb3cSopenharmony_ci } 25223b3eb3cSopenharmony_ci std::string maybeModuleName = tokens[0]; 25323b3eb3cSopenharmony_ci // [*] or app/hsp at least has 3 chars 25423b3eb3cSopenharmony_ci if (maybeModuleName.size() < 3) { 25523b3eb3cSopenharmony_ci return false; 25623b3eb3cSopenharmony_ci } 25723b3eb3cSopenharmony_ci char begin = *maybeModuleName.begin(); 25823b3eb3cSopenharmony_ci char end = maybeModuleName.at(maybeModuleName.size() - 1); 25923b3eb3cSopenharmony_ci bool headCheckPass = false; 26023b3eb3cSopenharmony_ci if (begin == '[' && end == ']') { 26123b3eb3cSopenharmony_ci // moduleName not include 2 brackets 26223b3eb3cSopenharmony_ci moduleName = maybeModuleName.substr(1, maybeModuleName.size() - 2); 26323b3eb3cSopenharmony_ci headCheckPass = true; 26423b3eb3cSopenharmony_ci } 26523b3eb3cSopenharmony_ci if (std::find(RESOURCE_HEADS.begin(), RESOURCE_HEADS.end(), tokens[0]) == RESOURCE_HEADS.end() && !headCheckPass) { 26623b3eb3cSopenharmony_ci return false; 26723b3eb3cSopenharmony_ci } 26823b3eb3cSopenharmony_ci if (!ConvertResourceType(tokens[1], resType)) { 26923b3eb3cSopenharmony_ci return false; 27023b3eb3cSopenharmony_ci } 27123b3eb3cSopenharmony_ci resName = resPath; 27223b3eb3cSopenharmony_ci return true; 27323b3eb3cSopenharmony_ci} 27423b3eb3cSopenharmony_ci 27523b3eb3cSopenharmony_civoid PreFixEmptyBundleName(napi_env env, napi_value value) 27623b3eb3cSopenharmony_ci{ 27723b3eb3cSopenharmony_ci napi_value bundleNameNApi = nullptr; 27823b3eb3cSopenharmony_ci if (napi_get_named_property(env, value, BUNDLE_NAME, &bundleNameNApi) != napi_ok) { 27923b3eb3cSopenharmony_ci return; 28023b3eb3cSopenharmony_ci } 28123b3eb3cSopenharmony_ci std::string bundleName; 28223b3eb3cSopenharmony_ci NapiStringToString(env, bundleNameNApi, bundleName); 28323b3eb3cSopenharmony_ci if (bundleName.empty()) { 28423b3eb3cSopenharmony_ci auto container = Container::CurrentSafely(); 28523b3eb3cSopenharmony_ci CHECK_NULL_VOID(container); 28623b3eb3cSopenharmony_ci bundleName = container->GetBundleName(); 28723b3eb3cSopenharmony_ci bundleNameNApi = CreateNapiString(env, bundleName); 28823b3eb3cSopenharmony_ci napi_set_named_property(env, value, BUNDLE_NAME, bundleNameNApi); 28923b3eb3cSopenharmony_ci } 29023b3eb3cSopenharmony_ci} 29123b3eb3cSopenharmony_ci 29223b3eb3cSopenharmony_ciResourceStruct CheckResourceStruct(napi_env env, napi_value value) 29323b3eb3cSopenharmony_ci{ 29423b3eb3cSopenharmony_ci napi_value idNApi = nullptr; 29523b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 29623b3eb3cSopenharmony_ci napi_typeof(env, value, &valueType); 29723b3eb3cSopenharmony_ci if (valueType != napi_object) { 29823b3eb3cSopenharmony_ci return ResourceStruct::CONSTANT; 29923b3eb3cSopenharmony_ci } 30023b3eb3cSopenharmony_ci if (napi_get_named_property(env, value, "id", &idNApi) != napi_ok) { 30123b3eb3cSopenharmony_ci return ResourceStruct::CONSTANT; 30223b3eb3cSopenharmony_ci } 30323b3eb3cSopenharmony_ci napi_typeof(env, idNApi, &valueType); 30423b3eb3cSopenharmony_ci if (valueType == napi_string) { 30523b3eb3cSopenharmony_ci return ResourceStruct::DYNAMIC_V1; 30623b3eb3cSopenharmony_ci } 30723b3eb3cSopenharmony_ci if (valueType == napi_number) { 30823b3eb3cSopenharmony_ci int32_t id = 0; 30923b3eb3cSopenharmony_ci napi_get_value_int32(env, idNApi, &id); 31023b3eb3cSopenharmony_ci if (id == UNKNOWN_RESOURCE_ID) { 31123b3eb3cSopenharmony_ci return ResourceStruct::DYNAMIC_V2; 31223b3eb3cSopenharmony_ci } 31323b3eb3cSopenharmony_ci } 31423b3eb3cSopenharmony_ci return ResourceStruct::CONSTANT; 31523b3eb3cSopenharmony_ci} 31623b3eb3cSopenharmony_ci 31723b3eb3cSopenharmony_civoid CompleteResourceParam(napi_env env, napi_value value) 31823b3eb3cSopenharmony_ci{ 31923b3eb3cSopenharmony_ci PreFixEmptyBundleName(env, value); 32023b3eb3cSopenharmony_ci ResourceStruct resourceStruct = CheckResourceStruct(env, value); 32123b3eb3cSopenharmony_ci switch (resourceStruct) { 32223b3eb3cSopenharmony_ci case ResourceStruct::CONSTANT: 32323b3eb3cSopenharmony_ci return; 32423b3eb3cSopenharmony_ci case ResourceStruct::DYNAMIC_V1: 32523b3eb3cSopenharmony_ci CompleteResourceParamV1(env, value); 32623b3eb3cSopenharmony_ci return; 32723b3eb3cSopenharmony_ci case ResourceStruct::DYNAMIC_V2: 32823b3eb3cSopenharmony_ci CompleteResourceParamV2(env, value); 32923b3eb3cSopenharmony_ci return; 33023b3eb3cSopenharmony_ci default: 33123b3eb3cSopenharmony_ci return; 33223b3eb3cSopenharmony_ci } 33323b3eb3cSopenharmony_ci} 33423b3eb3cSopenharmony_ci 33523b3eb3cSopenharmony_civoid CompleteResourceParamV1(napi_env env, napi_value value) 33623b3eb3cSopenharmony_ci{ 33723b3eb3cSopenharmony_ci napi_value idNApi = nullptr; 33823b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 33923b3eb3cSopenharmony_ci napi_typeof(env, value, &valueType); 34023b3eb3cSopenharmony_ci if (valueType != napi_object) { 34123b3eb3cSopenharmony_ci return; 34223b3eb3cSopenharmony_ci } 34323b3eb3cSopenharmony_ci if (napi_get_named_property(env, value, "id", &idNApi) != napi_ok) { 34423b3eb3cSopenharmony_ci return; 34523b3eb3cSopenharmony_ci } 34623b3eb3cSopenharmony_ci std::string resName; 34723b3eb3cSopenharmony_ci std::string moduleName; 34823b3eb3cSopenharmony_ci ResourceType resType; 34923b3eb3cSopenharmony_ci if (!ParseDollarResource(env, idNApi, resType, resName, moduleName)) { 35023b3eb3cSopenharmony_ci return; 35123b3eb3cSopenharmony_ci } 35223b3eb3cSopenharmony_ci bool hasProperty = false; 35323b3eb3cSopenharmony_ci napi_value typeIdNApi = nullptr; 35423b3eb3cSopenharmony_ci napi_value resourceIdNApi = nullptr; 35523b3eb3cSopenharmony_ci napi_value typeKeyNApi = CreateNapiString(env, "type"); 35623b3eb3cSopenharmony_ci napi_value defaultNameNApi = CreateNapiString(env, ""); 35723b3eb3cSopenharmony_ci napi_value bundleNameKeyNApi = CreateNapiString(env, "bundleName"); 35823b3eb3cSopenharmony_ci napi_value moduleNameKeyNApi = CreateNapiString(env, "moduleName"); 35923b3eb3cSopenharmony_ci napi_create_int32(env, UNKNOWN_RESOURCE_ID, &resourceIdNApi); 36023b3eb3cSopenharmony_ci napi_create_int32(env, static_cast<uint32_t>(resType), &typeIdNApi); 36123b3eb3cSopenharmony_ci ModifyResourceParam(env, value, resType, resName); 36223b3eb3cSopenharmony_ci napi_set_property(env, value, typeKeyNApi, typeIdNApi); 36323b3eb3cSopenharmony_ci napi_set_property(env, value, CreateNapiString(env, "id"), resourceIdNApi); 36423b3eb3cSopenharmony_ci napi_has_property(env, value, bundleNameKeyNApi, &hasProperty); 36523b3eb3cSopenharmony_ci if (!hasProperty) { 36623b3eb3cSopenharmony_ci napi_set_property(env, value, bundleNameKeyNApi, defaultNameNApi); 36723b3eb3cSopenharmony_ci } 36823b3eb3cSopenharmony_ci napi_has_property(env, value, moduleNameKeyNApi, &hasProperty); 36923b3eb3cSopenharmony_ci if (!hasProperty) { 37023b3eb3cSopenharmony_ci napi_set_property(env, value, moduleNameKeyNApi, defaultNameNApi); 37123b3eb3cSopenharmony_ci } 37223b3eb3cSopenharmony_ci} 37323b3eb3cSopenharmony_ci 37423b3eb3cSopenharmony_civoid CompleteResourceParamV2(napi_env env, napi_value value) 37523b3eb3cSopenharmony_ci{ 37623b3eb3cSopenharmony_ci napi_value paramsNApi = nullptr; 37723b3eb3cSopenharmony_ci if (napi_get_named_property(env, value, "params", ¶msNApi) != napi_ok) { 37823b3eb3cSopenharmony_ci return; 37923b3eb3cSopenharmony_ci } 38023b3eb3cSopenharmony_ci bool isArray = false; 38123b3eb3cSopenharmony_ci napi_is_array(env, paramsNApi, &isArray); 38223b3eb3cSopenharmony_ci if (!isArray) { 38323b3eb3cSopenharmony_ci return; 38423b3eb3cSopenharmony_ci } 38523b3eb3cSopenharmony_ci uint32_t paramCount = 0; 38623b3eb3cSopenharmony_ci napi_get_array_length(env, paramsNApi, ¶mCount); 38723b3eb3cSopenharmony_ci if (paramCount <= 0) { 38823b3eb3cSopenharmony_ci return; 38923b3eb3cSopenharmony_ci } 39023b3eb3cSopenharmony_ci napi_value resNameNApi = nullptr; 39123b3eb3cSopenharmony_ci napi_get_element(env, paramsNApi, 0, &resNameNApi); 39223b3eb3cSopenharmony_ci std::string resName; 39323b3eb3cSopenharmony_ci std::string moduleName; 39423b3eb3cSopenharmony_ci ResourceType resType; 39523b3eb3cSopenharmony_ci if (!ParseDollarResource(env, resNameNApi, resType, resName, moduleName)) { 39623b3eb3cSopenharmony_ci return; 39723b3eb3cSopenharmony_ci } 39823b3eb3cSopenharmony_ci napi_value typeIdNApi = nullptr; 39923b3eb3cSopenharmony_ci napi_value typeKeyNApi = CreateNapiString(env, "type"); 40023b3eb3cSopenharmony_ci napi_create_int32(env, static_cast<uint32_t>(resType), &typeIdNApi); 40123b3eb3cSopenharmony_ci napi_set_property(env, value, typeKeyNApi, typeIdNApi); 40223b3eb3cSopenharmony_ci if (!moduleName.empty()) { 40323b3eb3cSopenharmony_ci napi_value moduleNameNApi = CreateNapiString(env, moduleName); 40423b3eb3cSopenharmony_ci napi_value moduleNameKeyNApi = CreateNapiString(env, "moduleName"); 40523b3eb3cSopenharmony_ci napi_set_property(env, value, moduleNameKeyNApi, moduleNameNApi); 40623b3eb3cSopenharmony_ci } 40723b3eb3cSopenharmony_ci} 40823b3eb3cSopenharmony_ci 40923b3eb3cSopenharmony_civoid ModifyResourceParam(napi_env env, napi_value value, const ResourceType& resType, const std::string& resName) 41023b3eb3cSopenharmony_ci{ 41123b3eb3cSopenharmony_ci // raw input : {"id":"app.xxx.xxx","params":[],"moduleName":"xxx","bundleName":"xxx"} 41223b3eb3cSopenharmony_ci // modified output : {"id":-1, "params":["app.xxx.xxx"],"type":xxxx,"moduleName":"xxx","bundleName":"xxx"} 41323b3eb3cSopenharmony_ci napi_value paramsNApi = nullptr; 41423b3eb3cSopenharmony_ci napi_get_named_property(env, value, "params", ¶msNApi); 41523b3eb3cSopenharmony_ci bool isArray = false; 41623b3eb3cSopenharmony_ci if (napi_is_array(env, paramsNApi, &isArray) != napi_ok) { 41723b3eb3cSopenharmony_ci return; 41823b3eb3cSopenharmony_ci } 41923b3eb3cSopenharmony_ci if (!isArray) { 42023b3eb3cSopenharmony_ci return; 42123b3eb3cSopenharmony_ci } 42223b3eb3cSopenharmony_ci uint32_t paramCount = 0; 42323b3eb3cSopenharmony_ci bool hasProperty = false; 42423b3eb3cSopenharmony_ci napi_get_array_length(env, paramsNApi, ¶mCount); 42523b3eb3cSopenharmony_ci napi_value typeKeyNApi = CreateNapiString(env, "type"); 42623b3eb3cSopenharmony_ci napi_value resNameNApi = CreateNapiString(env, resName); 42723b3eb3cSopenharmony_ci if (resType == ResourceType::PLURAL || resType == ResourceType::STRING) { 42823b3eb3cSopenharmony_ci std::vector<napi_value> tmpParams; 42923b3eb3cSopenharmony_ci for (uint32_t i = 0; i < paramCount; i++) { 43023b3eb3cSopenharmony_ci napi_value param = nullptr; 43123b3eb3cSopenharmony_ci napi_get_element(env, paramsNApi, i, ¶m); 43223b3eb3cSopenharmony_ci tmpParams.insert(tmpParams.end(), param); 43323b3eb3cSopenharmony_ci } 43423b3eb3cSopenharmony_ci napi_set_element(env, paramsNApi, 0, resNameNApi); 43523b3eb3cSopenharmony_ci uint32_t paramIndex = 1; 43623b3eb3cSopenharmony_ci napi_has_property(env, value, typeKeyNApi, &hasProperty); 43723b3eb3cSopenharmony_ci if (hasProperty) { 43823b3eb3cSopenharmony_ci napi_value firstParam = nullptr; 43923b3eb3cSopenharmony_ci napi_get_property(env, value, typeKeyNApi, &firstParam); 44023b3eb3cSopenharmony_ci napi_set_element(env, paramsNApi, paramIndex, firstParam); 44123b3eb3cSopenharmony_ci paramIndex++; 44223b3eb3cSopenharmony_ci } 44323b3eb3cSopenharmony_ci for (auto tmpParam : tmpParams) { 44423b3eb3cSopenharmony_ci napi_set_element(env, paramsNApi, paramIndex, tmpParam); 44523b3eb3cSopenharmony_ci paramIndex++; 44623b3eb3cSopenharmony_ci } 44723b3eb3cSopenharmony_ci } else { 44823b3eb3cSopenharmony_ci napi_set_element(env, paramsNApi, 0, resNameNApi); 44923b3eb3cSopenharmony_ci } 45023b3eb3cSopenharmony_ci} 45123b3eb3cSopenharmony_ci 45223b3eb3cSopenharmony_civoid ParseCurveInfo(const std::string& curveString, std::string& curveTypeString, std::vector<float>& curveValue) 45323b3eb3cSopenharmony_ci{ 45423b3eb3cSopenharmony_ci if (curveString.back() != ')') { 45523b3eb3cSopenharmony_ci return; 45623b3eb3cSopenharmony_ci } 45723b3eb3cSopenharmony_ci std::string::size_type leftEmbracePosition = curveString.find_last_of('('); 45823b3eb3cSopenharmony_ci if (leftEmbracePosition == std::string::npos) { 45923b3eb3cSopenharmony_ci return; 46023b3eb3cSopenharmony_ci } 46123b3eb3cSopenharmony_ci curveTypeString = curveString.substr(0, leftEmbracePosition); 46223b3eb3cSopenharmony_ci auto params = curveString.substr(leftEmbracePosition + 1, curveString.length() - leftEmbracePosition - 2); 46323b3eb3cSopenharmony_ci if (curveTypeString.empty() || params.empty()) { 46423b3eb3cSopenharmony_ci return; 46523b3eb3cSopenharmony_ci } 46623b3eb3cSopenharmony_ci std::vector<std::string> paramsVector; 46723b3eb3cSopenharmony_ci StringUtils::StringSplitter(params, ',', paramsVector); 46823b3eb3cSopenharmony_ci for (auto& param : paramsVector) { 46923b3eb3cSopenharmony_ci Framework::RemoveHeadTailSpace(param); 47023b3eb3cSopenharmony_ci if (param == "true" || param == "start") { 47123b3eb3cSopenharmony_ci param = "1.000000"; 47223b3eb3cSopenharmony_ci } 47323b3eb3cSopenharmony_ci if (param == "false" || param == "end") { 47423b3eb3cSopenharmony_ci param = "0.000000"; 47523b3eb3cSopenharmony_ci } 47623b3eb3cSopenharmony_ci errno = 0; 47723b3eb3cSopenharmony_ci char* end = nullptr; 47823b3eb3cSopenharmony_ci float value = strtof(param.c_str(), &end); 47923b3eb3cSopenharmony_ci if (end == param.c_str() || errno == ERANGE) { 48023b3eb3cSopenharmony_ci LOGW("%{public}s can not be converted to float or is out of range.", param.c_str()); 48123b3eb3cSopenharmony_ci } 48223b3eb3cSopenharmony_ci curveValue.emplace_back(value); 48323b3eb3cSopenharmony_ci } 48423b3eb3cSopenharmony_ci} 48523b3eb3cSopenharmony_ci 48623b3eb3cSopenharmony_cinapi_value ParseCurve(napi_env env, napi_value value, std::string& curveTypeString, std::vector<float>& curveValue) 48723b3eb3cSopenharmony_ci{ 48823b3eb3cSopenharmony_ci CHECK_NULL_RETURN(value, nullptr); 48923b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 49023b3eb3cSopenharmony_ci napi_typeof(env, value, &valueType); 49123b3eb3cSopenharmony_ci NAPI_ASSERT(env, valueType == napi_object || valueType == napi_string, "The type of curve is incorrect"); 49223b3eb3cSopenharmony_ci if (valueType == napi_object) { 49323b3eb3cSopenharmony_ci napi_value curveObjectNApi = nullptr; 49423b3eb3cSopenharmony_ci napi_get_named_property(env, value, "__curveString", &curveObjectNApi); 49523b3eb3cSopenharmony_ci value = curveObjectNApi; 49623b3eb3cSopenharmony_ci } 49723b3eb3cSopenharmony_ci 49823b3eb3cSopenharmony_ci size_t paramLen = 0; 49923b3eb3cSopenharmony_ci napi_status status = napi_get_value_string_utf8(env, value, nullptr, 0, ¶mLen); 50023b3eb3cSopenharmony_ci NAPI_ASSERT(env, paramLen > 0 && paramLen < NAPI_BUF_LENGTH && status == napi_ok, "paramLen error"); 50123b3eb3cSopenharmony_ci char params[NAPI_BUF_LENGTH] = { 0 }; 50223b3eb3cSopenharmony_ci status = napi_get_value_string_utf8(env, value, params, paramLen + 1, ¶mLen); 50323b3eb3cSopenharmony_ci NAPI_ASSERT(env, status == napi_ok, "Parse curve failed"); 50423b3eb3cSopenharmony_ci 50523b3eb3cSopenharmony_ci RefPtr<Curve> curve; 50623b3eb3cSopenharmony_ci const std::string domAnimationDefaultCurveString = "ease-in-out"; 50723b3eb3cSopenharmony_ci if (params[0] == '\0') { 50823b3eb3cSopenharmony_ci curve = Framework::CreateCurve(domAnimationDefaultCurveString); 50923b3eb3cSopenharmony_ci } else { 51023b3eb3cSopenharmony_ci curve = Framework::CreateCurve(params); 51123b3eb3cSopenharmony_ci } 51223b3eb3cSopenharmony_ci std::string curveString = curve->ToString(); 51323b3eb3cSopenharmony_ci ParseCurveInfo(curveString, curveTypeString, curveValue); 51423b3eb3cSopenharmony_ci return nullptr; 51523b3eb3cSopenharmony_ci} 51623b3eb3cSopenharmony_ci 51723b3eb3cSopenharmony_cinapi_valuetype GetValueType(napi_env env, napi_value value) 51823b3eb3cSopenharmony_ci{ 51923b3eb3cSopenharmony_ci if (value == nullptr) { 52023b3eb3cSopenharmony_ci return napi_undefined; 52123b3eb3cSopenharmony_ci } 52223b3eb3cSopenharmony_ci 52323b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 52423b3eb3cSopenharmony_ci NAPI_CALL_BASE(env, napi_typeof(env, value, &valueType), napi_undefined); 52523b3eb3cSopenharmony_ci return valueType; 52623b3eb3cSopenharmony_ci} 52723b3eb3cSopenharmony_ci 52823b3eb3cSopenharmony_cistd::optional<std::string> GetStringFromValueUtf8(napi_env env, napi_value value) 52923b3eb3cSopenharmony_ci{ 53023b3eb3cSopenharmony_ci static constexpr size_t maxLength = 2048; 53123b3eb3cSopenharmony_ci if (GetValueType(env, value) != napi_string) { 53223b3eb3cSopenharmony_ci return std::nullopt; 53323b3eb3cSopenharmony_ci } 53423b3eb3cSopenharmony_ci 53523b3eb3cSopenharmony_ci size_t paramLen = 0; 53623b3eb3cSopenharmony_ci napi_status status = napi_get_value_string_utf8(env, value, nullptr, 0, ¶mLen); 53723b3eb3cSopenharmony_ci if (paramLen == 0 || paramLen > maxLength || status != napi_ok) { 53823b3eb3cSopenharmony_ci return std::nullopt; 53923b3eb3cSopenharmony_ci } 54023b3eb3cSopenharmony_ci char params[maxLength] = { 0 }; 54123b3eb3cSopenharmony_ci status = napi_get_value_string_utf8(env, value, params, paramLen + 1, ¶mLen); 54223b3eb3cSopenharmony_ci if (status != napi_ok) { 54323b3eb3cSopenharmony_ci return std::nullopt; 54423b3eb3cSopenharmony_ci } 54523b3eb3cSopenharmony_ci return params; 54623b3eb3cSopenharmony_ci} 54723b3eb3cSopenharmony_ci 54823b3eb3cSopenharmony_cibool GetIntProperty(napi_env env, napi_value value, const std::string& key, int32_t& result) 54923b3eb3cSopenharmony_ci{ 55023b3eb3cSopenharmony_ci CHECK_NULL_RETURN(value, false); 55123b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 55223b3eb3cSopenharmony_ci napi_value propertyNApi = nullptr; 55323b3eb3cSopenharmony_ci napi_get_named_property(env, value, key.c_str(), &propertyNApi); 55423b3eb3cSopenharmony_ci if (valueType != napi_number) { 55523b3eb3cSopenharmony_ci LOGE("The type of property is incorrect"); 55623b3eb3cSopenharmony_ci return false; 55723b3eb3cSopenharmony_ci } 55823b3eb3cSopenharmony_ci int32_t property = 0; 55923b3eb3cSopenharmony_ci napi_status status = napi_get_value_int32(env, propertyNApi, &property); 56023b3eb3cSopenharmony_ci if (status != napi_ok) { 56123b3eb3cSopenharmony_ci LOGE("Get property failed"); 56223b3eb3cSopenharmony_ci return false; 56323b3eb3cSopenharmony_ci } 56423b3eb3cSopenharmony_ci return true; 56523b3eb3cSopenharmony_ci} 56623b3eb3cSopenharmony_ci 56723b3eb3cSopenharmony_cistatic uint32_t CompleteColorAlphaIfIncomplete(uint32_t origin) 56823b3eb3cSopenharmony_ci{ 56923b3eb3cSopenharmony_ci constexpr uint32_t colorAlphaOffset = 24; 57023b3eb3cSopenharmony_ci constexpr uint32_t colorAlphaDefaultValue = 0xFF000000; 57123b3eb3cSopenharmony_ci uint32_t result = origin; 57223b3eb3cSopenharmony_ci if ((origin >> colorAlphaOffset) == 0) { 57323b3eb3cSopenharmony_ci result = origin | colorAlphaDefaultValue; 57423b3eb3cSopenharmony_ci } 57523b3eb3cSopenharmony_ci return result; 57623b3eb3cSopenharmony_ci} 57723b3eb3cSopenharmony_ci 57823b3eb3cSopenharmony_cibool ParseColorFromResourceObject(napi_env env, napi_value value, Color& colorResult) 57923b3eb3cSopenharmony_ci{ 58023b3eb3cSopenharmony_ci ResourceInfo resourceInfo; 58123b3eb3cSopenharmony_ci if (!ParseResourceParam(env, value, resourceInfo)) { 58223b3eb3cSopenharmony_ci LOGE("Parse color from resource failed"); 58323b3eb3cSopenharmony_ci return false; 58423b3eb3cSopenharmony_ci } 58523b3eb3cSopenharmony_ci auto themeConstants = GetThemeConstants(resourceInfo.bundleName, resourceInfo.moduleName); 58623b3eb3cSopenharmony_ci if (themeConstants == nullptr) { 58723b3eb3cSopenharmony_ci LOGE("themeConstants is nullptr"); 58823b3eb3cSopenharmony_ci return false; 58923b3eb3cSopenharmony_ci } 59023b3eb3cSopenharmony_ci if (resourceInfo.type == static_cast<int32_t>(ResourceType::STRING)) { 59123b3eb3cSopenharmony_ci auto colorString = themeConstants->GetString(resourceInfo.type); 59223b3eb3cSopenharmony_ci return Color::ParseColorString(colorString, colorResult); 59323b3eb3cSopenharmony_ci } 59423b3eb3cSopenharmony_ci if (resourceInfo.type == static_cast<int32_t>(ResourceType::INTEGER)) { 59523b3eb3cSopenharmony_ci auto colorInt = themeConstants->GetInt(resourceInfo.type); 59623b3eb3cSopenharmony_ci colorResult = Color(CompleteColorAlphaIfIncomplete(colorInt)); 59723b3eb3cSopenharmony_ci return true; 59823b3eb3cSopenharmony_ci } 59923b3eb3cSopenharmony_ci colorResult = themeConstants->GetColor(resourceInfo.resId); 60023b3eb3cSopenharmony_ci return true; 60123b3eb3cSopenharmony_ci} 60223b3eb3cSopenharmony_ci 60323b3eb3cSopenharmony_cibool ParseColor(napi_env env, napi_value value, Color& result) 60423b3eb3cSopenharmony_ci{ 60523b3eb3cSopenharmony_ci napi_valuetype valueType = GetValueType(env, value); 60623b3eb3cSopenharmony_ci if (valueType != napi_number && valueType != napi_string && valueType != napi_object) { 60723b3eb3cSopenharmony_ci return false; 60823b3eb3cSopenharmony_ci } 60923b3eb3cSopenharmony_ci if (valueType == napi_number) { 61023b3eb3cSopenharmony_ci int32_t colorId = 0; 61123b3eb3cSopenharmony_ci napi_get_value_int32(env, value, &colorId); 61223b3eb3cSopenharmony_ci result = Color(CompleteColorAlphaIfIncomplete(static_cast<uint32_t>(colorId))); 61323b3eb3cSopenharmony_ci return true; 61423b3eb3cSopenharmony_ci } 61523b3eb3cSopenharmony_ci if (valueType == napi_string) { 61623b3eb3cSopenharmony_ci std::optional<std::string> colorString = GetStringFromValueUtf8(env, value); 61723b3eb3cSopenharmony_ci if (!colorString.has_value()) { 61823b3eb3cSopenharmony_ci LOGE("Parse color from string failed"); 61923b3eb3cSopenharmony_ci } 62023b3eb3cSopenharmony_ci return Color::ParseColorString(colorString.value(), result); 62123b3eb3cSopenharmony_ci } 62223b3eb3cSopenharmony_ci 62323b3eb3cSopenharmony_ci return ParseColorFromResourceObject(env, value, result); 62423b3eb3cSopenharmony_ci} 62523b3eb3cSopenharmony_ci 62623b3eb3cSopenharmony_cibool ParseResourceParam(napi_env env, napi_value value, ResourceInfo& info) 62723b3eb3cSopenharmony_ci{ 62823b3eb3cSopenharmony_ci CompleteResourceParam(env, value); 62923b3eb3cSopenharmony_ci napi_value idNApi = nullptr; 63023b3eb3cSopenharmony_ci napi_value typeNApi = nullptr; 63123b3eb3cSopenharmony_ci napi_value paramsNApi = nullptr; 63223b3eb3cSopenharmony_ci napi_value bundleNameNApi = nullptr; 63323b3eb3cSopenharmony_ci napi_value moduleNameNApi = nullptr; 63423b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 63523b3eb3cSopenharmony_ci napi_typeof(env, value, &valueType); 63623b3eb3cSopenharmony_ci if (valueType == napi_object) { 63723b3eb3cSopenharmony_ci napi_get_named_property(env, value, "id", &idNApi); 63823b3eb3cSopenharmony_ci napi_get_named_property(env, value, "type", &typeNApi); 63923b3eb3cSopenharmony_ci napi_get_named_property(env, value, "params", ¶msNApi); 64023b3eb3cSopenharmony_ci napi_get_named_property(env, value, "bundleName", &bundleNameNApi); 64123b3eb3cSopenharmony_ci napi_get_named_property(env, value, "moduleName", &moduleNameNApi); 64223b3eb3cSopenharmony_ci } else { 64323b3eb3cSopenharmony_ci return false; 64423b3eb3cSopenharmony_ci } 64523b3eb3cSopenharmony_ci 64623b3eb3cSopenharmony_ci napi_typeof(env, idNApi, &valueType); 64723b3eb3cSopenharmony_ci if (valueType == napi_number) { 64823b3eb3cSopenharmony_ci napi_get_value_int32(env, idNApi, &info.resId); 64923b3eb3cSopenharmony_ci } 65023b3eb3cSopenharmony_ci 65123b3eb3cSopenharmony_ci napi_typeof(env, typeNApi, &valueType); 65223b3eb3cSopenharmony_ci if (valueType == napi_number) { 65323b3eb3cSopenharmony_ci napi_get_value_int32(env, typeNApi, &info.type); 65423b3eb3cSopenharmony_ci } 65523b3eb3cSopenharmony_ci 65623b3eb3cSopenharmony_ci bool isArray = false; 65723b3eb3cSopenharmony_ci if (napi_is_array(env, paramsNApi, &isArray) != napi_ok) { 65823b3eb3cSopenharmony_ci return false; 65923b3eb3cSopenharmony_ci } 66023b3eb3cSopenharmony_ci 66123b3eb3cSopenharmony_ci if (!isArray) { 66223b3eb3cSopenharmony_ci return false; 66323b3eb3cSopenharmony_ci } 66423b3eb3cSopenharmony_ci 66523b3eb3cSopenharmony_ci uint32_t arrayLength = 0; 66623b3eb3cSopenharmony_ci napi_get_array_length(env, paramsNApi, &arrayLength); 66723b3eb3cSopenharmony_ci 66823b3eb3cSopenharmony_ci for (uint32_t i = 0; i < arrayLength; i++) { 66923b3eb3cSopenharmony_ci size_t ret = 0; 67023b3eb3cSopenharmony_ci napi_value indexValue = nullptr; 67123b3eb3cSopenharmony_ci napi_get_element(env, paramsNApi, i, &indexValue); 67223b3eb3cSopenharmony_ci napi_typeof(env, indexValue, &valueType); 67323b3eb3cSopenharmony_ci if (valueType == napi_string) { 67423b3eb3cSopenharmony_ci size_t strLen = GetParamLen(env, indexValue) + 1; 67523b3eb3cSopenharmony_ci std::unique_ptr<char[]> indexStr = std::make_unique<char[]>(strLen); 67623b3eb3cSopenharmony_ci napi_get_value_string_utf8(env, indexValue, indexStr.get(), strLen, &ret); 67723b3eb3cSopenharmony_ci info.params.emplace_back(indexStr.get()); 67823b3eb3cSopenharmony_ci } else if (valueType == napi_number) { 67923b3eb3cSopenharmony_ci int32_t num; 68023b3eb3cSopenharmony_ci napi_get_value_int32(env, indexValue, &num); 68123b3eb3cSopenharmony_ci info.params.emplace_back(std::to_string(num)); 68223b3eb3cSopenharmony_ci } 68323b3eb3cSopenharmony_ci } 68423b3eb3cSopenharmony_ci 68523b3eb3cSopenharmony_ci napi_typeof(env, bundleNameNApi, &valueType); 68623b3eb3cSopenharmony_ci if (valueType == napi_string) { 68723b3eb3cSopenharmony_ci size_t ret = 0; 68823b3eb3cSopenharmony_ci size_t strLen = GetParamLen(env, bundleNameNApi) + 1; 68923b3eb3cSopenharmony_ci std::unique_ptr<char[]> bundleNameStr = std::make_unique<char[]>(strLen); 69023b3eb3cSopenharmony_ci napi_get_value_string_utf8(env, bundleNameNApi, bundleNameStr.get(), strLen, &ret); 69123b3eb3cSopenharmony_ci info.bundleName = bundleNameStr.get(); 69223b3eb3cSopenharmony_ci } 69323b3eb3cSopenharmony_ci 69423b3eb3cSopenharmony_ci napi_typeof(env, moduleNameNApi, &valueType); 69523b3eb3cSopenharmony_ci if (valueType == napi_string) { 69623b3eb3cSopenharmony_ci size_t ret = 0; 69723b3eb3cSopenharmony_ci size_t strLen = GetParamLen(env, moduleNameNApi) + 1; 69823b3eb3cSopenharmony_ci std::unique_ptr<char[]> moduleNameStr = std::make_unique<char[]>(strLen); 69923b3eb3cSopenharmony_ci napi_get_value_string_utf8(env, moduleNameNApi, moduleNameStr.get(), strLen, &ret); 70023b3eb3cSopenharmony_ci info.moduleName = moduleNameStr.get(); 70123b3eb3cSopenharmony_ci } 70223b3eb3cSopenharmony_ci 70323b3eb3cSopenharmony_ci return true; 70423b3eb3cSopenharmony_ci} 70523b3eb3cSopenharmony_ci 70623b3eb3cSopenharmony_cistd::string DimensionToString(Dimension dimension) 70723b3eb3cSopenharmony_ci{ 70823b3eb3cSopenharmony_ci static const int32_t unitsNum = 6; 70923b3eb3cSopenharmony_ci static const int32_t percentIndex = 3; 71023b3eb3cSopenharmony_ci static const int32_t percentUnit = 100; 71123b3eb3cSopenharmony_ci static std::array<std::string, unitsNum> units = { "px", "vp", "fp", "%", "lpx", "auto" }; 71223b3eb3cSopenharmony_ci auto unit = dimension.Unit(); 71323b3eb3cSopenharmony_ci auto value = dimension.Value(); 71423b3eb3cSopenharmony_ci if (unit == DimensionUnit::NONE) { 71523b3eb3cSopenharmony_ci return StringUtils::DoubleToString(value).append("none"); 71623b3eb3cSopenharmony_ci } 71723b3eb3cSopenharmony_ci if (units[static_cast<int>(unit)] == units[percentIndex]) { 71823b3eb3cSopenharmony_ci return StringUtils::DoubleToString(value * percentUnit).append(units[static_cast<int>(unit)]); 71923b3eb3cSopenharmony_ci } 72023b3eb3cSopenharmony_ci return StringUtils::DoubleToString(value).append(units[static_cast<int>(unit)]); 72123b3eb3cSopenharmony_ci} 72223b3eb3cSopenharmony_ci 72323b3eb3cSopenharmony_cibool ParseString(const ResourceInfo& info, std::string& result) 72423b3eb3cSopenharmony_ci{ 72523b3eb3cSopenharmony_ci auto resourceWrapper = CreateResourceWrapper(info); 72623b3eb3cSopenharmony_ci if (info.type == static_cast<int>(ResourceType::PLURAL)) { 72723b3eb3cSopenharmony_ci std::string pluralResults; 72823b3eb3cSopenharmony_ci if (info.resId == UNKNOWN_RESOURCE_ID) { 72923b3eb3cSopenharmony_ci auto count = StringUtils::StringToInt(info.params[1]); 73023b3eb3cSopenharmony_ci pluralResults = resourceWrapper->GetPluralStringByName(info.params[0], count); 73123b3eb3cSopenharmony_ci ReplaceHolder(pluralResults, info.params, 2); // plural holder in index 2 73223b3eb3cSopenharmony_ci } else { 73323b3eb3cSopenharmony_ci auto count = StringUtils::StringToInt(info.params[0]); 73423b3eb3cSopenharmony_ci pluralResults = resourceWrapper->GetPluralString(info.resId, count); 73523b3eb3cSopenharmony_ci ReplaceHolder(pluralResults, info.params, 1); 73623b3eb3cSopenharmony_ci } 73723b3eb3cSopenharmony_ci result = pluralResults; 73823b3eb3cSopenharmony_ci return true; 73923b3eb3cSopenharmony_ci } 74023b3eb3cSopenharmony_ci if (info.type == static_cast<int>(ResourceType::RAWFILE)) { 74123b3eb3cSopenharmony_ci auto fileName = info.params[0]; 74223b3eb3cSopenharmony_ci result = resourceWrapper->GetRawfile(fileName); 74323b3eb3cSopenharmony_ci return true; 74423b3eb3cSopenharmony_ci } 74523b3eb3cSopenharmony_ci if (info.type == static_cast<int>(ResourceType::FLOAT)) { 74623b3eb3cSopenharmony_ci if (info.resId == UNKNOWN_RESOURCE_ID) { 74723b3eb3cSopenharmony_ci result = DimensionToString(resourceWrapper->GetDimensionByName(info.params[0])); 74823b3eb3cSopenharmony_ci } else { 74923b3eb3cSopenharmony_ci result = DimensionToString(resourceWrapper->GetDimension(info.resId)); 75023b3eb3cSopenharmony_ci } 75123b3eb3cSopenharmony_ci return true; 75223b3eb3cSopenharmony_ci } 75323b3eb3cSopenharmony_ci if (info.type == static_cast<int>(ResourceType::STRING)) { 75423b3eb3cSopenharmony_ci std::string originStr; 75523b3eb3cSopenharmony_ci if (info.resId == UNKNOWN_RESOURCE_ID) { 75623b3eb3cSopenharmony_ci originStr = resourceWrapper->GetStringByName(info.params[0]); 75723b3eb3cSopenharmony_ci ReplaceHolder(originStr, info.params, 1); 75823b3eb3cSopenharmony_ci } else { 75923b3eb3cSopenharmony_ci originStr = resourceWrapper->GetString(info.resId); 76023b3eb3cSopenharmony_ci ReplaceHolder(originStr, info.params, 0); 76123b3eb3cSopenharmony_ci } 76223b3eb3cSopenharmony_ci result = originStr; 76323b3eb3cSopenharmony_ci return true; 76423b3eb3cSopenharmony_ci } 76523b3eb3cSopenharmony_ci if (info.type == static_cast<int>(ResourceType::COLOR)) { 76623b3eb3cSopenharmony_ci result = resourceWrapper->GetColor(info.resId).ColorToString(); 76723b3eb3cSopenharmony_ci return true; 76823b3eb3cSopenharmony_ci } 76923b3eb3cSopenharmony_ci if (info.type == static_cast<int>(ResourceType::INTEGER)) { 77023b3eb3cSopenharmony_ci result = std::to_string(resourceWrapper->GetInt(info.resId)); 77123b3eb3cSopenharmony_ci return true; 77223b3eb3cSopenharmony_ci } 77323b3eb3cSopenharmony_ci return true; 77423b3eb3cSopenharmony_ci} 77523b3eb3cSopenharmony_ci 77623b3eb3cSopenharmony_cistd::string ErrorToMessage(int32_t code) 77723b3eb3cSopenharmony_ci{ 77823b3eb3cSopenharmony_ci auto iter = ERROR_CODE_TO_MSG.find(code); 77923b3eb3cSopenharmony_ci return (iter != ERROR_CODE_TO_MSG.end()) ? iter->second : ""; 78023b3eb3cSopenharmony_ci} 78123b3eb3cSopenharmony_ci 78223b3eb3cSopenharmony_cibool GetSingleParam(napi_env env, napi_callback_info info, napi_value* argv, napi_valuetype& valueType) 78323b3eb3cSopenharmony_ci{ 78423b3eb3cSopenharmony_ci size_t argc = 1; 78523b3eb3cSopenharmony_ci napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); 78623b3eb3cSopenharmony_ci if (argc != 1) { 78723b3eb3cSopenharmony_ci return false; 78823b3eb3cSopenharmony_ci } 78923b3eb3cSopenharmony_ci napi_typeof(env, argv[0], &valueType); 79023b3eb3cSopenharmony_ci return true; 79123b3eb3cSopenharmony_ci} 79223b3eb3cSopenharmony_ci 79323b3eb3cSopenharmony_ci// (Color | number | string | undifened) 79423b3eb3cSopenharmony_cistd::optional<Color> GetOptionalColor(napi_env env, napi_value argv, napi_valuetype& valueType) 79523b3eb3cSopenharmony_ci{ 79623b3eb3cSopenharmony_ci if (valueType == napi_number) { 79723b3eb3cSopenharmony_ci uint32_t num; 79823b3eb3cSopenharmony_ci uint32_t alpha = 0xff000000; 79923b3eb3cSopenharmony_ci napi_get_value_uint32(env, argv, &num); 80023b3eb3cSopenharmony_ci if ((num & alpha) == 0) { 80123b3eb3cSopenharmony_ci num |= alpha; 80223b3eb3cSopenharmony_ci } 80323b3eb3cSopenharmony_ci return Color(num); 80423b3eb3cSopenharmony_ci } else if (valueType == napi_string) { 80523b3eb3cSopenharmony_ci std::string str; 80623b3eb3cSopenharmony_ci bool result = GetNapiString(env, argv, str, valueType); 80723b3eb3cSopenharmony_ci Color color; 80823b3eb3cSopenharmony_ci if (!result || !Color::ParseColorString(str, color)) { 80923b3eb3cSopenharmony_ci return std::nullopt; 81023b3eb3cSopenharmony_ci } 81123b3eb3cSopenharmony_ci return color; 81223b3eb3cSopenharmony_ci } else { 81323b3eb3cSopenharmony_ci return std::nullopt; 81423b3eb3cSopenharmony_ci } 81523b3eb3cSopenharmony_ci} 81623b3eb3cSopenharmony_ci 81723b3eb3cSopenharmony_cibool ParseIntegerToString(const ResourceInfo& info, std::string& result) 81823b3eb3cSopenharmony_ci{ 81923b3eb3cSopenharmony_ci auto resourceWrapper = CreateResourceWrapper(info); 82023b3eb3cSopenharmony_ci if (info.type == static_cast<int>(ResourceType::INTEGER)) { 82123b3eb3cSopenharmony_ci if (info.resId == UNKNOWN_RESOURCE_ID) { 82223b3eb3cSopenharmony_ci result = std::to_string(resourceWrapper->GetIntByName(info.params[0])); 82323b3eb3cSopenharmony_ci } else { 82423b3eb3cSopenharmony_ci result = std::to_string(resourceWrapper->GetInt(info.resId)); 82523b3eb3cSopenharmony_ci } 82623b3eb3cSopenharmony_ci return true; 82723b3eb3cSopenharmony_ci } 82823b3eb3cSopenharmony_ci return true; 82923b3eb3cSopenharmony_ci} 83023b3eb3cSopenharmony_ci 83123b3eb3cSopenharmony_cibool HasProperty(napi_env env, napi_value value, const std::string& targetStr) 83223b3eb3cSopenharmony_ci{ 83323b3eb3cSopenharmony_ci bool hasProperty = false; 83423b3eb3cSopenharmony_ci napi_has_named_property(env, value, targetStr.c_str(), &hasProperty); 83523b3eb3cSopenharmony_ci return hasProperty; 83623b3eb3cSopenharmony_ci} 83723b3eb3cSopenharmony_ci 83823b3eb3cSopenharmony_cinapi_value GetReturnObject(napi_env env, std::string callbackString) 83923b3eb3cSopenharmony_ci{ 84023b3eb3cSopenharmony_ci napi_value result = nullptr; 84123b3eb3cSopenharmony_ci napi_value returnObj = nullptr; 84223b3eb3cSopenharmony_ci napi_create_object(env, &returnObj); 84323b3eb3cSopenharmony_ci napi_create_string_utf8(env, callbackString.c_str(), NAPI_AUTO_LENGTH, &result); 84423b3eb3cSopenharmony_ci napi_set_named_property(env, returnObj, "errMsg", result); 84523b3eb3cSopenharmony_ci return returnObj; 84623b3eb3cSopenharmony_ci} 84723b3eb3cSopenharmony_ci 84823b3eb3cSopenharmony_cibool ParseNapiDimension(napi_env env, CalcDimension& result, napi_value napiValue, DimensionUnit defaultUnit) 84923b3eb3cSopenharmony_ci{ 85023b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 85123b3eb3cSopenharmony_ci napi_typeof(env, napiValue, &valueType); 85223b3eb3cSopenharmony_ci if (valueType == napi_number) { 85323b3eb3cSopenharmony_ci double value = 0; 85423b3eb3cSopenharmony_ci napi_get_value_double(env, napiValue, &value); 85523b3eb3cSopenharmony_ci result.SetUnit(defaultUnit); 85623b3eb3cSopenharmony_ci result.SetValue(value); 85723b3eb3cSopenharmony_ci return true; 85823b3eb3cSopenharmony_ci } else if (valueType == napi_string) { 85923b3eb3cSopenharmony_ci std::string valueString; 86023b3eb3cSopenharmony_ci if (!GetNapiString(env, napiValue, valueString, valueType)) { 86123b3eb3cSopenharmony_ci return false; 86223b3eb3cSopenharmony_ci } 86323b3eb3cSopenharmony_ci result = StringUtils::StringToCalcDimension(valueString, false, defaultUnit); 86423b3eb3cSopenharmony_ci return true; 86523b3eb3cSopenharmony_ci } else if (valueType == napi_object) { 86623b3eb3cSopenharmony_ci ResourceInfo recv; 86723b3eb3cSopenharmony_ci std::string parameterStr; 86823b3eb3cSopenharmony_ci if (!ParseResourceParam(env, napiValue, recv)) { 86923b3eb3cSopenharmony_ci return false; 87023b3eb3cSopenharmony_ci } 87123b3eb3cSopenharmony_ci if (!ParseString(recv, parameterStr)) { 87223b3eb3cSopenharmony_ci return false; 87323b3eb3cSopenharmony_ci } 87423b3eb3cSopenharmony_ci result = StringUtils::StringToDimensionWithUnit(parameterStr, defaultUnit); 87523b3eb3cSopenharmony_ci return true; 87623b3eb3cSopenharmony_ci } 87723b3eb3cSopenharmony_ci return false; 87823b3eb3cSopenharmony_ci} 87923b3eb3cSopenharmony_ci 88023b3eb3cSopenharmony_cibool ParseNapiDimensionNG( 88123b3eb3cSopenharmony_ci napi_env env, CalcDimension& result, napi_value napiValue, DimensionUnit defaultUnit, bool isSupportPercent) 88223b3eb3cSopenharmony_ci{ 88323b3eb3cSopenharmony_ci napi_valuetype valueType = napi_undefined; 88423b3eb3cSopenharmony_ci napi_typeof(env, napiValue, &valueType); 88523b3eb3cSopenharmony_ci if (valueType == napi_number) { 88623b3eb3cSopenharmony_ci double value = 0; 88723b3eb3cSopenharmony_ci napi_get_value_double(env, napiValue, &value); 88823b3eb3cSopenharmony_ci 88923b3eb3cSopenharmony_ci result.SetUnit(defaultUnit); 89023b3eb3cSopenharmony_ci result.SetValue(value); 89123b3eb3cSopenharmony_ci return true; 89223b3eb3cSopenharmony_ci } else if (valueType == napi_string) { 89323b3eb3cSopenharmony_ci std::string valueString; 89423b3eb3cSopenharmony_ci if (!GetNapiString(env, napiValue, valueString, valueType)) { 89523b3eb3cSopenharmony_ci return false; 89623b3eb3cSopenharmony_ci } 89723b3eb3cSopenharmony_ci if (valueString.back() == '%' && !isSupportPercent) { 89823b3eb3cSopenharmony_ci return false; 89923b3eb3cSopenharmony_ci } 90023b3eb3cSopenharmony_ci return StringUtils::StringToCalcDimensionNG(valueString, result, false, defaultUnit); 90123b3eb3cSopenharmony_ci } else if (valueType == napi_object) { 90223b3eb3cSopenharmony_ci ResourceInfo recv; 90323b3eb3cSopenharmony_ci std::string parameterStr; 90423b3eb3cSopenharmony_ci if (!ParseResourceParam(env, napiValue, recv)) { 90523b3eb3cSopenharmony_ci return false; 90623b3eb3cSopenharmony_ci } 90723b3eb3cSopenharmony_ci if (!ParseString(recv, parameterStr)) { 90823b3eb3cSopenharmony_ci return false; 90923b3eb3cSopenharmony_ci } 91023b3eb3cSopenharmony_ci if (!ParseIntegerToString(recv, parameterStr)) { 91123b3eb3cSopenharmony_ci return false; 91223b3eb3cSopenharmony_ci } 91323b3eb3cSopenharmony_ci result = StringUtils::StringToDimensionWithUnit(parameterStr, defaultUnit); 91423b3eb3cSopenharmony_ci return true; 91523b3eb3cSopenharmony_ci } 91623b3eb3cSopenharmony_ci return false; 91723b3eb3cSopenharmony_ci} 91823b3eb3cSopenharmony_ci 91923b3eb3cSopenharmony_cibool ParseNapiColor(napi_env env, napi_value value, Color& result) 92023b3eb3cSopenharmony_ci{ 92123b3eb3cSopenharmony_ci napi_valuetype valueType = GetValueType(env, value); 92223b3eb3cSopenharmony_ci if (valueType != napi_number && valueType != napi_string && valueType != napi_object) { 92323b3eb3cSopenharmony_ci return false; 92423b3eb3cSopenharmony_ci } 92523b3eb3cSopenharmony_ci if (valueType == napi_number) { 92623b3eb3cSopenharmony_ci int32_t colorId = 0; 92723b3eb3cSopenharmony_ci napi_get_value_int32(env, value, &colorId); 92823b3eb3cSopenharmony_ci constexpr uint32_t colorAlphaOffset = 24; 92923b3eb3cSopenharmony_ci constexpr uint32_t colorAlphaDefaultValue = 0xFF000000; 93023b3eb3cSopenharmony_ci auto origin = static_cast<uint32_t>(colorId); 93123b3eb3cSopenharmony_ci uint32_t alphaResult = origin; 93223b3eb3cSopenharmony_ci if ((origin >> colorAlphaOffset) == 0) { 93323b3eb3cSopenharmony_ci alphaResult = origin | colorAlphaDefaultValue; 93423b3eb3cSopenharmony_ci } 93523b3eb3cSopenharmony_ci result = Color(alphaResult); 93623b3eb3cSopenharmony_ci return true; 93723b3eb3cSopenharmony_ci } 93823b3eb3cSopenharmony_ci if (valueType == napi_string) { 93923b3eb3cSopenharmony_ci std::optional<std::string> colorString = GetStringFromValueUtf8(env, value); 94023b3eb3cSopenharmony_ci if (!colorString.has_value()) { 94123b3eb3cSopenharmony_ci LOGE("Parse color from string failed"); 94223b3eb3cSopenharmony_ci return false; 94323b3eb3cSopenharmony_ci } 94423b3eb3cSopenharmony_ci return Color::ParseColorString(colorString.value(), result); 94523b3eb3cSopenharmony_ci } 94623b3eb3cSopenharmony_ci 94723b3eb3cSopenharmony_ci return ParseColorFromResourceObject(env, value, result); 94823b3eb3cSopenharmony_ci} 94923b3eb3cSopenharmony_ci 95023b3eb3cSopenharmony_cibool ParseStyle(napi_env env, napi_value value, std::optional<BorderStyle>& style) 95123b3eb3cSopenharmony_ci{ 95223b3eb3cSopenharmony_ci napi_valuetype valueType = GetValueType(env, value); 95323b3eb3cSopenharmony_ci if (valueType != napi_number) { 95423b3eb3cSopenharmony_ci return false; 95523b3eb3cSopenharmony_ci } 95623b3eb3cSopenharmony_ci int32_t num; 95723b3eb3cSopenharmony_ci napi_get_value_int32(env, value, &num); 95823b3eb3cSopenharmony_ci style = static_cast<BorderStyle>(num); 95923b3eb3cSopenharmony_ci if (style < BorderStyle::SOLID || style > BorderStyle::NONE) { 96023b3eb3cSopenharmony_ci return false; 96123b3eb3cSopenharmony_ci } 96223b3eb3cSopenharmony_ci return true; 96323b3eb3cSopenharmony_ci} 96423b3eb3cSopenharmony_ci 96523b3eb3cSopenharmony_cibool ParseShadowColorStrategy(napi_env env, napi_value value, ShadowColorStrategy& strategy) 96623b3eb3cSopenharmony_ci{ 96723b3eb3cSopenharmony_ci napi_valuetype valueType = GetValueType(env, value); 96823b3eb3cSopenharmony_ci if (valueType == napi_string) { 96923b3eb3cSopenharmony_ci std::optional<std::string> colorStr = GetStringFromValueUtf8(env, value); 97023b3eb3cSopenharmony_ci if (colorStr.has_value()) { 97123b3eb3cSopenharmony_ci if (colorStr->compare("average") == 0) { 97223b3eb3cSopenharmony_ci strategy = ShadowColorStrategy::AVERAGE; 97323b3eb3cSopenharmony_ci return true; 97423b3eb3cSopenharmony_ci } else if (colorStr->compare("primary") == 0) { 97523b3eb3cSopenharmony_ci strategy = ShadowColorStrategy::PRIMARY; 97623b3eb3cSopenharmony_ci return true; 97723b3eb3cSopenharmony_ci } 97823b3eb3cSopenharmony_ci } 97923b3eb3cSopenharmony_ci } 98023b3eb3cSopenharmony_ci return false; 98123b3eb3cSopenharmony_ci} 98223b3eb3cSopenharmony_ci} // namespace OHOS::Ace::Napi 983