1 /* 2 * Copyright (c) 2021 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 LOCAL_ABILITY_MANAGER_H 17 #define LOCAL_ABILITY_MANAGER_H 18 19 #include <string> 20 #include <unordered_map> 21 #include <list> 22 #include <unistd.h> 23 #include <condition_variable> 24 #include <shared_mutex> 25 #include "local_ability_manager_stub.h" 26 #include "system_ability.h" 27 #include "thread_pool.h" 28 #include "parse_util.h" 29 #include "single_instance.h" 30 #include "system_ability_ondemand_reason.h" 31 #include "system_ability_status_change_stub.h" 32 #include "if_system_ability_manager.h" 33 #include "timer.h" 34 35 namespace OHOS { 36 // Check all dependencies's availability before the timeout period ended, [200, 60000]. 37 const int32_t MIN_DEPENDENCY_TIMEOUT = 200; 38 const int32_t MAX_DEPENDENCY_TIMEOUT = 60000; 39 const int32_t DEFAULT_DEPENDENCY_TIMEOUT = 6000; 40 41 enum ListenerState { 42 INIT, 43 NOTIFIED 44 }; 45 46 class LocalAbilityManager : public LocalAbilityManagerStub { 47 DECLARE_SINGLE_INSTANCE_BASE(LocalAbilityManager); 48 49 public: 50 bool AddAbility(SystemAbility* ability); 51 bool RemoveAbility(int32_t systemAbilityId); 52 SystemAbility* GetAbility(int32_t systemAbilityId); 53 bool GetRunningStatus(int32_t systemAbilityId); 54 bool AddSystemAbilityListener(int32_t systemAbilityId, int32_t listenerSaId); 55 bool RemoveSystemAbilityListener(int32_t systemAbilityId, int32_t listenerSaId); 56 std::vector<int32_t> CheckDependencyStatus(const std::vector<int32_t>& dependSas); 57 void StartSystemAbilityTask(SystemAbility* sa); 58 bool CheckSystemAbilityManagerReady(); 59 bool InitSystemAbilityProfiles(const std::string& profilePath, int32_t saId); 60 void ClearResource(); 61 void StartOndemandSystemAbility(int32_t systemAbilityId); 62 void StopOndemandSystemAbility(int32_t systemAbilityId); 63 bool StartAbility(int32_t systemAbilityId, const std::string& eventStr) override; 64 bool ActiveAbility(int32_t systemAbilityId, 65 const nlohmann::json& activeReason) override; 66 bool IdleAbility(int32_t systemAbilityId, const nlohmann::json& idleReason, 67 int32_t& delayTime) override; 68 bool StopAbility(int32_t systemAbilityId, const std::string& eventStr) override; 69 void DoStartSAProcess(const std::string& profilePath, int32_t saId); 70 void SetStartReason(int32_t systemAbilityId, const nlohmann::json& event); 71 void SetStopReason(int32_t systemAbilityId, const nlohmann::json& event); 72 nlohmann::json GetStartReason(int32_t systemAbilityId); 73 nlohmann::json GetStopReason(int32_t systemAbilityId); 74 SystemAbilityOnDemandReason JsonToOnDemandReason(const nlohmann::json& reasonJson); 75 bool SendStrategyToSA(int32_t type, int32_t systemAbilityId, int32_t level, std::string& action) override; 76 bool IpcStatCmdProc(int32_t fd, int32_t cmd) override; 77 bool FfrtDumperProc(std::string& result) override; 78 int32_t SystemAbilityExtProc(const std::string& extension, int32_t said, 79 SystemAbilityExtensionPara* callback, bool isAsync = false) override; 80 81 private: 82 LocalAbilityManager(); 83 ~LocalAbilityManager(); 84 85 bool AddLocalAbilityManager(); 86 void RegisterOnDemandSystemAbility(int32_t saId); 87 void FindAndStartPhaseTasks(int32_t saId); 88 void StartPhaseTasks(const std::list<SystemAbility*>& startTasks); 89 void CheckTrustSa(const std::string& path, const std::string& process, const std::list<SaProfile>& saInfos); 90 sptr<ISystemAbilityStatusChange> GetSystemAbilityStatusChange(); 91 void FindAndNotifyAbilityListeners(int32_t systemAbilityId, const std::string& deviceId, int32_t code); 92 void NotifyAbilityListener(int32_t systemAbilityId, int32_t listenerSaId, 93 const std::string& deviceId, int32_t code); 94 void WaitForTasks(); 95 void StartDependSaTask(SystemAbility* ability); 96 class SystemAbilityListener : public SystemAbilityStatusChangeStub { 97 public: 98 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 99 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 100 }; 101 102 bool CheckAndGetProfilePath(const std::string& profilePath, std::string& realProfilePath); 103 bool InitializeSaProfiles(int32_t saId); 104 bool InitializeOnDemandSaProfile(int32_t saId); 105 bool InitializeRunOnCreateSaProfiles(uint32_t bootPhase); 106 bool InitializeSaProfilesInnerLocked(const SaProfile& saProfile); 107 bool Run(int32_t saId); 108 bool NeedRegisterOnDemand(const SaProfile& saProfile, int32_t saId); 109 bool OnStartAbility(int32_t systemAbilityId); 110 bool OnStopAbility(int32_t systemAbilityId); 111 std::string GetTraceTag(const std::string& profilePath); 112 bool IsResident(); 113 bool IsConfigUnused(); 114 void InitUnusedCfg(); 115 void StartTimedQuery(); 116 void IdentifyUnusedResident(); 117 void IdentifyUnusedOndemand(); 118 bool NoNeedCheckUnused(int32_t saId); 119 void LimitUnusedTimeout(int32_t saId, int32_t timeout); 120 bool GetSaLastRequestTime(const sptr<ISystemAbilityManager>& samgr, int32_t saId, uint64_t& lastRequestTime); 121 122 std::map<int32_t, SystemAbility*> localAbilityMap_; 123 std::map<uint32_t, std::list<SystemAbility*>> abilityPhaseMap_; 124 std::shared_mutex localAbilityMapLock_; 125 sptr<LocalAbilityManager> localAbilityManager_; 126 std::map<int32_t, nlohmann::json> saIdToStartReason_; 127 std::map<int32_t, nlohmann::json> saIdToStopReason_; 128 // Max task number in pool is 20. 129 const int32_t MAX_TASK_NUMBER = 20; 130 // Check dependent sa status every 50 ms, it equals 50000us. 131 const int32_t CHECK_DEPENDENT_SA_PERIOD = 50000; 132 133 std::mutex listenerLock_; 134 sptr<ISystemAbilityStatusChange> statusChangeListener_; 135 std::map<int32_t, std::list<std::pair<int32_t, ListenerState>>> localListenerMap_; 136 std::mutex ReasonLock_; 137 std::shared_ptr<ParseUtil> profileParser_; 138 139 std::condition_variable startPhaseCV_; 140 std::mutex startPhaseLock_; 141 int32_t startTaskNum_ = 0; 142 std::u16string procName_; 143 int64_t startBegin_ = 0; 144 145 // Thread pool used to start system abilities in parallel. 146 std::unique_ptr<ThreadPool> initPool_; 147 std::unique_ptr<Utils::Timer> idleTimer_; 148 // longtime-unusedtimeout map 149 std::shared_mutex unusedCfgMapLock_; 150 std::map<int32_t, int32_t> unusedCfgMap_; 151 }; 152 } 153 #endif 154