1 /*
2 * Copyright (c) 2020 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_ms_feature.h"
17
18 #include "ability_info_utils.h"
19 #include "appexecfwk_errors.h"
20 #include "bundle_info_utils.h"
21 #include "bundle_inner_interface.h"
22 #include "bundle_manager_service.h"
23 #include "bundle_message_id.h"
24 #include "convert_utils.h"
25 #include "liteipc_adapter.h"
26 #include "log.h"
27 #include "message.h"
28 #include "ohos_init.h"
29 #include "samgr_lite.h"
30 #include "securec.h"
31 #include "serializer.h"
32 #include "utils.h"
33 #include "want_utils.h"
34
35 namespace OHOS {
36 #ifdef __LINUX__
37 constexpr static uint32_t MAX_IPC_STRING_LENGTH = 8192UL;
38 #endif
39 static BmsImpl g_bmsImpl = {
40 SERVER_IPROXY_IMPL_BEGIN,
41 .Invoke = BundleMsFeature::Invoke,
42 .QueryAbilityInfo = BundleMsFeature::QueryAbilityInfo,
43 .GetBundleInfo = BundleMsFeature::GetBundleInfo,
44 .GetBundleInfos = BundleMsFeature::GetBundleInfos,
45 .QueryKeepAliveBundleInfos = BundleMsFeature::QueryKeepAliveBundleInfos,
46 .GetBundleNameForUid = BundleMsFeature::GetBundleNameForUid,
47 .GetBundleSize = BundleMsFeature::GetBundleSize,
48 IPROXY_END
49 };
50
51 BundleInvokeType BundleMsFeature::BundleMsInvokeFuc[BMS_INNER_BEGIN] {
52 QueryInnerAbilityInfo,
53 GetInnerBundleInfo,
54 ChangeInnerCallbackServiceId,
55 GetInnerBundleNameForUid,
56 HandleGetBundleInfos,
57 HandleGetBundleInfos,
58 HandleGetBundleInfos,
59 HasSystemCapability,
60 GetInnerBundleSize,
61 GetSystemAvailableCapabilities,
62 };
63
GetBmsFeatureApi(Feature * feature)64 IUnknown *GetBmsFeatureApi(Feature *feature)
65 {
66 g_bmsImpl.bundleMsFeature = reinterpret_cast<BundleMsFeature *>(feature);
67 return GET_IUNKNOWN(g_bmsImpl);
68 }
69
Init()70 static void Init()
71 {
72 SamgrLite *sm = SAMGR_GetInstance();
73 if (sm == nullptr) {
74 return;
75 }
76 sm->RegisterFeature(BMS_SERVICE, reinterpret_cast<Feature *>(BundleMsFeature::GetInstance()));
77 sm->RegisterFeatureApi(BMS_SERVICE, BMS_FEATURE,
78 GetBmsFeatureApi(reinterpret_cast<Feature *>(BundleMsFeature::GetInstance())));
79 HILOG_DEBUG(HILOG_MODULE_APP, "BundleMS feature start success");
80 }
81 APP_FEATURE_INIT(Init);
82
BundleMsFeature()83 BundleMsFeature::BundleMsFeature() : identity_()
84 {
85 this->Feature::GetName = BundleMsFeature::GetFeatureName;
86 this->Feature::OnInitialize = BundleMsFeature::OnFeatureInitialize;
87 this->Feature::OnStop = BundleMsFeature::OnFeatureStop;
88 this->Feature::OnMessage = BundleMsFeature::OnFeatureMessage;
89 }
90
~BundleMsFeature()91 BundleMsFeature::~BundleMsFeature() {}
92
GetFeatureName(Feature * feature)93 const char *BundleMsFeature::GetFeatureName(Feature *feature)
94 {
95 (void) feature;
96 return BMS_FEATURE;
97 }
98
OnFeatureInitialize(Feature * feature,Service * parent,Identity identity)99 void BundleMsFeature::OnFeatureInitialize(Feature *feature, Service *parent, Identity identity)
100 {
101 if (feature == nullptr) {
102 return;
103 }
104 (reinterpret_cast<BundleMsFeature *>(feature))->identity_ = identity;
105 bool ret = GetInstance()->BundleServiceTaskInit();
106 HILOG_DEBUG(HILOG_MODULE_APP, "BundleMS feature initialized, result is %d", ret);
107 }
108
OnFeatureStop(Feature * feature,Identity identity)109 void BundleMsFeature::OnFeatureStop(Feature *feature, Identity identity)
110 {
111 (void) feature;
112 (void) identity;
113 }
114
OnFeatureMessage(Feature * feature,Request * request)115 BOOL BundleMsFeature::OnFeatureMessage(Feature *feature, Request *request)
116 {
117 if (feature == nullptr || request == nullptr) {
118 return FALSE;
119 }
120 ManagerService::GetInstance().ServiceMsgProcess(request);
121 return TRUE;
122 }
123
InnerFreeDataBuff(void * ptr)124 static void InnerFreeDataBuff(void *ptr)
125 {
126 if (ptr != nullptr) {
127 cJSON_free(ptr);
128 }
129 }
130
HasSystemCapability(const uint8_t funcId,IpcIo * req,IpcIo * reply)131 uint8_t BundleMsFeature::HasSystemCapability(const uint8_t funcId, IpcIo *req, IpcIo *reply)
132 {
133 if ((req == nullptr) || (reply == nullptr)) {
134 return ERR_APPEXECFWK_OBJECT_NULL;
135 }
136 size_t size = 0;
137 char *sysCapName = reinterpret_cast<char *>(IpcIoPopString(req, &size));
138 if (sysCapName == nullptr) {
139 return ERR_APPEXECFWK_DESERIALIZATION_FAILED;
140 }
141 bool hasSysCap = OHOS::ManagerService::GetInstance().HasSystemCapability(sysCapName);
142 if (hasSysCap) {
143 IpcIoPushUint8(reply, static_cast<uint8_t>(OHOS_SUCCESS));
144 return OHOS_SUCCESS;
145 }
146 return ERR_APPEXECFWK_OBJECT_NULL;
147 }
148
GetSystemAvailableCapabilities(const uint8_t funcId,IpcIo * req,IpcIo * reply)149 uint8_t BundleMsFeature::GetSystemAvailableCapabilities(const uint8_t funcId, IpcIo *req, IpcIo *reply)
150 {
151 if ((req == nullptr) || (reply == nullptr)) {
152 return ERR_APPEXECFWK_OBJECT_NULL;
153 }
154
155 char sysCaps[MAX_SYSCAP_NUM][MAX_SYSCAP_NAME_LEN];
156 int32_t len = MAX_SYSCAP_NUM;
157 uint8_t errorCode = OHOS::ManagerService::GetInstance().GetSystemAvailableCapabilities(sysCaps, &len);
158 if (errorCode != OHOS_SUCCESS) {
159 return errorCode;
160 }
161 IpcIoPushUint8(reply, static_cast<uint8_t>(OHOS_SUCCESS));
162 IpcIoPushInt32(reply, len);
163 for (int32_t index = 0; index < len; index++) {
164 IpcIoPushString(reply, sysCaps[index]);
165 }
166 return errorCode;
167 }
168
QueryInnerAbilityInfo(const uint8_t funcId,IpcIo * req,IpcIo * reply)169 uint8_t BundleMsFeature::QueryInnerAbilityInfo(const uint8_t funcId, IpcIo *req, IpcIo *reply)
170 {
171 if ((req == nullptr) || (reply == nullptr)) {
172 return ERR_APPEXECFWK_OBJECT_NULL;
173 }
174 Want want;
175 if (memset_s(&want, sizeof(Want), 0, sizeof(Want)) != EOK) {
176 HILOG_ERROR(HILOG_MODULE_APP, "BundleMS feature memset want failed");
177 return ERR_APPEXECFWK_SYSTEM_INTERNAL_ERROR;
178 }
179 if (!DeserializeWant(&want, req)) {
180 HILOG_ERROR(HILOG_MODULE_APP, "BundleMS feature deserialize failed");
181 return ERR_APPEXECFWK_SYSTEM_INTERNAL_ERROR;
182 }
183 AbilityInfo abilityInfo;
184 if (memset_s(&abilityInfo, sizeof(AbilityInfo), 0, sizeof(AbilityInfo)) != EOK) {
185 ClearWant(&want);
186 HILOG_ERROR(HILOG_MODULE_APP, "BundleMS feature memset ability info failed");
187 return ERR_APPEXECFWK_SYSTEM_INTERNAL_ERROR;
188 }
189 uint8_t errorCode = QueryAbilityInfo(&want, &abilityInfo);
190 ClearWant(&want);
191 if (errorCode != OHOS_SUCCESS) {
192 ClearAbilityInfo(&abilityInfo);
193 return errorCode;
194 }
195 char *str = ConvertUtils::ConvertAbilityInfoToString(&abilityInfo);
196 if (str == nullptr) {
197 ClearAbilityInfo(&abilityInfo);
198 return ERR_APPEXECFWK_SERIALIZATION_FAILED;
199 }
200 #ifdef __LINUX__
201 if (strlen(str) > MAX_IPC_STRING_LENGTH) {
202 ClearAbilityInfo(&abilityInfo);
203 return ERR_APPEXECFWK_SERIALIZATION_FAILED;
204 }
205 #endif
206 IpcIoPushUint8(reply, static_cast<uint8_t>(OHOS_SUCCESS));
207 #ifdef __LINUX__
208 IpcIoPushString(reply, str);
209 #else
210 BuffPtr dataBuff = {.buffSz = strlen(str) + 1, .buff = str};
211 IpcIoPushDataBuffWithFree(reply, &dataBuff, InnerFreeDataBuff);
212 #endif
213 ClearAbilityInfo(&abilityInfo);
214 return OHOS_SUCCESS;
215 }
216
GetInnerBundleInfo(const uint8_t funcId,IpcIo * req,IpcIo * reply)217 uint8_t BundleMsFeature::GetInnerBundleInfo(const uint8_t funcId, IpcIo *req, IpcIo *reply)
218 {
219 if ((req == nullptr) || (reply == nullptr)) {
220 return ERR_APPEXECFWK_OBJECT_NULL;
221 }
222 size_t size = 0;
223 char *bundleName = reinterpret_cast<char *>(IpcIoPopString(req, &size));
224 if (bundleName == nullptr) {
225 return ERR_APPEXECFWK_DESERIALIZATION_FAILED;
226 }
227 BundleInfo bundleInfo;
228 if (memset_s(&bundleInfo, sizeof(BundleInfo), 0, sizeof(BundleInfo)) != EOK) {
229 return ERR_APPEXECFWK_SYSTEM_INTERNAL_ERROR;
230 }
231 uint8_t errorCode = GetBundleInfo(bundleName, IpcIoPopInt32(req), &bundleInfo);
232 if (errorCode != OHOS_SUCCESS) {
233 HILOG_ERROR(HILOG_MODULE_APP, "BundleMS GET_BUNDLE_INFO errorcode: %{public}d\n", errorCode);
234 return errorCode;
235 }
236 char *str = ConvertUtils::ConvertBundleInfoToString(&bundleInfo);
237 if (str == nullptr) {
238 return ERR_APPEXECFWK_SERIALIZATION_FAILED;
239 }
240 #ifdef __LINUX__
241 if (strlen(str) > MAX_IPC_STRING_LENGTH) {
242 return ERR_APPEXECFWK_SERIALIZATION_FAILED;
243 }
244 #endif
245 IpcIoPushUint8(reply, static_cast<uint8_t>(OHOS_SUCCESS));
246 #ifdef __LINUX__
247 IpcIoPushString(reply, str);
248 #else
249 BuffPtr dataBuff = {.buffSz = strlen(str) + 1, .buff = str};
250 IpcIoPushDataBuffWithFree(reply, &dataBuff, InnerFreeDataBuff);
251 #endif
252 return OHOS_SUCCESS;
253 }
254
GetInnerBundleSize(const uint8_t funcId,IpcIo * req,IpcIo * reply)255 uint8_t BundleMsFeature::GetInnerBundleSize(const uint8_t funcId, IpcIo *req, IpcIo *reply)
256 {
257 if ((req == nullptr) || (reply == nullptr)) {
258 return ERR_APPEXECFWK_OBJECT_NULL;
259 }
260 size_t size = 0;
261 uint32_t bundleSize = 0;
262 char *bundleName = reinterpret_cast<char *>(IpcIoPopString(req, &size));
263 if (bundleName == nullptr) {
264 IpcIoPushUint8(reply, static_cast<uint8_t>(OHOS_SUCCESS));
265 IpcIoPushUint32(reply, bundleSize);
266 return OHOS_SUCCESS;
267 }
268 bundleSize = GetBundleSize(bundleName);
269 IpcIoPushUint8(reply, static_cast<uint8_t>(OHOS_SUCCESS));
270 IpcIoPushUint32(reply, bundleSize);
271 return OHOS_SUCCESS;
272 }
273
HandleGetBundleInfos(const uint8_t funcId,IpcIo * req,IpcIo * reply)274 uint8_t BundleMsFeature::HandleGetBundleInfos(const uint8_t funcId, IpcIo *req, IpcIo *reply)
275 {
276 if ((req == nullptr) || (reply == nullptr)) {
277 return ERR_APPEXECFWK_OBJECT_NULL;
278 }
279 BundleInfo *bundleInfos = nullptr;
280 char *metaDataKey = nullptr;
281 int32_t lengthOfBundleInfo = 0;
282 uint8_t errorCode = 0;
283 size_t length = 0;
284 if (funcId == GET_BUNDLE_INFOS) {
285 errorCode = GetBundleInfos(IpcIoPopInt32(req), &bundleInfos, &lengthOfBundleInfo);
286 } else if (funcId == QUERY_KEEPALIVE_BUNDLE_INFOS) {
287 errorCode = QueryKeepAliveBundleInfos(&bundleInfos, &lengthOfBundleInfo);
288 } else if (funcId == GET_BUNDLE_INFOS_BY_METADATA) {
289 metaDataKey = reinterpret_cast<char *>(IpcIoPopString(req, &length));
290 if (metaDataKey == nullptr) {
291 return ERR_APPEXECFWK_DESERIALIZATION_FAILED;
292 }
293 errorCode = GetBundleInfosByMetaData(metaDataKey, &bundleInfos, &lengthOfBundleInfo);
294 } else {
295 return ERR_APPEXECFWK_COMMAND_ERROR;
296 }
297 if (errorCode != OHOS_SUCCESS) {
298 BundleInfoUtils::FreeBundleInfos(bundleInfos, lengthOfBundleInfo);
299 return errorCode;
300 }
301 char *strs = ConvertUtils::ConvertBundleInfosToString(&bundleInfos, lengthOfBundleInfo);
302 if (strs == nullptr) {
303 BundleInfoUtils::FreeBundleInfos(bundleInfos, lengthOfBundleInfo);
304 return ERR_APPEXECFWK_SERIALIZATION_FAILED;
305 }
306 #ifdef __LINUX__
307 if (strlen(strs) > MAX_IPC_STRING_LENGTH) {
308 BundleInfoUtils::FreeBundleInfos(bundleInfos, lengthOfBundleInfo);
309 return ERR_APPEXECFWK_SERIALIZATION_FAILED;
310 }
311 #endif
312 IpcIoPushUint8(reply, static_cast<uint8_t>(OHOS_SUCCESS));
313 IpcIoPushInt32(reply, lengthOfBundleInfo);
314 #ifdef __LINUX__
315 IpcIoPushString(reply, strs);
316 #else
317 BuffPtr dataBuff = {.buffSz = strlen(strs) + 1, .buff = strs};
318 IpcIoPushDataBuffWithFree(reply, &dataBuff, InnerFreeDataBuff);
319 #endif
320 BundleInfoUtils::FreeBundleInfos(bundleInfos, lengthOfBundleInfo);
321 return OHOS_SUCCESS;
322 }
323
GetInnerBundleNameForUid(const uint8_t funcId,IpcIo * req,IpcIo * reply)324 uint8_t BundleMsFeature::GetInnerBundleNameForUid(const uint8_t funcId, IpcIo *req, IpcIo *reply)
325 {
326 if ((req == nullptr) || (reply == nullptr)) {
327 return ERR_APPEXECFWK_OBJECT_NULL;
328 }
329 char *bundleName = nullptr;
330 uint8_t errorCode = GetBundleNameForUid(IpcIoPopInt32(req), &bundleName);
331 if (errorCode != OHOS_SUCCESS) {
332 AdapterFree(bundleName);
333 return errorCode;
334 }
335 IpcIoPushUint8(reply, static_cast<uint8_t>(OHOS_SUCCESS));
336 IpcIoPushString(reply, bundleName);
337 AdapterFree(bundleName);
338 return errorCode;
339 }
340
ChangeInnerCallbackServiceId(const uint8_t funcId,IpcIo * req,IpcIo * reply)341 uint8_t BundleMsFeature::ChangeInnerCallbackServiceId(const uint8_t funcId, IpcIo *req, IpcIo *reply)
342 {
343 if ((req == nullptr) || (reply == nullptr)) {
344 return ERR_APPEXECFWK_OBJECT_NULL;
345 }
346 bool flag = IpcIoPopBool(req);
347 SvcIdentity *svc = IpcIoPopSvc(req);
348 if (svc == nullptr) {
349 return ERR_APPEXECFWK_DESERIALIZATION_FAILED;
350 }
351
352 auto svcIdentity = reinterpret_cast<SvcIdentity *>(AdapterMalloc(sizeof(SvcIdentity)));
353 if (svcIdentity == nullptr) {
354 #ifdef __LINUX__
355 AdapterFree(svc);
356 svc = nullptr;
357 #endif
358 return ERR_APPEXECFWK_SYSTEM_INTERNAL_ERROR;
359 }
360 *svcIdentity = *svc;
361 #ifdef __LINUX__
362 BinderAcquire(svc->ipcContext, svc->handle);
363 AdapterFree(svc);
364 svc = nullptr;
365 #endif
366 Request request = {
367 .msgId = BUNDLE_CHANGE_CALLBACK,
368 .len = static_cast<int16>(sizeof(SvcIdentity)),
369 .data = reinterpret_cast<void *>(svcIdentity),
370 .msgValue = static_cast<uint32>(flag ? 1 : 0)
371 };
372 int32 propRet = SAMGR_SendRequest(&(GetInstance()->identity_), &request, nullptr);
373 if (propRet != OHOS_SUCCESS) {
374 AdapterFree(svcIdentity);
375 return ERR_APPEXECFWK_UNINSTALL_FAILED_SEND_REQUEST_ERROR;
376 }
377 return ERR_OK;
378 }
379
Invoke(IServerProxy * iProxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)380 int32 BundleMsFeature::Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
381 {
382 if (req == nullptr) {
383 return ERR_APPEXECFWK_OBJECT_NULL;
384 }
385 IpcIoPushUint8(reply, static_cast<uint8_t>(funcId));
386 uint8_t ret = OHOS_SUCCESS;
387 if (funcId >= GET_BUNDLE_INFOS && funcId <= GET_BUNDLE_INFOS_BY_METADATA) {
388 ret = BundleMsInvokeFuc[GET_BUNDLE_INFOS](funcId, req, reply);
389 } else if (funcId >= QUERY_ABILITY_INFO && funcId <= GET_BUNDLENAME_FOR_UID) {
390 ret = BundleMsInvokeFuc[funcId](funcId, req, reply);
391 } else if (funcId >= CHECK_SYS_CAP && funcId <= GET_SYS_CAP) {
392 ret = BundleMsInvokeFuc[funcId](funcId, req, reply);
393 } else {
394 ret = ERR_APPEXECFWK_COMMAND_ERROR;
395 }
396
397 if (ret != OHOS_SUCCESS) {
398 IpcIoPushUint8(reply, ret);
399 }
400 return ret;
401 }
402
BundleServiceTaskInit()403 bool BundleMsFeature::BundleServiceTaskInit()
404 {
405 Request request = {
406 .msgId = BUNDLE_SERVICE_INITED,
407 .len = 0,
408 .data = nullptr,
409 .msgValue = 0
410 };
411 int32 propRet = SAMGR_SendRequest(&identity_, &request, nullptr);
412 if (propRet != OHOS_SUCCESS) {
413 return false;
414 }
415 return true;
416 }
417
QueryAbilityInfo(const Want * want,AbilityInfo * abilityInfo)418 uint8_t BundleMsFeature::QueryAbilityInfo(const Want *want, AbilityInfo *abilityInfo)
419 {
420 if ((abilityInfo == nullptr) || (want == nullptr) || (want->element == nullptr)) {
421 return ERR_APPEXECFWK_OBJECT_NULL;
422 }
423
424 BundleInfo *bundleInfo = OHOS::ManagerService::GetInstance().QueryBundleInfo(want->element->bundleName);
425 if (bundleInfo == nullptr) {
426 return ERR_APPEXECFWK_QUERY_NO_INFOS;
427 }
428
429 if ((bundleInfo->abilityInfos == nullptr) || (bundleInfo->numOfAbility == 0)) {
430 return ERR_APPEXECFWK_QUERY_NO_INFOS;
431 }
432
433 for (int32_t i = 0; i < bundleInfo->numOfAbility; ++i) {
434 if ((want->element->abilityName != nullptr) && ((bundleInfo->abilityInfos)[i].name != nullptr) &&
435 (strcmp(want->element->abilityName, (bundleInfo->abilityInfos)[i].name) == 0)) {
436 OHOS::AbilityInfoUtils::CopyAbilityInfo(abilityInfo, bundleInfo->abilityInfos[i]);
437 return OHOS_SUCCESS;
438 }
439 }
440 return ERR_APPEXECFWK_QUERY_NO_INFOS;
441 }
442
GetBundleInfo(const char * bundleName,int32_t flags,BundleInfo * bundleInfo)443 uint8_t BundleMsFeature::GetBundleInfo(const char *bundleName, int32_t flags, BundleInfo *bundleInfo)
444 {
445 return OHOS::ManagerService::GetInstance().GetBundleInfo(bundleName, flags, *bundleInfo);
446 }
447
GetBundleInfos(const int flags,BundleInfo ** bundleInfos,int32_t * len)448 uint8_t BundleMsFeature::GetBundleInfos(const int flags, BundleInfo **bundleInfos, int32_t *len)
449 {
450 return OHOS::ManagerService::GetInstance().GetBundleInfos(flags, bundleInfos, len);
451 }
452
GetBundleSize(const char * bundleName)453 uint32_t BundleMsFeature::GetBundleSize(const char *bundleName)
454 {
455 return OHOS::ManagerService::GetInstance().GetBundleSize(bundleName);
456 }
457
QueryKeepAliveBundleInfos(BundleInfo ** bundleInfos,int32_t * len)458 uint8_t BundleMsFeature::QueryKeepAliveBundleInfos(BundleInfo **bundleInfos, int32_t *len)
459 {
460 if ((bundleInfos == nullptr) || (len == nullptr)) {
461 return ERR_APPEXECFWK_OBJECT_NULL;
462 }
463 BundleInfo *allBundleInfos = nullptr;
464 int32_t numOfAllBundleInfos = 0;
465 uint8_t errorCode = GetBundleInfos(1, &allBundleInfos, &numOfAllBundleInfos);
466 if (errorCode != OHOS_SUCCESS) {
467 return errorCode;
468 }
469 *len = 0;
470 for (int32_t i = 0; i < numOfAllBundleInfos; i++) {
471 if (allBundleInfos[i].isKeepAlive && allBundleInfos[i].isSystemApp) {
472 (*len)++;
473 }
474 }
475
476 if (*len == 0) {
477 BundleInfoUtils::FreeBundleInfos(allBundleInfos, numOfAllBundleInfos);
478 return ERR_APPEXECFWK_QUERY_NO_INFOS;
479 }
480 *bundleInfos = reinterpret_cast<BundleInfo *>(AdapterMalloc(sizeof(BundleInfo) * (*len)));
481 if (*bundleInfos == nullptr || memset_s(*bundleInfos, sizeof(BundleInfo) * (*len), 0,
482 sizeof(BundleInfo) * (*len)) != EOK) {
483 BundleInfoUtils::FreeBundleInfos(allBundleInfos, numOfAllBundleInfos);
484 return ERR_APPEXECFWK_QUERY_INFOS_INIT_ERROR;
485 }
486
487 int32_t count = 0;
488 for (int32_t i = 0; i < numOfAllBundleInfos && count < *len; i++) {
489 if (allBundleInfos[i].isKeepAlive && allBundleInfos[i].isSystemApp) {
490 OHOS::BundleInfoUtils::CopyBundleInfo(1, *bundleInfos + count, allBundleInfos[i]);
491 count++;
492 }
493 }
494 BundleInfoUtils::FreeBundleInfos(allBundleInfos, numOfAllBundleInfos);
495 return OHOS_SUCCESS;
496 }
497
CheckBundleInfoWithSpecialMetaData(const BundleInfo & bundleInfo,const char * metaDataKey)498 static bool CheckBundleInfoWithSpecialMetaData(const BundleInfo &bundleInfo, const char *metaDataKey)
499 {
500 if (metaDataKey == nullptr) {
501 return false;
502 }
503
504 for (int32_t i = 0; i < bundleInfo.numOfModule; i++) {
505 for (int32_t j = 0; j < METADATA_SIZE; j++) {
506 if ((bundleInfo.moduleInfos[i].metaData[j] != nullptr) &&
507 (bundleInfo.moduleInfos[i].metaData[j]->name != nullptr) &&
508 strcmp(metaDataKey, bundleInfo.moduleInfos[i].metaData[j]->name) == 0) {
509 return true;
510 }
511 }
512 }
513 return false;
514 }
515
GetBundleInfosByMetaData(const char * metaDataKey,BundleInfo ** bundleInfos,int32_t * len)516 uint8_t BundleMsFeature::GetBundleInfosByMetaData(const char *metaDataKey, BundleInfo **bundleInfos, int32_t *len)
517 {
518 if (metaDataKey == nullptr || bundleInfos == nullptr) {
519 return ERR_APPEXECFWK_OBJECT_NULL;
520 }
521
522 BundleInfo *allBundleInfos = nullptr;
523 int32_t numOfAllBundleInfos = 0;
524 uint8_t errorCode = GetBundleInfos(1, &allBundleInfos, &numOfAllBundleInfos);
525 if (errorCode != OHOS_SUCCESS) {
526 return errorCode;
527 }
528 *len = 0;
529 for (int32_t i = 0; i < numOfAllBundleInfos; i++) {
530 if (CheckBundleInfoWithSpecialMetaData(allBundleInfos[i], metaDataKey)) {
531 (*len)++;
532 }
533 }
534
535 if (*len == 0) {
536 BundleInfoUtils::FreeBundleInfos(allBundleInfos, numOfAllBundleInfos);
537 return ERR_APPEXECFWK_QUERY_NO_INFOS;
538 }
539
540 *bundleInfos = reinterpret_cast<BundleInfo *>(AdapterMalloc(sizeof(BundleInfo) * (*len)));
541 if (*bundleInfos == nullptr || memset_s(*bundleInfos, sizeof(BundleInfo) * (*len), 0,
542 sizeof(BundleInfo) * (*len)) != EOK) {
543 BundleInfoUtils::FreeBundleInfos(allBundleInfos, numOfAllBundleInfos);
544 return ERR_APPEXECFWK_QUERY_INFOS_INIT_ERROR;
545 }
546
547 int32_t count = 0;
548 for (int32_t i = 0; i < numOfAllBundleInfos && count < *len; i++) {
549 if (CheckBundleInfoWithSpecialMetaData(allBundleInfos[i], metaDataKey)) {
550 BundleInfoUtils::CopyBundleInfo(1, *bundleInfos + count, allBundleInfos[i]);
551 count++;
552 }
553 }
554 BundleInfoUtils::FreeBundleInfos(allBundleInfos, numOfAllBundleInfos);
555 return OHOS_SUCCESS;
556 }
557
GetBundleNameForUid(int32_t uid,char ** bundleName)558 uint8_t BundleMsFeature::GetBundleNameForUid(int32_t uid, char **bundleName)
559 {
560 if (bundleName == nullptr) {
561 return ERR_APPEXECFWK_OBJECT_NULL;
562 }
563 BundleInfo *infos = nullptr;
564 int32_t numOfInfos = 0;
565 uint8_t errorCode = GetBundleInfos(0, &infos, &numOfInfos);
566 if (errorCode != OHOS_SUCCESS) {
567 BundleInfoUtils::FreeBundleInfos(infos, numOfInfos);
568 return errorCode;
569 }
570
571 for (int i = 0; i < numOfInfos; ++i) {
572 if (infos[i].uid == uid) {
573 *bundleName = Utils::Strdup(infos[i].bundleName);
574 break;
575 }
576 }
577 BundleInfoUtils::FreeBundleInfos(infos, numOfInfos);
578 if (*bundleName == nullptr) {
579 return ERR_APPEXECFWK_NO_BUNDLENAME_FOR_UID;
580 }
581 return OHOS_SUCCESS;
582 }
583 } // namespace OHOS
584