1 /*
2  * Copyright (c) 2024 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 OHOS_ABILITY_RUNTIME_SUB_MANAGERS_HELPER_H
17 #define OHOS_ABILITY_RUNTIME_SUB_MANAGERS_HELPER_H
18 
19 #include <mutex>
20 #include <string>
21 #include <unordered_map>
22 
23 #include "ability_connect_manager.h"
24 #include "ability_event_handler.h"
25 #include "cpp/mutex.h"
26 #include "data_ability_manager.h"
27 #include "mission_list_manager_interface.h"
28 #include "nocopyable.h"
29 #include "pending_want_manager.h"
30 #include "scene_board/ui_ability_lifecycle_manager.h"
31 #include "task_handler_wrap.h"
32 
33 namespace OHOS {
34 namespace AAFwk {
35 using CreateMissionListMgrFunc = MissionListWrap*(*)();
36 class SubManagersHelper {
37 public:
38     SubManagersHelper(std::shared_ptr<TaskHandlerWrap> taskHandler, std::shared_ptr<AbilityEventHandler> eventHandler);
39     virtual ~SubManagersHelper();
40 
41     void InitSubManagers(int userId, bool switchUser);
42     void InitMissionListManager(int userId, bool switchUser);
43     void InitUIAbilityManager(int userId, bool switchUser);
44     void InitConnectManager(int32_t userId, bool switchUser);
45     void InitDataAbilityManager(int32_t userId, bool switchUser);
46     void InitPendWantManager(int32_t userId, bool switchUser);
47 
48     void ClearSubManagers(int userId);
49 
50     std::shared_ptr<DataAbilityManager> GetCurrentDataAbilityManager();
51     std::shared_ptr<DataAbilityManager> GetDataAbilityManager(const sptr<IAbilityScheduler> &scheduler);
52     std::unordered_map<int, std::shared_ptr<DataAbilityManager>> GetDataAbilityManagers();
53     std::shared_ptr<DataAbilityManager> GetDataAbilityManagerByUserId(int32_t userId);
54     std::shared_ptr<DataAbilityManager> GetDataAbilityManagerByToken(const sptr<IRemoteObject> &token);
55 
56     std::unordered_map<int, std::shared_ptr<AbilityConnectManager>> GetConnectManagers();
57     std::shared_ptr<AbilityConnectManager> GetCurrentConnectManager();
58     std::shared_ptr<AbilityConnectManager> GetConnectManagerByUserId(int32_t userId);
59     std::shared_ptr<AbilityConnectManager> GetConnectManagerByToken(const sptr<IRemoteObject> &token);
60     std::shared_ptr<AbilityConnectManager> GetConnectManagerByAbilityRecordId(const int64_t &abilityRecordId);
61 
62     std::shared_ptr<PendingWantManager> GetCurrentPendingWantManager();
63     std::shared_ptr<PendingWantManager> GetPendingWantManagerByUserId(int32_t userId);
64 
65     std::unordered_map<int, std::shared_ptr<MissionListManagerInterface>> GetMissionListManagers();
66     std::shared_ptr<MissionListManagerInterface> GetCurrentMissionListManager();
67     std::shared_ptr<MissionListManagerInterface> GetMissionListManagerByUserId(int32_t userId);
68     std::shared_ptr<MissionListManagerInterface> GetMissionListManagerByUid(int32_t uid);
69 
70     std::unordered_map<int, std::shared_ptr<UIAbilityLifecycleManager>> GetUIAbilityManagers();
71     std::shared_ptr<UIAbilityLifecycleManager> GetCurrentUIAbilityManager();
72     std::shared_ptr<UIAbilityLifecycleManager> GetUIAbilityManagerByUserId(int32_t userId);
73     std::shared_ptr<UIAbilityLifecycleManager> GetUIAbilityManagerByUid(int32_t uid);
74 
75     void UninstallApp(const std::string &bundleName, int32_t uid);
76     void UninstallAppInUIAbilityManagers(int32_t userId, const std::string &bundleName, int32_t uid);
77     void UninstallAppInMissionListManagers(int32_t userId, const std::string &bundleName, int32_t uid);
78     bool VerificationAllToken(const sptr<IRemoteObject> &token);
79     bool VerificationAllTokenForConnectManagers(const sptr<IRemoteObject> &token);
80 
81     std::shared_ptr<MissionListWrap> GetMissionListWrap();
82     std::shared_ptr<MissionListManagerInterface> CreateMissionListMgr(int32_t userId);
83 private:
84     DISALLOW_COPY_AND_MOVE(SubManagersHelper);
85 
86     std::shared_ptr<TaskHandlerWrap> taskHandler_;
87     std::shared_ptr<AbilityEventHandler> eventHandler_;
88 
89     ffrt::mutex managersMutex_;
90     std::unordered_map<int, std::shared_ptr<AbilityConnectManager>> connectManagers_;
91     std::shared_ptr<AbilityConnectManager> currentConnectManager_;
92     std::unordered_map<int, std::shared_ptr<DataAbilityManager>> dataAbilityManagers_;
93     std::shared_ptr<DataAbilityManager> currentDataAbilityManager_;
94     std::unordered_map<int, std::shared_ptr<PendingWantManager>> pendingWantManagers_;
95     std::shared_ptr<PendingWantManager> currentPendingWantManager_;
96     std::unordered_map<int, std::shared_ptr<MissionListManagerInterface>> missionListManagers_;
97     std::shared_ptr<MissionListManagerInterface> currentMissionListManager_;
98     std::unordered_map<int, std::shared_ptr<UIAbilityLifecycleManager>> uiAbilityManagers_;
99     std::shared_ptr<UIAbilityLifecycleManager> currentUIAbilityManager_;
100 
101     std::mutex missionListWrapMutex_;
102     void* missionLibHandle_ = nullptr;
103     std::shared_ptr<MissionListWrap> missionListWrap_;
104 };
105 }  // namespace AAFwk
106 }  // namespace OHOS
107 #endif  // OHOS_ABILITY_RUNTIME_SUB_MANAGERS_HELPER_H
108