1 /*
2  * Copyright (c) 2021-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 #ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BASE_BUNDLE_INSTALLER_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BASE_BUNDLE_INSTALLER_H
18 
19 #include <map>
20 #include <unordered_map>
21 #include <string>
22 
23 #include "nocopyable.h"
24 
25 #include "access_token.h"
26 #include "bundle_common_event_mgr.h"
27 #include "bundle_data_mgr.h"
28 #include "bundle_install_checker.h"
29 #include "event_report.h"
30 #include "install_param.h"
31 #include "quick_fix/appqf_info.h"
32 #include "shared_bundle_installer.h"
33 
34 #ifdef APP_DOMAIN_VERIFY_ENABLED
35 #include "app_domain_verify_mgr_client.h"
36 #endif
37 
38 namespace OHOS {
39 namespace AppExecFwk {
40 class BaseBundleInstaller {
41 public:
42     BaseBundleInstaller();
43     virtual ~BaseBundleInstaller();
44     void SetCallingUid(int32_t callingUid);
45 
46 protected:
47     bool otaInstall_ = false;
48     enum class InstallerState : uint8_t {
49         INSTALL_START,
50         INSTALL_BUNDLE_CHECKED = 5,
51         INSTALL_SYSCAP_CHECKED = 10,
52         INSTALL_SIGNATURE_CHECKED = 15,
53         INSTALL_PARSED = 20,
54         INSTALL_HAP_HASH_PARAM_CHECKED = 25,
55         INSTALL_OVERLAY_CHECKED = 30,
56         INSTALL_VERSION_AND_BUNDLENAME_CHECKED = 35,
57         INSTALL_NATIVE_SO_CHECKED = 40,
58         INSTALL_PROXY_DATA_CHECKED = 45,
59         INSTALL_REMOVE_SANDBOX_APP = 50,
60         INSTALL_EXTRACTED = 60,
61         INSTALL_INFO_SAVED = 80,
62         INSTALL_RENAMED = 90,
63         INSTALL_SUCCESS = 100,
64         INSTALL_FAILED,
65     };
66 
67     enum SingletonState {
68         DEFAULT,
69         SINGLETON_TO_NON = 1,
70         NON_TO_SINGLETON = 2,
71     };
72 
73     struct SharedBundleRollBackInfo {
74         std::vector<std::string> newDirs; // record newly created directories, delete when rollback
75         std::vector<std::string> newBundles; // record newly installed bundle, uninstall when rollback
76         std::unordered_map<std::string, InnerBundleInfo> backupBundles; // record initial InnerBundleInfo
77     };
78 
79     /**
80      * @brief The main function for system and normal bundle install.
81      * @param bundlePath Indicates the path for storing the HAP file of the application
82      *                   to install or update.
83      * @param installParam Indicates the install parameters.
84      * @param appType Indicates the application type.
85      * @return Returns ERR_OK if the application install successfully; returns error code otherwise.
86      */
87     ErrCode InstallBundle(
88         const std::string &bundlePath, const InstallParam &installParam, const Constants::AppType appType);
89     /**
90      * @brief The main function for system and normal bundle install.
91      * @param bundlePaths Indicates the paths for storing the HAP file sof the application
92      *                   to install or update.
93      * @param installParam Indicates the install parameters.
94      * @param appType Indicates the application type.
95      * @return Returns ERR_OK if the application install successfully; returns error code otherwise.
96      */
97     ErrCode InstallBundle(const std::vector<std::string> &bundlePaths, const InstallParam &installParam,
98         const Constants::AppType appType);
99     /**
100      * @brief The main function for uninstall a bundle.
101      * @param bundleName Indicates the bundle name of the application to uninstall.
102      * @param installParam Indicates the uninstall parameters.
103      * @return Returns ERR_OK if the application uninstall successfully; returns error code otherwise.
104      */
105     ErrCode UninstallBundle(const std::string &bundleName, const InstallParam &installParam);
106     /**
107      * @brief The main function for uninstall a module in a specific bundle.
108      * @param bundleName Indicates the bundle name of the application to uninstall.
109      * @param modulePackage Indicates the module package of the module to uninstall.
110      * @param installParam Indicates the uninstall parameters.
111      * @return Returns ERR_OK if the application uninstall successfully; returns error code otherwise.
112      */
113     ErrCode UninstallBundle(
114         const std::string &bundleName, const std::string &modulePackage, const InstallParam &installParam);
115     /**
116      * @brief The main function for uninstall a bundle by uninstallParam.
117      * @param uninstallParam Indicates the input of uninstallParam.
118      * @return Returns ERR_OK if the application uninstall successfully; returns error code otherwise.
119      */
120     ErrCode CheckUninstallInnerBundleInfo(const InnerBundleInfo &info, const std::string &bundleName);
121     ErrCode UninstallBundleByUninstallParam(const UninstallParam &uninstallParam);
122     /**
123      * @brief Update the installer state.
124      * @attention This function changes the base class state only.
125      * @param state Indicates the state to be updated to.
126      * @return
127      */
128     virtual void UpdateInstallerState(const InstallerState state);
129     /**
130      * @brief Get the installer state.
131      * @return The current state of the installer object.
132      */
GetInstallerState()133     inline InstallerState GetInstallerState()
134     {
135         return state_;
136     }
137     /**
138      * @brief Get the installer state.
139      * @param state Indicates the state to be updated to.
140      * @return
141      */
SetInstallerState(InstallerState state)142     inline void SetInstallerState(InstallerState state)
143     {
144         state_ = state;
145     }
146 
GetCurrentBundleName() const147     std::string GetCurrentBundleName() const
148     {
149         return bundleName_;
150     }
151     /**
152      * @brief The main function for bundle install by bundleName.
153      * @param bundleName Indicates the bundleName of the application to install.
154      * @param installParam Indicates the install parameters.
155      * @return Returns ERR_OK if the application install successfully; returns error code otherwise.
156      */
157     ErrCode Recover(const std::string &bundleName, const InstallParam &installParam);
158     /**
159      * @brief The main function for bundle install by bundleName.
160      * @param bundleName Indicates the bundleName of the application to install.
161      * @param installParam Indicates the install parameters.
162      * @return Returns ERR_OK if the application install successfully; returns error code otherwise.
163      */
164     ErrCode InstallBundleByBundleName(const std::string &bundleName, const InstallParam &installParam);
165     /**
166      * @brief Reset install properties.
167      */
168     void ResetInstallProperties();
169     /**
170      * @brief Reset install properties.
171      * @param isBootScene Indicates the event occurs in the boot phase.
172      */
MarkPreBundleSyeEventBootTag(bool isBootScene)173     void MarkPreBundleSyeEventBootTag(bool isBootScene)
174     {
175         sysEventInfo_.preBundleScene =
176             isBootScene ? InstallScene::BOOT : InstallScene::REBOOT;
177     }
178 
179     bool NotifyAllBundleStatus();
180 
181     std::string GetCheckResultMsg() const;
182 
183     void SetCheckResultMsg(const std::string checkResultMsg) const;
184 
185     ErrCode RollbackHmpUserInfo(const std::string &bundleName);
186 
187     ErrCode RollbackHmpCommonInfo(const std::string &bundleName);
188 
189     bool HasDriverExtensionAbility(const std::string &bundleName);
190 
191 private:
192     /**
193      * @brief The real procedure for system and normal bundle install.
194      * @param bundlePath Indicates the path for storing the HAP file of the application
195      *                   to install or update.
196      * @param installParam Indicates the install parameters.
197      * @param appType Indicates the application type.
198      * @param uid Indicates the uid of the application.
199      * @return Returns ERR_OK if the bundle install successfully; returns error code otherwise.
200      */
201     ErrCode ProcessBundleInstall(const std::vector<std::string> &bundlePaths, const InstallParam &installParam,
202         const Constants::AppType appType, int32_t &uid);
203 
204     ErrCode InnerProcessBundleInstall(std::unordered_map<std::string, InnerBundleInfo> &newInfos,
205         InnerBundleInfo &oldInfo, const InstallParam &installParam, int32_t &uid);
206 
207     /**
208      * @brief The real procedure function for uninstall a bundle.
209      * @param bundleName Indicates the bundle name of the application to uninstall.
210      * @param installParam Indicates the uninstall parameters.
211      * @param uid Indicates the uid of the application.
212      * @return Returns ERR_OK if the bundle uninstall successfully; returns error code otherwise.
213      */
214     ErrCode ProcessBundleUninstall(const std::string &bundleName, const InstallParam &installParam, int32_t &uid);
215     /**
216      * @brief The real procedure for uninstall a module in a specific bundle.
217      * @param bundleName Indicates the bundle name of the application to uninstall.
218      * @param modulePackage Indicates the module package of the module to uninstall.
219      * @param installParam Indicates the uninstall parameters.
220      * @param uid Indicates the uid of the application.
221      * @return Returns ERR_OK if the module uninstall successfully; returns error code otherwise.
222      */
223     ErrCode ProcessBundleUninstall(const std::string &bundleName, const std::string &modulePackage,
224         const InstallParam &installParam, int32_t &uid);
225     /**
226      * @brief The process of installing a new bundle.
227      * @param info Indicates the InnerBundleInfo parsed from the config.json in the HAP package.
228      * @param uid Indicates the uid of the application.
229      * @return Returns ERR_OK if the new bundle install successfully; returns error code otherwise.
230      */
231     ErrCode ProcessBundleInstallStatus(InnerBundleInfo &info, int32_t &uid);
232     /**
233      * @brief The process of installing a native bundle.
234      * @param info Indicates the InnerBundleInfo parsed from the config.json in the HAP package.
235      * @param uid Indicates the uid of the application.
236      * @return Returns ERR_OK if the native bundle install successfully; returns error code otherwise.
237      */
238     ErrCode ProcessBundleInstallNative(InnerBundleInfo &info, int32_t &userId);
239     /**
240      * @brief The process of uninstalling a native bundle.
241      * @param info Indicates the InnerBundleInfo parsed from the config.json in the HAP package.
242      * @param uid Indicates the uid of the application.
243      * @param bundleName Indicates the bundleName of the application.
244      * @return Returns ERR_OK if the native bundle uninstall successfully; returns error code otherwise.
245      */
246     ErrCode ProcessBundleUnInstallNative(InnerBundleInfo &info, int32_t &userId, std::string bundleName);
247     /**
248      * @brief The process of updating an exist bundle.
249      * @param oldInfo Indicates the exist InnerBundleInfo object get from the database.
250      * @param newInfo Indicates the InnerBundleInfo object parsed from the config.json in the HAP package.
251      * @param isReplace Indicates whether there is the replace flag in the install flag.
252      * @return Returns ERR_OK if the bundle updating successfully; returns error code otherwise.
253      */
254     ErrCode ProcessBundleUpdateStatus(InnerBundleInfo &oldInfo,
255         InnerBundleInfo &newInfo, bool isReplace, bool killProcess = true);
256     /**
257      * @brief Remove a whole bundle.
258      * @param info Indicates the InnerBundleInfo object of a bundle.
259      * @param isKeepData Indicates that whether to save data.
260      * @return Returns ERR_OK if the bundle removed successfully; returns error code otherwise.
261      */
262     ErrCode RemoveBundle(InnerBundleInfo &info, bool isKeepData);
263     /**
264      * @brief Create the code and data directories of a bundle.
265      * @param info Indicates the InnerBundleInfo object of a bundle.
266      * @return Returns ERR_OK if the bundle directories created successfully; returns error code otherwise.
267      */
268     ErrCode CreateBundleAndDataDir(InnerBundleInfo &info) const;
269     /**
270      * @brief Extract the code to temporilay directory and rename it.
271      * @param info Indicates the InnerBundleInfo object of a bundle.
272      * @param modulePath normal files decompression path.
273      * @return Returns ERR_OK if the bundle extract and renamed successfully; returns error code otherwise.
274      */
275     ErrCode ExtractModule(InnerBundleInfo &info, const std::string &modulePath);
276     /**
277      * @brief Remove the code and data directories of a bundle.
278      * @param info Indicates the InnerBundleInfo object of a bundle.
279      * @param isKeepData Indicates that whether to save data.
280      * @return Returns ERR_OK if the bundle directories removed successfully; returns error code otherwise.
281      */
282     ErrCode RemoveBundleAndDataDir(const InnerBundleInfo &info, bool isKeepData) const;
283     /**
284      * @brief Remove the code and data directories of a module in a bundle.
285      * @param info Indicates the InnerBundleInfo object of a bundle.
286      * @param modulePackage Indicates the module to be removed.
287      * @param userId Indicates the userId.
288      * @param isKeepData Indicates that whether to save data.
289      * @return Returns ERR_OK if the bundle directories removed successfully; returns error code otherwise.
290      */
291     ErrCode RemoveModuleAndDataDir(const InnerBundleInfo &info,
292         const std::string &modulePackage, int32_t userId, bool isKeepData) const;
293     /**
294      * @brief Remove the current installing module directory.
295      * @param info Indicates the InnerBundleInfo object of a bundle under installing.
296      * @return Returns ERR_OK if the module directory removed successfully; returns error code otherwise.
297      */
298     ErrCode RemoveModuleDir(const std::string &modulePath) const;
299     /**
300      * @brief Extract files of the current installing module package.
301      * @param info Indicates the InnerBundleInfo object of a bundle under installing.
302      * @param modulePath normal files decompression path.
303      * @param targetSoPath so files decompression path.
304      * @param cpuAbi cpuAbi.
305      * @return Returns ERR_OK if the module files extraced successfully; returns error code otherwise.
306      */
307     ErrCode ExtractModuleFiles(const InnerBundleInfo &info, const std::string &modulePath,
308         const std::string &targetSoPath, const std::string &cpuAbi);
309     /**
310      * @brief Rename the directory of current installing module package.
311      * @param info Indicates the InnerBundleInfo object of a bundle under installing.
312      * @return Returns ERR_OK if the module directory renamed successfully; returns error code otherwise.
313      */
314     ErrCode RenameModuleDir(const InnerBundleInfo &info) const;
315     /**
316      * @brief The process of install a new module package.
317      * @param newInfo Indicates the InnerBundleInfo object parsed from the config.json in the HAP package.
318      * @param oldInfo Indicates the exist InnerBundleInfo object get from the database.
319      * @return Returns ERR_OK if the new module install successfully; returns error code otherwise.
320      */
321     ErrCode ProcessNewModuleInstall(InnerBundleInfo &newInfo, InnerBundleInfo &oldInfo);
322     /**
323      * @brief The process of updating an exist module package.
324      * @param newInfo Indicates the InnerBundleInfo object parsed from the config.json in the HAP package.
325      * @param oldInfo Indicates the exist InnerBundleInfo object get from the database.
326      * @return Returns ERR_OK if the module updating successfully; returns error code otherwise.
327      */
328     ErrCode ProcessModuleUpdate(InnerBundleInfo &newInfo,
329         InnerBundleInfo &oldInfo, bool isReplace, bool killProcess = true);
330     /**
331      * @brief The real procedure for bundle install by bundleName.
332      * @param bundleName Indicates the bundleName the application to install.
333      * @param installParam Indicates the install parameters.
334      * @param uid Indicates the uid of the application.
335      * @return Returns ERR_OK if the bundle install successfully; returns error code otherwise.
336      */
337     ErrCode ProcessRecover(
338         const std::string &bundleName, const InstallParam &installParam, int32_t &uid);
339     /**
340      * @brief The real procedure for bundle install by bundleName.
341      * @param bundleName Indicates the bundleName the application to install.
342      * @param installParam Indicates the install parameters.
343      * @param uid Indicates the uid of the application.
344      * @return Returns ERR_OK if the bundle install successfully; returns error code otherwise.
345      */
346     ErrCode ProcessInstallBundleByBundleName(
347         const std::string &bundleName, const InstallParam &installParam, int32_t &uid);
348     /**
349      * @brief The real procedure for bundle install by bundleName.
350      * @param bundleName Indicates the bundleName the application to install.
351      * @param installParam Indicates the install parameters.
352      * @param uid Indicates the uid of the application.
353      * @return Returns ERR_OK if the bundle install successfully; returns error code otherwise.
354      */
355     ErrCode InnerProcessInstallByPreInstallInfo(
356         const std::string &bundleName, const InstallParam &installParam, int32_t &uid);
357     /**
358      * @brief Check syscap.
359      * @param bundlePaths Indicates the file paths of all HAP packages.
360      * @return Returns ERR_OK if the syscap satisfy; returns error code otherwise.
361      */
362     ErrCode CheckSysCap(const std::vector<std::string> &bundlePaths);
363     /**
364      * @brief Check signature info of multiple haps.
365      * @param bundlePaths Indicates the file paths of all HAP packages.
366      * @param installParam Indicates the install parameters.
367      * @param hapVerifyRes Indicates the signature info.
368      * @return Returns ERR_OK if the every hap has signature info and all haps have same signature info.
369      */
370     ErrCode CheckMultipleHapsSignInfo(
371         const std::vector<std::string> &bundlePaths,
372         const InstallParam &installParam,
373         std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes);
374     /**
375      * @brief To parse hap files and to obtain innerBundleInfo of each hap.
376      * @param bundlePaths Indicates the file paths of all HAP packages.
377      * @param installParam Indicates the install parameters.
378      * @param appType Indicates the app type of the hap.
379      * @param hapVerifyRes Indicates all signature info of all haps.
380      * @param infos Indicates the innerBundleinfo of each hap.
381      * @return Returns ERR_OK if each hap is parsed successfully; returns error code otherwise.
382      */
383     ErrCode ParseHapFiles(
384         const std::vector<std::string> &bundlePaths,
385         const InstallParam &installParam,
386         const Constants::AppType appType,
387         std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes,
388         std::unordered_map<std::string, InnerBundleInfo> &infos);
389 
390     ErrCode CheckInstallCondition(std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes,
391         std::unordered_map<std::string, InnerBundleInfo> &infos, bool isSysCapValid);
392 
393     ErrCode CheckInstallPermission(const InstallParam &installParam,
394         std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes);
395     /**
396      * @brief To check dependency whether or not exists.
397      * @param infos Indicates all innerBundleInfo for all haps need to be installed.
398      * @param sharedBundleInstaller Cross-app shared bundle installer
399      * @return Returns ERR_OK if haps checking successfully; returns error code otherwise.
400      */
401     ErrCode CheckDependency(std::unordered_map<std::string, InnerBundleInfo> &infos,
402         const SharedBundleInstaller &sharedBundleInstaller);
403 
404     /**
405      * @brief To check the hap hash param.
406      * @param infos .Indicates all innerBundleInfo for all haps need to be installed.
407      * @param hashParams .Indicates all hashParams in installParam.
408      * @return Returns ERR_OK if haps checking successfully; returns error code otherwise.
409      */
410     ErrCode CheckHapHashParams(
411         std::unordered_map<std::string, InnerBundleInfo> &infos,
412         std::map<std::string, std::string> hashParams);
413     /**
414      * @brief To check the version code and bundleName in all haps.
415      * @param infos .Indicates all innerBundleInfo for all haps need to be installed.
416      * @return Returns ERR_OK if haps checking successfully; returns error code otherwise.
417      */
418     ErrCode CheckAppLabelInfo(const std::unordered_map<std::string, InnerBundleInfo> &infos);
419 
420     /**
421      * @brief send notify to start install applicaiton
422      * @param installParam Indicates the install parameters.
423      * @param infos .Indicates all innerBundleInfo for all haps need to be installed.
424     */
425     void SendStartInstallNotify(const InstallParam &installParam,
426         const std::unordered_map<std::string, InnerBundleInfo> &infos);
427 
428     ErrCode CheckSharedBundleLabelInfo(std::unordered_map<std::string, InnerBundleInfo> &infos);
429     /**
430      * @brief To check native file in all haps.
431      * @param infos .Indicates all innerBundleInfo for all haps need to be installed.
432      * @return Returns ERR_OK if haps checking successfully; returns error code otherwise.
433      */
434     ErrCode CheckMultiNativeFile(
435         std::unordered_map<std::string, InnerBundleInfo> &infos);
436     /**
437      * @brief To roll back when the installation is failed.
438      * @param infos .Indicates the innerBundleInfo needs to be roll back.
439      * @param oldInfo Indicates the original innerBundleInfo of the bundle.
440      * @return Returns ERR_OK if roll back successfully; returns error code otherwise.
441      */
442     void RollBack(const InnerBundleInfo &info, InnerBundleInfo &oldInfo);
443     /**
444      * @brief To check the version code and bundleName in all haps.
445      * @param newInfos .Indicates all innerBundleInfo for all haps need to be rolled back.
446      * @param oldInfo Indicates the original innerBundleInfo of the bundle.
447      * @return Returns ERR_OK if roll back successfully; returns error code otherwise.
448      */
449     void RollBack(const std::unordered_map<std::string, InnerBundleInfo> &newInfos, InnerBundleInfo &oldInfo);
450     /**
451      * @brief To remove innerBundleInfo or moduleInfo of the corresponding haps.
452      * @param bundleName Indicates the bundle name of the bundle which needs to be removed innerBundleInfo or
453      *                   moudleInfo.
454      * @param packageName Indicates the package name of the hap which needs to be removed the moduleInfo.
455      * @return Returns ERR_OK if the removing is successful; returns error code otherwise.
456      */
457     void RemoveInfo(const std::string &bundleName, const std::string &packageName);
458     /**
459      * @brief To roll back the moduleInfo of the corresponding haps.
460      * @param bundleName Indicates the bundle name of the bundle which needs to be rolled back the moudleInfo.
461      * @param oldInfo Indicates the original innerBundleInfo of the bundle.
462      * @return Returns ERR_OK if the rollback is successful; returns error code otherwise.
463      */
464     void RollBackModuleInfo(const std::string &bundleName, InnerBundleInfo &oldInfo);
465     /**
466      * @brief To obtain the innerBundleInfo of the corresponding hap.
467      * @param info Indicates the innerBundleInfo obtained.
468      * @param isAppExist Indicates if the innerBundleInfo is existed or not.
469      * @return Returns ERR_OK if the innerBundleInfo is obtained successfully; returns error code otherwise.
470      */
471     bool GetInnerBundleInfoWithDisable(InnerBundleInfo &info, bool &isAppExist);
472     /**
473      * @brief To check whether the version code is compatible for application or not.
474      * @param oldInfo Indicates the original innerBundleInfo of the bundle.
475      * @return Returns ERR_OK if version code is checked successfully; returns error code otherwise.
476      */
477     ErrCode CheckVersionCompatibility(const InnerBundleInfo &oldInfo);
478     /**
479      * @brief To check whether the version code is compatible for application or not.
480      * @param oldInfo Indicates the original innerBundleInfo of the bundle.
481      * @return Returns ERR_OK if version code is checked successfully; returns error code otherwise.
482      */
483     ErrCode CheckVersionCompatibilityForApplication(const InnerBundleInfo &oldInfo);
484     /**
485      * @brief To check whether the version code is compatible for openharmony service or not.
486      * @param info Indicates the original innerBundleInfo of the bundle.
487      * @return Returns ERR_OK if version code is checked successfully; returns error code otherwise.
488      */
489     ErrCode CheckVersionCompatibilityForHmService(const InnerBundleInfo &oldInfo);
490     /**
491      * @brief To uninstall lower version feature haps.
492      * @param info Indicates the innerBundleInfo of the bundle.
493      * @param packageVec Indicates the array of package names of the high version entry or feature hap.
494      * @return Returns ERR_OK if uninstall successfully; returns error code otherwise.
495      */
496     ErrCode UninstallLowerVersionFeature(const std::vector<std::string> &packageVec, bool killProcess = false);
497     /**
498      * @brief To get userId.
499      * @param installParam Indicates the installParam of the bundle.
500      * @return Returns userId.
501      */
502     int32_t GetUserId(const int32_t &userId) const;
503     /**
504      * @brief Remove bundle user data.
505      * @param innerBundleInfo Indicates the innerBundleInfo of the bundle.
506      * @param needRemoveData Indicates need remove data or not.
507      * @return Returns BundleUserMgr.
508      */
509     ErrCode RemoveBundleUserData(InnerBundleInfo &innerBundleInfo, bool needRemoveData = true);
510     /**
511      * @brief Create bundle user data.
512      * @param innerBundleInfo Indicates the bundle type of the application.
513      * @return Returns ERR_OK if result is ok; returns error code otherwise.
514      */
515     ErrCode CreateBundleUserData(InnerBundleInfo &innerBundleInfo);
516     void AddBundleStatus(const NotifyBundleEvents &installRes);
517     ErrCode CheckInstallationFree(const InnerBundleInfo &innerBundleInfo,
518         const std::unordered_map<std::string, InnerBundleInfo> &infos) const;
519 
520     bool UninstallAppControl(const std::string &appId, int32_t userId);
521 
522     ErrCode InstallNormalAppControl(const std::string &installAppId, int32_t userId, bool isPreInstallApp = false);
523 
524     ErrCode CreateBundleCodeDir(InnerBundleInfo &info) const;
525     ErrCode CreateBundleDataDir(InnerBundleInfo &info) const;
526     ErrCode RemoveBundleCodeDir(const InnerBundleInfo &info) const;
527     ErrCode RemoveBundleDataDir(const InnerBundleInfo &info, bool forException = false) const;
528     void RemoveEmptyDirs(const std::unordered_map<std::string, InnerBundleInfo> &infos) const;
529     std::string GetModuleNames(const std::unordered_map<std::string, InnerBundleInfo> &infos) const;
530     ErrCode UpdateHapToken(bool needUpdate, InnerBundleInfo &newInfo);
531     ErrCode SetDirApl(const InnerBundleInfo &info);
532     /**
533      * @brief Check to set isRemovable true when install.
534      * @param newInfos Indicates all innerBundleInfo for all haps need to be installed.
535      * @param oldInfo Indicates the original innerBundleInfo of the bundle.
536      * @param userId Indicates the userId.
537      * @param isFreeInstallFlag Indicates whether is FREE_INSTALL flag.
538      * @param isAppExist Indicates whether app is exist.
539      * @return
540      */
541     void CheckEnableRemovable(std::unordered_map<std::string, InnerBundleInfo> &newInfos,
542         InnerBundleInfo &oldInfo, int32_t &userId, bool isFreeInstallFlag, bool isAppExist);
543     /**
544      * @brief Save oldInfo isRemovable to newInfo isRemovable.
545      * @param newModuleInfo Indicates the old innerModuleInfo of the bundle..
546      * @param oldInfo Indicates the old innerBundleInfo of the bundle.
547      * @param existModule Indicates whether module is exist.
548      * @return
549      */
550     void SaveOldRemovableInfo(InnerModuleInfo &newModuleInfo, InnerBundleInfo &oldInfo, bool existModule);
551     /**
552      * @brief Save hap path to records.
553      * @param isPreInstallApp Indicates isPreInstallApp or not.
554      * @param infos Indicates all innerBundleInfo for all haps need to be installed.
555      * @return
556      */
557     void SaveHapPathToRecords(
558         bool isPreInstallApp, const std::unordered_map<std::string, InnerBundleInfo> &infos);
559     void OnSingletonChange(bool killProcess);
560     bool AllowSingletonChange(const std::string &bundleName);
561     void MarkPreInstallState(const std::string &bundleName, bool isUninstalled);
562     ErrCode UninstallAllSandboxApps(const std::string &bundleName, int32_t userId = Constants::INVALID_USERID);
563     ErrCode CheckAppLabel(const InnerBundleInfo &oldInfo, const InnerBundleInfo &newInfo) const;
564     bool CheckReleaseTypeIsCompatible(const InnerBundleInfo &oldInfo, const InnerBundleInfo &newInfo) const;
565     void SendBundleSystemEvent(const std::string &bundleName, BundleEventType bundleEventType,
566         const InstallParam &installParam, InstallScene preBundleScene, ErrCode errCode);
567     ErrCode CheckNativeFileWithOldInfo(
568         const InnerBundleInfo &oldInfo, std::unordered_map<std::string, InnerBundleInfo> &newInfos);
569     bool HasAllOldModuleUpdate(
570         const InnerBundleInfo &oldInfo, std::unordered_map<std::string, InnerBundleInfo> &newInfos);
571     ErrCode CheckArkNativeFileWithOldInfo(
572         const InnerBundleInfo &oldInfo, std::unordered_map<std::string, InnerBundleInfo> &newInfos);
573     ErrCode CheckNativeSoWithOldInfo(
574         const InnerBundleInfo &oldInfo, std::unordered_map<std::string, InnerBundleInfo> &newInfos);
575     ErrCode NotifyBundleStatus(const NotifyBundleEvents &installRes);
576     void AddNotifyBundleEvents(const NotifyBundleEvents &notifyBundleEvents);
577     void ProcessHqfInfo(const InnerBundleInfo &oldInfo, const InnerBundleInfo &newInfo) const;
578     ErrCode ProcessDiffFiles(const AppqfInfo &appQfInfo, const std::string &nativeLibraryPath,
579         const std::string &cpuAbi) const;
580     ErrCode ProcessDeployedHqfInfo(const std::string &nativeLibraryPath,
581         const std::string &cpuAbi, const InnerBundleInfo &newInfo, const AppQuickFix &appQuickFix) const;
582     ErrCode ProcessDeployingHqfInfo(
583         const std::string &nativeLibraryPath, const std::string &cpuAbi, const InnerBundleInfo &newInfo) const;
584     ErrCode UpdateLibAttrs(const InnerBundleInfo &newInfo,
585         const std::string &cpuAbi, const std::string &nativeLibraryPath, AppqfInfo &appQfInfo) const;
586     bool CheckHapLibsWithPatchLibs(
587         const std::string &nativeLibraryPath, const std::string &hqfLibraryPath) const;
588     ErrCode ExtractArkNativeFile(InnerBundleInfo &info, const std::string &modulePath);
589     ErrCode DeleteOldArkNativeFile(const InnerBundleInfo &oldInfo);
590     int32_t GetConfirmUserId(
591         const int32_t &userId, std::unordered_map<std::string, InnerBundleInfo> &newInfos);
592     ErrCode CheckUserId(const int32_t &userId) const;
593     ErrCode CreateArkProfile(
594         const std::string &bundleName, int32_t userId, int32_t uid, int32_t gid) const;
595     ErrCode DeleteArkProfile(const std::string &bundleName, int32_t userId) const;
596     ErrCode ExtractArkProfileFile(const std::string &modulePath, const std::string &bundleName,
597         int32_t userId) const;
598     ErrCode ExtractAllArkProfileFile(const InnerBundleInfo &oldInfo, bool checkRepeat = false) const;
599     ErrCode CopyPgoFileToArkProfileDir(const std::string &moduleName, const std::string &modulePath,
600         const std::string &bundleName, int32_t userId) const;
601     ErrCode CopyPgoFile(const std::string &moduleName, const std::string &pgoPath,
602         const std::string &bundleName, int32_t userId) const;
603     ErrCode CheckOverlayInstallation(std::unordered_map<std::string, InnerBundleInfo> &newInfos, int32_t userId);
604     ErrCode CheckOverlayUpdate(const InnerBundleInfo &oldInfo, const InnerBundleInfo &newInfo, int32_t userId) const;
605     NotifyType GetNotifyType();
606     void KillRelatedProcessIfArkWeb(const std::string &bundleName, bool isAppExist, bool isOta);
607     ErrCode CheckAppService(
608         const InnerBundleInfo &newInfo, const InnerBundleInfo &oldInfo, bool isAppExist);
609     ErrCode CheckSingleton(const InnerBundleInfo &newInfo, const int32_t userId);
610     void GetCallingEventInfo(EventInfo &eventInfo);
611     void GetInstallEventInfo(EventInfo &eventInfo);
612     void GetInstallEventInfo(const InnerBundleInfo &bundleInfo, EventInfo &eventInfo);
613     ErrCode CheckArkProfileDir(const InnerBundleInfo &newInfo, const InnerBundleInfo &oldInfo) const;
614     ErrCode ProcessAsanDirectory(InnerBundleInfo &info) const;
615     ErrCode CleanAsanDirectory(InnerBundleInfo &info) const;
616     void AddAppProvisionInfo(const std::string &bundleName,
617         const Security::Verify::ProvisionInfo &provisionInfo, const InstallParam &installParam) const;
618     void UpdateRouterInfo();
619     void DeleteRouterInfo(const std::string &bundleName, const std::string &moduleName = "");
620     ErrCode UninstallHspBundle(std::string &uninstallDir, const std::string &bundleName);
621     ErrCode UninstallHspVersion(std::string &uninstallDir, int32_t versionCode, InnerBundleInfo &info);
622     ErrCode CheckProxyDatas(const std::unordered_map<std::string, InnerBundleInfo> &newInfos);
623     bool CheckDuplicateProxyData(const std::unordered_map<std::string, InnerBundleInfo> &newInfos);
624     bool CheckDuplicateProxyData(const InnerBundleInfo &newInfo, const InnerBundleInfo &oldInfo);
625     bool CheckDuplicateProxyData(const std::vector<ProxyData> &proxyDatas);
626     bool CheckApiInfo(const std::unordered_map<std::string, InnerBundleInfo> &infos);
627     ErrCode InnerProcessNativeLibs(InnerBundleInfo &info, const std::string &modulePath);
628     ErrCode CheckSoEncryption(InnerBundleInfo &info, const std::string &cpuAbi, const std::string &targetSoPath);
629     bool ExtractSoFiles(const std::string &soPath, const std::string &cpuAbi) const;
630     void ProcessOldNativeLibraryPath(const std::unordered_map<std::string, InnerBundleInfo> &newInfos,
631         uint32_t oldVersionCode, const std::string &oldNativeLibraryPath) const;
632     void ProcessAOT(bool isOTA, const std::unordered_map<std::string, InnerBundleInfo> &infos) const;
633     void RemoveOldHapIfOTA(const InstallParam &installParam,
634         const std::unordered_map<std::string, InnerBundleInfo> &newInfos, const InnerBundleInfo &oldInfo) const;
635     ErrCode CopyHapsToSecurityDir(const InstallParam &installParam, std::vector<std::string> &bundlePaths);
636     ErrCode RenameAllTempDir(const std::unordered_map<std::string, InnerBundleInfo> &newInfos) const;
637     ErrCode FindSignatureFileDir(const std::string &moduleName, std::string &signatureFileDir);
638     ErrCode MoveFileToRealInstallationDir(const std::unordered_map<std::string, InnerBundleInfo> &infos);
639     std::string GetTempHapPath(const InnerBundleInfo &info);
640     ErrCode SaveHapToInstallPath(const std::unordered_map<std::string, InnerBundleInfo> &infos);
641     ErrCode CheckHapEncryption(const std::unordered_map<std::string, InnerBundleInfo> &infos);
642     void UpdateAppInstallControlled(int32_t userId);
643     ErrCode MoveSoFileToRealInstallationDir(const std::unordered_map<std::string, InnerBundleInfo> &infos);
644     void ProcessDataGroupInfo(const std::vector<std::string> &bundlePaths,
645         std::unordered_map<std::string, InnerBundleInfo> &infos,
646         int32_t userId, const std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes);
647     ErrCode GetGroupDirsChange(const InnerBundleInfo &info, const InnerBundleInfo &oldInfo, bool oldInfoExisted);
648     ErrCode GetRemoveDataGroupDirs(const InnerBundleInfo &oldInfo, const InnerBundleInfo &newInfo);
649     ErrCode RemoveOldGroupDirs() const;
650     ErrCode CreateGroupDirs() const;
651     void CreateDataGroupDir(InnerBundleInfo &info) const;
652     ErrCode GetDataGroupCreateInfos(const InnerBundleInfo &newInfo);
653     ErrCode RemoveDataGroupDirs(const std::string &bundleName, int32_t userId, bool isKeepData = false) const;
654     void DeleteGroupDirsForException() const;
655     ErrCode CreateDataGroupDirs(
656         const std::unordered_map<std::string, InnerBundleInfo> &newInfos, const InnerBundleInfo &oldInfo);
657     bool NeedDeleteOldNativeLib(
658         std::unordered_map<std::string, InnerBundleInfo> &newInfos,
659         const InnerBundleInfo &oldInfo);
660     ErrCode UninstallBundleFromBmsExtension(const std::string &bundleName);
661     ErrCode CheckBundleInBmsExtension(const std::string &bundleName, int32_t userId);
662     ErrCode CheckMDMUpdateBundleForSelf(const InstallParam &installParam, InnerBundleInfo &oldInfo,
663         const std::unordered_map<std::string, InnerBundleInfo> &newInfos, bool isAppExist);
664     void ExtractResourceFiles(const InnerBundleInfo &info, const std::string &targetPath) const;
665     void RemoveTempSoDir(const std::string &tempSoDir);
666     bool CheckAppIdentifier(InnerBundleInfo &oldInfo, InnerBundleInfo &newInfo);
667     ErrCode InstallEntryMoudleFirst(std::unordered_map<std::string, InnerBundleInfo> &newInfos,
668         InnerBundleInfo &bundleInfo, const InnerBundleUserInfo &innerBundleUserInfo, const InstallParam &installParam);
669     void ProcessQuickFixWhenInstallNewModule(const InstallParam &installParam,
670         const std::unordered_map<std::string, InnerBundleInfo> &newInfos);
671     bool ExtractEncryptedSoFiles(const InnerBundleInfo &info, const std::string &tmpSoPath, int32_t uid) const;
672     void SetOldAppIsEncrypted(const InnerBundleInfo &oldInfo);
673     void GetAllConeCodeProtectBundleInfos(std::vector<CodeProtectBundleInfo> &infos,
674         const InnerBundleInfo &innerBundleInfo);
675     bool UpdateEncryptedStatus();
676     bool DeleteEncryptedStatus(const std::string &bundleName, int32_t uid);
677     void ProcessEncryptedKeyExisted(int32_t res, uint32_t type,
678         const std::vector<CodeProtectBundleInfo> &infos);
679     ErrCode VerifyCodeSignatureForNativeFiles(InnerBundleInfo &info, const std::string &cpuAbi,
680         const std::string &targetSoPath, const std::string &signatureFileDir) const;
681     ErrCode VerifyCodeSignatureForHap(const std::unordered_map<std::string, InnerBundleInfo> &infos,
682         const std::string &srcHapPath, const std::string &realHapPath);
683     ErrCode DeliveryProfileToCodeSign() const;
684     ErrCode RemoveProfileFromCodeSign(const std::string &bundleName) const;
685     ErrCode ExtractResFileDir(const std::string &modulePath) const;
686     ErrCode ExtractHnpFileDir(const std::string &cpuAbi, const std::string &hnpPackageInfoString,
687         const std::string &modulePath) const;
688     void DeleteOldNativeLibraryPath() const;
689     void RemoveTempPathOnlyUsedForSo(const InnerBundleInfo &innerBundleInfo) const;
690     void GenerateOdid(std::unordered_map<std::string, InnerBundleInfo> &infos,
691         const std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes) const;
692     void SetAppDistributionType(const std::unordered_map<std::string, InnerBundleInfo> &infos);
693     ErrCode CreateShaderCache(const std::string &bundleName, int32_t uid, int32_t gid) const;
694     ErrCode DeleteShaderCache(const std::string &bundleName) const;
695     ErrCode CleanShaderCache(const std::string &bundleName) const;
696     void CreateCloudShader(const std::string &bundleName, int32_t uid, int32_t gid) const;
697     bool VerifyActivationLock() const;
698     std::vector<std::string> GenerateScreenLockProtectionDir(const std::string &bundleName) const;
699     void CreateScreenLockProtectionDir();
700     void DeleteScreenLockProtectionDir(const std::string bundleName) const;
701     bool SetEncryptionDirPolicy(InnerBundleInfo &info);
702     void DeleteEncryptionKeyId(const InnerBundleInfo &oldInfo, bool isKeepData) const;
703     void CreateScreenLockProtectionExistDirs(const InnerBundleInfo &info, const std::string &dir);
704 #ifdef APP_DOMAIN_VERIFY_ENABLED
705     void PrepareSkillUri(const std::vector<Skill> &skills, std::vector<AppDomainVerify::SkillUri> &skillUris) const;
706 #endif
707     void PrepareBundleDirQuota(const std::string &bundleName, const int32_t uid,
708         const std::string &bundleDataDirPath, const int32_t limitSize) const;
709     void VerifyDomain();
710     void ClearDomainVerifyStatus(const std::string &appIdentifier, const std::string &bundleName) const;
711     void SetAtomicServiceModuleUpgrade(const InnerBundleInfo &oldInfo);
712     void UpdateExtensionSandboxInfo(std::unordered_map<std::string, InnerBundleInfo> &newInfos,
713         const std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes);
714     void GetValidDataGroupIds(const std::vector<std::string> &extensionDataGroupIds,
715         const std::vector<std::string> &bundleDataGroupIds, std::vector<std::string> &validGroupIds) const;
716     void GetExtensionDirsChange(std::unordered_map<std::string, InnerBundleInfo> &newInfos,
717         const InnerBundleInfo &oldInfo);
718     void GetCreateExtensionDirs(std::unordered_map<std::string, InnerBundleInfo> &newInfos);
719     void GetRemoveExtensionDirs(
720         std::unordered_map<std::string, InnerBundleInfo> &newInfos, const InnerBundleInfo &oldInfo);
721     void CreateExtensionDataDir(InnerBundleInfo &info) const;
722     void RemoveCreatedExtensionDirsForException() const;
723     void RemoveOldExtensionDirs() const;
724     ErrCode InnerProcessUpdateHapToken(const bool isOldSystemApp);
725     bool InitDataMgr();
726     std::string GetInstallSource(const InstallParam &installParam) const;
727     void SetInstallSourceToAppInfo(std::unordered_map<std::string, InnerBundleInfo> &infos,
728         const InstallParam &installParam) const;
729     void SetApplicationFlagsForPreinstallSource(std::unordered_map<std::string, InnerBundleInfo> &infos,
730         const InstallParam &installParam) const;
731     bool IsAppInBlocklist(const std::string &bundleName, const int32_t userId) const;
732     bool CheckWhetherCanBeUninstalled(const std::string &bundleName) const;
733     void CheckSystemFreeSizeAndClean() const;
734     void CheckBundleNameAndStratAbility(const std::string &bundleName, const std::string &appIdentifier) const;
735     void GetUninstallBundleInfo(bool isKeepData, int32_t userId,
736         const InnerBundleInfo &oldInfo, UninstallBundleInfo &uninstallBundleInfo);
737     bool CheckInstallOnKeepData(const std::string &bundleName, bool isOTA,
738         const std::unordered_map<std::string, InnerBundleInfo> &infos);
739     void SaveUninstallBundleInfo(const std::string bundleName, bool isKeepData,
740         const UninstallBundleInfo &uninstallBundleInfo);
741     void DeleteUninstallBundleInfo(const std::string &bundleName);
742     void MarkInstallFinish();
743     bool IsArkWeb(const std::string &bundleName) const;
744 #ifdef WEBVIEW_ENABLE
745     ErrCode VerifyArkWebInstall(const std::string &bundleName);
746 #endif
747 
748     bool SetDisposedRuleWhenBundleUpdateStart(const std::unordered_map<std::string, InnerBundleInfo> &infos,
749         const InnerBundleInfo &oldBundleInfo, bool isPreInstallApp);
750 
751     bool DeleteDisposedRuleWhenBundleUpdateEnd(const InnerBundleInfo &oldBundleInfo);
752 
753     InstallerState state_ = InstallerState::INSTALL_START;
754     std::shared_ptr<BundleDataMgr> dataMgr_ = nullptr;  // this pointer will get when public functions called
755     std::string bundleName_;
756     std::string moduleTmpDir_;
757     std::string modulePath_;
758     std::string baseDataPath_;
759     std::string modulePackage_;
760     std::string mainAbility_;
761     // key is package name, value is boolean
762     std::unordered_map<std::string, bool> installedModules_;
763     bool isAppExist_ = false;
764     bool isContainEntry_ = false;
765     uint32_t versionCode_ = 0;
766     uint32_t accessTokenId_ = 0;
767     bool isAppService_ = false;
768     // value is packageName for uninstalling
769     bool isFeatureNeedUninstall_ = false;
770     std::vector<std::string> uninstallModuleVec_;
771     // for quick fix
772     bool needDeleteQuickFixInfo_ = false;
773     uint32_t oldApplicationReservedFlag_ = 0;
774 
775     int32_t userId_ = Constants::INVALID_USERID;
776     bool hasInstalledInUser_ = false;
777     SingletonState singletonState_ = SingletonState::DEFAULT;
778     std::map<std::string, std::string> hapPathRecords_;
779     // used to record system event infos
780     EventInfo sysEventInfo_;
781     std::unique_ptr<BundleInstallChecker> bundleInstallChecker_ = nullptr;
782     int32_t overlayType_ = NON_OVERLAY_TYPE;
783     std::string moduleName_;
784     // utilizing for code-signature
785     std::map<std::string, std::string> verifyCodeParams_;
786     std::vector<std::string> toDeleteTempHapPath_;
787     std::vector<NotifyBundleEvents> bundleEvents_;
788     // key is the temp path of hap or hsp
789     // value is the signature file path
790     std::map<std::string, std::string> signatureFileMap_;
791     std::vector<DataGroupInfo> createGroupDirs_;
792     std::vector<std::string> removeGroupDirs_;
793     std::vector<std::string> bundlePaths_;
794     std::unordered_map<std::string, std::string> signatureFileTmpMap_;
795     std::string uninstallBundleAppId_;
796     bool isModuleUpdate_ = false;
797     BundleType bundleType_ = BundleType::APP;
798     int32_t atomicServiceModuleUpgrade_ = 0;
799     // utilize for install entry firstly from multi-installation
800     bool isEntryInstalled_ = false;
801     std::string entryModuleName_ = "";
802     std::map<std::string, std::string> pgoParams_;
803     bool isEnterpriseBundle_ = false;
804     bool isInternaltestingBundle_ = false;
805     std::string appIdentifier_ = "";
806     // When it is true, it means that the same bundleName and same userId was uninstalled with keepData before
807     bool existBeforeKeepDataApp_ = false;
808     Security::Verify::HapVerifyResult verifyRes_;
809     std::map<std::string, std::string> targetSoPathMap_;
810     bool copyHapToInstallPath_ = false;
811     std::string appDistributionType_;
812     // indicates sandboxd dirs need to create by extension
813     std::vector<std::string> newExtensionDirs_;
814     // indicates sandboxd dirs need to create by extension
815     std::vector<std::string> createExtensionDirs_;
816     // indicates sandboxd dirs need to remove by extension
817     std::vector<std::string> removeExtensionDirs_;
818     bool needSetDisposeRule_ = false;
819 
820     DISALLOW_COPY_AND_MOVE(BaseBundleInstaller);
821 
822 #define CHECK_RESULT(errcode, errmsg)                                              \
823     do {                                                                           \
824         if ((errcode) != ERR_OK) {                                                   \
825             APP_LOGE(errmsg, errcode);                                             \
826             return errcode;                                                        \
827         }                                                                          \
828     } while (0)
829 
830 #define CHECK_RESULT_WITH_ROLLBACK(errcode, errmsg, newInfos, oldInfo)             \
831     do {                                                                           \
832         if ((errcode) == ERR_APPEXECFWK_INSTALL_SINGLETON_NOT_SAME ||              \
833             (errcode) == ERR_APPEXECFWK_INSTALL_ZERO_USER_WITH_NO_SINGLETON) {     \
834             APP_LOGE(errmsg, errcode);                                             \
835             return errcode;                                                        \
836         }                                                                          \
837                                                                                    \
838         if ((errcode) != ERR_OK) {                                                   \
839             APP_LOGE(errmsg, errcode);                                             \
840             RollBack(newInfos, oldInfo);                                           \
841             return errcode;                                                        \
842         }                                                                          \
843     } while (0)
844 };
845 }  // namespace AppExecFwk
846 }  // namespace OHOS
847 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BASE_BUNDLE_INSTALLER_H