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 auto assetProviderImpl = AceType::MakeRefPtr<HapAssetProviderImpl>();
116 if (assetProviderImpl->Initialize(packagePath, assetBasePaths, useCache)) {
117 return assetProviderImpl;
118 }
119 } else {
120 auto assetProviderImpl = AceType::MakeRefPtr<FileAssetProviderImpl>();
121 if (assetProviderImpl->Initialize(packagePath, assetBasePaths)) {
122 return assetProviderImpl;
123 }
124 }
125 return nullptr;
126 }
127
ConvertAvoidArea(const OHOS::Rosen::AvoidArea & avoidArea)128 NG::SafeAreaInsets ConvertAvoidArea(const OHOS::Rosen::AvoidArea& avoidArea)
129 {
130 return NG::SafeAreaInsets({ avoidArea.leftRect_.posX_, avoidArea.leftRect_.posX_ + avoidArea.leftRect_.width_ },
131 { avoidArea.topRect_.posY_, avoidArea.topRect_.posY_ + avoidArea.topRect_.height_ },
132 { avoidArea.rightRect_.posX_, avoidArea.rightRect_.posX_ + avoidArea.rightRect_.width_ },
133 { avoidArea.bottomRect_.posY_, avoidArea.bottomRect_.posY_ + avoidArea.bottomRect_.height_ });
134 }
135
ConvertDMRect2Rect(const OHOS::Rosen::DMRect & displayAvailableRect)136 Rect ConvertDMRect2Rect(const OHOS::Rosen::DMRect& displayAvailableRect)
137 {
138 return Rect(displayAvailableRect.posX_, displayAvailableRect.posY_, displayAvailableRect.width_,
139 displayAvailableRect.height_);
140 }
141 } // namespace OHOS::Ace
142