1 /*
2  * Copyright (c) 2022 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 #include "adapter/ohos/entrance/utils.h"
17 
18 #include <cstdio>
19 #include <regex>
20 #include <sstream>
21 #include <string>
22 
23 #include "extractor.h"
24 #include "wm/wm_common.h"
25 #include "dm/dm_common.h"
26 
27 #include "adapter/ohos/entrance/file_asset_provider_impl.h"
28 #include "adapter/ohos/entrance/hap_asset_provider_impl.h"
29 
30 namespace OHOS::Ace {
31 
GetStringFromFile(const std::string & packagePathStr,const std::string & fileName)32 std::string GetStringFromFile(const std::string& packagePathStr, const std::string& fileName)
33 {
34     auto configPath = packagePathStr + fileName;
35     char realPath[PATH_MAX] = { 0x00 };
36     if (realpath(configPath.c_str(), realPath) == nullptr) {
37         LOGE("realpath fail! filePath: %{private}s, fail reason: %{public}s", configPath.c_str(), strerror(errno));
38         return "";
39     }
40     std::unique_ptr<FILE, decltype(&fclose)> file(fopen(realPath, "rb"), fclose);
41     if (!file) {
42         LOGE("open file failed, filePath: %{private}s, fail reason: %{public}s", configPath.c_str(), strerror(errno));
43         return "";
44     }
45     if (std::fseek(file.get(), 0, SEEK_END) != 0) {
46         LOGE("seek file tail error");
47         return "";
48     }
49 
50     int64_t size = std::ftell(file.get());
51     if (size == -1L) {
52         return "";
53     }
54 
55     std::string fileData;
56     fileData.resize(size);
57 
58     rewind(file.get());
59     size_t result = std::fread(fileData.data(), 1, fileData.size(), file.get());
60     if (result != static_cast<size_t>(size)) {
61         LOGE("read file failed");
62         return "";
63     }
64 
65     return fileData;
66 }
67 
GetStringFromHap(const std::string & hapPath,const std::string & fileName)68 std::string GetStringFromHap(const std::string& hapPath, const std::string& fileName)
69 {
70     bool newCreate = false;
71     std::string loadPath = AbilityBase::ExtractorUtil::GetLoadFilePath(hapPath);
72     std::shared_ptr<AbilityBase::Extractor> extractor = AbilityBase::ExtractorUtil::GetExtractor(loadPath, newCreate);
73     if (!extractor) {
74         LOGE("read file %{public}s error\n", hapPath.c_str());
75         return "";
76     }
77 
78     std::ostringstream osstream;
79     bool hasFile = extractor->GetFileBuffer(fileName, osstream);
80     if (!hasFile) {
81         LOGE("read file %{public}s /config.json error\n", hapPath.c_str());
82         return "";
83     }
84 
85     return osstream.str();
86 }
87 
CheckUrlValid(const std::string & url,const std::string & hapPath)88 bool CheckUrlValid(const std::string& url, const std::string& hapPath)
89 {
90     std::string bundleNameFlag = "@bundle:";
91     if (url.find(bundleNameFlag) == 0) {
92         return true;
93     }
94 
95     auto moduleContent = GetStringFromHap(hapPath, "module.json");
96     auto moduleValue = JsonUtil::ParseJsonString(moduleContent);
97     auto pagesValue = moduleValue->GetValue("module")->GetString("pages");
98     std::string profileMark = "$profile:";
99     auto jsonPath = pagesValue.replace(0, profileMark.size(), "resources/base/profile/") + ".json";
100 
101     auto jsonContent = GetStringFromHap(hapPath, jsonPath);
102     auto jsonValue = JsonUtil::ParseJsonString(jsonContent);
103     auto srcValue = jsonValue->GetValue("src");
104     auto arrSize = srcValue->GetArraySize();
105 
106     for (int32_t i = 0; i < arrSize; i++) {
107         auto urlPath = srcValue->GetArrayItem(i)->GetString();
108         if (urlPath == url) {
109             return true;
110         }
111     }
112 
113     return false;
114 }
115 
CreateAssetProviderImpl(const std::string & packagePath,const std::vector<std::string> & assetBasePaths,bool useCache)116 RefPtr<AssetProviderImpl> CreateAssetProviderImpl(
117     const std::string& packagePath, const std::vector<std::string>& assetBasePaths, bool useCache)
118 {
119     if (std::regex_match(packagePath, std::regex(".*\\.hap"))) {
120         auto assetProviderImpl = AceType::MakeRefPtr<HapAssetProviderImpl>();
121         if (assetProviderImpl->Initialize(packagePath, assetBasePaths, useCache)) {
122             return assetProviderImpl;
123         }
124     } else {
125         auto assetProviderImpl = AceType::MakeRefPtr<FileAssetProviderImpl>();
126         if (assetProviderImpl->Initialize(packagePath, assetBasePaths)) {
127             return assetProviderImpl;
128         }
129     }
130     return nullptr;
131 }
132 
ConvertAvoidArea(const OHOS::Rosen::AvoidArea & avoidArea)133 NG::SafeAreaInsets ConvertAvoidArea(const OHOS::Rosen::AvoidArea& avoidArea)
134 {
135     return NG::SafeAreaInsets({ avoidArea.leftRect_.posX_, avoidArea.leftRect_.posX_ + avoidArea.leftRect_.width_ },
136         { avoidArea.topRect_.posY_, avoidArea.topRect_.posY_ + avoidArea.topRect_.height_ },
137         { avoidArea.rightRect_.posX_, avoidArea.rightRect_.posX_ + avoidArea.rightRect_.width_ },
138         { avoidArea.bottomRect_.posY_, avoidArea.bottomRect_.posY_ + avoidArea.bottomRect_.height_ });
139 }
140 
ConvertDMRect2Rect(const OHOS::Rosen::DMRect & displayAvailableRect)141 Rect ConvertDMRect2Rect(const OHOS::Rosen::DMRect& displayAvailableRect)
142 {
143     return Rect(displayAvailableRect.posX_, displayAvailableRect.posY_, displayAvailableRect.width_,
144         displayAvailableRect.height_);
145 }
146 } // namespace OHOS::Ace
147