1 /*
2  * Copyright (c) 2022 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 SYS_INSTALLER_MANAGER_H
17 #define SYS_INSTALLER_MANAGER_H
18 
19 #include "installer_manager_helper.h"
20 #include "macros_updater.h"
21 #include "status_manager.h"
22 
23 namespace OHOS {
24 namespace SysInstaller {
25 class InstallerManager {
26     DISALLOW_COPY_MOVE(InstallerManager);
27 public:
28     void RegisterDump(std::unique_ptr<IInstallerManagerHelper> ptr);
29     static InstallerManager &GetInstance();
30 
31     virtual int32_t SysInstallerInit();
32     virtual int32_t StartUpdatePackageZip(const std::string &pkgPath);
33     virtual int32_t SetUpdateCallback(const sptr<ISysInstallerCallback> &updateCallback);
34     virtual int32_t GetUpdateStatus();
35     virtual int32_t StartUpdateParaZip(const std::string &pkgPath,
36         const std::string &location, const std::string &cfgDir);
37     virtual int32_t StartDeleteParaZip(const std::string &location, const std::string &cfgDir);
38     virtual int32_t AccDecompressAndVerifyPkg(const std::string &srcPath,
39         const std::string &dstPath, const uint32_t type);
40     virtual int32_t AccDeleteDir(const std::string &dstPath);
41 
42 protected:
43     std::unique_ptr<IInstallerManagerHelper> helper_ {};
44 
45 private:
46     InstallerManager() = default;
47     ~InstallerManager() = default;
48 };
49 
50 enum SysInstallerInitEvent {
51     SYS_PRE_INIT_EVENT = 0,
52     SYS_POST_INIT_EVENT,
53     SYS_POST_EVENT,
54     SYS_APP_QUICKFIX_EVENT,
55     SYS_INIT_EVENT_BUTT
56 };
57 using InitHandler = void (*)(void);
58 
59 class SysInstallerManagerInit {
60     DISALLOW_COPY_MOVE(SysInstallerManagerInit);
61 public:
GetInstance()62     static SysInstallerManagerInit &GetInstance()
63     {
64         static SysInstallerManagerInit instance;
65         return instance;
66     }
InvokeEvent(enum SysInstallerInitEvent eventId)67     void InvokeEvent(enum SysInstallerInitEvent eventId) const
68     {
69         if (eventId >= SYS_INIT_EVENT_BUTT) {
70             return;
71         }
72         for (const auto &handler : initEvent_[eventId]) {
73             if (handler != nullptr) {
74                 handler();
75             }
76         }
77     }
SubscribeEvent(enum SysInstallerInitEvent eventId,InitHandler handler)78     void SubscribeEvent(enum SysInstallerInitEvent eventId, InitHandler handler)
79     {
80         if (eventId < SYS_INIT_EVENT_BUTT) {
81             initEvent_[eventId].push_back(handler);
82         }
83     }
84 private:
85     SysInstallerManagerInit() = default;
86     ~SysInstallerManagerInit() = default;
87     std::vector<InitHandler> initEvent_[SYS_INIT_EVENT_BUTT];
88 };
89 } // SysInstaller
90 } // namespace OHOS
91 #endif // SYS_INSTALLER_MANAGER_HELPER_H
92