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_INNER_SHARED_BUNDLE_INSTALLER_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INNER_SHARED_BUNDLE_INSTALLER_H
18 
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include "bundle_install_checker.h"
24 #include "bundle_common_event_mgr.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 InnerSharedBundleInstaller {
33 public:
34     /**
35      * @brief Cross-app shared bundle installer for hsp files of same bundle.
36      * @param path Indicates the real path or the parent directory of hsp files to be installed.
37      */
38     explicit InnerSharedBundleInstaller(const std::string &path);
39     virtual ~InnerSharedBundleInstaller();
40 
41     /**
42      * @brief Parse cross-app hsp files of same bundle.
43      * @param checkParam Indicates the install check param.
44      * @return Returns ERR_OK if the files are parsed successfully; returns error code otherwise.
45      */
46     ErrCode ParseFiles(const InstallCheckParam &checkParam);
47 
48     /**
49      * @brief Get the bundle name of current shared bundle to be installed.
50      * @return Returns the bundle name of current shared bundle to be installed.
51      */
GetBundleName()52     inline std::string GetBundleName() const
53     {
54         return bundleName_;
55     }
56 
57     /**
58      * @brief Install cross-app shared bundles of same bundle.
59      * @param installParam Indicates the install param.
60      * @return Returns ERR_OK if the files are installed successfully; returns error code otherwise.
61      */
62     ErrCode Install(const InstallParam &installParam);
63 
64     /**
65      * @brief Roll back the install action.
66     */
67     void RollBack();
68 
69     /**
70      * @brief Checks whether the dependency is satisfied by current installing shared bundle.
71      * @param dependency Indicates the dependency to be checked.
72      * @return Returns true if the dependency is satisfied; returns false otherwise.
73      */
74     bool CheckDependency(const Dependency &dependency) const;
75 
76     void SetCheckResultMsg(const std::string checkResultMsg) const;
77 
78     /**
79      * @brief Send bundle system event.
80      * @param eventTemplate Indicates the template of EventInfo to send after installation.
81      */
82     void SendBundleSystemEvent(const EventInfo &eventTemplate) const;
83 
84     /**
85      * @brief send notify to start install applicaiton
86      * @param installParam Indicates the install parameters.
87      * @param infos .Indicates all innerBundleInfo for all haps need to be installed.
88     */
89     void sendStartSharedBundleInstallNotify(const InstallCheckParam &installCheckParam,
90         const std::unordered_map<std::string, InnerBundleInfo> &infos);
91 
92     ErrCode NotifyBundleStatusOfShared(const NotifyBundleEvents &installRes);
93 
94     ErrCode DeliveryProfileToCodeSign(std::vector<Security::Verify::HapVerifyResult> &hapVerifyResults) const;
95 
96 private:
97     ErrCode CheckAppLabelInfo();
98     ErrCode CheckBundleTypeWithInstalledVersion();
99     ErrCode ExtractSharedBundles(const std::string &bundlePath, InnerBundleInfo &newInfo);
100     ErrCode MkdirIfNotExist(const std::string &dir);
101     void MergeBundleInfos();
102     ErrCode SavePreInstallInfo(const InstallParam &installParam);
103     ErrCode SaveBundleInfoToStorage();
104     void GetInstallEventInfo(EventInfo &eventInfo) const;
105     void AddAppProvisionInfo(const std::string &bundleName,
106         const Security::Verify::ProvisionInfo &provisionInfo) const;
107     void SaveInstallParamInfo(const std::string &bundleName, const InstallParam &installParam) const;
108     ErrCode CopyHspToSecurityDir(std::vector<std::string> &bundlePaths);
109     ErrCode ObtainHspFileAndSignatureFilePath(const std::vector<std::string> &inBundlePaths,
110         std::vector<std::string> &bundlePaths, std::string &signatureFilePath);
111     ErrCode SaveHspToRealInstallationDir(const std::string &bundlePath, const std::string &moduleDir,
112         const std::string &moduleName, const std::string &realHspPath);
113     std::string ObtainTempSoPath(const std::string &moduleName, const std::string &nativeLibPath);
114     ErrCode MoveSoToRealPath(const std::string &moduleName, const std::string &versionDir);
115     ErrCode ProcessNativeLibrary(const std::string &bundlePath,
116         const std::string &moduleDir, const std::string &moduleName, const std::string &versionDir,
117         InnerBundleInfo &newInfo);
118     ErrCode VerifyCodeSignatureForNativeFiles(const std::string &bundlePath, const std::string &cpuAbi,
119         const std::string &targetSoPath, const std::string &signatureFileDir, bool isPreInstalledBundle) const;
120     ErrCode VerifyCodeSignatureForHsp(const std::string &tempHspPath, const std::string &appIdentifier,
121         bool isEnterpriseBundle, bool isCompileSdkOpenHarmony, const std::string &bundleName) const;
122     void UpdateInnerModuleInfo(const std::string packageName, const InnerModuleInfo &innerModuleInfo);
123     ErrCode MarkInstallFinish();
124 
125     // the real path or the parent directory of hsp files to be installed.
126     std::string sharedBundlePath_;
127     std::string bundleName_;
128     std::string signatureFileDir_;
129     std::vector<std::string> toDeleteTempHspPath_;
130     // the key is the real path of each hsp file
131     std::unordered_map<std::string, InnerBundleInfo> parsedBundles_;
132     // created directories during installation, will be deleted when rollback.
133     std::vector<std::string> createdDirs_;
134     bool isBundleExist_ = false;
135     InnerBundleInfo oldBundleInfo_;
136     InnerBundleInfo newBundleInfo_;
137     std::unique_ptr<BundleInstallChecker> bundleInstallChecker_ = nullptr;
138     std::string nativeLibraryPath_;
139     bool isEnterpriseBundle_ = false;
140     std::string appIdentifier_;
141     std::string compileSdkType_;
142     std::string cpuAbi_;
143     std::string tempSoPath_;
144     bool isPreInstalledBundle_ = false;
145 
146     DISALLOW_COPY_AND_MOVE(InnerSharedBundleInstaller);
147 
148 #define CHECK_RESULT(errcode, errmsg)                                              \
149     do {                                                                           \
150         if ((errcode) != ERR_OK) {                                                   \
151             APP_LOGE(errmsg, errcode);                                             \
152             return errcode;                                                        \
153         }                                                                          \
154     } while (0)
155 };
156 }  // namespace AppExecFwk
157 }  // namespace OHOS
158 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INNER_SHARED_BUNDLE_INSTALLER_H