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 "core/common/asset_manager_impl.h"
17 #include "base/utils/utils.h"
18
19 namespace OHOS::Ace {
GetSize() const20 size_t AssetImpl::GetSize() const
21 {
22 CHECK_NULL_RETURN(assetMapping_, 0);
23 return assetMapping_->GetSize();
24 }
25
GetData() const26 const uint8_t* AssetImpl::GetData() const
27 {
28 CHECK_NULL_RETURN(assetMapping_, nullptr);
29 return assetMapping_->GetAsset();
30 }
31
PushFront(RefPtr<AssetProvider> provider)32 void AssetManagerImpl::PushFront(RefPtr<AssetProvider> provider)
33 {
34 if (provider && provider->IsValid()) {
35 assetProviders_.push_front(std::move(provider));
36 }
37 }
38
PushBack(RefPtr<AssetProvider> provider)39 void AssetManagerImpl::PushBack(RefPtr<AssetProvider> provider)
40 {
41 if (provider && provider->IsValid()) {
42 assetProviders_.push_back(std::move(provider));
43 }
44 }
45
GetAsset(const std::string & assetName)46 RefPtr<Asset> AssetManagerImpl::GetAsset(const std::string& assetName)
47 {
48 if (assetName.empty()) {
49 LOGW("GetAsset failed. assetName is null.");
50 return nullptr;
51 }
52
53 for (const auto& assetProvider : assetProviders_) {
54 auto asProvider = AceType::DynamicCast<AssetProviderImpl>(assetProvider);
55 if (!asProvider) {
56 continue;
57 }
58
59 auto assetMapping = asProvider->GetAsMapping(assetName);
60 if (assetMapping) {
61 return AceType::MakeRefPtr<AssetImpl>(std::move(assetMapping));
62 }
63 }
64
65 LOGW("GetAsset failed: %{public}s", assetName.c_str());
66 return nullptr;
67 }
68
GetAssetFromI18n(const std::string & assetName)69 std::vector<RefPtr<Asset>> AssetManagerImpl::GetAssetFromI18n(const std::string& assetName)
70 {
71 std::vector<RefPtr<Asset>> i18nAssetVector;
72 if (assetName.empty()) {
73 LOGW("GetAsset from i18n failed. assetName is null.");
74 return i18nAssetVector;
75 }
76
77 for (const auto& assetProvider : assetProviders_) {
78 auto asProvider = AceType::DynamicCast<AssetProviderImpl>(assetProvider);
79 if (!asProvider) {
80 continue;
81 }
82
83 auto assetVector = asProvider->GetAsMappingFromI18n(assetName);
84 for (auto& assetMapping : assetVector) {
85 i18nAssetVector.push_back(AceType::MakeRefPtr<AssetImpl>(std::move(assetMapping)));
86 }
87 }
88 return i18nAssetVector;
89 }
90
GetAssetPath(const std::string & assetName,bool isAddHapPath)91 std::string AssetManagerImpl::GetAssetPath(const std::string& assetName, bool isAddHapPath)
92 {
93 for (const auto& assetProvider : assetProviders_) {
94 std::string assetPath = assetProvider->GetAssetPath(assetName, isAddHapPath);
95 if (!assetPath.empty()) {
96 return assetPath;
97 }
98 }
99
100 return "";
101 }
102
SetLibPath(const std::string & appLibPathKey,const std::vector<std::string> & packagePath)103 void AssetManagerImpl::SetLibPath(const std::string& appLibPathKey, const std::vector<std::string>& packagePath)
104 {
105 libKey_ = appLibPathKey;
106 libPath_ = packagePath;
107 }
108
GetLibPath() const109 std::vector<std::string> AssetManagerImpl::GetLibPath() const
110 {
111 return libPath_;
112 }
113
GetAppLibPathKey() const114 std::string AssetManagerImpl::GetAppLibPathKey() const
115 {
116 return libKey_;
117 }
118
GetAssetList(const std::string & path,std::vector<std::string> & assetList) const119 void AssetManagerImpl::GetAssetList(const std::string& path, std::vector<std::string>& assetList) const
120 {
121 for (const auto& assetProvider : assetProviders_) {
122 assetProvider->GetAssetList(path, assetList);
123 }
124 }
125
GetFileInfo(const std::string & fileName,MediaFileInfo & fileInfo) const126 bool AssetManagerImpl::GetFileInfo(const std::string& fileName, MediaFileInfo& fileInfo) const
127 {
128 for (const auto& assetProvider : assetProviders_) {
129 if (assetProvider->GetFileInfo(fileName, fileInfo)) {
130 return true;
131 }
132 }
133 return false;
134 }
135
ReloadProvider()136 void AssetManagerImpl::ReloadProvider()
137 {
138 for (auto& assetProvider : assetProviders_) {
139 assetProvider->Reload();
140 }
141 }
142 } // namespace OHOS::Ace
143