• 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 <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;
~SystemAbilityStateScheduler()38     virtual ~SystemAbilityStateScheduler()
39     {
40         CleanResource();
41     }
42     void Init(const std::list<SaProfile>& saProfiles);
43     void CleanFfrt();
44     void SetFfrt();
45     void CleanResource();
46 
47     int32_t HandleAbilityDiedEvent(int32_t systemAbilityId);
48     int32_t HandleLoadAbilityEvent(const LoadRequestInfo& loadRequestInfo);
49     int32_t HandleLoadAbilityEvent(int32_t systemAbilityId, bool& isExist);
50     int32_t HandleUnloadAbilityEvent(const std::shared_ptr<UnloadRequestInfo> unloadRequestInfo);
51     int32_t HandleCancelUnloadAbilityEvent(int32_t systemAbilityId);
52     int32_t UnloadAllIdleSystemAbility();
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 private:
75     void InitStateContext(const std::list<SaProfile>& saProfiles);
76 
77     int32_t LimitDelayUnloadTime(int32_t delayUnloadTime);
78     bool GetSystemAbilityContext(int32_t systemAbilityId,
79         std::shared_ptr<SystemAbilityContext>& abilityContext);
80     bool GetSystemProcessContext(const std::u16string& processName,
81         std::shared_ptr<SystemProcessContext>& processContext);
82 
83     int32_t HandleLoadAbilityEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext,
84         const LoadRequestInfo& loadRequestInfo);
85     int32_t HandleUnloadAbilityEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext,
86         const std::shared_ptr<UnloadRequestInfo> unloadRequestInfo);
87 
88     int32_t SendDelayUnloadEventLocked(uint32_t systemAbilityId, int32_t delayTime = UNLOAD_DELAY_TIME);
89     int32_t RemoveDelayUnloadEventLocked(uint32_t systemAbilityId);
90     int32_t ProcessDelayUnloadEvent(int32_t systemAbilityId);
91     int32_t ProcessDelayUnloadEventLocked(int32_t systemAbilityId);
92 
93     int32_t PendLoadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext,
94         const LoadRequestInfo& loadRequestInfo);
95     int32_t PendUnloadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext,
96         const std::shared_ptr<UnloadRequestInfo> unloadRequestInfo);
97     int32_t RemovePendingUnloadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext);
98     int32_t HandlePendingLoadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext);
99     int32_t HandlePendingUnloadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext);
100 
101     int32_t DoLoadSystemAbilityLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext,
102         const LoadRequestInfo& loadRequestInfo);
103     int32_t DoUnloadSystemAbilityLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext);
104 
105     int32_t PostTryUnloadAllAbilityTask(const std::shared_ptr<SystemProcessContext>& processContext);
106     int32_t PostUnloadTimeoutTask(const std::shared_ptr<SystemProcessContext>& processContext);
107     void RemoveUnloadTimeoutTask(const std::shared_ptr<SystemProcessContext>& processContext);
108     int32_t PostTryKillProcessTask(const std::shared_ptr<SystemProcessContext>& processContext);
109 
110     int32_t TryUnloadAllSystemAbility(const std::shared_ptr<SystemProcessContext>& processContext);
111     bool CanUnloadAllSystemAbility(const std::shared_ptr<SystemProcessContext>& processContext);
112     bool CanUnloadAllSystemAbilityLocked(const std::shared_ptr<SystemProcessContext>& processContext,
113         bool isNeedCheckRecycleStrategy = false);
114     bool CheckSaIsImmediatelyRecycle(const std::shared_ptr<SystemProcessContext>& processContext);
115     int32_t UnloadAllSystemAbilityLocked(const std::shared_ptr<SystemProcessContext>& processContext);
116 
117     int32_t TryKillSystemProcess(const std::shared_ptr<SystemProcessContext>& processContext);
118     bool CanKillSystemProcess(const std::shared_ptr<SystemProcessContext>& processContext);
119     bool CanKillSystemProcessLocked(const std::shared_ptr<SystemProcessContext>& processContext);
120     int32_t KillSystemProcessLocked(const std::shared_ptr<SystemProcessContext>& processContext);
121 
122     bool CanRestartProcessLocked(const std::shared_ptr<SystemProcessContext>& processContext);
123     int32_t GetAbnormallyDiedAbilityLocked(std::shared_ptr<SystemProcessContext>& processContext,
124         std::list<std::shared_ptr<SystemAbilityContext>>& abnormallyDiedAbilityList);
125     int32_t HandleAbnormallyDiedAbilityLocked(std::shared_ptr<SystemProcessContext>& processContext,
126         std::list<std::shared_ptr<SystemAbilityContext>>& abnormallyDiedAbilityList);
127 
128     void NotifyProcessStarted(const std::shared_ptr<SystemProcessContext>& processContext);
129     void NotifyProcessStopped(const std::shared_ptr<SystemProcessContext>& processContext);
130     void OnAbilityNotLoadedLocked(int32_t systemAbilityId) override;
131     void OnAbilityLoadedLocked(int32_t systemAbilityId) override;
132     void OnAbilityUnloadableLocked(int32_t systemAbilityId) override;
133     void OnProcessNotStartedLocked(const std::u16string& processName) override;
134     void OnProcessStartedLocked(const std::u16string& processName) override;
135 
136     int32_t ActiveSystemAbilityLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext,
137         const nlohmann::json& activeReason);
138 
139     class UnloadEventHandler : public std::enable_shared_from_this<UnloadEventHandler> {
140     public:
UnloadEventHandler(const std::weak_ptr<SystemAbilityStateScheduler> & stateScheduler)141         UnloadEventHandler(const std::weak_ptr<SystemAbilityStateScheduler>& stateScheduler)
142             : stateScheduler_(stateScheduler)
143         {
144             handler_ = std::make_shared<FFRTHandler>("UnloadEventHandler");
145         }
146         ~UnloadEventHandler() = default;
147         void ProcessEvent(uint32_t eventId);
148         bool SendEvent(uint32_t eventId, int64_t extraDataId, uint64_t delayTime);
149         void RemoveEvent(uint32_t eventId);
150         bool HasInnerEvent(uint32_t eventId);
151         void CleanFfrt();
152         void SetFfrt();
153 
154     private:
155         std::weak_ptr<SystemAbilityStateScheduler> stateScheduler_;
156         std::shared_ptr<FFRTHandler> handler_;
157     };
158     std::shared_ptr<SystemAbilityStateMachine> stateMachine_;
159     std::shared_ptr<SystemAbilityEventHandler> stateEventHandler_;
160     std::shared_mutex abiltyMapLock_;
161     std::shared_mutex processMapLock_;
162     std::map<int32_t, std::shared_ptr<SystemAbilityContext>> abilityContextMap_;
163     std::map<std::u16string, std::shared_ptr<SystemProcessContext>> processContextMap_;
164     std::shared_ptr<UnloadEventHandler> unloadEventHandler_;
165     std::shared_ptr<FFRTHandler> processHandler_;
166     std::shared_mutex listenerSetLock_;
167     std::list<sptr<ISystemProcessStatusChange>> processListeners_;
168     sptr<IRemoteObject::DeathRecipient> processListenerDeath_;
169     std::mutex startEnableOnceLock_;
170     std::map<int32_t, std::list<OnDemandEvent>> startEnableOnceMap_;
171     std::mutex stopEnableOnceLock_;
172     std::map<int32_t, std::list<OnDemandEvent>> stopEnableOnceMap_;
173 };
174 } // namespace OHOS
175 
176 #endif // !defined(OHOS_SYSTEM_ABILITY_MANAGER_SYSTEM_ABILITY_STATE_SCHEDULER_H)