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