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