• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bundle_resource_parser.h"
17 
18 #include "app_log_wrapper.h"
19 #include "bundle_resource_configuration.h"
20 #include "bundle_system_state.h"
21 #include "bundle_resource_drawable.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
BundleResourceParser()25 BundleResourceParser::BundleResourceParser()
26 {
27 }
28 
~BundleResourceParser()29 BundleResourceParser::~BundleResourceParser()
30 {
31 }
32 
ParseResourceInfo(ResourceInfo & resourceInfo)33 bool BundleResourceParser::ParseResourceInfo(ResourceInfo &resourceInfo)
34 {
35     if (resourceInfo.defaultIconHapPath_.empty()) {
36         return ParseResourceInfoWithSameHap(resourceInfo);
37     }
38     return ParseResourceInfoWithDifferentHap(resourceInfo);
39 }
40 
ParseResourceInfos(std::vector<ResourceInfo> & resourceInfos)41 bool BundleResourceParser::ParseResourceInfos(std::vector<ResourceInfo> &resourceInfos)
42 {
43     if (resourceInfos.empty()) {
44         APP_LOGE("resourceInfos is empty");
45         return false;
46     }
47     bool result = true;
48     for (auto &info : resourceInfos) {
49         if (!ParseResourceInfo(info)) {
50             APP_LOGW("ParseResource failed, key: %{public}s", info.GetKey().c_str());
51             result = false;
52         }
53     }
54     return result;
55 }
56 
ParseResourceInfoWithSameHap(ResourceInfo & resourceInfo)57 bool BundleResourceParser::ParseResourceInfoWithSameHap(ResourceInfo &resourceInfo)
58 {
59     if (resourceInfo.hapPath_.empty()) {
60         APP_LOGE("resourceInfo.hapPath_ is empty");
61         return false;
62     }
63     std::shared_ptr<Global::Resource::ResourceManager> resourceManager(Global::Resource::CreateResourceManager());
64     if (resourceManager == nullptr) {
65         APP_LOGE("resourceManager is nullptr");
66         return false;
67     }
68     if (!BundleResourceConfiguration::InitResourceGlobalConfig(resourceInfo.hapPath_, resourceManager)) {
69         APP_LOGE("InitResourceGlobalConfig failed, key:%{public}s", resourceInfo.GetKey().c_str());
70         return false;
71     }
72     if (!ParseResourceInfoByResourceManager(resourceManager, resourceInfo)) {
73         APP_LOGE("ParseResourceInfo failed, key:%{public}s", resourceInfo.GetKey().c_str());
74         return false;
75     }
76     return true;
77 }
78 
ParseResourceInfoWithDifferentHap(ResourceInfo & resourceInfo)79 bool BundleResourceParser::ParseResourceInfoWithDifferentHap(ResourceInfo &resourceInfo)
80 {
81     if (!ParseLabelResourceByPath(resourceInfo.hapPath_, resourceInfo.labelId_, resourceInfo.label_)) {
82         APP_LOGE("bundleName: %{public}s ParseLabelResource failed", resourceInfo.bundleName_.c_str());
83         return false;
84     }
85     if (!ParseIconResourceByPath(resourceInfo.defaultIconHapPath_, resourceInfo.iconId_, resourceInfo.icon_)) {
86         APP_LOGE("bundleName: %{public}s ParseIconResource failed", resourceInfo.bundleName_.c_str());
87         return false;
88     }
89     return true;
90 }
91 
ParseLabelResourceByPath(const std::string & hapPath,const int32_t labelId,std::string & label)92 bool BundleResourceParser::ParseLabelResourceByPath(
93     const std::string &hapPath, const int32_t labelId, std::string &label)
94 {
95     if (hapPath.empty()) {
96         APP_LOGE("hapPath is empty");
97         return false;
98     }
99     // allow label resource parse failed, then label is bundleName
100     if (labelId <= 0) {
101         APP_LOGW("labelId is 0");
102         return true;
103     }
104     std::shared_ptr<Global::Resource::ResourceManager> resourceManager(Global::Resource::CreateResourceManager());
105     if (resourceManager == nullptr) {
106         APP_LOGE("resourceManager is nullptr");
107         return false;
108     }
109     if (!BundleResourceConfiguration::InitResourceGlobalConfig(hapPath, resourceManager)) {
110         APP_LOGE("InitResourceGlobalConfig failed, key:%{private}s", hapPath.c_str());
111         return false;
112     }
113     if (!ParseLabelResourceByResourceManager(resourceManager, labelId, label)) {
114         APP_LOGE("ParseLabelResource failed, label: %{public}d", labelId);
115         return false;
116     }
117     return true;
118 }
119 
ParseIconResourceByPath(const std::string & hapPath,const int32_t iconId,std::string & icon)120 bool BundleResourceParser::ParseIconResourceByPath(const std::string &hapPath, const int32_t iconId, std::string &icon)
121 {
122     if (hapPath.empty()) {
123         APP_LOGE("hapPath is empty");
124         return false;
125     }
126     std::shared_ptr<Global::Resource::ResourceManager> resourceManager(Global::Resource::CreateResourceManager());
127     if (resourceManager == nullptr) {
128         APP_LOGE("resourceManager is nullptr");
129         return false;
130     }
131     if (!BundleResourceConfiguration::InitResourceGlobalConfig(hapPath, resourceManager)) {
132         APP_LOGE("InitResourceGlobalConfig failed, hapPath:%{private}s", hapPath.c_str());
133         return false;
134     }
135     if (!ParseIconResourceByResourceManager(resourceManager, iconId, icon)) {
136         APP_LOGE("failed, iconId: %{public}d", iconId);
137         return false;
138     }
139     return true;
140 }
141 
ParseResourceInfoByResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> resourceManager,ResourceInfo & resourceInfo)142 bool BundleResourceParser::ParseResourceInfoByResourceManager(
143     const std::shared_ptr<Global::Resource::ResourceManager> resourceManager,
144     ResourceInfo &resourceInfo)
145 {
146     if (resourceManager == nullptr) {
147         APP_LOGE("resourceManager is nullptr");
148         return false;
149     }
150 
151     if (!ParseLabelResourceByResourceManager(resourceManager, resourceInfo.labelId_, resourceInfo.label_)) {
152         APP_LOGE("ParseLabelResource failed, key: %{public}s", resourceInfo.GetKey().c_str());
153         return false;
154     }
155 
156     if (!ParseIconResourceByResourceManager(resourceManager, resourceInfo.iconId_, resourceInfo.icon_)) {
157         APP_LOGE("ParseIconResource failed, key: %{public}s", resourceInfo.GetKey().c_str());
158         return false;
159     }
160 
161     return true;
162 }
163 
ParseLabelResourceByResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> resourceManager,const int32_t labelId,std::string & label)164 bool BundleResourceParser::ParseLabelResourceByResourceManager(
165     const std::shared_ptr<Global::Resource::ResourceManager> resourceManager,
166     const int32_t labelId, std::string &label)
167 {
168     if (resourceManager == nullptr) {
169         APP_LOGE("resourceManager is nullptr");
170         return false;
171     }
172     if (labelId <= 0) {
173         APP_LOGD("ParseLabelResource labelId is 0 or less than 0, label is bundleName");
174         return true;
175     }
176     auto ret = resourceManager->GetStringById(static_cast<uint32_t>(labelId), label);
177     if (ret != OHOS::Global::Resource::RState::SUCCESS) {
178         APP_LOGE("GetStringById failed errcode: %{public}d, labelId: %{public}d",
179             static_cast<int32_t>(ret), labelId);
180         return false;
181     }
182     return true;
183 }
184 
ParseIconResourceByResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> resourceManager,const int32_t iconId,std::string & icon)185 bool BundleResourceParser::ParseIconResourceByResourceManager(
186     const std::shared_ptr<Global::Resource::ResourceManager> resourceManager,
187     const int32_t iconId, std::string &icon)
188 {
189     if (resourceManager == nullptr) {
190         APP_LOGE("resourceManager is nullptr");
191         return false;
192     }
193     if (iconId <= 0) {
194         APP_LOGE("iconId is 0 or less than 0");
195         return false;
196     }
197     // density 0
198     BundleResourceDrawable drawable;
199     if (!drawable.GetIconResourceByDrawable(iconId, 0, resourceManager, icon)) {
200         APP_LOGE("parse layered-image failed iconId:%{public}d", iconId);
201         return false;
202     }
203     return true;
204 }
205 } // AppExecFwk
206 } // OHOS