• 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/hap_asset_provider.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 HapAssetProvider::Initialize(const std::string& hapPath, const std::vector<std::string>& assetBasePaths,
24     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_NOLOG(runtimeExtractor_, false);
39     assetBasePaths_ = assetBasePaths;
40     hapPath_ = hapPath;
41     LOGD("hapPath_:%{public}s", hapPath_.c_str());
42     return true;
43 }
44 
Reload()45 void HapAssetProvider::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 HapAssetProvider::IsValid() const
56 {
57     return true;
58 }
59 
60 class HapAssetMapping : public fml::Mapping {
61 public:
HapAssetMapping(const std::ostringstream & ostream)62     explicit HapAssetMapping(const std::ostringstream& ostream)
63     {
64         const std::string& content = ostream.str();
65         data_.assign(content.data(), content.data() + content.size());
66     }
67 
68     ~HapAssetMapping() override = default;
69 
GetSize() const70     size_t GetSize() const override
71     {
72         return data_.size();
73     }
74 
GetMapping() const75     const uint8_t* GetMapping() const override
76     {
77         return data_.data();
78     }
79 
80 private:
81     std::vector<uint8_t> data_;
82 };
83 
GetAsMapping(const std::string & assetName) const84 std::unique_ptr<fml::Mapping> HapAssetProvider::GetAsMapping(const std::string& assetName) const
85 {
86     ACE_SCOPED_TRACE("GetAsMapping");
87     LOGD("assert name is: %{public}s :: %{public}s", hapPath_.c_str(), assetName.c_str());
88     std::lock_guard<std::mutex> lock(mutex_);
89 
90     CHECK_NULL_RETURN_NOLOG(runtimeExtractor_, nullptr);
91     for (const auto& basePath : assetBasePaths_) {
92         std::string fileName = basePath + assetName;
93         bool hasFile = runtimeExtractor_->HasEntry(fileName);
94         if (!hasFile) {
95             LOGD("HasEntry failed: %{public}s %{public}s", hapPath_.c_str(), fileName.c_str());
96             continue;
97         }
98         std::ostringstream osstream;
99         hasFile = runtimeExtractor_->GetFileBuffer(fileName, osstream);
100         if (!hasFile) {
101             LOGD("GetFileBuffer failed: %{public}s %{public}s", hapPath_.c_str(), fileName.c_str());
102             continue;
103         }
104         LOGD("GetFileBuffer Success: %{public}s %{public}s", hapPath_.c_str(), fileName.c_str());
105         return std::make_unique<HapAssetMapping>(osstream);
106     }
107     LOGI("Cannot find base path of %{public}s", assetName.c_str());
108     return nullptr;
109 }
110 
GetAssetPath(const std::string & assetName,bool isAddHapPath)111 std::string HapAssetProvider::GetAssetPath(const std::string& assetName, bool isAddHapPath)
112 {
113     std::lock_guard<std::mutex> lock(mutex_);
114     CHECK_NULL_RETURN_NOLOG(runtimeExtractor_, "");
115     for (const auto& basePath : assetBasePaths_) {
116         std::string fileName = basePath + assetName;
117         bool hasFile = runtimeExtractor_->HasEntry(fileName);
118         if (!hasFile) {
119             continue;
120         }
121         return isAddHapPath? (hapPath_ + "/" + basePath) : fileName;
122     }
123     LOGI("Cannot find base path of %{public}s", assetName.c_str());
124     return "";
125 }
126 
GetAssetList(const std::string & path,std::vector<std::string> & assetList)127 void HapAssetProvider::GetAssetList(const std::string& path, std::vector<std::string>& assetList)
128 {
129     std::lock_guard<std::mutex> lock(mutex_);
130     if (!runtimeExtractor_) {
131         LOGW("RuntimeExtractor null:%{public}s", loadPath_.c_str());
132         return;
133     }
134     for (const auto& basePath : assetBasePaths_) {
135         std::string assetPath = basePath + path;
136         bool res = runtimeExtractor_->IsDirExist(assetPath);
137         if (!res) {
138             LOGD("IsDirExist failed: %{public}s %{public}s", hapPath_.c_str(), assetPath.c_str());
139             continue;
140         }
141         res = runtimeExtractor_->GetFileList(assetPath, assetList);
142         if (!res) {
143             LOGD("GetAssetList failed: %{public}s %{public}s", hapPath_.c_str(), assetPath.c_str());
144             continue;
145         }
146         return;
147     }
148     LOGI("Cannot Get File List from %{public}s", path.c_str());
149 }
150 
GetFileInfo(const std::string & fileName,MediaFileInfo & fileInfo) const151 bool HapAssetProvider::GetFileInfo(const std::string& fileName, MediaFileInfo& fileInfo) const
152 {
153     std::lock_guard<std::mutex> lock(mutex_);
154     CHECK_NULL_RETURN_NOLOG(runtimeExtractor_, false);
155     OHOS::AbilityBase::FileInfo fileInfoAbility;
156     auto state = runtimeExtractor_->GetFileInfo(fileName, fileInfoAbility);
157     if (!state) {
158         LOGE("GetFileInfo failed, fileName=%{public}s", fileName.c_str());
159         return false;
160     }
161     fileInfo.fileName = fileInfoAbility.fileName;
162     fileInfo.offset = fileInfoAbility.offset;
163     fileInfo.length = fileInfoAbility.length;
164     fileInfo.lastModTime = fileInfoAbility.lastModTime;
165     fileInfo.lastModDate = fileInfoAbility.lastModDate;
166     return true;
167 }
168 } // namespace OHOS::Ace
169