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 OHOS_SYSTEM_ABILITY_MANAGER_SYSTEM_ABILITY_STATE_SCHEDULER_H
17 #define OHOS_SYSTEM_ABILITY_MANAGER_SYSTEM_ABILITY_STATE_SCHEDULER_H
18 
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <shared_mutex>
24 
25 #include "ffrt_handler.h"
26 #include "isystem_process_status_change.h"
27 #include "nlohmann/json.hpp"
28 #include "sa_profiles.h"
29 #include "schedule/system_ability_event_handler.h"
30 
31 namespace OHOS {
32 constexpr int32_t UNLOAD_DELAY_TIME = 20 * 1000;
33 
34 class SystemAbilityStateScheduler : public SystemAbilityStateListener,
35     public std::enable_shared_from_this<SystemAbilityStateScheduler> {
36 public:
37     SystemAbilityStateScheduler() = default;
38     virtual ~SystemAbilityStateScheduler() = default;
39     void Init(const std::list<SaProfile>& saProfiles);
40     void CleanFfrt();
41     void SetFfrt();
42 
43     int32_t HandleAbilityDiedEvent(int32_t systemAbilityId);
44     int32_t HandleLoadAbilityEvent(const LoadRequestInfo& loadRequestInfo);
45     int32_t HandleLoadAbilityEvent(int32_t systemAbilityId, bool& isExist);
46     int32_t HandleUnloadAbilityEvent(const std::shared_ptr<UnloadRequestInfo> unloadRequestInfo);
47     int32_t HandleCancelUnloadAbilityEvent(int32_t systemAbilityId);
48     int32_t UnloadAllIdleSystemAbility();
49     int32_t SendAbilityStateEvent(int32_t systemAbilityId, AbilityStateEvent event);
50     int32_t SendProcessStateEvent(const ProcessInfo& processInfo, ProcessStateEvent event);
51     bool IsSystemAbilityUnloading(int32_t systemAbilityId);
52 
53     int32_t GetSystemProcessInfo(int32_t systemAbilityId, SystemProcessInfo& systemProcessInfo);
54     int32_t GetRunningSystemProcess(std::list<SystemProcessInfo>& systemProcessInfos);
55     int32_t GetProcessNameByProcessId(int32_t pid, std::u16string& processName);
56     void GetAllSystemAbilityInfo(std::string& result);
57     void GetSystemAbilityInfo(int32_t said, std::string& result);
58     void GetProcessInfo(const std::string& processName, std::string& result);
59     void GetAllSystemAbilityInfoByState(const std::string& state, std::string& result);
60     int32_t SubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener);
61     int32_t UnSubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener);
62     bool IsSystemProcessNeverStartedLocked(const std::u16string& processName);
63     void InitSamgrProcessContext();
64     void CheckEnableOnce(const OnDemandEvent& event, const std::list<SaControlInfo>& saControlList);
65     int32_t CheckStartEnableOnce(const OnDemandEvent& event, const SaControlInfo& saControl,
66         sptr<ISystemAbilityLoadCallback> callback);
67     int32_t CheckStopEnableOnce(const OnDemandEvent& event, const SaControlInfo& saControl);
68     void UpdateLimitDelayUnloadTime(int32_t systemAbilityId);
69     void UpdateLimitDelayUnloadTimeTask(int32_t systemAbilityId);
70 private:
71     void InitStateContext(const std::list<SaProfile>& saProfiles);
72 
73     int32_t LimitDelayUnloadTime(int32_t delayUnloadTime);
74     bool GetSystemAbilityContext(int32_t systemAbilityId,
75         std::shared_ptr<SystemAbilityContext>& abilityContext);
76     bool GetSystemProcessContext(const std::u16string& processName,
77         std::shared_ptr<SystemProcessContext>& processContext);
78 
79     int32_t HandleLoadAbilityEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext,
80         const LoadRequestInfo& loadRequestInfo);
81     int32_t HandleUnloadAbilityEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext,
82         const std::shared_ptr<UnloadRequestInfo> unloadRequestInfo);
83 
84     int32_t SendDelayUnloadEventLocked(uint32_t systemAbilityId, int32_t delayTime = UNLOAD_DELAY_TIME);
85     int32_t RemoveDelayUnloadEventLocked(uint32_t systemAbilityId);
86     int32_t ProcessDelayUnloadEvent(int32_t systemAbilityId);
87     int32_t ProcessDelayUnloadEventLocked(int32_t systemAbilityId);
88 
89     int32_t PendLoadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext,
90         const LoadRequestInfo& loadRequestInfo);
91     int32_t PendUnloadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext,
92         const std::shared_ptr<UnloadRequestInfo> unloadRequestInfo);
93     int32_t RemovePendingUnloadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext);
94     int32_t HandlePendingLoadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext);
95     int32_t HandlePendingUnloadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext);
96 
97     int32_t DoLoadSystemAbilityLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext,
98         const LoadRequestInfo& loadRequestInfo);
99     int32_t DoUnloadSystemAbilityLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext);
100 
101     int32_t PostTryUnloadAllAbilityTask(const std::shared_ptr<SystemProcessContext>& processContext);
102     int32_t PostUnloadTimeoutTask(const std::shared_ptr<SystemProcessContext>& processContext);
103     void RemoveUnloadTimeoutTask(const std::shared_ptr<SystemProcessContext>& processContext);
104     int32_t PostTryKillProcessTask(const std::shared_ptr<SystemProcessContext>& processContext);
105 
106     int32_t TryUnloadAllSystemAbility(const std::shared_ptr<SystemProcessContext>& processContext);
107     bool CanUnloadAllSystemAbility(const std::shared_ptr<SystemProcessContext>& processContext);
108     bool CanUnloadAllSystemAbilityLocked(const std::shared_ptr<SystemProcessContext>& processContext,
109         bool isNeedCheckRecycleStrategy = false);
110     bool CheckSaIsImmediatelyRecycle(const std::shared_ptr<SystemProcessContext>& processContext);
111     int32_t UnloadAllSystemAbilityLocked(const std::shared_ptr<SystemProcessContext>& processContext);
112 
113     int32_t TryKillSystemProcess(const std::shared_ptr<SystemProcessContext>& processContext);
114     bool CanKillSystemProcess(const std::shared_ptr<SystemProcessContext>& processContext);
115     bool CanKillSystemProcessLocked(const std::shared_ptr<SystemProcessContext>& processContext);
116     int32_t KillSystemProcessLocked(const std::shared_ptr<SystemProcessContext>& processContext);
117 
118     bool CanRestartProcessLocked(const std::shared_ptr<SystemProcessContext>& processContext);
119     int32_t GetAbnormallyDiedAbilityLocked(std::shared_ptr<SystemProcessContext>& processContext,
120         std::list<std::shared_ptr<SystemAbilityContext>>& abnormallyDiedAbilityList);
121     int32_t HandleAbnormallyDiedAbilityLocked(std::shared_ptr<SystemProcessContext>& processContext,
122         std::list<std::shared_ptr<SystemAbilityContext>>& abnormallyDiedAbilityList);
123 
124     void NotifyProcessStarted(const std::shared_ptr<SystemProcessContext>& processContext);
125     void NotifyProcessStopped(const std::shared_ptr<SystemProcessContext>& processContext);
126     void OnAbilityNotLoadedLocked(int32_t systemAbilityId) override;
127     void OnAbilityLoadedLocked(int32_t systemAbilityId) override;
128     void OnAbilityUnloadableLocked(int32_t systemAbilityId) override;
129     void OnProcessNotStartedLocked(const std::u16string& processName) override;
130     void OnProcessStartedLocked(const std::u16string& processName) override;
131 
132     int32_t ActiveSystemAbilityLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext,
133         const nlohmann::json& activeReason);
134 
135     class UnloadEventHandler {
136     public:
UnloadEventHandler(const std::weak_ptr<SystemAbilityStateScheduler> & stateScheduler)137         UnloadEventHandler(const std::weak_ptr<SystemAbilityStateScheduler>& stateScheduler)
138             : stateScheduler_(stateScheduler)
139         {
140             handler_ = std::make_shared<FFRTHandler>("UnloadEventHandler");
141         }
142         ~UnloadEventHandler() = default;
143         void ProcessEvent(uint32_t eventId);
144         bool SendEvent(uint32_t eventId, int64_t extraDataId, uint64_t delayTime);
145         void RemoveEvent(uint32_t eventId);
146         bool HasInnerEvent(uint32_t eventId);
147         void CleanFfrt();
148         void SetFfrt();
149 
150     private:
151         std::weak_ptr<SystemAbilityStateScheduler> stateScheduler_;
152         std::shared_ptr<FFRTHandler> handler_;
153     };
154     std::shared_ptr<SystemAbilityStateMachine> stateMachine_;
155     std::shared_ptr<SystemAbilityEventHandler> stateEventHandler_;
156     std::shared_mutex abiltyMapLock_;
157     std::shared_mutex processMapLock_;
158     std::map<int32_t, std::shared_ptr<SystemAbilityContext>> abilityContextMap_;
159     std::map<std::u16string, std::shared_ptr<SystemProcessContext>> processContextMap_;
160     std::shared_ptr<UnloadEventHandler> unloadEventHandler_;
161     std::shared_ptr<FFRTHandler> processHandler_;
162     std::shared_mutex listenerSetLock_;
163     std::list<sptr<ISystemProcessStatusChange>> processListeners_;
164     sptr<IRemoteObject::DeathRecipient> processListenerDeath_;
165     std::mutex startEnableOnceLock_;
166     std::map<int32_t, std::list<OnDemandEvent>> startEnableOnceMap_;
167     std::mutex stopEnableOnceLock_;
168     std::map<int32_t, std::list<OnDemandEvent>> stopEnableOnceMap_;
169 };
170 } // namespace OHOS
171 
172 #endif // !defined(OHOS_SYSTEM_ABILITY_MANAGER_SYSTEM_ABILITY_STATE_SCHEDULER_H)