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 "event_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 41 int32_t HandleAbilityDiedEvent(int32_t systemAbilityId); 42 int32_t HandleLoadAbilityEvent(const LoadRequestInfo& loadRequestInfo); 43 int32_t HandleLoadAbilityEvent(int32_t systemAbilityId, bool& isExist); 44 int32_t HandleUnloadAbilityEvent(const UnloadRequestInfo& unloadRequestInfo); 45 int32_t HandleCancelUnloadAbilityEvent(int32_t systemAbilityId); 46 int32_t SendAbilityStateEvent(int32_t systemAbilityId, AbilityStateEvent event); 47 int32_t SendProcessStateEvent(const ProcessInfo& processInfo, ProcessStateEvent event); 48 bool IsSystemAbilityUnloading(int32_t systemAbilityId); 49 50 int32_t GetRunningSystemProcess(std::list<SystemProcessInfo>& systemProcessInfos); 51 int32_t SubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener); 52 int32_t UnSubscribeSystemProcess(const sptr<ISystemProcessStatusChange>& listener); 53 private: 54 void InitStateContext(const std::list<SaProfile>& saProfiles); 55 56 int32_t LimitDelayUnloadTime(int32_t delayUnloadTime); 57 bool GetSystemAbilityContext(int32_t systemAbilityId, 58 std::shared_ptr<SystemAbilityContext>& abilityContext); 59 bool GetSystemProcessContext(const std::u16string& processName, 60 std::shared_ptr<SystemProcessContext>& processContext); 61 62 int32_t HandleLoadAbilityEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext, 63 const LoadRequestInfo& loadRequestInfo); 64 int32_t HandleUnloadAbilityEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext, 65 const UnloadRequestInfo& unloadRequestInfo); 66 67 int32_t SendDelayUnloadEventLocked(uint32_t systemAbilityId, int32_t delayTime = UNLOAD_DELAY_TIME); 68 int32_t RemoveDelayUnloadEventLocked(uint32_t systemAbilityId); 69 int32_t ProcessDelayUnloadEvent(int32_t systemAbilityId); 70 71 int32_t PendLoadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext, 72 const LoadRequestInfo& loadRequestInfo); 73 int32_t PendUnloadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext, 74 const UnloadRequestInfo& unloadRequestInfo); 75 int32_t RemovePendingUnloadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext); 76 int32_t HandlePendingLoadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext); 77 int32_t HandlePendingUnloadEventLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext); 78 79 int32_t DoLoadSystemAbilityLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext, 80 const LoadRequestInfo& loadRequestInfo); 81 int32_t DoUnloadSystemAbilityLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext); 82 83 int32_t PostTryUnloadAllAbilityTask(const std::shared_ptr<SystemProcessContext>& processContext); 84 int32_t PostUnloadTimeoutTask(const std::shared_ptr<SystemProcessContext>& processContext); 85 void RemoveUnloadTimeoutTask(const std::shared_ptr<SystemProcessContext>& processContext); 86 int32_t PostTryKillProcessTask(const std::shared_ptr<SystemProcessContext>& processContext); 87 88 int32_t TryUnloadAllSystemAbility(const std::shared_ptr<SystemProcessContext>& processContext); 89 bool CanUnloadAllSystemAbility(const std::shared_ptr<SystemProcessContext>& processContext); 90 int32_t UnloadAllSystemAbilityLocked(const std::shared_ptr<SystemProcessContext>& processContext); 91 92 int32_t TryKillSystemProcess(const std::shared_ptr<SystemProcessContext>& processContext); 93 bool CanKillSystemProcess(const std::shared_ptr<SystemProcessContext>& processContext); 94 int32_t KillSystemProcessLocked(const std::shared_ptr<SystemProcessContext>& processContext); 95 96 bool CanRestartProcessLocked(const std::shared_ptr<SystemProcessContext>& processContext); 97 int32_t GetAbnormallyDiedAbilityLocked(std::shared_ptr<SystemProcessContext>& processContext, 98 std::list<std::shared_ptr<SystemAbilityContext>>& abnormallyDiedAbilityList); 99 int32_t HandleAbnormallyDiedAbilityLocked(std::shared_ptr<SystemProcessContext>& processContext, 100 std::list<std::shared_ptr<SystemAbilityContext>>& abnormallyDiedAbilityList); 101 102 void NotifyProcessStarted(const std::shared_ptr<SystemProcessContext>& processContext); 103 void NotifyProcessStopped(const std::shared_ptr<SystemProcessContext>& processContext); 104 void OnAbilityNotLoadedLocked(int32_t systemAbilityId) override; 105 void OnAbilityLoadedLocked(int32_t systemAbilityId) override; 106 void OnAbilityUnloadableLocked(int32_t systemAbilityId) override; 107 void OnProcessNotStartedLocked(const std::u16string& processName) override; 108 void OnProcessStartedLocked(const std::u16string& processName) override; 109 110 int32_t ActiveSystemAbilityLocked(const std::shared_ptr<SystemAbilityContext>& abilityContext, 111 const nlohmann::json& activeReason); 112 113 class UnloadEventHandler : public AppExecFwk::EventHandler { 114 public: UnloadEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner,const std::weak_ptr<SystemAbilityStateScheduler> & stateScheduler)115 UnloadEventHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner, 116 const std::weak_ptr<SystemAbilityStateScheduler>& stateScheduler) 117 : AppExecFwk::EventHandler(runner), stateScheduler_(stateScheduler) {} 118 ~UnloadEventHandler() = default; 119 void ProcessEvent(const OHOS::AppExecFwk::InnerEvent::Pointer& event) override; 120 private: 121 std::weak_ptr<SystemAbilityStateScheduler> stateScheduler_; 122 }; 123 std::shared_ptr<SystemAbilityStateMachine> stateMachine_; 124 std::shared_ptr<SystemAbilityEventHandler> stateEventHandler_; 125 std::shared_mutex abiltyMapLock_; 126 std::shared_mutex processMapLock_; 127 std::map<int32_t, std::shared_ptr<SystemAbilityContext>> abilityContextMap_; 128 std::map<std::u16string, std::shared_ptr<SystemProcessContext>> processContextMap_; 129 std::shared_ptr<UnloadEventHandler> unloadEventHandler_; 130 std::shared_ptr<AppExecFwk::EventHandler> processHandler_; 131 std::shared_mutex listenerSetLock_; 132 std::list<sptr<ISystemProcessStatusChange>> processListeners_; 133 sptr<IRemoteObject::DeathRecipient> processListenerDeath_; 134 }; 135 } // namespace OHOS 136 137 #endif // !defined(OHOS_SYSTEM_ABILITY_MANAGER_SYSTEM_ABILITY_STATE_SCHEDULER_H)