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 {
23
Initialize(const std::string & hapPath,const std::vector<std::string> & assetBasePaths)24 bool HapAssetProvider::Initialize(const std::string& hapPath, const std::vector<std::string>& assetBasePaths)
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 runtimeExtractor_ = AbilityRuntime::RuntimeExtractor::Create(hapPath);
33 CHECK_NULL_RETURN_NOLOG(runtimeExtractor_, false);
34 assetBasePaths_ = assetBasePaths;
35 hapPath_ = hapPath;
36 LOGD("hapPath_:%{public}s", hapPath_.c_str());
37 return true;
38 }
39
IsValid() const40 bool HapAssetProvider::IsValid() const
41 {
42 return true;
43 }
44
45 class HapAssetMapping : public fml::Mapping {
46 public:
HapAssetMapping(const std::ostringstream & ostream)47 explicit HapAssetMapping(const std::ostringstream& ostream)
48 {
49 const std::string& content = ostream.str();
50 data_.assign(content.data(), content.data() + content.size());
51 }
52
53 ~HapAssetMapping() override = default;
54
GetSize() const55 size_t GetSize() const override
56 {
57 return data_.size();
58 }
59
GetMapping() const60 const uint8_t* GetMapping() const override
61 {
62 return data_.data();
63 }
64
65 private:
66 std::vector<uint8_t> data_;
67 };
68
GetAsMapping(const std::string & assetName) const69 std::unique_ptr<fml::Mapping> HapAssetProvider::GetAsMapping(const std::string& assetName) const
70 {
71 ACE_SCOPED_TRACE("GetAsMapping");
72 LOGD("assert name is: %{public}s :: %{public}s", hapPath_.c_str(), assetName.c_str());
73 std::lock_guard<std::mutex> lock(mutex_);
74
75 for (const auto& basePath : assetBasePaths_) {
76 std::string fileName = basePath + assetName;
77 bool hasFile = runtimeExtractor_->HasEntry(fileName);
78 if (!hasFile) {
79 LOGD("HasEntry failed: %{public}s %{public}s", hapPath_.c_str(), fileName.c_str());
80 continue;
81 }
82 std::ostringstream osstream;
83 hasFile = runtimeExtractor_->GetFileBuffer(fileName, osstream);
84 if (!hasFile) {
85 LOGD("GetFileBuffer failed: %{public}s %{public}s", hapPath_.c_str(), fileName.c_str());
86 continue;
87 }
88 LOGD("GetFileBuffer Success: %{public}s %{public}s", hapPath_.c_str(), fileName.c_str());
89 return std::make_unique<HapAssetMapping>(osstream);
90 }
91 LOGI("Cannot find base path of %{public}s", assetName.c_str());
92 return nullptr;
93 }
94
GetAssetPath(const std::string & assetName)95 std::string HapAssetProvider::GetAssetPath(const std::string& assetName)
96 {
97 std::lock_guard<std::mutex> lock(mutex_);
98 for (const auto& basePath : assetBasePaths_) {
99 std::string fileName = basePath + assetName;
100 bool hasFile = runtimeExtractor_->HasEntry(fileName);
101 if (!hasFile) {
102 continue;
103 }
104 return hapPath_ + "/" + basePath;
105 }
106 LOGI("Cannot find base path of %{public}s", assetName.c_str());
107 return "";
108 }
109
GetAssetList(const std::string & path,std::vector<std::string> & assetList)110 void HapAssetProvider::GetAssetList(const std::string& path, std::vector<std::string>& assetList)
111 {
112 std::lock_guard<std::mutex> lock(mutex_);
113 for (const auto& basePath : assetBasePaths_) {
114 std::string assetPath = basePath + path;
115 bool res = runtimeExtractor_->IsDirExist(assetPath);
116 if (!res) {
117 LOGD("IsDirExist failed: %{public}s %{public}s", hapPath_.c_str(), assetPath.c_str());
118 continue;
119 }
120 res = runtimeExtractor_->GetFileList(assetPath, assetList);
121 if (!res) {
122 LOGD("GetAssetList failed: %{public}s %{public}s", hapPath_.c_str(), assetPath.c_str());
123 continue;
124 }
125 return;
126 }
127 LOGI("Cannot Get File List from %{public}s", path.c_str());
128 }
129
130 } // namespace OHOS::Ace
131