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