1 /*
2  * Copyright (c) 2020-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 "gt_bundle_manager_service.h"
17 #include "aafwk_event_error_id.h"
18 #include "aafwk_event_error_code.h"
19 #include "ability_info_utils.h"
20 #include "ability_message_id.h"
21 #include "appexecfwk_errors.h"
22 #include "bundle_common.h"
23 #include "bundle_message_id.h"
24 #include "bundle_util.h"
25 #include "bundlems_log.h"
26 #include "cmsis_os2.h"
27 #include "cstdio"
28 #include "dirent.h"
29 #include "fcntl.h"
30 #include "gt_bundle_extractor.h"
31 #include "gt_bundle_parser.h"
32 #include "gt_extractor_util.h"
33 #include "jerryscript_adapter.h"
34 #include "los_tick.h"
35 #include "sys/stat.h"
36 #include "unistd.h"
37 #include "utils.h"
38 #include "want.h"
39 
40 using namespace OHOS::ACELite;
41 
42 namespace OHOS {
43 const uint8_t OPERATION_DOING = 200;
44 const uint8_t BMS_INSTALLATION_START = 101;
45 const uint8_t BMS_UNINSTALLATION_START = 104;
46 const uint8_t BMS_INSTALLATION_COMPLETED = 100;
47 
GtManagerService()48 GtManagerService::GtManagerService()
49 {
50     installer_ = new (std::nothrow) GtBundleInstaller();
51     bundleResList_ = new (std::nothrow) List<BundleRes *>();
52     bundleMap_ = BundleMap::GetInstance();
53     bundleInstallMsg_ = nullptr;
54     jsEngineVer_ = nullptr;
55     installedThirdBundleNum_ = 0;
56     preAppList_ = nullptr;
57     updateFlag_ = false;
58     oldVersionCode_ = -1;
59     listenList_ = new (std::nothrow) List<InstallerCallback>();
60 }
61 
~GtManagerService()62 GtManagerService::~GtManagerService()
63 {
64     delete installer_;
65     installer_ = nullptr;
66     delete bundleResList_;
67     bundleResList_ = nullptr;
68     delete listenList_;
69     listenList_ = nullptr;
70 }
71 
Install(const char * hapPath,const InstallParam * installParam,InstallerCallback installerCallback)72 bool GtManagerService::Install(const char *hapPath, const InstallParam *installParam,
73     InstallerCallback installerCallback)
74 {
75     if (installer_ == nullptr) {
76         installer_ = new (std::nothrow) GtBundleInstaller();
77     }
78     if (hapPath == nullptr) {
79         return false;
80     }
81     if (installerCallback == nullptr) {
82         return false;
83     }
84     char *path = reinterpret_cast<char *>(AdapterMalloc(strlen(hapPath) + 1));
85     if (path == nullptr) {
86         return false;
87     }
88     if (strncpy_s(path, strlen(hapPath) + 1, hapPath, strlen(hapPath)) != EOK) {
89         AdapterFree(path);
90         return false;
91     }
92 
93     // delete resource temp dir first
94     (void) BundleUtil::RemoveDir(TMP_RESOURCE_DIR);
95     // create new bundleInstallMsg
96     bundleInstallMsg_ = reinterpret_cast<BundleInstallMsg *>(AdapterMalloc(sizeof(BundleInstallMsg)));
97     if (bundleInstallMsg_ == nullptr) {
98         AdapterFree(path);
99         return false;
100     }
101     if (memset_s(bundleInstallMsg_, sizeof(BundleInstallMsg), 0, sizeof(BundleInstallMsg)) != EOK) {
102         AdapterFree(bundleInstallMsg_);
103         AdapterFree(path);
104         return false;
105     }
106     // set bundleName、label、smallIconPath、bigIconPath in bundleInstallMsg_
107     uint8_t ret = GtBundleExtractor::ExtractInstallMsg(path,
108         &(bundleInstallMsg_->bundleName),
109         &(bundleInstallMsg_->label),
110         &(bundleInstallMsg_->smallIconPath),
111         &(bundleInstallMsg_->bigIconPath),
112         bundleInstallMsg_->actionService);
113     if (ret != 0) {
114         HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] Install extract install msg failed, ret is %{public}d", ret);
115         if (bundleInstallMsg_->actionService) {
116             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] install fail and actionService is on.");
117             (void)ReportHceInstallCallback(ret, BUNDLE_INSTALL_FAIL, BMS_INSTALLATION_COMPLETED);
118         }
119         char *name = strrchr(path, '/');
120         bundleInstallMsg_->bundleName = Utils::Strdup(name + 1);
121         (void) ReportInstallCallback(ret, BUNDLE_INSTALL_FAIL, BMS_INSTALLATION_COMPLETED, installerCallback);
122         ClearSystemBundleInstallMsg();
123         (void) BundleUtil::RemoveDir(TMP_RESOURCE_DIR);
124         AdapterFree(path);
125         return false;
126     }
127 
128     updateFlag_ = false;
129     oldVersionCode_ = -1;
130     BundleInfo *installedInfo = bundleMap_->Get(bundleInstallMsg_->bundleName);
131     if (installedInfo != nullptr) {
132         updateFlag_ = true;
133         oldVersionCode_ = installedInfo->versionCode;
134         HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] App is in the updated state");
135     }
136 
137     SetCurrentBundle(bundleInstallMsg_->bundleName);
138     (void) ReportInstallCallback(OPERATION_DOING, 0, BMS_INSTALLATION_START, installerCallback);
139 #ifdef _MINI_BMS_PERMISSION_
140     DisableServiceWdg();
141     RefreshAllServiceTimeStamp();
142 #endif
143     ret = installer_->Install(path, installerCallback);
144 #ifdef _MINI_BMS_PERMISSION_
145     EnableServiceWdg();
146     RefreshAllServiceTimeStamp();
147 #endif
148     HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] Install ret is %d", ret);
149     if (ret == 0) {
150         (void)ReportInstallCallback(ret, BUNDLE_INSTALL_OK, BMS_INSTALLATION_COMPLETED, installerCallback);
151         if (bundleInstallMsg_->actionService) {
152             HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] install success and actionService is on.");
153             (void)ReportHceInstallCallback(ret, BUNDLE_INSTALL_OK, BMS_INSTALLATION_COMPLETED);
154         }
155     } else {
156         (void)ReportInstallCallback(ret, BUNDLE_INSTALL_FAIL, BMS_INSTALLATION_COMPLETED, installerCallback);
157         if (bundleInstallMsg_->actionService) {
158             HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] install fail and actionService is on.");
159             (void)ReportHceInstallCallback(ret, BUNDLE_INSTALL_FAIL, BMS_INSTALLATION_COMPLETED);
160         }
161     }
162     SetCurrentBundle(nullptr);
163     ClearSystemBundleInstallMsg();
164     (void) BundleUtil::RemoveDir(TMP_RESOURCE_DIR);
165     AdapterFree(path);
166     return true;
167 }
168 
Uninstall(const char * bundleName,const InstallParam * installParam,InstallerCallback installerCallback)169 bool GtManagerService::Uninstall(const char *bundleName, const InstallParam *installParam,
170     InstallerCallback installerCallback)
171 {
172     if (installer_ == nullptr) {
173         installer_ = new (std::nothrow) GtBundleInstaller();
174     }
175     if (bundleName == nullptr) {
176         HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] Parsed bundleName to be uninstalled is null");
177         return false;
178     }
179     if (installerCallback == nullptr) {
180         return false;
181     }
182     char *innerBundleName = reinterpret_cast<char *>(AdapterMalloc(strlen(bundleName) + 1));
183     if (innerBundleName == nullptr) {
184         return false;
185     }
186     if (strncpy_s(innerBundleName, strlen(bundleName) + 1, bundleName, strlen(bundleName)) != EOK) {
187         AdapterFree(innerBundleName);
188         return false;
189     }
190     SetCurrentBundle(innerBundleName);
191     // obtain the skill.action whether contain HOST_APDU_SERVICE
192     uint8_t actionService = GtBundleExtractor::ParseBundleInfoGetActionService(innerBundleName);
193     (void) ReportUninstallCallback(
194         OPERATION_DOING, BUNDLE_UNINSTALL_DOING, innerBundleName, BMS_UNINSTALLATION_START, installerCallback);
195 #ifdef _MINI_BMS_PERMISSION_
196     DisableServiceWdg();
197     RefreshAllServiceTimeStamp();
198 #endif
199     uint8_t ret = installer_->Uninstall(innerBundleName);
200 #ifdef _MINI_BMS_PERMISSION_
201     EnableServiceWdg();
202     RefreshAllServiceTimeStamp();
203 #endif
204     HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] Uninstall ret is %d", ret);
205     if (ret == 0) {
206         (void) ReportUninstallCallback(
207             ret, BUNDLE_UNINSTALL_OK, innerBundleName, BMS_INSTALLATION_COMPLETED, installerCallback);
208         if (actionService != 0) {
209             HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] uninstall success and actionService is on.");
210             (void) ReportHceUninstallCallback(ret, BUNDLE_UNINSTALL_OK, innerBundleName, BMS_INSTALLATION_COMPLETED);
211         }
212     } else {
213         (void) ReportUninstallCallback(
214             ret, BUNDLE_UNINSTALL_FAIL, innerBundleName, BMS_INSTALLATION_COMPLETED, installerCallback);
215         if (actionService != 0) {
216             HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] uninstall fail and actionService is on.");
217             (void) ReportHceUninstallCallback(ret, BUNDLE_UNINSTALL_FAIL, innerBundleName, BMS_INSTALLATION_COMPLETED);
218         }
219     }
220 
221     SetCurrentBundle(nullptr);
222     AdapterFree(innerBundleName);
223     return true;
224 }
225 
GetInstallState(const char * bundleName,InstallState * installState,uint8_t * installProcess)226 bool GtManagerService::GetInstallState(const char *bundleName, InstallState *installState, uint8_t *installProcess)
227 {
228     if (bundleName == nullptr) {
229         return false;
230     }
231     BundleInfo *installedInfo = bundleMap_->Get(bundleName);
232     if (installedInfo != nullptr) {
233         bool isUpdateSuccess = updateFlag_ && oldVersionCode_ < installedInfo->versionCode;
234         if (!updateFlag_ || isUpdateSuccess) {
235             *installState = BUNDLE_INSTALL_OK;
236             *installProcess = BMS_INSTALLATION_COMPLETED;
237             return true;
238         }
239     }
240     if (bundleInstallMsg_ == nullptr || bundleInstallMsg_->bundleName == nullptr) {
241         *installState = BUNDLE_INSTALL_FAIL;
242         *installProcess = 0;
243         return true;
244     }
245     if (strcmp(bundleName, bundleInstallMsg_->bundleName) == 0) {
246         *installState = bundleInstallMsg_->installState;
247         *installProcess = bundleInstallMsg_->installProcess;
248         return true;
249     }
250     *installState = BUNDLE_INSTALL_FAIL;
251     *installProcess = 0;
252     return true;
253 }
254 
GetBundleSize(const char * bundleName)255 uint32_t GtManagerService::GetBundleSize(const char *bundleName)
256 {
257     if (bundleName == nullptr) {
258         return 0;
259     }
260     BundleInfo *installedInfo = bundleMap_->Get(bundleName);
261     if (installedInfo == nullptr) {
262         HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] failed to get bundle size because the bundle does not exist!");
263         return 0;
264     }
265     char *codePath = installedInfo->codePath;
266     uint32_t codeBundleSize = BundleUtil::GetFileFolderSize(codePath);
267     if (codeBundleSize == 0) {
268         HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] failed to get code bundle size!");
269         return 0;
270     }
271     char *dataPath = installedInfo->dataPath;
272     uint32_t dataBundleSize = BundleUtil::GetFileFolderSize(dataPath);
273     return codeBundleSize + dataBundleSize;
274 }
275 
QueryAbilityInfo(const Want * want,AbilityInfo * abilityInfo)276 uint8_t GtManagerService::QueryAbilityInfo(const Want *want, AbilityInfo *abilityInfo)
277 {
278     if (want->element == nullptr) {
279         return 0;
280     }
281     const char *bundleName = want->element->bundleName;
282     BundleInfo *bundleInfo = OHOS::GtManagerService::GetInstance().QueryBundleInfo(bundleName);
283     if (bundleInfo == nullptr) {
284         return 0;
285     }
286     AbilityInfo *ability = bundleInfo->abilityInfo;
287     if (ability == nullptr) {
288         return 0;
289     }
290     OHOS::AbilityInfoUtils::SetAbilityInfoBundleName(abilityInfo, bundleName);
291     OHOS::AbilityInfoUtils::SetAbilityInfoSrcPath(abilityInfo, ability->srcPath);
292     return 1;
293 }
294 
QueryAbilityInfos(const Want * want,AbilityInfo ** abilityInfo,int32_t * len)295 uint8_t GtManagerService::QueryAbilityInfos(const Want *want, AbilityInfo **abilityInfo, int32_t *len)
296 {
297     if (want == nullptr || abilityInfo == nullptr || want->actions == nullptr || bundleMap_ == nullptr) {
298         return 0;
299     }
300     int32_t abilityInfoLen = 0;
301     List<BundleInfo *> *bundleInfos_ = new (std::nothrow) List<BundleInfo *>();
302     bundleMap_->GetBundleInfosInner(*bundleInfos_);
303 
304     for (auto node = bundleInfos_->Begin(); node != bundleInfos_->End(); node = node->next_) {
305         BundleInfo *info = node->value_;
306         if (info == nullptr || info->abilityInfo == nullptr) {
307             continue;
308         }
309         // find bundleInfo->abilityInfo->skills[i]->actions whether contain target string
310         if (MatchSkills(want, info->abilityInfo->skills)) {
311             abilityInfoLen++;
312         }
313     }
314     // Request memory
315     AbilityInfo *infos = reinterpret_cast<AbilityInfo *>(AdapterMalloc(sizeof(AbilityInfo) * abilityInfoLen));
316     if (infos == nullptr ||
317         memset_s(infos, sizeof(AbilityInfo) * abilityInfoLen, 0, sizeof(AbilityInfo) * abilityInfoLen) != EOK) {
318         AdapterFree(infos);
319         return ERR_APPEXECFWK_QUERY_INFOS_INIT_ERROR;
320     }
321     *abilityInfo = infos;
322     // copy the abilityInfo from bundleInfos
323     for (auto node = bundleInfos_->Begin(); node != bundleInfos_->End(); node = node->next_) {
324         BundleInfo *info = node->value_;
325         if (info == nullptr || info->abilityInfo == nullptr) {
326             continue;
327         }
328         // find bundleInfo->abilityInfo->skills[i]->actions whether contain HOST_APDU_SERVICE
329         if (MatchSkills(want, info->abilityInfo->skills)) {
330             OHOS::AbilityInfoUtils::CopyAbilityInfo(infos++, *(info->abilityInfo));
331         }
332     }
333     *len = abilityInfoLen;
334     return 1;
335 }
MatchSkills(const Want * want,Skill * const skills[])336 bool GtManagerService::MatchSkills(const Want *want, Skill *const skills[])
337 {
338     if (skills == nullptr || want == nullptr) {
339         return false;
340     }
341     for (int32_t i = 0; i < SKILL_SIZE; i++) {
342         if (skills[i] == nullptr) {
343             return false;
344         }
345         if (isMatchActions(want->actions, skills[i]->actions) || isMatchEntities(want->entities, skills[i]->entities)) {
346             return true;
347         }
348     }
349     return false;
350 }
isMatchActions(const char * actions,char * const skillActions[])351 bool GtManagerService::isMatchActions(const char *actions, char *const skillActions[])
352 {
353     if (actions == nullptr || skillActions == nullptr) {
354         return false;
355     }
356     for (int32_t i = 0; i < MAX_SKILL_ITEM; i++) {
357         if (skillActions[i] == nullptr) {
358             return false;
359         }
360         if (isMatch(actions, skillActions[i])) {
361             return true;
362         }
363     }
364     return false;
365 }
isMatchEntities(const char * entities,char * const skillEntities[])366 bool GtManagerService::isMatchEntities(const char *entities, char *const skillEntities[])
367 {
368     if (entities == nullptr) {
369         return true;
370     }
371     for (int32_t i = 0; i < MAX_SKILL_ITEM; i++) {
372         if (skillEntities[i] == nullptr) {
373             return false;
374         }
375         if (isMatch(entities, skillEntities[i])) {
376             return true;
377         }
378     }
379     return false;
380 }
isMatch(const char * dst,const char * src)381 bool GtManagerService::isMatch(const char *dst, const char *src)
382 {
383     // compare whether is ohos.nfc.cardemulation.action.HOST_APDU_SERVICE
384     return strcmp(dst, src) == 0;
385 }
386 
GetBundleInfo(const char * bundleName,int32_t flags,BundleInfo & bundleInfo)387 uint8_t GtManagerService::GetBundleInfo(const char *bundleName, int32_t flags, BundleInfo &bundleInfo)
388 {
389     if (bundleMap_ == nullptr) {
390         return ERR_APPEXECFWK_OBJECT_NULL;
391     }
392     return bundleMap_->GetBundleInfo(bundleName, flags, bundleInfo);
393 }
394 
GetBundleInfos(const int flags,BundleInfo ** bundleInfos,int32_t * len)395 uint8_t GtManagerService::GetBundleInfos(const int flags, BundleInfo **bundleInfos, int32_t *len)
396 {
397     if (bundleMap_ == nullptr) {
398         return ERR_APPEXECFWK_OBJECT_NULL;
399     }
400     return bundleMap_->GetBundleInfos(flags, bundleInfos, len);
401 }
402 
GetBundleInfosNoReplication(const int flags,BundleInfo ** bundleInfos,int32_t * len)403 uint8_t GtManagerService::GetBundleInfosNoReplication(const int flags, BundleInfo **bundleInfos, int32_t *len)
404 {
405     if (bundleMap_ == nullptr) {
406         return ERR_APPEXECFWK_OBJECT_NULL;
407     }
408     return bundleMap_->GetBundleInfosNoReplication(flags, bundleInfos, len);
409 }
410 
RegisterInstallerCallback(InstallerCallback installerCallback)411 bool GtManagerService::RegisterInstallerCallback(InstallerCallback installerCallback)
412 {
413     if (installerCallback == nullptr) {
414         return false;
415     }
416 #ifdef BC_TRANS_ENABLE
417     ScanPackages();
418 #endif
419     InstallPreBundle(systemPathList_, installerCallback);
420     return true;
421 }
422 
InstallPreBundle(List<ToBeInstalledApp * > systemPathList,InstallerCallback installerCallback)423 void GtManagerService::InstallPreBundle(List<ToBeInstalledApp *> systemPathList, InstallerCallback installerCallback)
424 {
425     if (!BundleUtil::IsDir(JSON_PATH_NO_SLASH_END)) {
426         BundleUtil::MkDirs(JSON_PATH_NO_SLASH_END);
427         InstallAllSystemBundle(installerCallback);
428         RemoveSystemAppPathList(&systemPathList);
429         return;
430     }
431     // get third system bundle uninstall record
432     cJSON *uninstallRecord = BundleUtil::GetJsonStream(UNINSTALL_THIRD_SYSTEM_BUNDLE_JSON);
433     if (uninstallRecord == nullptr) {
434         HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] InstallPreBundle uninstallRecord is nullptr!");
435         (void) unlink(UNINSTALL_THIRD_SYSTEM_BUNDLE_JSON);
436     }
437 
438     // scan system apps and third system apps
439 #ifdef _MINI_BMS_PERMISSION_
440     DisableServiceWdg();
441     RefreshAllServiceTimeStamp();
442 #endif
443     ScanSystemApp(uninstallRecord, &systemPathList_);
444     if (uninstallRecord != nullptr) {
445         cJSON_Delete(uninstallRecord);
446     }
447 
448     // scan third apps
449     ScanThirdApp(INSTALL_PATH, &systemPathList_);
450 #ifdef _MINI_BMS_PERMISSION_
451     EnableServiceWdg();
452     RefreshAllServiceTimeStamp();
453 #endif
454     for (auto node = systemPathList.Begin(); node != systemPathList.End(); node = node->next_) {
455         ToBeInstalledApp *toBeInstalledApp = node->value_;
456         if (!BundleUtil::IsFile(toBeInstalledApp->path) ||
457             !BundleUtil::EndWith(toBeInstalledApp->path, INSTALL_FILE_SUFFIX)) {
458             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] Pre install file path is invalid");
459             continue;
460         }
461         if (toBeInstalledApp->isUpdated) {
462             (void) ReloadBundleInfo(toBeInstalledApp->installedPath, toBeInstalledApp->appId,
463                 toBeInstalledApp->isSystemApp);
464         }
465         (void) Install(toBeInstalledApp->path, nullptr, installerCallback);
466         RefreshAllServiceTimeStamp();
467     }
468     RemoveSystemAppPathList(&systemPathList);
469 }
470 
InstallAllSystemBundle(InstallerCallback installerCallback)471 void GtManagerService::InstallAllSystemBundle(InstallerCallback installerCallback)
472 {
473     PreAppList *list = preAppList_;
474     if (list == nullptr) {
475         HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] InstallAllSystemBundle InitAllAppInfo fail, list is nullptr");
476         return;
477     }
478 
479     PreAppList *currentNode = nullptr;
480     PreAppList *nextNode = nullptr;
481     LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(currentNode, nextNode, &list->appDoubleList, PreAppList, appDoubleList) {
482         if (currentNode == nullptr) {
483             return;
484         }
485         if ((strcmp(((PreAppList *)currentNode)->filePath, ".") == 0) ||
486             (strcmp(((PreAppList *)currentNode)->filePath, "..") == 0)) {
487             continue;
488         }
489 
490         if (!BundleUtil::IsFile(((PreAppList *)currentNode)->filePath) ||
491             !BundleUtil::EndWith(((PreAppList *)currentNode)->filePath, INSTALL_FILE_SUFFIX)) {
492             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] Install all system bundle file path is invalid");
493             return;
494         }
495         (void) Install(((PreAppList *)currentNode)->filePath, nullptr, installerCallback);
496     }
497     GtManagerService::FreePreAppInfo(list);
498 }
499 
ClearSystemBundleInstallMsg()500 void GtManagerService::ClearSystemBundleInstallMsg()
501 {
502     if (bundleInstallMsg_ == nullptr) {
503         return;
504     }
505 
506     if (bundleInstallMsg_->bundleName != nullptr) {
507         AdapterFree(bundleInstallMsg_->bundleName);
508         bundleInstallMsg_->bundleName = nullptr;
509     }
510 
511     if (bundleInstallMsg_->label != nullptr) {
512         AdapterFree(bundleInstallMsg_->label);
513         bundleInstallMsg_->label = nullptr;
514     }
515 
516     if (bundleInstallMsg_->smallIconPath != nullptr) {
517         AdapterFree(bundleInstallMsg_->smallIconPath);
518         bundleInstallMsg_->smallIconPath = nullptr;
519     }
520 
521     if (bundleInstallMsg_->bigIconPath != nullptr) {
522         AdapterFree(bundleInstallMsg_->bigIconPath);
523         bundleInstallMsg_->bigIconPath = nullptr;
524     }
525 
526     AdapterFree(bundleInstallMsg_);
527     bundleInstallMsg_ = nullptr;
528 }
529 
ScanPackages()530 void GtManagerService::ScanPackages()
531 {
532 #ifdef BC_TRANS_ENABLE
533     JerryBmsPsRamMemInit();
534     bms_task_context_init();
535     jsEngineVer_ = get_jerry_version_no();
536     if (jsEngineVer_ == nullptr) {
537         HILOG_WARN(HILOG_MODULE_AAFWK, "[BMS] get jsEngine version fail when restart!");
538     }
539 #endif
540     HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] get jsEngine version success!");
541 }
542 
RemoveSystemAppPathList(List<ToBeInstalledApp * > * systemPathList)543 void GtManagerService::RemoveSystemAppPathList(List<ToBeInstalledApp *> *systemPathList)
544 {
545     if (systemPathList == nullptr) {
546         return;
547     }
548 
549     for (auto node = systemPathList->Begin(); node != systemPathList->End(); node = node->next_) {
550         ToBeInstalledApp *toBeInstalledApp = node->value_;
551         if (toBeInstalledApp != nullptr) {
552             AdapterFree(toBeInstalledApp->installedPath);
553             AdapterFree(toBeInstalledApp->path);
554             AdapterFree(toBeInstalledApp->appId);
555             UI_Free(toBeInstalledApp);
556         }
557     }
558 }
559 
ScanSystemApp(const cJSON * uninstallRecord,List<ToBeInstalledApp * > * systemPathList)560 void GtManagerService::ScanSystemApp(const cJSON *uninstallRecord, List<ToBeInstalledApp *> *systemPathList)
561 {
562     PreAppList *list = preAppList_;
563     if (list == nullptr) {
564         HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] ScanSystemApp InitAllAppInfo fail, list is nullptr");
565         return;
566     }
567 
568     uint8_t scanFlag = 0;
569     char *bundleName = nullptr;
570     int32_t versionCode = -1;
571     PreAppList *currentNode = nullptr;
572     PreAppList *nextNode = nullptr;
573 
574     LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(currentNode, nextNode, &list->appDoubleList, PreAppList, appDoubleList) {
575         if (currentNode == nullptr) {
576             return;
577         }
578         if ((strcmp(((PreAppList *)currentNode)->filePath, ".") == 0) ||
579             (strcmp(((PreAppList *)currentNode)->filePath, "..") == 0)) {
580             continue;
581         }
582 
583         if (BundleUtil::StartWith(((PreAppList *)currentNode)->filePath, SYSTEM_BUNDLE_PATH)) {
584             scanFlag = SYSTEM_APP_FLAG;
585         } else if (BundleUtil::StartWith(((PreAppList *)currentNode)->filePath, THIRD_SYSTEM_BUNDLE_PATH)) {
586             scanFlag = THIRD_SYSTEM_APP_FLAG;
587         } else {
588             continue; // skip third app
589         }
590 
591         // scan system app
592         bool res = CheckSystemBundleIsValid(((PreAppList *)currentNode)->filePath, &bundleName, versionCode);
593         if (!res) {
594             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] ScanSystemApp CheckSystemBundleIsValid failed!");
595             APP_ERRCODE_EXTRA(EXCE_ACE_APP_SCAN, EXCE_ACE_APP_SCAN_INVALID_SYSTEM_APP);
596             AdapterFree(bundleName);
597             continue;
598         }
599 
600         // third system app cannot restore after uninstall
601         if (scanFlag == THIRD_SYSTEM_APP_FLAG &&
602             CheckThirdSystemBundleHasUninstalled(bundleName, uninstallRecord)) {
603             AdapterFree(bundleName);
604             continue;
605         }
606 
607         ReloadEntireBundleInfo(((PreAppList *)currentNode)->filePath, bundleName,
608             systemPathList, versionCode, scanFlag);
609         AdapterFree(bundleName);
610     }
611     GtManagerService::FreePreAppInfo(list);
612 }
613 
ScanThirdApp(const char * appDir,const List<ToBeInstalledApp * > * systemPathList)614 void GtManagerService::ScanThirdApp(const char *appDir, const List<ToBeInstalledApp *> *systemPathList)
615 {
616     dirent *ent = nullptr;
617 
618     if (appDir == nullptr) {
619         return;
620     }
621 
622     DIR *dir = opendir(appDir);
623     if (dir == nullptr) {
624         return;
625     }
626     char *bundleName = reinterpret_cast<char *>(AdapterMalloc(MAX_BUNDLE_NAME_LEN + 1));
627     if (bundleName == nullptr) {
628         closedir(dir);
629         return;
630     }
631     int32_t entLen = 0;
632     while ((ent = readdir(dir)) != nullptr) {
633         ++entLen;
634         if (memset_s(bundleName, MAX_BUNDLE_NAME_LEN + 1, 0, MAX_BUNDLE_NAME_LEN + 1) != EOK) {
635             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] memset fail when initialize bundle name!");
636             break;
637         }
638 
639         if (strcpy_s(bundleName, MAX_BUNDLE_NAME_LEN + 1, ent->d_name) != 0) {
640             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] failed to copy bundle name!");
641             break;
642         }
643 
644         if ((strcmp(bundleName, ".") == 0) || (strcmp(bundleName, "..")) == 0) {
645             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] strcmp fail when reload third app!");
646             continue;
647         }
648 
649         int32_t len = strlen(appDir) + 1 + strlen(bundleName) + 1;
650         char *appPath = reinterpret_cast<char *>(UI_Malloc(len));
651         if (appPath == nullptr) {
652             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] malloc fail when reload third app!");
653             break;
654         }
655 
656         if (sprintf_s(appPath, len, "%s/%s", appDir, bundleName) < 0) {
657             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] strcat fail when reload third app!");
658             UI_Free(appPath);
659             break;
660         }
661 
662         if (!BundleUtil::IsDir(appPath)) {
663             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] app path is not dir when reload third app!");
664             UI_Free(appPath);
665             continue;
666         }
667 
668         if (IsSystemBundleInstalledPath(appPath, systemPathList)) {
669             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] app path is not third bundle path!");
670             UI_Free(appPath);
671             continue;
672         }
673 
674         if (installedThirdBundleNum_ >= MAX_THIRD_BUNDLE_NUMBER) {
675             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] third bundle reload number is %d!",
676                 installedThirdBundleNum_);
677             UI_Free(appPath);
678             continue;
679         }
680 
681         ReloadEntireBundleInfo(appPath, bundleName, nullptr, -1, THIRD_APP_FLAG);
682         UI_Free(appPath);
683     }
684 
685     HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] third app number is %{public}d", entLen);
686     AdapterFree(bundleName);
687     closedir(dir);
688 }
689 
IsSystemBundleInstalledPath(const char * appPath,const List<ToBeInstalledApp * > * systemPathList)690 bool GtManagerService::IsSystemBundleInstalledPath(const char *appPath, const List<ToBeInstalledApp *> *systemPathList)
691 {
692     if (appPath == nullptr || systemPathList == nullptr) {
693         return false;
694     }
695 
696     for (auto node = systemPathList->Begin(); node != systemPathList->End(); node = node->next_) {
697         ToBeInstalledApp *toBeInstalledApp = node->value_;
698         if (toBeInstalledApp->installedPath != nullptr &&
699             strcmp(appPath, toBeInstalledApp->installedPath) == 0) {
700             return true;
701         }
702     }
703     return false;
704 }
705 
CheckSystemBundleIsValid(const char * appPath,char ** bundleName,int32_t & versionCode)706 bool GtManagerService::CheckSystemBundleIsValid(const char *appPath, char **bundleName, int32_t &versionCode)
707 {
708     if (appPath == nullptr || bundleName == nullptr) {
709         return false;
710     }
711 
712     if (!BundleUtil::EndWith(appPath, INSTALL_FILE_SUFFIX)) {
713         return false;
714     }
715 
716     if (!GtBundleParser::ParseBundleAttr(appPath, bundleName, versionCode)) {
717         HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] parse bundle attr fail!");
718         return false;
719     }
720 
721     if (*bundleName != nullptr && strlen(*bundleName) >= MAX_BUNDLE_NAME_LEN) {
722         return false;
723     }
724     return true;
725 }
726 
ReloadEntireBundleInfo(const char * appPath,const char * bundleName,List<ToBeInstalledApp * > * systemPathList,int32_t versionCode,uint8_t scanFlag)727 void GtManagerService::ReloadEntireBundleInfo(const char *appPath, const char *bundleName,
728     List<ToBeInstalledApp *> *systemPathList, int32_t versionCode, uint8_t scanFlag)
729 {
730     char *codePath = nullptr;
731     char *appId = nullptr;
732     int32_t oldVersionCode = -1;
733 
734     if (appPath == nullptr || bundleName == nullptr) {
735         HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] ReloadEntireBundleInfo app path or bundle name is nullptr!");
736         APP_ERRCODE_EXTRA(EXCE_ACE_APP_SCAN, EXCE_ACE_APP_SCAN_UNKNOWN_BUNDLE_INFO);
737         return;
738     }
739 
740     if (QueryBundleInfo(bundleName) != nullptr) {
741         HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] bundle has been reloaded!");
742         return;
743     }
744 
745     bool res = BundleUtil::CheckBundleJsonIsValid(bundleName, &codePath, &appId, oldVersionCode);
746     bool isSystemApp = (scanFlag == SYSTEM_APP_FLAG);
747     if (scanFlag != THIRD_APP_FLAG) {
748         if (!res) {
749             AddSystemAppPathList(nullptr, appPath, systemPathList, isSystemApp, false, appId);
750             AdapterFree(appId);
751             AdapterFree(codePath);
752             return;
753         }
754         if (oldVersionCode < versionCode) {
755             AddSystemAppPathList(codePath, appPath, systemPathList, isSystemApp, true, appId);
756             AdapterFree(appId);
757             AdapterFree(codePath);
758             return;
759         }
760     } else {
761         if (!res && !BundleUtil::CheckBundleJsonIsValid(bundleName, &codePath, &appId, oldVersionCode)) {
762             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] ReloadEntireBundleInfo CheckBundleJsonIsValid failed!");
763             RecordAbiityInfoEvt(bundleName);
764             APP_ERRCODE_EXTRA(EXCE_ACE_APP_SCAN, EXCE_ACE_APP_SCAN_PARSE_JSON_FALIED);
765             AdapterFree(appId);
766             AdapterFree(codePath);
767             BundleUtil::RemoveDir(appPath);
768             return;
769         }
770     }
771 #ifdef BC_TRANS_ENABLE
772     TransformJsToBcWhenRestart(codePath, bundleName);
773 #endif
774     bool ret = ReloadBundleInfo(codePath, appId, isSystemApp);
775     if (ret && (scanFlag == THIRD_APP_FLAG)) {
776         installedThirdBundleNum_++;
777     }
778     AdapterFree(appId);
779     AdapterFree(codePath);
780 }
781 
AddSystemAppPathList(const char * installedPath,const char * path,List<ToBeInstalledApp * > * systemPathList,bool isSystemApp,bool isUpdated,const char * appId)782 void GtManagerService::AddSystemAppPathList(const char *installedPath, const char *path,
783     List<ToBeInstalledApp *> *systemPathList, bool isSystemApp, bool isUpdated, const char *appId)
784 {
785     if (path == nullptr || systemPathList == nullptr) {
786         return;
787     }
788 
789     ToBeInstalledApp *toBeInstalledApp =
790         reinterpret_cast<ToBeInstalledApp *>(UI_Malloc(sizeof(ToBeInstalledApp)));
791     if (toBeInstalledApp == nullptr) {
792         return;
793     }
794     toBeInstalledApp->installedPath = Utils::Strdup(installedPath);
795     toBeInstalledApp->path = Utils::Strdup(path);
796     toBeInstalledApp->isSystemApp = isSystemApp;
797     toBeInstalledApp->isUpdated = isUpdated;
798     toBeInstalledApp->appId = Utils::Strdup(appId);
799     systemPathList->PushBack(toBeInstalledApp);
800 }
801 
ReloadBundleInfo(const char * profileDir,const char * appId,bool isSystemApp)802 bool GtManagerService::ReloadBundleInfo(const char *profileDir, const char *appId, bool isSystemApp)
803 {
804     if (profileDir == nullptr) {
805         return false;
806     }
807     BundleRes *bundleRes = reinterpret_cast<BundleRes *>(AdapterMalloc(sizeof(BundleRes)));
808     if (bundleRes == nullptr) {
809         return false;
810     }
811     if (memset_s(bundleRes, sizeof(BundleRes), 0, sizeof(BundleRes)) != EOK) {
812         AdapterFree(bundleRes);
813         return false;
814     }
815     BundleInfo *bundleInfo = GtBundleParser::ParseHapProfile(profileDir, bundleRes);
816     if (bundleInfo != nullptr) {
817         bundleInfo->isSystemApp = isSystemApp;
818         bundleInfo->appId = Utils::Strdup(appId);
819         if (bundleInfo->appId == nullptr) {
820             BundleInfoUtils::FreeBundleInfo(bundleInfo);
821             AdapterFree(bundleRes->abilityRes);
822             AdapterFree(bundleRes);
823             return false;
824         }
825         bundleMap_->Add(bundleInfo);
826         if (bundleRes->abilityRes == nullptr ||
827             (bundleRes->abilityRes->labelId == 0 && bundleRes->abilityRes->iconId == 0)) {
828             AdapterFree(bundleRes->abilityRes);
829             AdapterFree(bundleRes);
830         } else {
831             bundleRes->bundleName = bundleInfo->bundleName;
832             AddBundleResList(bundleRes);
833         }
834         return true;
835     }
836     AdapterFree(bundleRes->abilityRes);
837     AdapterFree(bundleRes);
838     APP_ERRCODE_EXTRA(EXCE_ACE_APP_SCAN, EXCE_ACE_APP_SCAN_PARSE_PROFILE_FALIED);
839     HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] reload bundle info fail!, isSystemApp is %d", isSystemApp);
840     return false;
841 }
842 
AddBundleResList(const BundleRes * bundleRes)843 void GtManagerService::AddBundleResList(const BundleRes *bundleRes)
844 {
845     if (bundleRes == nullptr || bundleRes->bundleName == nullptr || bundleResList_ == nullptr) {
846         return;
847     }
848 
849     for (auto node = bundleResList_->Begin(); node != bundleResList_->End(); node = node->next_) {
850         BundleRes *res = node->value_;
851         if (res != nullptr && res->bundleName != nullptr && strcmp(res->bundleName, bundleRes->bundleName) == 0) {
852             return;
853         }
854     }
855     bundleResList_->PushFront(const_cast<BundleRes *>(bundleRes));
856 }
857 
RemoveBundleResList(const char * bundleName)858 void GtManagerService::RemoveBundleResList(const char *bundleName)
859 {
860     if (bundleName == nullptr || bundleResList_ == nullptr) {
861         return;
862     }
863 
864     for (auto node = bundleResList_->Begin(); node != bundleResList_->End(); node = node->next_) {
865         BundleRes *res = node->value_;
866         if (res == nullptr) {
867             return;
868         }
869         if (res->bundleName != nullptr && strcmp(bundleName, res->bundleName) == 0) {
870             AdapterFree(res->abilityRes);
871             AdapterFree(res);
872             bundleResList_->Remove(node);
873             return;
874         }
875     }
876 }
877 
UpdateBundleInfoList()878 void GtManagerService::UpdateBundleInfoList()
879 {
880     if (bundleResList_ == nullptr) {
881         return;
882     }
883 
884     for (auto node = bundleResList_->Begin(); node != bundleResList_->End(); node = node->next_) {
885         BundleRes *res = node->value_;
886         if (res == nullptr || res->bundleName == nullptr || res->abilityRes == nullptr) {
887             continue;
888         }
889 
890         BundleInfo *bundleInfo = bundleMap_->Get(res->bundleName);
891         if (bundleInfo == nullptr) {
892             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] get no bundleInfo when change bundle res!");
893             continue;
894         }
895 
896         int32_t len = strlen(INSTALL_PATH) + 1 + strlen(res->bundleName);
897         char *path = reinterpret_cast<char *>(UI_Malloc(len + 1));
898         if (path == nullptr) {
899             continue;
900         }
901 
902         if (sprintf_s(path, len + 1, "%s/%s", INSTALL_PATH, res->bundleName) < 0) {
903             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] change bundle res failed! because sprintf_s fail");
904             UI_Free(path);
905             continue;
906         }
907 
908         uint8_t errorCode = GtBundleParser::ConvertResInfoToBundleInfo(path, res->abilityRes->labelId,
909             res->abilityRes->iconId, bundleInfo);
910         UI_Free(path);
911         if (errorCode != ERR_OK) {
912             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] change bundle res failed! errorCode is %d", errorCode);
913             return;
914         }
915     }
916 }
917 
918 #ifdef BC_TRANS_ENABLE
TransformJsToBcWhenRestart(const char * codePath,const char * bundleName)919 void GtManagerService::TransformJsToBcWhenRestart(const char *codePath, const char *bundleName)
920 {
921     if (codePath == nullptr) {
922         return;
923     }
924 
925     char *bundleJsonPathComp[] = {
926         const_cast<char *>(JSON_PATH), const_cast<char *>(bundleName), const_cast<char *>(JSON_SUFFIX)
927     };
928     char *bundleJsonPath = BundleUtil::Strscat(bundleJsonPathComp, sizeof(bundleJsonPathComp) / sizeof(char *));
929     if (bundleJsonPath == nullptr) {
930         return;
931     }
932 
933     cJSON *installRecordJson = BundleUtil::GetJsonStream(bundleJsonPath);
934     if (installRecordJson == nullptr) {
935         AdapterFree(bundleJsonPath);
936         HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] get installRecord fail when restart!");
937         return;
938     }
939 
940     if (jsEngineVer_ == nullptr) {
941         cJSON_Delete(installRecordJson);
942         AdapterFree(bundleJsonPath);
943         HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] TransformJsToBcWhenRestart jsEngineVer_ is nullptr!");
944         return;
945     }
946 
947     cJSON *jsEngineVerObj = cJSON_CreateString(jsEngineVer_);
948     if (jsEngineVerObj == nullptr) {
949         cJSON_Delete(installRecordJson);
950         AdapterFree(bundleJsonPath);
951         HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] create string object fail when restart!");
952         return;
953     }
954 
955     cJSON *oldJsEngineVerObj = cJSON_GetObjectItem(installRecordJson, JSON_SUB_KEY_JSENGINE_VERSION);
956     if (oldJsEngineVerObj == nullptr) {
957         cJSON_Delete(jsEngineVerObj);
958         cJSON_Delete(installRecordJson);
959         AdapterFree(bundleJsonPath);
960         return;
961     }
962     if (cJSON_IsString(oldJsEngineVerObj) && strcmp(oldJsEngineVerObj->valuestring, jsEngineVer_) == 0) {
963         cJSON_Delete(jsEngineVerObj);
964         cJSON *transformResultObj = cJSON_GetObjectItem(installRecordJson, JSON_SUB_KEY_TRANSFORM_RESULT);
965         if (cJSON_IsNumber(transformResultObj) && transformResultObj->valueint == 0) {
966             cJSON_Delete(installRecordJson);
967             AdapterFree(bundleJsonPath);
968             return;
969         }
970     } else if (oldJsEngineVerObj == nullptr) {
971         if (!cJSON_AddItemToObject(installRecordJson, JSON_SUB_KEY_JSENGINE_VERSION, jsEngineVerObj)) {
972             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] add js engine version record fail when restart!");
973             cJSON_Delete(jsEngineVerObj);
974             cJSON_Delete(installRecordJson);
975             AdapterFree(bundleJsonPath);
976             return;
977         }
978     } else {
979         if (!cJSON_ReplaceItemInObject(installRecordJson, JSON_SUB_KEY_JSENGINE_VERSION, jsEngineVerObj)) {
980             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] refresh js engine version fail when restart!");
981             cJSON_Delete(jsEngineVerObj);
982             cJSON_Delete(installRecordJson);
983             AdapterFree(bundleJsonPath);
984             return;
985         }
986     }
987 
988     TransformJsToBc(codePath, bundleJsonPath, installRecordJson);
989     cJSON_Delete(installRecordJson);
990     AdapterFree(bundleJsonPath);
991 }
992 
TransformJsToBc(const char * codePath,const char * bundleJsonPath,cJSON * installRecordObj)993 void GtManagerService::TransformJsToBc(const char *codePath, const char *bundleJsonPath, cJSON *installRecordObj)
994 {
995     if (codePath == nullptr || installRecordObj == nullptr || bundleJsonPath == nullptr) {
996         return;
997     }
998 
999     char *jsPathComp[] = {const_cast<char *>(codePath), const_cast<char *>(ASSET_JS_PATH)};
1000     char *jsPath = BundleUtil::Strscat(jsPathComp, sizeof(jsPathComp) / sizeof(char *));
1001     if (jsPath == nullptr) {
1002         return;
1003     }
1004 
1005     EXECRES result = walk_directory(jsPath);
1006     HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] transform js to bc, result is %d", result);
1007     if (result != EXCE_ACE_JERRY_EXEC_OK) {
1008         result = walk_del_bytecode(jsPath);
1009         HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] delete byte code, result is %d", result);
1010         AdapterFree(jsPath);
1011         return;
1012     }
1013     AdapterFree(jsPath);
1014 
1015     cJSON *resultObj = cJSON_CreateNumber(0);
1016     if (resultObj == nullptr) {
1017         return;
1018     }
1019     cJSON *oldResultObj = cJSON_GetObjectItem(installRecordObj, JSON_SUB_KEY_TRANSFORM_RESULT);
1020     if (oldResultObj == nullptr) {
1021         if (!cJSON_AddItemToObject(installRecordObj, JSON_SUB_KEY_TRANSFORM_RESULT, resultObj)) {
1022             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] add transform result record fail when restart!");
1023             cJSON_Delete(resultObj);
1024             return;
1025         }
1026     } else {
1027         if (!cJSON_ReplaceItemInObject(installRecordObj, JSON_SUB_KEY_TRANSFORM_RESULT, resultObj)) {
1028             HILOG_ERROR(HILOG_MODULE_AAFWK, "[BMS] refresh transform result record fail when restart!");
1029             cJSON_Delete(resultObj);
1030             return;
1031         }
1032     }
1033     (void)BundleUtil::StoreJsonContentToFile(bundleJsonPath, installRecordObj);
1034 }
1035 #endif
1036 
CheckThirdSystemBundleHasUninstalled(const char * bundleName,const cJSON * object)1037 bool GtManagerService::CheckThirdSystemBundleHasUninstalled(const char *bundleName, const cJSON *object)
1038 {
1039     if (object == nullptr || bundleName == nullptr) {
1040         return false;
1041     }
1042 
1043     cJSON *array = cJSON_GetObjectItem(object, JSON_MAIN_KEY);
1044     if (!cJSON_IsArray(array)) {
1045         return false;
1046     }
1047 
1048     int32_t size = cJSON_GetArraySize(array);
1049     for (int32_t i = 0; i < size; i++) {
1050         cJSON *item = cJSON_GetArrayItem(array, i);
1051         if (!cJSON_IsString(item)) {
1052             return false;
1053         }
1054         if ((item->valuestring != nullptr) && strcmp(bundleName, item->valuestring) == 0) {
1055             return true;
1056         }
1057     }
1058     return false;
1059 }
1060 
QueryBundleInfo(const char * bundleName)1061 BundleInfo *GtManagerService::QueryBundleInfo(const char *bundleName)
1062 {
1063     if (bundleName == nullptr || bundleMap_ == nullptr) {
1064         return nullptr;
1065     }
1066     return bundleMap_->Get(bundleName);
1067 }
1068 
RemoveBundleInfo(const char * bundleName)1069 void GtManagerService::RemoveBundleInfo(const char *bundleName)
1070 {
1071     if (bundleName == nullptr || bundleMap_ == nullptr) {
1072         return;
1073     }
1074     bundleMap_->Erase(bundleName);
1075 }
1076 
AddBundleInfo(BundleInfo * info)1077 void GtManagerService::AddBundleInfo(BundleInfo *info)
1078 {
1079     if (info == nullptr || info->bundleName == nullptr || bundleMap_ == nullptr) {
1080         return;
1081     }
1082     bundleMap_->Add(info);
1083 }
1084 
UpdateBundleInfo(BundleInfo * info)1085 bool GtManagerService::UpdateBundleInfo(BundleInfo *info)
1086 {
1087     if (info == nullptr) {
1088         return false;
1089     }
1090     return bundleMap_->Update(info);
1091 }
1092 
GetNumOfThirdBundles()1093 uint32_t GtManagerService::GetNumOfThirdBundles()
1094 {
1095     return installedThirdBundleNum_;
1096 }
1097 
AddNumOfThirdBundles()1098 void GtManagerService::AddNumOfThirdBundles()
1099 {
1100     installedThirdBundleNum_++;
1101 }
1102 
ReduceNumOfThirdBundles()1103 void GtManagerService::ReduceNumOfThirdBundles()
1104 {
1105     installedThirdBundleNum_--;
1106 }
1107 
RegisterEvent(InstallerCallback listenCallback)1108 bool GtManagerService::RegisterEvent(InstallerCallback listenCallback)
1109 {
1110     HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] RegisterListen RegisterEvent");
1111     if (listenCallback == nullptr) {
1112         HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] RegisterListen is fail because is null");
1113         return false;
1114     }
1115     listenList_->PushBack(listenCallback);
1116     return true;
1117 }
1118 
UnregisterEvent(InstallerCallback listenCallback)1119 bool GtManagerService::UnregisterEvent(InstallerCallback listenCallback)
1120 {
1121     HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] UnregisterListen UnregisterEvent");
1122     if (listenCallback == nullptr) {
1123         HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] UnregisterListen is fail because is null");
1124         return false;
1125     }
1126     for (auto node = listenList_->Begin(); node != listenList_->End(); node = node->next_) {
1127         if (node == nullptr) {
1128             HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] Listener not found");
1129             return false;
1130         }
1131         if ((*(node->value_)) == listenCallback) {
1132             listenList_->Remove(node);
1133             return true;
1134         }
1135     }
1136     return false;
1137 }
ReportHceInstallCallback(uint8_t errCode,uint8_t installState,uint8_t process)1138 int32_t GtManagerService::ReportHceInstallCallback(uint8_t errCode, uint8_t installState, uint8_t process)
1139 {
1140     HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] reportListener ReportHceInstallCallback");
1141     BundleInstallMsg *bundleInstallMsg = reinterpret_cast<BundleInstallMsg *>(AdapterMalloc(sizeof(BundleInstallMsg)));
1142     if (memset_s(bundleInstallMsg, sizeof(BundleInstallMsg), 0, sizeof(BundleInstallMsg)) != EOK) {
1143         AdapterFree(bundleInstallMsg);
1144     }
1145     if (bundleInstallMsg == nullptr) {
1146         HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] ReportHceInstallCallback is fail because bundleInstallMsg is null");
1147         return -1;
1148     }
1149     bundleInstallMsg_->installState = static_cast<InstallState>(installState);
1150     bundleInstallMsg_->installProcess = process;
1151     bundleInstallMsg->installState = bundleInstallMsg_->installState;
1152     bundleInstallMsg->installProcess = bundleInstallMsg_->installProcess;
1153     bundleInstallMsg->bundleName = bundleInstallMsg_->bundleName;
1154     for (auto node = listenList_->Begin(); node != listenList_->End(); node = node->next_) {
1155         (*(node->value_))(errCode, bundleInstallMsg);
1156     }
1157     return 0;
1158 }
1159 
ReportHceUninstallCallback(uint8_t errCode,uint8_t installState,char * bundleName,uint8_t process)1160 int32_t GtManagerService::ReportHceUninstallCallback(
1161     uint8_t errCode, uint8_t installState, char *bundleName, uint8_t process)
1162 {
1163     HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] unReportListener ReportHceUninstallCallback");
1164     BundleInstallMsg *bundleInstallMsg = reinterpret_cast<BundleInstallMsg *>(AdapterMalloc(sizeof(BundleInstallMsg)));
1165     if (memset_s(bundleInstallMsg, sizeof(BundleInstallMsg), 0, sizeof(BundleInstallMsg)) != EOK) {
1166         AdapterFree(bundleInstallMsg);
1167     }
1168     if (bundleInstallMsg == nullptr) {
1169         HILOG_INFO(HILOG_MODULE_AAFWK, "[BMS] ReportHceUninstallCallback is fail because bundleInstallMsg is null");
1170         return -1;
1171     }
1172     bundleInstallMsg->installState = static_cast<InstallState>(installState);
1173     bundleInstallMsg->installProcess = process;
1174     bundleInstallMsg->bundleName = bundleName;
1175     for (auto node = listenList_->Begin(); node != listenList_->End(); node = node->next_) {
1176         (*(node->value_))(errCode, bundleInstallMsg);
1177     }
1178     return 0;
1179 }
1180 
ReportInstallCallback(uint8_t errCode,uint8_t installState,uint8_t process,InstallerCallback installerCallback)1181 int32_t GtManagerService::ReportInstallCallback(
1182     uint8_t errCode, uint8_t installState, uint8_t process, InstallerCallback installerCallback)
1183 {
1184     if (bundleInstallMsg_ == nullptr) {
1185         return -1;
1186     }
1187     if (installerCallback == nullptr) {
1188         return -1;
1189     }
1190     BundleInstallMsg *bundleInstallMsg = reinterpret_cast<BundleInstallMsg *>(AdapterMalloc(sizeof(BundleInstallMsg)));
1191     if (bundleInstallMsg == nullptr) {
1192         return -1;
1193     }
1194     bundleInstallMsg_->installState = static_cast<InstallState>(installState);
1195     bundleInstallMsg_->installProcess = process;
1196     bundleInstallMsg->installState = bundleInstallMsg_->installState;
1197     bundleInstallMsg->installProcess = bundleInstallMsg_->installProcess;
1198     bundleInstallMsg->label = bundleInstallMsg_->label;
1199     bundleInstallMsg->bundleName = bundleInstallMsg_->bundleName;
1200     bundleInstallMsg->smallIconPath = bundleInstallMsg_->smallIconPath;
1201     bundleInstallMsg->bigIconPath = bundleInstallMsg_->bigIconPath;
1202     (*installerCallback)(errCode, bundleInstallMsg);
1203     return 0;
1204 }
1205 
ReportUninstallCallback(uint8_t errCode,uint8_t installState,char * bundleName,uint8_t process,InstallerCallback installerCallback)1206 int32_t GtManagerService::ReportUninstallCallback(uint8_t errCode, uint8_t installState, char *bundleName,
1207     uint8_t process, InstallerCallback installerCallback)
1208 {
1209     if (installerCallback == nullptr) {
1210         return -1;
1211     }
1212     BundleInstallMsg *bundleInstallMsg = reinterpret_cast<BundleInstallMsg *>(AdapterMalloc(sizeof(BundleInstallMsg)));
1213     if (bundleInstallMsg == nullptr) {
1214         return -1;
1215     }
1216     bundleInstallMsg->installState = static_cast<InstallState>(installState);
1217     bundleInstallMsg->bundleName = bundleName;
1218     bundleInstallMsg->installProcess = process;
1219     (*installerCallback)(errCode, bundleInstallMsg);
1220     return 0;
1221 }
1222 
InitPreAppInfo()1223 PreAppList *GtManagerService::InitPreAppInfo()
1224 {
1225     PreAppList *list = (PreAppList *)AdapterMalloc(sizeof(PreAppList));
1226     if (list == nullptr) {
1227         return nullptr;
1228     }
1229 
1230     if (memset_s(list, sizeof(PreAppList), 0, sizeof(PreAppList)) != EOK) {
1231         AdapterFree(list);
1232         return nullptr;
1233     }
1234 
1235     LOS_ListInit(&list->appDoubleList);
1236     return list;
1237 }
1238 
QueryPreAppInfo(const char * appDir,PreAppList * list)1239 void GtManagerService::QueryPreAppInfo(const char *appDir, PreAppList *list)
1240 {
1241     struct dirent *ent = nullptr;
1242     if (appDir == nullptr) {
1243         return;
1244     }
1245 
1246     DIR *dir = opendir(appDir);
1247     if (dir == nullptr) {
1248         return;
1249     }
1250     char *fileName = reinterpret_cast<char *>(AdapterMalloc(MAX_NAME_LEN + 1));
1251     if (fileName == nullptr) {
1252         closedir(dir);
1253         return;
1254     }
1255     while ((ent = readdir(dir)) != nullptr) {
1256         if (memset_s(fileName, MAX_NAME_LEN + 1, 0, MAX_NAME_LEN + 1) != EOK) {
1257             break;
1258         }
1259 
1260         if (strcpy_s(fileName, MAX_NAME_LEN + 1, ent->d_name) != 0) {
1261             break;
1262         }
1263 
1264         if ((strcmp(fileName, ".") == 0) || (strcmp(fileName, "..")) == 0) {
1265             continue;
1266         }
1267 
1268         int32_t len = strlen(appDir) + 1 + strlen(fileName) + 1;
1269         char *appPath = reinterpret_cast<char *>(AdapterMalloc(len));
1270         if (appPath == nullptr) {
1271             break;
1272         }
1273 
1274         if (sprintf_s(appPath, len, "%s/%s", appDir, fileName) < 0) {
1275             AdapterFree(appPath);
1276             break;
1277         }
1278 
1279         if (!BundleUtil::IsFile(appPath)) {
1280             AdapterFree(appPath);
1281             continue;
1282         }
1283 
1284         InsertPreAppInfo(appPath, (PreAppList *)&list->appDoubleList);
1285         AdapterFree(appPath);
1286     }
1287     AdapterFree(fileName);
1288     closedir(dir);
1289 }
1290 
InsertPreAppInfo(const char * filePath,PreAppList * list)1291 void GtManagerService::InsertPreAppInfo(const char *filePath, PreAppList *list)
1292 {
1293     if ((filePath == nullptr) || (list == nullptr)) {
1294         return;
1295     }
1296 
1297     PreAppList *app = (PreAppList *)AdapterMalloc(sizeof(PreAppList));
1298     if (app == nullptr) {
1299         return;
1300     }
1301 
1302     if (memset_s(app, sizeof(PreAppList), 0, sizeof(PreAppList)) != 0) {
1303         AdapterFree(app);
1304         return;
1305     }
1306 
1307     if (memcpy_s(app->filePath, sizeof(app->filePath), filePath, strnlen(filePath, MAX_APP_FILE_PATH_LEN)) != 0) {
1308         AdapterFree(app);
1309         return;
1310     }
1311 
1312     LOS_ListTailInsert(&list->appDoubleList, &app->appDoubleList);
1313     return;
1314 }
1315 
SetPreAppInfo(PreAppList * list)1316 void GtManagerService::SetPreAppInfo(PreAppList *list)
1317 {
1318     if (list == nullptr) {
1319         return;
1320     }
1321     preAppList_ = list;
1322     return;
1323 }
1324 
FreePreAppInfo(const PreAppList * list)1325 void GtManagerService::FreePreAppInfo(const PreAppList *list)
1326 {
1327     if (list == nullptr) {
1328         return;
1329     }
1330 
1331     PreAppList *currentNode = nullptr;
1332     PreAppList *nextNode = nullptr;
1333     LOS_DL_LIST_FOR_EACH_ENTRY_SAFE(currentNode, nextNode, &list->appDoubleList, PreAppList, appDoubleList) {
1334         if (currentNode != nullptr) {
1335             LOS_ListDelete(&(currentNode->appDoubleList));
1336             AdapterFree(currentNode);
1337             currentNode = nullptr;
1338         }
1339     }
1340 
1341     if (list != nullptr) {
1342         AdapterFree(list);
1343     }
1344     return;
1345 }
1346 } // namespace OHOS
1347 extern "C" {
1348 static char *g_currentBundle = nullptr;
1349 const int32_t BUNDLENAME_MUTEX_TIMEOUT = 2000;
1350 static osMutexId_t g_currentBundleMutex;
1351 
SetCurrentBundle(const char * name)1352 void SetCurrentBundle(const char *name)
1353 {
1354     MutexAcquire(&g_currentBundleMutex, BUNDLENAME_MUTEX_TIMEOUT);
1355     AdapterFree(g_currentBundle);
1356     if (name == nullptr) {
1357         MutexRelease(&g_currentBundleMutex);
1358         return;
1359     }
1360 
1361     int len = strlen(name);
1362     g_currentBundle = (char *)AdapterMalloc(len + 1);
1363     if (g_currentBundle == nullptr || strncpy_s(g_currentBundle, len + 1, name, len) < 0) {
1364         AdapterFree(g_currentBundle);
1365     }
1366     MutexRelease(&g_currentBundleMutex);
1367 }
1368 
GetCurrentBundle()1369 const char *GetCurrentBundle()
1370 {
1371     MutexAcquire(&g_currentBundleMutex, BUNDLENAME_MUTEX_TIMEOUT);
1372     if (g_currentBundle == nullptr) {
1373         MutexRelease(&g_currentBundleMutex);
1374         return nullptr;
1375     }
1376 
1377     int len = strlen(g_currentBundle);
1378     char *bundleName = (char *)AdapterMalloc(len + 1);
1379     if (bundleName == nullptr || strncpy_s(bundleName, len + 1, g_currentBundle, len) < 0) {
1380         AdapterFree(bundleName);
1381         MutexRelease(&g_currentBundleMutex);
1382         return nullptr;
1383     }
1384 
1385     MutexRelease(&g_currentBundleMutex);
1386     return bundleName;
1387 }
1388 }
1389