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