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