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     LOGD("hapPath_:%{public}s", hapPath_.c_str());
42     return true;
43 }
44 
Reload()45 void HapAssetProviderImpl::Reload()
46 {
47     bool newCreate = false;
48     AbilityBase::ExtractorUtil::DeleteExtractor(loadPath_);
49     runtimeExtractor_ = AbilityBase::ExtractorUtil::GetExtractor(loadPath_, newCreate);
50     if (!runtimeExtractor_) {
51         LOGW("GetExtractor failed:%{public}s", loadPath_.c_str());
52     }
53 }
54 
IsValid() const55 bool HapAssetProviderImpl::IsValid() const
56 {
57     return true;
58 }
59 
GetAsMapping(const std::string & assetName) const60 std::unique_ptr<AssetMapping> HapAssetProviderImpl::GetAsMapping(const std::string& assetName) const
61 {
62     ACE_SCOPED_TRACE("GetAsMapping");
63     LOGD("assert name is: %{public}s :: %{public}s", hapPath_.c_str(), assetName.c_str());
64     std::lock_guard<std::mutex> lock(mutex_);
65 
66     CHECK_NULL_RETURN(runtimeExtractor_, nullptr);
67     for (const auto& basePath : assetBasePaths_) {
68         std::string fileName = basePath + assetName;
69         bool hasFile = runtimeExtractor_->HasEntry(fileName);
70         if (!hasFile) {
71             LOGD("HasEntry failed: %{public}s %{public}s", hapPath_.c_str(), fileName.c_str());
72             continue;
73         }
74         std::ostringstream osstream;
75         hasFile = runtimeExtractor_->GetFileBuffer(fileName, osstream);
76         if (!hasFile) {
77             LOGD("GetFileBuffer failed: %{public}s %{public}s", hapPath_.c_str(), fileName.c_str());
78             continue;
79         }
80         LOGD("GetFileBuffer Success: %{public}s %{public}s", hapPath_.c_str(), fileName.c_str());
81         return std::make_unique<HapAssetImplMapping>(osstream);
82     }
83     return nullptr;
84 }
85 
GetAsMappingFromI18n(const std::string & assetName) const86 std::vector<std::unique_ptr<AssetMapping>> HapAssetProviderImpl::GetAsMappingFromI18n(
87     const std::string& assetName) const
88 {
89     ACE_SCOPED_TRACE("GetAsMappingFromI18n");
90     std::lock_guard<std::mutex> lock(mutex_);
91 
92     std::vector<std::unique_ptr<AssetMapping>> i18nVector;
93     CHECK_NULL_RETURN(runtimeExtractor_, i18nVector);
94     for (const auto& basePath : assetBasePaths_) {
95         std::string fileName = basePath + assetName;
96         bool hasFile = runtimeExtractor_->HasEntry(fileName);
97         if (!hasFile) {
98             continue;
99         }
100         std::ostringstream osstream;
101         hasFile = runtimeExtractor_->GetFileBuffer(fileName, osstream);
102         if (!hasFile) {
103             continue;
104         }
105         i18nVector.push_back(std::make_unique<HapAssetImplMapping>(osstream));
106     }
107     return i18nVector;
108 }
109 
GetAssetPath(const std::string & assetName,bool isAddHapPath)110 std::string HapAssetProviderImpl::GetAssetPath(const std::string& assetName, bool isAddHapPath)
111 {
112     std::lock_guard<std::mutex> lock(mutex_);
113     CHECK_NULL_RETURN(runtimeExtractor_, "");
114     for (const auto& basePath : assetBasePaths_) {
115         std::string fileName = basePath + assetName;
116         bool hasFile = runtimeExtractor_->HasEntry(fileName);
117         if (!hasFile) {
118             continue;
119         }
120         return isAddHapPath ? (hapPath_ + "/" + basePath) : fileName;
121     }
122     LOGI("Cannot find base path of %{public}s", assetName.c_str());
123     return "";
124 }
125 
GetAssetList(const std::string & path,std::vector<std::string> & assetList)126 void HapAssetProviderImpl::GetAssetList(const std::string& path, std::vector<std::string>& assetList)
127 {
128     std::lock_guard<std::mutex> lock(mutex_);
129     if (!runtimeExtractor_) {
130         LOGW("RuntimeExtractor null:%{public}s", loadPath_.c_str());
131         return;
132     }
133     for (const auto& basePath : assetBasePaths_) {
134         std::string assetPath = basePath + path;
135         bool res = runtimeExtractor_->IsDirExist(assetPath);
136         if (!res) {
137             LOGD("IsDirExist failed: %{public}s %{public}s", hapPath_.c_str(), assetPath.c_str());
138             continue;
139         }
140         res = runtimeExtractor_->GetFileList(assetPath, assetList);
141         if (!res) {
142             LOGD("GetAssetList failed: %{public}s %{public}s", hapPath_.c_str(), assetPath.c_str());
143             continue;
144         }
145         return;
146     }
147     LOGI("Cannot Get File List from %{public}s", path.c_str());
148 }
149 
GetFileInfo(const std::string & fileName,MediaFileInfo & fileInfo) const150 bool HapAssetProviderImpl::GetFileInfo(const std::string& fileName, MediaFileInfo& fileInfo) const
151 {
152     std::lock_guard<std::mutex> lock(mutex_);
153     CHECK_NULL_RETURN(runtimeExtractor_, false);
154     OHOS::AbilityBase::FileInfo fileInfoAbility;
155     auto state = runtimeExtractor_->GetFileInfo(fileName, fileInfoAbility);
156     if (!state) {
157         LOGE("GetFileInfo failed, fileName=%{public}s", fileName.c_str());
158         return false;
159     }
160     fileInfo.fileName = fileInfoAbility.fileName;
161     fileInfo.offset = fileInfoAbility.offset;
162     fileInfo.length = fileInfoAbility.length;
163     fileInfo.lastModTime = fileInfoAbility.lastModTime;
164     fileInfo.lastModDate = fileInfoAbility.lastModDate;
165     return true;
166 }
167 } // namespace OHOS::Ace
168