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 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 void GetAllSystemAbilityInfo(std::string& result); 56 void GetSystemAbilityInfo(int32_t said, std::string& result); 57 void GetProcessInfo(const std::string& processName, std::string& result); 58 void GetAllSystemAbilityInfoByState(const std::string& state, std::string& result); 59 int32_t SubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener); 60 int32_t UnSubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener); 61 bool IsSystemProcessNeverStartedLocked(const std::u16string& processName); 62 private: 63 void InitStateContext(const std::list<SaProfile>& saProfiles); 64 65 int32_t LimitDelayUnloadTime(int32_t delayUnloadTime); 66 bool GetSystemAbilityContext(int32_t systemAbilityId, 67 std::shared_ptr<SystemAbilityContext>& abilityContext); 68 bool GetSystemProcessContext(const std::u16string& processName, 69 std::shared_ptr<SystemProcessContext>& processContext); 70 71 int32_t HandleLoadAbilityEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext, 72 const LoadRequestInfo& loadRequestInfo); 73 int32_t HandleUnloadAbilityEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext, 74 const UnloadRequestInfo& unloadRequestInfo); 75 76 int32_t SendDelayUnloadEventLocked(uint32_t systemAbilityId, int32_t delayTime = UNLOAD_DELAY_TIME); 77 int32_t RemoveDelayUnloadEventLocked(uint32_t systemAbilityId); 78 int32_t ProcessDelayUnloadEvent(int32_t systemAbilityId); 79 int32_t ProcessDelayUnloadEventLocked(int32_t systemAbilityId); 80 81 int32_t PendLoadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext, 82 const LoadRequestInfo& loadRequestInfo); 83 int32_t PendUnloadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext, 84 const UnloadRequestInfo& unloadRequestInfo); 85 int32_t RemovePendingUnloadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext); 86 int32_t HandlePendingLoadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext); 87 int32_t HandlePendingUnloadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext); 88 89 int32_t DoLoadSystemAbilityLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext, 90 const LoadRequestInfo& loadRequestInfo); 91 int32_t DoUnloadSystemAbilityLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext); 92 93 int32_t PostTryUnloadAllAbilityTask(const std::shared_ptr<SystemProcessContext>& processContext); 94 int32_t PostUnloadTimeoutTask(const std::shared_ptr<SystemProcessContext>& processContext); 95 void RemoveUnloadTimeoutTask(const std::shared_ptr<SystemProcessContext>& processContext); 96 int32_t PostTryKillProcessTask(const std::shared_ptr<SystemProcessContext>& processContext); 97 98 int32_t TryUnloadAllSystemAbility(const std::shared_ptr<SystemProcessContext>& processContext); 99 bool CanUnloadAllSystemAbility(const std::shared_ptr<SystemProcessContext>& processContext); 100 bool CanUnloadAllSystemAbilityLocked(const std::shared_ptr<SystemProcessContext>& processContext, 101 bool isNeedCheckRecycleStrategy = false); 102 bool CheckSaIsImmediatelyRecycle(const std::shared_ptr<SystemProcessContext>& processContext); 103 int32_t UnloadAllSystemAbilityLocked(const std::shared_ptr<SystemProcessContext>& processContext); 104 105 int32_t TryKillSystemProcess(const std::shared_ptr<SystemProcessContext>& processContext); 106 bool CanKillSystemProcess(const std::shared_ptr<SystemProcessContext>& processContext); 107 bool CanKillSystemProcessLocked(const std::shared_ptr<SystemProcessContext>& processContext); 108 int32_t KillSystemProcessLocked(const std::shared_ptr<SystemProcessContext>& processContext); 109 110 bool CanRestartProcessLocked(const std::shared_ptr<SystemProcessContext>& processContext); 111 int32_t GetAbnormallyDiedAbilityLocked(std::shared_ptr<SystemProcessContext>& processContext, 112 std::list<std::shared_ptr<SystemAbilityContext>>& abnormallyDiedAbilityList); 113 int32_t HandleAbnormallyDiedAbilityLocked(std::shared_ptr<SystemProcessContext>& processContext, 114 std::list<std::shared_ptr<SystemAbilityContext>>& abnormallyDiedAbilityList); 115 116 void NotifyProcessStarted(const std::shared_ptr<SystemProcessContext>& processContext); 117 void NotifyProcessStopped(const std::shared_ptr<SystemProcessContext>& processContext); 118 void OnAbilityNotLoadedLocked(int32_t systemAbilityId) override; 119 void OnAbilityLoadedLocked(int32_t systemAbilityId) override; 120 void OnAbilityUnloadableLocked(int32_t systemAbilityId) override; 121 void OnProcessNotStartedLocked(const std::u16string& processName) override; 122 void OnProcessStartedLocked(const std::u16string& processName) override; 123 124 int32_t ActiveSystemAbilityLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext, 125 const nlohmann::json& activeReason); 126 127 class UnloadEventHandler { 128 public: UnloadEventHandler(const std::weak_ptr<SystemAbilityStateScheduler> & stateScheduler)129 UnloadEventHandler(const std::weak_ptr<SystemAbilityStateScheduler>& stateScheduler) 130 : stateScheduler_(stateScheduler) 131 { 132 handler_ = std::make_shared<FFRTHandler>("UnloadEventHandler"); 133 } 134 ~UnloadEventHandler() = default; 135 void ProcessEvent(uint32_t eventId); 136 bool SendEvent(uint32_t eventId, int64_t extraDataId, uint64_t delayTime); 137 void RemoveEvent(uint32_t eventId); 138 bool HasInnerEvent(uint32_t eventId); 139 void CleanFfrt(); 140 void SetFfrt(); 141 142 private: 143 std::weak_ptr<SystemAbilityStateScheduler> stateScheduler_; 144 std::shared_ptr<FFRTHandler> handler_; 145 }; 146 std::shared_ptr<SystemAbilityStateMachine> stateMachine_; 147 std::shared_ptr<SystemAbilityEventHandler> stateEventHandler_; 148 std::shared_mutex abiltyMapLock_; 149 std::shared_mutex processMapLock_; 150 std::map<int32_t, std::shared_ptr<SystemAbilityContext>> abilityContextMap_; 151 std::map<std::u16string, std::shared_ptr<SystemProcessContext>> processContextMap_; 152 std::shared_ptr<UnloadEventHandler> unloadEventHandler_; 153 std::shared_ptr<FFRTHandler> processHandler_; 154 std::shared_mutex listenerSetLock_; 155 std::list<sptr<ISystemProcessStatusChange>> processListeners_; 156 sptr<IRemoteObject::DeathRecipient> processListenerDeath_; 157 }; 158 } // namespace OHOS 159 160 #endif // !defined(OHOS_SYSTEM_ABILITY_MANAGER_SYSTEM_ABILITY_STATE_SCHEDULER_H)