• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 #include "adapter/ohos/entrance/file_asset_provider.h"
27 #include "adapter/ohos/entrance/hap_asset_provider.h"
28 
29 namespace OHOS::Ace {
30 
GetStringFromFile(const std::string & packagePathStr,const std::string & fileName)31 std::string GetStringFromFile(const std::string& packagePathStr, const std::string& fileName)
32 {
33     auto configPath = packagePathStr + fileName;
34     char realPath[PATH_MAX] = { 0x00 };
35     if (realpath(configPath.c_str(), realPath) == nullptr) {
36         LOGE("realpath fail! filePath: %{private}s, fail reason: %{public}s", configPath.c_str(), strerror(errno));
37         return "";
38     }
39     std::unique_ptr<FILE, decltype(&fclose)> file(fopen(realPath, "rb"), fclose);
40     if (!file) {
41         LOGE("open file failed, filePath: %{private}s, fail reason: %{public}s", configPath.c_str(), strerror(errno));
42         return "";
43     }
44     if (std::fseek(file.get(), 0, SEEK_END) != 0) {
45         LOGE("seek file tail error");
46         return "";
47     }
48 
49     int64_t size = std::ftell(file.get());
50     if (size == -1L) {
51         return "";
52     }
53 
54     std::string fileData;
55     fileData.resize(size);
56 
57     rewind(file.get());
58     size_t result = std::fread(fileData.data(), 1, fileData.size(), file.get());
59     if (result != static_cast<size_t>(size)) {
60         LOGE("read file failed");
61         return "";
62     }
63 
64     return fileData;
65 }
66 
GetStringFromHap(const std::string & hapPath,const std::string & fileName)67 std::string GetStringFromHap(const std::string& hapPath, const std::string& fileName)
68 {
69     bool newCreate = false;
70     std::string loadPath = AbilityBase::ExtractorUtil::GetLoadFilePath(hapPath);
71     std::shared_ptr<AbilityBase::Extractor> extractor = AbilityBase::ExtractorUtil::GetExtractor(loadPath, newCreate);
72     if (!extractor) {
73         LOGE("read file %{public}s error\n", hapPath.c_str());
74         return "";
75     }
76 
77     std::ostringstream osstream;
78     bool hasFile = extractor->GetFileBuffer(fileName, osstream);
79     if (!hasFile) {
80         LOGE("read file %{public}s /config.json error\n", hapPath.c_str());
81         return "";
82     }
83 
84     return osstream.str();
85 }
86 
CreateAssetProvider(const std::string & packagePath,const std::vector<std::string> & assetBasePaths,bool useCache)87 RefPtr<FlutterAssetProvider> CreateAssetProvider(
88     const std::string& packagePath, const std::vector<std::string>& assetBasePaths, bool useCache)
89 {
90     if (std::regex_match(packagePath, std::regex(".*\\.hap"))) {
91         auto assetProvider = AceType::MakeRefPtr<HapAssetProvider>();
92         if (assetProvider->Initialize(packagePath, assetBasePaths, useCache)) {
93             return assetProvider;
94         }
95     } else {
96         auto assetProvider = AceType::MakeRefPtr<FileAssetProvider>();
97         if (assetProvider->Initialize(packagePath, assetBasePaths)) {
98             return assetProvider;
99         }
100     }
101     return nullptr;
102 }
103 
ConvertAvoidArea(const OHOS::Rosen::AvoidArea & avoidArea)104 NG::SafeAreaInsets ConvertAvoidArea(const OHOS::Rosen::AvoidArea& avoidArea)
105 {
106     return NG::SafeAreaInsets({ avoidArea.leftRect_.posX_, avoidArea.leftRect_.posX_ + avoidArea.leftRect_.width_ },
107         { avoidArea.topRect_.posY_, avoidArea.topRect_.posY_ + avoidArea.topRect_.height_ },
108         { avoidArea.rightRect_.posX_, avoidArea.rightRect_.posX_ + avoidArea.rightRect_.width_ },
109         { avoidArea.bottomRect_.posY_, avoidArea.bottomRect_.posY_ + avoidArea.bottomRect_.height_ });
110 }
111 } // namespace OHOS::Ace
112