123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2022 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 "adapter/ohos/entrance/utils.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_ci#include <regex>
1923b3eb3cSopenharmony_ci
2023b3eb3cSopenharmony_ci#include "wm/wm_common.h"
2123b3eb3cSopenharmony_ci#include "dm/dm_common.h"
2223b3eb3cSopenharmony_ci
2323b3eb3cSopenharmony_ci#include "adapter/ohos/entrance/file_asset_provider_impl.h"
2423b3eb3cSopenharmony_ci#include "adapter/ohos/entrance/hap_asset_provider_impl.h"
2523b3eb3cSopenharmony_ci
2623b3eb3cSopenharmony_cinamespace OHOS::Ace {
2723b3eb3cSopenharmony_ci
2823b3eb3cSopenharmony_cistd::string GetStringFromFile(const std::string& packagePathStr, const std::string& fileName)
2923b3eb3cSopenharmony_ci{
3023b3eb3cSopenharmony_ci    auto configPath = packagePathStr + fileName;
3123b3eb3cSopenharmony_ci    char realPath[PATH_MAX] = { 0x00 };
3223b3eb3cSopenharmony_ci    if (realpath(configPath.c_str(), realPath) == nullptr) {
3323b3eb3cSopenharmony_ci        LOGE("realpath fail! filePath: %{private}s, fail reason: %{public}s", configPath.c_str(), strerror(errno));
3423b3eb3cSopenharmony_ci        return "";
3523b3eb3cSopenharmony_ci    }
3623b3eb3cSopenharmony_ci    std::unique_ptr<FILE, decltype(&fclose)> file(fopen(realPath, "rb"), fclose);
3723b3eb3cSopenharmony_ci    if (!file) {
3823b3eb3cSopenharmony_ci        LOGE("open file failed, filePath: %{private}s, fail reason: %{public}s", configPath.c_str(), strerror(errno));
3923b3eb3cSopenharmony_ci        return "";
4023b3eb3cSopenharmony_ci    }
4123b3eb3cSopenharmony_ci    if (std::fseek(file.get(), 0, SEEK_END) != 0) {
4223b3eb3cSopenharmony_ci        LOGE("seek file tail error");
4323b3eb3cSopenharmony_ci        return "";
4423b3eb3cSopenharmony_ci    }
4523b3eb3cSopenharmony_ci
4623b3eb3cSopenharmony_ci    int64_t size = std::ftell(file.get());
4723b3eb3cSopenharmony_ci    if (size == -1L) {
4823b3eb3cSopenharmony_ci        return "";
4923b3eb3cSopenharmony_ci    }
5023b3eb3cSopenharmony_ci
5123b3eb3cSopenharmony_ci    std::string fileData;
5223b3eb3cSopenharmony_ci    fileData.resize(size);
5323b3eb3cSopenharmony_ci
5423b3eb3cSopenharmony_ci    rewind(file.get());
5523b3eb3cSopenharmony_ci    size_t result = std::fread(fileData.data(), 1, fileData.size(), file.get());
5623b3eb3cSopenharmony_ci    if (result != static_cast<size_t>(size)) {
5723b3eb3cSopenharmony_ci        LOGE("read file failed");
5823b3eb3cSopenharmony_ci        return "";
5923b3eb3cSopenharmony_ci    }
6023b3eb3cSopenharmony_ci
6123b3eb3cSopenharmony_ci    return fileData;
6223b3eb3cSopenharmony_ci}
6323b3eb3cSopenharmony_ci
6423b3eb3cSopenharmony_cistd::string GetStringFromHap(const std::string& hapPath, const std::string& fileName)
6523b3eb3cSopenharmony_ci{
6623b3eb3cSopenharmony_ci    bool newCreate = false;
6723b3eb3cSopenharmony_ci    std::string loadPath = AbilityBase::ExtractorUtil::GetLoadFilePath(hapPath);
6823b3eb3cSopenharmony_ci    std::shared_ptr<AbilityBase::Extractor> extractor = AbilityBase::ExtractorUtil::GetExtractor(loadPath, newCreate);
6923b3eb3cSopenharmony_ci    if (!extractor) {
7023b3eb3cSopenharmony_ci        LOGE("read file %{public}s error\n", hapPath.c_str());
7123b3eb3cSopenharmony_ci        return "";
7223b3eb3cSopenharmony_ci    }
7323b3eb3cSopenharmony_ci
7423b3eb3cSopenharmony_ci    std::ostringstream osstream;
7523b3eb3cSopenharmony_ci    bool hasFile = extractor->GetFileBuffer(fileName, osstream);
7623b3eb3cSopenharmony_ci    if (!hasFile) {
7723b3eb3cSopenharmony_ci        LOGE("read file %{public}s /config.json error\n", hapPath.c_str());
7823b3eb3cSopenharmony_ci        return "";
7923b3eb3cSopenharmony_ci    }
8023b3eb3cSopenharmony_ci
8123b3eb3cSopenharmony_ci    return osstream.str();
8223b3eb3cSopenharmony_ci}
8323b3eb3cSopenharmony_ci
8423b3eb3cSopenharmony_cibool CheckUrlValid(const std::string& url, const std::string& hapPath)
8523b3eb3cSopenharmony_ci{
8623b3eb3cSopenharmony_ci    std::string bundleNameFlag = "@bundle:";
8723b3eb3cSopenharmony_ci    if (url.find(bundleNameFlag) == 0) {
8823b3eb3cSopenharmony_ci        return true;
8923b3eb3cSopenharmony_ci    }
9023b3eb3cSopenharmony_ci
9123b3eb3cSopenharmony_ci    auto moduleContent = GetStringFromHap(hapPath, "module.json");
9223b3eb3cSopenharmony_ci    auto moduleValue = JsonUtil::ParseJsonString(moduleContent);
9323b3eb3cSopenharmony_ci    auto pagesValue = moduleValue->GetValue("module")->GetString("pages");
9423b3eb3cSopenharmony_ci    std::string profileMark = "$profile:";
9523b3eb3cSopenharmony_ci    auto jsonPath = pagesValue.replace(0, profileMark.size(), "resources/base/profile/") + ".json";
9623b3eb3cSopenharmony_ci
9723b3eb3cSopenharmony_ci    auto jsonContent = GetStringFromHap(hapPath, jsonPath);
9823b3eb3cSopenharmony_ci    auto jsonValue = JsonUtil::ParseJsonString(jsonContent);
9923b3eb3cSopenharmony_ci    auto srcValue = jsonValue->GetValue("src");
10023b3eb3cSopenharmony_ci    auto arrSize = srcValue->GetArraySize();
10123b3eb3cSopenharmony_ci
10223b3eb3cSopenharmony_ci    for (int32_t i = 0; i < arrSize; i++) {
10323b3eb3cSopenharmony_ci        auto urlPath = srcValue->GetArrayItem(i)->GetString();
10423b3eb3cSopenharmony_ci        if (urlPath == url) {
10523b3eb3cSopenharmony_ci            return true;
10623b3eb3cSopenharmony_ci        }
10723b3eb3cSopenharmony_ci    }
10823b3eb3cSopenharmony_ci
10923b3eb3cSopenharmony_ci    return false;
11023b3eb3cSopenharmony_ci}
11123b3eb3cSopenharmony_ci
11223b3eb3cSopenharmony_ciRefPtr<AssetProviderImpl> CreateAssetProviderImpl(
11323b3eb3cSopenharmony_ci    const std::string& packagePath, const std::vector<std::string>& assetBasePaths, bool useCache)
11423b3eb3cSopenharmony_ci{
11523b3eb3cSopenharmony_ci    if (std::regex_match(packagePath, std::regex(".*\\.hap"))) {
11623b3eb3cSopenharmony_ci        auto assetProviderImpl = AceType::MakeRefPtr<HapAssetProviderImpl>();
11723b3eb3cSopenharmony_ci        if (assetProviderImpl->Initialize(packagePath, assetBasePaths, useCache)) {
11823b3eb3cSopenharmony_ci            return assetProviderImpl;
11923b3eb3cSopenharmony_ci        }
12023b3eb3cSopenharmony_ci    } else {
12123b3eb3cSopenharmony_ci        auto assetProviderImpl = AceType::MakeRefPtr<FileAssetProviderImpl>();
12223b3eb3cSopenharmony_ci        if (assetProviderImpl->Initialize(packagePath, assetBasePaths)) {
12323b3eb3cSopenharmony_ci            return assetProviderImpl;
12423b3eb3cSopenharmony_ci        }
12523b3eb3cSopenharmony_ci    }
12623b3eb3cSopenharmony_ci    return nullptr;
12723b3eb3cSopenharmony_ci}
12823b3eb3cSopenharmony_ci
12923b3eb3cSopenharmony_ciNG::SafeAreaInsets ConvertAvoidArea(const OHOS::Rosen::AvoidArea& avoidArea)
13023b3eb3cSopenharmony_ci{
13123b3eb3cSopenharmony_ci    return NG::SafeAreaInsets({ avoidArea.leftRect_.posX_, avoidArea.leftRect_.posX_ + avoidArea.leftRect_.width_ },
13223b3eb3cSopenharmony_ci        { avoidArea.topRect_.posY_, avoidArea.topRect_.posY_ + avoidArea.topRect_.height_ },
13323b3eb3cSopenharmony_ci        { avoidArea.rightRect_.posX_, avoidArea.rightRect_.posX_ + avoidArea.rightRect_.width_ },
13423b3eb3cSopenharmony_ci        { avoidArea.bottomRect_.posY_, avoidArea.bottomRect_.posY_ + avoidArea.bottomRect_.height_ });
13523b3eb3cSopenharmony_ci}
13623b3eb3cSopenharmony_ci
13723b3eb3cSopenharmony_ciRect ConvertDMRect2Rect(const OHOS::Rosen::DMRect& displayAvailableRect)
13823b3eb3cSopenharmony_ci{
13923b3eb3cSopenharmony_ci    return Rect(displayAvailableRect.posX_, displayAvailableRect.posY_, displayAvailableRect.width_,
14023b3eb3cSopenharmony_ci        displayAvailableRect.height_);
14123b3eb3cSopenharmony_ci}
14223b3eb3cSopenharmony_ci} // namespace OHOS::Ace
143