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 #ifndef SYS_INSTALLER_MODULE_UPDATE_MAIN_H
16 #define SYS_INSTALLER_MODULE_UPDATE_MAIN_H
17 
18 #include <unordered_map>
19 #include <unordered_set>
20 
21 #include "imodule_update.h"
22 #include "iservice_registry.h"
23 #include "isys_installer_callback.h"
24 #include "module_ipc_helper.h"
25 #include "module_utils.h"
26 #include "singleton.h"
27 
28 namespace OHOS {
29 namespace SysInstaller {
30 class ModuleUpdateMain final : public Singleton<ModuleUpdateMain> {
31     DECLARE_SINGLETON(ModuleUpdateMain);
32 public:
33     bool DoHotInstall(ModuleUpdateStatus &status);
34     int32_t CheckHmpName(const std::string &hmpName);
35     int32_t UninstallModulePackage(const std::string &hmpName);
36     int32_t GetModulePackageInfo(const std::string &hmpName,
37         std::list<ModulePackageInfo> &modulePackageInfos);
38     int32_t ReportModuleUpdateStatus(const ModuleUpdateStatus &status);
39     std::vector<HmpVersionInfo> GetHmpVersionInfo();
40     void ExitModuleUpdate();
41     int32_t InstallModuleFile(const std::string &hmpName, const std::string &file, ModuleUpdateStatus &status) const;
42     void CollectModulePackageInfo(const std::string &hmpName, std::list<ModulePackageInfo> &modulePackageInfos) const;
43     bool BackupActiveModules(const std::string &hmpName) const;
44     bool GetHmpVersion(const std::string &hmpPath, HmpVersionInfo &versionInfo);
45     void SaveInstallerResult(const std::string &hmpPath, int result,
46         const std::string &resultInfo, const Timer &timer);
47     int32_t ReallyInstallModulePackage(const std::string &pkgPath, const sptr<ISysInstallerCallback> &updateCallback);
48     void ParseHmpVersionInfo(std::vector<HmpVersionInfo> &versionInfos, const HmpVersionInfo &preInfo,
49         const HmpVersionInfo &actInfo);
50 
51     bool HotAppInstall(ModuleUpdateStatus &status);
52     void HotSaInstall(ModuleUpdateStatus &status);
53     void HotAppInstallWhenUpdateFail(ModuleUpdateStatus &status);
54     void FirstRevertInstallHotApp(ModuleUpdateStatus &status);
55     void SecondRevertInstallHotApp(ModuleUpdateStatus &status);
56 
57     void ScanPreInstalledHmp();
58     void Start();
59     void Stop();
60 
61 private:
62     int32_t ValidateVersion(ModuleFile &installFile, const std::string &hmpName) const;
63     sptr<ISystemAbilityManager> &GetSystemAbilityManager();
64 
65     sptr<ISystemAbilityManager> samgr_ = nullptr;
66     std::unordered_set<std::string> hmpSet_;
67     std::unordered_map<int32_t, std::string> saIdHmpMap_;
68     std::mutex mlock_;
69 };
70 } // namespace SysInstaller
71 } // namespace OHOS
72 #endif // SYS_INSTALLER_MODULE_UPDATE_MAIN_H