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_manager.h"
17
18 #include "account_helper.h"
19 #include "app_log_wrapper.h"
20 #include "bundle_common_event_mgr.h"
21 #include "bundle_resource_parser.h"
22 #include "bundle_resource_process.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 constexpr const char* GLOBAL_RESOURCE_BUNDLE_NAME = "ohos.global.systemres";
28 }
BundleResourceManager()29 BundleResourceManager::BundleResourceManager()
30 {
31 bundleResourceRdb_ = std::make_shared<BundleResourceRdb>();
32 }
33
~BundleResourceManager()34 BundleResourceManager::~BundleResourceManager()
35 {
36 }
37
AddResourceInfo(const InnerBundleInfo & innerBundleInfo,const int32_t userId,std::string hapPath)38 bool BundleResourceManager::AddResourceInfo(const InnerBundleInfo &innerBundleInfo,
39 const int32_t userId, std::string hapPath)
40 {
41 std::vector<ResourceInfo> resourceInfos;
42 if (!BundleResourceProcess::GetResourceInfo(innerBundleInfo, userId, resourceInfos)) {
43 APP_LOGE("bundleName: %{public}s failed to GetResourceInfo", innerBundleInfo.GetBundleName().c_str());
44 return false;
45 }
46
47 if (!hapPath.empty()) {
48 for (auto &info : resourceInfos) {
49 info.hapPath_ = hapPath;
50 }
51 }
52 return AddResourceInfos(resourceInfos);
53 }
54
AddResourceInfoByBundleName(const std::string & bundleName,const int32_t userId)55 bool BundleResourceManager::AddResourceInfoByBundleName(const std::string &bundleName, const int32_t userId)
56 {
57 APP_LOGD("start, bundleName:%{public}s", bundleName.c_str());
58 std::vector<ResourceInfo> resourceInfos;
59 if (!BundleResourceProcess::GetResourceInfoByBundleName(bundleName, userId, resourceInfos)) {
60 APP_LOGE("bundleName: %{public}s GetResourceInfoByBundleName failed", bundleName.c_str());
61 return false;
62 }
63 if (!AddResourceInfos(resourceInfos)) {
64 APP_LOGE("error, bundleName:%{public}s", bundleName.c_str());
65 return false;
66 }
67 APP_LOGD("success, bundleName:%{public}s", bundleName.c_str());
68 return true;
69 }
70
AddResourceInfoByAbility(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,const int32_t userId)71 bool BundleResourceManager::AddResourceInfoByAbility(const std::string &bundleName, const std::string &moduleName,
72 const std::string &abilityName, const int32_t userId)
73 {
74 APP_LOGD("start, bundleName:%{public}s", bundleName.c_str());
75 ResourceInfo resourceInfo;
76 if (!BundleResourceProcess::GetResourceInfoByAbilityName(bundleName, moduleName, abilityName,
77 userId, resourceInfo)) {
78 APP_LOGE("bundleName: %{public}s, moduleName: %{public}s, abilityName: %{public}s failed",
79 bundleName.c_str(), moduleName.c_str(), abilityName.c_str());
80 return false;
81 }
82 if (!AddResourceInfo(resourceInfo)) {
83 APP_LOGE("error, bundleName: %{public}s, moduleName: %{public}s, abilityName: %{public}s failed",
84 bundleName.c_str(), moduleName.c_str(), abilityName.c_str());
85 return false;
86 }
87 APP_LOGD("success, bundleName: %{public}s, moduleName: %{public}s, abilityName: %{public}s failed",
88 bundleName.c_str(), moduleName.c_str(), abilityName.c_str());
89 return true;
90 }
91
AddAllResourceInfo(const int32_t userId)92 bool BundleResourceManager::AddAllResourceInfo(const int32_t userId)
93 {
94 std::vector<ResourceInfo> resourceInfos;
95 if (!BundleResourceProcess::GetAllResourceInfo(userId, resourceInfos)) {
96 APP_LOGE("GetAllResourceInfo failed, userId:%{public}d", userId);
97 return false;
98 }
99 if (!AddResourceInfos(resourceInfos)) {
100 APP_LOGE("failed, userId:%{public}d", userId);
101 return false;
102 }
103 SendBundleResourcesChangedEvent(userId);
104 return true;
105 }
106
DeleteAllResourceInfo()107 bool BundleResourceManager::DeleteAllResourceInfo()
108 {
109 return bundleResourceRdb_->DeleteAllResourceInfo();
110 }
111
AddResourceInfo(ResourceInfo & resourceInfo)112 bool BundleResourceManager::AddResourceInfo(ResourceInfo &resourceInfo)
113 {
114 // need to parse label and icon
115 BundleResourceParser parser;
116 if (!parser.ParseResourceInfo(resourceInfo)) {
117 APP_LOGW("key: %{public}s ParseResourceInfo failed", resourceInfo.GetKey().c_str());
118 ProcessResourceInfoWhenParseFailed(resourceInfo);
119 }
120 return bundleResourceRdb_->AddResourceInfo(resourceInfo);
121 }
122
AddResourceInfos(std::vector<ResourceInfo> & resourceInfos)123 bool BundleResourceManager::AddResourceInfos(std::vector<ResourceInfo> &resourceInfos)
124 {
125 if (resourceInfos.empty()) {
126 APP_LOGE("resourceInfos is empty.");
127 return false;
128 }
129 // need to parse label and icon
130 BundleResourceParser parser;
131 if (!parser.ParseResourceInfos(resourceInfos)) {
132 APP_LOGW("Parse ResourceInfos failed, need to modify label and icon");
133 for (auto &resourceInfo : resourceInfos) {
134 ProcessResourceInfoWhenParseFailed(resourceInfo);
135 }
136 }
137 return bundleResourceRdb_->AddResourceInfos(resourceInfos);
138 }
139
DeleteResourceInfo(const std::string & key)140 bool BundleResourceManager::DeleteResourceInfo(const std::string &key)
141 {
142 return bundleResourceRdb_->DeleteResourceInfo(key);
143 }
144
GetAllResourceName(std::vector<std::string> & keyNames)145 bool BundleResourceManager::GetAllResourceName(std::vector<std::string> &keyNames)
146 {
147 return bundleResourceRdb_->GetAllResourceName(keyNames);
148 }
149
AddResourceInfoByColorModeChanged(const int32_t userId)150 bool BundleResourceManager::AddResourceInfoByColorModeChanged(const int32_t userId)
151 {
152 std::vector<ResourceInfo> resourceInfos;
153 // to judge whether the current colorMode exists in the database
154 bool isExist = bundleResourceRdb_->IsCurrentColorModeExist();
155 if (isExist) {
156 // exist then update new install bundles
157 std::vector<std::string> names;
158 if (!bundleResourceRdb_->GetAllResourceName(names)) {
159 APP_LOGE("GetAllResourceName failed");
160 return false;
161 }
162 if (!BundleResourceProcess::GetResourceInfoByColorModeChanged(names, resourceInfos)) {
163 APP_LOGE("GetResourceInfoByColorModeChanged failed");
164 return false;
165 }
166 if (resourceInfos.empty()) {
167 APP_LOGI("no need to add resource");
168 SendBundleResourcesChangedEvent(userId);
169 return true;
170 }
171 } else {
172 // not exist then update all resource info
173 if (!BundleResourceProcess::GetAllResourceInfo(userId, resourceInfos)) {
174 APP_LOGE("GetAllResourceInfo failed, userId:%{public}d", userId);
175 return false;
176 }
177 }
178 if (!AddResourceInfos(resourceInfos)) {
179 APP_LOGE("failed, userId:%{public}d", userId);
180 return false;
181 }
182 SendBundleResourcesChangedEvent(userId);
183 return true;
184 }
185
GetBundleResourceInfo(const std::string & bundleName,const uint32_t flags,BundleResourceInfo & bundleResourceInfo)186 bool BundleResourceManager::GetBundleResourceInfo(const std::string &bundleName, const uint32_t flags,
187 BundleResourceInfo &bundleResourceInfo)
188 {
189 APP_LOGD("start, bundleName:%{public}s", bundleName.c_str());
190 uint32_t resourceFlags = CheckResourceFlags(flags);
191 if (bundleResourceRdb_->GetBundleResourceInfo(bundleName, resourceFlags, bundleResourceInfo)) {
192 APP_LOGD("success, bundleName:%{public}s", bundleName.c_str());
193 return true;
194 }
195 APP_LOGW("bundleName:%{public}s not exist in resource rdb, need add again ", bundleName.c_str());
196 int32_t currentUserId = AccountHelper::GetCurrentActiveUserId();
197 if (currentUserId <= 0) {
198 // invalid userId
199 currentUserId = Constants::START_USERID;
200 }
201 if (!AddResourceInfoByBundleName(bundleName, currentUserId)) {
202 APP_LOGW("bundleName:%{public}s add failed", bundleName.c_str());
203 }
204 return bundleResourceRdb_->GetBundleResourceInfo(bundleName, resourceFlags, bundleResourceInfo);
205 }
206
GetLauncherAbilityResourceInfo(const std::string & bundleName,const uint32_t flags,std::vector<LauncherAbilityResourceInfo> & launcherAbilityResourceInfo)207 bool BundleResourceManager::GetLauncherAbilityResourceInfo(const std::string &bundleName, const uint32_t flags,
208 std::vector<LauncherAbilityResourceInfo> &launcherAbilityResourceInfo)
209 {
210 APP_LOGD("start, bundleName:%{public}s", bundleName.c_str());
211 uint32_t resourceFlags = CheckResourceFlags(flags);
212 if (bundleResourceRdb_->GetLauncherAbilityResourceInfo(bundleName, resourceFlags, launcherAbilityResourceInfo)) {
213 APP_LOGD("success, bundleName:%{public}s", bundleName.c_str());
214 return true;
215 }
216 APP_LOGW("bundleName:%{public}s not exist in resource rdb, need add again ", bundleName.c_str());
217 int32_t currentUserId = AccountHelper::GetCurrentActiveUserId();
218 if (currentUserId <= 0) {
219 // invalid userId
220 currentUserId = Constants::START_USERID;
221 }
222 if (!AddResourceInfoByBundleName(bundleName, currentUserId)) {
223 APP_LOGW("bundleName:%{public}s add failed", bundleName.c_str());
224 }
225 return bundleResourceRdb_->GetLauncherAbilityResourceInfo(bundleName, resourceFlags, launcherAbilityResourceInfo);
226 }
227
GetAllBundleResourceInfo(const uint32_t flags,std::vector<BundleResourceInfo> & bundleResourceInfos)228 bool BundleResourceManager::GetAllBundleResourceInfo(const uint32_t flags,
229 std::vector<BundleResourceInfo> &bundleResourceInfos)
230 {
231 APP_LOGD("start");
232 uint32_t resourceFlags = CheckResourceFlags(flags);
233 return bundleResourceRdb_->GetAllBundleResourceInfo(resourceFlags, bundleResourceInfos);
234 }
235
GetAllLauncherAbilityResourceInfo(const uint32_t flags,std::vector<LauncherAbilityResourceInfo> & launcherAbilityResourceInfos)236 bool BundleResourceManager::GetAllLauncherAbilityResourceInfo(const uint32_t flags,
237 std::vector<LauncherAbilityResourceInfo> &launcherAbilityResourceInfos)
238 {
239 APP_LOGD("start");
240 uint32_t resourceFlags = CheckResourceFlags(flags);
241 return bundleResourceRdb_->GetAllLauncherAbilityResourceInfo(resourceFlags, launcherAbilityResourceInfos);
242 }
243
CheckResourceFlags(const uint32_t flags)244 uint32_t BundleResourceManager::CheckResourceFlags(const uint32_t flags)
245 {
246 APP_LOGD("flags:%{public}u", flags);
247 if (((flags & static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_ALL)) ==
248 static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_ALL)) ||
249 ((flags & static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL)) ==
250 static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL)) ||
251 ((flags & static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_ICON)) ==
252 static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_ICON))) {
253 return flags;
254 }
255 APP_LOGD("illegal flags");
256 return flags | static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_ALL);
257 }
258
ProcessResourceInfoWhenParseFailed(ResourceInfo & resourceInfo)259 void BundleResourceManager::ProcessResourceInfoWhenParseFailed(ResourceInfo &resourceInfo)
260 {
261 APP_LOGI("start, bundleName:%{public}s", resourceInfo.bundleName_.c_str());
262 if (resourceInfo.label_.empty()) {
263 resourceInfo.label_ = resourceInfo.bundleName_;
264 }
265 if (resourceInfo.icon_.empty()) {
266 resourceInfo.icon_ = GetDefaultIcon();
267 }
268 }
269
GetDefaultIcon()270 std::string BundleResourceManager::GetDefaultIcon()
271 {
272 BundleResourceInfo bundleResourceInfo;
273 if (!GetBundleResourceInfo(GLOBAL_RESOURCE_BUNDLE_NAME,
274 static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_WITH_ICON),
275 bundleResourceInfo)) {
276 APP_LOGE("get default icon failed");
277 return std::string();
278 }
279 return bundleResourceInfo.icon;
280 }
281
SendBundleResourcesChangedEvent(int32_t userId)282 void BundleResourceManager::SendBundleResourcesChangedEvent(int32_t userId)
283 {
284 APP_LOGD("send bundleResource event");
285 std::shared_ptr<BundleCommonEventMgr> commonEventMgr = std::make_shared<BundleCommonEventMgr>();
286 commonEventMgr->NotifyBundleResourcesChanged(userId);
287 }
288 } // AppExecFwk
289 } // OHOS
290