1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_APP_SERVICE_FWK_INSTALLER_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_APP_SERVICE_FWK_INSTALLER_H
18 
19 #include <memory>
20 #include <string>
21 #include <unordered_map>
22 
23 #include "bundle_data_mgr.h"
24 #include "bundle_install_checker.h"
25 #include "event_report.h"
26 #include "inner_bundle_info.h"
27 #include "install_param.h"
28 #include "nocopyable.h"
29 
30 namespace OHOS {
31 namespace AppExecFwk {
32 class AppServiceFwkInstaller {
33 public:
34     AppServiceFwkInstaller();
35     virtual ~AppServiceFwkInstaller();
36 
37     ErrCode Install(
38         const std::vector<std::string> &hspPaths, InstallParam &installParam);
39     ErrCode UnInstall(const std::string &bundleName, bool isKeepData = false);
40 private:
41     void ResetProperties();
42     ErrCode BeforeInstall(
43         const std::vector<std::string> &hspPaths, InstallParam &installParam);
44     ErrCode BeforeUninstall(const std::string &bundleName);
45     ErrCode ProcessInstall(
46         const std::vector<std::string> &hspPaths, InstallParam &installParam);
47     ErrCode CheckAndParseFiles(
48         const std::vector<std::string> &hspPaths, InstallParam &installParam,
49         std::unordered_map<std::string, InnerBundleInfo> &newInfos);
50     ErrCode InnerProcessInstall(
51         std::unordered_map<std::string, InnerBundleInfo> &newInfos,
52         InstallParam &installParam);
53     ErrCode CheckAppLabelInfo(
54         const std::unordered_map<std::string, InnerBundleInfo> &infos);
55     ErrCode CheckFileType(const std::vector<std::string> &bundlePaths);
56     void SendBundleSystemEvent(
57         const std::vector<std::string> &hspPaths, BundleEventType bundleEventType,
58         const InstallParam &installParam, InstallScene preBundleScene, ErrCode errCode);
59     ErrCode ExtractModule(
60         InnerBundleInfo &newInfo, const std::string &bundlePath, bool copyHapToInstallPath = false);
61     ErrCode ExtractModule(InnerBundleInfo &oldInfo, InnerBundleInfo &newInfo, const std::string &bundlePath);
62     ErrCode MkdirIfNotExist(const std::string &dir);
63     ErrCode ProcessNativeLibrary(
64         const std::string &bundlePath,
65         const std::string &moduleDir,
66         const std::string &moduleName,
67         const std::string &versionDir,
68         InnerBundleInfo &newInfo,
69         bool copyHapToInstallPath = false);
70     ErrCode MoveSoToRealPath(
71         const std::string &moduleName, const std::string &versionDir,
72         const std::string &nativeLibraryPath);
73     void MergeBundleInfos(InnerBundleInfo &info);
74     ErrCode SaveBundleInfoToStorage();
75     void AddAppProvisionInfo(
76         const std::string &bundleName,
77         const Security::Verify::ProvisionInfo &provisionInfo,
78         const InstallParam &installParam) const;
79 
80     void RollBack();
81     ErrCode RemoveBundleCodeDir(const InnerBundleInfo &info) const;
82     void RemoveInfo(const std::string &bundleName);
83     void SavePreInstallBundleInfo(ErrCode installResult,
84         const std::unordered_map<std::string, InnerBundleInfo> &newInfos, const InstallParam &installParam);
85     ErrCode UpdateAppService(InnerBundleInfo &oldInfo,
86         std::unordered_map<std::string, InnerBundleInfo> &newInfos,
87         InstallParam &installParam);
88     ErrCode UninstallLowerVersion(const std::vector<std::string> &moduleNameList);
89     bool GetInnerBundleInfo(InnerBundleInfo &info, bool &isAppExist);
90     bool CheckNeedInstall(const std::unordered_map<std::string, InnerBundleInfo> &infos, InnerBundleInfo &oldInfo);
91     bool CheckNeedUpdate(const InnerBundleInfo &newInfo, const InnerBundleInfo &oldInfo);
92     ErrCode ProcessBundleUpdateStatus(InnerBundleInfo &oldInfo, InnerBundleInfo &newInfo,
93         const std::string &hspPath, const InstallParam &installParam);
94     ErrCode ProcessNewModuleInstall(InnerBundleInfo &newInfo, InnerBundleInfo &oldInfo,
95         const std::string &hspPath, const InstallParam &installParam);
96     ErrCode ProcessModuleUpdate(InnerBundleInfo &newInfo, InnerBundleInfo &oldInfo,
97         const std::string &hspPath, const InstallParam &installParam);
98     ErrCode RemoveLowerVersionSoDir(uint32_t versionCode);
99     ErrCode VerifyCodeSignatureForNativeFiles(const std::string &bundlePath, const std::string &cpuAbi,
100         const std::string &targetSoPath) const;
101     ErrCode DeliveryProfileToCodeSign(std::vector<Security::Verify::HapVerifyResult> &hapVerifyResults) const;
102     void GenerateOdid(std::unordered_map<std::string, InnerBundleInfo> &infos,
103         const std::vector<Security::Verify::HapVerifyResult> &hapVerifyRes) const;
104     ErrCode VerifyCodeSignatureForHsp(const std::string &realHspPath, const std::string &realSoPath) const;
105     ErrCode MarkInstallFinish();
106 
107     std::unique_ptr<BundleInstallChecker> bundleInstallChecker_ = nullptr;
108     std::shared_ptr<BundleDataMgr> dataMgr_ = nullptr;
109     std::string bundleName_;
110     std::string bundleMsg_;
111     std::vector<std::string> uninstallModuleVec_;
112     bool versionUpgrade_ = false;
113     bool moduleUpdate_ = false;
114     std::vector<std::string> deleteBundlePath_;
115     uint32_t versionCode_ = 0;
116     InnerBundleInfo newInnerBundleInfo_;
117     bool isEnterpriseBundle_ = false;
118     std::string appIdentifier_;
119     std::string compileSdkType_;
120     std::string cpuAbi_;
121     std::string nativeLibraryPath_;
122     DISALLOW_COPY_AND_MOVE(AppServiceFwkInstaller);
123 
124 #define CHECK_RESULT(errcode, errmsg)                                              \
125     do {                                                                           \
126         if ((errcode) != ERR_OK) {                                                   \
127             APP_LOGE(errmsg, errcode);                                             \
128             return errcode;                                                        \
129         }                                                                          \
130     } while (0)
131 };
132 }  // namespace AppExecFwk
133 }  // namespace OHOS
134 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_APP_SERVICE_FWK_INSTALLER_H