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 UPDATE_SERVICE_MODULE_MANAGER_H
17 #define UPDATE_SERVICE_MODULE_MANAGER_H
18 
19 #include <map>
20 #include <mutex>
21 #include <set>
22 #include <string>
23 #include <vector>
24 
25 #include "ipc_skeleton.h"
26 #include "refbase.h"
27 #include "system_ability_ondemand_reason.h"
28 
29 #include "update_service_module.h"
30 
31 namespace OHOS {
32 namespace UpdateEngine {
33 class ModuleManager {
34 public:
35     ModuleManager();
36     ~ModuleManager() = default;
37 
38     static ModuleManager& GetInstance();
39 
40     void LoadModule(std::string libPath);
41     bool IsModuleLoaded();
42     void HookFunc(std::vector<uint32_t>, RequestFuncType handleRemoteRequest);
43     int32_t HandleFunc(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption &option);
44     void HookOnStartOnStopFunc(std::string phase, LifeCycleFuncType handleSAOnStartOnStop);
45     void HandleOnStartOnStopFunc(std::string phase, const OHOS::SystemAbilityOnDemandReason &reason);
46     void HookOnIdleFunc(std::string phase, LifeCycleFuncReturnType handleSAOnIdle);
47     int32_t HandleOnIdleFunc(std::string phase, const OHOS::SystemAbilityOnDemandReason &reason);
48     bool IsMapFuncExist(uint32_t code);
49 
50 private:
51     int32_t retryInterval_ = 0;
52     void *dueModuleHandler = nullptr;
53     static std::map<std::string, LifeCycleFuncType> onStartOnStopFuncMap_;
54     static std::map<std::string, LifeCycleFuncReturnType> onIdleFuncMap_;
55     static std::map<uint32_t, RequestFuncType> onRemoteRequestFuncMap_;
56     static bool isLoaded;
57     static std::mutex onRemoteRequestFuncMapMutex_;
58     static std::mutex onStartOnStopFuncMapMutex_;
59     static std::mutex onIdleFuncMapMutex_;
60 };
61 } // namespace UpdateEngine
62 } // namespace OHOS
63 #endif // UPDATE_SERVICE_MODULE_MANAGER_H
64