1 /*
2 * Copyright (c) 2023 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/hap_asset_provider_impl.h"
17
18 #include "base/log/ace_trace.h"
19 #include "base/utils/utils.h"
20
21 namespace OHOS::Ace {
Initialize( const std::string& hapPath, const std::vector<std::string>& assetBasePaths, bool useCache)22 bool HapAssetProviderImpl::Initialize(
23 const std::string& hapPath, const std::vector<std::string>& assetBasePaths, bool useCache)
24 {
25 ACE_SCOPED_TRACE("Initialize");
26 if (hapPath.empty() || assetBasePaths.empty()) {
27 LOGE("the packagePath or assetBasePath is empty");
28 return false;
29 }
30
31 bool newCreate = false;
32 loadPath_ = AbilityBase::ExtractorUtil::GetLoadFilePath(hapPath);
33 if (!useCache) {
34 AbilityBase::ExtractorUtil::DeleteExtractor(loadPath_);
35 }
36 runtimeExtractor_ = AbilityBase::ExtractorUtil::GetExtractor(loadPath_, newCreate);
37 CHECK_NULL_RETURN(runtimeExtractor_, false);
38 assetBasePaths_ = assetBasePaths;
39 hapPath_ = hapPath;
40 return true;
41 }
42
Reload()43 void HapAssetProviderImpl::Reload()
44 {
45 bool newCreate = false;
46 AbilityBase::ExtractorUtil::DeleteExtractor(loadPath_);
47 runtimeExtractor_ = AbilityBase::ExtractorUtil::GetExtractor(loadPath_, newCreate);
48 if (!runtimeExtractor_) {
49 LOGW("GetExtractor failed:%{public}s", loadPath_.c_str());
50 }
51 }
52
IsValid() const53 bool HapAssetProviderImpl::IsValid() const
54 {
55 return true;
56 }
57
GetAsMapping(const std::string& assetName) const58 std::unique_ptr<AssetMapping> HapAssetProviderImpl::GetAsMapping(const std::string& assetName) const
59 {
60 ACE_SCOPED_TRACE("GetAsMapping");
61 std::lock_guard<std::mutex> lock(mutex_);
62
63 CHECK_NULL_RETURN(runtimeExtractor_, nullptr);
64 for (const auto& basePath : assetBasePaths_) {
65 std::string fileName = basePath + assetName;
66 bool hasFile = runtimeExtractor_->HasEntry(fileName);
67 if (!hasFile) {
68 continue;
69 }
70 std::ostringstream osstream;
71 hasFile = runtimeExtractor_->GetFileBuffer(fileName, osstream);
72 if (!hasFile) {
73 continue;
74 }
75 return std::make_unique<HapAssetImplMapping>(osstream);
76 }
77 return nullptr;
78 }
79
GetAsMappingFromI18n( const std::string& assetName) const80 std::vector<std::unique_ptr<AssetMapping>> HapAssetProviderImpl::GetAsMappingFromI18n(
81 const std::string& assetName) const
82 {
83 ACE_SCOPED_TRACE("GetAsMappingFromI18n");
84 std::lock_guard<std::mutex> lock(mutex_);
85
86 std::vector<std::unique_ptr<AssetMapping>> i18nVector;
87 CHECK_NULL_RETURN(runtimeExtractor_, i18nVector);
88 for (const auto& basePath : assetBasePaths_) {
89 std::string fileName = basePath + assetName;
90 bool hasFile = runtimeExtractor_->HasEntry(fileName);
91 if (!hasFile) {
92 continue;
93 }
94 std::ostringstream osstream;
95 hasFile = runtimeExtractor_->GetFileBuffer(fileName, osstream);
96 if (!hasFile) {
97 continue;
98 }
99 i18nVector.push_back(std::make_unique<HapAssetImplMapping>(osstream));
100 }
101 return i18nVector;
102 }
103
GetAssetPath(const std::string& assetName, bool isAddHapPath)104 std::string HapAssetProviderImpl::GetAssetPath(const std::string& assetName, bool isAddHapPath)
105 {
106 std::lock_guard<std::mutex> lock(mutex_);
107 CHECK_NULL_RETURN(runtimeExtractor_, "");
108 for (const auto& basePath : assetBasePaths_) {
109 std::string fileName = basePath + assetName;
110 bool hasFile = runtimeExtractor_->HasEntry(fileName);
111 if (!hasFile) {
112 continue;
113 }
114 return isAddHapPath ? (hapPath_ + "/" + basePath) : fileName;
115 }
116 LOGI("Cannot find base path of %{public}s", assetName.c_str());
117 return "";
118 }
119
GetAssetList(const std::string& path, std::vector<std::string>& assetList)120 void HapAssetProviderImpl::GetAssetList(const std::string& path, std::vector<std::string>& assetList)
121 {
122 std::lock_guard<std::mutex> lock(mutex_);
123 if (!runtimeExtractor_) {
124 LOGW("RuntimeExtractor null:%{public}s", loadPath_.c_str());
125 return;
126 }
127 for (const auto& basePath : assetBasePaths_) {
128 std::string assetPath = basePath + path;
129 bool res = runtimeExtractor_->IsDirExist(assetPath);
130 if (!res) {
131 continue;
132 }
133 res = runtimeExtractor_->GetFileList(assetPath, assetList);
134 if (!res) {
135 continue;
136 }
137 return;
138 }
139 LOGI("Cannot Get File List from %{public}s", path.c_str());
140 }
141
GetFileInfo(const std::string& fileName, MediaFileInfo& fileInfo) const142 bool HapAssetProviderImpl::GetFileInfo(const std::string& fileName, MediaFileInfo& fileInfo) const
143 {
144 std::lock_guard<std::mutex> lock(mutex_);
145 CHECK_NULL_RETURN(runtimeExtractor_, false);
146 OHOS::AbilityBase::FileInfo fileInfoAbility;
147 auto state = runtimeExtractor_->GetFileInfo(fileName, fileInfoAbility);
148 if (!state) {
149 LOGE("GetFileInfo failed, fileName=%{public}s", fileName.c_str());
150 return false;
151 }
152 fileInfo.fileName = fileInfoAbility.fileName;
153 fileInfo.offset = fileInfoAbility.offset;
154 fileInfo.length = fileInfoAbility.length;
155 fileInfo.lastModTime = fileInfoAbility.lastModTime;
156 fileInfo.lastModDate = fileInfoAbility.lastModDate;
157 return true;
158 }
159 } // namespace OHOS::Ace
160