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