• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <string>
20 
21 #include "nocopyable.h"
22 
23 #include "appexecfwk_errors.h"
24 #include "inner_bundle_info.h"
25 #include "install_param.h"
26 #include "bundle_data_mgr.h"
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 
31 class BaseBundleInstaller {
32 public:
33     BaseBundleInstaller();
34     virtual ~BaseBundleInstaller();
35 
36 protected:
37     enum class InstallerState {
38         INSTALL_START,
39         INSTALL_BUNDLE_CHECKED = 5,
40         INSTALL_PARSED = 10,
41         INSTALL_SIGNATURE_CHECKED = 15,
42         INSTALL_PERMS_REQ = 20,
43         INSTALL_CREATDIR = 40,
44         INSTALL_EXTRACTED = 60,
45         INSTALL_RENAMED = 80,
46         INSTALL_INFO_SAVED = 90,
47         INSTALL_SUCCESS = 100,
48         INSTALL_FAILED,
49     };
50 
51     /**
52      * @brief The main function for system and normal bundle install.
53      * @param bundlePath Indicates the path for storing the HAP file of the application
54      *                   to install or update.
55      * @param installParam Indicates the install parameters.
56      * @param appType Indicates the application type.
57      * @return Returns ERR_OK if the application install successfully; returns error code otherwise.
58      */
59     ErrCode InstallBundle(
60         const std::string &bundlePath, const InstallParam &installParam, const Constants::AppType appType);
61     /**
62      * @brief The main function for uninstall a bundle.
63      * @param bundleName Indicates the bundle name of the application to uninstall.
64      * @param installParam Indicates the uninstall parameters.
65      * @return Returns ERR_OK if the application uninstall successfully; returns error code otherwise.
66      */
67     ErrCode UninstallBundle(const std::string &bundleName, const InstallParam &installParam);
68     /**
69      * @brief The main function for uninstall a module in a specific bundle.
70      * @param bundleName Indicates the bundle name of the application to uninstall.
71      * @param modulePackage Indicates the module package of the module to uninstall.
72      * @param installParam Indicates the uninstall parameters.
73      * @return Returns ERR_OK if the application uninstall successfully; returns error code otherwise.
74      */
75     ErrCode UninstallBundle(
76         const std::string &bundleName, const std::string &modulePackage, const InstallParam &installParam);
77     /**
78      * @brief Update the installer state.
79      * @attention This function changes the base class state only.
80      * @param state Indicates the state to be updated to.
81      * @return
82      */
83     virtual void UpdateInstallerState(const InstallerState state);
84     /**
85      * @brief Get the installer state.
86      * @return The current state of the installer object.
87      */
GetInstallerState()88     inline InstallerState GetInstallerState()
89     {
90         return state_;
91     }
92     /**
93      * @brief Get the installer state.
94      * @param state Indicates the state to be updated to.
95      * @return
96      */
SetInstallerState(InstallerState state)97     inline void SetInstallerState(InstallerState state)
98     {
99         state_ = state;
100     }
101 
102 private:
103     /**
104      * @brief The real procedure for system and normal bundle install.
105      * @param bundlePath Indicates the path for storing the HAP file of the application
106      *                   to install or update.
107      * @param installParam Indicates the install parameters.
108      * @param appType Indicates the application type.
109      * @param uid Indicates the uid of the application.
110      * @return Returns ERR_OK if the bundle install successfully; returns error code otherwise.
111      */
112     ErrCode ProcessBundleInstall(const std::string &bundlePath, const InstallParam &installParam,
113         const Constants::AppType appType, int32_t &uid);
114     /**
115      * @brief The real procedure function for uninstall a bundle.
116      * @param bundleName Indicates the bundle name of the application to uninstall.
117      * @param installParam Indicates the uninstall parameters.
118      * @param uid Indicates the uid of the application.
119      * @return Returns ERR_OK if the bundle uninstall successfully; returns error code otherwise.
120      */
121     ErrCode ProcessBundleUninstall(const std::string &bundleName, const InstallParam &installParam, int32_t &uid);
122     /**
123      * @brief The real procedure for uninstall a module in a specific bundle.
124      * @param bundleName Indicates the bundle name of the application to uninstall.
125      * @param modulePackage Indicates the module package of the module to uninstall.
126      * @param installParam Indicates the uninstall parameters.
127      * @param uid Indicates the uid of the application.
128      * @return Returns ERR_OK if the module uninstall successfully; returns error code otherwise.
129      */
130     ErrCode ProcessBundleUninstall(const std::string &bundleName, const std::string &modulePackage,
131         const InstallParam &installParam, int32_t &uid);
132     /**
133      * @brief The process of installing a new bundle.
134      * @param info Indicates the InnerBundleInfo parsed from the config.json in the HAP package.
135      * @param uid Indicates the uid of the application.
136      * @return Returns ERR_OK if the new bundle install successfully; returns error code otherwise.
137      */
138     ErrCode ProcessBundleInstallStatus(InnerBundleInfo &info, int32_t &uid);
139     /**
140      * @brief The process of updating an exist bundle.
141      * @param oldInfo Indicates the exist InnerBundleInfo object get from the database.
142      * @param newInfo Indicates the InnerBundleInfo object parsed from the config.json in the HAP package.
143      * @param isReplace Indicates whether there is the replace flag in the install flag.
144      * @return Returns ERR_OK if the bundle updating successfully; returns error code otherwise.
145      */
146     ErrCode ProcessBundleUpdateStatus(InnerBundleInfo &oldInfo, InnerBundleInfo &newInfo, bool isReplace);
147     /**
148      * @brief Remove a whole bundle.
149      * @param info Indicates the InnerBundleInfo object of a bundle.
150      * @return Returns ERR_OK if the bundle removed successfully; returns error code otherwise.
151      */
152     ErrCode RemoveBundle(InnerBundleInfo &info);
153     /**
154      * @brief Create the code and data directories of a bundle.
155      * @param info Indicates the InnerBundleInfo object of a bundle.
156      * @return Returns ERR_OK if the bundle directories created successfully; returns error code otherwise.
157      */
158     ErrCode CreateBundleAndDataDir(InnerBundleInfo &info) const;
159     /**
160      * @brief Extract the code to temporilay directory and rename it.
161      * @param info Indicates the InnerBundleInfo object of a bundle.
162      * @return Returns ERR_OK if the bundle extract and renamed successfully; returns error code otherwise.
163      */
164     ErrCode ExtractModuleAndRename(InnerBundleInfo &info);
165     /**
166      * @brief Remove the code and data directories of a bundle.
167      * @param info Indicates the InnerBundleInfo object of a bundle.
168      * @param isUninstall Indicates that whether the remove is in an uninstall process.
169      * @return Returns ERR_OK if the bundle directories removed successfully; returns error code otherwise.
170      */
171     ErrCode RemoveBundleAndDataDir(InnerBundleInfo &info, bool isUninstall) const;
172     /**
173      * @brief Remove the code and data directories of a module in a bundle.
174      * @param info Indicates the InnerBundleInfo object of a bundle.
175      * @param modulePackage Indicates the module to be removed.
176      * @return Returns ERR_OK if the bundle directories removed successfully; returns error code otherwise.
177      */
178     ErrCode RemoveModuleAndDataDir(InnerBundleInfo &info, const std::string &modulePackage) const;
179     /**
180      * @brief Parse the bundle config.json file.
181      * @param bundleFilePath Indicates the HAP file path.
182      * @param InnerBundleInfo Indicates the InnerBundleInfo object of a bundle.
183      * @return Returns ERR_OK if the bundle parsed successfully; returns error code otherwise.
184      */
185     ErrCode ParseBundleInfo(const std::string &bundleFilePath, InnerBundleInfo &info) const;
186     /**
187      * @brief Remove the current installing module directory.
188      * @param info Indicates the InnerBundleInfo object of a bundle under installing.
189      * @return Returns ERR_OK if the module directory removed successfully; returns error code otherwise.
190      */
191     ErrCode RemoveModuleDir(InnerBundleInfo &info) const;
192     /**
193      * @brief Extract files of the current installing module package.
194      * @param info Indicates the InnerBundleInfo object of a bundle under installing.
195      * @return Returns ERR_OK if the module files extraced successfully; returns error code otherwise.
196      */
197     ErrCode ExtractModuleFiles(InnerBundleInfo &info);
198     /**
199      * @brief Create the data directories of current installing module package.
200      * @param info Indicates the InnerBundleInfo object of a bundle under installing.
201      * @return Returns ERR_OK if the module directory created successfully; returns error code otherwise.
202      */
203     ErrCode CreateModuleDataDir(InnerBundleInfo &info) const;
204     /**
205      * @brief Remove the data directories of current installing module package.
206      * @param info Indicates the InnerBundleInfo object of a bundle under installing.
207      * @return Returns ERR_OK if the module directory removed successfully; returns error code otherwise.
208      */
209     ErrCode RemoveModuleDataDir(InnerBundleInfo &info) const;
210     /**
211      * @brief Rename the directory of current installing module package.
212      * @param info Indicates the InnerBundleInfo object of a bundle under installing.
213      * @return Returns ERR_OK if the module directory renamed successfully; returns error code otherwise.
214      */
215     ErrCode RenameModuleDir(InnerBundleInfo &info) const;
216     /**
217      * @brief Modify the install directory path for different install type.
218      * @param info Indicates the InnerBundleInfo object of a bundle under installing.
219      * @return Returns true if the path set successfully; returns false otherwise.
220      */
221     bool ModifyInstallDirByHapType(InnerBundleInfo &info);
222     /**
223      * @brief Update the bundle paths in the InnerBundleInfo object.
224      * @param info Indicates the InnerBundleInfo object of a bundle under installing.
225      * @param baseDataPath Indicates the data file paths.
226      * @return Returns true if the path set successfully; returns false otherwise.
227      */
228     bool UpdateBundlePaths(InnerBundleInfo &info, const std::string baseDataPath) const;
229     /**
230      * @brief The process of install a new module package.
231      * @param newInfo Indicates the InnerBundleInfo object parsed from the config.json in the HAP package.
232      * @param oldInfo Indicates the exist InnerBundleInfo object get from the database.
233      * @return Returns ERR_OK if the new module install successfully; returns error code otherwise.
234      */
235     ErrCode ProcessNewModuleInstall(InnerBundleInfo &newInfo, InnerBundleInfo &oldInfo);
236     /**
237      * @brief The process of updating an exist module package.
238      * @param newInfo Indicates the InnerBundleInfo object parsed from the config.json in the HAP package.
239      * @param oldInfo Indicates the exist InnerBundleInfo object get from the database.
240      * @return Returns ERR_OK if the module updating successfully; returns error code otherwise.
241      */
242     ErrCode ProcessModuleUpdate(InnerBundleInfo &newInfo, InnerBundleInfo &oldInfo);
243 
244 private:
245     InstallerState state_ = InstallerState::INSTALL_START;
246     std::shared_ptr<BundleDataMgr> dataMgr_ = nullptr;  // this pointer will get when public functions called
247     std::string bundleName_;
248     std::string moduleTmpDir_;
249     std::string modulePath_;
250     std::string baseCodePath_;
251     std::string baseDataPath_;
252     std::string modulePackage_;
253     std::string mainAbility_;
254     bool isAppExist_ = false;
255 
256     DISALLOW_COPY_AND_MOVE(BaseBundleInstaller);
257 };
258 
259 }  // namespace AppExecFwk
260 }  // namespace OHOS
261 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BASE_BUNDLE_INSTALLER_H